Alternc  3.2
Alternc logiel libre pour l'hébergement
 All Data Structures Namespaces Files Functions Variables Pages
m_quota Class Reference

Class for hosting quotas management. More...

Public Member Functions

 m_quota ()
 Constructor.
 hook_menu ()
 cancreate ($ressource="")
 Check if a user can use a ressource.
 qlist ()
 List the quota-managed services in the server array the quota names and description (translated)
 synchronise_user_profile ()
 Synchronise the quotas of the users with the quota of the user's profile.
 create_missing_quota_profile ()
 getquota ($ressource="", $recheck=false)
 Return a ressource usage (u) and total quota (t)
 setquota ($ressource, $size)
 Set the quota for a user (and for a ressource)
 delquotas ()
 Erase all quota information about the user.
 getdefaults ()
 Get the default quotas as an associative array.
 setdefaults ($newq)
 Set the default quotas.
 addtype ($type)
 Add an account type for quotas.
 listtype ()
 List for quotas.
 deltype ($type)
 Delete an account type for quotas.
 addquotas ()
 Create default quotas entries for a new user.
 display_val ($type, $value)
 Return a quota value with its unit (when it is a space quota) in MB, GB, TB ...
 _get_sum_sql ($sql)
 _get_count_sql ($sql)
 _get_size_and_record_sql ($sql)
 get_size_web_sum_all ()
 get_size_web_sum_user ($u)
 get_size_mail_sum_all ()
 get_size_mail_sum_domain ($dom)
 get_size_mail_count_all ()
 get_size_mail_count_domain ($dom)
 get_size_mail_details_domain ($dom)
 get_size_mailman_sum_all ()
 get_size_mailman_sum_domain ($dom)
 get_size_mailman_sum_user ($u)
 get_size_mailman_count_all ()
 get_size_mailman_count_user ($u)
 get_size_mailman_details_user ($u)
 get_size_db_sum_all ()
 get_size_db_sum_user ($u)
 get_size_db_count_all ()
 get_size_db_count_user ($u)
 get_size_db_details_user ($u)
 get_size_unit ($size)
 quota_displaybar ($usage, $color_type=1)
 hook_admin_del_member ()
 Hook function call when a user is deleted AlternC's standard function called when a user is deleted globals $cuid is the appropriate user.
 hook_admin_add_member ()
 Hook function called when a user is created This function initialize the user's quotas.
 alternc_export_conf ()
 Exports all the quota related information for an account.

Data Fields

 $disk = Array()
 $disk_quota_enable
 $quotas
 $clquota

Private Member Functions

 dummy_for_translation ()

Detailed Description

Class for hosting quotas management.

This class manages services' quotas for each user of AlternC. The available quotas for each service is stored in the system.quotas mysql table. The used value is computed by the class using a callback function alternc_quota_check($uid) that may by exported by each service class.
each class may also export a function alternc_quota_names() that returns an array with the quotas names managed by this class.

Definition at line 38 of file m_quota.php.

Member Function Documentation

_get_count_sql (   $sql)

Definition at line 419 of file m_quota.php.

References $cuid, $db, $err, and $r.

Referenced by get_size_db_count_all(), get_size_db_count_user(), get_size_mail_count_all(), get_size_mail_count_domain(), get_size_mailman_count_all(), and get_size_mailman_count_user().

{
global $db,$err,$cuid;
$db->query($sql);
if ($db->num_rows() == 0) {
return 0;
} else {
$db->next_record();
$r = $db->Record;
return $r['count'];
}
}
_get_size_and_record_sql (   $sql)

Definition at line 431 of file m_quota.php.

References $cuid, $db, and $err.

Referenced by get_size_db_details_user(), get_size_mail_details_domain(), and get_size_mailman_details_user().

{
global $db,$err,$cuid;
$db->query($sql);
if ($db->num_rows() == 0) {
return array();
} else {
$ret = array();
while ($db->next_record()) {
$ret[] = $db->Record;
}
return $ret;
}
}
_get_sum_sql (   $sql)

