root/alternc-awstats/branches/r0/bureau/class/m_aws.php

Revision 1030, 13.3 kB (checked in by anonymous, 4 years ago)

Initial revision

  • 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_stats {
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_stats() {
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("stats"=>_("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("stats","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("stats",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("stats","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("stats",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==$val) $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("stats","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("stats",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("stats",6);
235       return false;
236     }
237     $lo=$mem->user["login"];
238     $l=substr($lo,0,1);
239     $db->query("UPDATE stats SET lang='$stlang', dir='/var/alternc/html/$l/$lo/$dir', uid='$cuid' WHERE id='$id';");
240     $this->_createconf($id);
241     return true;
242   }
243
244   /* ----------------------------------------------------------------- */
245   /**
246    * Efface un jeu de statistiques existant.
247    * @param integer $id est le numéro du jeu de statistiques à supprimer
248    * @return string le nom du domaine du jeu ainsi effacé, ou FALSE si une erreur est survenue.
249    */
250   function delete_stats($id) {
251     global $db,$err,$quota,$cuid;
252     $err->log("stats","delete_stats",$id);
253     $db->query("SELECT hostname FROM stats WHERE id='$id' and uid='$cuid';");
254     if (!$db->num_rows()) {
255       $err->raise("stats",2);
256       return false;
257     }
258     $db->next_record();
259     $this->_delconf($db->f("hostname"));
260     $db->query("DELETE FROM stats WHERE id='$id'");
261     $quota->dec("stats");
262     return $name;
263   }
264
265   /* ----------------------------------------------------------------- */
266   /**
267    * Cree un nouveau jeu de statistiques
268    * @param string $dir est le chemin d'accès racine du compte ftp dans le compte du membre
269    * @param string $lang est la langue choisie
270    * @param string $hostname est le nom de domaine sur lequel on fait des stats
271    * @return boolean TRUE si le jeu de stats a été créé avec succès, FALSE sinon.
272    */
273   function add_stats($hostname,$dir,$lang) {
274     global $db,$err,$quota,$bro,$mem,$cuid;
275     $err->log("stats","add_stats",$hostname);
276     $dir=$bro->convertabsolute($dir);
277     if (substr($dir,0,1)=="/") {
278       $dir=substr($dir,1);
279     }
280     // On a épuré $dir des problèmes eventuels ... On est en DESSOUS du dossier de l'utilisateur.
281     $r=$this->host_list();
282     if (!in_array($hostname,$r) || $hostname=="") {
283       $err->raise("stats",3);
284       return false;
285     }
286     if (!$this->langname[$lang]) {
287       $err->raise("stats",6);
288       return false;
289     }
290     $lo=$mem->user[login];
291     $l=substr($lo,0,1);
292     // Le compte n'existe pas, on le crée.
293     if ($quota->cancreate("stats")) {
294       $quota->inc("stats");
295       $db->query("INSERT INTO stats (hostname,lang,dir,uid) VALUES ('$hostname','$lang','/var/alternc/html/$l/$lo/$dir','$cuid')");
296       $this->_createconf($db->lastid());
297       return true;
298     } else {
299       $err->raise("stats",5);
300       return false;
301     }
302   }
303
304   function alternc_del_member() {
305     global $db,$quota,$err,$cuid;
306     $err->log("stats","del_member");
307     $db->query("SELECT * FROM stats WHERE uid='$cuid';");
308     $cnt=0;
309     $t=array();
310     while ($db->next_record()) {
311       $cnt++;
312       $t[]=$db->f("hostname");
313     }
314     $db->query("DELETE FROM stats WHERE uid='$cuid';");
315     for($i=0;$i<cnt;$i++) {
316       $this->_delconf($t[$i]);
317     }
318     return true;
319   }
320
321   /* ----------------------------------------------------------------- */
322   /**
323    * Fonction appellée par m_dom lorsqu'un domaine est supprimé.
324    * @param string $dom est le domaine à supprimer.
325    */
326   function alternc_del_domain($dom) {
327     global $db,$quota,$err,$cuid;
328     $err->log("stats","del_dom",$dom);
329     // on remonte les quotas ;)
330     $db=new DB_System();
331     $db->query("SELECT * FROM stats WHERE uid='$cuid' AND hostname like '%$dom'");
332     $cnt=0;
333     $t=array();
334     while ($db->next_record()) {
335       $cnt++;
336       $t[]=$db->f("hostname");
337     }
338     $r=$quota->getquota("stats");
339     $quota->setquota("stats",$r["u"]-$cnt,1);
340     // on détruit les jeux de stats associés au préfixe correspondant :
341     for($i=0;$i<cnt;$i++) {
342       $db->query("DELETE FROM stats WHERE uid='$cuid' AND hostname='".$t[$i]."';");
343       $this->_delconf($t[$i]);
344     }
345     return true;
346   }
347
348   /* ----------------------------------------------------------------- */
349   /**
350    * Recalcule le quota complet de l'utilisateur courant, ou de l'utilisateur $id
351    * @param integer $id Numéro de l'utilisateur (facultatif)
352    */
353   function alternc_quota_check() {
354     global $db,$err,$quota,$cuid;
355     $err->log("stats","checkquota");
356     $db->query("SELECT COUNT(*) AS cnt FROM stats WHERE uid='$cuid'");
357     $db->next_record();
358     $quota->setquota("stats",intval($db->f("cnt")),1);
359     return true;
360   }
361
362   /* ----------------------------------------------------------------- */
363   /** Effacement du fichier de conf webalizer du domaine $hostname
364    * @access private
365    */
366   function _delconf($hostname) {
367     @unlink($this->CONFDIR."/".$hostname.".conf");
368   }
369
370   /* ----------------------------------------------------------------- */
371   /** Création du fichier de configuration Webalizer du domaine $id
372    * @access private
373    */
374   function _createconf($id,$nochk=0) {
375     global $db,$err,$cuid;
376     $s=implode("",file($this->TEMPLATEFILE));
377     if ($nochk) {
378         $db->query("SELECT * FROM stats WHERE id='$id';");
379     } else {
380         $db->query("SELECT * FROM stats WHERE id='$id' AND uid='$cuid';");
381     }
382     if (!$db->num_rows()) {
383       $err->raise("stats",2);
384       return false;
385     }
386     $db->next_record();
387     $s=str_replace("%OUTPUTDIR%",$db->f("dir"),$s);
388     $s=str_replace("%HOSTNAME%",$db->f("hostname"),$s);
389     $f=fopen($this->CONFDIR."/".$db->f("hostname").".conf","wb");
390     fputs($f,$s,strlen($s));
391     fclose($f);
392   }
393
394 } /* CLASSE m_stats */
395
396 ?>
Note: See TracBrowser for help on using the browser.