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