root/alternc-awstats/tags/nightly_sarge/bureau/class/m_aws.php

Revision 1068, 20.1 kB (checked in by benjamin, 3 years ago)

using crypt instead of md5cr for apache - and adding alternc-export to m_aws

  • 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 */
38 class m_aws {
39
40   /** Where are the awstats configuration files :
41    * @access private
42    */
43   var $CONFDIR="/etc/awstats";
44   var $HTAFILE="/etc/alternc/awstats.htpasswd";
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, 1);
224       if (is_array($users)) {
225         foreach($users as $v) {
226           $this->allow_login($v,$id,1);
227     }
228       }
229       $this->_createconf($id);
230       $this->_createhtpasswd();
231       return true;
232     } else return false;
233   }
234
235   /* ----------------------------------------------------------------- */
236   /**
237    * Efface un jeu de statistiques existant.
238    * @param integer $id est le numéro du jeu de statistiques à supprimer
239    * @return string le nom du domaine du jeu ainsi effacé, ou FALSE si une erreur est survenue.
240    */
241   function delete_stats($id) {
242     global $db,$err,$cuid;
243     $err->log("aws","delete_stats",$id);
244     $db->query("SELECT hostname FROM aws WHERE id='$id' and uid='$cuid';");
245     if (!$db->num_rows()) {
246       $err->raise("aws",2); // This statistic does not exist
247       return false;
248     }
249     $db->next_record();
250     $hostname=$db->f("hostname");
251     $this->delete_allowed_login($id,1);
252     $this->_delconf($hostname);
253     $db->query("DELETE FROM aws WHERE id='$id'");
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       $db->query("INSERT INTO aws (hostname,uid) VALUES ('$hostname','$cuid')");
274       $id=$db->lastid();
275       if (is_array($users)) {
276         foreach($users as $v) {
277           $this->allow_login($v,$id, 1);
278         }
279       }
280       $this->_createconf($id);
281       $this->_createhtpasswd();
282       mkdir($this->CACHEDIR."/".$hostname,0777);
283       return true;
284     } else {
285       $err->raise("aws",4); // Your stat quota is over...
286       return false;
287     }
288   }
289
290   /* ----------------------------------------------------------------- */
291   function list_login() {
292     global $db,$err,$cuid;
293     $err->log("aws","list_login");
294     $db->query("SELECT login FROM aws_users WHERE uid='$cuid';");
295     $res=array();
296     if (!$db->next_record()) {
297     $err->raise("aws",13); // No user currently defined
298       return false;
299     }
300     do {
301       $res[]=$db->f("login");
302     } while ($db->next_record());
303     return $res;
304   }
305
306   /* ----------------------------------------------------------------- */
307   function list_allowed_login($id) {
308     global $db,$err,$cuid;
309     $err->log("aws","list_allowed_login");
310     $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';");
311     $res=array();
312     if (!$db->next_record()) {
313       return false;
314     }
315     do {
316       $res[]=array("login"=>$db->f("login"),"selected"=>($db->f("id")));
317     } while ($db->next_record());
318     return $res;
319   }
320
321   /* ----------------------------------------------------------------- */
322   /* Check that a login exists ($exists=1) or doesn't exist ($exists=0) */
323   function login_exists($login,$exists=1) {
324     global $db,$err,$cuid;
325     $err->log("aws","list_login");
326     $db->query("SELECT login FROM aws_users WHERE uid='$cuid' AND login='$login';");
327     if (!$db->next_record()) {
328       return ($exists==0);
329     } else {
330       return ($exists==1);
331     }
332   }
333
334   /* ----------------------------------------------------------------- */
335   function del_login($login) {
336     global $db,$err,$cuid;
337     $err->log("aws","del_login");
338     if (!$this->login_exists($login,1)) {
339       $err->raise("aws",5); // Login does not exists
340       return false;
341     }
342     $db->query("DELETE FROM aws_users WHERE uid='$cuid' AND login='$login';");
343     $this->_createhtpasswd();
344     return true;
345   }
346
347   /* ----------------------------------------------------------------- */
348   function add_login($login,$pass) {
349     global $db,$err,$cuid;
350     $err->log("aws","add_login");
351
352     if (!($login=$this->_check($login))) {
353       $err->raise("aws",6); // Login incorrect
354       return false;     
355     }
356     if (!($this->login_exists($login,0))) {
357       $err->raise("aws",7); // Login already not exists
358       return false;
359     }
360     $pass=crypt($pass);
361     $db->query("INSERT INTO aws_users (uid,login,pass) VALUES ('$cuid','$login','$pass');");
362     $this->_createhtpasswd();
363     return true;
364   }
365
366   /* ----------------------------------------------------------------- */
367   function change_pass($login,$pass) {
368     global $db,$err,$cuid;
369     $err->log("aws","change_pass");
370
371     if (!($login=$this->_check($login))) {
372       $err->raise("aws",6); // Login incorrect
373       return false;     
374     }
375     if (!($this->login_exists($login))) {
376       $err->raise("aws",5); // Login does not exists
377       return false;
378     }
379     $pass=crypt($pass);
380     $db->query("UPDATE aws_users SET pass='$pass' WHERE login='$login';");
381     $this->_createhtpasswd();
382     return true;
383   }
384
385   /* ----------------------------------------------------------------- */
386   function allow_login($login,$id,$noconf=0) { // allow user $login to access stats $id.
387     global $db,$err,$cuid;
388     $err->log("aws","allow_login");
389
390     if (!($login=$this->_check($login))) {
391       $err->raise("aws",6); // Login incorrect
392       return false;     
393     }
394     if (!$this->login_exists($login)) {
395       $err->raise("aws",5); // Login does not exists
396       return false;
397     }
398     $db->query("SELECT id FROM aws WHERE id='$id' AND uid='$cuid'");
399     if (!$db->next_record()) {
400       $err->raise("aws",2); // The requested statistic does not exist.
401       return false;
402     }
403     $db->query("SELECT login FROM aws_access WHERE id='$id' AND login='$login'");
404     if ($db->next_record()) {
405       $err->raise("aws",8); // This login is already allowed for this statistics.
406       return false;
407     }
408     $db->query("INSERT INTO aws_access (uid,id,login) VALUES ('$cuid','$id','$login');");
409     if (!$noconf) {
410       $this->_createconf($id);
411       $this->_createhtpasswd();
412     }
413     return true;
414   }
415   /* ----------------------------------------------------------------- */
416   function delete_allowed_login($id,$noconf=0) {
417     global $db,$err,$cuid;
418     $err->log("aws","delete_allowed_login");
419
420     $db->query("SELECT id FROM aws WHERE id='$id' AND uid='$cuid'");
421     if (!$db->next_record()) {
422       $err->raise("aws",2); // The requested statistic does not exist.
423       return false;
424     }
425     $db->query("DELETE FROM aws_access WHERE id='$id';");
426     if (!$noconf) {
427       $this->_createconf($id);
428       $this->_createhtpasswd();
429     }
430     return true;
431   }
432   /* ----------------------------------------------------------------- */
433   function deny_login($login,$id,$noconf=0) { // deny user $login to access stats $id.
434     global $db,$err,$cuid;
435     $err->log("aws","deny_login");
436
437     if (!($login=$this->_check($login))) {
438       $err->raise("aws",6); // Login incorrect
439       return false;     
440     }
441     if (!$this->login_exists($login,0)) {
442       $err->raise("aws",5); // Login does not exists
443       return false;
444     }
445     $db->query("SELECT id FROM aws WHERE id='$id' AND uid='$cuid'");
446     if (!$db->next_record()) {
447       $err->raise("aws",2); // The requested statistic does not exist.
448       return false;
449     }
450     $db->query("SELECT login FROM aws_access WHERE id='$id' AND login='$login'");
451     if (!$db->next_record()) {
452       $err->raise("aws",9); // This login is already denied for this statistics.
453       return false;
454     }
455     $db->query("DELETE FROM aws_access WHERE id='$id' AND login='$login';");
456     if (!$noconf) {
457       $this->_createconf($id);
458       $this->_createhtpasswd();
459     }
460     return true;
461   }
462
463   /* ----------------------------------------------------------------- */
464   function alternc_del_member() {
465     global $db,$err,$cuid;
466     $err->log("aws","del_member");
467     $db->query("SELECT * FROM aws WHERE uid='$cuid';");
468     $t=array();
469     while ($db->next_record()) {
470       $t[]=$db->f("hostname");
471     }
472     $db->query("DELETE FROM aws WHERE uid='$cuid';");
473     foreach ($t as $i) {
474       $this->_delconf($i);
475     }
476     $this->_createhtpasswd();
477     $db->query("DELETE FROM aws_access WHERE uid='$cuid'");
478     $db->query("DELETE FROM aws_users WHERE uid='$cuid';");
479     return true;
480   }
481
482   /* ----------------------------------------------------------------- */
483   /**
484    * Fonction appellée par m_dom lorsqu'un domaine est supprimé.
485    * @param string $dom est le domaine à supprimer.
486    */
487   function alternc_del_domain($dom) {
488     global $db,$err,$cuid;
489     $err->log("aws","alternc_del_domain",$dom);
490     $db=new DB_System();
491     $db->query("SELECT id,hostname FROM aws WHERE uid='$cuid' AND (hostname='$dom' OR hostname like '%.$dom')");
492     $t=array();
493     while ($db->next_record()) {
494       $t[]=array($db->f("hostname"),$db->f("id"));
495     }
496     // on détruit les jeux de stats associés au préfixe correspondant :
497     foreach ($t as $i) {
498       $db->query("DELETE FROM aws WHERE uid='$cuid' AND hostname='".$i[0]."';");
499       $db->query("DELETE FROM aws_access WHERE uid='$cuid' AND id='".$i[1]."';");
500       $this->_delconf($i[0]);
501     }
502     $this->_createhtpasswd();
503     return true;
504   }
505
506   /* ----------------------------------------------------------------- */
507   /**
508    * Recalcule le quota complet de l'utilisateur courant, ou de l'utilisateur $id
509    * @param integer $id Numéro de l'utilisateur (facultatif)
510    */
511   function alternc_get_quota($name) {
512     global $db,$err,$cuid;
513     if ($name=="aws") {
514       $err->log("aws","get_quota");
515       $db->query("SELECT COUNT(*) AS cnt FROM aws WHERE uid='$cuid'");
516       $db->next_record();
517       return