| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | $Id: m_quota.php,v 1.3 2003/06/09 20:01:40 root Exp $ |
|---|
| 4 | ---------------------------------------------------------------------- |
|---|
| 5 | AlternC - Web Hosting System |
|---|
| 6 | Copyright (C) 2002 by the AlternC Development Team. |
|---|
| 7 | http://alternc.org/ |
|---|
| 8 | ---------------------------------------------------------------------- |
|---|
| 9 | Based on: |
|---|
| 10 | Valentin Lacambre's web hosting softwares: http://altern.org/ |
|---|
| 11 | ---------------------------------------------------------------------- |
|---|
| 12 | LICENSE |
|---|
| 13 | |
|---|
| 14 | This program is free software; you can redistribute it and/or |
|---|
| 15 | modify it under the terms of the GNU General Public License (GPL) |
|---|
| 16 | as published by the Free Software Foundation; either version 2 |
|---|
| 17 | of the License, or (at your option) any later version. |
|---|
| 18 | |
|---|
| 19 | This program is distributed in the hope that it will be useful, |
|---|
| 20 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 22 | GNU General Public License for more details. |
|---|
| 23 | |
|---|
| 24 | To read the license please visit http://www.gnu.org/copyleft/gpl.html |
|---|
| 25 | ---------------------------------------------------------------------- |
|---|
| 26 | Original Author of file: |
|---|
| 27 | Purpose of file: |
|---|
| 28 | ---------------------------------------------------------------------- |
|---|
| 29 | */ |
|---|
| 30 | /* |
|---|
| 31 | # Structure de la table `defquotas` |
|---|
| 32 | CREATE TABLE `defquotas` ( |
|---|
| 33 | `quota` varchar(128) NOT NULL default '', |
|---|
| 34 | `value` bigint(20) unsigned NOT NULL default '0' |
|---|
| 35 | ) TYPE=MyISAM COMMENT='Quotas par défaut (nouveaux comptes)'; |
|---|
| 36 | # Structure de la table `quotas` |
|---|
| 37 | CREATE TABLE `quotas` ( |
|---|
| 38 | `uid` int(10) unsigned NOT NULL default '0', |
|---|
| 39 | `name` varchar(64) NOT NULL default '', |
|---|
| 40 | `total` int(11) NOT NULL default '0', |
|---|
| 41 | `used` int(11) NOT NULL default '0', |
|---|
| 42 | PRIMARY KEY (`uid`,`name`) |
|---|
| 43 | ) TYPE=MyISAM COMMENT='Quotas des Membres'; |
|---|
| 44 | */ |
|---|
| 45 | |
|---|
| 46 | class m_quota { |
|---|
| 47 | |
|---|
| 48 | var $uid=0; /* Membre dont on souhaite gérer les quotas DISQUE DUR */ |
|---|
| 49 | |
|---|
| 50 | var $disk=Array( /* Liste des ressources disque soumises a quota */ |
|---|
| 51 | "web"=>"web"); |
|---|
| 52 | |
|---|
| 53 | var $quotas; |
|---|
| 54 | |
|---|
| 55 | /***************************************************************************** |
|---|
| 56 | m_quota([$uid]) Constructeur de la classe m_quota, initialise le membre |
|---|
| 57 | *****************************************************************************/ |
|---|
| 58 | function m_quota($membre=0) { |
|---|
| 59 | $this->uid=$membre; |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | /*****************************************************************************/ |
|---|
| 63 | function cancreate($ressource="") { |
|---|
| 64 | $t=$this->getquota($ressource); |
|---|
| 65 | return $t["u"]<$t["t"]; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | /** Retourne la liste des quotas possibles. **/ |
|---|
| 69 | function qlist() { |
|---|
| 70 | global $classes; |
|---|
| 71 | $qlist=array(); |
|---|
| 72 | reset($this->disk); |
|---|
| 73 | while (list($key,$val)=each($this->disk)) { |
|---|
| 74 | $qlist[$key]=_("quota_".$key); |
|---|
| 75 | } |
|---|
| 76 | for($i=0;$i<count($classes);$i++) { |
|---|
| 77 | if ($GLOBALS[$classes[$i]]->alternc_quota_name) { |
|---|
| 78 | $qlist[$GLOBALS[$classes[$i]]->alternc_quota_name]=_("quota_".$GLOBALS[$classes[$i]]->alternc_quota_name); |
|---|
| 79 | } |
|---|
| 80 | } |
|---|
| 81 | return $qlist; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | /*****************************************************************************/ |
|---|
| 85 | function getquota($ressource="",$uid=-1) { |
|---|
| 86 | global $db,$err; |
|---|
| 87 | $err->log("quota","getquota",$ressource); |
|---|
| 88 | if ($uid==-1) $uid=$this->uid; |
|---|
| 89 | if (!is_array($this->quotas)) { /* First time, read the quotas */ |
|---|
| 90 | $db->query("select * from quotas where uid=$uid;"); |
|---|
| 91 | if ($db->num_rows()==0) { |
|---|
| 92 | return array("t"=>0, "u"=>0); |
|---|
| 93 | } else { |
|---|
| 94 | while ($db->next_record()) { |
|---|
| 95 | $this->quotas[$db->Record["name"]]=array("t"=>$db->Record["total"],"u"=>$db->Record["used"]); |
|---|
| 96 | } |
|---|
| 97 | } |
|---|
| 98 | } |
|---|
| 99 | reset($this->disk); |
|---|
| 100 | while (list($key,$val)=each($this->disk)) { |
|---|
| 101 | $a=array(); |
|---|
| 102 | exec("/usr/lib/alternc/quota_get ".$uid." ".$val,$a); |
|---|
| 103 | $this->quotas[$val]=array("t"=>$a[1],"u"=>$a[0]); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | if ($ressource) { |
|---|
| 107 | return $this->quotas[$ressource]; |
|---|
| 108 | } else { |
|---|
| 109 | return $this->quotas; |
|---|
| 110 | } |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | /*****************************************************************************/ |
|---|
| 114 | function setquota($ressource,$size,$used=0,$uid=-1) { |
|---|
| 115 | global $err,$db; |
|---|
| 116 | $err->log("quota","setquota",$ressource."/".$size); |
|---|
| 117 | if ($uid==-1) $uid=$this->uid; |
|---|
| 118 | if (intval($size)==0) $size="0"; |
|---|
| 119 | if ($this->disk[$ressource]) { |
|---|
| 120 | // C'est une ressource disque : exec |
|---|
| 121 | exec("/usr/lib/alternc/quota_edit $uid $size"); |
|---|
| 122 | return true; |
|---|
| 123 | } |
|---|
| 124 | // 1. on vérifie que ce quota existe pour le membre, sinon, on le crée avec 2 valeurs à 0. |
|---|
| 125 | $db->query("SELECT * FROM quotas WHERE uid=$uid AND name='$ressource'"); |
|---|
| 126 | if ($db->num_rows()) { |
|---|
| 127 | // Existe : on update |
|---|
| 128 | if ($used) { |
|---|
| 129 | $db->query("UPDATE quotas SET used=$size WHERE uid=$uid AND name='$ressource';"); |
|---|
| 130 | } else { |
|---|
| 131 | $db->query("UPDATE quotas SET total=$size WHERE uid=$uid AND name='$ressource';"); |
|---|
| 132 | } |
|---|
| 133 | } else { |
|---|
| 134 | // N'existe pas, on crée. |
|---|
| 135 | if ($used) { |
|---|
| 136 | $db->query("INSERT INTO quotas (uid,name,used,total) VALUES ($uid,'$ressource',$size,0);"); |
|---|
| 137 | } else { |
|---|
| 138 | $db->query("INSERT INTO quotas (uid,name,used,total) VALUES ($uid,'$ressource',0,$size);"); |
|---|
| 139 | } |
|---|
| 140 | } |
|---|
| 141 | return true; |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | /*****************************************************************************/ |
|---|
| 145 | function inc($ressource) { |
|---|
| 146 | global $db,$err; |
|---|
| 147 | $err->log("quota","inc",$ressource); |
|---|
| 148 | // 1. on vérifie que ce quota existe pour le membre, sinon, on le crée avec 2 valeurs à 0. |
|---|
| 149 | $db->query("SELECT * FROM quotas WHERE uid=".$this->uid." AND name='$ressource'"); |
|---|
| 150 | if ($db->num_rows()) { |
|---|
| 151 | // Existe : on update |
|---|
| 152 | $db->query("UPDATE quotas SET used=used+1 WHERE uid=".$this->uid." AND name='$ressource';"); |
|---|
| 153 | } else { |
|---|
| 154 | // N'Existe pas, on crée. |
|---|
| 155 | $db->query("INSERT INTO quotas (uid,name,used,total) VALUES (".$this->uid.",'$ressource',1,0);"); |
|---|
| 156 | } |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | /*****************************************************************************/ |
|---|
| 160 | function dec($ressource,$uid=0) { |
|---|
| 161 | global $db,$err; |
|---|
| 162 | $err->log("quota","dec",$ressource); |
|---|
| 163 | if (!$uid) $uid=$this->uid; |
|---|
| 164 | // 1. on vérifie que ce quota existe pour le membre, sinon, on le crée avec 2 valeurs à 0. |
|---|
| 165 | $db->query("SELECT * FROM quotas WHERE uid='$uid' AND name='$ressource'"); |
|---|
| 166 | if ($db->num_rows()) { |
|---|
| 167 | // Existe : on update |
|---|
| 168 | $db->query("UPDATE quotas SET used=used-1 WHERE uid='$uid' AND name='$ressource';"); |
|---|
| 169 | } else { |
|---|
| 170 | // N'Existe pas, on crée. |
|---|
| 171 | $db->query("INSERT INTO quotas (uid,name,used,total) VALUES ('$uid','$ressource',-1,0);"); |
|---|
| 172 | } |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | /*****************************************************************************/ |
|---|
| 176 | function checkquota($id=-1) { |
|---|
| 177 | global $err; |
|---|
| 178 | $err->log("quota","checkquota",$id); |
|---|
| 179 | if ($id==-1) $id=$this->uid; |
|---|
| 180 | // Check quotas for all classes : |
|---|
| 181 | for($i=0;$i<count($classes);$i++) { |
|---|
| 182 | if (method_exists($GLOBALS[$classes[$i]],"alternc_quota_check")) { |
|---|
| 183 | $GLOBALS[$classes[$i]]->alternc_quota_check($uid); |
|---|
| 184 | } |
|---|
| 185 | } |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | /*****************************************************************************/ |
|---|
| 189 | function delquotas($uid) { |
|---|
| 190 | global $db,$err; |
|---|
| 191 | $err->log("quota","delquota",$id); |
|---|
| 192 | $db->query("DELETE FROM quotas WHERE uid='$uid';"); |
|---|
| 193 | return true; |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | /*****************************************************************************/ |
|---|
| 197 | function getdefaults() { |
|---|
| 198 | global $db; |
|---|
| 199 | $qlist=$this->qlist(); |
|---|
| 200 | reset($qlist); |
|---|
| 201 | $c=array(); |
|---|
| 202 | while (list($key,$val)=each($qlist)) { |
|---|
| 203 | $db->query("SELECT value FROM defquotas WHERE quota='$key'"); |
|---|
| 204 | $db->next_record(); |
|---|
| 205 | $c[$key]=array("name"=>$val,"value"=>$db->f("value")); |
|---|
| 206 | } |
|---|
| 207 | return $c; |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | /*****************************************************************************/ |
|---|
| 211 | function setdefaults($newq) { |
|---|
| 212 | global $db; |
|---|
| 213 | $qlist=$this->qlist(); |
|---|
| 214 | reset($qlist); |
|---|
| 215 | while (list($key,$val)=each($qlist)) { |
|---|
| 216 | $db->query("SELECT value FROM defquotas WHERE quota='$key'"); |
|---|
| 217 | if (!$db->next_record()) { |
|---|
| 218 | $db->query("INSERT INTO defquotas (value,quota) VALUES ('".$newq[$key]."','$key');"); |
|---|
| 219 | } else { |
|---|
| 220 | $db->query("UPDATE defquotas SET value='".$newq[$key]."' WHERE quota='$key';"); |
|---|
| 221 | } |
|---|
| 222 | } |
|---|
| 223 | return true; |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | /*****************************************************************************/ |
|---|
| 227 | function addquotas($uid) { |
|---|
| 228 | global $db,$err; |
|---|
| 229 | $err->log("quota","addquota",$id); |
|---|
| 230 | $ql=$this->qlist(); |
|---|
| 231 | reset($ql); |
|---|
| 232 | while (list($key,$val)=each($ql)) { |
|---|
| 233 | if (!in_array($key,$this->disk)) { |
|---|
| 234 | $db->query("SELECT value FROM defquotas WHERE quota='$key';"); |
|---|
| 235 | if ($db->next_record()) { |
|---|
| 236 | $tot=$db->f("value"); |
|---|
| 237 | } else { |
|---|
| 238 | $db->query("INSERT INTO defquotas (quota,value) VALUES ('$key',0);"); |
|---|
| 239 | $tot=0; |
|---|
| 240 | } |
|---|
| 241 | $db->query("INSERT INTO quotas(uid,name,total) VALUES('$uid','$key','$tot');"); |
|---|
| 242 | } |
|---|
| 243 | } |
|---|
| 244 | reset($this->disk); |
|---|
| 245 | while (list($key,$val)=each($this->disk)) { |
|---|
| 246 | $db->query("SELECT value FROM defquotas WHERE quota='$key';"); |
|---|
| 247 | if ($db->next_record()) { |
|---|
| 248 | $tot=$db->f("value"); |
|---|
| 249 | } else { |
|---|
| 250 | $db->query("INSERT INTO defquotas (quota,value) ('$key',0);"); |
|---|
| 251 | $tot=0; |
|---|
| 252 | } |
|---|
| 253 | exec("/usr/lib/alternc/quota_edit $uid ".($tot)); |
|---|
| 254 | } |
|---|
| 255 | return true; |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | function alternc_del_member($uid) { |
|---|
| 259 | $this->delquotas($uid); |
|---|
| 260 | } |
|---|
| 261 | |
|---|
| 262 | function alternc_add_member($uid) { |
|---|
| 263 | $this->addquotas($uid); |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | } /* Class m_quota */ |
|---|
| 267 | |
|---|
| 268 | ?> |
|---|