/**
  * Executes the command logic with the specified RPC parameters.
  *
  * @param Object $params Command parameters sent from client.
  * @return Object Result object to be passed back to client.
  */
 public function execute($params)
 {
     $file = MOXMAN::getFile($params->path);
     $url = parse_url($params->url);
     $config = $file->getConfig();
     if ($config->get('general.demo')) {
         throw new MOXMAN_Exception("This action is restricted in demo mode.", MOXMAN_Exception::DEMO_MODE);
     }
     if ($file->exists()) {
         throw new MOXMAN_Exception("To file already exist: " . $file->getPublicPath(), MOXMAN_Exception::FILE_EXISTS);
     }
     if (!$file->canWrite()) {
         throw new MOXMAN_Exception("No write access to file: " . $file->getPublicPath(), MOXMAN_Exception::NO_WRITE_ACCESS);
     }
     $filter = MOXMAN_Vfs_CombinedFileFilter::createFromConfig($config, "upload");
     if (!$filter->accept($file, true)) {
         throw new MOXMAN_Exception("Invalid file name for: " . $file->getPublicPath(), MOXMAN_Exception::INVALID_FILE_NAME);
     }
     $port = "";
     if (isset($url["port"])) {
         $port = ":" . $url["port"];
     }
     $query = "";
     if (isset($url["query"])) {
         $query = "?" . $url["query"];
     }
     $path = $url["path"] . $query;
     $host = $url["scheme"] . "://" . $url["host"] . $port;
     $httpClient = new MOXMAN_Http_HttpClient($host);
     $request = $httpClient->createRequest($path);
     $response = $request->send();
     // Handle redirects
     $location = $response->getHeader("location");
     if ($location) {
         $httpClient->close();
         $httpClient = new MOXMAN_Http_HttpClient($location);
         $request = $httpClient->createRequest($location);
         $response = $request->send();
     }
     // Read file into ram
     // TODO: This should not happen if we know the file size
     $content = "";
     while (($chunk = $response->read()) != "") {
         $content .= $chunk;
     }
     $httpClient->close();
     // Fire before file action add event
     $args = $this->fireBeforeFileAction("add", $file, strlen($content));
     $file = $args->getFile();
     $stream = $file->open(MOXMAN_Vfs_IFileStream::WRITE);
     $stream->write($content);
     $stream->close();
     $args = new MOXMAN_Vfs_FileActionEventArgs("add", $file);
     MOXMAN::getPluginManager()->get("core")->fire("FileAction", $args);
     return parent::fileToJson($file, true);
 }
 private function getUrlContent($url, $config)
 {
     $url = parse_url($url);
     $port = "";
     if (isset($url["port"])) {
         $port = ":" . $url["port"];
     }
     $query = "";
     if (isset($url["query"])) {
         $query = "?" . $url["query"];
     }
     $path = $url["path"] . $query;
     $host = $url["scheme"] . "://" . $url["host"] . $port;
     $httpClient = new MOXMAN_Http_HttpClient($host);
     $httpClient->setProxy($config->get("general.http_proxy"));
     $request = $httpClient->createRequest($path);
     $response = $request->send();
     // Handle redirects
     $location = $response->getHeader("location");
     if ($location) {
         $httpClient->close();
         $httpClient = new MOXMAN_Http_HttpClient($location);
         $request = $httpClient->createRequest($location);
         $response = $request->send();
     }
     // Read file into ram
     // TODO: This should not happen if we know the file size
     $content = "";
     while (($chunk = $response->read()) != "") {
         $content .= $chunk;
     }
     $httpClient->close();
     return $content;
 }
Exemple #3
0
 /**
  * Validate token
  * @param  string $token Token should be gotten by $_GET["code"] from redirect.
  */
 public function validate($token)
 {
     $this->token = $token;
     $urlParts = parse_url($this->token_url);
     $path = $urlParts["path"];
     $parameters = array();
     $parameters["grant_type"] = "authorization_code";
     $parameters["code"] = $token;
     $parameters["client_id"] = $this->client_id;
     $parameters["client_secret"] = $this->client_secret;
     $parameters["redirect_uri"] = $this->callback;
     $client = new MOXMAN_Http_HttpClient($urlParts["scheme"] . "://" . $urlParts["host"]);
     $client->setLogLevel(0);
     $request = $client->createRequest($path, "POST");
     $response = $request->send($parameters);
     $body = $response->getBody();
     $client->close();
     $data = json_decode($body);
     $this->refresh_token = $data->refresh_token;
     $this->token = $data->access_token;
     $this->expires = $data->expires_in;
     $this->id_token = $data->id_token;
     return $data;
 }