/** * Display an upload form and file listing * * @return void */ public function displayTask() { // Incoming directory (this should be a path built from a resource ID and its creation year/month) $resource = Request::getInt('resource', 0); if (!$resource) { echo '<p class="error">' . Lang::txt('No resource ID provided.') . '</p>'; return; } if ($resource < 1 || substr($resource, 0, 4) == '9999') { $row = Resource::blank(); } else { $row = Resource::oneOrFail($resource); } $row->set('id', $resource); // Incoming sub-directory $subdir = Request::getVar('subdir', ''); // Allow for temp resource uploads if (!$row->get('created') || $row->get('created') == '0000-00-00 00:00:00') { $row->set('created', Date::format('Y-m-d 00:00:00')); } $path = $row->filespace() . DS . 'media'; $folders = array(); $docs = array(); if (is_dir($path)) { // Loop through all files and separate them into arrays of images, folders, and other $dirIterator = new \DirectoryIterator($path); foreach ($dirIterator as $file) { if ($file->isDot()) { continue; } $name = $file->getFilename(); if ($file->isDir()) { $folders[$path . DS . $name] = $name; continue; } if ($file->isFile()) { if ('cvs' == strtolower($name) || '.svn' == strtolower($name)) { continue; } $docs[$path . DS . $name] = $name; } } ksort($folders); ksort($docs); } // Output the HTML $this->view->set('resource', $resource)->set('row', $row)->set('subdir', $subdir)->set('path', $path)->set('docs', $docs)->set('folders', $folders)->setErrors($this->getErrors())->setLayout('display')->display(); }
/** * Save an attachment * * @return void */ public function saveTask() { if (Request::getVar('no_html', 0)) { return $this->ajaxUploadTask(); } // Incoming $pid = Request::getInt('pid', 0); if (!$pid) { $this->setError(Lang::txt('CONTRIBUTE_NO_ID')); return $this->displayTask($pid); } // Incoming file $file = Request::getVar('upload', '', 'files', 'array'); if (!$file['name']) { $this->setError(Lang::txt('CONTRIBUTE_NO_FILE')); return $this->displayTask($pid); } // Make the filename safe $file['name'] = Filesystem::clean($file['name']); // Ensure file names fit. $ext = Filesystem::extension($file['name']); $file['name'] = str_replace(' ', '_', $file['name']); if (strlen($file['name']) > 230) { $file['name'] = substr($file['name'], 0, 230); $file['name'] .= '.' . $ext; } // Instantiate a new resource object $resource = Resource::blank()->set(array('title' => $file['name'], 'introtext' => $file['name'], 'created' => Date::toSql(), 'created_by' => User::get('id'), 'published' => 1, 'publish_up' => Date::toSql(), 'publish_down' => '0000-00-00 00:00:00', 'standalone' => 0, 'access' => 0, 'path' => '', 'type' => $this->_getChildType($file['name']))); // File already exists $parent = Resource::oneOrFail($pid); if ($parent->hasChild($file['name'])) { $this->setError(Lang::txt('A file with this name and type appears to already exist.')); return $this->displayTask($pid); } // Store new content if (!$resource->save()) { $this->setError($resource->getError()); return $this->displayTask($pid); } // Build the path $listdir = $this->_buildPathFromDate($resource->get('created'), $resource->get('id'), ''); $path = $this->_buildUploadPath($listdir, ''); // Make sure the upload path exist if (!is_dir($path)) { if (!Filesystem::makeDirectory($path)) { $this->setError(Lang::txt('COM_CONTRIBUTE_UNABLE_TO_CREATE_UPLOAD_PATH')); return $this->displayTask($pid); } } // Perform the upload if (!Filesystem::upload($file['tmp_name'], $path . DS . $file['name'])) { $this->setError(Lang::txt('COM_CONTRIBUTE_ERROR_UPLOADING')); } else { // File was uploaded // Check the file type $resource->set('type', $this->_getChildType($file['name'])); } // Scan for viruses $fpath = $path . DS . $file['name']; if (!Filesystem::isSafe($fpath)) { if (Filesystem::delete($fpath)) { // Delete resource $resource->destroy(); } $this->setError(Lang::txt('File rejected because the anti-virus scan failed.')); return $this->displayTask($pid); } // Set path value // // NOTE: This is relative to the base resources upload path if (!$resource->get('path')) { $resource->set('path', $listdir . DS . $file['name']); } $resource->set('path', ltrim($resource->get('path'), DS)); // Store new content if (!$resource->save()) { $this->setError($resource->getError()); return $this->displayTask($pid); } // Create new parent/child association if (!$resource->makeChildOf($pid)) { $this->setError($resource->getError()); return $this->displayTask($pid); } // Textifier $this->textifier($fpath, $resource->get('id')); // Push through to the attachments view $this->displayTask($pid); }