reasonableframework/system/storage.php

190 lines
5.1 KiB
PHP
Raw Normal View History

2018-03-04 17:23:35 +00:00
<?php
2018-05-07 13:40:15 +00:00
/**
* @file stroage.php
2018-05-21 08:24:45 +00:00
* @date 2018-05-21
2018-05-07 13:40:15 +00:00
* @author Go Namhyeon <gnh1201@gmail.com>
* @brief Stroage module
*/
2018-05-12 16:07:50 +00:00
if(!function_exists("get_storage_dir")) {
2018-05-07 13:40:15 +00:00
function get_storage_dir() {
$config = get_config();
return get_value_in_array("storage_dir", $config, "storage");
}
}
if(!function_exists("get_storage_path")) {
2018-05-07 14:14:02 +00:00
function get_storage_path($type="data") {
2018-05-12 16:07:50 +00:00
$dir_path = sprintf("./%s/%s", get_storage_dir(), $type);
if(!is_dir($dir_path)) {
if(!mkdir($dir_path, 0777)) {
set_error("can not create directory");
show_errors();
}
}
return $dir_path;
2018-05-07 13:40:15 +00:00
}
}
if(!function_exists("get_storage_url")) {
2018-05-07 14:14:02 +00:00
function get_storage_url($type="data") {
2018-05-07 13:40:15 +00:00
return sprintf("%s%s/%s", base_url(), get_storage_dir(), $type);
}
}
if(!function_exists("move_uploaded_file_to_storage")) {
2018-05-07 14:09:52 +00:00
function move_uploaded_file_to_stroage($options=array()) {
2018-05-07 14:14:02 +00:00
$files = array();
2018-05-07 14:09:52 +00:00
$requests = get_requests();
2018-05-07 13:40:15 +00:00
2018-05-07 14:09:52 +00:00
$storage_type = get_value_in_array("storage_type", $options, "data");
$upload_base_dir = get_storage_path($storage_type);
$upload_base_url = get_storage_url($storage_type);
2018-05-07 13:40:15 +00:00
2018-05-07 14:09:52 +00:00
if(!array_key_empty("only_image", $options)) {
$upload_allow_ext = array(
"png", "gif", "jpg", "jpeg", "tif"
);
} elseif(!array_key_empty("only_docs", $options)) {
$upload_allow_ext = array(
"png", "gif", "jpg", "jpeg", "tif",
"xls", "ppt", "doc", "xlsx", "pptx",
"docx", "odt", "odp", "ods", "xlsm",
"tiff", "pdf", "xlsm"
);
} elseif(!array_key_empty("only_audio", $options)) {
$upload_allow_ext = array(
"mp3", "ogg", "m4a", "wma", "wav"
);
2018-05-07 13:40:15 +00:00
} else {
$upload_allow_ext = array();
}
2018-05-07 14:14:02 +00:00
foreach($files as $k=>$file) {
2018-05-25 16:33:24 +00:00
$upload_ext = get_file_extension($files[$k]['name']);
2018-05-07 14:09:52 +00:00
$upload_name = make_random_id(32) . (empty($upload_ext) ? "" : "." . $upload_ext);
2018-05-07 13:40:15 +00:00
$upload_file = $upload_base_dir . $upload_name;
$upload_url = $upload_base_url . $upload_name;
if(count($upload_allow_ext) == 0 || in_array($upload_ext, $upload_allow_ext)) {
2018-05-07 14:14:02 +00:00
if(move_uploaded_file($files[$k]['tmp_name'], $upload_file)) {
2018-05-07 13:40:15 +00:00
$response['files'][] = array(
2018-05-12 11:53:48 +00:00
"storage_type" => $storage_type,
2018-05-07 13:40:15 +00:00
"upload_ext" => $upload_ext,
"upload_name" => $upload_name,
"upload_file" => $upload_file,
"upload_url" => $upload_url,
"upload_error" => ""
);
} else {
$response['files'][] = array(
"upload_error" => "File write error."
);
}
} else {
$response['files'][] = array(
"upload_error" => "Not allowed file type."
);
}
}
return $response['files'];
}
}
2018-05-07 14:09:52 +00:00
if(!function_exists("read_storage_file")) {
function read_storage_file($filename, $options=array()) {
2018-05-21 08:24:45 +00:00
$result = false;
2018-05-12 16:07:50 +00:00
2018-05-07 14:09:52 +00:00
$storage_type = get_value_in_array("storage_type", $options, "data");
2018-05-12 11:53:48 +00:00
$upload_base_path = get_storage_path($storage_type);
2018-05-07 14:09:52 +00:00
$upload_base_url = get_storage_url($storage_type);
2018-05-12 11:53:48 +00:00
$upload_filename = $upload_base_path . "/" . $filename;
2018-05-07 14:09:52 +00:00
2018-05-21 08:24:45 +00:00
if(file_exists($upload_filename)) {
if($fhandle = fopen($upload_filename, "r")) {
$result = fread($fhandle, filesize($upload_filename));
fclose($fhandle);
}
2018-05-07 14:09:52 +00:00
2018-05-21 08:24:45 +00:00
if(!array_key_empty("encode_base64", $options)) {
$result = base64_encode($result);
}
2018-05-07 14:09:52 +00:00
}
2018-05-21 08:24:45 +00:00
return $result;
2018-05-07 14:09:52 +00:00
}
2018-03-04 17:23:35 +00:00
}
2018-05-12 11:53:48 +00:00
if(!function_exists("write_storage_file")) {
function write_storage_file($data, $options=array()) {
2018-05-12 16:07:50 +00:00
$result = false;
2018-05-12 11:53:48 +00:00
2018-05-21 08:24:45 +00:00
$filename = get_value_in_array("filename", $options, make_random_id(32));
2018-05-12 11:53:48 +00:00
$storage_type = get_value_in_array("storage_type", $options, "data");
$upload_base_path = get_storage_path($storage_type);
$upload_base_url = get_storage_url($storage_type);
$upload_filename = $upload_base_path . "/" . $filename;
2018-05-12 11:56:27 +00:00
if(file_exists($upload_filename)) {
2018-05-21 08:24:45 +00:00
if(!array_key_empty("filename", $options)) {
$result = $upload_filename;
} else {
$result = write_storage_file($data, $options);
}
2018-05-12 11:53:48 +00:00
} else {
2018-05-12 11:56:27 +00:00
if($fhandle = fopen($upload_filename, "w")) {
2018-05-12 16:07:50 +00:00
if(fwrite($fhandle, $data)) {
$result = $upload_filename;
}
2018-05-12 11:56:27 +00:00
fclose($fhandle);
} else {
set_error("maybe, your storage is write-protected.");
show_errors();
}
2018-05-12 11:53:48 +00:00
}
2018-05-12 16:07:50 +00:00
2018-05-12 11:53:48 +00:00
return $result;
}
}
2018-05-21 08:24:45 +00:00
if(!function_exists("get_real_path")) {
function get_real_path($file) {
return file_exists($file) ? realpath($file) : false;
}
}
2018-05-25 16:17:21 +00:00
2018-05-25 16:34:30 +00:00
if(!function_exists("retrieve_storage_files")) {
function retrieve_storage_files($type, $recursive=false, $excludes=array(".", ".."), $files=array()) {
$storage_path = get_storage_path($type);
if(is_dir($storage_path)) {
if($handle = opendir($storage_path)) {
while(false !== ($file = readdir($handle))) {
if(!in_array($file, $exclude)) {
$file_path = $storage_path . "/" . $file;
if(is_file($file_path)) {
$files[] = $file_path;
} elseif($recursive) {
$files = retrieve_storage_dir($type . "/" . $file, $recursive, $excludes, $files);
}
}
}
closedir($handle);
}
}
return $files;
}
2018-05-25 16:17:21 +00:00
}
2018-05-25 16:25:51 +00:00
2018-05-25 16:34:30 +00:00
if(!function_exists("get_file_extension")) {
function get_file_extension($file) {
return pathinfo($file, PATHINFO_EXTENSION);
}
2018-05-25 16:25:51 +00:00
}
2018-05-25 16:34:30 +00:00
if(!function_exists("check_file_extension")) {
function check_file_extension($file, $extension) {
return (get_file_extension($file) === $extension);
}
2018-05-25 16:33:24 +00:00
}