Definition at line 407 of file m_quota.php.

References $cuid, $db, $err, and $r.

Referenced by get_size_db_sum_all(), get_size_db_sum_user(), get_size_mail_sum_all(), get_size_mailman_sum_all(), get_size_mailman_sum_domain(), get_size_mailman_sum_user(), get_size_web_sum_all(), and get_size_web_sum_user().

{
global $db,$err,$cuid;
$db->query($sql);
if ($db->num_rows() == 0) {
return -1;
} else {
$db->next_record();
$r = $db->Record;
return $r['sum'];
}
}
addquotas ( )

Create default quotas entries for a new user.

The user we are talking about is in the global $cuid.

Definition at line 364 of file m_quota.php.

References $cuid, $db, $err, $q, $res, $t, $val, addtype(), qlist(), and setquota().

Referenced by hook_admin_add_member().

{
global $db,$err,$cuid;
$err->log("quota","addquota");
$ql=$this->qlist();
reset($ql);
$db->query("SELECT type,quota FROM defquotas WHERE type='default'");
if(!$db->next_record())
$this->addtype('default');
$db->query("SELECT type FROM membres WHERE uid='$cuid'");
$db->next_record();
$t = $db->f("type");
foreach($ql as $res => $val) {
$db->query("SELECT value FROM defquotas WHERE quota='$res' AND type='$t'");
$q = $db->next_record() ? $db->f("value") : 0;
$this->setquota($res, $q);
}
return true;
}
addtype (   $type)

Add an account type for quotas.

Parameters
string$typeaccount type to be added
Returns
boolean true if all went ok

Definition at line 309 of file m_quota.php.

References $db, $err, $key, $qlist, $val, and qlist().

Referenced by addquotas(), and getdefaults().

{
global $db,$err;
$qlist=$this->qlist();
if(empty($type)) return false;
$type=strtolower($type);
if (!preg_match("#^[a-z0-9]*$#",$type)) {
$err->raise("quota", "Type can only contains characters a-z and 0-9");
return false;
}
while (list($key,$val)=each($qlist)) {
if(!$db->query("INSERT IGNORE INTO defquotas (quota,type) VALUES('$key', '$type');")
|| $db->affected_rows() == 0)
return false;
}
return true;
}
alternc_export_conf ( )

Exports all the quota related information for an account.

private EXPERIMENTAL function ;)

Definition at line 599 of file m_quota.php.

References $db, $err, $q, and getquota().

{
global $db,$err;
$err->log("quota","export");
$str=" <quota>";
$q=$this->getquota();
foreach ($q as $k=>$v) {
$str.=" <$k>\n";
$str.=" <used>".($v["u"])."</used>\n";
$str.=" <total>".($v["t"])."</total>\n";
$str.=" </$k>\n";
}
$str.="</quota>\n";
return $str;
}
cancreate (   $ressource = "")

Check if a user can use a ressource.

Parameters
string$ressourcethe ressource name (a named quota) TRUE if the user can create a ressource (= is there any quota left ?)

Definition at line 90 of file m_quota.php.

References $t, and getquota().

{
$t=$this->getquota($ressource);
return $t["u"]<$t["t"];
}
create_missing_quota_profile ( )

Definition at line 137 of file m_quota.php.

References $db, $err, $q, and $t.

{
global $db,$quota,$err;
$err->log("quota","create_missing_quota_profile");
$qt=$quota->getquota('',true);
$type=$quota->listtype();
foreach($type as $t) {
foreach($qt as $q=>$vv) {
$db->query("INSERT IGNORE defquotas (value,quota,type) VALUES (0,'$q','$t');");
}
}
return true;
}
delquotas ( )

Erase all quota information about the user.

Definition at line 255 of file m_quota.php.

References $cuid, $db, and $err.

Referenced by hook_admin_del_member().

