You are here

function _contact_attach_upload_validate in Contact Attach 5

Same name and namespace in other branches
  1. 6 contact_attach.module \_contact_attach_upload_validate()

Validate the attachment(s).

Parameters

form_id: A unique string identifying the form.

form_values: The contents of the form fields.

3 calls to _contact_attach_upload_validate()
contact_attach_contact_mail_page_validate in ./contact_attach.module
Override contact_mail_page_validate().
contact_attach_contact_mail_user_validate in ./contact_attach.module
Override contact_mail_user_validate().
contact_attach_og_contact_mail_page_validate in ./contact_attach.module
Override og_contact_mail_page_validate().

File

./contact_attach.module, line 522
This is the main code file for the Contact attach module. This module gives users the ability of attaching one or more files to e-mails sent using the site-wide contact form.

Code

function _contact_attach_upload_validate($form_id, $form_values) {

  // Accumulator for disk space quotas.
  $filesize = 0;

  // Loop through each possible attachment.
  for ($i = 1; $i <= variable_get('contact_attach_number', '3'); $i++) {
    if ($file = file_check_upload('contact_attach_' . $i)) {

      // Check to see if an attachment exists.
      if ($file->filesize > 0) {
        global $user;

        // Bypass validation for uid  = 1.
        if ($user->uid != 1) {

          // Update filesize accumulator.
          $filesize += $file->filesize;
          $error = array();

          // Validate file against all users roles.
          // Only denies an upload when all roles prevent it.
          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 && $filesize > $usersize) {
              $error['usersize']++;
            }
          }
          $user_roles = count($user->roles);
          if ($error['extension'] == $user_roles) {
            form_set_error('contact_attach_' . $i, 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,
            )));
          }
          elseif ($error['uploadsize'] == $user_roles) {
            form_set_error('contact_attach_' . $i, 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),
            )));
          }
          elseif ($error['usersize'] == $user_roles) {
            form_set_error('contact_attach_' . $i, t('The selected file %name can not be attached to this post, because the maximum file size of %quota per upload has been reached.', array(
              '%name' => $file->filename,
              '%quota' => format_size($usersize),
            )));
          }
          elseif (drupal_strlen($_FILES['files']['name']['contact_attach_' . $i]) > 255) {
            form_set_error('contact_attach_' . $i, t('The selected file %name can not be attached to this post, because the filename is too long.', array(
              '%name' => $file->filename,
            )));
          }
        }
      }
    }
  }
}