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

Revision 990, 11.6 KB checked in by nahuel, 7 years ago (diff)

On test que le quota existe bien dans la liste qui lui est envoyé pour éviter d'avoir une erreur sql ( en particulier avec quota_web )
Closes: #623

RevLine 
[1]1<?php
2/*
[802]3 $Id: m_quota.php,v 1.17 2006/02/09 19:48:30 benjamin Exp $
[1]4 ----------------------------------------------------------------------
5 AlternC - Web Hosting System
[802]6 Copyright (C) 2006 Le réseau Koumbit Inc.
7 http://koumbit.org/
[1]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 ----------------------------------------------------------------------
[578]28 Original Author of file: Benjamin Sonntag
[802]29 Purpose of file: Manage user quota
[1]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'
[802]37  `type`  varchar(128) NOT NULL default ''
[1]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
[540]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>
[578]56* each class may also export a function <code>alternc_quota_names()</code>
[540]57* that returns an array with the quotas names managed by this class.
58*
[578]59* @copyright    AlternC-Team 2001-2005 http://alternc.org/
[540]60*
61*/
62
63
[1]64class m_quota {
65
66  var $disk=Array(  /* Liste des ressources disque soumises a quota */
67                  "web"=>"web");
68
69  var $quotas;
[428]70  var $clquota; // Which class manage which quota.
[1]71
[222]72  /* ----------------------------------------------------------------- */
73  /**
[802]74   * Constructor
[222]75   */
76  function m_quota() {
[1]77  }
78
[222]79  /* ----------------------------------------------------------------- */
[578]80  /** Check if a user can use a ressource.
81   * @Return TRUE if the user can create a ressource (= is there any quota left ?)
[222]82   */
[1]83  function cancreate($ressource="") {
84    $t=$this->getquota($ressource);
85    return $t["u"]<$t["t"];
86  }
[802]87
[222]88  /* ----------------------------------------------------------------- */
89  /**
90   * @Return an array with the list of quota-managed services in the server
91   */
[1]92  function qlist() {
93    global $classes;
94    $qlist=array();
95    reset($this->disk);
96    while (list($key,$val)=each($this->disk)) {
[428]97      $qlist[$key]=_("quota_".$key); // those are specific disks quotas.
[1]98    }
[904]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.
[293]102        if($res != "") {
103          if (is_array($res)) {
104            foreach($res as $k) {
105              $qlist[$k]=_("quota_".$k);
[904]106              $this->clquota[$k]=$c;
[293]107            }
108          } else {
109            $qlist[$res]=_("quota_".$res);
[904]110            $this->clquota[$res]=$c;
[222]111          }
112        }
[1]113      }
114    }
115    return $qlist;
116  }
[802]117
[222]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;
[1]125    $err->log("quota","getquota",$ressource);
[429]126    $this->qlist(); // Generate the quota list.
[222]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()) {
[471]132        $ttmp[]=$db->Record;
[802]133      }
[471]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"]);
[428]138        }
[471]139        $this->quotas[$tt["name"]]=$g;
[1]140      }
141    }
142    reset($this->disk);
143    while (list($key,$val)=each($this->disk)) {
144      $a=array();
[222]145      exec("/usr/lib/alternc/quota_get ".$cuid." ".$val,$a);
[1]146      $this->quotas[$val]=array("t"=>$a[1],"u"=>$a[0]);
147    }
[802]148
[1]149    if ($ressource) {
150      return $this->quotas[$ressource];
151    } else {
152      return $this->quotas;
153    }
154  }
[802]155
[222]156  /* ----------------------------------------------------------------- */
[578]157  /** Set the quota for a user (and for a ressource)
[802]158   * @param string ressource to set quota of
[222]159   * @param integer size of the quota (available or used)
160   */
[429]161  function setquota($ressource,$size) {
[222]162    global $err,$db,$cuid;
[1]163    $err->log("quota","setquota",$ressource."/".$size);
164    if (intval($size)==0) $size="0";
165    if ($this->disk[$ressource]) {
[578]166      // It's a disk resource, update it with shell command
[802]167      exec("/usr/lib/alternc/quota_edit $cuid $size");
[1]168    }
[802]169    // We check that this ressource exists for this client :
[222]170    $db->query("SELECT * FROM quotas WHERE uid='$cuid' AND name='$ressource'");
[1]171    if ($db->num_rows()) {
[222]172        $db->query("UPDATE quotas SET total='$size' WHERE uid='$cuid' AND name='$ressource';");
[1]173    } else {
[802]174        $db->query("INSERT INTO quotas (uid,name,total) VALUES ('$cuid','$ressource','$size');");
[1]175    }
176    return true;
177  }
178
[222]179  /* ----------------------------------------------------------------- */
180  /**
181   * Increment the resource usage for the named resource
[428]182   * TODO : delete this function as it is useless... (and empty ;) )
[222]183   */
[1]184  function inc($ressource) {
[222]185    global $db,$err,$cuid;
[1]186    $err->log("quota","inc",$ressource);
[428]187    return true;
[1]188  }
189
[222]190  /* ----------------------------------------------------------------- */
191  /**
192   * Decrement the resource usage for the named resource
[428]193   * TODO : delete this function as it is useless... (and empty ;) )
[222]194   */
195  function dec($ressource) {
196    global $db,$err,$cuid;
[1]197    $err->log("quota","dec",$ressource);
[428]198    return true;
[1]199  }
200
[222]201  /* ----------------------------------------------------------------- */
202  /**
203   * Check a user's quota: call a function for each class.
[428]204   * TODO : delete this function as it is useless... (and empty ;) )
[222]205   */
206  function checkquota() {
207    global $err,$classes,$cuid;
[1]208    $err->log("quota","checkquota",$id);
[428]209    return true;
[1]210  }
211
[222]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';");
[1]220    return true;
221  }
222
[222]223  /* ----------------------------------------------------------------- */
224  /**
[578]225   * Get the default quotas as an associative array
[540]226   * @return array the array of the default quotas
[222]227   */
[1]228  function getdefaults() {
229    global $db;
230    $c=array();
[802]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");
[1]241    }
242    return $c;
243  }
244
[222]245  /* ----------------------------------------------------------------- */
246  /**
247   * Set the default quotas
[578]248   * @param array associative array of quota (key=>val)
[222]249   */
[1]250  function setdefaults($newq) {
251    global $db;
252    $qlist=$this->qlist();
[802]253
254    foreach($newq as $type => $quotas) {
255      reset($qlist);
256      foreach($qlist as $qname => $val) {
[990]257        if($quota==$qname){
258          $db->query("SELECT value FROM defquotas WHERE type='$type' AND quota='$qname'");
259          if (!$db->next_record()) {
260            $db->query("INSERT INTO defquotas (value,quota,type) VALUES ('".$quotas[$qname]."','$key','$type');");
261          } else {
262            $db->query("UPDATE defquotas SET value='".$quotas[$qname]."' WHERE type='$type' AND quota='$qname';");
263          }
264        }
[802]265      }
266    }
267    return true;
268  }
269
270  /* ----------------------------------------------------------------- */
271  /**
272   * Add an account type for quotas
273   * @param string account type to be added
274   */
275  function addtype($type) {
276    global $db;
277    $qlist=$this->qlist();
[1]278    reset($qlist);
279    while (list($key,$val)=each($qlist)) {
[802]280      $db->query("SELECT value FROM defquotas WHERE quota='$key' AND type='$type'");
[1]281      if (!$db->next_record()) {
[802]282        $db->query("INSERT INTO defquotas (quota,type) VALUES('$key', '$type');");
[1]283      }
284    }
285  }
[802]286
[222]287  /* ----------------------------------------------------------------- */
288  /**
[802]289   * Delete an account type for quotas
290   * @param string account type to be deleted
291   */
292  function deltype($type) {
293    global $db;
294    $qlist=$this->qlist();
295    reset($qlist);
296
297    $db->query("UPDATE membres SET type='default' WHERE type='$type'");
298    $db->query("DELETE FROM defquotas WHERE type='$type'");
299  }
300
301  /* ----------------------------------------------------------------- */
302  /**
[578]303   * Create default quotas entries for a new user.
[222]304   */
305  function addquotas() {
306    global $db,$err,$cuid;
307    $err->log("quota","addquota");
[1]308    $ql=$this->qlist();
309    reset($ql);
[802]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);
[1]323    }
324    return true;
325  }
326
[222]327  /* ----------------------------------------------------------------- */
[791]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':
[906]337      return format_size($value);
[791]338    case 'web':
[906]339      return format_size($value*1024);
[791]340    default:
341      return $value;
342    }
343  }
344
345  /* ----------------------------------------------------------------- */
[578]346  /** Hook function called when a user is created
347   * This function initialize the user's quotas.
[222]348   */
349  function alternc_del_member() {
350    $this->delquotas();
[1]351  }
352
[222]353  /* ----------------------------------------------------------------- */
[578]354  /** Hook function call when a user is deleted
355   * AlternC's standard function called when a user is deleted
[222]356   */
357  function alternc_add_member() {
358    $this->addquotas();
[1]359  }
360
[904]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
[1]383} /* Class m_quota */
384
[906]385?>
Note: See TracBrowser for help on using the repository browser.