root/alternc-webalizer/trunk/bureau/class/m_webalizer.php

Revision 1525, 14.5 kB (checked in by benjamin, 3 years ago)

Prevents the overwrite of existing stats or web pages by webalizer daily process. Closes #89

  • 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 Webalizer.
28  ----------------------------------------------------------------------
29 */
30 /**
31 * Classe de gestion des statistiques web webalizer / apache des hébergés
32 *
33 * Cette classe permet de gérer les statistiques web générées par webalizer
34 * dans la langue de votre choix, ainsi que les fichiers logs bruts d'apache.<br />
35 * Copyleft {@link http://alternc.net/ AlternC Team}
36 *
37 * @copyright    AlternC-Team 2002-11-01 http://alternc.net/
38 *
39 */
40 class m_webalizer {
41
42   /** Emplacement des fichiers de conf webalizer
43    * @access private
44    */
45   var $CONFDIR="/var/cache/alternc-webalizer/";
46
47   /** Emplacement du fichier patron pour recopie.
48    * @access private
49    */
50   var $TEMPLATEFILE="/etc/alternc/webalizer.template.conf";
51
52   /** Nom des langues disponibles */
53   var $langname=array(
54               "ca_ES"=>"Catalan",        "hr_HR"=>"Croatian",
55               "cs_CZ"=>"Czech",                  "da_DK"=>"Danish",
56               "nl_NL"=>"Dutch",                  "en_US"=>"English",
57               "et_EE"=>"Estonian",        "fi_FI"=>"Finnish",
58               "fr_FR"=>"French",        "de_DE"=>"German",
59               "el_GR"=>"Greek",                  "hu_HU"=>"Hungarian",
60               "is_IS"=>"Icelandic",        "id_ID"=>"Indonesian",
61               "it_IT"=>"Italian",        "ja_JP.UTF-8"=>"Japanese",
62               "ko_KR.UTF-8"=>"Korean",        "lv_LV"=>"Latvian",
63               "ms_MY"=>"Malay",                  "no_NO"=>"Norwegian",
64               "pl_PL"=>"Polish",        "pr_PR"=>"Portuguese",
65               "pt_BR"=>"Portuguese of Brazil",
66               "ro_RO"=>"Romanian",        "ru_RU"=>"Russian",
67               "sr_YU"=>"Serbian",        "zh_HK"=>"Simplified Chinese",
68               "sk_SK"=>"Slovak",        "sl_SI"=>"Slovene",
69               "es_ES"=>"Spanish",        "sv_SE"=>"Swedish",
70               "tr_TR"=>"Turkish",        "uk_UA"=>"Ukrainian",
71               );
72
73   /* ----------------------------------------------------------------- */
74   /**
75    * Constructeur
76    */
77   function m_webalizer() {
78   }
79
80   /* ----------------------------------------------------------------- */
81   /**
82    * Nom du quota
83    */
84   function alternc_quota_names() {
85     return "stats";
86   }
87
88   /* ----------------------------------------------------------------- */
89   /**
90    * Nom des fonctions du module
91    */
92   function alternc_module_description() {
93     return array("webalizer"=>_("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. Webalizer is the soft used to produce those stats. The statistics can be produced in many languages."));
94   }
95
96   /* ----------------------------------------------------------------- */
97   /**
98    * Retourne un tableau contenant les jeux de statistiques d'un membre.
99    *
100    * @return array retourne un tableau indexé de tableaux associatif de la
101    *  forme :
102    *  $r[0-n]["id"] = numéros du jeu
103    *  $r[0-n]["hostname"]= domaine concerné
104    *  $r[0-n]["dir"]= Répertoire destination (dans le dossier du membre)
105    *  $r[0-n]["lang"]= Langue de production des statistiques
106    */
107   function get_list() {
108     global $db,$err,$cuid;
109     $err->log("webalizer","get_list");
110     $r=array();
111     $db->query("SELECT id, hostname, dir, lang FROM stats WHERE uid='$cuid' ORDER BY hostname;");
112     if ($db->num_rows()) {
113       while ($db->next_record()) {
114     // On passe /var/alternc/html/u/user
115     preg_match("/^\/var\/alternc\/html\/.\/[^\/]*\/(.*)/", $db->f("dir"),$match);
116     $r[]=array(
117            "id"=>$db->f("id"),
118            "hostname"=>$db->f("hostname"),
119            "lang"=>$db->f("lang"),
120            "dir"=>$match[1]
121            );
122       }
123       return $r;
124     } else {
125       $err->raise("webalizer",1);
126       return false;
127     }
128   }
129
130   /* ----------------------------------------------------------------- */
131   /**
132    * Retourne un tableau contenant les détails d'un jeu de statistiques
133    *  d'un membre.
134    *
135    * @param integer $id Numéro du jeu de stats dont on veut les infos.
136    * @return array retourne un tableau associatif de la forme :
137    *  $r["id"] = numéros du jeu
138    *  $r["hostname"]= domaine concerné
139    *  $r["dir"]= Répertoire destination (dans le dossier du membre)
140    *  $r["lang"]= Langue de production des statistiques
141    */
142   function get_stats_details($id) {
143     global $db,$err,$cuid;
144     $err->log("webalizer","get_stats_details",$id);
145     $r=array();
146     $db->query("SELECT id, hostname, dir, lang FROM stats WHERE uid='$cuid' AND id='$id';");
147     if ($db->num_rows()) {
148       $db->next_record();
149       // On passe /var/alternc/html/u/user
150       preg_match("/^\/var\/alternc\/html\/.\/[^\/]*\/(.*)/", $db->f("dir"),$match);
151       return array(
152            "id"=>$db->f("id"),
153            "hostname"=> $db->f("hostname"),
154            "lang"=>$db->f("lang"),
155            "dir"=>$match[1]
156            );
157     } else {
158       $err->raise("webalizer",2);
159       return false;
160     }
161   }
162
163
164   /* ----------------------------------------------------------------- */
165   /** Retourne la liste des domaines / sous-domaines autorisés pour le membre.
166    *
167    * @return array retourne un tableau indexé des domaines / sous-domaines utilisables.
168    */
169   function host_list() {
170     global $db,$err,$cuid;
171     $r=array();
172     $db->query("SELECT domaine,sub FROM sub_domaines WHERE compte='$cuid' ORDER BY domaine,sub;");
173     while ($db->next_record()) {
174       if ($db->f("sub")) {
175     $r[]=$db->f("sub").".".$db->f("domaine");
176       } else {
177     $r[]=$db->f("domaine");
178       }
179     }
180     return $r;
181   }
182
183   /* ----------------------------------------------------------------- */
184   /**
185    * Affiche des options select de la liste des noms de domaines autorisés pour
186    * le membre. sous forme de champs d'options (select)
187    */
188   function select_host_list($current) {
189     $r=$this->host_list();
190     reset($r);
191     while (list($key,$val)=each($r)) {
192       if ($current==$val) $c=" selected=\"selected\""; else $c="";
193       echo "<option$c>$val</option>";
194     }
195     return true;
196   }
197
198   /* ----------------------------------------------------------------- */
199   /**
200    * Affiche des options select de la liste des langues autorisées pour
201    * le membre. sous forme de champs d'options (select)
202    */
203   function select_lang_list($current) {
204     reset($this->langname);
205     while (list($key,$val)=each($this->langname)) {
206       if ($current==$key) $c=" selected=\"selected\""; else $c="";
207       echo "<option value=\"$key\"$c>"._($val)."</option>";
208     }
209     return true;
210   }
211
212   /* ----------------------------------------------------------------- */
213   /**
214    * Modifie un jeu de statistiques existant
215    * @param integer $id est le numéro du jeu de statistiques
216    * @param string $dir est un chemin relatif à "/var/alternc/html/u/user"
217    * @param string $stlang est la langue de production des statistiques (code à 2 lettres)
218    */
219   function put_stats_details($id,$dir,$stlang) {
220     global $db,$err,$bro,$mem,$cuid;
221     $err->log("webalizer","put_stats_details",$id);
222     $db->query("SELECT count(*) AS cnt FROM stats WHERE id='$id' and uid='$cuid';");
223     $db->next_record();
224     if (!$db->f("cnt")) {
225       $err->raise("webalizer",2);
226       return false;
227     }
228     $dir=$bro->convertabsolute($dir);
229     if (substr($dir,0,1)=="/") {
230       $dir=substr($dir,1);
231     }
232     // On a épuré $dir des problèmes eventuels ... On est en DESSOUS du dossier de l'utilisateur.
233     if (!$this->langname[$stlang]) {
234       $err->raise("webalizer",6);
235       return false;
236     }
237     $lo=$mem->user["login"];
238     $l=substr($lo,0,1);
239     // Un jeu de stat utilise-t-il déjà ce dossier ?
240     $db->query("SELECT COUNT(*) AS ct FROM stats WHERE dir='/var/alternc/html/$l/$lo/$dir' AND id!='$id';");
241     $db->next_record();
242     if ($db->f("ct")) {
243       $err->raise("webalizer",7);
244       return false;
245     }
246     // Un fichier index existe-t-il déjà dans ce dossier ?
247     if (file_exists("/var/alternc/html/$l/$lo/$dir/index.html") ||
248     file_exists("/var/alternc/html/$l/$lo/$dir/index.htm") ||
249     file_exists("/var/alternc/html/$l/$lo/$dir/index.php") ||
250     file_exists("/var/alternc/html/$l/$lo/$dir/index.php3") ||
251     file_exists("/var/alternc/html/$l/$lo/$dir/index.php4")
252     ) {
253       $err->raise("webalizer",8);
254       return false;
255     }
256     $db->query("UPDATE stats SET lang='$stlang', dir='/var/alternc/html/$l/$lo/$dir', uid='$cuid' WHERE id='$id';");
257     $this->_createconf($id);
258     return true;
259   }
260
261   /* ----------------------------------------------------------------- */
262   /**
263    * Efface un jeu de statistiques existant.
264    * @param integer $id est le numéro du jeu de statistiques à supprimer
265    * @return string le nom du domaine du jeu ainsi effacé, ou FALSE si une erreur est survenue.
266    */
267   function delete_stats($id) {
268     global $db,$err,$cuid;
269     $err->log("webalizer","delete_stats",$id);
270     $db->query("SELECT hostname FROM stats WHERE id='$id' and uid='$cuid';");
271     if (!$db->num_rows()) {
272       $err->raise("webalizer",2);
273       return false;
274     }
275     $db->next_record();
276     $this->_delconf($db->f("hostname"));
277     $db->query("DELETE FROM stats WHERE id='$id'");
278     return $name;
279   }
280
281   /* ----------------------------------------------------------------- */
282   /**
283    * Cree un nouveau jeu de statistiques
284    * @param string $dir est le chemin d'accès racine du compte ftp dans le compte du membre
285    * @param string $lang est la langue choisie
286    * @param string $hostname est le nom de domaine sur lequel on fait des stats
287    * @return boolean TRUE si le jeu de stats a été créé avec succès, FALSE sinon.
288    */
289   function add_stats($hostname,$dir,$lang) {
290     global $db,$err,$quota,$bro,$mem,$cuid;
291     $err->log("webalizer","add_stats",$hostname);
292     $dir=$bro->convertabsolute($dir);
293     if (substr($dir,0,1)=="/") {
294       $dir=substr($dir,1);
295     }
296     // On a épuré $dir des problèmes eventuels ... On est en DESSOUS du dossier de l'utilisateur.
297     $r=$this->host_list();
298     if (!in_array($hostname,$r) || $hostname=="") {
299       $err->raise("webalizer",3);
300       return false;
301     }
302     if (!$this->langname[$lang]) {
303       $err->raise("webalizer",6);
304       return false;
305     }
306     $lo=$mem->user[login];
307     $l=substr($lo,0,1);
308     // Un jeu de stat utilise-t-il déjà ce dossier ?
309     $db->query("SELECT COUNT(*) AS ct FROM stats WHERE dir='/var/alternc/html/$l/$lo/$dir';");
310     $db->next_record();
311     if ($db->f("ct")) {
312       $err->raise("webalizer",7);
313       return false;
314     }
315     // Un fichier index existe-t-il déjà dans ce dossier ?
316     if (file_exists("/var/alternc/html/$l/$lo/$dir/index.html") ||
317     file_exists("/var/alternc/html/$l/$lo/$dir/index.htm") ||
318     file_exists("/var/alternc/html/$l/$lo/$dir/index.php") ||
319     file_exists("/var/alternc/html/$l/$lo/$dir/index.php3") ||
320     file_exists("/var/alternc/html/$l/$lo/$dir/index.php4")
321     ) {
322       $err->raise("webalizer",8);
323       return false;
324     }
325     // Le compte n'existe pas, on le crée.
326     if ($quota->cancreate("stats")) {
327       $db->query("INSERT INTO stats (hostname,lang,dir,uid) VALUES ('$hostname','$lang','/var/alternc/html/$l/$lo/$dir','$cuid')");
328       $this->_createconf($db->lastid());
329       return true;
330     } else {
331       $err->raise("webalizer",5);
332       return false;
333     }
334   }
335
336   function alternc_del_member() {
337     global $db,$err,$cuid;
338     $err->log("webalizer","del_member");
339     $db->query("SELECT * FROM stats WHERE uid='$cuid';");
340     $cnt=0;
341     $t=array();
342     while ($db->next_record()) {
343       $cnt++;
344       $t[]=$db->f("hostname");
345     }
346     $db->query("DELETE FROM stats WHERE uid='$cuid';");
347     for($i=0;$i<cnt;$i++) {
348       $this->_delconf($t[$i]);
349     }
350     return true;
351   }
352
353   /* ----------------------------------------------------------------- */
354   /**
355    * Fonction appellée par m_dom lorsqu'un domaine est supprimé.
356    * @param string $dom est le domaine à supprimer.
357    */
358   function alternc_del_domain($dom) {
359     global $db,$err,$cuid;
360     $err->log("webalizer","del_dom",$dom);
361     $db=new DB_System();
362     $db->query("SELECT * FROM stats WHERE uid='$cuid' AND hostname like '%$dom'");
363     $cnt=0;
364     $t=array();
365     while ($db->next_record()) {
366       $cnt++;
367       $t[]=$db->f("hostname");
368     }
369     // on détruit les jeux de stats associés au préfixe correspondant :
370     for($i=0;$i<cnt;$i++) {
371       $db->query("DELETE FROM stats WHERE uid='$cuid' AND hostname='".$t[$i]."';");
372       $this->_delconf($t[$i]);
373     }
374     return true;
375   }
376
377   /* ----------------------------------------------------------------- */
378   /**
379    * Recalcule le quota complet de l'utilisateur courant, ou de l'utilisateur $id
380    * @param integer $id Numéro de l'utilisateur (facultatif)
381    */
382   function alternc_get_quota($name) {
383     global $db,$err,$cuid;
384     if ($name=="stats") {
385       $err->log("stats","get_quota");
386       $db->query("SELECT COUNT(*) AS cnt FROM stats WHERE uid='$cuid'");
387       $db->next_record();
388       return $db->f("cnt");
389     } else return false;
390   }
391
392   /* ----------------------------------------------------------------- */
393   /** Effacement du fichier de conf webalizer du domaine $hostname
394    * @access private
395    */
396   function _delconf($hostname) {
397     @unlink($this->CONFDIR."/".$hostname.".conf");
398   }
399
400   /* ----------------------------------------------------------------- */
401   /** Création du fichier de configuration Webalizer du domaine $id
402    * @access private
403    */
404   function _createconf($id,$nochk=0) {
405     global $db,$err,$cuid;
406     $s=implode("",file($this->TEMPLATEFILE));
407     if ($nochk) {
408         $db->query("SELECT * FROM stats WHERE id='$id';");
409     } else {
410         $db->query("SELECT * FROM stats WHERE id='$id' AND uid='$cuid';");
411     }
412     if (!$db->num_rows()) {
413       $err->raise("webalizer",2);
414       return false;
415     }
416     $db->next_record();
417     $s=str_replace("%OUTPUTDIR%",$db->f("dir"),$s);
418     $s=str_replace("%HOSTNAME%",$db->f("hostname"),$s);
419     $f=fopen($this->CONFDIR."/".$db->f("hostname").".conf","wb");
420     fputs($f,$s,strlen($s));
421     fclose($f);
422   }
423
424 } /* CLASSE m_webalizer */
425
426 ?>
427
Note: See TracBrowser for help on using the browser.