2007-07-30 22:53:10 +00:00
< ? php
2007-08-07 08:39:36 +00:00
// $Id$
2007-07-30 22:53:10 +00:00
/**
* Form API callback to validate the upload settings form .
*/
function upload_admin_settings_validate ( $form , & $form_state ) {
if (( $form_state [ 'values' ][ 'upload_max_resolution' ] != '0' )) {
if ( ! preg_match ( '/^[0-9]+x[0-9]+$/' , $form_state [ '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.' ));
}
}
$default_uploadsize = $form_state [ 'values' ][ 'upload_uploadsize_default' ];
$default_usersize = $form_state [ 'values' ][ 'upload_usersize_default' ];
2008-04-14 17:48:46 +00:00
$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/>' ;
2008-01-05 22:43:41 +00:00
$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. " );
2007-07-30 22:53:10 +00:00
if ( ! is_numeric ( $default_uploadsize ) || ( $default_uploadsize <= 0 )) {
form_set_error ( 'upload_uploadsize_default' , t ( 'The %role file size limit must be a number and greater than zero.' , array ( '%role' => t ( 'default' ))));
}
2008-04-20 07:58:38 +00:00
if ( ! is_numeric ( $default_usersize ) || ( $default_usersize < 0 )) {
form_set_error ( 'upload_usersize_default' , t ( 'The %role file size limit must be a number zero or greater.' , array ( '%role' => t ( 'default' ))));
2007-07-30 22:53:10 +00:00
}
2008-01-05 22:41:16 +00:00
if ( $default_uploadsize * 1024 * 1024 > file_upload_max_size ()) {
2007-07-30 22:53:10 +00:00
form_set_error ( 'upload_uploadsize_default' , $exceed_max_msg . $more_info );
$more_info = '' ;
}
2008-04-20 07:58:38 +00:00
if ( $default_usersize && $default_uploadsize > $default_usersize ) {
2007-07-30 22:53:10 +00:00
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' ))));
}
foreach ( $form_state [ 'values' ][ 'roles' ] as $rid => $role ) {
2008-04-14 17:48:46 +00:00
$uploadsize = $form_state [ 'values' ][ 'upload_uploadsize_' . $rid ];
$usersize = $form_state [ 'values' ][ 'upload_usersize_' . $rid ];
2007-07-30 22:53:10 +00:00
if ( ! is_numeric ( $uploadsize ) || ( $uploadsize <= 0 )) {
2008-04-14 17:48:46 +00:00
form_set_error ( 'upload_uploadsize_' . $rid , t ( 'The %role file size limit must be a number and greater than zero.' , array ( '%role' => $role )));
2007-07-30 22:53:10 +00:00
}
2008-04-20 07:58:38 +00:00
if ( ! is_numeric ( $usersize ) || ( $usersize < 0 )) {
form_set_error ( 'upload_usersize_' . $rid , t ( 'The %role file size limit must be a number zero or greater.' , array ( '%role' => $role )));
2007-07-30 22:53:10 +00:00
}
2008-01-05 22:41:16 +00:00
if ( $uploadsize * 1024 * 1024 > file_upload_max_size ()) {
2008-04-14 17:48:46 +00:00
form_set_error ( 'upload_uploadsize_' . $rid , $exceed_max_msg . $more_info );
2007-07-30 22:53:10 +00:00
$more_info = '' ;
}
2008-04-20 07:58:38 +00:00
if ( $usersize && $uploadsize > $usersize ) {
2008-04-14 17:48:46 +00:00
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 )));
2007-07-30 22:53:10 +00:00
}
}
}
/**
* Menu callback for the upload settings form .
*/
function upload_admin_settings () {
2007-10-17 21:47:14 +00:00
$upload_extensions_default = variable_get ( 'upload_extensions_default' , 'jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp' );
2007-07-30 22:53:10 +00:00
$upload_uploadsize_default = variable_get ( 'upload_uploadsize_default' , 1 );
$upload_usersize_default = variable_get ( 'upload_usersize_default' , 1 );
$form [ 'settings_general' ] = array (
'#type' => 'fieldset' ,
'#title' => t ( 'General settings' ),
'#collapsible' => TRUE ,
);
$form [ 'settings_general' ][ 'upload_max_resolution' ] = array (
'#type' => 'textfield' ,
'#title' => t ( 'Maximum resolution for uploaded images' ),
'#default_value' => variable_get ( 'upload_max_resolution' , 0 ),
'#size' => 15 ,
'#maxlength' => 10 ,
'#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' ))),
2008-04-14 17:48:46 +00:00
'#field_suffix' => '<kbd>' . t ( 'WIDTHxHEIGHT' ) . '</kbd>'
2007-07-30 22:53:10 +00:00
);
$form [ 'settings_general' ][ 'upload_list_default' ] = array (
'#type' => 'select' ,
'#title' => t ( 'List files by default' ),
'#default_value' => variable_get ( 'upload_list_default' , 1 ),
'#options' => array ( 0 => t ( 'No' ), 1 => t ( 'Yes' )),
2007-11-12 19:06:33 +00:00
'#description' => t ( 'Display attached files when viewing a post.' ),
2007-07-30 22:53:10 +00:00
);
$form [ 'settings_general' ][ 'upload_extensions_default' ] = array (
'#type' => 'textfield' ,
'#title' => t ( 'Default permitted file extensions' ),
'#default_value' => $upload_extensions_default ,
'#maxlength' => 255 ,
'#description' => t ( 'Default extensions that users can upload. Separate extensions with a space and do not include the leading dot.' ),
);
$form [ 'settings_general' ][ 'upload_uploadsize_default' ] = array (
'#type' => 'textfield' ,
'#title' => t ( 'Default maximum file size per upload' ),
'#default_value' => $upload_uploadsize_default ,
'#size' => 5 ,
'#maxlength' => 5 ,
'#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.' ),
'#field_suffix' => t ( 'MB' ),
);
$form [ 'settings_general' ][ 'upload_usersize_default' ] = array (
'#type' => 'textfield' ,
'#title' => t ( 'Default total file size per user' ),
'#default_value' => $upload_usersize_default ,
2008-04-20 07:58:38 +00:00
'#size' => 7 ,
'#maxlength' => 7 ,
'#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.' ),
2007-07-30 22:53:10 +00:00
'#field_suffix' => t ( 'MB' ),
);
2008-04-14 17:48:46 +00:00
$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>' );
2007-07-30 22:53:10 +00:00
2008-01-10 20:22:57 +00:00
$roles = user_roles ( FALSE , 'upload files' );
2007-07-30 22:53:10 +00:00
$form [ 'roles' ] = array ( '#type' => 'value' , '#value' => $roles );
foreach ( $roles as $rid => $role ) {
2008-04-14 17:48:46 +00:00
$form [ 'settings_role_' . $rid ] = array (
2007-07-30 22:53:10 +00:00
'#type' => 'fieldset' ,
'#title' => t ( 'Settings for @role' , array ( '@role' => $role )),
'#collapsible' => TRUE ,
'#collapsed' => TRUE ,
);
2008-04-14 17:48:46 +00:00
$form [ 'settings_role_' . $rid ][ 'upload_extensions_' . $rid ] = array (
2007-07-30 22:53:10 +00:00
'#type' => 'textfield' ,
'#title' => t ( 'Permitted file extensions' ),
2008-04-14 17:48:46 +00:00
'#default_value' => variable_get ( 'upload_extensions_' . $rid , $upload_extensions_default ),
2007-07-30 22:53:10 +00:00
'#maxlength' => 255 ,
'#description' => t ( 'Extensions that users in this role can upload. Separate extensions with a space and do not include the leading dot.' ),
);
2008-04-14 17:48:46 +00:00
$form [ 'settings_role_' . $rid ][ 'upload_uploadsize_' . $rid ] = array (
2007-07-30 22:53:10 +00:00
'#type' => 'textfield' ,
'#title' => t ( 'Maximum file size per upload' ),
2008-04-14 17:48:46 +00:00
'#default_value' => variable_get ( 'upload_uploadsize_' . $rid , $upload_uploadsize_default ),
2007-07-30 22:53:10 +00:00
'#size' => 5 ,
'#maxlength' => 5 ,
'#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.' ),
'#field_suffix' => t ( 'MB' ),
);
2008-04-14 17:48:46 +00:00
$form [ 'settings_role_' . $rid ][ 'upload_usersize_' . $rid ] = array (
2007-07-30 22:53:10 +00:00
'#type' => 'textfield' ,
'#title' => t ( 'Total file size per user' ),
2008-04-14 17:48:46 +00:00
'#default_value' => variable_get ( 'upload_usersize_' . $rid , $upload_usersize_default ),
2008-04-20 07:58:38 +00:00
'#size' => 7 ,
'#maxlength' => 7 ,
'#description' => t ( 'The maximum size of all files a user can have on the site. Set to 0 for no restriction.' ),
2007-07-30 22:53:10 +00:00
'#field_suffix' => t ( 'MB' ),
);
}
$form [ '#validate' ] = array ( 'upload_admin_settings_validate' );
return system_settings_form ( $form );
2007-11-12 19:06:33 +00:00
}