Пример #1
0
 /**
  * Upload a file to the profile via AJAX
  *
  * @return     string
  */
 public function doajaxuploadTask()
 {
     //allowed extensions for uplaod
     $allowedExtensions = array('png', 'jpe', 'jpeg', 'jpg', 'gif');
     //max upload size
     $sizeLimit = $this->config->get('maxAllowed', '40000000');
     // get the file
     if (isset($_GET['qqfile'])) {
         $stream = true;
         $file = $_GET['qqfile'];
         $size = (int) $_SERVER["CONTENT_LENGTH"];
     } elseif (isset($_FILES['qqfile'])) {
         $stream = false;
         $file = $_FILES['qqfile']['name'];
         $size = (int) $_FILES['qqfile']['size'];
     } else {
         echo json_encode(array('error' => Lang::txt('Please select a file to upload')));
         return;
     }
     //check to make sure we have a file and its not too big
     if ($size == 0) {
         echo json_encode(array('error' => Lang::txt('File is empty')));
         return;
     }
     if ($size > $sizeLimit) {
         $max = preg_replace('/<abbr \\w+=\\"\\w+\\">(\\w{1,3})<\\/abbr>/', '$1', \Hubzero\Utility\Number::formatBytes($sizeLimit));
         echo json_encode(array('error' => Lang::txt('File is too large. Max file upload size is ') . $max));
         return;
     }
     //check to make sure we have an allowable extension
     $pathinfo = pathinfo($file);
     $filename = $pathinfo['filename'];
     $ext = $pathinfo['extension'];
     if ($allowedExtensions && !in_array(strtolower($ext), $allowedExtensions)) {
         $these = implode(', ', $allowedExtensions);
         echo json_encode(array('error' => Lang::txt('File has an invalid extension, it should be one of ' . $these . '.')));
         return;
     }
     // Make the filename safe
     $file = Filesystem::clean($file);
     // Check project exists
     if (!$this->model->exists()) {
         echo json_encode(array('error' => Lang::txt('Error loading project')));
         return;
     }
     // Make sure user is authorized (project manager)
     if (!$this->model->access('manager')) {
         echo json_encode(array('error' => Lang::txt('Unauthorized action')));
         return;
     }
     // Build project image path
     $path = PATH_APP . DS . trim($this->config->get('imagepath', '/site/projects'), DS);
     $path .= DS . $this->model->get('alias') . DS . 'images';
     if (!is_dir($path)) {
         if (!Filesystem::makeDirectory($path, 0755, true, true)) {
             echo json_encode(array('error' => Lang::txt('COM_PROJECTS_UNABLE_TO_CREATE_UPLOAD_PATH')));
             return;
         }
     }
     // Delete older file with same name
     if (file_exists($path . DS . $file)) {
         Filesystem::delete($path . DS . $file);
     }
     if ($stream) {
         //read the php input stream to upload file
         $input = fopen("php://input", "r");
         $temp = tmpfile();
         $realSize = stream_copy_to_stream($input, $temp);
         fclose($input);
         if (Helpers\Html::virusCheck($temp)) {
             echo json_encode(array('error' => Lang::txt('Virus detected, refusing to upload')));
             return;
         }
         //move from temp location to target location which is user folder
         $target = fopen($path . DS . $file, "w");
         fseek($temp, 0, SEEK_SET);
         stream_copy_to_stream($temp, $target);
         fclose($target);
     } else {
         move_uploaded_file($_FILES['qqfile']['tmp_name'], $path . DS . $file);
     }
     // Perform the upload
     if (!is_file($path . DS . $file)) {
         echo json_encode(array('error' => Lang::txt('COM_PROJECTS_ERROR_UPLOADING')));
         return;
     } else {
         //resize image to max 200px and rotate in case user didnt before uploading
         $hi = new \Hubzero\Image\Processor($path . DS . $file);
         if (count($hi->getErrors()) == 0) {
             $hi->autoRotate();
             $hi->resize(200);
             $hi->setImageType(IMAGETYPE_PNG);
             $hi->save($path . DS . $file);
         } else {
             echo json_encode(array('error' => $hi->getError()));
             return;
         }
         // Delete previous thumb
         if (file_exists($path . DS . 'thumb.png')) {
             Filesystem::delete($path . DS . 'thumb.png');
         }
         // create thumb
         $hi = new \Hubzero\Image\Processor($path . DS . $file);
         if (count($hi->getErrors()) == 0) {
             $hi->resize(50, false, true, true);
             $hi->save($path . DS . 'thumb.png');
         } else {
             echo json_encode(array('error' => $hi->getError()));
             return;
         }
         // Save picture name
         $this->model->set('picture', $file);
         if (!$this->model->store()) {
             echo json_encode(array('error' => $this->model->getError()));
             return;
         } elseif (!$this->model->inSetup()) {
             // Record activity
             $this->model->recordActivity(Lang::txt('COM_PROJECTS_REPLACED_PROJECT_PICTURE'));
         }
     }
     echo json_encode(array('success' => true));
     return;
 }
