Index: modules/upload.module =================================================================== RCS file: /cvs/drupal/drupal/modules/Attic/upload.module,v retrieving revision 1.100.2.5 diff -u -p -r1.100.2.5 upload.module --- modules/upload.module 4 Jul 2006 09:24:33 -0000 1.100.2.5 +++ modules/upload.module 3 Aug 2006 16:08:49 -0000 @@ -101,6 +101,77 @@ function upload_menu($may_cache) { return $items; } +/** + * Helper function for upload_max_size() + */ +function _convert_to_MB($val){ + + $val = trim($val); + $last = strtolower($val{strlen($val)-1}); + switch($last) { + // The 'G' modifier is available since PHP 5.1.0 + case 'g': + $size = $val*1024; + break; + case 'k': + $size = $val/1024; + break; + default: + $size = (int)$val; + } + return $size; +} + +/** + * Query the PHP settings and return the max upload size in MB. + */ +function upload_max_size() { + static $max_size = -1; + + if ($max_size < 0) { + $upload_max = _convert_to_MB(ini_get('upload_max_filesize')); + // sanity check- a single upload should not be more than 50% the size limit of the total post + $post_max = _convert_to_MB(ini_get('post_max_size'))/2; + $max_size = ($upload_max < $post_max) ? $upload_max : $post_max; + } + return $max_size; +} + +/** + * Form API callback for the upload settings form. + */ +function upload_settings_form_validate($form_id, $form_values) { + + if (($form_values['upload_max_resolution'] != '0')){ + if (!preg_match("/^[0-9]+[x|*|X][0-9]+$/",$form_values['upload_max_resolution'])){ + form_set_error('upload_max_resolution',t('The maximum allowed image size expressed as WIDTHxHEIGHT (e.g. 640x480). Set to 0 for no restriction.')); + } + } + + foreach ($form_values['roles'] as $rid => $role) { + + $uploadsize = $form_values["upload_uploadsize_$rid"]; + $usersize = $form_values["upload_usersize_$rid"]; + + if (!is_numeric($uploadsize) || ($uploadsize <= 0)){ + form_set_error("upload_uploadsize_$rid", t('File size limits for %role must be numeric and greater than zero.', array('%role' => $role))); + } + + if (!is_numeric($usersize) || ($usersize <= 0)){ + form_set_error("upload_usersize_$rid", t('File size limits for %role must be numeric and greater than zero.', array('%role' => $role))); + } + + if ($uploadsize > upload_max_size()) { + form_set_error("upload_uploadsize_$rid", t('Your PHP settings limit the maximum upload filesize to %size MB.', array('%size' => upload_max_size()))); + } + + if ( $uploadsize > $usersize) { + form_set_error("upload_uploadsize_$rid", t('error').': '. + t('Maximum file size per upload').' > '.t('Total file size per user')); + } + } +} + function upload_settings() { $form['settings_general'] = array('#type' => 'fieldset', '#title' => t('General settings')); $form['settings_general']['upload_max_resolution'] = array( @@ -114,7 +185,10 @@ function upload_settings() { '#description' => t('Set whether files attached to nodes are listed or not in the node view by default.'), ); + $form['upload_max_size'] = array('#value' => t('Your PHP settings limit the maximum upload filesize to %size MB.', array('%size' => upload_max_size()))); $roles = user_roles(0, 'upload files'); + + $form['roles'] = array('#type' => 'value', '#value' => $roles); foreach ($roles as $rid => $role) { $form["settings_role_$rid"] = array('#type' => 'fieldset', '#title' => t('Settings for %role', array('%role' => theme('placeholder', $role))), '#collapsible' => TRUE, '#collapsed' => TRUE); @@ -131,6 +205,7 @@ function upload_settings() { '#size' => 5, '#maxlength' => 5, '#description' => t('The maximum size of all files a user can have on the site (in megabytes).') ); } +// Note: form_id constructed in system_site_settings() is 'upload_settings_form' return $form; }