示例#1
0
 /**
  * Method to return session screenshot
  *
  * @apiMethod GET
  * @apiUri    /tools/{sessionid}/screenshot
  * @apiParameter {
  * 		"name":          "sessionid",
  * 		"description":   "Tool session identifier",
  * 		"type":          "integer",
  * 		"required":      true,
  * 		"default":       0
  * }
  * @apiParameter {
  * 		"name":          "type",
  * 		"description":   "Image format",
  * 		"type":          "string",
  * 		"required":      false,
  * 		"default":       "png",
  * 		"allowedValues": "png, jpg, jpeg, gif"
  * }
  * @apiParameter {
  * 		"name":          "notfound",
  * 		"description":   "Not found",
  * 		"type":          "integer",
  * 		"required":      false,
  * 		"default":       0
  * }
  * @return     void
  */
 public function screenshotTask()
 {
     //$this->requiresAuthentication();
     require_once dirname(dirname(__DIR__)) . DS . 'tables' . DS . 'session.php';
     require_once dirname(dirname(__DIR__)) . DS . 'tables' . DS . 'viewperm.php';
     //instantiate middleware database object
     $mwdb = \Components\Tools\Helpers\Utils::getMWDBO();
     //get any request vars
     $type = Request::getVar('type', 'png');
     $sessionid = Request::getVar('sessionid', '');
     $notFound = Request::getVar('notfound', 0);
     $image_type = IMAGETYPE_PNG;
     if ($type == 'jpeg' || $type == 'jpg') {
         $image_type = IMAGETYPE_JPEG;
     } else {
         if ($type == 'gif') {
             $image_type = IMAGETYPE_GIF;
         }
     }
     //check to make sure we have a valid sessionid
     if ($sessionid == '' || !is_numeric($sessionid)) {
         throw new Exception(Lang::txt('No session ID Specified.'), 401);
     }
     $screenshot = '';
     //load session
     $ms = new \Components\Tools\Tables\Session($mwdb);
     $sess = $ms->loadSession($sessionid);
     if (!$sess) {
         if ($notFound) {
             $screenshot = dirname(dirname(__DIR__)) . DS . 'site' . DS . 'assets' . DS . 'img' . DS . 'screenshot-notfound.png';
         } else {
             throw new Exception(Lang::txt('Session not found.'), 404);
         }
     }
     if (!$screenshot) {
         //check to make sure we have a sessions dir
         $home_directory = DS . 'webdav' . DS . 'home' . DS . strtolower($sess->username) . DS . 'data' . DS . 'sessions';
         if (!is_dir($home_directory)) {
             clearstatcache();
             if (!is_dir($home_directory)) {
                 throw new Exception(Lang::txt('Unable to find users sessions directory: %s', $home_directory), 404);
             }
         }
         //check to make sure we have an active session with the ID supplied
         $home_directory .= DS . $sessionid . '{,L,D}';
         $directories = glob($home_directory, GLOB_BRACE);
         if (empty($directories)) {
             throw new Exception(Lang::txt('No Session directory with the ID: %s', $sessionid), 404);
         } else {
             $home_directory = $directories[0];
         }
         // check to make sure we have a screenshot
         $screenshot = $home_directory . DS . 'screenshot.png';
         if (!file_exists($screenshot)) {
             if ($notFound) {
                 $screenshot = dirname(dirname(__DIR__)) . DS . 'site' . DS . 'assets' . DS . 'img' . DS . 'screenshot-notfound.png';
             } else {
                 throw new Exception(Lang::txt('No screenshot Found.'), 404);
             }
         }
     }
     // Load image and serve up
     $image = new \Hubzero\Image\Processor($screenshot);
     $image->setImageType($image_type);
     $image->display();
     exit;
 }