root/alternc/tags/0.9.7/bureau/class/m_quota.php

Revision 1945, 11.6 kB (checked in by azerttyu, 1 year ago)

revert des commit [1944], [1939], [1933], [1925] : nouveau bureau.
On recommencera mais en patchant la branche /franck-desktop

Line 
1 <?php
2 /*
3  $Id: m_quota.php,v 1.17 2006/02/09 19:48:30 benjamin Exp $
4  ----------------------------------------------------------------------
5  AlternC - Web Hosting System
6  Copyright (C) 2006 Le réseau Koumbit Inc.
7  http://koumbit.org/
8  Copyright (C) 2002 by the AlternC Development Team.
9  http://alternc.org/
10  ----------------------------------------------------------------------
11  Based on:
12  Valentin Lacambre's web hosting softwares: http://altern.org/
13  ----------------------------------------------------------------------
14  LICENSE
15
16  This program is free software; you can redistribute it and/or
17  modify it under the terms of the GNU General Public License (GPL)
18  as published by the Free Software Foundation; either version 2
19  of the License, or (at your option) any later version.
20
21  This program is distributed in the hope that it will be useful,
22  but WITHOUT ANY WARRANTY; without even the implied warranty of
23  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  GNU General Public License for more details.
25
26  To read the license please visit http://www.gnu.org/copyleft/gpl.html
27  ----------------------------------------------------------------------
28  Original Author of file: Benjamin Sonntag
29  Purpose of file: Manage user quota
30  ----------------------------------------------------------------------
31 */
32 /*
33 # Structure de la table `defquotas`
34 CREATE TABLE `defquotas` (
35   `quota` varchar(128) NOT NULL default '',
36   `value` bigint(20) unsigned NOT NULL default '0'
37   `type`  varchar(128) NOT NULL default ''
38 ) TYPE=MyISAM COMMENT='Quotas par défaut (nouveaux comptes)';
39 # Structure de la table `quotas`
40 CREATE TABLE `quotas` (
41   `uid` int(10) unsigned NOT NULL default '0',
42   `name` varchar(64) NOT NULL default '',
43   `total` int(11) NOT NULL default '0',
44   PRIMARY KEY  (`uid`,`name`)
45 ) TYPE=MyISAM COMMENT='Quotas des Membres';
46 */
47
48 /**
49 * Class for hosting quotas management
50 *
51 * This class manages services' quotas for each user of AlternC.
52 * The available quotas for each service is stored in the system.quotas
53 * mysql table. The used value is computed by the class using a
54 * callback function <code>alternc_quota_check($uid)</code> that
55 * may by exported by each service class.<br>
56 * each class may also export a function <code>alternc_quota_names()</code>
57 * that returns an array with the quotas names managed by this class.
58 *
59 * @copyright    AlternC-Team 2001-2005 http://alternc.org/
60 *
61 */
62
63
64 class m_quota {
65
66   var $disk=Array(  /* Liste des ressources disque soumises a quota */
67           "web"=>"web");
68
69   var $quotas;
70   var $clquota; // Which class manage which quota.
71
72   /* ----------------------------------------------------------------- */
73   /**
74    * Constructor
75    */
76   function m_quota() {
77   }
78
79   /* ----------------------------------------------------------------- */
80   /** Check if a user can use a ressource.
81    * @Return TRUE if the user can create a ressource (= is there any quota left ?)
82    */
83   function cancreate($ressource="") {
84     $t=$this->getquota($ressource);
85     return $t["u"]<$t["t"];
86   }
87
88   /* ----------------------------------------------------------------- */
89   /**
90    * @Return an array with the list of quota-managed services in the server
91    */
92   function qlist() {
93     global $classes;
94     $qlist=array();
95     reset($this->disk);
96     while (list($key,$val)=each($this->disk)) {
97       $qlist[$key]=_("quota_".$key); // those are specific disks quotas.
98     }
99     foreach($classes as $c) {
100       if (method_exists($GLOBALS[$c],"alternc_quota_names")) {
101     $res=$GLOBALS[$c]->alternc_quota_names(); // returns a string or an array.
102     if($res != "") {
103       if (is_array($res)) {
104         foreach($res as $k) {
105           $qlist[$k]=_("quota_".$k);
106           $this->clquota[$k]=$c;
107         }
108       } else {
109         $qlist[$res]=_("quota_".$res);
110         $this->clquota[$res]=$c;
111       }
112     }
113       }
114     }
115     return $qlist;
116   }
117
118   /* ----------------------------------------------------------------- */
119   /**
120    * @param string ressource to get quota of
121    * @Return the quota used and total for this ressource (or for all ressource if unspecified)
122    */
123   function getquota($ressource="") {
124     global $db,$err,$cuid;
125     $err->log("quota","getquota",$ressource);
126     $this->qlist(); // Generate the quota list.
127     $db->query("select * from quotas where uid='$cuid';");
128     if ($db->num_rows()==0) {
129       return array("t"=>0, "u"=>0);
130     } else {
131       while ($db->next_record()) {
132     $ttmp[]=$db->Record;
133       }
134       foreach ($ttmp as $tt) {
135     $g=array("t"=>$tt["total"],"u"=>0);
136     if (method_exists($GLOBALS[$this->clquota[$tt["name"]]],"alternc_get_quota")) {
137       $g["u"]=$GLOBALS[$this->clquota[$tt["name"]]]->alternc_get_quota($tt["name"]);
138     }
139     $this->quotas[$tt["name"]]=$g;
140       }
141     }
142     reset($this->disk);
143     while (list($key,$val)=each($this->disk)) {
144       $a=array();
145       exec("/usr/lib/alternc/quota_get ".$cuid." ".$val,$a);
146       $this->quotas[$val]=array("t"=>$a[1],"u"=>$a[0]);
147     }
148
149     if ($ressource) {
150       return $this->quotas[$ressource];
151     } else {
152       return $this->quotas;
153     }
154   }
155
156   /* ----------------------------------------------------------------- */
157   /** Set the quota for a user (and for a ressource)
158    * @param string ressource to set quota of
159    * @param integer size of the quota (available or used)
160    */
161   function setquota($ressource,$size) {
162     global $err,$db,$cuid;
163     $err->log("quota","setquota",$ressource."/".$size);
164     if (intval($size)==0) $size="0";
165     if ($this->disk[$ressource]) {
166       // It's a disk resource, update it with shell command
167       exec("/usr/lib/alternc/quota_edit $cuid $size");
168       // Now we check that the value has been written properly :
169       exec("/usr/lib/alternc/quota_get ".$cuid,$a);
170       if ($size!=$a[1]) {
171     $err->raise("quota",1);
172     return false;
173       }
174     }
175     // We check that this ressource exists for this client :
176     $db->query("SELECT * FROM quotas WHERE uid='$cuid' AND name='$ressource'");
177     if ($db->num_rows()) {
178     $db->query("UPDATE quotas SET total='$size' WHERE uid='$cuid' AND name='$ressource';");
179     } else {
180     $db->query("INSERT INTO quotas (uid,name,total) VALUES ('$cuid','$ressource','$size');");
181     }
182     return true;
183   }
184
185   /* ----------------------------------------------------------------- */
186   /**
187    * Increment the resource usage for the named resource
188    * TODO : delete this function as it is useless... (and empty ;) )
189    */
190   function inc($ressource) {
191     global $db,$err,$cuid;
192     $err->log("quota","inc",$ressource);
193     return true;
194   }
195
196   /* ----------------------------------------------------------------- */
197   /**
198    * Decrement the resource usage for the named resource
199    * TODO : delete this function as it is useless... (and empty ;) )
200    */
201   function dec($ressource) {
202     global $db,$err,$cuid;
203     $err->log("quota","dec",$ressource);
204     return true;
205   }
206
207   /* ----------------------------------------------------------------- */
208   /**
209    * Check a user's quota: call a function for each class.
210    * TODO : delete this function as it is useless... (and empty ;) )
211    */
212   function checkquota() {
213     global $err,$classes,$cuid;
214     $err->log("quota","checkquota",$id);
215     return true;
216   }
217
218   /* ----------------------------------------------------------------- */
219   /**
220    * Erase all quota information about the user.
221    */
222   function delquotas() {
223     global $db,$err,$cuid;
224     $err->log("quota","delquota");
225     $db->query("DELETE FROM quotas WHERE uid='$cuid';");
226     return true;
227   }
228
229   /* ----------------------------------------------------------------- */
230   /**
231    * Get the default quotas as an associative array
232    * @return array the array of the default quotas
233    */
234   function getdefaults() {
235     global $db;
236     $c=array();
237
238     $db->query("SELECT type,quota FROM defquotas WHERE type='default'");
239     if(!$db->next_record())
240       $this->addtype('default');
241
242     $db->query("SELECT value,quota,type FROM defquotas ORDER BY type,quota");
243     while($db->next_record()) {
244       $type = $db->f("type");
245
246       $c[$type][$db->f("quota")] = $db->f("value");
247     }
248     return $c;
249   }
250
251   /* ----------------------------------------------------------------- */
252   /**
253    * Set the default quotas
254    * @param array associative array of quota (key=>val)
255    */
256   function setdefaults($newq) {
257     global $db;
258     $qlist=$this->qlist();
259
260     foreach($newq as $type => $quotas) {
261       foreach($quotas as $qname => $value) {
262     if(array_key_exists($qname, $qlist)) {
263       if(!$db->query("REPLACE INTO defquotas (value,quota,type) VALUES ($value,'$qname','$type');"))
264         return false;
265     }
266       }
267     }
268     return true;
269   }
270
271   /* ----------------------------------------------------------------- */
272   /**
273    * Add an account type for quotas
274    * @param string account type to be added
275    */
276   function addtype($type) {
277     global $db;
278     $qlist=$this->qlist();
279     reset($qlist);
280     if(empty($type))
281     return false;
282     while (list($key,$val)=each($qlist)) {
283       if(!$db->query("INSERT IGNORE INTO defquotas (quota,type) VALUES('$key', '$type');")
284      || $db->affected_rows() == 0)
285     return false;
286     }
287     return true;
288   }
289
290   /* ----------------------------------------------------------------- */
291   /**
292    * Delete an account type for quotas
293    * @param string account type to be deleted
294    */
295   function deltype($type) {
296     global $db;
297     $qlist=$this->qlist();
298     reset($qlist);
299
300     if($db->query("UPDATE membres SET type='default' WHERE type='$type'") &&
301        $db->query("DELETE FROM defquotas WHERE type='$type'")) {
302       return true;
303     } else {
304       return false;
305     }
306   }
307
308   /* ----------------------------------------------------------------- */
309   /**
310    * Create default quotas entries for a new user.
311    */
312   function addquotas() {
313     global $db,$err,$cuid;
314     $err->log("quota","addquota");
315     $ql=$this->qlist();
316     reset($ql);
317
318     $db->query("SELECT type,quota FROM defquotas WHERE type='default'");
319     if(!$db->next_record())
320       $this->addtype('default');
321
322     $db->query("SELECT type FROM membres WHERE uid='$cuid'");
323     $db->next_record();
324     $t = $db->f("type");
325
326     foreach($ql as $res => $val) {
327       $db->query("SELECT value FROM defquotas WHERE quota='$res' AND type='$t'");
328       $q = $db->next_record() ? $db->f("value") : 0;
329       $this->setquota($res, $q);
330     }
331     return true;
332   }
333
334   /* ----------------------------------------------------------------- */
335   /** Return a quota value with its unit (when it is a space quota)
336    * in MB, GB, TB ...
337    * @param string $type The quota type
338    * @param integer $value The quota value
339    * @return string a quota value with its unit.
340    */
341   function display_val($type, $value) {
342     switch ($type) {
343     case 'bw_web':
344       return format_size($value);
345     case 'web':
346       return format_size($value*1024);
347     default:
348       return $value;
349     }
350   }
351
352   /* ----------------------------------------------------------------- */
353   /** Hook function called when a user is created
354    * This function initialize the user's quotas.
355    */
356   function alternc_del_member() {
357     $this->delquotas();
358   }
359
360   /* ----------------------------------------------------------------- */
361   /** Hook function call when a user is deleted
362    * AlternC's standard function called when a user is deleted
363    */
364   function alternc_add_member() {
365     $this->addquotas();
366   }
367
368
369   /* ----------------------------------------------------------------- */
370   /**
371    * Exports all the quota related information for an account.
372    * @access private
373    * EXPERIMENTAL 'sid' function ;)
374    */
375   function alternc_export($tmpdir) {
376     global $db,$err;
377     $err->log("quota","export");
378     $str="<quota>\n";
379
380     $q=$this->getquota();
381     foreach ($q as $k=>$v) {
382       $str.="  <service>\n    <name>".xml_entities($k)."</name>\n";
383       $str.="    <total>".xml_entities($v)."</total>\n  </service>\n";
384     }
385     $str.="</quota>\n";
386     return $str;
387   }
388
389
390 } /* Class m_quota */
391
392 ?>
393
Note: See TracBrowser for help on using the browser.