{
global $db,$err,$cuid;
$err->log("quota","delquota");
$db->query("DELETE FROM quotas WHERE uid='$cuid';");
return true;
}
deltype (   $type)

Delete an account type for quotas.

Parameters
string$typeaccount type to be deleted
Returns
boolean true if all went ok

Definition at line 347 of file m_quota.php.

References $db, $qlist, and qlist().

{
global $db;
$qlist=$this->qlist();
if($db->query("UPDATE membres SET type='default' WHERE type='$type'") &&
$db->query("DELETE FROM defquotas WHERE type='$type'")) {
return true;
} else {
return false;
}
}
display_val (   $type,
  $value 
)

Return a quota value with its unit (when it is a space quota) in MB, GB, TB ...

Parameters
string$typeThe quota type
integer$valueThe quota value
Returns
string a quota value with its unit.

Definition at line 394 of file m_quota.php.

References $value, and format_size().

{
switch ($type) {
case 'bw_web':
case 'web':
return format_size($value*1024);
default:
return $value;
}
}
dummy_for_translation ( )
private

Definition at line 59 of file m_quota.php.

{
_("quota_web");
}
get_size_db_count_all ( )

Definition at line 522 of file m_quota.php.

References _get_count_sql().

{
return $this->_get_count_sql("SELECT COUNT(*) AS count FROM size_db;");
}
get_size_db_count_user (   $u)

Definition at line 527 of file m_quota.php.

References _get_count_sql().

{
return $this->_get_count_sql("SELECT COUNT(*) AS count FROM size_db WHERE db = '{$u}' OR db LIKE '{$u}\_%'");
}
get_size_db_details_user (   $u)

Definition at line 532 of file m_quota.php.

References _get_size_and_record_sql().

{
return $this->_get_size_and_record_sql("SELECT db,size FROM size_db WHERE db='{$u}' OR db LIKE '{$u}\_%';");
}
get_size_db_sum_all ( )

Definition at line 512 of file m_quota.php.

References _get_sum_sql().

{
return $this->_get_sum_sql("SELECT SUM(size) AS sum FROM size_db;");
}
get_size_db_sum_user (   $u)

Definition at line 517 of file m_quota.php.

References _get_sum_sql().

{
return $this->_get_sum_sql("SELECT SUM(size) AS sum FROM size_db WHERE db = '{$u}' OR db LIKE '{$u}\_%'");
}
get_size_mail_count_all ( )

Definition at line 467 of file m_quota.php.

References _get_count_sql().

{
return $this->_get_count_sql("SELECT COUNT(*) AS count FROM mailbox;");
}
get_size_mail_count_domain (   $dom)

Definition at line 472 of file m_quota.php.

References _get_count_sql().

{
return $this->_get_count_sql("SELECT COUNT(*) AS count FROM dovecot_view WHERE user LIKE '%@{$dom}'");
}
get_size_mail_details_domain (   $dom)

Definition at line 477 of file m_quota.php.

References _get_size_and_record_sql().

{
return $this->_get_size_and_record_sql("SELECT user as alias,quota_dovecot as size FROM dovecot_view WHERE user LIKE '%@{$dom}' ORDER BY alias;");
}
get_size_mail_sum_all ( )

Definition at line 456 of file m_quota.php.

References _get_sum_sql().

{
return $this->_get_sum_sql("SELECT SUM(bytes) AS sum FROM mailbox;");
}
get_size_mail_sum_domain (   $dom)

Definition at line 461 of file m_quota.php.

References $dom.

{
global $mail;
return $mail->get_total_size_for_domain($dom);
}
get_size_mailman_count_all ( )

Definition at line 497 of file m_quota.php.

References _get_count_sql().

{
return $this->_get_count_sql("SELECT COUNT(*) AS count FROM size_mailman;");
}
get_size_mailman_count_user (   $u)

Definition at line 502 of file m_quota.php.

References _get_count_sql().

