示例#1
0
 /**
  * Upload a file to the wiki
  *
  * @return     void
  */
 public function uploadTask()
 {
     // Check if they are logged in
     if (User::isGuest()) {
         return false;
     }
     Request::checkToken();
     // Incoming member ID
     $id = Request::getInt('id', 0);
     if (!$id) {
         $this->setError(Lang::txt('MEMBERS_NO_ID'));
         $this->displayTask('', $id);
         return;
     }
     // Incoming file
     $file = Request::getVar('upload', '', 'files', 'array');
     if (!$file['name']) {
         $this->setError(Lang::txt('MEMBERS_NO_FILE'));
         $this->displayTask('', $id);
         return;
     }
     // Build upload path
     $dir = \Hubzero\Utility\String::pad($id);
     $path = PATH_APP . DS . $this->filespace() . DS . $dir;
     if (!is_dir($path)) {
         if (!Filesystem::makeDirectory($path)) {
             $this->setError(Lang::txt('UNABLE_TO_CREATE_UPLOAD_PATH'));
             $this->displayTask('', $id);
             return;
         }
     }
     // Make the filename safe
     $file['name'] = Filesystem::clean($file['name']);
     $file['name'] = str_replace(' ', '_', $file['name']);
     // Do we have an old file we're replacing?
     $curfile = Request::getVar('currentfile', '');
     // Perform the upload
     if (!Filesystem::upload($file['tmp_name'], $path . DS . $file['name'])) {
         $this->setError(Lang::txt('ERROR_UPLOADING'));
         $file = $curfile;
     } else {
         $ih = new \Components\Members\Helpers\ImgHandler();
         if ($curfile != '') {
             // Yes - remove it
             if (file_exists($path . DS . $curfile)) {
                 if (!Filesystem::delete($path . DS . $curfile)) {
                     $this->setError(Lang::txt('UNABLE_TO_DELETE_FILE'));
                     $this->displayTask($file['name'], $id);
                     return;
                 }
             }
             $curthumb = $ih->createThumbName($curfile);
             if (file_exists($path . DS . $curthumb)) {
                 if (!Filesystem::delete($path . DS . $curthumb)) {
                     $this->setError(Lang::txt('UNABLE_TO_DELETE_FILE'));
                     $this->displayTask($file['name'], $id);
                     return;
                 }
             }
         }
         // Instantiate a profile, change some info and save
         $profile = \Hubzero\User\Profile::getInstance($id);
         $profile->set('picture', $file['name']);
         if (!$profile->update()) {
             $this->setError($profile->getError());
         }
         // Resize the image if necessary
         $ih->set('image', $file['name']);
         $ih->set('path', $path . DS);
         $ih->set('maxWidth', 186);
         $ih->set('maxHeight', 186);
         if (!$ih->process()) {
             $this->setError($ih->getError());
         }
         // Create a thumbnail image
         $ih->set('maxWidth', 50);
         $ih->set('maxHeight', 50);
         $ih->set('cropratio', '1:1');
         $ih->set('outputName', $ih->createThumbName());
         if (!$ih->process()) {
             $this->setError($ih->getError());
         }
         $file = $file['name'];
     }
     // Push through to the image view
     $this->displayTask($file, $id);
 }