Code coverage for /20081101/modules/upload/upload.admin.inc

Line #Times calledCode
1
<?php
2
// $Id: upload.admin.inc,v 1.10 2008/07/16 21:59:28 dries Exp $
3
4
/**
5
 * Form API callback to validate the upload settings form.
6
 */
79
function upload_admin_settings_validate($form, &$form_state) {
83
  if (($form_state['values']['upload_max_resolution'] != '0')) {
90
    if (!preg_match('/^[0-9]+x[0-9]+$/',
$form_state['values']['upload_max_resolution'])) {
100
      form_set_error('upload_max_resolution', t('The maximum allowed image
size expressed as WIDTHxHEIGHT (e.g. 640x480). Set to 0 for no
restriction.'));
110
    }
120
  }
13
143
  $default_uploadsize = $form_state['values']['upload_uploadsize_default'];
153
  $default_usersize = $form_state['values']['upload_usersize_default'];
16
173
  $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/>';
183
  $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.");
19
203
  if (!is_numeric($default_uploadsize) || ($default_uploadsize <= 0)) {
210
    form_set_error('upload_uploadsize_default', t('The %role file size
limit must be a number and greater than zero.', array('%role' =>
t('default'))));
220
  }
233
  if (!is_numeric($default_usersize) || ($default_usersize < 0)) {
240
    form_set_error('upload_usersize_default', t('The %role file size limit
must be a number zero or greater.', array('%role' => t('default'))));
250
  }
263
  if ($default_uploadsize * 1024 * 1024 > file_upload_max_size()) {
270
    form_set_error('upload_uploadsize_default', $exceed_max_msg .
$more_info);
280
    $more_info = '';
290
  }
303
  if ($default_usersize && $default_uploadsize > $default_usersize) {
310
    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'))));
320
  }
33
343
  foreach ($form_state['values']['roles'] as $rid => $role) {
353
    $uploadsize = $form_state['values']['upload_uploadsize_' . $rid];
363
    $usersize = $form_state['values']['upload_usersize_' . $rid];
37
383
    if (!is_numeric($uploadsize) || ($uploadsize <= 0)) {
390
      form_set_error('upload_uploadsize_' . $rid, t('The %role file size
limit must be a number and greater than zero.', array('%role' => $role)));
400
    }
413
    if (!is_numeric($usersize) || ($usersize < 0)) {
420
      form_set_error('upload_usersize_' . $rid, t('The %role file size
limit must be a number zero or greater.', array('%role' => $role)));
430
    }
443
    if ($uploadsize * 1024 * 1024 > file_upload_max_size()) {
450
      form_set_error('upload_uploadsize_' . $rid, $exceed_max_msg .
$more_info);
460
      $more_info = '';
470
    }
483
    if ($usersize && $uploadsize > $usersize) {
490
      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)));
500
    }
513
  }
523
}
53
54
/**
55
 * Menu callback for the upload settings form.
56
 */
579
function upload_admin_settings() {
589
  $upload_extensions_default = variable_get('upload_extensions_default',
'jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp');
599
  $upload_uploadsize_default = variable_get('upload_uploadsize_default',
1);
609
  $upload_usersize_default = variable_get('upload_usersize_default', 1);
61
629
  $form['settings_general'] = array(
639
    '#type' => 'fieldset',
649
    '#title' => t('General settings'),
659
    '#collapsible' => TRUE,
66
  );
679
  $form['settings_general']['upload_max_resolution'] = array(
689
    '#type' => 'textfield',
699
    '#title' => t('Maximum resolution for uploaded images'),
709
    '#default_value' => variable_get('upload_max_resolution', 0),
719
    '#size' => 15,
729
    '#maxlength' => 10,
739
    '#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'))),
749
    '#field_suffix' => '<kbd>' . t('WIDTHxHEIGHT') . '</kbd>'
759
  );
769
  $form['settings_general']['upload_list_default'] = array(
779
    '#type' => 'select',
789
    '#title' => t('List files by default'),
799
    '#default_value' => variable_get('upload_list_default', 1),
809
    '#options' => array(0 => t('No'), 1 => t('Yes')),
819
    '#description' => t('Display attached files when viewing a post.'),
82
  );
83
849
  $form['settings_general']['upload_extensions_default'] = array(
859
    '#type' => 'textfield',
869
    '#title' => t('Default permitted file extensions'),
879
    '#default_value' => $upload_extensions_default,
889
    '#maxlength' => 255,
899
    '#description' => t('Default extensions that users can upload. Separate
extensions with a space and do not include the leading dot.'),
90
  );
919
  $form['settings_general']['upload_uploadsize_default'] = array(
929
    '#type' => 'textfield',
939
    '#title' => t('Default maximum file size per upload'),
949
    '#default_value' => $upload_uploadsize_default,
959
    '#size' => 5,
969
    '#maxlength' => 5,
979
    '#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.'),
989
    '#field_suffix' => t('MB'),
99
  );
1009
  $form['settings_general']['upload_usersize_default'] = array(
1019
    '#type' => 'textfield',
1029
    '#title' => t('Default total file size per user'),
1039
    '#default_value' => $upload_usersize_default,
1049
    '#size' => 7,
1059
    '#maxlength' => 7,
1069
    '#description' => t('The default maximum size of all files a user can
have on the site. Set to 0 for no restriction. The least restrictive limit
is used, so setting this to 0 will disable any role-specific total file
size limits.'),
1079
    '#field_suffix' => t('MB'),
108
  );
109
1109
  $form['settings_general']['upload_max_size'] = array('#markup' => '<p>' .
t('Your PHP settings limit the maximum file size per upload to %size.',
array('%size' => format_size(file_upload_max_size()))) . '</p>');
111
1129
  $roles = user_roles(FALSE, 'upload files');
1139
  $form['roles'] = array('#type' => 'value', '#value' => $roles);
114
1159
  foreach ($roles as $rid => $role) {
1169
    $form['settings_role_' . $rid] = array(
1179
      '#type' => 'fieldset',
1189
      '#title' => t('Settings for @role', array('@role' => $role)),
1199
      '#collapsible' => TRUE,
1209
      '#collapsed' => TRUE,
121
    );
1229
    $form['settings_role_' . $rid]['upload_extensions_' . $rid] = array(
1239
      '#type' => 'textfield',
1249
      '#title' => t('Permitted file extensions'),
1259
      '#default_value' => variable_get('upload_extensions_' . $rid,
$upload_extensions_default),
1269
      '#maxlength' => 255,
1279
      '#description' => t('Extensions that users in this role can upload.
Separate extensions with a space and do not include the leading dot.'),
128
    );
1299
    $form['settings_role_' . $rid]['upload_uploadsize_' . $rid] = array(
1309
      '#type' => 'textfield',
1319
      '#title' => t('Maximum file size per upload'),
1329
      '#default_value' => variable_get('upload_uploadsize_' . $rid,
$upload_uploadsize_default),
1339
      '#size' => 5,
1349
      '#maxlength' => 5,
1359
      '#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.'),
1369
      '#field_suffix' => t('MB'),
137
    );
1389
    $form['settings_role_' . $rid]['upload_usersize_' . $rid] = array(
1399
      '#type' => 'textfield',
1409
      '#title' => t('Total file size per user'),
1419
      '#default_value' => variable_get('upload_usersize_' . $rid,
$upload_usersize_default),
1429
      '#size' => 7,
1439
      '#maxlength' => 7,
1449
      '#description' => t('The maximum size of all files a user can have on
the site. Set to 0 for no restriction.'),
1459
      '#field_suffix' => t('MB'),
146
    );
1479
  }
148
1499
  $form['#validate'] = array('upload_admin_settings_validate');
150
1519
  return system_settings_form($form);
1520
}
1539