upload.admin.inc

File

drupal-6.x/modules/upload/upload.admin.inc
View source
  1. <?php
  2. /**
  3. * Form API callback to validate the upload settings form.
  4. */
  5. function upload_admin_settings_validate($form, &$form_state) {
  6. if (($form_state['values']['upload_max_resolution'] != '0')) {
  7. if (!preg_match('/^[0-9]+x[0-9]+$/', $form_state['values']['upload_max_resolution'])) {
  8. form_set_error('upload_max_resolution', t('The maximum allowed image size expressed as WIDTHxHEIGHT (e.g. 640x480). Set to 0 for no restriction.'));
  9. }
  10. }
  11. $default_uploadsize = $form_state['values']['upload_uploadsize_default'];
  12. $default_usersize = $form_state['values']['upload_usersize_default'];
  13. $exceed_max_msg = t('Your PHP settings limit the maximum file size per upload to %size.', array('%size' => format_size(file_upload_max_size()))) .'<br/>';
  14. $more_info = t("Depending on your server environment, these settings may be changed in the system-wide php.ini file, a php.ini file in your Drupal root directory, in your Drupal site's settings.php file, or in the .htaccess file in your Drupal root directory.");
  15. if (!is_numeric($default_uploadsize) || ($default_uploadsize <= 0)) {
  16. form_set_error('upload_uploadsize_default', t('The %role file size limit must be a number and greater than zero.', array('%role' => t('default'))));
  17. }
  18. if (!is_numeric($default_usersize) || ($default_usersize <= 0)) {
  19. form_set_error('upload_usersize_default', t('The %role file size limit must be a number and greater than zero.', array('%role' => t('default'))));
  20. }
  21. if ($default_uploadsize * 1024 * 1024 > file_upload_max_size()) {
  22. form_set_error('upload_uploadsize_default', $exceed_max_msg . $more_info);
  23. $more_info = '';
  24. }
  25. if ($default_uploadsize > $default_usersize) {
  26. form_set_error('upload_uploadsize_default', t('The %role maximum file size per upload is greater than the total file size allowed per user', array('%role' => t('default'))));
  27. }
  28. foreach ($form_state['values']['roles'] as $rid => $role) {
  29. $uploadsize = $form_state['values']['upload_uploadsize_'. $rid];
  30. $usersize = $form_state['values']['upload_usersize_'. $rid];
  31. if (!is_numeric($uploadsize) || ($uploadsize <= 0)) {
  32. form_set_error('upload_uploadsize_'. $rid, t('The %role file size limit must be a number and greater than zero.', array('%role' => $role)));
  33. }
  34. if (!is_numeric($usersize) || ($usersize <= 0)) {
  35. form_set_error('upload_usersize_'. $rid, t('The %role file size limit must be a number and greater than zero.', array('%role' => $role)));
  36. }
  37. if ($uploadsize * 1024 * 1024 > file_upload_max_size()) {
  38. form_set_error('upload_uploadsize_'. $rid, $exceed_max_msg . $more_info);
  39. $more_info = '';
  40. }
  41. if ($uploadsize > $usersize) {
  42. form_set_error('upload_uploadsize_'. $rid, t('The %role maximum file size per upload is greater than the total file size allowed per user', array('%role' => $role)));
  43. }
  44. }
  45. }
  46. /**
  47. * Menu callback for the upload settings form.
  48. */
  49. function upload_admin_settings() {
  50. $upload_extensions_default = variable_get('upload_extensions_default', 'jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp');
  51. $upload_uploadsize_default = variable_get('upload_uploadsize_default', 1);
  52. $upload_usersize_default = variable_get('upload_usersize_default', 1);
  53. $form['settings_general'] = array(
  54. '#type' => 'fieldset',
  55. '#title' => t('General settings'),
  56. '#collapsible' => TRUE,
  57. );
  58. $form['settings_general']['upload_max_resolution'] = array(
  59. '#type' => 'textfield',
  60. '#title' => t('Maximum resolution for uploaded images'),
  61. '#default_value' => variable_get('upload_max_resolution', 0),
  62. '#size' => 15,
  63. '#maxlength' => 10,
  64. '#description' => t('The maximum allowed image size (e.g. 640x480). Set to 0 for no restriction. If an <a href="!image-toolkit-link">image toolkit</a> is installed, files exceeding this value will be scaled down to fit.', array('!image-toolkit-link' => url('admin/settings/image-toolkit'))),
  65. '#field_suffix' => '<kbd>'. t('WIDTHxHEIGHT') .'</kbd>'
  66. );
  67. $form['settings_general']['upload_list_default'] = array(
  68. '#type' => 'select',
  69. '#title' => t('List files by default'),
  70. '#default_value' => variable_get('upload_list_default', 1),
  71. '#options' => array(0 => t('No'), 1 => t('Yes')),
  72. '#description' => t('Display attached files when viewing a post.'),
  73. );
  74. $form['settings_general']['upload_extensions_default'] = array(
  75. '#type' => 'textfield',
  76. '#title' => t('Default permitted file extensions'),
  77. '#default_value' => $upload_extensions_default,
  78. '#maxlength' => 255,
  79. '#description' => t('Default extensions that users can upload. Separate extensions with a space and do not include the leading dot.'),
  80. );
  81. $form['settings_general']['upload_uploadsize_default'] = array(
  82. '#type' => 'textfield',
  83. '#title' => t('Default maximum file size per upload'),
  84. '#default_value' => $upload_uploadsize_default,
  85. '#size' => 5,
  86. '#maxlength' => 5,
  87. '#description' => t('The default maximum file size a user can upload. If an image is uploaded and a maximum resolution is set, the size will be checked after the file has been resized.'),
  88. '#field_suffix' => t('MB'),
  89. );
  90. $form['settings_general']['upload_usersize_default'] = array(
  91. '#type' => 'textfield',
  92. '#title' => t('Default total file size per user'),
  93. '#default_value' => $upload_usersize_default,
  94. '#size' => 5,
  95. '#maxlength' => 5,
  96. '#description' => t('The default maximum size of all files a user can have on the site.'),
  97. '#field_suffix' => t('MB'),
  98. );
  99. $form['settings_general']['upload_max_size'] = array('#value' => '<p>'. t('Your PHP settings limit the maximum file size per upload to %size.', array('%size' => format_size(file_upload_max_size()))) .'</p>');
  100. $roles = user_roles(FALSE, 'upload files');
  101. $form['roles'] = array('#type' => 'value', '#value' => $roles);
  102. foreach ($roles as $rid => $role) {
  103. $form['settings_role_'. $rid] = array(
  104. '#type' => 'fieldset',
  105. '#title' => t('Settings for @role', array('@role' => $role)),
  106. '#collapsible' => TRUE,
  107. '#collapsed' => TRUE,
  108. );
  109. $form['settings_role_'. $rid]['upload_extensions_'. $rid] = array(
  110. '#type' => 'textfield',
  111. '#title' => t('Permitted file extensions'),
  112. '#default_value' => variable_get('upload_extensions_'. $rid, $upload_extensions_default),
  113. '#maxlength' => 255,
  114. '#description' => t('Extensions that users in this role can upload. Separate extensions with a space and do not include the leading dot.'),
  115. );
  116. $form['settings_role_'. $rid]['upload_uploadsize_'. $rid] = array(
  117. '#type' => 'textfield',
  118. '#title' => t('Maximum file size per upload'),
  119. '#default_value' => variable_get('upload_uploadsize_'. $rid, $upload_uploadsize_default),
  120. '#size' => 5,
  121. '#maxlength' => 5,
  122. '#description' => t('The maximum size of a file a user can upload. If an image is uploaded and a maximum resolution is set, the size will be checked after the file has been resized.'),
  123. '#field_suffix' => t('MB'),
  124. );
  125. $form['settings_role_'. $rid]['upload_usersize_'. $rid] = array(
  126. '#type' => 'textfield',
  127. '#title' => t('Total file size per user'),
  128. '#default_value' => variable_get('upload_usersize_'. $rid, $upload_usersize_default),
  129. '#size' => 5,
  130. '#maxlength' => 5,
  131. '#description' => t('The maximum size of all files a user can have on the site.'),
  132. '#field_suffix' => t('MB'),
  133. );
  134. }
  135. $form['#validate'] = array('upload_admin_settings_validate');
  136. return system_settings_form($form);
  137. }