Same name and namespace in other branches
  1. 4.7.x modules/upload.module \_upload_validate()
1 call to _upload_validate()
upload_js in modules/upload/upload.module
Menu-callback for JavaScript-based uploads.

File

modules/upload/upload.module, line 398
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}", variable_get('upload_extensions_default', 'jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp'));
            $uploadsize = variable_get("upload_uploadsize_{$rid}", variable_get('upload_uploadsize_default', 1)) * 1024 * 1024;
            $usersize = variable_get("upload_usersize_{$rid}", variable_get('upload_usersize_default', 1)) * 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' => $file->filename,
              '%files-allowed' => $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' => $file->filename,
              '%maxsize' => 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' => $file->filename,
              '%quota' => 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' => $file->filename,
            )));
            $valid = FALSE;
          }
          if (!$valid) {
            unset($node->files[$fid], $_SESSION['file_previews'][$fid]);
            file_delete($file->filepath);
          }
        }
      }
    }
  }
}