Exemple #1
0
 /**
  * Upload a file to the wiki
  *
  * @return     void
  */
 public function uploadTask()
 {
     // Check if they're logged in
     if (User::isGuest()) {
         $this->displayTask();
         return;
     }
     if (Request::getVar('no_html', 0)) {
         return $this->ajaxUploadTask();
     }
     // Ensure we have an ID to work with
     $listdir = Request::getInt('listdir', 0, 'post');
     if (!$listdir) {
         $this->setError(Lang::txt('COM_WIKI_NO_ID'));
         $this->displayTask();
         return;
     }
     // Incoming file
     $file = Request::getVar('upload', '', 'files', 'array');
     if (!$file['name']) {
         $this->setError(Lang::txt('COM_WIKI_NO_FILE'));
         $this->displayTask();
         return;
     }
     $attachment = new Tables\Attachment($this->database);
     // Build the upload path if it doesn't exist
     $path = $attachment->filespace() . DS . $listdir;
     if (!is_dir($path)) {
         if (!Filesystem::makeDirectory($path)) {
             $this->setError(Lang::txt('COM_WIKI_ERROR_UNABLE_TO_CREATE_DIRECTORY'));
             $this->displayTask();
             return;
         }
     }
     // Make the filename safe
     $file['name'] = urldecode($file['name']);
     $file['name'] = Filesystem::clean($file['name']);
     $file['name'] = str_replace(' ', '_', $file['name']);
     // Upload new files
     if (!Filesystem::upload($file['tmp_name'], $path . DS . $file['name'])) {
         $this->setError(Lang::txt('COM_WIKI_ERROR_UPLOADING'));
     } else {
         // Create database entry
         $attachment->pageid = $listdir;
         $attachment->filename = $file['name'];
         $attachment->description = trim(Request::getVar('description', '', 'post'));
         $attachment->created = Date::toSql();
         $attachment->created_by = User::get('id');
         if (!$attachment->check()) {
             $this->setError($attachment->getError());
         }
         if (!$attachment->store()) {
             $this->setError($attachment->getError());
         }
     }
     // Push through to the media view
     $this->displayTask();
 }