/**
  * Download a file
  * Runs through various permissions checks to ensure user has access
  *
  * @return     void
  */
 public function downloadTask()
 {
     // Incoming
     $id = Request::getInt('id', 0);
     $alias = Request::getVar('alias', '');
     $d = Request::getVar('d', 'inline');
     //make sure we have a proper disposition
     if ($d != "inline" && $d != "attachment") {
         $d = "inline";
     }
     // Load the resource
     $resource = new Resource($this->database);
     if ($alias && !$resource->loadAlias($alias)) {
         App::abort(404, Lang::txt('COM_RESOURCES_RESOURCE_NOT_FOUND'));
         return;
     } elseif (substr($id, 0, 4) == '9999') {
         $resource->id = $id;
         $resource->standalone = 1;
         $resource->path = null;
         $resource->created = Date::of('now')->format('Y-m-d 00:00:00');
     } elseif (!$resource->load($id)) {
         App::abort(404, Lang::txt('COM_RESOURCES_RESOURCE_NOT_FOUND'));
         return;
     }
     // Check if the resource is for logged-in users only and the user is logged-in
     if ($token = Request::getVar('token', '', 'get')) {
         $token = base64_decode($token);
         $key = App::hash(@$_SERVER['HTTP_USER_AGENT']);
         $crypter = new \Hubzero\Encryption\Encrypter(new \Hubzero\Encryption\Cipher\Simple(), new \Hubzero\Encryption\Key('simple', $key, $key));
         $session_id = $crypter->decrypt($token);
         $session = \Hubzero\Session\Helper::getSession($session_id);
         $user = User::getInstance($session->userid);
         $user->guest = 0;
         $user->id = $session->userid;
         $user->usertype = $session->usertype;
     } else {
         $user = User::getRoot();
     }
     if ($resource->access == 1 && $user->get('guest')) {
         App::abort(403, Lang::txt('COM_RESOURCES_ALERTNOTAUTH'));
         return;
     }
     // Check if the resource is "private" and the user is allowed to view it
     if ($resource->access == 4 || $resource->access == 3 || !$resource->standalone) {
         if ($this->checkGroupAccess($resource, $user)) {
             App::abort(403, Lang::txt('COM_RESOURCES_ALERTNOTAUTH'));
             return;
         }
     }
     if ($resource->standalone && !$resource->path) {
         $resource->path = DS . trim($this->config->get('uploadpath', '/site/resources'), DS) . Html::build_path($resource->created, $resource->id, '') . DS . 'media' . DS . Request::getVar('file');
     }
     $resource->path = trim($resource->path);
     // Ensure we have a path
     // Ensure resource is published - stemedhub #472
     if (empty($resource->path) && $resource->published != 1) {
         App::abort(404, Lang::txt('COM_RESOURCES_FILE_NOT_FOUND'));
         return;
     }
     // Get the configured upload path
     $base_path = $this->config->get('uploadpath', '/site/resources');
     if ($base_path) {
         $base_path = DS . trim($base_path, DS);
     }
     // Does the path start with a slash?
     if (substr($resource->path, 0, 1) != DS) {
         $resource->path = DS . $resource->path;
         // Does the beginning of the $resource->path match the config path?
         if (substr($resource->path, 0, strlen($base_path)) == $base_path) {
             // Yes - this means the full path got saved at some point
         } else {
             // No - append it
             $resource->path = $base_path . $resource->path;
         }
     }
     // Add root path
     $filename = PATH_APP . $resource->path;
     // Ensure the file exist
     if (!file_exists($filename)) {
         App::abort(404, Lang::txt('COM_RESOURCES_FILE_NOT_FOUND') . ' ' . $filename);
         return;
     }
     $ext = strtolower(\Filesystem::extension($filename));
     if (!in_array($ext, array('jpg', 'jpeg', 'jpe', 'gif', 'png', 'pdf', 'htm', 'html', 'txt', 'json', 'xml'))) {
         $d = 'attachment';
     }
     // Initiate a new content server and serve up the file
     $xserver = new \Hubzero\Content\Server();
     $xserver->filename($filename);
     $xserver->disposition($d);
     $xserver->acceptranges(false);
     // @TODO fix byte range support
     if (!$xserver->serve()) {
         // Should only get here on error
         throw new Exception(Lang::txt('COM_RESOURCES_SERVER_ERROR'), 500);
     } else {
         exit;
     }
     return;
 }
 /**
  * Generate a Windows tool invoke URL to redirect to
  *
  * @param   string  $option  Name of the component
  * @return  void
  */
 public function invoke($option)
 {
     $no_html = Request::getInt('no_html', 0);
     $response = new StdClass();
     $response->success = false;
     $response->message = Lang::txt('No invoke URL found.');
     // Check for an imconing token.
     if ($token = Request::getVar('token', '', 'get')) {
         $dtoken = base64_decode($token);
         $key = App::hash(@$_SERVER['HTTP_USER_AGENT']);
         $crypter = new \Hubzero\Encryption\Encrypter(new \Hubzero\Encryption\Cipher\Simple(), new \Hubzero\Encryption\Key('simple', $key, $key));
         $session_id = $crypter->decrypt($dtoken);
         $session = \Hubzero\Session\Helper::getSession($session_id);
         $user = User::getInstance($session->userid);
         $user->set('guest', 0);
         $user->set('id', $session->userid);
         $user->set('username', $session->username);
         $ip = $session->ip;
     } else {
         $user = User::getInstance();
         $ip = Request::ip();
     }
     // Is the user validated?
     if ($user->isGuest()) {
         $response->message = Lang::txt('Login is required to perform this action.');
     } else {
         $appid = Request::getVar('appid');
         // Generate the URL
         $url = $this->generateInvokeUrl($option, $appid, $user, $ip);
         if ($url) {
             if (!$token) {
                 $session = App::get('session');
                 $session_id = $session->getId();
                 $key = App::hash(@$_SERVER['HTTP_USER_AGENT']);
                 $crypter = new \Hubzero\Encryption\Encrypter(new \Hubzero\Encryption\Cipher\Simple(), new \Hubzero\Encryption\Key('simple', $key, $key));
                 $token = base64_encode($crypter->encrypt($session_id));
             }
             $rurl = rtrim($this->params->get('invoke_url', 'http://wapps.hubzero.org'), '/') . '/v1?';
             //standaloneUrl=' . $url;
             $params = array();
             $params[] = 'token=' . $token;
             if ($appid) {
                 $params[] = 'appid=' . $appid;
             }
             $params[] = 'standaloneUrl=' . $url;
             $rurl .= implode('&', $params);
             $response->success = true;
             $response->message = $rurl;
             if (!$no_html) {
                 $this->view('invoke', 'display')->set('url', $rurl)->set('rurl', $_SERVER['HTTP_REFERER'])->display();
                 exit;
                 App::redirect($url);
             }
         }
     }
     if (!$no_html) {
         App::abort(404, Lang::txt('No invoke URL found.'));
     }
     $response = json_encode($response);
     if ($callback = Request::getVar('callback')) {
         $response = $callback . '(' . $response . ')';
     }
     echo $response;
     exit;
 }