*/ /** * Controller class of the file module * @author NAVER (developers@xpressengine.com) */ class fileController extends file { /** * Initialization * @return void */ function init() { } /** * Upload attachments in the editor * * Determine the upload target srl from editor_sequence and uploadTargetSrl variables. * Create and return the UploadTargetSrl if not exists so that UI can use the value * for sync. * * @return void */ function procFileUpload() { Context::setRequestMethod('JSON'); $file_info = $_FILES['Filedata']; // An error appears if not a normally uploaded file if(!$file_info || !is_uploaded_file($file_info['tmp_name'])) exit(); // Basic variables setting $oFileModel = getModel('file'); $editor_sequence = Context::get('editor_sequence'); $module_srl = $this->module_srl; // Exit a session if there is neither upload permission nor information if(!$_SESSION['upload_info'][$editor_sequence]->enabled) { return $this->setError('msg_not_permitted'); } // Get upload_target_srl $upload_target_srl = intval(Context::get('uploadTargetSrl')) ?: intval(Context::get('upload_target_srl')); if (!$upload_target_srl) { $upload_target_srl = $_SESSION['upload_info'][$editor_sequence]->upload_target_srl; } if (!$upload_target_srl) { $upload_target_srl = getNextSequence(); $_SESSION['upload_info'][$editor_sequence]->upload_target_srl = $upload_target_srl; } // Handle chunking if (preg_match('!^bytes (\d+)-(\d+)/(\d+)$!', $_SERVER['HTTP_CONTENT_RANGE'], $matches)) { // Check basic sanity $chunk_start = intval($matches[1]); $chunk_size = ($matches[2] - $matches[1]) + 1; $total_size = intval($matches[3]); if ($chunk_start < 0 || $chunk_size < 0 || $total_size < 0 || $chunk_start + $chunk_size > $total_size || $chunk_size != $file_info['size']) { return $this->setError('msg_upload_invalid_chunk'); } $this->add('chunk_current_size', $chunk_size); $this->add('chunk_uploaded_size', $chunk_start); // Check existing chunks $nonce = Context::get('nonce'); $temp_key = hash_hmac('sha1', sprintf('%d:%d:%d:%s:%s', $editor_sequence, $upload_target_srl, $module_srl, $file_info['name'], $nonce), config('crypto.authentication_key')); $temp_filename = RX_BASEDIR . 'files/attach/chunks/' . $temp_key; if ($chunk_start == 0 && Rhymix\Framework\Storage::isFile($temp_filename)) { Rhymix\Framework\Storage::delete($temp_filename); $this->add('chunk_status', 11); return $this->setError('msg_upload_invalid_chunk'); } if ($chunk_start != 0 && (!Rhymix\Framework\Storage::isFile($temp_filename) || Rhymix\Framework\Storage::getSize($temp_filename) != $chunk_start)) { Rhymix\Framework\Storage::delete($temp_filename); $this->add('chunk_status', 12); return $this->setError('msg_upload_invalid_chunk'); } // Check size limit $is_admin = (Context::get('logged_info')->is_admin === 'Y'); if (!$is_admin) { $module_config = getModel('file')->getFileConfig($module_srl); $allowed_attach_size = $module_config->allowed_attach_size * 1024 * 1024; $allowed_filesize = $module_config->allowed_filesize * 1024 * 1024; if ($total_size > $allowed_filesize) { $this->add('chunk_status', 21); return $this->setError('msg_exceeds_limit_size'); } $output = executeQuery('file.getAttachedFileSize', (object)array('upload_target_srl' => $upload_target_srl)); if (intval($output->data->attached_size) + $total_size > $allowed_attach_size) { $this->add('chunk_status', 22); return $this->setError('msg_exceeds_limit_size'); } } // Append to chunk $fp = fopen($file_info['tmp_name'], 'r'); $success = Rhymix\Framework\Storage::write($temp_filename, $fp, 'a'); if ($success && Rhymix\Framework\Storage::getSize($temp_filename) == $chunk_start + $chunk_size) { $this->add('chunk_status', 0); $this->add('chunk_uploaded_size', $chunk_start + $chunk_size); if ($chunk_start + $chunk_size == $total_size) { $file_info['tmp_name'] = $temp_filename; $file_info['size'] = Rhymix\Framework\Storage::getSize($temp_filename); } else { return; } } else { Rhymix\Framework\Storage::delete($temp_filename); $this->add('chunk_status', 40); return $this->setError('msg_upload_invalid_chunk'); } } else { $this->add('chunk_status', -1); } // Save the file $output = $this->insertFile($file_info, $module_srl, $upload_target_srl); Context::setResponseMethod('JSON'); $this->add('file_srl', $output->get('file_srl')); $this->add('file_size', $output->get('file_size')); $this->add('direct_download', $output->get('direct_download')); $this->add('source_filename', $output->get('source_filename')); $this->add('upload_target_srl', $output->get('upload_target_srl')); $this->add('download_url', $oFileModel->getDirectFileUrl($output->get('uploaded_filename'))); if($output->error != '0') $this->stop($output->message); } /** * Iframe upload attachments * * @return Object */ function procFileIframeUpload() { // Basic variables setting $editor_sequence = Context::get('editor_sequence'); $callback = Context::get('callback'); $module_srl = $this->module_srl; $upload_target_srl = intval(Context::get('uploadTargetSrl')); if(!$upload_target_srl) $upload_target_srl = intval(Context::get('upload_target_srl')); // Exit a session if there is neither upload permission nor information if(!$_SESSION['upload_info'][$editor_sequence]->enabled) exit(); // Extract from session information if upload_target_srl is not specified if(!$upload_target_srl) $upload_target_srl = $_SESSION['upload_info'][$editor_sequence]->upload_target_srl; // Create if upload_target_srl is not defined in the session information if(!$upload_target_srl) $_SESSION['upload_info'][$editor_sequence]->upload_target_srl = $upload_target_srl = getNextSequence(); // Delete and then attempt to re-upload if file_srl is requested $file_srl = Context::get('file_srl'); if($file_srl) { $oFileModel = getModel('file'); $logged_info = Context::get('logged_info'); $file_info = $oFileModel->getFile($file_srl); if($file_info->file_srl == $file_srl && $oFileModel->getFileGrant($file_info, $logged_info)->is_deletable) { $this->deleteFile($file_srl); } } $file_info = Context::get('Filedata'); // An error appears if not a normally uploaded file if(is_uploaded_file($file_info['tmp_name'])) { $output = $this->insertFile($file_info, $module_srl, $upload_target_srl); Context::set('uploaded_fileinfo',$output); } Context::set('layout','none'); $this->setTemplatePath($this->module_path.'tpl'); $this->setTemplateFile('iframe'); } /** * Image resize * * @return Object */ function procFileImageResize() { $file_srl = Context::get('file_srl'); $width = Context::get('width'); $height = Context::get('height'); if(!$file_srl || !$width) { return $this->setError('msg_invalid_request'); } $oFileModel = getModel('file'); $fileInfo = $oFileModel->getFile($file_srl); if(!$fileInfo || $fileInfo->direct_download != 'Y') { return $this->setError('msg_invalid_request'); } $source_src = $fileInfo->uploaded_filename; $output_src = $source_src . '.resized' . strrchr($source_src,'.'); if(!$height) $height = $width-1; if(FileHandler::createImageFile($source_src,$output_src,$width,$height,'','ratio')) { $output = new stdClass(); $output->info = getimagesize($output_src); $output->src = $output_src; } else { return $this->setError('msg_invalid_request'); } $this->add('resized_info',$output); } /** * Download Attachment * *
* Receive a request directly * file_srl: File sequence * sid : value in DB for comparison, No download if not matched * * This method call trigger 'file.downloadFile'. * before, after. * Trigger object contains: * - download_url * - file_srl * - upload_target_srl * - upload_target_type * - sid * - module_srl * - member_srl * - download_count * - direct_download * - source_filename * - uploaded_filename * - file_size * - comment * - isvalid * - regdate * - ipaddress ** * return void */ function procFileDownload() { $oFileModel = getModel('file'); if(isset($this->grant->access) && $this->grant->access !== true) return $this->setError('msg_not_permitted'); $file_srl = Context::get('file_srl'); $sid = Context::get('sid'); $logged_info = Context::get('logged_info'); // Get file information from the DB $columnList = array('file_srl', 'sid', 'isvalid', 'source_filename', 'module_srl', 'uploaded_filename', 'file_size', 'member_srl', 'upload_target_srl', 'upload_target_type'); $file_obj = $oFileModel->getFile($file_srl, $columnList); // If the requested file information is incorrect, an error that file cannot be found appears if($file_obj->file_srl!=$file_srl || $file_obj->sid!=$sid) return $this->stop('msg_file_not_found'); // Notify that file download is not allowed when standing-by(Only a top-administrator is permitted) if($logged_info->is_admin != 'Y' && $file_obj->isvalid!='Y') return $this->stop('msg_not_permitted_download'); // File name $filename = $file_obj->source_filename; $file_module_config = $oFileModel->getFileModuleConfig($file_obj->module_srl); // Not allow the file outlink if($file_module_config->allow_outlink == 'N' && $_SERVER["HTTP_REFERER"]) { // Handles extension to allow outlink if($file_module_config->allow_outlink_format) { $allow_outlink_format_array = array(); $allow_outlink_format_array = explode(',', $file_module_config->allow_outlink_format); if(!is_array($allow_outlink_format_array)) $allow_outlink_format_array[0] = $file_module_config->allow_outlink_format; foreach($allow_outlink_format_array as $val) { $val = trim($val); if(preg_match("/\.{$val}$/i", $filename)) { $file_module_config->allow_outlink = 'Y'; break; } } } // Sites that outlink is allowed if($file_module_config->allow_outlink != 'Y') { $referer = parse_url($_SERVER["HTTP_REFERER"]); if($referer['host'] != $_SERVER['HTTP_HOST']) { if($file_module_config->allow_outlink_site) { $allow_outlink_site_array = array(); $allow_outlink_site_array = explode("\n", $file_module_config->allow_outlink_site); if(!is_array($allow_outlink_site_array)) $allow_outlink_site_array[0] = $file_module_config->allow_outlink_site; foreach($allow_outlink_site_array as $val) { $site = parse_url(trim($val)); if($site['host'] == $referer['host']) { $file_module_config->allow_outlink = 'Y'; break; } } } } else $file_module_config->allow_outlink = 'Y'; } if($file_module_config->allow_outlink != 'Y') return $this->stop('msg_not_allowed_outlink'); } // Check if a permission for file download is granted $downloadGrantCount = 0; if(is_array($file_module_config->download_grant)) { foreach($file_module_config->download_grant AS $value) if($value) $downloadGrantCount++; } if(is_array($file_module_config->download_grant) && $downloadGrantCount>0) { if(!Context::get('is_logged')) return $this->stop('msg_not_permitted_download'); $logged_info = Context::get('logged_info'); if($logged_info->is_admin != 'Y') { $oModuleModel =& getModel('module'); $columnList = array('module_srl', 'site_srl'); $module_info = $oModuleModel->getModuleInfoByModuleSrl($file_obj->module_srl, $columnList); if(!$oModuleModel->isSiteAdmin($logged_info, $module_info->site_srl)) { $oMemberModel =& getModel('member'); $member_groups = $oMemberModel->getMemberGroups($logged_info->member_srl, $module_info->site_srl); $is_permitted = false; for($i=0;$i
* This method call trigger 'file.insertFile'. * * Before trigger object contains: * - module_srl * - upload_target_srl * * After trigger object contains: * - file_srl * - upload_target_srl * - module_srl * - direct_download * - source_filename * - uploaded_filename * - donwload_count * - file_size * - comment * - member_srl * - sid ** * @param object $file_info PHP file information array * @param int $module_srl Sequence of module to upload file * @param int $upload_target_srl Sequence of target to upload file * @param int $download_count Initial download count * @param bool $manual_insert If set true, pass validation check * @return Object */ function insertFile($file_info, $module_srl, $upload_target_srl, $download_count = 0, $manual_insert = false) { // Call a trigger (before) $trigger_obj = new stdClass; $trigger_obj->file_info = $file_info; $trigger_obj->module_srl = $module_srl; $trigger_obj->upload_target_srl = $upload_target_srl; $output = ModuleHandler::triggerCall('file.insertFile', 'before', $trigger_obj); if(!$output->toBool()) return $output; // A workaround for Firefox upload bug if(preg_match('/^=\?UTF-8\?B\?(.+)\?=$/i', $file_info['name'], $match)) { $file_info['name'] = base64_decode(strtr($match[1], ':', '/')); } if(!$manual_insert) { // Get the file configurations $logged_info = Context::get('logged_info'); if($logged_info->is_admin != 'Y') { $oFileModel = getModel('file'); $config = $oFileModel->getFileConfig($module_srl); // check file type if(isset($config->allowed_filetypes) && $config->allowed_filetypes !== '*.*') { $filetypes = explode(';', $config->allowed_filetypes); $ext = array(); foreach($filetypes as $item) { $item = explode('.', $item); $ext[] = strtolower($item[1]); } $uploaded_ext = explode('.', $file_info['name']); $uploaded_ext = strtolower(array_pop($uploaded_ext)); if(!in_array($uploaded_ext, $ext)) { return $this->stop('msg_not_allowed_filetype'); } } $allowed_filesize = $config->allowed_filesize * 1024 * 1024; $allowed_attach_size = $config->allowed_attach_size * 1024 * 1024; // An error appears if file size exceeds a limit if($allowed_filesize < filesize($file_info['tmp_name'])) return $this->setError('msg_exceeds_limit_size'); // Get total file size of all attachements (from DB) $size_args = new stdClass; $size_args->upload_target_srl = $upload_target_srl; $output = executeQuery('file.getAttachedFileSize', $size_args); $attached_size = (int)$output->data->attached_size + filesize($file_info['tmp_name']); if($attached_size > $allowed_attach_size) return $this->setError('msg_exceeds_limit_size'); } } // Sanitize filename $file_info['name'] = Rhymix\Framework\Filters\FilenameFilter::clean($file_info['name']); // Set upload path by checking if the attachement is an image or other kinds of file if(Rhymix\Framework\Filters\FilenameFilter::isDirectDownload($file_info['name'])) { $path = RX_BASEDIR . sprintf("files/attach/images/%s/%s", $module_srl,getNumberingPath($upload_target_srl,3)); // special character to '_' // change to random file name. because window php bug. window php is not recognize unicode character file name - by cherryfilter $ext = substr(strrchr($file_info['name'],'.'),1); //$_filename = preg_replace('/[#$&*?+%"\']/', '_', $file_info['name']); $filename = $path . Rhymix\Framework\Security::getRandom(32, 'hex') . '.' . $ext; while(file_exists($filename)) { $filename = $path . Rhymix\Framework\Security::getRandom(32, 'hex') . '.' . $ext; } $direct_download = 'Y'; } else { $path = RX_BASEDIR . sprintf("files/attach/binaries/%s/%s", $module_srl, getNumberingPath($upload_target_srl,3)); $filename = $path . Rhymix\Framework\Security::getRandom(32, 'hex'); while(file_exists($filename)) { $filename = $path . Rhymix\Framework\Security::getRandom(32, 'hex'); } $direct_download = 'N'; } // Create a directory if(!Rhymix\Framework\Storage::isDirectory($path) && !Rhymix\Framework\Storage::createDirectory($path)) { return $this->setError('msg_not_permitted_create'); } // Move the file if($manual_insert) { @copy($file_info['tmp_name'], $filename); if(!file_exists($filename)) { @copy($file_info['tmp_name'], $filename); if(!file_exists($filename)) { return $this->setError('msg_file_upload_error'); } } } elseif(starts_with(RX_BASEDIR . 'files/attach/chunks/', $file_info['tmp_name'])) { if (!Rhymix\Framework\Storage::move($file_info['tmp_name'], $filename)) { if (!Rhymix\Framework\Storage::move($file_info['tmp_name'], $filename)) { return $this->setError('msg_file_upload_error'); } } } else { if(!@move_uploaded_file($file_info['tmp_name'], $filename)) { if(!@move_uploaded_file($file_info['tmp_name'], $filename)) { return $this->setError('msg_file_upload_error'); } } } // Get member information $oMemberModel = getModel('member'); $member_srl = $oMemberModel->getLoggedMemberSrl(); // List file information $args = new stdClass; $args->file_srl = getNextSequence(); $args->upload_target_srl = $upload_target_srl; $args->module_srl = $module_srl; $args->direct_download = $direct_download; $args->source_filename = $file_info['name']; $args->uploaded_filename = './' . substr($filename, strlen(RX_BASEDIR)); $args->download_count = $download_count; $args->file_size = @filesize($filename); $args->comment = NULL; $args->member_srl = $member_srl; $args->sid = Rhymix\Framework\Security::getRandom(32, 'hex'); $output = executeQuery('file.insertFile', $args); if(!$output->toBool()) return $output; // Call a trigger (after) ModuleHandler::triggerCall('file.insertFile', 'after', $args); $_SESSION['__XE_UPLOADING_FILES_INFO__'][$args->file_srl] = true; $output->add('file_srl', $args->file_srl); $output->add('file_size', $args->file_size); $output->add('sid', $args->sid); $output->add('direct_download', $args->direct_download); $output->add('source_filename', $args->source_filename); $output->add('upload_target_srl', $upload_target_srl); $output->add('uploaded_filename', $args->uploaded_filename); return $output; } /** * Delete the attachment * *
* This method call trigger 'file.deleteFile'. * Before, after trigger object contains: * - download_url * - file_srl * - upload_target_srl * - upload_target_type * - sid * - module_srl * - member_srl * - download_count * - direct_download * - source_filename * - uploaded_filename * - file_size * - comment * - isvalid * - regdate * - ipaddress ** * @param int $file_srl Sequence of file to delete * @return Object */ function deleteFile($file_srl) { if(!$file_srl) return; $srls = (is_array($file_srl)) ? $file_srl : explode(',', $file_srl); if(!count($srls)) return; $oDocumentController = getController('document'); $documentSrlList = array(); foreach($srls as $srl) { $srl = (int)$srl; if(!$srl) { continue; } $args = new stdClass(); $args->file_srl = $srl; $output = executeQuery('file.getFile', $args); if(!$output->toBool() || !$output->data) { continue; } $file_info = $output->data; if($file_info->upload_target_srl) { $documentSrlList[] = $file_info->upload_target_srl; } $source_filename = $output->data->source_filename; $uploaded_filename = $output->data->uploaded_filename; // Call a trigger (before) $trigger_obj = $output->data; $output = ModuleHandler::triggerCall('file.deleteFile', 'before', $trigger_obj); if(!$output->toBool()) return $output; // Remove from the DB $output = executeQuery('file.deleteFile', $args); if(!$output->toBool()) return $output; // If successfully deleted, remove the file Rhymix\Framework\Storage::delete(FileHandler::getRealPath($uploaded_filename)); // Call a trigger (after) ModuleHandler::triggerCall('file.deleteFile', 'after', $trigger_obj); // Remove empty directories Rhymix\Framework\Storage::deleteEmptyDirectory(dirname(FileHandler::getRealPath($uploaded_filename)), true); } $oDocumentController->updateUploaedCount($documentSrlList); return $output; } /** * Delete all attachments of a particular document * * @param int $upload_target_srl Upload target srl to delete files * @return Object */ function deleteFiles($upload_target_srl) { // Get a list of attachements $oFileModel = getModel('file'); $columnList = array('file_srl', 'uploaded_filename', 'module_srl'); $file_list = $oFileModel->getFiles($upload_target_srl, $columnList); // Success returned if no attachement exists if(!is_array($file_list)||!count($file_list)) return new BaseObject(); // Delete the file foreach ($file_list as $file) { $this->deleteFile($file->file_srl); } // Remove from the DB $args = new stdClass(); $args->upload_target_srl = $upload_target_srl; $output = executeQuery('file.deleteFiles', $args); if(!$output->toBool()) return $output; return $output; } /** * Move an attachement to the other document * * @param int $source_srl Sequence of target to move * @param int $target_module_srl New squence of module * @param int $target_srl New sequence of target * @return void */ function moveFile($source_srl, $target_module_srl, $target_srl) { if($source_srl == $target_srl) return; $oFileModel = getModel('file'); $file_list = $oFileModel->getFiles($source_srl); if(!$file_list) return; $file_count = count($file_list); for($i=0;$i<$file_count;$i++) { unset($file_info); $file_info = $file_list[$i]; $old_file = $file_info->uploaded_filename; // Determine the file path by checking if the file is an image or other kinds if (Rhymix\Framework\Filters\FilenameFilter::isDirectDownload($file_info->source_filename)) { $path = sprintf("./files/attach/images/%s/%s", $target_module_srl, getNumberingPath($target_srl, 3)); $new_file = $path . $file_info->source_filename; } else { $path = sprintf("./files/attach/binaries/%s/%s", $target_module_srl, getNumberingPath($target_srl, 3)); $new_file = $path . Rhymix\Framework\Security::getRandom(32, 'hex'); } // Pass if a target document to move is same if($old_file == $new_file) continue; // Create a directory FileHandler::makeDir($path); // Move the file FileHandler::rename($old_file, $new_file); // Delete old path Rhymix\Framework\Storage::deleteEmptyDirectory(dirname(FileHandler::getRealPath($old_file)), true); // Update DB information $args = new stdClass; $args->file_srl = $file_info->file_srl; $args->uploaded_filename = $new_file; $args->module_srl = $file_info->module_srl; $args->upload_target_srl = $target_srl; executeQuery('file.updateFile', $args); } } public function procFileSetCoverImage() { $vars = Context::getRequestVars(); $logged_info = Context::get('logged_info'); if(!$vars->editor_sequence) return $this->setError('msg_invalid_request'); $upload_target_srl = $_SESSION['upload_info'][$vars->editor_sequence]->upload_target_srl; $oFileModel = getModel('file'); $file_info = $oFileModel->getFile($vars->file_srl); if(!$file_info) return $this->setError('msg_not_founded'); if(!$this->manager && !$file_info->member_srl === $logged_info->member_srl) return $this->setError('msg_not_permitted'); $args = new stdClass(); $args->file_srl = $vars->file_srl; $args->upload_target_srl = $upload_target_srl; $oDB = &DB::getInstance(); $oDB->begin(); $args->cover_image = 'N'; $output = executeQuery('file.updateClearCoverImage', $args); if(!$output->toBool()) { $oDB->rollback(); return $output; } if($file_info->cover_image != 'Y') { $args->cover_image = 'Y'; $output = executeQuery('file.updateCoverImage', $args); if(!$output->toBool()) { $oDB->rollback(); return $output; } } $oDB->commit(); $this->add('is_cover',$args->cover_image); // 썸네일 삭제 $thumbnail_path = sprintf('files/thumbnails/%s', getNumberingPath($upload_target_srl, 3)); Filehandler::removeFilesInDir($thumbnail_path); } /** * Find the attachment where a key is upload_target_srl and then return java script code * * @deprecated * @param int $editor_sequence * @param int $upload_target_srl * @return void */ function printUploadedFileList($editor_sequence, $upload_target_srl) { return; } function triggerCopyModule(&$obj) { $oModuleModel = getModel('module'); $fileConfig = $oModuleModel->getModulePartConfig('file', $obj->originModuleSrl); $oModuleController = getController('module'); if(is_array($obj->moduleSrlList)) { foreach($obj->moduleSrlList AS $key=>$moduleSrl) { $oModuleController->insertModulePartConfig('file', $moduleSrl, $fileConfig); } } } } /* End of file file.controller.php */ /* Location: ./modules/file/file.controller.php */