source: trunk/bureau/class/m_quota.php @ 992

Revision 992, 11.4 KB checked in by joe, 7 years ago (diff)

Ajout de vérifications additionelles/meilleure atomicité.

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`
34CREATE 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`
40CREATE 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
64class 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    }
169    // We check that this ressource exists for this client :
170    $db->query("SELECT * FROM quotas WHERE uid='$cuid' AND name='$ressource'");
171    if ($db->num_rows()) {
172        $db->query("UPDATE quotas SET total='$size' WHERE uid='$cuid' AND name='$ressource';");
173    } else {
174        $db->query("INSERT INTO quotas (uid,name,total) VALUES ('$cuid','$ressource','$size');");
175    }
176    return true;
177  }
178
179  /* ----------------------------------------------------------------- */
180  /**
181   * Increment the resource usage for the named resource
182   * TODO : delete this function as it is useless... (and empty ;) )
183   */
184  function inc($ressource) {
185    global $db,$err,$cuid;
186    $err->log("quota","inc",$ressource);
187    return true;
188  }
189
190  /* ----------------------------------------------------------------- */
191  /**
192   * Decrement the resource usage for the named resource
193   * TODO : delete this function as it is useless... (and empty ;) )
194   */
195  function dec($ressource) {
196    global $db,$err,$cuid;
197    $err->log("quota","dec",$ressource);
198    return true;
199  }
200
201  /* ----------------------------------------------------------------- */
202  /**
203   * Check a user's quota: call a function for each class.
204   * TODO : delete this function as it is useless... (and empty ;) )
205   */
206  function checkquota() {
207    global $err,$classes,$cuid;
208    $err->log("quota","checkquota",$id);
209    return true;
210  }
211
212  /* ----------------------------------------------------------------- */
213  /**
214   * Erase all quota information about the user.
215   */
216  function delquotas() {
217    global $db,$err,$cuid;
218    $err->log("quota","delquota");
219    $db->query("DELETE FROM quotas WHERE uid='$cuid';");
220    return true;
221  }
222
223  /* ----------------------------------------------------------------- */
224  /**
225   * Get the default quotas as an associative array
226   * @return array the array of the default quotas
227   */
228  function getdefaults() {
229    global $db;
230    $c=array();
231
232    $db->query("SELECT type,quota FROM defquotas WHERE type='default'");
233    if(!$db->next_record())
234      $this->addtype('default');
235
236    $db->query("SELECT value,quota,type FROM defquotas ORDER BY type,quota");
237    while($db->next_record()) {
238      $type = $db->f("type");
239
240      $c[$type][$db->f("quota")] = $db->f("value");
241    }
242    return $c;
243  }
244
245  /* ----------------------------------------------------------------- */
246  /**
247   * Set the default quotas
248   * @param array associative array of quota (key=>val)
249   */
250  function setdefaults($newq) {
251    global $db;
252    $qlist=$this->qlist();
253
254    foreach($newq as $type => $quotas) {
255      foreach($quotas as $qname => $value) {
256        if(array_key_exists($qname, $qlist)) {
257          if(!$db->query("REPLACE INTO defquotas (value,quota,type) VALUES ($value,'$qname','$type');"))
258            return false;
259        }
260      }
261    }
262    return true;
263  }
264
265  /* ----------------------------------------------------------------- */
266  /**
267   * Add an account type for quotas
268   * @param string account type to be added
269   */
270  function addtype($type) {
271    global $db;
272    $qlist=$this->qlist();
273    reset($qlist);
274   
275    while (list($key,$val)=each($qlist)) {
276      if(!$db->query("INSERT IGNORE INTO defquotas (quota,type) VALUES('$key', '$type');")
277         || $db->affected_rows() == 0)
278        return false;
279    }
280    return true;
281  }
282
283  /* ----------------------------------------------------------------- */
284  /**
285   * Delete an account type for quotas
286   * @param string account type to be deleted
287   */
288  function deltype($type) {
289    global $db;
290    $qlist=$this->qlist();
291    reset($qlist);
292
293    if($db->query("UPDATE membres SET type='default' WHERE type='$type'") &&
294       $db->query("DELETE FROM defquotas WHERE type='$type'")) {
295      return true;
296    } else {
297      return false;
298    }
299  }
300
301  /* ----------------------------------------------------------------- */
302  /**
303   * Create default quotas entries for a new user.
304   */
305  function addquotas() {
306    global $db,$err,$cuid;
307    $err->log("quota","addquota");
308    $ql=$this->qlist();
309    reset($ql);
310
311    $db->query("SELECT type,quota FROM defquotas WHERE type='default'");
312    if(!$db->next_record())
313      $this->addtype('default');
314
315    $db->query("SELECT type FROM membres WHERE uid='$cuid'");
316    $db->next_record();
317    $t = $db->f("type");
318
319    foreach($ql as $res => $val) {
320      $db->query("SELECT value FROM defquotas WHERE quota='$res' AND type='$t'");
321      $q = $db->next_record() ? $db->f("value") : 0;
322      $this->setquota($res, $q);
323    }
324    return true;
325  }
326
327  /* ----------------------------------------------------------------- */
328  /** Return a quota value with its unit (when it is a space quota)
329   * in MB, GB, TB ...
330   * @param string $type The quota type
331   * @param integer $value The quota value
332   * @return string a quota value with its unit.
333   */
334  function display_val($type, $value) {
335    switch ($type) {
336    case 'bw_web':
337      return format_size($value);
338    case 'web':
339      return format_size($value*1024);
340    default:
341      return $value;
342    }
343  }
344
345  /* ----------------------------------------------------------------- */
346  /** Hook function called when a user is created
347   * This function initialize the user's quotas.
348   */
349  function alternc_del_member() {
350    $this->delquotas();
351  }
352
353  /* ----------------------------------------------------------------- */
354  /** Hook function call when a user is deleted
355   * AlternC's standard function called when a user is deleted
356   */
357  function alternc_add_member() {
358    $this->addquotas();
359  }
360
361
362  /* ----------------------------------------------------------------- */
363  /**
364   * Exports all the quota related information for an account.
365   * @access private
366   * EXPERIMENTAL 'sid' function ;)
367   */
368  function alternc_export($tmpdir) {
369    global $db,$err;
370    $err->log("quota","export");
371    $str="<quota>\n";
372
373    $q=$this->getquota();
374    foreach ($q as $k=>$v) {
375      $str.="  <service>\n    <name>".xml_entities($k)."</name>\n";
376      $str.="    <total>".xml_entities($v)."</total>\n  </service>\n";
377    }
378    $str.="</quota>\n";
379    return $str;
380  }
381
382
383} /* Class m_quota */
384
385?>
Note: See TracBrowser for help on using the repository browser.