Example #1
0
 /**
  * Upload a screenshot
  *
  * @return     void
  */
 public function uploadTask()
 {
     // Incoming
     $pid = Request::getInt('pid', 0);
     if (!$pid) {
         $this->setError(Lang::txt('COM_TOOLS_CONTRIBUTE_NO_ID'));
         $this->displayTask($pid, $version);
         return;
     }
     $version = Request::getVar('version', 'dev');
     $title = preg_replace('/\\s+/', ' ', Request::getVar('title', ''));
     $allowed = array('.gif', '.jpg', '.png', '.bmp');
     $changing_version = Request::getInt('changing_version', 0);
     if ($changing_version) {
         // reload screen
         $this->displayTask($pid, $version);
         return;
     }
     // Get resource information
     $resource = new \Components\Resources\Tables\Resource($this->database);
     $resource->load($pid);
     // Incoming file
     $file = Request::getVar('upload', '', 'files', 'array');
     if (!$file['name']) {
         $this->setError(Lang::txt('COM_TOOLS_CONTRIBUTE_NO_FILE'));
         $this->displayTask($pid, $version);
         return;
     }
     // Make the filename safe
     $file['name'] = Filesystem::clean($file['name']);
     $file['name'] = str_replace(' ', '_', $file['name']);
     $file['name'] = str_replace('-tn', '', $file['name']);
     $file_basename = substr($file['name'], 0, strripos($file['name'], '.'));
     // strip extention
     $file_ext = substr($file['name'], strripos($file['name'], '.'));
     // Make sure we have an allowed format
     if (!in_array(strtolower($file_ext), $allowed)) {
         $this->setError(Lang::txt('COM_TOOLS_CONTRIBUTE_WRONG_FILE_FORMAT'));
         $this->displayTask($pid, $version);
         return;
     }
     // Get version id
     $objV = new \Components\Tools\Tables\Version($this->database);
     $vid = $objV->getVersionIdFromResource($pid, $version);
     if ($vid == NULL) {
         $this->setError(Lang::txt('COM_TOOLS_CONTRIBUTE_VERSION_ID_NOT_FOUND'));
         $this->displayTask($pid, $version);
         return;
     }
     // Instantiate a new screenshot object
     $row = new \Components\Resources\Tables\Screenshot($this->database);
     // Check if file with the same name already exists
     $files = $row->getFiles($pid, $vid);
     if (count($files) > 0) {
         $files = \Components\Tools\Helpers\Utils::transform($files, 'filename');
         foreach ($files as $f) {
             if ($f == $file['name']) {
                 // append extra characters in the end
                 $file['name'] = $file_basename . '_' . time() . $file_ext;
                 $file_basename = $file_basename . '_' . time();
             }
         }
     }
     $row->title = preg_replace('/"((.)*?)"/i', "“\\1”", $title);
     $row->versionid = $vid;
     $ordering = $row->getLastOrdering($pid, $vid);
     $row->ordering = $ordering ? $ordering + 1 : count($files) + 1;
     // put in the end
     $row->filename = $file['name'];
     $row->resourceid = $pid;
     // Check content
     if (!$row->check()) {
         $this->setError($row->getError());
         $this->displayTask($pid, $version);
         return;
     }
     // Build the path
     include_once PATH_CORE . DS . 'components' . DS . 'com_resources' . DS . 'helpers' . DS . 'html.php';
     $listdir = \Components\Resources\Helpers\Html::build_path($resource->created, $pid, '');
     $listdir .= DS . $vid;
     $path = $this->_buildUploadPath($listdir, '');
     // Make sure the upload path exist
     if (!is_dir($path)) {
         if (!Filesystem::makeDirectory($path)) {
             $this->setError(Lang::txt('COM_TOOLS_UNABLE_TO_CREATE_UPLOAD_PATH') . $path);
             $this->displayTask($pid, $version);
             return;
         }
     }
     // Perform the upload
     if (!\Filesystem::upload($file['tmp_name'], $path . DS . $file['name'])) {
         $this->setError(Lang::txt('COM_TOOLS_ERROR_UPLOADING'));
     } else {
         // Store new content
         if (!$row->store()) {
             $this->setError($row->getError());
             $this->displayTask($pid, $version);
             return;
         }
         if (!$row->id) {
             $row->id = $row->insertid();
         }
         // Create thumbnail
         $ss_height = intval($this->config->get('screenshot_maxheight', 58)) > 30 ? intval($this->config->get('screenshot_maxheight', 58)) : 58;
         $ss_width = intval($this->config->get('screenshot_maxwidth', 91)) > 80 ? intval($this->config->get('screenshot_maxwidth', 91)) : 91;
         $tn = \Components\Resources\Helpers\Html::thumbnail($file['name']);
         if ($file_ext != '.swf') {
             $this->_createThumb($path . DS . $file['name'], $ss_width, $ss_height, $path, $tn);
         } else {
             //$this->_createAnimThumb($path . DS . $file['name'], $ss_width, $ss_height, $path, $tn);
         }
     }
     // Store new content
     if (!$row->store()) {
         $this->setError($row->getError());
         $this->displayTask($pid, $version);
         return;
     }
     $this->_rid = $pid;
     // Push through to the screenshot view
     $this->displayTask($pid, $version);
 }