You are here

function _mass_contact_upload_validate in Mass Contact 6

Same name and namespace in other branches
  1. 5.2 mass_contact.module \_mass_contact_upload_validate()

Validate the attachment(s).

Parameters

form_id: A unique string identifying the form.

form_values: The contents of the form fields.

File

./mass_contact.module, line 2136
This is the main code file for the Mass Contact module. This module enables users to contact multiple users through selected roles.

Code

function _mass_contact_upload_validate($form_id, $form_values) {

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

  // Loop through each possible attachment.
  for ($i = 1; $i <= variable_get('mass_contact_number_of_attachments', '3'); $i++) {

    // Check to see if an attachment exists.
    if ($_FILES['files']['size']['attachment_' . $i] > 0) {
      global $user;

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

        // Update filesize accumulator.
        $filesize += $_FILES['files']['size']['attachment_' . $i];
        $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, $_FILES['files']['name']['attachment_' . $i])) {
            $error['extension']++;
          }
          if ($uploadsize && $_FILES['files']['size']['attachment_' . $i] > $uploadsize) {
            $error['uploadsize']++;
          }
          if ($usersize && $filesize > $usersize) {
            $error['usersize']++;
          }
        }
        $user_roles = count($user->roles);
        if ($error['extension'] == $user_roles) {
          form_set_error('attachment_' . $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' => $_FILES['files']['name']['attachment_' . $i],
            '%files-allowed' => $extensions,
          )));
        }
        if ($error['uploadsize'] == $user_roles) {
          form_set_error('attachment_' . $i, t('The selected file %name can not be attached to this post, because it exceeded the maximum filesize of %maxsize.', array(
            '%name' => $_FILES['files']['name']['attachment_' . $i],
            '%maxsize' => format_size($uploadsize),
          )));
        }
        if ($error['usersize'] == $user_roles) {
          form_set_error('attachment_' . $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' => $_FILES['files']['name']['attachment_' . $i],
            '%quota' => format_size($usersize),
          )));
        }
        if (drupal_strlen($_FILES['files']['name']['attachment_' . $i]) > 255) {
          form_set_error('attachment_' . $i, t('The selected file %name can not be attached to this post, because the filename is too long.', array(
            '%name' => $_FILES['files']['name']['attachment_' . $i],
          )));
        }
      }
    }
  }
}