You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
105 lines
3.9 KiB
105 lines
3.9 KiB
<?php
|
|
function upload_event_files($current, $type, $uploadedFile, $key=-1){
|
|
$destination = origin_folders($type);
|
|
if( (is_dir($destination) && is_writable($destination))) {
|
|
//this happens when uploading an array of files, we need to get the exact file to be uploaded from the tray
|
|
if($key>=0){
|
|
$uploadedFile = array(
|
|
"name" => $uploadedFile["name"][$key],
|
|
"size" => $uploadedFile["size"][$key],
|
|
"tmp_name" => $uploadedFile["tmp_name"][$key],
|
|
"error" => $uploadedFile["error"][$key],
|
|
"type" => $uploadedFile["type"][$key]
|
|
);
|
|
}
|
|
|
|
if($type=="sponsors"){ //for sponsor pictures
|
|
$allowed_file_ext = array("jpeg", "gif", "jpg", "png");
|
|
$allowed_mime_types = array("image");
|
|
} else if($type=="events") { //for event attachments
|
|
$allowed_file_ext = array("mp3", "mp4", "png", "jpeg", "gif", "jpg");
|
|
$allowed_mime_types = array("audio", "video", "image");
|
|
}
|
|
|
|
$tmp = explode(".", $uploadedFile["name"] );
|
|
$file_ext = $tmp[countVal($tmp)-1];
|
|
$rename = generate_random_keys(20).'.'.$file_ext;
|
|
$dest_path = $destination.$rename;
|
|
|
|
if ($uploadedFile['error'] !== UPLOAD_ERR_OK) {
|
|
output_to_json($current, array(
|
|
"mtype" => "error" ,
|
|
"message" => $current->lang->line("upload_error_code") .$uploadedFile['error']
|
|
));
|
|
} else if(!is_uploaded_file($uploadedFile['tmp_name'])) {
|
|
output_to_json($current, array(
|
|
"mtype" => "warning" ,
|
|
"message" => "Possible file upload attack from : ".$uploadedFile['tmp_name']
|
|
));
|
|
} else if(!in_array(strtolower($file_ext), $allowed_file_ext) || !in_array(current( explode("/",$uploadedFile['type'])), $allowed_mime_types)) {
|
|
output_to_json($current, array(
|
|
"mtype" => "warning" ,
|
|
"message" => "The file you are uploading is not allowed!"
|
|
));
|
|
} else if( move_uploaded_file($uploadedFile['tmp_name'], $dest_path) ) {
|
|
|
|
return array(
|
|
"validSize" => getImageRatios($uploadedFile,$file_ext,$dest_path),
|
|
"file_name" => $rename,
|
|
"file_size" => $uploadedFile['size']
|
|
);
|
|
//process upload
|
|
|
|
} else {
|
|
output_to_json($current, array(
|
|
"mtype" => "error" ,
|
|
"message" => "Upload failed! Try again later."
|
|
));
|
|
}
|
|
} else {
|
|
output_to_json($current, array(
|
|
"mtype" => "warning" ,
|
|
"message" => "Upload destination directory is not writtable."
|
|
));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* get Image ratios return "True" if ration Heigh width is less than expected desire height or width
|
|
*
|
|
* @param array $uploadedFile
|
|
* @param string $file_ext
|
|
* @param string $dest_path
|
|
* @return bool
|
|
*/
|
|
function getImageRatios($uploadedFile,$file_ext,$dest_path){
|
|
|
|
$allowed_file_ext = array("jpeg", "gif", "jpg", "png");
|
|
$allowed_mime_types = array("image");
|
|
|
|
if(!in_array(strtolower($file_ext), $allowed_file_ext) || !in_array(current( explode("/",$uploadedFile['type'])), $allowed_mime_types)) {
|
|
return [
|
|
"isInValid" => false,
|
|
"message" => "File extension for ration compare should be jpg,gif,jpeg,png"
|
|
];
|
|
}
|
|
|
|
if($dest_path == null || empty($dest_path)){
|
|
return [
|
|
"isInValid" => false,
|
|
"message" => "Destination path is empty!"
|
|
];
|
|
}
|
|
|
|
list($width, $height) = getimagesize($dest_path);
|
|
|
|
if((int)$width < DESIRED_IMAGE_WIDTH && (int)$height < DESIRED_IMAGE_HEIGHT){
|
|
return [
|
|
"isInValid" => true,
|
|
"message" => "Ratio should be: ".DESIRED_IMAGE_WIDTH.'x'.DESIRED_IMAGE_HEIGHT,
|
|
"info" => $width.'x'.$height
|
|
];
|
|
}
|
|
|
|
return ["isInValid" => false];
|
|
}
|