source: bureau/class/m_quota.php @ 802

Revision 802, 11.0 KB checked in by anarcat, 7 years ago (diff)
  • Ajout des 3 derniers patchs de DARCS d'un coup.
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    for($i=0;$i<count($classes);$i++) {
100      if (method_exists($GLOBALS[$classes[$i]],"alternc_quota_names")) {
101        $res=$GLOBALS[$classes[$i]]->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]=$classes[$i];
107            }
108          } else {
109            $qlist[$res]=_("quota_".$res);
110            $this->clquota[$res]=$classes[$i];
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      reset($qlist);
256      foreach($qlist as $qname => $val) {
257        $db->query("SELECT value FROM defquotas WHERE type='$type' AND quota='$qname'");
258        if (!$db->next_record()) {
259          $db->query("INSERT INTO defquotas (value,quota,type) VALUES ('".$quotas[$qname]."','$key','$type');");
260        } else {
261          $db->query("UPDATE defquotas SET value='".$quotas[$qname]."' WHERE type='$type' AND quota='$qname';");
262        }
263      }
264    }
265    return true;
266  }
267
268  /* ----------------------------------------------------------------- */
269  /**
270   * Add an account type for quotas
271   * @param string account type to be added
272   */
273  function addtype($type) {
274    global $db;
275    $qlist=$this->qlist();
276    reset($qlist);
277    while (list($key,$val)=each($qlist)) {
278      $db->query("SELECT value FROM defquotas WHERE quota='$key' AND type='$type'");
279      if (!$db->next_record()) {
280        $db->query("INSERT INTO defquotas (quota,type) VALUES('$key', '$type');");
281      }
282    }
283  }
284
285  /* ----------------------------------------------------------------- */
286  /**
287   * Delete an account type for quotas
288   * @param string account type to be deleted
289   */
290  function deltype($type) {
291    global $db;
292    $qlist=$this->qlist();
293    reset($qlist);
294
295    $db->query("UPDATE membres SET type='default' WHERE type='$type'");
296    $db->query("DELETE FROM defquotas WHERE type='$type'");
297  }
298
299  /* ----------------------------------------------------------------- */
300  /**
301   * Create default quotas entries for a new user.
302   */
303  function addquotas() {
304    global $db,$err,$cuid;
305    $err->log("quota","addquota");
306    $ql=$this->qlist();
307    reset($ql);
308
309    $db->query("SELECT type,quota FROM defquotas WHERE type='default'");
310    if(!$db->next_record())
311      $this->addtype('default');
312
313    $db->query("SELECT type FROM membres WHERE uid='$cuid'");
314    $db->next_record();
315    $t = $db->f("type");
316
317    foreach($ql as $res => $val) {
318      $db->query("SELECT value FROM defquotas WHERE quota='$res' AND type='$t'");
319      $q = $db->next_record() ? $db->f("value") : 0;
320      $this->setquota($res, $q);
321    }
322    return true;
323  }
324
325  /* ----------------------------------------------------------------- */
326  /** Return a quota value with its unit (when it is a space quota)
327   * in MB, GB, TB ...
328   * @param string $type The quota type
329   * @param integer $value The quota value
330   * @return string a quota value with its unit.
331   */
332  function display_val($type, $value) {
333    switch ($type) {
334    case 'bw_web':
335    case 'web':
336      return format_size($value);
337    default:
338      return $value;
339    }
340  }
341
342  /* ----------------------------------------------------------------- */
343  /** Hook function called when a user is created
344   * This function initialize the user's quotas.
345   */
346  function alternc_del_member() {
347    $this->delquotas();
348  }
349
350  /* ----------------------------------------------------------------- */
351  /** Hook function call when a user is deleted
352   * AlternC's standard function called when a user is deleted
353   */
354  function alternc_add_member() {
355    $this->addquotas();
356  }
357
358} /* Class m_quota */
359
360?>
Note: See TracBrowser for help on using the repository browser.