You are here

function webfm_upload_validate in Web File Manager 5

Same name and namespace in other branches
  1. 5.2 webfm.module \webfm_upload_validate()

webfm_upload_validate *

Parameters

object $file - file object:

string &$err_arr - ref to error array for client feedback:

Return value

bool - returns TRUE if there are no errors otherwise FALSE

1 call to webfm_upload_validate()
webfm_upload in ./webfm.module
Called by upload form submit

File

./webfm.module, line 2730

Code

function webfm_upload_validate($file, &$err_arr) {
  global $user;
  if ($user->uid == 1) {
    return TRUE;
  }
  $copy_err = is_array($err_arr);
  foreach ($user->roles as $rid => $name) {
    $regex = webfm_get_extensions_regex($rid);
    $uploadsize = variable_get("webfm_uploadsize_" . $rid, 1) * 1024 * 1024;
    $usersize = variable_get("webfm_usersize_" . $rid, 1) * 1024 * 1024;
    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) {
    if ($copy_err) {
      $err_arr[] = $file->filepath . " has an invalid extension";
    }
    form_set_error('webfm_uploads', t('%name can not be uploaded because it does not have one of the following extensions: %files-allowed.', array(
      '%name' => $file->filename,
      '%files-allowed' => $extensions,
    )));
    $valid = FALSE;
  }
  elseif ($error['uploadsize'] == $user_roles) {
    if ($copy_err) {
      $err_arr[] = $file->filepath . " exceeds the max filesize";
    }
    form_set_error('webfm_uploads', t('%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) {
    if ($copy_err) {
      $err_arr[] = $file->filepath . " exceeds the max disk quota";
    }
    form_set_error('webfm_uploads', t('%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) {
    if ($copy_err) {
      $err_arr[] = $file->filepath . " exceeds the max name length";
    }
    form_set_error('webfm_uploads', t('The selected file can not be attached to this post, because the filename is too long.'));
    $valid = FALSE;
  }
  return $valid ? TRUE : FALSE;
}