Пример #2
0
 /**
  * Upload a file to the profile via AJAX
  *
  * @return     string
  */
 public function doajaxuploadTask()
 {
     Request::checkToken(['get', 'post']);
     //allowed extensions for uplaod
     $allowedExtensions = array('png', 'jpe', 'jpeg', 'jpg', 'gif');
     //max upload size
     $sizeLimit = $this->config->get('maxAllowed', '40000000');
     //get the file
     if (isset($_GET['qqfile'])) {
         $stream = true;
         $file = $_GET['qqfile'];
         $size = (int) $_SERVER["CONTENT_LENGTH"];
     } elseif (isset($_FILES['qqfile'])) {
         $stream = false;
         $file = $_FILES['qqfile']['name'];
         $size = (int) $_FILES['qqfile']['size'];
     } else {
         return;
     }
     //get the id and load profile
     $id = Request::getVar('id', 0);
     $profile = \Hubzero\User\Profile::getInstance($id);
     if (!$profile) {
         return;
     }
     //define upload directory and make sure its writable
     $uploadDirectory = PATH_APP . DS . $this->filespace() . DS . \Hubzero\Utility\String::pad($id) . DS;
     if (!is_dir($uploadDirectory)) {
         if (!Filesystem::makeDirectory($uploadDirectory)) {
             echo json_encode(array('error' => 'Server error. Unable to create upload directory.'));
             return;
         }
     }
     if (!is_writable($uploadDirectory)) {
         echo json_encode(array('error' => "Server error. Upload directory isn't writable."));
         return;
     }
     //check to make sure we have a file and its not too big
     if ($size == 0) {
         echo json_encode(array('error' => 'File is empty'));
         return;
     }
     if ($size > $sizeLimit) {
         $max = preg_replace('/<abbr \\w+=\\"\\w+\\">(\\w{1,3})<\\/abbr>/', '$1', \Hubzero\Utility\Number::formatBytes($sizeLimit));
         echo json_encode(array('error' => 'File is too large. Max file upload size is ' . $max));
         return;
     }
     //check to make sure we have an allowable extension
     $pathinfo = pathinfo($file);
     $filename = $pathinfo['filename'];
     $ext = $pathinfo['extension'];
     if ($allowedExtensions && !in_array(strtolower($ext), $allowedExtensions)) {
         $these = implode(', ', $allowedExtensions);
         echo json_encode(array('error' => 'File has an invalid extension, it should be one of ' . $these . '.'));
         return;
     }
     // don't overwrite previous files that were uploaded
     while (file_exists($uploadDirectory . $filename . '.' . $ext)) {
         $filename .= rand(10, 99);
     }
     $file = $uploadDirectory . $filename . '.' . $ext;
     $final_file = $uploadDirectory . 'profile.png';
     $final_thumb = $uploadDirectory . 'thumb.png';
     if ($stream) {
         //read the php input stream to upload file
         $input = fopen("php://input", "r");
         $temp = tmpfile();
         $realSize = stream_copy_to_stream($input, $temp);
         fclose($input);
         //move from temp location to target location which is user folder
         $target = fopen($file, "w");
         fseek($temp, 0, SEEK_SET);
         stream_copy_to_stream($temp, $target);
         fclose($target);
     } else {
         move_uploaded_file($_FILES['qqfile']['tmp_name'], $file);
     }
     //resize image to max 400px and rotate in case user didnt before uploading
     $hi = new \Hubzero\Image\Processor($file);
     if (count($hi->getErrors()) == 0) {
         $hi->autoRotate();
         $hi->resize(400);
         $hi->setImageType(IMAGETYPE_PNG);
         $hi->save($final_file);
     } else {
         echo json_encode(array('error' => $hi->getError()));
         return;
     }
     // create thumb
     $hi = new \Hubzero\Image\Processor($final_file);
     if (count($hi->getErrors()) == 0) {
         $hi->resize(50, false, true, true);
         $hi->save($final_thumb);
     } else {
         echo json_encode(array('error' => $hi->getError()));
         return;
     }
     // remove orig
     unlink($file);
     echo json_encode(array('success' => true, 'file' => str_replace($uploadDirectory, '', $final_file), 'directory' => str_replace(PATH_ROOT, '', $uploadDirectory)));
 }