function upload_qc_file_validate in Upload QC 7
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.
File
- ./
upload_qc.module, line 90
Code
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;
}