--- upload.module.orig Tue Jul 4 13:00:02 2006 +++ upload.module Sun Jul 23 23:49:42 2006 @@ -101,6 +101,60 @@ function upload_menu($may_cache) { return $items; } +/* + * Query the PHP settings and return the max upload size in MB. + * + */ +function upload_max_size() { + static $max_size = -1; + + if ($max_size < 0) { + $max= ini_get('upload_max_filesize'); + $max = trim($max); + $last = strtolower($max{strlen($max)-1}); + switch($last) { + // The 'G' modifier is available since PHP 5.1.0 + case 'g': + $max_size = (int)$max*1024; + break; + case 'k': + $max_size = (int)($max/1024); + break; + default: + $max_size = (int)$max; + } + } + return $max_size; +} + +/** + * Form API callback for the upload settings form. + */ +function upload_settings_form_validate($form_id, $form_values) { + + $roles = user_roles(0, 'upload files'); + + foreach ($roles as $rid => $role) { + + $uploadsize = $form_values["upload_uploadsize_$rid"]; + $usersize = $form_values["upload_usersize_$rid"]; + + if (!is_numeric($uploadsize) || !is_numeric($usersize)){ + form_set_error('', t('Files size limits for %role must be numeric and greater than zero.', array('%role' => $role))); + } + elseif (($uploadsize <= 0) || ($usersize <= 0)) { + form_set_error('', t('Files size limits for %role must be numeric and greater than zero.', array('%role' => $role))); + } + elseif ($uploadsize > upload_max_size()) { + form_set_error('', t('Your PHP settings limit the maximum upload filesize to %size MB', array('%size' => upload_max_size()))); + } + elseif ( $uploadsize > $usersize) { + form_set_error('', t('Settings for %role', array('%role' => theme('placeholder', $role))).': '. + 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,6 +168,7 @@ 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'); foreach ($roles as $rid => $role) { @@ -131,6 +186,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).') ); } + $form['#validate'] = array('upload_settings_form_validate' => array()); return $form; }