private function checkMimeType($source_directory, $filename, $ftype) { switch ($ftype) { case DOCUMENT_FOLDER: $type_list = \PHPWS_Settings::get('filecabinet', 'document_files'); break; case IMAGE_FOLDER: $type_list = \PHPWS_Settings::get('filecabinet', 'image_files'); break; case MULTIMEDIA_FOLDER: $type_list = \PHPWS_Settings::get('filecabinet', 'media_files'); break; } $ext = PHPWS_File::getFileExtension($filename); // First check if the extension is allowed for the current folder type. $type_array = explode(',', str_replace(' ', '', $type_list)); if (!in_array($ext, $type_array)) { $this->sendErrorHeader('File type not allowed in folder'); } // second check that file is the type it claims to be if (!PHPWS_File::checkMimeType($source_directory, $ext)) { $this->sendErrorHeader('Unknown file type'); } }
public function classifyIntoFolder($folder_id, $files) { if (empty($files)) { return; } $folder = new Folder($folder_id); if (!$folder->id) { return; } $classify_dir = $this->getClassifyDir(); if (empty($classify_dir)) { return; } switch ($folder->ftype) { case IMAGE_FOLDER: $type = 'image'; PHPWS_Core::initModClass('filecabinet', 'Image.php'); $class_name = 'PHPWS_Image'; break; case DOCUMENT_FOLDER: $type = 'document'; PHPWS_Core::initModClass('filecabinet', 'Document.php'); $class_name = 'PHPWS_Document'; break; case MULTIMEDIA_FOLDER: $type = 'media'; PHPWS_Core::initModClass('filecabinet', 'Multimedia.php'); $class_name = 'PHPWS_Multimedia'; break; } $allowed_types = $this->getAllowedTypes($type); foreach ($files as $filename) { $path = $classify_dir . $filename; $ext = PHPWS_File::getFileExtension($filename); if ($this->fileTypeAllowed($path, $type) && PHPWS_File::checkMimeType($path) && in_array($ext, $allowed_types)) { $file_obj = new $class_name(); $file_obj->folder_id = $folder->id; $file_obj->setFilename($filename); $file_obj->setTitle(str_replace(".{$ext}", '', $filename)); $file_obj->setDirectory($folder->getFullDirectory()); $folder_directory = $file_obj->getPath(); if (!@rename($path, $folder_directory)) { PHPWS_Error::log(FC_FILE_MOVE, 'filecabinet', 'Cabinet::classifyIntoFolder', $folder_directory); continue; } $file_obj->file_type = PHPWS_File::getMimeType($file_obj->getPath()); $file_obj->loadFileSize(); if ($folder->ftype == IMAGE_FOLDER) { $file_obj->loadDimensions(); $file_obj->save(true, false); } else { $file_obj->save(false); } } } }
public function classifyFile($files) { $tpl = null; $this->cabinet->title = dgettext('filecabinet', 'Classify Files'); $classify_dir = $this->cabinet->getClassifyDir(); if (empty($classify_dir) || !is_dir($classify_dir)) { $this->cabinet->content = dgettext('filecabinet', 'Unable to locate the classify directory. Please check your File Cabinet settings, configuration file and directory permissions.'); return; } if (!is_array($files)) { $files = array($files); } // image folders $image_folders = Cabinet::listFolders(IMAGE_FOLDER, true); // document folders $document_folders = Cabinet::listFolders(DOCUMENT_FOLDER, true); // multimedia folders $multimedia_folders = Cabinet::listFolders(MULTIMEDIA_FOLDER, true); $count = 0; $image_types = $this->cabinet->getAllowedTypes('image'); $document_types = $this->cabinet->getAllowedTypes('document'); $media_types = $this->cabinet->getAllowedTypes('media'); foreach ($files as $file) { if (!is_file($classify_dir . $file) || !PHPWS_File::checkMimeType($classify_dir . $file) || !$this->cabinet->fileTypeAllowed($file)) { continue; } $form = new PHPWS_Form('file_form_' . $count); $ext = PHPWS_File::getFileExtension($file); if (in_array($ext, $image_types)) { $folders =& $image_folders; } elseif (in_array($ext, $media_types)) { $folders =& $multimedia_folders; } elseif (in_array($ext, $document_types)) { $folders =& $document_folders; } else { continue; } $form->addSelect("folder[{$count}]", $folders); $form->setTag("folder[{$count}]", 'folder'); $form->setLabel("folder[{$count}]", dgettext('filecabinet', 'Folder')); $form->addText("file_title[{$count}]", $file); $form->setLabel("file_title[{$count}]", dgettext('filecabinet', 'Title')); $form->setTag("file_title[{$count}]", 'file_title'); $form->setSize("file_title[{$count}]", 40); $form->addTextarea("file_description[{$count}]"); $form->setLabel("file_description[{$count}]", dgettext('filecabinet', 'Description')); $form->setTag("file_description[{$count}]", 'file_description'); $form->addSubmit('submit', dgettext('filecabinet', 'Classify files')); $subtpl = $form->getTemplate(); $subtpl['HIDDEN'] = sprintf('<input type="hidden" name="file_count[%s]" value="%s" />', $count, $file); $subtpl['FILE_NAME'] = $file; $subtpl['FILE_NAME_LABEL'] = dgettext('filecabinet', 'File name'); unset($subtpl['START_FORM']); unset($subtpl['END_FORM']); $tpl['files'][] = $subtpl; $count++; } $form = new PHPWS_Form('classify_files'); $form->addHidden('module', 'filecabinet'); $form->addHidden('aop', 'post_classifications'); if (!empty($tpl)) { $form_template = $form->getTemplate(true, true, $tpl); $this->cabinet->content = PHPWS_Template::process($form_template, 'filecabinet', 'Forms/classify_file.tpl'); } else { $this->cabinet->content = dgettext('filecabinet', 'Unable to classify files.'); } }