1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297:
<?php
$plugin_is_filter = 5 | ADMIN_PLUGIN;
$plugin_description = gettext("Provides a quota management system to limit the sum of sizes of images a user uploads.");
$plugin_notice = gettext("<strong>Note:</strong> if FTP is used to upload images, manual user assignment is necessary. ZIP file upload is disabled by default as quotas are not applied to the files contained therein.");
$plugin_author = "Stephen Billard (sbillard)";
$plugin_disable = (zp_has_filter('get_upload_header_text') && !extensionEnabled('quota_manager')) ? sprintf(gettext('<a href="#%1$s"><code>%1$s</code></a> is already enabled.'), stripSuffix(get_filterScript('get_upload_header_text'))) : '';
$plugin_category = gettext('Users');
$option_interface = 'quota_manager';
if ($plugin_disable) {
enableExtension('quota_manager', 0);
} else {
zp_register_filter('save_admin_custom_data', 'quota_manager::save_admin');
zp_register_filter('edit_admin_custom_data', 'quota_manager::edit_admin');
zp_register_filter('new_image', 'quota_manager::new_image');
zp_register_filter('image_refresh', 'quota_manager::image_refresh');
zp_register_filter('check_upload_quota', 'quota_manager::checkQuota');
zp_register_filter('get_upload_limit', 'quota_manager::getUploadLimit');
zp_register_filter('get_upload_header_text', 'quota_manager::get_header');
zp_register_filter('upload_filetypes', 'quota_manager::upload_filetypes');
zp_register_filter('upload_helper_js', 'quota_manager::upload_helper_js');
}
class quota_manager {
function __construct() {
setOptionDefault('quota_default', 250000);
setOptionDefault('quota_allowZIP', 1);
}
function getOptionsSupported() {
return array(gettext('Default quota') => array('key' => 'quota_default', 'type' => OPTION_TYPE_TEXTBOX,
'desc' => gettext('Default size limit in kilobytes.')),
gettext('Allow ZIP files') => array('key' => 'quota_allowZIP', 'type' => OPTION_TYPE_CHECKBOX,
'desc' => gettext('The size of a ZIP file may be slightly smaller than the sum of the <em>image</em> files it contains. Un-check this box if you wish to disable uploading of ZIP files.'))
);
}
function handleOption($option, $currentValue) {
}
static function save_admin($updated, $userobj, $i, $alter) {
if (isset($_POST[$i . 'quota']) && $alter) {
$oldquota = $userobj->getQuota();
$userobj->setQuota(sanitize_numeric($_POST[$i . 'quota']));
if ($oldquota != $userobj->getQuota()) {
$updated = true;
}
}
return $updated;
}
static function edit_admin($html, $userobj, $i, $background, $current, $local_alterrights) {
if ($userobj->getRights() & (ADMIN_RIGHTS | MANAGE_ALL_ALBUM_RIGHTS))
return $html;
if (!($userobj->getRights() & UPLOAD_RIGHTS))
return $html;
$quota = $userobj->getQuota();
if ($quota == NULL) {
$quota = getOption('quota_default');
}
if ($userobj->getValid()) {
$used = sprintf(gettext('(%s kb used)'), number_format(quota_manager::getCurrentUse($userobj)));
} else {
$used = '';
}
$result =
'<tr' . ((!$current) ? ' style="display:none;"' : '') . ' class="userextrainfo">
<td colspan="2"' . ((!empty($background)) ? ' style="' . $background . '"' : '') . ' valign="top" width="345">' . gettext("Image storage quota:") . ' ' .
sprintf(gettext('Allowed: %s kb'), '<input type="text" size="10" name="' . $i . 'quota" value="' . $quota . '" ' . $local_alterrights . ' />') . ' ' .
$used .
"\n" .
'</td>' .
'</tr>' . "\n";
return $html . $result;
}
static function getCurrentUse($userobj) {
global $_zp_current_admin_obj;
if (is_null($userobj)) {
$userobj = $_zp_current_admin_obj;
}
$sql = 'SELECT sum(`filesize`) FROM ' . prefix('images') . ' WHERE `owner`="' . $userobj->getUser() . '"';
$result = query_single_row($sql);
return array_shift($result) / 1024;
}
static function new_image($image) {
global $_zp_current_admin_obj;
if (is_object($_zp_current_admin_obj)) {
$image->set('owner', $_zp_current_admin_obj->getUser());
}
$image->set('filesize', filesize($image->localpath));
$image->setLastChangeUser($_zp_current_admin_obj->getUser());
$image->save();
return $image;
}
static function image_refresh($image) {
global $_zp_current_admin_obj;
$image->set('filesize', filesize($image->localpath));
$image->setLastChangeUser($_zp_current_admin_obj->getUser());
$image->save();
return $image;
}
static function getUploadQuota($quota) {
global $_zp_current_admin_obj;
if (zp_loggedin(MANAGE_ALL_ALBUM_RIGHTS)) {
$quota = -1;
} else {
$quota = $_zp_current_admin_obj->getQuota();
if ($quota == NULL)
$quota = getOption('quota_default');
}
return $quota;
}
static function getUploadLimit($uploadlimit) {
if (!zp_loggedin(MANAGE_ALL_ALBUM_RIGHTS)) {
$uploadlimit = (quota_manager::getUploadQuota(0) - quota_manager::getCurrentUse(NULL)) * 1024;
}
return $uploadlimit;
}
static function checkQuota($error, $image) {
if (zp_loggedin(MANAGE_ALL_ALBUM_RIGHTS)) {
return UPLOAD_ERR_OK;
}
if (getSuffix($image) == 'zip') {
return UPLOAD_ERR_EXTENSION;
}
$quota = quota_manager::getUploadQuota(0);
if ($image) {
$size = round(filesize($image) / 1024);
} else {
$size = 0;
}
if ($quota > 0) {
if (quota_manager::getCurrentUse(NULL) + $size >= $quota) {
$error = UPLOAD_ERR_QUOTA;
}
}
return $error;
}
static function get_header($default) {
if (zp_loggedin(MANAGE_ALL_ALBUM_RIGHTS)) {
return $default;
}
$uploadlimit = quota_manager::getUploadLimit(0);
if ($uploadlimit <= 1024) {
$color = 'style="color:red;"';
$warn = ' <span style="color:red;">' . gettext('Uploading is disabled.') . '</span>';
} else {
$color = '';
$warn = '';
}
return sprintf(gettext('Your available upload quota is <span %1$s>%2$s</span> kb.'), $color, number_format(round($uploadlimit / 1024))) . $warn;
}
static function upload_helper_js($defaultJS) {
$quota = quota_manager::getUploadLimit(99999);
$quotaOK = $quota < 0 || $quota > 1024;
if ($quotaOK) {
$quota_management_js = '';
} else {
$quota_management_js = "
$(document).ready(function() {
$('#albumselect').hide();
});";
}
return $quota_management_js . $defaultJS;
}
static function upload_filetypes($types) {
if (zp_loggedin(MANAGE_ALL_ALBUM_RIGHTS) || (getoption('quota_allowZIP'))) {
return $types;
}
$key = array_search('ZIP', $types);
if ($key !== false)
unset($types[$key]);
return $types;
}
}
?>