{
return $this->_get_count_sql("SELECT COUNT(*) AS count FROM size_mailman WHERE uid = '{$u}'");
}
get_size_mailman_details_user (   $u)

Definition at line 507 of file m_quota.php.

References _get_size_and_record_sql().

{
return $this->_get_size_and_record_sql("SELECT s.size,CONCAT(m.list,'@',m.domain) as list FROM size_mailman s LEFT JOIN mailman m ON s.list=m.name WHERE s.uid='{$u}' ORDER BY s.list ASC");
}
get_size_mailman_sum_all ( )

Definition at line 482 of file m_quota.php.

References _get_sum_sql().

{
return $this->_get_sum_sql("SELECT SUM(size) AS sum FROM size_mailman;");
}
get_size_mailman_sum_domain (   $dom)

Definition at line 487 of file m_quota.php.

References _get_sum_sql().

{
return $this->_get_sum_sql("SELECT SUM(size) AS sum FROM size_mailman WHERE list LIKE '%@{$dom}'");
}
get_size_mailman_sum_user (   $u)

Definition at line 492 of file m_quota.php.

References _get_sum_sql().

{
return $this->_get_sum_sql("SELECT SUM(size) AS sum FROM size_mailman WHERE uid = '{$u}'");
}
get_size_unit (   $size)

Definition at line 537 of file m_quota.php.

References $units, and $value.

{
$units=array(1073741824=>_("GB"), 1048576=>_("MB"), 1024=>_("KB"), 0=>_("B"));
foreach($units as $value=>$unit){
if($size>=$value){
$size=str_pad(round($size/($value ? $value : 1), 1), 5, ' ', STR_PAD_LEFT);
return array('size'=>$size, 'unit'=>$unit);
}
}
}
get_size_web_sum_all ( )

Definition at line 446 of file m_quota.php.

References _get_sum_sql().

{
return $this->_get_sum_sql("SELECT SUM(size) AS sum FROM size_web;");
}
get_size_web_sum_user (   $u)

Definition at line 451 of file m_quota.php.

References _get_sum_sql().

{
return $this->_get_sum_sql("SELECT SUM(size) AS sum FROM size_web WHERE uid='$u';");
}
getdefaults ( )

Get the default quotas as an associative array.

Returns
array the array of the default quotas

Definition at line 267 of file m_quota.php.

References $c, $db, and addtype().

{
global $db;
$c=array();
$db->query("SELECT type,quota FROM defquotas WHERE type='default'");
if(!$db->next_record())
$this->addtype('default');
$db->query("SELECT value,quota,type FROM defquotas ORDER BY type,quota");
while($db->next_record()) {
$type = $db->f("type");
$c[$type][$db->f("quota")] = $db->f("value");
}
return $c;
}
getquota (   $ressource = "",
  $recheck = false 
)

Return a ressource usage (u) and total quota (t)

Parameters
string$ressourceressource to get quota of array the quota used and total for this ressource (or for all ressource if unspecified)

Definition at line 155 of file m_quota.php.

References $cuid, $db, $err, $hooks, $key, $mem, $quotas, $r, $res, $val, and isset.

Referenced by alternc_export_conf(), cancreate(), hook_admin_add_member(), hook_menu(), and qlist().

