GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

m_aws   D
last analyzed

Complexity

Total Complexity 122

Size/Duplication

Total Lines 801
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 801
rs 4.4444
wmc 122
lcom 1
cbo 1

34 Methods

Rating   Name   Duplication   Size   Complexity  
A m_aws() 0 2 1
A hook_menu() 0 10 1
A alternc_password_policy() 0 3 1
A alternc_module_description() 0 3 1
B get_list() 0 31 5
B get_stats_details() 0 27 3
A host_list() 0 21 3
A prefix_list() 0 10 2
A select_prefix_list() 0 9 3
A select_host_list() 0 11 4
A check_host_available() 0 15 4
A get_hostaliases() 0 12 3
A put_stats_details() 0 17 4
A delete_stats() 0 17 2
C add_stats() 0 43 13
A list_login() 0 14 3
A list_allowed_login() 0 13 3
A get_view_public() 0 11 2
A login_exists() 0 10 2
A del_login() 0 12 2
A add_login() 0 17 3
A change_pass() 0 16 3
B allow_login() 0 29 6
A delete_allowed_login() 0 16 3
B deny_login() 0 29 6
A alternc_del_member() 0 16 3
A alternc_del_domain() 0 16 3
A hook_quota_get() 0 10 2
B _check() 0 22 4
A _delconf() 0 8 2
B _createconf() 0 49 6
A _createhtpasswd() 0 15 3
B alternc_export() 0 24 5
F crypt_apr1_md5() 0 27 11

How to fix   Complexity   

Complex Class

Complex classes like m_aws often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use m_aws, and based on these observations, apply Extract Interface, too.

