Esempio n. 1
0
 /**
  * Throws a internal runtime error as a new exception.
  *
  * @param int $errno Error number.
  * @param string $errstr Error string.
  * @param string $errfile Error file.
  * @param int $errline Error line.
  */
 public static function throwRuntimeError($errno, $errstr, $errfile, $errline)
 {
     $exception = new MOXMAN_Exception($errstr, $errno);
     $exception->setFile($errfile);
     $exception->setLine($errline);
     throw $exception;
 }
 /**
  * Wrap Moxiemanager's api.php in a controller action.
  *
  * @return void
  */
 public function api()
 {
     try {
         $pluginPath = Plugin::path('CkTools');
         define('MOXMAN_CLASSES', $pluginPath . 'src/Lib/moxiemanager/classes');
         define('MOXMAN_PLUGINS', $pluginPath . 'src/Lib/moxiemanager/plugins');
         define('MOXMAN_ROOT', $pluginPath . 'src/Lib/moxiemanager');
         define('MOXMAN_API_FILE', __FILE__);
         $appConfig = Configure::read('CkTools.moxiemanager');
         Configure::load('CkTools.moxiemanager');
         $moxieManagerConfig = Configure::read('moxiemanager');
         if (is_array($appConfig)) {
             $moxieManagerConfig = Hash::merge($moxieManagerConfig, $appConfig);
         }
         $GLOBALS['moxieManagerConfig'] = $moxieManagerConfig;
         require_once MOXMAN_CLASSES . '/MOXMAN.php';
         $context = \MOXMAN_Http_Context::getCurrent();
         $pluginManager = \MOXMAN::getPluginManager();
         foreach ($pluginManager->getAll() as $plugin) {
             if ($plugin instanceof \MOXMAN_Http_IHandler) {
                 $plugin->processRequest($context);
             }
         }
     } catch (Exception $e) {
         \MOXMAN_Exception::printException($e);
     }
     return $this->render(false, false);
 }
 /**
  * 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));
     }
 }
Esempio n. 4
0
 /**
  * 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)
 {
     $tempFilePath = null;
     $chunkFilePath = null;
     $request = $httpContext->getRequest();
     $response = $httpContext->getResponse();
     try {
         // Check if the user is authenticated or not
         if (!MOXMAN::getAuthManager()->isAuthenticated()) {
             if (!isset($json->method) || !preg_match('/^(login|logout)$/', $json->method)) {
                 $exception = new MOXMAN_Exception("Access denied by authenticator(s).", 10);
                 $exception->setData(array("login_url" => MOXMAN::getConfig()->get("authenticator.login_page")));
                 throw $exception;
             }
         }
         $file = MOXMAN::getFile($request->get("path"));
         $config = $file->getConfig();
         if ($config->get('general.demo')) {
             throw new MOXMAN_Exception("This action is restricted in demo mode.", MOXMAN_Exception::DEMO_MODE);
         }
         $maxSizeBytes = preg_replace("/[^0-9.]/", "", $config->get("upload.maxsize"));
         if (strpos(strtolower($config->get("upload.maxsize")), "k") > 0) {
             $maxSizeBytes = round(floatval($maxSizeBytes) * 1024);
         }
         if (strpos(strtolower($config->get("upload.maxsize")), "m") > 0) {
             $maxSizeBytes = round(floatval($maxSizeBytes) * 1024 * 1024);
         }
         function generateRandomString($length = 10)
         {
             $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
             $charactersLength = strlen($characters);
             $randomString = '';
             for ($i = 0; $i < $length; $i++) {
                 $randomString .= $characters[rand(0, $charactersLength - 1)];
             }
             return $randomString;
         }
         $filename = generateRandomString() . '.' . MOXMAN_Util_PathUtils::getExtension($request->get("name"));
         $id = $request->get("id");
         $loaded = intval($request->get("loaded", "0"));
         $total = intval($request->get("total", "-1"));
         $file = MOXMAN::getFile($file->getPath(), $filename);
         // Generate unique id for first chunk
         // TODO: We should cleanup orphan ID:s if upload fails etc
         if ($loaded == 0) {
             $id = uniqid();
         }
         // Setup path to temp file based on id
         $tempFilePath = MOXMAN_Util_PathUtils::combine(MOXMAN_Util_PathUtils::getTempDir(), "mcupload_" . $id . "." . MOXMAN_Util_PathUtils::getExtension($file->getName()));
         $chunkFilePath = MOXMAN_Util_PathUtils::combine(MOXMAN_Util_PathUtils::getTempDir(), "mcupload_chunk_" . $id . "." . MOXMAN_Util_PathUtils::getExtension($file->getName()));
         if (!$file->canWrite()) {
             throw new MOXMAN_Exception("No write access to path: " . $file->getPublicPath(), MOXMAN_Exception::NO_WRITE_ACCESS);
         }
         if ($total > $maxSizeBytes) {
             throw new MOXMAN_Exception("File size to large: " . $file->getPublicPath(), MOXMAN_Exception::FILE_SIZE_TO_LARGE);
         }
         // Operations on first chunk
         if ($loaded == 0) {
             // Fire before file action add event
             $args = new MOXMAN_Core_FileActionEventArgs("add", $file);
             $args->getData()->fileSize = $total;
             MOXMAN::getPluginManager()->get("core")->fire("BeforeFileAction", $args);
             $file = $args->getFile();
             if ($file->exists()) {
                 if (!$config->get("upload.overwrite") && !$request->get("overwrite")) {
                     throw new MOXMAN_Exception("Target file exists: " . $file->getPublicPath(), MOXMAN_Exception::FILE_EXISTS);
                 } else {
                     MOXMAN::getPluginManager()->get("core")->deleteThumbnail($file);
                     $file->delete();
                 }
             }
             $filter = MOXMAN_Vfs_CombinedFileFilter::createFromConfig($config, "upload");
             if ($filter->accept($file) !== MOXMAN_Vfs_CombinedFileFilter::ACCEPTED) {
                 throw new MOXMAN_Exception("Invalid file name for: " . $file->getPublicPath(), MOXMAN_Exception::INVALID_FILE_NAME);
             }
         }
         $blobSize = 0;
         $inputFile = $request->getFile("file");
         if (!$inputFile) {
             throw new MOXMAN_Exception("No input file specified.");
         }
         if ($loaded === 0) {
             // Check if we should mock or not
             if (defined('PHPUNIT')) {
                 if (!copy($inputFile['tmp_name'], $tempFilePath)) {
                     throw new MOXMAN_Exception("Could not move the uploaded temp file.");
                 }
             } else {
                 if (!move_uploaded_file($inputFile['tmp_name'], $tempFilePath)) {
                     throw new MOXMAN_Exception("Could not move the uploaded temp file.");
                 }
             }
             $blobSize = filesize($tempFilePath);
         } else {
             // Check if we should mock or not
             if (defined('PHPUNIT')) {
                 if (!copy($inputFile['tmp_name'], $chunkFilePath)) {
                     throw new MOXMAN_Exception("Could not move the uploaded temp file.");
                 }
             } else {
                 if (!move_uploaded_file($inputFile['tmp_name'], $chunkFilePath)) {
                     throw new MOXMAN_Exception("Could not move the uploaded temp file.");
                 }
             }
             $in = fopen($chunkFilePath, 'r');
             if ($in) {
                 $out = fopen($tempFilePath, 'a');
                 if ($out) {
                     while ($buff = fread($in, 8192)) {
                         $blobSize += strlen($buff);
                         fwrite($out, $buff);
                     }
                     fclose($out);
                 }
                 fclose($in);
             }
             unlink($chunkFilePath);
         }
         // Import file when all chunks are complete
         if ($total == -1 || $loaded + $blobSize == $total) {
             clearstatcache();
             // Check if file is valid on last chunk we also check on first chunk but not in the onces in between
             $filter = MOXMAN_Vfs_CombinedFileFilter::createFromConfig($config, "upload");
             if ($filter->accept($file) !== MOXMAN_Vfs_CombinedFileFilter::ACCEPTED) {
                 throw new MOXMAN_Exception("Invalid file name for: " . $file->getPublicPath(), MOXMAN_Exception::INVALID_FILE_NAME);
             }
             // Resize the temporary blob
             if ($config->get("upload.autoresize") && preg_match('/gif|jpe?g|png/i', MOXMAN_Util_PathUtils::getExtension($tempFilePath)) === 1) {
                 $size = getimagesize($tempFilePath);
                 $maxWidth = $config->get('upload.max_width');
                 $maxHeight = $config->get('upload.max_height');
                 if ($size[0] > $maxWidth || $size[1] > $maxHeight) {
                     $imageAlter = new MOXMAN_Media_ImageAlter();
                     $imageAlter->load($tempFilePath);
                     $imageAlter->resize($maxWidth, $maxHeight, true);
                     $imageAlter->save($tempFilePath, $config->get("upload.autoresize_jpeg_quality"));
                 }
             }
             // Create thumbnail and upload then import local blob
             MOXMAN::getPluginManager()->get("core")->createThumbnail($file, $tempFilePath);
             $file->importFrom($tempFilePath);
             unlink($tempFilePath);
             $args = new MOXMAN_Core_FileActionEventArgs("add", $file);
             MOXMAN::getPluginManager()->get("core")->fire("FileAction", $args);
             // In case file is modified
             $file = $args->getFile();
             $result = MOXMAN_Core_Plugin::fileToJson($file, true);
         } else {
             $result = $id;
         }
         $response->sendJson(array("jsonrpc" => "2.0", "result" => $result, "id" => null));
     } catch (Exception $e) {
         if ($tempFilePath && file_exists($tempFilePath)) {
             unlink($tempFilePath);
         }
         if ($chunkFilePath && file_exists($chunkFilePath)) {
             unlink($chunkFilePath);
         }
         MOXMAN::dispose();
         // Closes any open file systems/connections
         $message = $e->getMessage();
         $data = null;
         // Add file and line number when running in debug mode
         // @codeCoverageIgnoreStart
         if (MOXMAN::getConfig()->get("general.debug")) {
             $message .= " " . $e->getFile() . " (" . $e->getLine() . ")";
         }
         // @codeCoverageIgnoreEnd
         // Grab the data from the exception
         if ($e instanceof MOXMAN_Exception && !$data) {
             $data = $e->getData();
         }
         // Json encode error response
         $response->sendJson((object) array("jsonrpc" => "2.0", "error" => array("code" => $e->getCode(), "message" => $message, "data" => $data), "id" => null));
     }
 }
Esempio n. 5
0
<?php

/**
 * api.php
 *
 * Copyright 2003-2014, Moxiecode Systems AB, All rights reserved.
 */
try {
    require_once './classes/MOXMAN.php';
    define("MOXMAN_API_FILE", __FILE__);
    $context = MOXMAN_Http_Context::getCurrent();
    $pluginManager = MOXMAN::getPluginManager();
    foreach ($pluginManager->getAll() as $plugin) {
        if ($plugin instanceof MOXMAN_Http_IHandler) {
            $plugin->processRequest($context);
        }
    }
} catch (Exception $e) {
    MOXMAN_Exception::printException($e);
}
Esempio n. 6
0
if (!defined('MOXMAN_PLUGINS')) {
    /**
     * Path to the plugins directory.
     *
     * @package MOXMAN
     */
    define('MOXMAN_PLUGINS', MOXMAN_ROOT . '/plugins');
}
// Load default config
if (!isset($moxieManagerConfig)) {
    $moxieManagerConfig = array();
    require_once MOXMAN_ROOT . '/config.php';
}
require_once MOXMAN_CLASSES . '/AutoLoader.php';
MOXMAN_AutoLoader::register();
MOXMAN_Exception::registerErrorHandler();
/**
 * MoxieManager factory instance.
 *
 * @package MOXMAN
 */
class MOXMAN
{
    /** @ignore */
    private static $logger, $pdo;
    /** @ignore */
    private static $fileSystemManager, $config, $pluginManager, $authManager, $storageManager;
    // @codeCoverageIgnoreStart
    /**
     * Returns the file system manager instance.
     *