{
global $db,$err,$cuid,$get_quota_cache,$hooks,$mem;
$err->log("quota","getquota",$ressource);
if ($recheck) { // rebuilding quota
$get_quota_cache=null;
$this->quota=array();
}
if (! empty($get_quota_cache[$cuid]) ) {
// This function is called many time each webpage, so I cache the result
$this->quotas = $get_quota_cache[$cuid];
} else {
$res=$hooks->invoke("hook_quota_get");
foreach($res as $r) {
$this->quotas[$r['name']]=$r;
$this->quotas[$r['name']]['u']=$r['used']; // retrocompatibilité
$this->quotas[$r['name']]['t']=0; // Default quota = 0
}
reset($this->disk);
if (!empty ($this->disk)) { // Check if there are some disk quota to check
// Look if there are some cached value
$disk_cached = $mem->session_tempo_params_get('quota_cache_disk');
while (list($key,$val)=each($this->disk)) {
$a=array();
if (
isset($disk_cached[$val])
&& !empty($disk_cached[$val])
&& $disk_cached[$val]['uid'] == $cuid
&& $disk_cached[$val]['timestamp'] > ( time() - (90) ) // Cache, en seconde
) {
// If there is a cached value
$a = $disk_cached[$val];
} else {
exec("/usr/lib/alternc/quota_get ".$cuid ,$ak);
$a['u']=intval($ak[0]);
$a['t']=@intval($ak[1]);
$a['timestamp'] = time();
$a['uid'] = $cuid;
$disk_cached = $mem->session_tempo_params_set('quota_cache_disk', array($val=>$a));
}
$this->quotas[$val]=array("name"=>"$val", 'description'=>_("quota_".$val), "t"=>$a['t'],"u"=>$a['u']);
}
}
// Get the allowed quota from database.
$db->query("select name, total from quotas where uid='$cuid';");
while ( $db->next_record() ) {
$this->quotas[$db->f('name')]['t']=$db->f('total');
}
$get_quota_cache[$cuid] = $this->quotas;
}
if ($ressource) {
if (isset($this->quotas[$ressource]) ) {
return $this->quotas[$ressource];
} else {
return 0;
}
} else {
return $this->quotas;
}
}
hook_admin_add_member ( )

Hook function called when a user is created This function initialize the user's quotas.

globals $cuid is the appropriate user

Definition at line 586 of file m_quota.php.

References $err, addquotas(), and getquota().

{
global $err;
$err->log("quota","hook_admin_add_member");
$this->addquotas();
$this->getquota('',true); // actualise quota
}
hook_admin_del_member ( )

Hook function call when a user is deleted AlternC's standard function called when a user is deleted globals $cuid is the appropriate user.

Definition at line 576 of file m_quota.php.

References delquotas().

{
$this->delquotas();
}
hook_menu ( )

Definition at line 63 of file m_quota.php.

References $key, $q, format_size(), getquota(), and isset.

{
$obj = array(
'title' => _("Show my quotas"),
'ico' => 'images/quota.png',
'link' => 'toggle',
'pos' => 110,
'divclass' => 'menu-quota',
'links' => array(),
) ;
$q=$this->getquota();
foreach ( array('web', 'bw_web') as $key ) {
if ( ! isset($q[$key]["u"]) || empty($q[$key]["t"])) continue;
$usage_percent = (int) ($q[$key]["u"] / $q[$key]["t"] * 100);
$obj['links'][] = array( 'txt'=>_("quota_".$key) . " " . sprintf(_("%s%% of %s"),$usage_percent,format_size($q[$key]["t"]*1024)), 'url'=>($key == 'bw_web' ? 'stats_show_per_month.php' : 'quota_show.php') );
$obj['links'][] = array( 'txt'=>'progressbar', 'total' => $q[$key]["t"], 'used' => $q[$key]["u"]);
}
return $obj;
}
listtype ( )

List for quotas.

Returns
array

Definition at line 331 of file m_quota.php.

References $db, and $t.

{
global $db;
$db->query("SELECT distinct(type) FROM defquotas ORDER by type");
$t=array();
while ($db->next_record()) {
$t[] = $db->f("type");
}
return $t;
}
m_quota ( )

Constructor.

Definition at line 51 of file m_quota.php.

References variable_get().

{
$this->disk_quota_enable = variable_get('disk_quota_enable', 1,'Are disk quota enabled for this server', array(array('desc'=>'Enabled','type'=>'boolean')));
if ( $this->disk_quota_enable ) {
$this->disk = Array( "web"=>"web" );
}
}
qlist ( )

List the quota-managed services in the server array the quota names and description (translated)

Definition at line 100 of file m_quota.php.

