allow engineering units (e.g. 400M, 1G) as service class limits

This commit is contained in:
redmatrix 2016-04-28 21:02:27 -07:00
parent 30a6ae3daa
commit bb96f44861
9 changed files with 16 additions and 13 deletions

View File

@ -26,7 +26,7 @@ class Hook {
$r = q("DELETE FROM `hook` where `hook` = '%s' and `file` = '%s' and `function` = '%s'", $r = q("DELETE FROM `hook` where `hook` = '%s' and `file` = '%s' and `function` = '%s'",
dbesc($hook), dbesc($hook),
dbesc($file), dbesc($file),
dbesc($function), dbesc($function)
); );
$r = q("INSERT INTO `hook` (`hook`, `file`, `function`, `priority`, `hook_version`) VALUES ( '%s', '%s', '%s', %d, %d )", $r = q("INSERT INTO `hook` (`hook`, `file`, `function`, `priority`, `hook_version`) VALUES ( '%s', '%s', '%s', %d, %d )",
@ -61,7 +61,7 @@ class Hook {
static public function unregister_by_file($file) { static public function unregister_by_file($file) {
$r = q("DELETE FROM hook WHERE `file` = '%s' ", $r = q("DELETE FROM hook WHERE `file` = '%s' ",
dbesc($file), dbesc($file)
); );
return $r; return $r;

View File

@ -1243,14 +1243,14 @@ class Item extends \Zotlabs\Web\Controller {
} }
if (!$iswebpage) { if (!$iswebpage) {
$max = service_class_fetch($channel_id,'total_items'); $max = engr_units_to_bytes(service_class_fetch($channel_id,'total_items'));
if(! service_class_allows($channel_id,'total_items',$r[0]['total'])) { if(! service_class_allows($channel_id,'total_items',$r[0]['total'])) {
$result['message'] .= upgrade_message() . sprintf( t('You have reached your limit of %1$.0f top level posts.'),$max); $result['message'] .= upgrade_message() . sprintf( t('You have reached your limit of %1$.0f top level posts.'),$max);
return $result; return $result;
} }
} }
else { else {
$max = service_class_fetch($channel_id,'total_pages'); $max = engr_units_to_bytes(service_class_fetch($channel_id,'total_pages'));
if(! service_class_allows($channel_id,'total_pages',$r[0]['total'])) { if(! service_class_allows($channel_id,'total_pages',$r[0]['total'])) {
$result['message'] .= upgrade_message() . sprintf( t('You have reached your limit of %1$.0f webpages.'),$max); $result['message'] .= upgrade_message() . sprintf( t('You have reached your limit of %1$.0f webpages.'),$max);
return $result; return $result;

View File

@ -615,7 +615,7 @@ class Photos extends \Zotlabs\Web\Controller {
); );
$limit = service_class_fetch(\App::$data['channel']['channel_id'],'photo_upload_limit'); $limit = engr_units_to_bytes(service_class_fetch(\App::$data['channel']['channel_id'],'photo_upload_limit'));
if($limit !== false) { if($limit !== false) {
$usage_message = sprintf( t("%1$.2f MB of %2$.2f MB photo storage used."), $r[0]['total'] / 1024000, $limit / 1024000 ); $usage_message = sprintf( t("%1$.2f MB of %2$.2f MB photo storage used."), $r[0]['total'] / 1024000, $limit / 1024000 );
} }

View File

@ -273,7 +273,7 @@ class Browser extends DAV\Browser\Plugin {
return; return;
// Storage and quota for the account (all channels of the owner of this directory)! // Storage and quota for the account (all channels of the owner of this directory)!
$limit = service_class_fetch($owner, 'attach_upload_limit'); $limit = engr_units_to_bytes(service_class_fetch($owner, 'attach_upload_limit'));
$r = q("SELECT SUM(filesize) AS total FROM attach WHERE aid = %d", $r = q("SELECT SUM(filesize) AS total FROM attach WHERE aid = %d",
intval($this->auth->channel_account_id) intval($this->auth->channel_account_id)
); );

View File

@ -315,13 +315,13 @@ class Directory extends DAV\Node implements DAV\ICollection, DAV\IQuota {
} }
// check against service class quota // check against service class quota
$limit = service_class_fetch($c[0]['channel_id'], 'attach_upload_limit'); $limit = engr_units_to_bytes(service_class_fetch($c[0]['channel_id'], 'attach_upload_limit'));
if ($limit !== false) { if ($limit !== false) {
$x = q("SELECT SUM(filesize) AS total FROM attach WHERE aid = %d ", $x = q("SELECT SUM(filesize) AS total FROM attach WHERE aid = %d ",
intval($c[0]['channel_account_id']) intval($c[0]['channel_account_id'])
); );
if (($x) && ($x[0]['total'] + $size > $limit)) { if (($x) && ($x[0]['total'] + $size > $limit)) {
logger('service class limit exceeded for ' . $c[0]['channel_name'] . ' total usage is ' . $x[0]['total'] . ' limit is ' . $limit); logger('service class limit exceeded for ' . $c[0]['channel_name'] . ' total usage is ' . $x[0]['total'] . ' limit is ' . userReadableSize($limit));
attach_delete($c[0]['channel_id'], $hash); attach_delete($c[0]['channel_id'], $hash);
return; return;
} }
@ -549,7 +549,7 @@ class Directory extends DAV\Node implements DAV\ICollection, DAV\IQuota {
intval($this->auth->owner_id) intval($this->auth->owner_id)
); );
$ulimit = service_class_fetch($c[0]['channel_id'], 'attach_upload_limit'); $ulimit = engr_units_to_bytes(service_class_fetch($c[0]['channel_id'], 'attach_upload_limit'));
$limit = (($ulimit) ? $ulimit : $limit); $limit = (($ulimit) ? $ulimit : $limit);
$x = q("select sum(filesize) as total from attach where aid = %d", $x = q("select sum(filesize) as total from attach where aid = %d",

View File

@ -208,13 +208,13 @@ class File extends DAV\Node implements DAV\IFile {
return; return;
} }
$limit = service_class_fetch($c[0]['channel_id'], 'attach_upload_limit'); $limit = engr_units_to_bytes(service_class_fetch($c[0]['channel_id'], 'attach_upload_limit'));
if ($limit !== false) { if ($limit !== false) {
$x = q("select sum(filesize) as total from attach where aid = %d ", $x = q("select sum(filesize) as total from attach where aid = %d ",
intval($c[0]['channel_account_id']) intval($c[0]['channel_account_id'])
); );
if (($x) && ($x[0]['total'] + $size > $limit)) { if (($x) && ($x[0]['total'] + $size > $limit)) {
logger('service class limit exceeded for ' . $c[0]['channel_name'] . ' total usage is ' . $x[0]['total'] . ' limit is ' . $limit); logger('service class limit exceeded for ' . $c[0]['channel_name'] . ' total usage is ' . $x[0]['total'] . ' limit is ' . userReadableSize($limit));
attach_delete($c[0]['channel_id'], $this->data['hash']); attach_delete($c[0]['channel_id'], $this->data['hash']);
return; return;
} }

View File

@ -591,6 +591,7 @@ function service_class_allows($uid, $property, $usage = false) {
if($limit === false) if($limit === false)
return true; // No service class set => everything is allowed return true; // No service class set => everything is allowed
$limit = engr_units_to_bytes($limit);
if($usage === false) { if($usage === false) {
// We use negative values for not allowed properties in a subscriber plan // We use negative values for not allowed properties in a subscriber plan
return ((x($limit)) ? (bool) $limit : true); return ((x($limit)) ? (bool) $limit : true);
@ -627,6 +628,8 @@ function account_service_class_allows($aid, $property, $usage = false) {
if($limit === false) if($limit === false)
return true; // No service class is set => everything is allowed return true; // No service class is set => everything is allowed
$limit = engr_units_to_bytes($limit);
if($usage === false) { if($usage === false) {
// We use negative values for not allowed properties in a subscriber plan // We use negative values for not allowed properties in a subscriber plan
return ((x($limit)) ? (bool) $limit : true); return ((x($limit)) ? (bool) $limit : true);

View File

@ -131,7 +131,7 @@ function photo_upload($channel, $observer, $args) {
intval($account_id) intval($account_id)
); );
$limit = service_class_fetch($channel_id,'photo_upload_limit'); $limit = engr_units_to_bytes(service_class_fetch($channel_id,'photo_upload_limit'));
if (($r) && ($limit !== false) && (($r[0]['total'] + strlen($imagedata)) > $limit)) { if (($r) && ($limit !== false) && (($r[0]['total'] + strlen($imagedata)) > $limit)) {
$ret['message'] = upgrade_message(); $ret['message'] = upgrade_message();

View File

@ -1825,7 +1825,7 @@ function lang_selector() {
} }
function return_bytes ($size_str) { function engr_units_to_bytes ($size_str) {
switch (substr ($size_str, -1)) { switch (substr ($size_str, -1)) {
case 'M': case 'm': return (int)$size_str * 1048576; case 'M': case 'm': return (int)$size_str * 1048576;
case 'K': case 'k': return (int)$size_str * 1024; case 'K': case 'k': return (int)$size_str * 1024;