Example #1
0
 /**
  * Uploads a file to a given directory and returns an attachment string
  * that is appended to report/comment bodies
  *
  * @param   string  $listdir  Directory to upload files to
  * @return  string  A string that gets appended to messages
  */
 public function uploadTask($listdir, $post_id)
 {
     // Check if they are logged in
     if (User::isGuest()) {
         return;
     }
     if (!$listdir) {
         $this->setError(Lang::txt('COM_FORUM_NO_UPLOAD_DIRECTORY'));
         return;
     }
     $row = new Tables\Attachment($this->database);
     $row->load(Request::getInt('attachment', 0));
     $row->description = trim(Request::getVar('description', ''));
     $row->post_id = $post_id;
     $row->parent = $listdir;
     // Incoming file
     $file = Request::getVar('upload', '', 'files', 'array');
     if (!$file['name']) {
         if ($row->id) {
             if (!$row->check()) {
                 $this->setError($row->getError());
             }
             if (!$row->store()) {
                 $this->setError($row->getError());
             }
         }
         return;
     }
     // Construct our file path
     $path = PATH_APP . DS . trim($this->config->get('webpath', '/site/forum'), DS) . DS . $listdir;
     if ($post_id) {
         $path .= DS . $post_id;
     }
     // Build the path if it doesn't exist
     if (!is_dir($path)) {
         if (!Filesystem::makeDirectory($path)) {
             $this->setError(Lang::txt('COM_FORUM_UNABLE_TO_CREATE_UPLOAD_PATH'));
             return;
         }
     }
     // Make the filename safe
     $file['name'] = Filesystem::clean($file['name']);
     $file['name'] = str_replace(' ', '_', $file['name']);
     $ext = strtolower(Filesystem::extension($file['name']));
     // Perform the upload
     if (!Filesystem::upload($file['tmp_name'], $path . DS . $file['name'])) {
         $this->setError(Lang::txt('COM_FORUM_ERROR_UPLOADING'));
         return;
     } else {
         // Perform the upload
         if (!Filesystem::isSafe($path . DS . $file['name'])) {
             $this->setError(Lang::txt('COM_FORUM_ERROR_UPLOADING'));
             return;
         }
         // File was uploaded
         // Create database entry
         $row->filename = $file['name'];
         if (!$row->check()) {
             $this->setError($row->getError());
         }
         if (!$row->store()) {
             $this->setError($row->getError());
         }
     }
 }