View source
<?php
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;
}
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.'),
),
);
}
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;
}
function upload_qc_file_validate($file) {
$errors = array();
if (!is_object($file)) {
return $errors;
}
$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', '');
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.");
}
}
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.");
}
}
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);
}
}
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);
}
}
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;
}