1
<?php
2
/*
3
 ----------------------------------------------------------------------
4
 AlternC - Web Hosting System
5
 Copyright (C) 2000-2012 by the AlternC Development Team. 
6
 https://alternc.org/ 
7
 ----------------------------------------------------------------------
8
 LICENSE
9
10
 This program is free software; you can redistribute it and/or
11
 modify it under the terms of the GNU General Public License (GPL)
12
 as published by the Free Software Foundation; either version 2
13
 of the License, or (at your option) any later version.
14
15
 This program is distributed in the hope that it will be useful,
16
 but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 GNU General Public License for more details.
19
20
 To read the license please visit http://www.gnu.org/copyleft/gpl.html
21
 ----------------------------------------------------------------------
22
 Purpose of file: Gestion des statistiques web par Awstats
23
 ----------------------------------------------------------------------
24
*/
25
/**
26
* This class manage awstats statistic sets.
27
* 
28
* Copyleft {@link http://alternc.org/ AlternC Team}
29
* 
30
* @copyright    AlternC-Team 2004-09-01 http://alternc.org/
31
* 
32
*/
33
class m_aws {
34
35
36
  /** Where are the awstats configuration files : 
37
   * @access private 
38
   */
39
  var $CONFDIR="/etc/awstats";
40
  var $HTAFILE="/etc/alternc/awstats.htpasswd";
41
  var $CACHEDIR="/var/cache/awstats";
42
43
44
  /** Where is the template for conf files :
45
   * @access private 
46
   */
47
  var $TEMPLATEFILE="/etc/alternc/templates/awstats/awstats.template.conf";
48
49
50
  /* ----------------------------------------------------------------- */
51
  /**
52
   * Constructor
53
   */
54
  function m_aws() {
55
  }
56
57
  function hook_menu() {
58
    $obj = array(
59
      'title'       => _("Web Statistics"),
60
      'ico'         => 'images/stat.png',
61
      'link'        => 'aws_list.php',
62
      'pos'         => 80,
63
     ) ;
64
65
     return $obj;
66
  }
67
68
  /* ----------------------------------------------------------------- */  
69
  /**
70
   * Password kind used in this class (hook for admin class)
71
   */
72
  function alternc_password_policy() {
73
    return array("aws"=>"Awstats Web Statistics");
74
  }
75
76
77
  /* ----------------------------------------------------------------- */
78
  /**
79
   * Name of the module function
80
   */
81
  function alternc_module_description() {
82
    return array("aws"=>_("The stats module allows any user to ask for statistics about his web site. Statistics are web pages generated daily based on the visits of the day before. Awstats is the soft used to produce those stats. The statistics are then protected by a login and a password."));
83
  } 
84
85
86
  /* ----------------------------------------------------------------- */
87
  /**
88
   * Returns an array with all the statistics of a member.
89
   *
90
   * @return array Returns an indexed array of associative arrays 
91
   * like that :
92
   *  $r[0-n]["id"] = Id of the stat set
93
   *  $r[0-n]["hostname"]= domain
94
   *  $r[0-n]["users"]= list of allowed users separated with ' '
95
   */
96
  function get_list() {
97
    global $db,$err,$cuid;
98
    $err->log("aws","get_list");
99
    $r=array();
100
    $db->query("SELECT id, hostname FROM aws WHERE uid='$cuid' ORDER BY hostname;");
101
    if ($db->num_rows()) {
102
      while ($db->next_record()) {
103
	$r[]=array(
104
		   "id"=>$db->f("id"),
105
		   "hostname"=>$db->f("hostname")
106
		   );
107
      }
108
      $t=array();
109
      foreach ($r as $v) {
110
	$db->query("SELECT login FROM aws_access WHERE id='".$v["id"]."';");
111
	$u="";
112
	while ($db->next_record()) {
113
		$u.=$db->f("login")." ";
114
	}
115
	$t[]=array(
116
        "id"=>$v["id"],
117
        "hostname"=>$v["hostname"],
118
        "users"=>$u
119
    );
120
      }
121
      return $t;
122
    } else {
123
      $err->raise("aws",_("No statistics currently defined"));
124
      return false;
125
    }
126
  }
127
128
129
  /* ----------------------------------------------------------------- */
130
  /**
131
   * Return an array with the details for 1 statistic set 
132
   *
133
   * @param integer $id ID of the set we want.
134
   * @return array Returns an associative array as follow : 
135
   *  $r["id"] = Id
136
   *  $r["hostname"]= domain
137
   *  $r["users"] = List of allowed users, separated by ' '
138
   */
139
  function get_stats_details($id) {
140
    global $db,$err,$cuid;
141
    $err->log("aws","get_stats_details",$id);
142
    $db->query("SELECT id, hostname, hostaliases, public FROM aws WHERE uid='$cuid' AND id='$id';");
143
    if ($db->num_rows()) {
144
      $db->next_record();
145
      $id=$db->f("id");
146
      $hostname=$db->f("hostname");
147
      $hostaliases=$db->f("hostaliases");
148
      $public=$db->f("public");
149
      $db->query("SELECT login FROM aws_access WHERE id='$id';");
150
      $u="";
151
      while ($db->next_record()) {
152
	$u.=$db->f("login")." ";
153
      }
154
      return array(
155
		"id"=>$id,
156
		"hostname"=>$hostname,
157
		"users"=>$u,
158
                "hostaliases"=>$hostaliases,
159
                "public"=>$public
160
		   );
161
    } else {
162
      $err->raise("aws",_("This statistic does not exist"));
163
      return false;
164
    }
165
  }
166
167
168
/* ----------------------------------------------------------------- */
169
  /** Return the list of domains / subdomains allowed for this member with the type (MX,URL,...)
170
   * 
171
   * @return array an array of allowed domains / subdomains.
172
   */
173
  function host_list() {
174
    global $db,$err,$cuid;
175
    $r=array();
176
    $db->query("SELECT sd.domaine, sd.sub, dt.name, dt.description FROM sub_domaines sd, domaines_type dt WHERE compte='$cuid' AND lower(sd.type) = lower(dt.name) AND dt.only_dns = false ORDER BY domaine,sub;");
177
    while ($db->next_record()) {
178
      if ($db->f("sub")) {
179
        $r[]=array(
180
            "hostname"=>$db->f("sub").".".$db->f("domaine"),
181
            "type"=>$db->f("name"),
182
            "desc"=>$db->f("description")
183
        );
184
      } else {
185
        $r[]=array(
186
            "hostname"=>$db->f("domaine"),
187
            "type"=>$db->f("name"),
188
            "desc"=>$db->f("description")
189
        );
190
      }
191
    }
192
    return $r;
193
  }
194
195
196
  /* ----------------------------------------------------------------- */
197
  /** Returns the list of prefixes that can be used on current account
198
   * @return array an arry with the list of domains + the login of the account
199
   */
200
  function prefix_list() {
201
    global $db,$mem,$cuid;
202
    $r=array();
203
    $r[]=$mem->user["login"];
204
    $db->query("SELECT domaine FROM domaines WHERE compte='$cuid' ORDER BY domaine;");
205
    while ($db->next_record()) {
206
      $r[]=$db->f("domaine");
207
    }
208
    return $r;
209
  }
210
211
212
  /* ----------------------------------------------------------------- */
213
  /** Echoes <option> tags of all the domains hosted on the account + the login of the account
214
   * They can be used as a root for the login that may have access to web statistics
215
   * hosted on an account
216
   * $current will be the selected value.
217
   * @param string $current The default selected value
218
   * @return boolean TRUE.
219
   */
220
  function select_prefix_list($current) {
221
    $r=$this->prefix_list();
222
    reset($r);
223
    while (list($key,$val)=each($r)) {
224
      if ($current==$val) $c=" selected=\"selected\""; else $c="";
225
      echo "<option$c>$val</option>";
226
    }
227
    return true;
228
  }
229
230
231
  /* ----------------------------------------------------------------- */
232
  /** 
233
   * Draw options for a select html code with the list of allowed and availables domains
234
   * for this member.
235
   */
236
  function select_host_list($current) {
237
    $r=$this->host_list();
238
    reset($r);
239
    while (list($key,$val)=each($r)) {
240
      $ho=$val["hostname"];
241
      $ty=$val["desc"];
242
      if ($current==$ho) $c=" selected=\"selected\""; else $c="";
243
      if ($this->check_host_available($ho)) echo "<option value=\"$ho\"$c>$ho ("._($ty).")</option>";
244
    }
245
    return true;
246
  }
247
248
249
  /* ----------------------------------------------------------------- */
250
  /** 
251
   * Check if hosts is already used by awstats
252
   * of available for this member.
253
   */
254
  function check_host_available($current) {
255
    global $err;
256
    $err->log("aws","check_host_available",$current);
257
    $r=$this->get_list();
258
    if(is_array($r)){
259
        reset($r);
260
        while (list($key,$val)=each($r)) {
261
          if ($current==$val["hostname"]) {
262
              $err->raise("aws",_("Host already managed by awstats!"));
263
              return false;
264
          }
265
        }
266
    }
267
    return true;
268
  }
269
270
271
  /* ----------------------------------------------------------------- */
272
  /** 
273
   * Return the hostaliases list with an id.
274
   */
275
  function get_hostaliases($id) {
276
    global $db,$err,$cuid;
277
    $r=array();
278
    if ($id == NULL)  
279
      return $r; 
280
    $db->query("SELECT hostaliases FROM aws WHERE uid='$cuid' and id='$id' ORDER by hostaliases;");
281
    while ($db->next_record()) {
282
      $r[]=$db->f("hostaliases");
283
    }
284
    
285
    return $r;
286
  }
287
288
289
  /* ----------------------------------------------------------------- */
290
  /** 
291
   * Edit a statistic set (change allowed user list)
292
   * @param integer $id the stat number we change
293
   * @param array $users the list of allowed users
294
   */
295
  function put_stats_details($id,$users,$hostaliases,$public) {
296
    global $err,$db,$cuid;
297
    if ($this->get_stats_details($id)) {
298
      $this->delete_allowed_login($id, 1);
299
      if (is_array($users)) {
300
        foreach($users as $v) {
301
          $this->allow_login($v,$id,1);
302
	}
303
      }
304
      $db->query("UPDATE aws SET hostaliases='$hostaliases', public='$public' where id='$id';");
305
      $this->_createconf($id);
306
      $this->_createhtpasswd();
307
      return true;
308
    } else {
309
      return false;
310
    }
311
  }
312
313
314
  /* ----------------------------------------------------------------- */
315
  /** 
316
   * Delete a statistic set.
317
   * @param integer $id The statistic set ID
318
   * @return string the domain name of the deleted statistic set, or FALSE if an error occurred
319
   */
320
  function delete_stats($id) {
321
    global $db,$err,$cuid,$action;
322
    $err->log("aws","delete_stats",$id);
323
    $db->query("SELECT hostname FROM aws WHERE id='$id' and uid='$cuid';");
324
    if (!$db->num_rows()) {
325
      $err->raise("aws",_("This statistic does not exist"));
326
      return false;
327
    }
328
    $db->next_record();
329
    $hostname=$db->f("hostname");
330
    $this->delete_allowed_login($id,1);
331
    $this->_delconf($hostname);
332
    $db->query("DELETE FROM aws WHERE id='$id'");
333
    $action->del($this->CACHEDIR. DIRECTORY_SEPARATOR . $hostname . DIRECTORY_SEPARATOR);
334
    return $hostname;
335
336
  }
337
338
339
  /* ----------------------------------------------------------------- */
340
  /** 
341
   * Create a new statistic set
342
   * @param string $hostname The domain name
343
   * @param $uers array the array of users that will be allowed
344
   * @param $hostaliases array an array of host aliases
345
   * @param $public boolean Shall this statistic set be public ? 
346
   * @return boolean TRUE if the set has been created
347
   */
348
  function add_stats($hostname,$users="", $hostaliases,$public) {
349
    global $db,$err,$quota,$mem,$cuid;
350
    $err->log("aws","add_stats",$hostname);
351
    $ha="";
352
    $r=$this->host_list();
353
    $hosts=array();
354
    foreach ($r as $key=>$val) {
355
        $hosts[]=$val["hostname"];
356
    }
357
    reset($hosts);
358
    if (!in_array($hostname,$hosts) || $hostname=="") {
359
      $err->raise("aws",_("This hostname does not exist (Domain name)"));
360
      return false;
361
    }
362
363
    // Parse the hostaliases array (it should contains valid domains)
364
	if (is_array($hostaliases)) {
365
		foreach($hostaliases as $ho) {
366
			if (!in_array($ho,$hosts) || $hostname=="") {
367
				$err->raise("aws",_("This hostname does not exist (Hostaliases)"));
368
				return false;
369
			}
370
			$ha .= "$ho ";
371
		}
372
    }
373
374
    if ($quota->cancreate("aws")) {
375
      $db->query("INSERT INTO aws (hostname,uid,hostaliases,public) VALUES ('$hostname','$cuid','$ha','$public')");
376
      $id=$db->lastid();
377
      if (is_array($users)) {
378
        foreach($users as $v) {
379
          $this->allow_login($v,$id, 1);
380
        }
381
      }
382
      if (!$this->_createconf($id) ) return false;
383
      if (!$this->_createhtpasswd() ) return false;
384
      mkdir($this->CACHEDIR."/".$hostname,0777);
385
      return true;
386
    } else {
387
      $err->raise("aws",_("Your stat quota is over..."));
388
      return false;
389
    }
390
  }
391
392
393
  /* ----------------------------------------------------------------- */
394
  function list_login() {
395
    global $db,$err,$cuid;
396
    $err->log("aws","list_login");
397
    $db->query("SELECT login FROM aws_users WHERE uid='$cuid';");
398
    $res=array();
399
    if (!$db->next_record()) {
400
	$err->raise("aws",_("No user currently defined"));
401
      return false;
402
    }
403
    do { 
404
      $res[]=$db->f("login");
405
    } while ($db->next_record());
406
    return $res;
407
  }
408
409
410
  /* ----------------------------------------------------------------- */
411
  function list_allowed_login($id) {
412
    global $db,$err,$cuid;
413
    $err->log("aws","list_allowed_login");
414
    $db->query("SELECT u.login,a.id FROM aws_users u LEFT JOIN aws_access a ON a.id='$id' AND a.login=u.login WHERE u.uid='$cuid';");
415
    $res=array();
416
    if (!$db->next_record()) {
417
      return false;
418
    }
419
    do { 
420
      $res[]=array("login"=>$db->f("login"),"selected"=>($db->f("id")));
421
    } while ($db->next_record());
422
    return $res;
423
  }
424
425
  /* ----------------------------------------------------------------- */
426
  function get_view_public($id) {
427
    global $db,$err,$cuid;
428
    $db->query("SELECT public FROM aws WHERE id='$id' and uid='$cuid';");
429
    if ($db->num_rows()) {
430
      $db->next_record();
431
      $pub=$db->f("public");
432
    } else {
433
	  $pub=1;
434
	}
435
    return $pub;
436
  }
437
438
439
  /* ----------------------------------------------------------------- */
440
  /* Check that a login exists ($exists=1) or doesn't exist ($exists=0) */
441
  function login_exists($login,$exists=1) {
442
    global $db,$err,$cuid;
443
    $err->log("aws","list_login");
444
    $db->query("SELECT login FROM aws_users WHERE uid='$cuid' AND login='$login';");
445
    if (!$db->next_record()) {
446
      return ($exists==0);
447
    } else {
448
      return ($exists==1);
449
    }
450
  }
451
452
453
  /* ----------------------------------------------------------------- */
454
  function del_login($login) {
455
    global $db,$err,$cuid;
456
    $err->log("aws","del_login");
457
    if (!$this->login_exists($login,1)) {
458
      $err->raise("aws",_("Login does not exist")); 
459
      return false;
460
    }
461
    $db->query("DELETE FROM aws_users WHERE uid='$cuid' AND login='$login';");
462
    $db->query("DELETE FROM aws_access WHERE uid='$cuid' AND login='$login';");
463
    $this->_createhtpasswd();
464
    return true;
465
  }
466
467
468
  /* ----------------------------------------------------------------- */
469
  function add_login($login,$pass) {
470
    global $db,$err,$cuid;
471
    $err->log("aws","add_login");
472
473
    if (!($login=$this->_check($login))) {
474
      $err->raise("aws",_("Login incorrect")); 
475
      return false;
476
    }
477
    if ($this->login_exists($login,1)) {
478
      $err->raise("aws",_("Login already exist")); 
479
      return false;
480
    }
481
    $pass=$this->crypt_apr1_md5($pass);
482
    // FIXME retourner une erreur l'insert se passe pas bien
483
    $db->query("INSERT INTO aws_users (uid,login,pass) VALUES ('$cuid','$login','$pass');");
484
    return $this->_createhtpasswd();
485
  }
486
487
488
  /* ----------------------------------------------------------------- */
489
  function change_pass($login,$pass) {
490
    global $db,$err,$cuid;
491
    $err->log("aws","change_pass");
492
493
    if (!($login=$this->_check($login))) {
494
      $err->raise("aws",_("Login incorrect")); // Login incorrect 
495
      return false;
496
    }
497
    if (!($this->login_exists($login))) {
498
      $err->raise("aws",_("Login does not exists")); // Login does not exists
499
      return false;
500
    }
501
    $pass=$this->crypt_apr1_md5($pass);
502
    $db->query("UPDATE aws_users SET pass='$pass' WHERE login='$login';");
503
    return $this->_createhtpasswd();
504
  }
505
506
507
  /* ----------------------------------------------------------------- */
508
  function allow_login($login,$id,$noconf=0) { // allow user $login to access stats $id.
509
    global $db,$err,$cuid;
510
    $err->log("aws","allow_login");
511
512
    if (!($login=$this->_check($login))) {
513
      $err->raise("aws",_("Login incorrect"));
514
      return false;      
515
    }
516
    if (!$this->login_exists($login)) {
517
      $err->raise("aws",_("Login does not exist"));
518
      return false;
519
    }
520
    $db->query("SELECT id FROM aws WHERE id='$id' AND uid='$cuid'");
521
    if (!$db->next_record()) {
522
      $err->raise("aws",_("The requested statistic does not exist.")); 
523
      return false;
524
    }
525
    $db->query("SELECT login FROM aws_access WHERE id='$id' AND login='$login'");
526
    if ($db->next_record()) {
527
      $err->raise("aws",_("This login is already allowed for this statistics."));
528
      return false;
529
    }
530
    $db->query("INSERT INTO aws_access (uid,id,login) VALUES ('$cuid','$id','$login');");
531
    if (!$noconf) { 
532
      $this->_createconf($id); 
533
      $this->_createhtpasswd();
534
    }
535
    return true;
536
  }
537
538
539
  /* ----------------------------------------------------------------- */
540
541
  /**
542
   * @param integer $id
543
   */
544
  function delete_allowed_login($id,$noconf=0) {
545
    global $db,$err,$cuid;
546
    $err->log("aws","delete_allowed_login");
547
548
    $db->query("SELECT id FROM aws WHERE id='$id' AND uid='$cuid'");
549
    if (!$db->next_record()) {
550
      $err->raise("aws",_("The requested statistic does not exist.")); 
551
      return false;
552
    }
553
    $db->query("DELETE FROM aws_access WHERE id='$id';");
554
    if (!$noconf) { 
555
      $this->_createconf($id); 
556
      $this->_createhtpasswd();
557
    }
558
    return true;
559
  }
560
561
562
  /* ----------------------------------------------------------------- */
563
  function deny_login($login,$id,$noconf=0) { // deny user $login to access stats $id.
564
    global $db,$err,$cuid;
565
    $err->log("aws","deny_login");
566
567
    if (!($login=$this->_check($login))) {
568
      $err->raise("aws",_("Login incorrect")); // Login incorrect 
569
      return false;
570
    }
571
    if (!$this->login_exists($login,0)) {
572
      $err->raise("aws",_("Login does not exists")); // Login does not exists
573
      return false;
574
    }
575
    $db->query("SELECT id FROM aws WHERE id='$id' AND uid='$cuid'");
576
    if (!$db->next_record()) {
577
      $err->raise("aws",_("The requested statistic does not exist."));
578
      return false;
579
    }
580
    $db->query("SELECT login FROM aws_access WHERE id='$id' AND login='$login'");
581
    if (!$db->next_record()) {
582
      $err->raise("aws",_("This login is already denied for this statistics."));
583
      return false;
584
    }
585
    $db->query("DELETE FROM aws_access WHERE id='$id' AND login='$login';");
586
    if (!$noconf) { 
587
      $this->_createconf($id); 
588
      $this->_createhtpasswd();
589
    }
590
    return true;
591
  }
592
593
594
  /* ----------------------------------------------------------------- */
595
  function alternc_del_member() {
596
    global $db,$err,$cuid;
597
    $err->log("aws","del_member");
598
    $db->query("SELECT * FROM aws WHERE uid='$cuid';");
599
    $t=array();
600
    while ($db->next_record()) {
601
      $t[]=$db->f("hostname");
602
    }
603
    $db->query("DELETE FROM aws WHERE uid='$cuid';");
604
    foreach ($t as $i) {
605
      $this->_delconf($i);
606
    }
607
    $db->query("DELETE FROM aws_access WHERE uid='$cuid'");
608
    $db->query("DELETE FROM aws_users WHERE uid='$cuid';");
609
    return $this->_createhtpasswd();
610
  }
611
612
613
  /* ----------------------------------------------------------------- */
614
  /** 
615
   * This function is called on each class when a domain name is uninstalled
616
   * @param string $dom the domain to uninstall
617
   */
618
  function alternc_del_domain($dom) {
619
    global $err,$cuid;
620
    $err->log("aws","alternc_del_domain",$dom);
621
    $db=new DB_System();
622
    $db->query("SELECT id,hostname FROM aws WHERE uid='$cuid' AND (hostname='$dom' OR hostname like '%.$dom')");
623
    $t=array();
624
    while ($db->next_record()) {
625
      $t[]=array($db->f("hostname"),$db->f("id"));
626
    }
627
    foreach ($t as $i) {
628
      $db->query("DELETE FROM aws WHERE uid='$cuid' AND hostname='".$i[0]."';");
629
      $db->query("DELETE FROM aws_access WHERE uid='$cuid' AND id='".$i[1]."';");
630
      $this->_delconf($i[0]);
631
    }
632
    return $this->_createhtpasswd();
633
  }
634
635
636
  /* ----------------------------------------------------------------- */
637
  /** 
638
   * This function is called when we are asked to compute the used quota
639
   * for a service
640
   */
641
  function hook_quota_get() {
642
    global $db,$err,$cuid;
643
    $err->log("aws","get_quota");
644
    $db->query("SELECT COUNT(*) AS cnt FROM aws WHERE uid='$cuid'");
645
    $q=Array("name"=>"aws", "description"=>_("Awstats"), "used"=>0);
646
    if ($db->next_record()) {
647
      $q['used']=$db->f("cnt");
648
    }
649
    return $q; 
650
  }
651
652
653
  /* ----------------------------------------------------------------- */
654
  function _check($login) {
655
    global $err,$mem;
656
    $login=trim($login); 
657
    $login=strtolower($login); 
658
    if ($c=strpos($login,"_")) {
659
	$prefix=substr($login,0,$c);
660
	$postfix=substr($login,$c+1);
661
    } else {
662
	$prefix=$login;
663
	$postfix="";
664
    }
665
    $r=$this->prefix_list();
666
    if (!in_array($prefix,$r)) { 
667
      $err->raise("aws",_("prefix not allowed.")); // prefix not allowed. 
668
      return false;
669
    } 
670
   if (!preg_match('/^[0-9a-z_-]*$/', $postfix)){
671
      $err->raise("aws",_("Forbidden caracters in the postfix.")); 
672
      return false;
673
    }
674
    return $login;
675
  }
676
677
678
  /* ----------------------------------------------------------------- */
679
  /** Delete the awstats configuration file for a statistic set.
680
   * @access private
681
   */
682
  function _delconf($hostname) {
683
    global $err,$action;
684
    if (!preg_match('/^[._a-z0-9-]*$/', $hostname)){
685
      $err->raise("aws",_("Hostname is incorrect")); 
686
      return false;
687
    }
688
    $action->del($this->CONFDIR. DIRECTORY_SEPARATOR . "awstats.".$hostname.".conf");
689
  }
690
691
692
  /* ----------------------------------------------------------------- */
693
  /** Create a configuration file for a statistic set.
694
   * if nochk==1, does not check the owner of the stat set (for admin only)
695
   * @access private
696
   */
697
  function _createconf($id,$nochk=0) {
698
    global $db,$err,$cuid,$L_ALTERNC_LOGS;
699
    $s=@implode("",file($this->TEMPLATEFILE));
700
    if (!$s) {
701
      $err->raise("aws",_("Problem to create the configuration"));
702
      return false;
703
    }
704
    if ($nochk) {
705
        $db->query("SELECT * FROM aws WHERE id='$id';");
706
    } else { 
707
        $db->query("SELECT * FROM aws WHERE id='$id' AND uid='$cuid';");
708
    }
709
    if (!$db->num_rows()) {
710
      $err->raise("aws",_("This statistic does not exist")); 
711
      return false;
712
    }
713
    $db->next_record();
714
    $uid = $db->f('uid');
715
    $hostname=$db->f("hostname");
716
    $hostaliases=$db->f("hostaliases");
717
    $public=$db->f("public");
718
    $db->query("SELECT login FROM membres WHERE uid = '$uid'");
719
    $db->next_record();
720
    $username = $db->f('login');
721
    $db->query("SELECT login FROM aws_access WHERE id='$id';");
722
    $users="";
723
    while ($db->next_record()) {
724
        $users.=$db->f("login")." ";
725
    }
726
727
    $replace_vars = array(
728
        '%%UID%%' => $uid,
729
        '%%USER%%' => $username,
730
        '%%ALTERNC_LOGS%%' => $L_ALTERNC_LOGS,
731
        '%%PUBLIC%%' => $public,
732
        '%%HOSTNAME%%' => $hostname,
733
        '%%HOSTALIASES%%' => $hostaliases,
734
        '%%USERS%%' => $users,
735
    );
736
    foreach ($replace_vars as $k=>$v){
737
        $s=str_replace($k,$v,$s);
738
    }
739
740
    $f=fopen($this->CONFDIR."/awstats.".$hostname.".conf","wb");
741
    fputs($f,$s,strlen($s));
742
    fclose($f);
743
744
    return true;
745
  }
746
747
748
  /* ----------------------------------------------------------------- */
749
  function _createhtpasswd() {
750
    global $db, $err;
751
    $f=@fopen($this->HTAFILE,"wb");
752
    if ($f) {
753
      $db->query("SELECT login,pass FROM aws_users;");
754
      while ($db->next_record()) {
755
        fputs($f,$db->f("login").":".$db->f("pass")."\n");
756
      }
757
      fclose($f);
758
      return true;
759
    } else {
760
      $err->raise("aws",sprintf(_("Problem to edit file %s"), $this->HTAFILE));
761
      return false;
762
    }
763
  }
764
765
766
  /* ----------------------------------------------------------------- */
767
  /**
768
   * Exports all the aws related information for an account.
769
   * @access private
770
   * EXPERIMENTAL 'sid' function ;) 
771
   */
772
  function alternc_export() {
773
    global $db,$err,$cuid;
774
    $err->log("aws","export");
775
    $str="<aws>\n";
776
    $db->query("SELECT login,pass FROM aws_users WHERE uid='$cuid';");
777
    while ($db->next_record()) {
778
      $str.="  <user><login>".$db->Record["login"]."</login><pass hash=\"des\">".$db->Record["pass"]."</pass></user>\n";
779
    }
780
    $r=array();
781
    $db->query("SELECT id, hostname FROM aws WHERE uid='$cuid' ORDER BY hostname;");
782
    while ($db->next_record()) {
783
      $r[$db->Record["id"]]=$db->Record["hostname"];
784
    }
785
    foreach($r as $id=>$host) {
786
      $str.="  <domain>\n    <name>".$host."</name>\n";
787
      $db->query("SELECT login FROM aws_access WHERE id='$id';");
788
      while ($db->next_record()) {
789
        $str.="    <user>".$db->Record["login"]."</user>\n";
790
      }
791
      $str.="  </domain>\n";
792
    }
793
    $str.="</aws>\n";
794
    return $str;
795
  }
796
797
798
  /* ----------------------------------------------------------------- */
799
  /**
800
   * from http://php.net/crypt#73619 
801
   */
802
  function crypt_apr1_md5($plainpasswd) {
803
    $salt = substr(str_shuffle("abcdefghijklmnopqrstuvwxyz0123456789"), 0, 8);
804
    $len = strlen($plainpasswd);
805
    $text = $plainpasswd.'$apr1$'.$salt;
806
    $bin = pack("H32", md5($plainpasswd.$salt.$plainpasswd));
807
    for($i = $len; $i > 0; $i -= 16) { $text .= substr($bin, 0, min(16, $i)); }
808
    for($i = $len; $i > 0; $i >>= 1) { $text .= ($i & 1) ? chr(0) : $plainpasswd{0}; }
809
    $bin = pack("H32", md5($text));
810
    for($i = 0; $i < 1000; $i++) {
811
      $new = ($i & 1) ? $plainpasswd : $bin;
812
      if ($i % 3) $new .= $salt;
813
      if ($i % 7) $new .= $plainpasswd;
814
      $new .= ($i & 1) ? $bin : $plainpasswd;
815
      $bin = pack("H32", md5($new));
816
    }
817
    for ($i = 0; $i < 5; $i++) {
818
      $k = $i + 6;
819
      $j = $i + 12;
820
      if ($j == 16) $j = 5;
821
      $tmp = $bin[$i].$bin[$k].$bin[$j].$tmp;
822
    }
823
    $tmp = chr(0).chr(0).$bin[11].$tmp;
824
    $tmp = strtr(strrev(substr(base64_encode($tmp), 2)),
825
		 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
826
		 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
827
    return "$"."apr1"."$".$salt."$".$tmp;
828
  }
829
830
831
832
833
} /* CLASSE m_aws */
834
835
?>
836