Exemplo n.º 1
0
 /**
  * Upload file or zipe archive.
  *
  * @return void
  */
 public static function upload($redirect = true)
 {
     $mainframe = JFactory::getApplication();
     /* @var $mainframe JApplication */
     $config = JoomDOCConfig::getInstance();
     // folder where upload
     $path = JoomDOCRequest::getPath();
     $folder = JoomDOCFileSystem::getFullPath($path);
     // control if given folder is subfolder of documents root
     if (!JoomDOCFileSystem::isSubFolder($folder, $config->docroot)) {
         JError::raiseError(403, JText::sprintf('JOOMDOC_UNABLE_UPLOAD_FILE'));
     }
     // unpack uploaded file (multiupload)
     $isZip = JRequest::getInt('iszip');
     // count of uploaded files
     $count = 0;
     $data = JRequest::getVar('upload', null, 'files', 'array');
     $modelDocument = JModelLegacy::getInstance(JOOMDOC_DOCUMENT, JOOMDOC_MODEL_PREFIX);
     /* @var $modelDocument JoomDOCModelDocument */
     if ($data) {
         if ($data['error'] != 0) {
             JError::raiseWarning(21, JText::sprintf('JOOMDOC_UNABLE_UPLOAD_FILE', ''));
         } elseif (!JFolder::exists($folder)) {
             JError::raiseWarning(21, JText::sprintf('JOOMDOC_PARENT_FOLDER_NO_EXISTS'), $folder);
         } else {
             if ($isZip && ($tmpFolder = JoomDOCFileSystem::createTemporaryFolder('joomdoc_unpack'))) {
                 $zip = JArchive::getAdapter('zip');
                 /* @var $zip JArchiveZip */
                 if ($zip->extract($data['tmp_name'], $tmpFolder) !== true) {
                     JError::raiseWarning(21, JText::sprintf('JOOMDOC_UNABLE_EXTRACT_FILE', $data['name']));
                 } else {
                     $rfolder = $path ? $path . DIRECTORY_SEPARATOR : '';
                     foreach (JFolder::folders($tmpFolder, '.', true, true) as $zipFolder) {
                         $newfolder = JPath::clean(str_replace($tmpFolder . DIRECTORY_SEPARATOR, $rfolder, $zipFolder));
                         if (!JoomDOCFileSystem::newFolder(JoomDOCFileSystem::getParentPath($newfolder), JFile::getName($newfolder), false, false)) {
                             return false;
                         }
                     }
                     foreach (JFolder::files($tmpFolder, '.', true, true) as $zipFile) {
                         if ($filePath = JoomDOCFileSystem::uploadFile($folder, $zipFile, str_replace($tmpFolder . DIRECTORY_SEPARATOR, '', $zipFile), true, false)) {
                             if ($config->fileDocumentAutomatically && JoomDOCAccessDocument::create($filePath)) {
                                 $modelDocument->setState('document.id', null);
                                 $modelDocument->save(array('path' => $filePath, 'title' => JFile::getName($filePath), 'state' => JOOMDOC_STATE_PUBLISHED));
                             }
                             $count++;
                         }
                     }
                 }
                 JFolder::delete($tmpFolder);
                 JModelLegacy::getInstance(JOOMDOC_DOCUMENTS, JOOMDOC_MODEL_PREFIX)->flat();
             } elseif ($filePath = JoomDOCFileSystem::uploadFile($folder, $data['tmp_name'], $data['name'])) {
                 if ($config->fileDocumentAutomatically && JoomDOCAccessDocument::create($filePath)) {
                     $modelDocument->setState('document.id', null);
                     $modelDocument->save(array('path' => $filePath, 'title' => JFile::getName($filePath), 'state' => JOOMDOC_STATE_PUBLISHED));
                     $document = $modelDocument->getItem();
                 }
                 $count++;
             }
         }
     }
     if ($redirect) {
         $mainframe->enqueueMessage(JText::sprintf('JOOMDOC_FILES_UPLOADED', $count));
         if ($config->fileDocumentAutomatically && $config->editDocumentImmediately && !$isZip && !empty($document) && JoomDOCAccessDocument::edit($document->id)) {
             if ($mainframe->isAdmin()) {
                 $mainframe->redirect(JoomDOCRoute::editDocument($document->id));
             } else {
                 $mainframe->redirect(JoomDOCRoute::edit($document->path, $document->full_alias));
             }
         } else {
             $mainframe->redirect(JoomDOCRoute::viewDocuments($path, false));
         }
     }
 }