References $classes, $hooks, $key, $qlist, $val, getquota(), and isset.

Referenced by addquotas(), addtype(), deltype(), and setdefaults().

{
global $classes,$hooks;
$qlist=array();
reset($this->disk);
while (list($key,$val)=each($this->disk)) {
$qlist[$key]=_("quota_".$key); // those are specific disks quotas.
}
foreach($this->getquota() as $qq) {
if (isset($qq['name'])) {
$qlist[$qq['name']]=$qq['description'];
}
}
return $qlist;
}
quota_displaybar (   $usage,
  $color_type = 1 
)

Definition at line 552 of file m_quota.php.

{
if ($color_type == 1) {
$csscolor = " background-color:".PercentToColor($usage);
} elseif ($color_type == 2) {
$csscolor = " background-color:".PercentToColor(100-$usage);
} else {
$csscolor = "";
}
echo '<div class="progress-bar">';
echo '<div class="barre" style="width:'.$usage.'%;'.$csscolor.'" ></div>';
echo '<div class="txt">'.$usage.'%</div>';
echo '</div>';
}
setdefaults (   $newq)

Set the default quotas.

Parameters
arrayassociative array of quota (key=>val)

Definition at line 288 of file m_quota.php.

References $db, $qlist, $quotas, $value, and qlist().

{
global $db;
$qlist=$this->qlist();
foreach($newq as $type => $quotas) {
foreach($quotas as $qname => $value) {
if(array_key_exists($qname, $qlist)) {
if(!$db->query("REPLACE INTO defquotas (value,quota,type) VALUES ($value,'$qname','$type');"))
return false;
}
}
}
return true;
}
setquota (   $ressource,
  $size 
)

Set the quota for a user (and for a ressource)

Parameters
string$ressourceressource to set quota of
integersize of the quota (available or used)

Definition at line 226 of file m_quota.php.

References $cuid, $db, $err, and isset.

Referenced by addquotas().

{
global $err,$db,$cuid;
$err->log("quota","setquota",$ressource."/".$size);
if (floatval($size)==0) $size="0";
if (isset($this->disk[$ressource])) {
// It's a disk resource, update it with shell command
exec("sudo /usr/lib/alternc/quota_edit $cuid $size &> /dev/null &");
// Now we check that the value has been written properly :
exec("sudo /usr/lib/alternc/quota_get $cuid &> /dev/null &",$a);
if (!isset($a[1]) || $size!=$a[1]) {
$err->raise("quota",_("Error writing the quota entry!"));
return false;
}
}
// We check that this ressource exists for this client :
$db->query("SELECT * FROM quotas WHERE uid='$cuid' AND name='$ressource'");
if ($db->num_rows()) {
$db->query("UPDATE quotas SET total='$size' WHERE uid='$cuid' AND name='$ressource';");
} else {
$db->query("INSERT INTO quotas (uid,name,total) VALUES ('$cuid','$ressource','$size');");
}
return true;
}
synchronise_user_profile ( )

Synchronise the quotas of the users with the quota of the user's profile.

If the user have a greater quota than the profile, no change. If the quota entry doesn't exist for the user, create it with the defaults value.

Definition at line 124 of file m_quota.php.

References $db, $err, and $q.

{
global $db,$err;
$err->log("quota","synchronise_user_profile");
$q="insert into quotas select m.uid as uid, d.quota as name, d.value as total from membres m, defquotas d left join quotas q on q.name=d.quota where m.type=d.type ON DUPLICATE KEY UPDATE total = greatest(d.value, quotas.total);";
if (!$db->query($q)) return false;
return true;
}

Field Documentation

$clquota

Definition at line 44 of file m_quota.php.

$disk = Array()

Definition at line 40 of file m_quota.php.

$disk_quota_enable

Definition at line 42 of file m_quota.php.

$quotas

Definition at line 43 of file m_quota.php.

Referenced by getquota(), and setdefaults().


The documentation for this class was generated from the following file: