Exemplo n.º 1
0
 /**
  * Sends the specified object as JSON.
  *
  * @param Mixed $obj Json data to send.
  */
 public function sendJson($obj)
 {
     $this->setHeader('Content-Type', 'application/json');
     $obj = MOXMAN_Util_Json::encode($obj);
     // IE 7 treats application/json download but in case some proxy/server
     // would ignore that content-type and deliver it as HTML trim a few things
     $obj = strtr($obj, array('<' => '\\u003c', '>' => '\\u003e'));
     $this->sendContent($obj);
 }
Exemplo n.º 2
0
 public function listFilesFiltered(MOXMAN_Vfs_IFileFilter $filter)
 {
     $entries = MOXMAN_Util_Json::decode(MOXMAN::getUserStorage()->get("uploaded.files", "[]"));
     $files = array();
     foreach ($entries as $entry) {
         $file = new MOXMAN_Uploaded_File($this->fileSystem, $entry->path, $entry);
         if ($filter->accept($file) == MOXMAN_Vfs_IFileFilter::ACCEPTED) {
             $files[] = $file;
         }
     }
     return $files;
 }
Exemplo n.º 3
0
 public function listFilesFiltered(MOXMAN_Vfs_IFileFilter $filter)
 {
     $files = array();
     if ($this->isDirectory()) {
         $fileSystem = $this->getFileSystem();
         $entries = MOXMAN_Util_Json::decode(MOXMAN::getUserStorage()->get("uploaded.files", "[]"));
         foreach ($entries as $entry) {
             $file = new MOXMAN_Favorites_File($fileSystem, $entry->path, $entry);
             if ($filter->accept($file)) {
                 $files[] = $file;
             }
         }
     }
     return new MOXMAN_Vfs_FileList($files);
 }
Exemplo n.º 4
0
 public function remove($params)
 {
     if (isset($params->paths) && is_array($params->paths)) {
         $paths = $params->paths;
         $files = MOXMAN_Util_Json::decode(MOXMAN::getUserStorage()->get("uploaded.files", "[]"));
         // Remove existing paths
         for ($i = 0; $i < count($files); $i++) {
             foreach ($paths as $path) {
                 if ($files[$i]->path == $path) {
                     array_splice($files, $i, 1);
                 }
             }
         }
         MOXMAN::getUserStorage()->put("uploaded.files", MOXMAN_Util_Json::encode($files));
     }
     return true;
 }
Exemplo n.º 5
0
 public function listFilesFiltered(MOXMAN_Vfs_IFileFilter $filter)
 {
     $files = array();
     if ($this->isDirectory()) {
         $fileSystem = $this->getFileSystem();
         $entries = MOXMAN_Util_Json::decode(MOXMAN::getUserStorage()->get("history.files", "[]"));
         $index = 0;
         foreach ($entries as $entry) {
             $file = new MOXMAN_History_File($fileSystem, $entry->path, $entry);
             if ($filter->accept($file)) {
                 $entry->name = basename($entry->path) . "_\$\$[" . $index++ . "]";
                 $files[] = $file;
             }
         }
     }
     return new MOXMAN_Vfs_FileList($files);
 }
Exemplo n.º 6
0
 public function remove($params)
 {
     if (isset($params->paths) && is_array($params->paths)) {
         $paths = $params->paths;
         $files = MOXMAN_Util_Json::decode(MOXMAN::getUserStorage()->get("favorites.files", "[]"));
         foreach ($paths as $path) {
             for ($i = count($files) - 1; $i >= 0; $i--) {
                 if ($files[$i]->path == $path) {
                     array_splice($files, $i, 1);
                     $i--;
                 }
             }
         }
         MOXMAN::getUserStorage()->put("favorites.files", MOXMAN_Util_Json::encode($files));
     }
     return true;
 }
Exemplo n.º 7
0
 public function remove($params)
 {
     if (MOXMAN::getConfig()->get('general.demo')) {
         throw new MOXMAN_Exception("This action is restricted in demo mode.", MOXMAN_Exception::DEMO_MODE);
     }
     if (isset($params->paths) && is_array($params->paths)) {
         $paths = $params->paths;
         $files = MOXMAN_Util_Json::decode(MOXMAN::getUserStorage()->get("history.files", "[]"));
         for ($i = count($files) - 1; $i >= 0; $i--) {
             foreach ($paths as $path) {
                 if ($files[$i]->path == $path) {
                     array_splice($files, $i, 1);
                     $i--;
                 }
             }
         }
         MOXMAN::getUserStorage()->put("history.files", MOXMAN_Util_Json::encode($files));
     }
     return true;
 }
 /**
  * Disposes the instance.
  */
 public function dispose()
 {
     foreach ($this->metaFileCache as $filePath => $data) {
         $metaFile = $this->fileSystem->getFile($filePath);
         if (!empty($data)) {
             $stream = $metaFile->open("w");
             $stream->write(gzcompress(MOXMAN_Util_Json::encode($data), 9));
             $stream->close();
         } else {
             if ($metaFile->exists()) {
                 $metaFile->delete();
             }
         }
     }
 }
 /**
  * Process a request using the specified context.
  *
  * @param MOXMAN_Http_Context $httpContext Context instance to pass to use for the handler.
  */
 public function processRequest(MOXMAN_Http_Context $httpContext)
 {
     $request = $httpContext->getRequest();
     $response = $httpContext->getResponse();
     $response->disableCache();
     $response->setHeader('Content-type', 'application/json');
     @set_time_limit(5 * 60);
     // 5 minutes execution time
     $id = null;
     try {
         $json = MOXMAN_Util_Json::decode($request->get("json"));
         // Check if we should install
         if ($json && $json->method != "install") {
             $config = MOXMAN::getConfig()->getAll();
             if (empty($config) || !isset($config["general.license"])) {
                 $exception = new MOXMAN_Exception("Installation needed.", MOXMAN_Exception::NEEDS_INSTALLATION);
                 throw $exception;
             }
             if (!preg_match('/^([0-9A-Z]{4}\\-){7}[0-9A-Z]{4}$/', trim($config["general.license"]))) {
                 throw new MOXMAN_Exception("Invalid license: " . $config["general.license"]);
             }
         }
         // Check if the user is authenticated or not
         if (!MOXMAN::getAuthManager()->isAuthenticated()) {
             if (!isset($json->method) || !preg_match('/^(login|logout|install)$/', $json->method)) {
                 $exception = new MOXMAN_Exception("Access denied by authenticator(s).", MOXMAN_Exception::NO_ACCESS);
                 $exception->setData(array("login_url" => MOXMAN::getConfig()->get("authenticator.login_page")));
                 throw $exception;
             }
         }
         if ($json && isset($json->id) && isset($json->method) && isset($json->params)) {
             $id = $json->id;
             $params = $json->params;
             $result = null;
             if (isset($params->access)) {
                 MOXMAN::getAuthManager()->setClientAuthData($params->access);
             }
             $plugins = MOXMAN::getPluginManager()->getAll();
             foreach ($plugins as $plugin) {
                 if ($plugin instanceof MOXMAN_ICommandHandler) {
                     $result = $plugin->execute($json->method, $json->params);
                     if ($result !== null) {
                         break;
                     }
                 }
             }
             if ($result === null) {
                 throw new Exception("Method not found: " . $json->method, -32601);
             }
             $response->sendJson((object) array("jsonrpc" => "2.0", "result" => $result, "id" => $id));
         } else {
             throw new Exception("Invalid Request.", -32600);
         }
         MOXMAN::dispose();
     } catch (Exception $e) {
         MOXMAN::dispose();
         $message = $e->getMessage();
         $data = null;
         if (MOXMAN::getConfig()->get("general.debug")) {
             $message .= "\n\nStacktrace:\n";
             $trace = $e->getTrace();
             array_shift($trace);
             $message .= $e->getFile() . ":" . $e->getLine() . "\n";
             foreach ($trace as $item) {
                 if (isset($item["file"]) && isset($item["line"])) {
                     $message .= $item["file"] . ":" . $item["line"] . "\n";
                 }
             }
         }
         if ($e instanceof MOXMAN_Exception && !$data) {
             $data = $e->getData();
         }
         $response->sendJson((object) array("jsonrpc" => "2.0", "error" => array("code" => $e->getCode(), "message" => $message, "data" => $data), "id" => $id));
     }
 }
Exemplo n.º 10
0
 /** @ignore */
 private function save()
 {
     if (!is_writable(dirname($this->storagePath))) {
         return $this;
     }
     $json = MOXMAN_Util_Json::encode($this->data, $this->config->get("general.debug"));
     // If there is no data to store remove the storage file
     if ($json === "{}" && file_exists($this->storagePath)) {
         unlink($this->storagePath);
     } else {
         file_put_contents($this->storagePath, $json);
     }
     return $this;
 }
Exemplo n.º 11
0
 /**
  * Sends the specified object as JSON.
  *
  * @param Mixed $obj Json data to send.
  */
 public function sendJson($obj)
 {
     $this->sendContent(MOXMAN_Util_Json::encode($obj));
 }