function _upload_validate in Drupal 4
Same name and namespace in other branches
- 5 modules/upload/upload.module \_upload_validate()
2 calls to _upload_validate()
- upload_js in modules/
upload.module - Menu-callback for JavaScript-based uploads.
- upload_nodeapi in modules/
upload.module - Implementation of hook_nodeapi().
File
- modules/
upload.module, line 302 - File-handling and attaching files to nodes.
Code
function _upload_validate(&$node) {
// Accumulator for disk space quotas.
$filesize = 0;
// Check if node->files exists, and if it contains something.
if (is_array($node->files)) {
// Update existing files with form data.
foreach ($node->files as $fid => $file) {
// Convert file to object for compatibility
$file = (object) $file;
// Validate new uploads.
if (strpos($fid, 'upload') !== false && !$file->remove) {
global $user;
// Bypass validation for uid = 1.
if ($user->uid != 1) {
// Update filesize accumulator.
$filesize += $file->filesize;
// Validate file against all users roles.
// Only denies an upload when all roles prevent it.
$total_usersize = upload_space_used($user->uid) + $filesize;
$error = array();
foreach ($user->roles as $rid => $name) {
$extensions = variable_get("upload_extensions_{$rid}", 'jpg jpeg gif png txt doc xls xls pdf ppt pps odt ods odp');
$uploadsize = variable_get("upload_uploadsize_{$rid}", 1) * 1024 * 1024;
$usersize = variable_get("upload_usersize_{$rid}", 10) * 1024 * 1024;
$regex = '/\\.(' . ereg_replace(' +', '|', preg_quote($extensions)) . ')$/i';
if (!preg_match($regex, $file->filename)) {
$error['extension']++;
}
if ($uploadsize && $file->filesize > $uploadsize) {
$error['uploadsize']++;
}
if ($usersize && $total_usersize + $file->filesize > $usersize) {
$error['usersize']++;
}
}
$user_roles = count($user->roles);
$valid = TRUE;
if ($error['extension'] == $user_roles) {
form_set_error('upload', t('The selected file %name can not be attached to this post, because it is only possible to attach files with the following extensions: %files-allowed.', array(
'%name' => theme('placeholder', $file->filename),
'%files-allowed' => theme('placeholder', $extensions),
)));
$valid = FALSE;
}
elseif ($error['uploadsize'] == $user_roles) {
form_set_error('upload', t('The selected file %name can not be attached to this post, because it exceeded the maximum filesize of %maxsize.', array(
'%name' => theme('placeholder', $file->filename),
'%maxsize' => theme('placeholder', format_size($uploadsize)),
)));
$valid = FALSE;
}
elseif ($error['usersize'] == $user_roles) {
form_set_error('upload', t('The selected file %name can not be attached to this post, because the disk quota of %quota has been reached.', array(
'%name' => theme('placeholder', $file->filename),
'%quota' => theme('placeholder', format_size($usersize)),
)));
$valid = FALSE;
}
elseif (strlen($file->filename) > 255) {
form_set_error('upload', t('The selected file %name can not be attached to this post, because the filename is too long.', array(
'%name' => theme('placeholder', $file->filename),
)));
$valid = FALSE;
}
if (!$valid) {
unset($node->files[$fid], $_SESSION['file_previews'][$fid]);
file_delete($file->filepath);
}
}
}
}
}
}