| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | $Id: m_sta2.php,v 1.8 2005/03/07 20:57:20 benjamin Exp $ |
|---|
| 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 | /** |
|---|
| 32 | * This class manages raw Apache log file for the end users. |
|---|
| 33 | * |
|---|
| 34 | * This class allow each AlternC's account to get its raw apache log file every day. |
|---|
| 35 | * The files are stored in the user space. |
|---|
| 36 | * |
|---|
| 37 | * @copyright AlternC's Team 2002-2005 http://alternc.org/ |
|---|
| 38 | * |
|---|
| 39 | */ |
|---|
| 40 | class m_sta2 { |
|---|
| 41 | |
|---|
| 42 | /* ----------------------------------------------------------------- */ |
|---|
| 43 | /** |
|---|
| 44 | * Constructor, dummy |
|---|
| 45 | */ |
|---|
| 46 | function m_sta2() { |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | /* ----------------------------------------------------------------- */ |
|---|
| 50 | /** Hook function that returns the quota names for this class |
|---|
| 51 | * |
|---|
| 52 | * @return string the quota names for this class |
|---|
| 53 | */ |
|---|
| 54 | function alternc_quota_names() { |
|---|
| 55 | return "sta2"; |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | /* ----------------------------------------------------------------- */ |
|---|
| 60 | /** Returns the list of domains and/or subdomains for this account |
|---|
| 61 | * |
|---|
| 62 | * @return array returns an array with all the domains / subdomains for this account. |
|---|
| 63 | */ |
|---|
| 64 | function host_list() { |
|---|
| 65 | global $db,$err,$cuid; |
|---|
| 66 | $r=array(); |
|---|
| 67 | $db->query("SELECT domaine,sub FROM sub_domaines WHERE compte='$cuid' ORDER BY domaine,sub;"); |
|---|
| 68 | while ($db->next_record()) { |
|---|
| 69 | if ($db->f("sub")) { |
|---|
| 70 | $r[]=$db->f("sub").".".$db->f("domaine"); |
|---|
| 71 | } else { |
|---|
| 72 | $r[]=$db->f("domaine"); |
|---|
| 73 | } |
|---|
| 74 | } |
|---|
| 75 | return $r; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | /* ----------------------------------------------------------------- */ |
|---|
| 79 | /** Draw option html tags of ths allowed domains / subdomains for the account. |
|---|
| 80 | * |
|---|
| 81 | * @param $current string The current selected value in the list |
|---|
| 82 | */ |
|---|
| 83 | function select_host_list($current) { |
|---|
| 84 | $r=$this->host_list(); |
|---|
| 85 | reset($r); |
|---|
| 86 | while (list($key,$val)=each($r)) { |
|---|
| 87 | if ($current==$val) $c=" selected=\"selected\""; else $c=""; |
|---|
| 88 | echo "<option$c>$val</option>"; |
|---|
| 89 | } |
|---|
| 90 | return true; |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | /* ----------------------------------------------------------------- */ |
|---|
| 94 | /** Hook function that delete a user's raw stats. |
|---|
| 95 | */ |
|---|
| 96 | function alternc_del_member() { |
|---|
| 97 | global $db,$err,$cuid; |
|---|
| 98 | $err->log("sta2","del_member"); |
|---|
| 99 | $db->query("DELETE FROM stats2 WHERE mid='$cuid';"); |
|---|
| 100 | return true; |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | /* ----------------------------------------------------------------- */ |
|---|
| 104 | /** Hook function that delete a user's domain, called by m_dom. |
|---|
| 105 | * @param string $dom is the domain that is to be deleted. |
|---|
| 106 | */ |
|---|
| 107 | function alternc_del_domain($dom) { |
|---|
| 108 | global $db,$err,$cuid; |
|---|
| 109 | $err->log("sta2","del_dom",$dom); |
|---|
| 110 | // Suppression des stats apache brutes : |
|---|
| 111 | $db->query("SELECT * FROM stats2 WHERE mid='$cuid' AND hostname like '%$dom'"); |
|---|
| 112 | $cnt=0; |
|---|
| 113 | $t=array(); |
|---|
| 114 | while ($db->next_record()) { |
|---|
| 115 | $cnt++; |
|---|
| 116 | $t[]=$db->f("hostname"); |
|---|
| 117 | } |
|---|
| 118 | // on détruit les jeux de stats associés au préfixe correspondant : |
|---|
| 119 | for($i=0;$i<cnt;$i++) { |
|---|
| 120 | $db->query("DELETE FROM stats2 WHERE mid='$cuid' AND hostname='".$t[$i]."';"); |
|---|
| 121 | } |
|---|
| 122 | return true; |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | /* ----------------------------------------------------------------- */ |
|---|
| 126 | /** Returns an array with the user's raw stat list |
|---|
| 127 | * The returned array is as follow : |
|---|
| 128 | * $r[0-n]["id"] = Id of the raw stat set. |
|---|
| 129 | * $r[0-n]["hostname"]= Domain |
|---|
| 130 | * $r[0-n]["folder"]= Destination's folder (in the user space) |
|---|
| 131 | * |
|---|
| 132 | * @return array Returns the array or FALSE if an error occured. |
|---|
| 133 | */ |
|---|
| 134 | function get_list_raw() { |
|---|
| 135 | global $db,$err,$cuid; |
|---|
| 136 | $err->log("sta2","get_list_raw"); |
|---|
| 137 | $r=array(); |
|---|
| 138 | $db->query("SELECT id, hostname, folder FROM stats2 WHERE mid='$cuid' ORDER BY hostname;"); |
|---|
| 139 | if ($db->num_rows()) { |
|---|
| 140 | while ($db->next_record()) { |
|---|
| 141 | // We skip /var/alternc/html/u/user |
|---|
| 142 | preg_match("/^\/var\/alternc\/html\/.\/[^\/]*\/(.*)/", $db->f("folder"),$match); |
|---|
| 143 | $r[]=array( |
|---|
| 144 | "id"=>$db->f("id"), |
|---|
| 145 | "hostname"=>$db->f("hostname"), |
|---|
| 146 | "folder"=>$match[1] |
|---|
| 147 | ); |
|---|
| 148 | } |
|---|
| 149 | return $r; |
|---|
| 150 | } else { |
|---|
| 151 | $err->raise("sta2",2); |
|---|
| 152 | return false; |
|---|
| 153 | } |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | /* ----------------------------------------------------------------- */ |
|---|
| 157 | /** |
|---|
| 158 | * retourne un tableau contenant les details d'un |
|---|
| 159 | * jeu de statistiques apache brut géré par le membre. |
|---|
| 160 | * $id est un id de jeu de stats. Retourne un tableau associatif sous la forme : |
|---|
| 161 | * "id" = numéro du jeu |
|---|
| 162 | * "hostname"= domaine concerné |
|---|
| 163 | * "folder"= Répertoire destination (dans le dossier du membre) |
|---|
| 164 | * @param string $id Numéro du jeu de stats brutes dont on veut les détails |
|---|
| 165 | * @return array Tableau contenant les détails du jeu, ou FALSE en cas d'erreur |
|---|
| 166 | */ |
|---|
| 167 | function get_stats_details_raw($id) { |
|---|
| 168 | global $db,$err,$cuid; |
|---|
| 169 | $err->log("sta2","get_stats_details_raw",$id); |
|---|
| 170 | $r=array(); |
|---|
| 171 | $db->query("SELECT id, hostname, folder FROM stats2 WHERE mid='$cuid' AND id='$id';"); |
|---|
| 172 | if ($db->num_rows()) { |
|---|
| 173 | $db->next_record(); |
|---|
| 174 | // On passe /var/alternc/html/u/user |
|---|
| 175 | preg_match("/^\/var\/alternc\/html\/.\/[^\/]*\/(.*)/", $db->f("folder"),$match); |
|---|
| 176 | return array( |
|---|
| 177 | "id"=>$db->f("id"), |
|---|
| 178 | "hostname"=> $db->f("hostname"), |
|---|
| 179 | "folder"=>$match[1] |
|---|
| 180 | ); |
|---|
| 181 | } else { |
|---|
| 182 | $err->raise("sta2",3); |
|---|
| 183 | return false; |
|---|
| 184 | } |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | /* ----------------------------------------------------------------- */ |
|---|
| 188 | /** |
|---|
| 189 | * Modifie un jeu de statistiques apache brutes existant |
|---|
| 190 | * $id est le numéro du jeu de statistiques |
|---|
| 191 | * $folder est un chemin relatif à "/var/alternc/html/u/user" |
|---|
| 192 | * @param integer $id Numéro du jeu de stats brutes à modifier |
|---|
| 193 | * @param string $folder Dossier destination des stats |
|---|
| 194 | * @return boolean TRUE si le jeu a été modifié, FALSE sinon. |
|---|
| 195 | */ |
|---|
| 196 | function put_stats_details_raw($id,$folder) { |
|---|
| 197 | global $db,$err,$bro,$mem,$cuid; |
|---|
| 198 | $err->log("sta2","put_stats_details_raw",$id); |
|---|
| 199 | $db->query("SELECT count(*) AS cnt FROM stats2 WHERE id='$id' and mid='$cuid';"); |
|---|
| 200 | $db->next_record(); |
|---|
| 201 | if (!$db->f("cnt")) { |
|---|
| 202 | $err->raise("sta2",3); |
|---|
| 203 | return false; |
|---|
| 204 | } |
|---|
| 205 | // TODO : replace with ,1 on convertabsolute call, and delete "/Var/alternc.../" at the query. |
|---|
| 206 | $folder=$bro->convertabsolute($folder); |
|---|
| 207 | if (substr($folder,0,1)=="/") { |
|---|
| 208 | $folder=substr($folder,1); |
|---|
| 209 | } |
|---|
| 210 | $lo=$mem->user["login"]; |
|---|
| 211 | $l=substr($lo,0,1); |
|---|
| 212 | $db->query("UPDATE stats2 SET folder='/var/alternc/html/$l/$lo/$folder', mid='$cuid' WHERE id='$id';"); |
|---|
| 213 | return true; |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | /* ----------------------------------------------------------------- */ |
|---|
| 217 | /** |
|---|
| 218 | * Efface un jeu de statistiques apache brut existant |
|---|
| 219 | * @param integer $id est un id de jeu de statistiques |
|---|
| 220 | * @return boolean TRUE si le jeu a été effacé, FALSE sinon. |
|---|
| 221 | */ |
|---|
| 222 | function delete_stats_raw($id) { |
|---|
| 223 | global $db,$err,$cuid; |
|---|
| 224 | $err->log("sta2","delete_stats_raw",$id); |
|---|
| 225 | $db->query("SELECT hostname FROM stats2 WHERE id='$id' and mid='$cuid';"); |
|---|
| 226 | if (!$db->num_rows()) { |
|---|
| 227 | $err->raise("sta2",3); |
|---|
| 228 | return false; |
|---|
| 229 | } |
|---|
| 230 | $db->next_record(); |
|---|
| 231 | $db->query("DELETE FROM stats2 WHERE id='$id'"); |
|---|
| 232 | return true; |
|---|
| 233 | } |
|---|
| 234 | |
|---|
| 235 | /* ----------------------------------------------------------------- */ |
|---|
| 236 | /** |
|---|
| 237 | * Crée un nouveau jeu de statistiques brutes apache. |
|---|
| 238 | * @param string $hostname est le domaine concerné |
|---|
| 239 | * @param string $dir est le chemin d'accès racine des stats dans le compte |
|---|
| 240 | * @return boolean TRUE si le jeu a été créé, FALSE si un erreur est survenue |
|---|
| 241 | */ |
|---|
| 242 | function add_stats_raw($hostname,$dir) { |
|---|
| 243 | global $db,$err,$quota,$bro,$mem,$cuid; |
|---|
| 244 | $err->log("sta2","add_stats_raw",$hostname); |
|---|
| 245 | // TODO : utiliser le second param de convertabsolute pour simplification. |
|---|
| 246 | $dir=$bro->convertabsolute($dir); |
|---|
| 247 | if (substr($dir,0,1)=="/") { |
|---|
| 248 | $dir=substr($dir,1); |
|---|
| 249 | } |
|---|
| 250 | $lo=$mem->user["login"]; |
|---|
| 251 | $l=substr($lo,0,1); |
|---|
| 252 | if ($quota->cancreate("sta2")) { |
|---|
| 253 | $db->query("INSERT INTO stats2 (hostname,folder,mid) VALUES ('$hostname','/var/alternc/html/$l/$lo/$dir','$cuid')"); |
|---|
| 254 | return true; |
|---|
| 255 | } else { |
|---|
| 256 | $err->raise("sta2",1); |
|---|
| 257 | return false; |
|---|
| 258 | } |
|---|
| 259 | } |
|---|
| 260 | |
|---|
| 261 | /* ----------------------------------------------------------------- */ |
|---|
| 262 | /** |
|---|
| 263 | * Returns the used quota for the $name service for the current user. |
|---|
| 264 | * @param $name string name of the quota |
|---|
| 265 | * @return integer the number of service used or false if an error occured |
|---|
| 266 | * @access private |
|---|
| 267 | */ |
|---|
| 268 | function alternc_get_quota($name) { |
|---|
| 269 | global $db,$err,$cuid; |
|---|
| 270 | if ($name=="sta2") { |
|---|
| 271 | $err->log("sta2","get_quota"); |
|---|
| 272 | $db->query("SELECT COUNT(*) AS cnt FROM stats2 WHERE mid='$cuid'"); |
|---|
| 273 | $db->next_record(); |
|---|
| 274 | return $db->f("cnt"); |
|---|
| 275 | } else return false; |
|---|
| 276 | } |
|---|
| 277 | |
|---|
| 278 | |
|---|
| 279 | |
|---|
| 280 | } /* CLASSE m_sta2 */ |
|---|
| 281 | |
|---|
| 282 | ?> |
|---|