source: trunk/bureau/class/m_aws.php @ 1042

Revision 1042, 18.1 KB checked in by anonymous, 9 years ago (diff)

pleins d'autres corrections

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1<?php
2/*
3 $Id$
4 ----------------------------------------------------------------------
5 AlternC - Web Hosting System
6 Copyright (C) 2002 by the AlternC Development Team.
7 http://alternc.org/
8 ----------------------------------------------------------------------
9 Based on:
10 Valentin Lacambre's web hosting softwares: http://altern.org/
11 ----------------------------------------------------------------------
12 LICENSE
13
14 This program is free software; you can redistribute it and/or
15 modify it under the terms of the GNU General Public License (GPL)
16 as published by the Free Software Foundation; either version 2
17 of the License, or (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 GNU General Public License for more details.
23
24 To read the license please visit http://www.gnu.org/copyleft/gpl.html
25 ----------------------------------------------------------------------
26 Original Author of file: Benjamin Sonntag
27 Purpose of file: Gestion des statistiques web par Awstats
28 ----------------------------------------------------------------------
29*/
30/**
31* This class manage awstats statistic sets.
32*
33* Copyleft {@link http://alternc.org/ AlternC Team}
34*
35* @copyright    AlternC-Team 2004-09-01 http://alternc.org/
36*
37*/
38class m_aws {
39
40  /** Where are the awstats configuration files :
41   * @access private
42   */
43  var $CONFDIR="/etc/awstats";
44
45  var $CACHEDIR="/var/cache/awstats";
46
47  /** Where is the template for conf files :
48   * @access private
49   */
50  var $TEMPLATEFILE="/etc/alternc/awstats.template.conf";
51
52  /* ----------------------------------------------------------------- */
53  /**
54   * Constructor
55   */
56  function m_aws() {
57  }
58
59  /* ----------------------------------------------------------------- */
60  /**
61   * Quota's name
62   */
63  function alternc_quota_names() {
64    return "aws";
65  } 
66
67  /* ----------------------------------------------------------------- */
68  /**
69   * Name of the module function
70   */
71  function alternc_module_description() {
72    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."));
73  } 
74
75  /* ----------------------------------------------------------------- */
76  /**
77   * Returns an array with all the statistics of a member.
78   *
79   * @return array Returns an indexed array of associative arrays
80   * like that :
81   *  $r[0-n]["id"] = Id of the stat set
82   *  $r[0-n]["hostname"]= domain
83   *  $r[0-n]["users"]= list of allowed users separated with ' '
84   */
85  function get_list() {
86    global $db,$err,$cuid;
87    $err->log("aws","get_list");
88    $r=array();
89    $db->query("SELECT id, hostname FROM aws WHERE uid='$cuid' ORDER BY hostname;");
90    if ($db->num_rows()) {
91      while ($db->next_record()) {
92        $r[]=array(
93                   "id"=>$db->f("id"),
94                   "hostname"=>$db->f("hostname"),
95                   );
96      }
97      $t=array();
98      foreach ($r as $v) {
99        $db->query("SELECT login FROM aws_access WHERE id='".$v["id"]."';");
100        $u="";
101        while ($db->next_record()) {
102                $u.=$db->f("login")." ";
103        }
104        $t[]=array("id"=>$v["id"],"hostname"=>$v["hostname"],"users"=>$u);
105      }
106      return $t;
107    } else {
108      $err->raise("aws",1); // No statistics currently defined
109      return false;
110    }
111  }
112
113  /* ----------------------------------------------------------------- */
114  /**
115   * Return an array with the details for 1 statistic set
116   *
117   * @param integer $id ID of the set we want.
118   * @return array Returns an associative array as follow :
119   *  $r["id"] = Id
120   *  $r["hostname"]= domain
121   *  $r["users"] = List of allowed users, separated by ' '
122   */
123  function get_stats_details($id) {
124    global $db,$err,$cuid;
125    $err->log("aws","get_stats_details",$id);
126    $r=array();
127    $db->query("SELECT id, hostname FROM aws WHERE uid='$cuid' AND id='$id';");
128    if ($db->num_rows()) {
129      $db->next_record();
130      $id=$db->f("id");
131      $hostname=$db->f("hostname");
132      $db->query("SELECT login FROM aws_access WHERE id='$id';");
133      $u="";
134      while ($db->next_record()) {
135        $u.=$db->f("login")." ";
136      }
137      return array(
138                "id"=>$id,
139                "hostname"=>$hostname,
140                "users"=>$u
141                   );
142    } else {
143      $err->raise("aws",2); // This statistic does not exist
144      return false;
145    }
146  }
147
148  /* ----------------------------------------------------------------- */
149  /** Return the list of domains / subdomains allowed for this member
150   *
151   * @return array an array of allowed domains / subdomains.
152   */
153  function host_list() {
154    global $db,$err,$cuid;
155    $r=array();
156    $db->query("SELECT domaine,sub FROM sub_domaines WHERE compte='$cuid' ORDER BY domaine,sub;");
157    while ($db->next_record()) {
158      if ($db->f("sub")) {
159        $r[]=$db->f("sub").".".$db->f("domaine");
160      } else {
161        $r[]=$db->f("domaine");
162      }
163    }
164    return $r;
165  }
166  /* ----------------------------------------------------------------- */
167  /** Retourne la liste des prefixes utilisables par le compte courant
168   * @return array tableau contenant la liste des prefixes (domaines + login)
169   *  du compte actuel.
170   */
171  function prefix_list() {
172    global $db,$mem,$cuid;
173    $r=array();
174    $r[]=$mem->user["login"];
175    $db->query("SELECT domaine FROM domaines WHERE compte='$cuid' ORDER BY domaine;");
176    while ($db->next_record()) {
177      $r[]=$db->f("domaine");
178    }
179    return $r;
180  }
181
182  /* ----------------------------------------------------------------- */
183  /** Affiche (ECHO) la liste des prefixes disponibles sous forme de champs d'option
184   * Les champs sont affichés sous la forme <option>prefixe</option>...
185   * La valeur $current se voit affublée de la balise SELECTED.
186   * @param string $current Prefixe sélectionné par défaut
187   * @return boolean TRUE.
188   */
189  function select_prefix_list($current) {
190    $r=$this->prefix_list();
191    reset($r);
192    while (list($key,$val)=each($r)) {
193      if ($current==$val) $c=" selected=\"selected\""; else $c="";
194      echo "<option$c>$val</option>";
195    }
196    return true;
197  }
198
199  /* ----------------------------------------------------------------- */
200  /**
201   * Draw options for a select html code with the list of allowed domains
202   * for this member.
203   */
204  function select_host_list($current) {
205    $r=$this->host_list();
206    reset($r);
207    while (list($key,$val)=each($r)) {
208      if ($current==$val) $c=" selected=\"selected\""; else $c="";
209      echo "<option$c>$val</option>";
210    }
211    return true;
212  }
213
214  /* ----------------------------------------------------------------- */
215  /**
216   * Edit a statistic set (change allowed user list)
217   * @param integer $id the stat number we change
218   * @param array $users the list of allowed users
219   */
220  function put_stats_details($id,$users) {
221    global $err,$db,$cuid;
222    if ($c=$this->get_stats_details($id)) {
223      $this->delete_allowed_login($id);
224      if (is_array($users)) {
225        foreach($users as $v) {
226          $this->allow_login($v,$id,1);
227        }
228      }
229      $this->_createconf($c["hostname"]);
230      return true;
231    } else return false;
232  }
233
234  /* ----------------------------------------------------------------- */
235  /**
236   * Efface un jeu de statistiques existant.
237   * @param integer $id est le numéro du jeu de statistiques à supprimer
238   * @return string le nom du domaine du jeu ainsi effacé, ou FALSE si une erreur est survenue.
239   */
240  function delete_stats($id) {
241    global $db,$err,$quota,$cuid;
242    $err->log("aws","delete_stats",$id);
243    $db->query("SELECT hostname FROM aws WHERE id='$id' and uid='$cuid';");
244    if (!$db->num_rows()) {
245      $err->raise("aws",2); // This statistic does not exist
246      return false;
247    }
248    $db->next_record();
249    $hostname=$db->f("hostname");
250    $this->delete_allowed_login($id,1);
251    $this->_delconf($hostname);
252    $db->query("DELETE FROM aws WHERE id='$id'");
253    $quota->dec("aws");
254    system("rm ".$this->CACHEDIR."/$hostname/ -rf");
255    return $name;
256  }
257
258  /* ----------------------------------------------------------------- */
259  /**
260   * Cree un nouveau jeu de statistiques
261   * @param string $hostname est le nom de domaine sur lequel on fait des stats
262   * @return boolean TRUE si le jeu de stats a été créé avec succès, FALSE sinon.
263   */
264  function add_stats($hostname,$users="") {
265    global $db,$err,$quota,$mem,$cuid;
266    $err->log("aws","add_stats",$hostname);
267    $r=$this->host_list();
268    if (!in_array($hostname,$r) || $hostname=="") {
269      $err->raise("aws",3); // This hostname does not exist
270      return false;
271    }
272    if ($quota->cancreate("aws")) {
273      $quota->inc("aws");
274      $db->query("INSERT INTO aws (hostname,uid) VALUES ('$hostname','$cuid')");
275      $id=$db->lastid();
276      if (is_array($users)) {
277        foreach($users as $v) {
278          $this->allow_login($v,$id);
279        }
280      }
281      $this->_createconf($hostname);
282      return true;
283    } else {
284      $err->raise("aws",4); // Your stat quota is over...
285      return false;
286    }
287  }
288
289  /* ----------------------------------------------------------------- */
290  function list_login() {
291    global $db,$err,$cuid;
292    $err->log("aws","list_login");
293    $db->query("SELECT login FROM aws_users WHERE uid='$cuid';");
294    $res=array();
295    if (!$db->next_record()) {
296        $err->raise("aws",13); // No user currently defined
297      return false;
298    }
299    do { 
300      $res[]=$db->f("login");
301    } while ($db->next_record());
302    return $res;
303  }
304
305  /* ----------------------------------------------------------------- */
306  function list_allowed_login($id) {
307    global $db,$err,$cuid;
308    $err->log("aws","list_allowed_login");
309    $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='$id';");
310    $res=array();
311    if (!$db->next_record()) {
312      return false;
313    }
314    do { 
315      $res[]=array("login"=>$db->f("login"),"selected"=>($db->f("id")));
316    } while ($db->next_record());
317    return $res;
318  }
319
320  /* ----------------------------------------------------------------- */
321  /* Check that a login exists ($exists=1) or doesn't exist ($exists=0) */
322  function login_exists($login,$exists=1) {
323    global $db,$err,$cuid;
324    $err->log("aws","list_login");
325    $db->query("SELECT login FROM aws_users WHERE uid='$cuid' AND login='$login';");
326    if (!$db->next_record()) {
327      return ($exists==0);
328    } else {
329      return ($exists==1);
330    }
331  }
332
333  /* ----------------------------------------------------------------- */
334  function del_login($login) {
335    global $db,$err,$cuid;
336    $err->log("aws","del_login");
337    if (!$this->login_exists($login,1)) {
338      $err->raise("aws",5); // Login does not exists
339      return false;
340    }
341    $db->query("DELETE FROM aws_users WHERE uid='$cuid' AND login='$login';");
342    return true;
343  }
344
345  /* ----------------------------------------------------------------- */
346  function add_login($login,$pass) {
347    global $db,$err,$cuid;
348    $err->log("aws","add_login");
349
350    if (!($login=$this->_check($login))) {
351      $err->raise("aws",6); // Login incorrect
352      return false;     
353    }
354    if (!($this->login_exists($login,0))) {
355      $err->raise("aws",5); // Login does not exists
356      return false;
357    }
358    $db->query("SELECT login FROM aws_users WHERE login='$login';");
359    if ($db->next_record()) {
360      $err->raise("aws",7); // Login already exists.
361      return false;
362    }
363    $pass=_md5cr($pass);
364    $db->query("INSERT INTO aws_users (uid,login,pass) VALUES ('$cuid','$login','$pass');");
365    return true;
366  }
367
368  /* ----------------------------------------------------------------- */
369  function allow_login($login,$id,$noconf=0) { // allow user $login to access stats $id.
370    global $db,$err,$cuid;
371    $err->log("aws","allow_login");
372
373    if (!($login=$this->_check($login))) {
374      $err->raise("aws",6); // Login incorrect
375      return false;     
376    }
377    if (!$this->login_exists($login,0)) {
378      $err->raise("aws",5); // Login does not exists
379      return false;
380    }
381    $db->query("SELECT id FROM aws WHERE id='$id' AND uid='$cuid'");
382    if (!$db->next_record()) {
383      $err->raise("aws",2); // The requested statistic does not exist.
384      return false;
385    }
386    $db->query("SELECT login FROM aws_access WHERE id='$id' AND login='$login'");
387    if ($db->next_record()) {
388      $err->raise("aws",8); // This login is already allowed for this statistics.
389      return false;
390    }
391    $db->query("INSERT INTO aws_access (uid,id,login) VALUES ('$cuid','$id','$login');");
392    if (!$noconf) { $this->_createconf($id); }
393    return true;
394  }
395  /* ----------------------------------------------------------------- */
396  function delete_allowed_login($id,$noconf=0) {
397    global $db,$err,$cuid;
398    $err->log("aws","delete_allowed_login");
399
400    $db->query("SELECT id FROM aws WHERE id='$id' AND uid='$cuid'");
401    if (!$db->next_record()) {
402      $err->raise("aws",2); // The requested statistic does not exist.
403      return false;
404    }
405    $db->query("DELETE FROM aws_access WHERE id='$id';");
406    if (!$noconf) { $this->_createconf($id); }
407    return true;
408  }
409  /* ----------------------------------------------------------------- */
410  function deny_login($login,$id,$noconf=0) { // deny user $login to access stats $id.
411    global $db,$err,$cuid;
412    $err->log("aws","deny_login");
413
414    if (!($login=$this->_check($login))) {
415      $err->raise("aws",6); // Login incorrect
416      return false;     
417    }
418    if (!$this->login_exists($login,0)) {
419      $err->raise("aws",5); // Login does not exists
420      return false;
421    }
422    $db->query("SELECT id FROM aws WHERE id='$id' AND uid='$cuid'");
423    if (!$db->next_record()) {
424      $err->raise("aws",2); // The requested statistic does not exist.
425      return false;
426    }
427    $db->query("SELECT login FROM aws_access WHERE id='$id' AND login='$login'");
428    if (!$db->next_record()) {
429      $err->raise("aws",9); // This login is already denied for this statistics.
430      return false;
431    }
432    $db->query("DELETE FROM aws_access WHERE id='$id' AND login='$login';");
433    if (!$noconf) { $this->_createconf($id); }
434    return true;
435  }
436
437  /* ----------------------------------------------------------------- */
438  function alternc_del_member() {
439    global $db,$quota,$err,$cuid;
440    $err->log("aws","del_member");
441    $db->query("SELECT * FROM aws WHERE uid='$cuid';");
442    $t=array();
443    while ($db->next_record()) {
444      $t[]=$db->f("hostname");
445    }
446    $db->query("DELETE FROM aws WHERE uid='$cuid';");
447    foreach ($t as $i) {
448      $this->_delconf($i);
449    }
450    $db->query("DELETE FROM aws_access WHERE uid='$cuid'");
451    $db->query("DELETE FROM aws_users WHERE uid='$cuid';");
452    return true;
453  }
454
455  /* ----------------------------------------------------------------- */
456  /**
457   * Fonction appellée par m_dom lorsqu'un domaine est supprimé.
458   * @param string $dom est le domaine à supprimer.
459   */
460  function alternc_del_domain($dom) {
461    global $db,$quota,$err,$cuid;
462    $err->log("aws","alternc_del_domain",$dom);
463    // on remonte les quotas ;)
464    $db=new DB_System();
465    $db->query("SELECT id,hostname FROM aws WHERE uid='$cuid' AND (hostname='$dom' OR hostname like '%.$dom'");
466    $t=array();
467    while ($db->next_record()) {
468      $t[]=array($db->f("hostname"),$db->f("id"));
469    }
470    $r=$quota->getquota("aws");
471    $quota->setquota("aws",$r["u"]-$cnt,1);
472    // on détruit les jeux de stats associés au préfixe correspondant :
473    foreach ($t as $i) {
474      $db->query("DELETE FROM aws WHERE uid='$cuid' AND hostname='".$i[0]."';");
475      $db->query("DELETE FROM aws_access WHERE uid='$cuid' AND id='".$i[1]."';");
476      $this->_delconf($i[0]);
477    }
478    return true;
479  }
480
481  /* ----------------------------------------------------------------- */
482  /**
483   * Recalcule le quota complet de l'utilisateur courant, ou de l'utilisateur $id
484   * @param integer $id Numéro de l'utilisateur (facultatif)
485   */
486  function alternc_quota_check() {
487    global $db,$err,$quota,$cuid;
488    $err->log("aws","checkquota");
489    $db->query("SELECT COUNT(*) AS cnt FROM aws WHERE uid='$cuid'");
490    $db->next_record();
491    $quota->setquota("aws",intval($db->f("cnt")),1);
492    return true;
493  }
494
495  /* ----------------------------------------------------------------- */
496  function _check($login) {
497    global $err,$mem;
498    $login=trim($login); 
499    $login=strtolower($login); 
500    if ($c=strpos($login,"_")) {
501        $prefix=substr($login,0,$c);
502        $postfix=substr($login,$c+1);
503    } else {
504        $prefix=$login;
505        $postfix="";
506    }
507    $r=$this->prefix_list();
508    if (!in_array($prefix,$r)) { 
509      $err->raise("aws",10); // prefix not allowed.
510      return false;
511    } 
512   if (!ereg("^[0-9a-z_-]*$",$postfix)) {
513      $err->raise("aws",11); // Forbidden caracters in the postfix.
514      return false;
515    }
516    return $login;
517  }
518
519  /* ----------------------------------------------------------------- */
520  /** Effacement du fichier de conf awstats du domaine $hostname
521   * @access private
522   */
523  function _delconf($hostname) {
524    if (!ereg("^[a-z0-9-]*$",$hostname)) {
525      $err->raise("aws",12); // Hostname is incorrect
526      return false;
527    }
528    @unlink($this->CONFDIR."/awstats.".$hostname.".conf");
529  }
530
531  /* ----------------------------------------------------------------- */
532  /** Création du fichier de configuration awstat du domaine $id
533   * if nochk==1, does not check the owner of the stat set (for admin only)
534   * @access private
535   */
536  function _createconf($id,$nochk=0) {
537    global $db,$err,$cuid;
538    $s=implode("",file($this->TEMPLATEFILE));
539    if ($nochk) {
540        $db->query("SELECT * FROM aws WHERE id='$id';");
541    } else { 
542        $db->query("SELECT * FROM aws WHERE id='$id' AND uid='$cuid';");
543    }
544    if (!$db->num_rows()) {
545      $err->raise("aws",2); // This statistic does not exist
546      return false;
547    }
548    $db->next_record();
549    $hostname=$db->f("hostname");
550    $db->query("SELECT login FROM aws_access WHERE id='$id';");
551    $users="";
552    while ($db->next_record()) {
553        $users.=$db->f("login")." ";
554    }
555    $s=str_replace("%%HOSTNAME%%",$hostname,$s);
556    $s=str_replace("%%USERS%%",$users,$s);
557    $f=fopen($this->CONFDIR."/awstats.".$hostname.".conf","wb");
558    fputs($f,$s,strlen($s));
559    fclose($f);
560  }
561
562} /* CLASSE m_aws */
563
564?>
Note: See TracBrowser for help on using the repository browser.