source: alternc/trunk/bureau/class/m_quota.php @ 2559

Revision 2559, 11.0 KB checked in by nahuel, 4 years ago (diff)

Merging blue desktop to trunk.

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 of `defquotas` table
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*/
62class m_quota {
63
64  var $disk=Array(  /* Liste des ressources disque soumises a quota */
65                  "web"=>"web");
66
67  var $quotas;
68  var $clquota; // Which class manage which quota.
69
70
71  /* ----------------------------------------------------------------- */
72  /**
73   * Constructor
74   */
75  function m_quota() {
76  }
77
78
79  /* ----------------------------------------------------------------- */
80  /** Check if a user can use a ressource.
81   * @param string $ressource the ressource name (a named quota)
82   * @Return TRUE if the user can create a ressource (= is there any quota left ?)
83   */
84  function cancreate($ressource="") {
85    $t=$this->getquota($ressource);
86    return $t["u"]<$t["t"];
87  }
88
89
90  /* ----------------------------------------------------------------- */
91  /**
92   * @Return an array with the list of quota-managed services in the server
93   */
94  function qlist() {
95    global $classes;
96    $qlist=array();
97    reset($this->disk);
98    while (list($key,$val)=each($this->disk)) {
99      $qlist[$key]=_("quota_".$key); // those are specific disks quotas.
100    }
101    foreach($classes as $c) {
102      if (method_exists($GLOBALS[$c],"alternc_quota_names")) {
103        $res=$GLOBALS[$c]->alternc_quota_names(); // returns a string or an array.
104        if($res != "") {
105          if (is_array($res)) {
106            foreach($res as $k) {
107              $qlist[$k]=_("quota_".$k);
108              $this->clquota[$k]=$c;
109            }
110          } else {
111            $qlist[$res]=_("quota_".$res);
112            $this->clquota[$res]=$c;
113          }
114        }
115      }
116    }
117    return $qlist;
118  }
119
120
121  /* ----------------------------------------------------------------- */
122  /** Return a ressource usage (u) and total quota (t)
123   * @param string $ressource ressource to get quota of
124   * @Return array the quota used and total for this ressource (or for all ressource if unspecified)
125   */
126  function getquota($ressource="") {
127    global $db,$err,$cuid;
128    $err->log("quota","getquota",$ressource);
129    $this->qlist(); // Generate the quota list.
130    $db->query("select * from quotas where uid='$cuid';");
131    if ($db->num_rows()==0) {
132      return array("t"=>0, "u"=>0);
133    } else {
134      while ($db->next_record()) {
135        $ttmp[]=$db->Record;
136      }
137      foreach ($ttmp as $tt) {
138        $g=array("t"=>$tt["total"],"u"=>0);
139        if (method_exists($GLOBALS[$this->clquota[$tt["name"]]],"alternc_get_quota")) {
140          $g["u"]=$GLOBALS[$this->clquota[$tt["name"]]]->alternc_get_quota($tt["name"]);
141        }
142        $this->quotas[$tt["name"]]=$g;
143      }
144    }
145    reset($this->disk);
146    while (list($key,$val)=each($this->disk)) {
147      $a=array();
148      exec("/usr/lib/alternc/quota_get ".$cuid." ".$val,$a);
149      $this->quotas[$val]=array("t"=>$a[1],"u"=>$a[0]);
150    }
151
152    if ($ressource) {
153      return $this->quotas[$ressource];
154    } else {
155      return $this->quotas;
156    }
157  }
158
159
160  /* ----------------------------------------------------------------- */
161  /** Set the quota for a user (and for a ressource)
162   * @param string $ressource ressource to set quota of
163   * @param integer size of the quota (available or used)
164   */
165  function setquota($ressource,$size) {
166    global $err,$db,$cuid;
167    $err->log("quota","setquota",$ressource."/".$size);
168    if (floatval($size)==0) $size="0";
169    if ($this->disk[$ressource]) {
170      // It's a disk resource, update it with shell command
171      exec("/usr/lib/alternc/quota_edit $cuid $size");
172      // Now we check that the value has been written properly :
173      exec("/usr/lib/alternc/quota_get ".$cuid,$a);
174      if ($size!=$a[1]) {
175        $err->raise("quota",1);
176        return false;
177      }
178    }
179    // We check that this ressource exists for this client :
180    $db->query("SELECT * FROM quotas WHERE uid='$cuid' AND name='$ressource'");
181    if ($db->num_rows()) {
182        $db->query("UPDATE quotas SET total='$size' WHERE uid='$cuid' AND name='$ressource';");
183    } else {
184        $db->query("INSERT INTO quotas (uid,name,total) VALUES ('$cuid','$ressource','$size');");
185    }
186    return true;
187  }
188
189
190  /* ----------------------------------------------------------------- */
191  /**
192   * Erase all quota information about the user.
193   */
194  function delquotas() {
195    global $db,$err,$cuid;
196    $err->log("quota","delquota");
197    $db->query("DELETE FROM quotas WHERE uid='$cuid';");
198    return true;
199  }
200
201
202  /* ----------------------------------------------------------------- */
203  /**
204   * Get the default quotas as an associative array
205   * @return array the array of the default quotas
206   */
207  function getdefaults() {
208    global $db;
209    $c=array();
210
211    $db->query("SELECT type,quota FROM defquotas WHERE type='default'");
212    if(!$db->next_record())
213      $this->addtype('default');
214
215    $db->query("SELECT value,quota,type FROM defquotas ORDER BY type,quota");
216    while($db->next_record()) {
217      $type = $db->f("type");
218
219      $c[$type][$db->f("quota")] = $db->f("value");
220    }
221    return $c;
222  }
223
224
225  /* ----------------------------------------------------------------- */
226  /**
227   * Set the default quotas
228   * @param array associative array of quota (key=>val)
229   */
230  function setdefaults($newq) {
231    global $db;
232    $qlist=$this->qlist();
233
234    foreach($newq as $type => $quotas) {
235      foreach($quotas as $qname => $value) {
236        if(array_key_exists($qname, $qlist)) {
237          if(!$db->query("REPLACE INTO defquotas (value,quota,type) VALUES ($value,'$qname','$type');"))
238            return false;
239        }
240      }
241    }
242    return true;
243  }
244
245
246  /* ----------------------------------------------------------------- */
247  /**
248   * Add an account type for quotas
249   * @param string $type account type to be added
250   * @return boolean true if all went ok
251   */
252  function addtype($type) {
253    global $db;
254    $qlist=$this->qlist();
255    reset($qlist);
256    if(empty($type))
257        return false;
258    while (list($key,$val)=each($qlist)) {
259      if(!$db->query("INSERT IGNORE INTO defquotas (quota,type) VALUES('$key', '$type');")
260         || $db->affected_rows() == 0)
261        return false;
262    }
263    return true;
264  }
265
266
267  /* ----------------------------------------------------------------- */
268  /**
269   * Delete an account type for quotas
270   * @param string $type account type to be deleted
271   * @return boolean true if all went ok
272   */
273  function deltype($type) {
274    global $db;
275    $qlist=$this->qlist();
276    reset($qlist);
277
278    if($db->query("UPDATE membres SET type='default' WHERE type='$type'") &&
279       $db->query("DELETE FROM defquotas WHERE type='$type'")) {
280      return true;
281    } else {
282      return false;
283    }
284  }
285
286
287  /* ----------------------------------------------------------------- */
288  /**
289   * Create default quotas entries for a new user.
290   * The user we are talking about is in the global $cuid.
291   */
292  function addquotas() {
293    global $db,$err,$cuid;
294    $err->log("quota","addquota");
295    $ql=$this->qlist();
296    reset($ql);
297
298    $db->query("SELECT type,quota FROM defquotas WHERE type='default'");
299    if(!$db->next_record())
300      $this->addtype('default');
301
302    $db->query("SELECT type FROM membres WHERE uid='$cuid'");
303    $db->next_record();
304    $t = $db->f("type");
305
306    foreach($ql as $res => $val) {
307      $db->query("SELECT value FROM defquotas WHERE quota='$res' AND type='$t'");
308      $q = $db->next_record() ? $db->f("value") : 0;
309      $this->setquota($res, $q);
310    }
311    return true;
312  }
313
314
315  /* ----------------------------------------------------------------- */
316  /** Return a quota value with its unit (when it is a space quota)
317   * in MB, GB, TB ...
318   * @param string $type The quota type
319   * @param integer $value The quota value
320   * @return string a quota value with its unit.
321   */
322  function display_val($type, $value) {
323    switch ($type) {
324    case 'bw_web':
325      return format_size($value);
326    case 'web':
327      return format_size($value*1024);
328    default:
329      return $value;
330    }
331  }
332
333
334  /* ----------------------------------------------------------------- */
335  /** Hook function call when a user is deleted
336   * AlternC's standard function called when a user is deleted
337   */
338  function alternc_del_member() {
339    $this->delquotas();
340  }
341
342
343  /* ----------------------------------------------------------------- */
344  /** Hook function called when a user is created
345   * This function initialize the user's quotas.
346   */
347  function alternc_add_member() {
348    $this->addquotas();
349  }
350
351
352  /* ----------------------------------------------------------------- */
353  /**
354   * Exports all the quota related information for an account.
355   * @access private
356   * EXPERIMENTAL 'sid' function ;)
357   */
358  function alternc_export($tmpdir) {
359    global $db,$err;
360    $err->log("quota","export");
361    $str="<quota>\n";
362
363    $q=$this->getquota();
364    foreach ($q as $k=>$v) {
365      $str.="  <service>\n    <name>".xml_entities($k)."</name>\n";
366      $str.="    <total>".xml_entities($v)."</total>\n  </service>\n";
367    }
368    $str.="</quota>\n";
369    return $str;
370  }
371
372
373} /* Class m_quota */
374
375?>
Note: See TracBrowser for help on using the repository browser.