You are here

upload_qc.module in Upload QC 7

File

upload_qc.module
View source
<?php

// **************** Hooks Begin ***************************

/**
* Implements hook_menu().
*/
function upload_qc_menu() {
  $items = array();
  $items['admin/config/services/upload_qc'] = array(
    'title' => 'Upload QC module settings',
    'description' => 'Upload QC module settings',
    'page callback' => 'drupal_get_form',
    'access callback' => 'user_access',
    'page arguments' => array(
      'upload_qc_form',
    ),
    'access arguments' => array(
      'access administration pages',
    ),
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
* Implements hook_permission().
*/
function upload_qc_permission() {
  return array(
    'process FTP requests over a URL' => array(
      'title' => t('Data Encrypt'),
      'description' => t('Settings for Data Encrypt, /upload_qc.'),
    ),
  );
}

/**
* Form generator for module settings.
*/
function upload_qc_form($form, &$form_state) {
  $form['upload_qc_error'] = array(
    '#type' => 'checkbox',
    '#title' => t('Check for PDO upload errors'),
    '#default_value' => variable_get('upload_qc_error', 0),
    '#description' => t("Check for PDO-related errors in MySQL db while uploading files."),
    '#required' => FALSE,
  );
  $form['upload_qc_length'] = array(
    '#type' => 'textfield',
    '#title' => t('File name length'),
    '#default_value' => variable_get('upload_qc_length', '225'),
    '#size' => 50,
    '#maxlength' => 50,
    '#description' => t("Maximum file name length in characters allowed for file being uploaded. That includes extensions. If not checking, leave blank."),
    '#required' => FALSE,
  );
  $form['upload_qc_include'] = array(
    '#type' => 'textfield',
    '#title' => t('Required words/characters'),
    '#default_value' => variable_get('upload_qc_include', ''),
    '#size' => 50,
    '#maxlength' => 50,
    '#description' => t("Required words/characters in the file name. If not checking, leave blank."),
    '#required' => FALSE,
  );
  $form['upload_qc_exclude'] = array(
    '#type' => 'textfield',
    '#title' => t('Banned words/characters'),
    '#default_value' => variable_get('upload_qc_exclude', ''),
    '#size' => 50,
    '#maxlength' => 50,
    '#description' => t("Banned words/characters in the file name. If not checking, leave blank."),
    '#required' => FALSE,
  );
  $form['upload_qc_size'] = array(
    '#type' => 'textfield',
    '#title' => t('File size'),
    '#default_value' => variable_get('upload_qc_size', '1024'),
    '#size' => 50,
    '#maxlength' => 50,
    '#description' => t("Maximum file size in KB allowed for file being uploaded. E.g. 1M file is 1024 KB. If not checking, leave blank."),
    '#required' => FALSE,
  );
  $form = system_settings_form($form);
  return $form;
}

/**
* Implements hook_file_validate().
* This implementation includes two test: filesize and existance of db entry 
* with the same URI.
* If it returns errors, the upload process is blocked and the errors are displayed.
* */
function upload_qc_file_validate($file) {

  // array to return
  $errors = array();

  // check if there is an object on the input
  if (!is_object($file)) {
    return $errors;
  }

  // get setting variables from db and check them
  $error_check = variable_get('upload_qc_error', '');
  $length_check = variable_get('upload_qc_length', '');
  $inlcude_check = variable_get('upload_qc_include', '');
  $exclude_check = variable_get('upload_qc_exclude', '');
  $size_check = variable_get('upload_qc_size', '');

  // check if file URI already exists in db.
  // That prevents the AJAX error with duplicate entries for 'uri' in file_managed
  if (!empty($error_check)) {
    $query_result = db_query('SELECT n.fid
    FROM {file_managed} n WHERE n.uri = :uri', array(
      ':uri' => $file->destination,
    ));
    $record = $query_result
      ->fetchObject();
    if (is_object($record)) {
      $errors[] = t("The file's name currently in use. Please wait a few seconds and try again.");
    }
  }

  // check the length of the filename
  if (!empty($length_check)) {
    if (empty($file->filename)) {
      $errors[] = t("The file's name is empty. Please give the file a name.");
    }
    if (strlen($file->filename) > $length_check) {
      $errors[] = t("The file's name exceeds the " . $length_check . " characters limit. Please give the file a shorter name and try again.");
    }
  }

  // check for required words/characters
  // check the length of the filename
  if (!empty($inlcude_check)) {
    $pattern_in = '/' . $inlcude_check . '/';
    if (!preg_match($pattern_in, $file->filename)) {
      $errors[] = t("The file's name does not include the required " . $inlcude_check);
    }
  }

  // check for required words/characters
  // check the length of the filename
  if (!empty($exclude_check)) {
    $pattern_ex = '/' . $exclude_check . '/';
    if (preg_match($pattern_ex, $file->filename)) {
      $errors[] = t("The file's name includes the banned expression " . $exclude_check);
    }
  }

  // check for required words/characters
  // check the length of the filename
  if (!empty($size_check)) {
    if ($file->filesize > $size_check * 1024) {
      $errors[] = t("Please select a smaller file. The file size limit is currently " . $size_check . " KB");
    }
  }
  return $errors;
}

Functions

Namesort descending Description
upload_qc_file_validate Implements hook_file_validate(). This implementation includes two test: filesize and existance of db entry with the same URI. If it returns errors, the upload process is blocked and the errors are displayed.
upload_qc_form Form generator for module settings.
upload_qc_menu Implements hook_menu().
upload_qc_permission Implements hook_permission().