/** * Alias for xapp_set_option * * @param null $key * @param null $value * @param null $mixed * @param bool $reset * @return mixed|null */ function xo_set($key = null, $value = null, &$mixed = null, $reset = false) { if ($mixed == null) { $mixed = XApp_Options_Utils::optionCallee(); } return xapp_set_option($key, $value, $mixed, $reset); }
/** * init class instance by validating cache directory for being readable and writable * * @error 16202 * @return void * @throws Xapp_Cache_Driver_Exception */ protected function init() { try { $dir = new SplFileInfo(xapp_get_option(self::PATH, $this)); if (!$dir->isReadable()) { throw new Xapp_Cache_Driver_Exception(_("cache directory is not readable"), 1620201); } if (!$dir->isWritable()) { throw new Xapp_Cache_Driver_Exception(_("cache directory is not writable"), 1620202); } xapp_set_option(self::PATH, rtrim($dir->getRealPath(), DS) . DS, $this); } catch (Exception $e) { throw new Xapp_Cache_Driver_Exception(xapp_sprintf(_("cache directory file info error: %d, %s"), $e->getCode(), $e->getMessage()), 1620203); } }
/** * executing requested service if found passing result from service invoking to response * or pass compile smd map to response if no service was called. if a callback was supplied * will wrap result into callback function * * @error 14706 * @return void */ protected function execute($call) { $get = $this->request()->getGet(); $response = $this->response(); try { $result = $this->invoke($call, $call[3]); $this->response()->skipHeader(); if (array_key_exists(xapp_get_option(self::CALLBACK, $this), $get)) { } else { $result = $response->encode($result); } $response->body($result); } catch (Exception $e) { if (xapp_is_option(self::EXCEPTION_CALLBACK, $this)) { $e = call_user_func_array(xapp_get_option(self::EXCEPTION_CALLBACK, $this), array(&$e, $this, $call)); if (!$e instanceof Exception) { if (xapp_get_option(self::COMPLY_TO_JSONRPC_1_2, $this)) { if (array_key_exists($e->getCode(), $this->codeMap)) { xapp_set_option(Xapp_Rpc_Response::STATUS_CODE, $this->codeMap[$e->getCode()], $response); } else { xapp_set_option(Xapp_Rpc_Response::STATUS_CODE, 500, $response); } } if (xapp_is_option(self::ERROR_CALLBACK, $this) && array_key_exists(xapp_get_option(self::ERROR_CALLBACK, $this), $get)) { $e = $get[xapp_get_option(self::ERROR_CALLBACK, $this)] . '(' . $response->encode($e) . ')'; } else { if (array_key_exists(xapp_get_option(self::CALLBACK, $this), $get)) { $e = $get[xapp_get_option(self::CALLBACK, $this)] . '(' . $response->encode($e) . ')'; } else { $e = $response->encode($e); } } $response->body($e); } } } }
private function setupRPC() { /*** * We support JSONP for all services */ $isJSONP = false; $hasJSONP = true; if ($hasJSONP) { $isJSONP = XApp_Service_Entry_Utils::isJSONP(); } $method = $_SERVER['REQUEST_METHOD']; if ($method === 'POST') { $hasJSONP = false; } /*** * Filtered methods */ $ignoredRPCMethods = array('load', 'getObject', 'init', 'setup', 'log', 'onBeforeCall', 'onAfterCall', 'dumpObject', 'applyFilter', 'getLastJSONError', 'cleanUrl', 'rootUrl', 'siteUrl', 'getXCOption', 'getIndexer', 'getIndexOptions', 'getIndexOptions', 'indexDocument', 'onBeforeSearch', 'toDSURL', 'searchTest'); if (xapp_get_option(self::IGNORED_RPC_METHODS, $this)) { $ignoredRPCMethods = array_merge(xapp_get_option(self::IGNORED_RPC_METHODS, $this), $ignoredRPCMethods); } elseif (xapp_has_option(self::AUTH_DELEGATE, $this)) { /*** * Additional security here, mark each service method which has not been authorized by the * auth delegate as ignored! */ $authDelegate = xapp_get_option(self::AUTH_DELEGATE, $this); if (method_exists($authDelegate, 'authorize')) { $xCommanderFunctionTable = XApp_Service_Entry_Utils::getXCommanderFuncTable(); foreach ($xCommanderFunctionTable as $key => $value) { if (!$authDelegate::authorize($value)) { array_push($ignoredRPCMethods, $value); } } } } $server = null; if ($hasJSONP && $isJSONP) { //Options for SMD based JSONP-RPC classes $opt = array(Xapp_Rpc_Smd::IGNORE_METHODS => $ignoredRPCMethods, Xapp_Rpc_Smd::IGNORE_PREFIXES => array('_', '__')); $smd = new Xapp_Rpc_Smd_Jsonp($opt); //Options for RPC server $opt = array(Xapp_Rpc_Server::ALLOW_FUNCTIONS => true, Xapp_Rpc_Server::APPLICATION_ERROR => false, Xapp_Rpc_Server::METHOD_AS_SERVICE => true, Xapp_Rpc_Server::DEBUG => XApp_Service_Entry_Utils::isDebug(), Xapp_Rpc_Server::SMD => $smd); $server = Xapp_Rpc::server(XApp_Service_Entry_Utils::isRaw() ? 'raw' : 'jsonp', $opt); } else { //Options for SMD based RPC classes $opt = array(Xapp_Rpc_Smd_Json::IGNORE_METHODS => $ignoredRPCMethods, Xapp_Rpc_Smd_Json::IGNORE_PREFIXES => array('_', '__'), Xapp_Rpc_Smd_Json::METHOD_TARGET => false, Xapp_Rpc_Smd_Json::SERVICE_OVER_GET => true, Xapp_Rpc_Smd_Json::TARGET => xapp_get_option(self::RPC_TARGET, $this)); $smd = new Xapp_Rpc_Smd_Json($opt); //Options for RPC server $opt = array(Xapp_Rpc_Server::ALLOW_FUNCTIONS => true, Xapp_Rpc_Server::APPLICATION_ERROR => false, Xapp_Rpc_Server::METHOD_AS_SERVICE => false, Xapp_Rpc_Server::ALLOW_BATCHED_REQUESTS => true, Xapp_Rpc_Server::SERVICE_OVER_GET => true, Xapp_Rpc_Server::DEBUG => XApp_Service_Entry_Utils::isDebug(), Xapp_Rpc_Server::VALIDATE => !XApp_Service_Entry_Utils::isUpload(), Xapp_Rpc_Server::SMD => $smd); $server = Xapp_Rpc::server('json', $opt); } if ($server) { xapp_set_option(self::RPC_SERVER, $server, $this); } }
public function setupService() { $flags = xapp_get_option(self::FLAGS, $this); /*** * The very basic paths */ if (!defined('XAPP_BASEDIR')) { define("XAPP_BASEDIR", xapp_get_option(self::BASEDIR, $this)); } if (!defined('XAPP_LIB')) { define("XAPP_LIB", XAPP_BASEDIR . DIRECTORY_SEPARATOR . "lib" . DIRECTORY_SEPARATOR); } /*** * Load utils */ if (!class_exists('XApp_Service_Entry_Utils')) { include_once XAPP_BASEDIR . 'XApp_Service_Entry_Utils.php'; } /*** * Get run-time configuration, there is 'debug' and 'release'. For both cases there are * different resources to load. */ $XAPP_RUN_TIME_CONFIGURATION = XApp_Service_Entry_Utils::getRunTimeConfiguration(); /*** * Load dependencies */ $this->loadDependencies(xapp_get_option(self::FLAGS, $this)); //some debugging tools XApp_Service_Entry_Utils::includeXAppDebugTools(); $plugins = null; $pluginResources = null; $xComPluginManager = null; $logger = null; $loggingFlags = xapp_has_option(self::LOGGING_FLAGS, $this) ? xapp_get_option(self::LOGGING_FLAGS, $this) : array(); /*** * Setup Logger */ if (in_array(XAPP_BOOTSTRAP_SETUP_LOGGER, $flags) && xapp_has_option(self::LOGGING_CONF, $this)) { $logger = $this->setupLogger(xapp_get_option(self::LOGGING_CONF)); xapp_set_option(self::LOGGER, $logger, $this); if (!function_exists('xp_log')) { function xp_log($message) { $bootstrap = XApp_Commander_Bootstrap::instance(); $log = xapp_get_option(XApp_Commander_Bootstrap::LOGGER, $bootstrap); $log->log($message); } } } else { if (!function_exists('xp_log')) { //fake logger function xp_log($message) { } } } /*** * Setup XApp-PHP */ if (in_array(XAPP_BOOTSTRAP_SETUP_XAPP, $flags)) { $this->setupXApp(xapp_has_option(self::XAPP_CONF, $this) ? xapp_get_option(self::XAPP_CONF, $this) : null); } /*** * Setup RPC Server */ if (in_array(XAPP_BOOTSTRAP_SETUP_RPC, $flags)) { $this->setupRPC(); } $storeService = null; /*** * Setup storage */ if (in_array(XAPP_BOOTSTRAP_SETUP_STORE, $flags) && xapp_has_option(self::STORE_CONF, $this)) { $storeService = $this->setupStore(xapp_get_option(self::STORE_CONF, $this)); //$this->testStore($storeService); xapp_set_option(self::STORE, $storeService, $this); } /*** * Prepare resource renderer */ if (in_array(XAPP_BOOTSTRAP_LOAD_CLIENT_RESOURCES, $flags)) { //clients resource config path $XAPP_RESOURCE_CONFIG_PATH = '' . xapp_get_option(self::APPDIR, $this) . DIRECTORY_SEPARATOR; $XAPP_RESOURCE_CONFIG = xapp_get_option(self::XAPP_RESOURCE_CONFIG, $this); if (strlen($XAPP_RESOURCE_CONFIG)) { if ($XAPP_RUN_TIME_CONFIGURATION === 'debug') { $XAPP_RESOURCE_CONFIG_PATH .= 'lib' . DIRECTORY_SEPARATOR . xapp_get_option(self::APP_NAME, $this) . DIRECTORY_SEPARATOR . $XAPP_RESOURCE_CONFIG . xapp_get_option(self::RESOURCE_CONFIG_SUFFIX, $this) . '.json'; } else { if ($XAPP_RUN_TIME_CONFIGURATION === 'release') { $XAPP_RESOURCE_CONFIG_PATH .= DIRECTORY_SEPARATOR . xapp_get_option(self::APP_FOLDER, $this) . DIRECTORY_SEPARATOR . xapp_get_option(self::APP_NAME, $this) . DIRECTORY_SEPARATOR . $XAPP_RESOURCE_CONFIG . xapp_get_option(self::RESOURCE_CONFIG_SUFFIX, $this) . '.json'; } } } else { if ($XAPP_RUN_TIME_CONFIGURATION === 'debug') { $XAPP_RESOURCE_CONFIG_PATH .= 'lib' . DIRECTORY_SEPARATOR . xapp_get_option(self::APP_NAME, $this) . DIRECTORY_SEPARATOR . 'resources-' . $XAPP_RUN_TIME_CONFIGURATION . xapp_get_option(self::RESOURCE_CONFIG_SUFFIX, $this) . '.json'; } else { if ($XAPP_RUN_TIME_CONFIGURATION === 'release') { $XAPP_RESOURCE_CONFIG_PATH .= DIRECTORY_SEPARATOR . xapp_get_option(self::APP_FOLDER, $this) . DIRECTORY_SEPARATOR . xapp_get_option(self::APP_NAME, $this) . DIRECTORY_SEPARATOR . 'resources-' . $XAPP_RUN_TIME_CONFIGURATION . xapp_get_option(self::RESOURCE_CONFIG_SUFFIX, $this) . '.json'; } } } } /*** * Load plugin resources */ if (in_array(XAPP_BOOTSTRAP_LOAD_PLUGIN_RESOURCES, $flags) || in_array(XAPP_BOOTSTRAP_REGISTER_SERVER_PLUGINS, $flags)) { if (xapp_get_option(self::ALLOW_PLUGINS, $this) && xapp_get_option(self::PLUGIN_DIRECTORY, $this) && xapp_get_option(self::PLUGIN_DIRECTORY, $this)) { if (!$xComPluginManager) { $xComPluginManager = new XApp_Commander_PluginManager(); } $pluginResources = $xComPluginManager->getPluginResources(xapp_get_option(self::PLUGIN_DIRECTORY, $this), xapp_get_option(self::PLUGIN_DIRECTORY, $this), xapp_get_option(self::PLUGIN_MASK, $this)); } } /*** * Get plugins */ if (in_array(XAPP_BOOTSTRAP_REGISTER_SERVER_PLUGINS, $flags)) { if (xapp_get_option(self::ALLOW_PLUGINS, $this) && xapp_get_option(self::PLUGIN_DIRECTORY, $this) && xapp_get_option(self::PLUGIN_DIRECTORY, $this)) { if (!$xComPluginManager) { $xComPluginManager = new XApp_Commander_PluginManager(); } $plugins = $xComPluginManager->getPlugins(xapp_get_option(self::PLUGIN_DIRECTORY, $this), xapp_get_option(self::PLUGIN_MASK, $this)); } } $pluginInstances = array(); /*** * Register server plugins */ if ($xComPluginManager !== null && count($plugins) && xapp_get_option(self::RPC_SERVER, $this) && in_array(XAPP_BOOTSTRAP_REGISTER_SERVER_PLUGINS, $flags)) { switch (XApp_Service_Entry_Utils::getServiceType()) { /*** * JSON-RPC-2.0 call. In this case we are only loading the plugin which has been specified in the RPC call('service') */ case XApp_Service_Entry_Utils::SMD_CALL: /*** * Load the plugin by its service class name. A RPC POST request looks like this : * { * "id": 0, * "method": "Xapp_FileService.rename", * "params": { * "path": "./tmp/wp-mail.php", * "newFileName": "wp-mail.php2" * }, * "jsonrpc": "2.0" * } * * In case we go over JSONP, the url looks like this : * * http://0.0.0.0/zoo254/components/com_xas/xapp/index.php?service=XLESS.test&id=4&callback=as * */ $method = XApp_Service_Entry_Utils::getSMDMethod(); $serviceClass = null; if ($method != null && strpos($method, '.') != -1) { $methodSplitted = explode('.', $method); if ($methodSplitted && count($methodSplitted) == 2) { $serviceClass = $methodSplitted[0]; } } if ($serviceClass) { if (in_array(XAPP_LOG_PLUGIN_CREATION, $loggingFlags)) { $this->log('Calling service class::' . $serviceClass); } foreach ($plugins as $pluginConfig) { //load only when not prohibited $prohibited = explode(',', xapp_get_option(self::PROHIBITED_PLUGINS, $this)); if (!in_array($serviceClass, $prohibited)) { if ($pluginConfig !== null && is_object($pluginConfig)) { //pull in if not done yet if (!class_exists($pluginConfig->name)) { $pluginPath = xapp_get_option(self::PLUGIN_DIRECTORY, $this) . DIRECTORY_SEPARATOR . $pluginConfig->location . DIRECTORY_SEPARATOR . $pluginConfig->name . '.php'; if (in_array(XAPP_LOG_PLUGIN_CREATION, $loggingFlags)) { $this->log('loading plugin ' . $pluginConfig->name . ' at ' . $pluginPath); } if (file_exists($pluginPath)) { include_once $pluginPath; } else { continue; } } //create instance if (class_exists($serviceClass)) { $plugin = $xComPluginManager->createPluginInstance($pluginConfig->name, true, array(), array(), xapp_get_options(), $pluginConfig); if ($plugin != null) { xapp_get_option(self::RPC_SERVER)->register($plugin, array('_load')); array_push($pluginInstances, $plugin); //share logger if (in_array(XAPP_LOG_SHARED_LOGGER_PLUGINS, $loggingFlags) && $logger) { $plugin->_setLogger($logger); } } } } } } } break; /*** * JSON-RPC-2.0 Service Introspection. That means we expose all plugins as Dojo SMD! You can see the full RPC class by opening http://localhost/joomla251/administrator/index.php?option=com_xappcommander&view=rpc */ /*** * JSON-RPC-2.0 Service Introspection. That means we expose all plugins as Dojo SMD! You can see the full RPC class by opening http://localhost/joomla251/administrator/index.php?option=com_xappcommander&view=rpc */ case XApp_Service_Entry_Utils::SMD_GET: foreach ($plugins as $pluginConfig) { /*** * Skip black listed plugins */ $prohibited = explode(',', xapp_get_option(self::PROHIBITED_PLUGINS, $this)); if (in_array($pluginConfig->name, $prohibited)) { continue; } //show only if the plugins wants to be exposed! if (property_exists($pluginConfig, 'showSMD') && $pluginConfig->showSMD == true) { if (!class_exists($pluginConfig->name)) { $pluginPath = xapp_get_option(self::PLUGIN_DIRECTORY, $this) . DIRECTORY_SEPARATOR . $pluginConfig->location . DIRECTORY_SEPARATOR . $pluginConfig->name . '.php'; if (in_array(XAPP_LOG_PLUGIN_CREATION, $loggingFlags)) { $this->log('loading plugin ' . $pluginConfig->name . ' at ' . $pluginPath); } if (file_exists($pluginPath)) { include_once $pluginPath; } else { continue; } } //now register as RPC class if (class_exists($pluginConfig->name)) { xapp_get_option(self::RPC_SERVER)->register($pluginConfig->name); } } } break; } } else { } $xappFileService = null; /*** * Register store service */ if ($storeService) { xapp_get_option(self::RPC_SERVER)->register($storeService); } $serviceConfigurations = xapp_get_option(self::SERIVCE_CONF); /*** * More services */ if (in_array(XAPP_BOOTSTRAP_SETUP_SERVICES, $flags) && xapp_has_option(self::SERIVCE_CONF, $this) && xapp_get_option(self::RPC_SERVER, $this)) { //pull in registry of xapp core framework XApp_Service_Entry_Utils::includeXAppRegistry(); $serviceConfigurations = $this->registerServices($serviceConfigurations, xapp_get_option(self::RPC_SERVER)); } //Pick directory service instance $directoryServiceInstance = null; foreach ($serviceConfigurations as $service) { if ($service[XApp_Service::XAPP_SERVICE_CLASS] === 'XCOM_Directory_Service') { $directoryServiceInstance = $service[XApp_Service::XAPP_SERVICE_INSTANCE]; break; } } //share directory service instance in plugins if ($directoryServiceInstance) { foreach ($pluginInstances as $plugin) { $plugin->directoryService = $directoryServiceInstance; } } /*** * Setup gateway */ if (in_array(XAPP_BOOTSTRAP_SETUP_GATEWAY, $flags) && xapp_get_option(self::RPC_SERVER, $this)) { try { $needsSigning = false; $opt = xapp_has_option(self::GATEWAY_CONF) ? xapp_get_option(self::GATEWAY_CONF) : array(); /*** * Raise security and demand that the client did sign its request */ $signServiceTypes = xapp_get_option(self::SIGNED_SERVICE_TYPES, $this); if (in_array(XApp_Service_Entry_Utils::getServiceType(), $signServiceTypes)) { $needsSigning = true; //set signed $opt[Xapp_Rpc_Gateway::SIGNED_REQUEST] = true; //complete configuration if (!array_key_exists(Xapp_Rpc_Gateway::SIGNED_REQUEST_METHOD, $opt)) { $opt[Xapp_Rpc_Gateway::SIGNED_REQUEST_METHOD] = 'user'; } if (!array_key_exists(Xapp_Rpc_Gateway::SIGNED_REQUEST_USER_PARAM, $opt)) { $opt[Xapp_Rpc_Gateway::SIGNED_REQUEST_USER_PARAM] = 'user'; } } $this->setGatewayOptionArray(Xapp_Rpc_Gateway::ALLOW_IP, $opt); $this->setGatewayOptionArray(Xapp_Rpc_Gateway::DENY_IP, $opt); $this->setGatewayOptionArray(Xapp_Rpc_Gateway::ALLOW_HOST, $opt); $this->setGatewayOptionArray(Xapp_Rpc_Gateway::DENY_HOST, $opt); $rpcServer = xapp_get_option(self::RPC_SERVER, $this); /*** * Create the gateway */ $gateway = Xapp_Rpc_Gateway::instance(xapp_get_option(self::RPC_SERVER, $this), $opt); /*** * Set the API key for signed requests */ if ($needsSigning) { $gateway->addKey(xapp_get_option(self::SIGNING_KEY, $this), xapp_get_option(self::SIGNING_TOKEN, $this)); } $gateway->run(); } catch (Exception $e) { Xapp_Rpc_Server_Json::dump($e); } } return $this; }
/** * method to set/get memcached instance. when setting will overwrite previously set memcached instance so * passed instance should be instantiated will all required options etc. * * @error 15807 * @param Memcached $memcached expects optional memcached instance when setting * @return Memcached|null */ public function memcached(Memcached $memcached = null) { if ($memcached !== null) { return $this->_memcached = xapp_set_option(self::INSTANCE, $memcached, $this, true); } else { return $this->_memcached; } }
/** * flush exception as json error object encapsulated in js callback or custom error callback function if supplied * * @error 14708 * @param Exception $error expects the exception to flush */ public function error(Exception $error) { $get = $this->request()->getGet(); $response = $this->response(); if (xapp_get_option(self::COMPLY_TO_JSONRPC_1_2, $this)) { if (array_key_exists($error->getCode(), $this->codeMap)) { xapp_set_option(Xapp_Rpc_Response::STATUS_CODE, $this->codeMap[$error->getCode()], $response); } else { xapp_set_option(Xapp_Rpc_Response::STATUS_CODE, 500, $response); } } $error = $this->compileError($error); if (xapp_is_option(self::ERROR_CALLBACK, $this) && array_key_exists(xapp_get_option(self::ERROR_CALLBACK, $this), $get)) { $error = $get[xapp_get_option(self::ERROR_CALLBACK, $this)] . '(' . $response->encode($error) . ')'; } else { if (array_key_exists(xapp_get_option(self::CALLBACK, $this), $get)) { $error = $get[xapp_get_option(self::CALLBACK, $this)] . '(' . $response->encode($error) . ')'; } else { $error = $response->encode($error); } } $response->body($error); $this->flush(); }
/** * init instance by completing all passed options. e.g. if a path is not a absolute * log file pointer the name will be automatically created by the class. the init * function will check if any log writer instances have been passed in instance options * and register those to be executed at a later stage. if not default log writers are * registered * * @error 11602 * @return void */ protected function init() { $this->stack = array(); $path = xapp_get_option(self::PATH, $this); if (!preg_match('/\\.([a-z0-9]{2,4})$/i', $path)) { if (!xapp_is_option(self::NAME, $this)) { xapp_set_option(self::NAME, strftime(xapp_get_option(self::FILE_FORMAT, $this), time()), $this); } if (strpos(xapp_get_option(self::NAME, $this), '.') === false) { xapp_set_option(self::NAME, trim(xapp_get_option(self::NAME, $this), '.') . '.' . trim(xapp_get_option(self::EXTENSION, $this), '.'), $this); } if (!empty($path)) { $path = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . xapp_get_option(self::NAME, $this); } else { $path = debug_backtrace(); $path = array_pop($path); $path = rtrim(dirname($path['file']), DS) . DS; $path = $path . xapp_get_option(self::NAME, $this); } xapp_set_option(self::PATH, $path, $this); } if (xapp_is_option(self::WRITER, $this)) { foreach (xapp_get_option(self::WRITER, $this) as $writer) { if (is_array($writer)) { $this->register(array_shift($writer), array_shift($writer)); } else { $this->register($writer); } } } else { $this->register(new Xapp_Log_Writer_File(xapp_get_option(self::PATH, $this), xapp_get_option(self::ROTATION, $this)), 'file'); if (xapp_is_option(self::EMAIL, $this)) { $this->register(new Xapp_Log_Writer_Mail(xapp_get_option(self::EMAIL, $this), xapp_get_option(self::EMAIL_SUBJECT, $this)), 'mail'); } } }
/** * setter/getter method to set or get headers. to get headers call method without any parameters * returning all registered header values as array. to set header either pass raw header to first * argument leaving second argument unset or pass header key => value pair as first and second * argument. the raw header must be passed as one string like "Pragma: public" otherwise pass * key to first argument "Pragma" and value "public" to second argument. you can also pass multiple * headers using the first two arguments passing an array of keys in first and array of values in * second argument. size of these arrays must match or will throw exception. pass replacement * value and header code in third and fourth parameter as if you would use phps internal header(). * function will return always all registerd headers as array. when calling method without any arguments * will first time merge all header from class options to header array unsetting the class option * * @error 14505 * @param null|string|array $mixed expects raw header string, header key as string or array of keys * @param null|mixed|array $value expects null for raw header, mixed for header value or array of values * @param null|bool $replace expects optional boolean value on whether to replace header * @param null|int $code expects optional response code * @return array * @throws Xapp_Rpc_Response_Exception */ public function header($mixed = null, $value = null, $replace = null, $code = null) { if ($mixed !== null) { //raw header in first parameter if (is_string($mixed) && $value === null) { $mixed = explode(':', $mixed); if (sizeof($mixed) === 2) { $mixed = array(array(trim($mixed[0]), trim($mixed[1]))); } else { throw new Xapp_Rpc_Response_Exception(_("response raw header passed is not valid"), 1450501); } //header with key => value pair in first and second parameter } else { if (is_string($mixed) && !is_array($value)) { $mixed = array(array(trim($mixed), trim($value))); //headers with key => value pairs as array in first and second parameter } else { if (is_array($mixed) && is_array($value)) { $mixed = array($mixed, $value); //header not recognized } else { throw new Xapp_Rpc_Response_Exception(_("response header passed is not recognized"), 1450502); } } } for ($i = 0; $i < sizeof($mixed); $i++) { if (isset($mixed[$i][0]) && isset($mixed[$i][1])) { if (preg_match("/^content\\-type\\:\\s+([^\\s]+)\\s+charset\\=(.+)\$/i", $mixed[$i][0] . ': ' . $mixed[$i][1], $m)) { xapp_set_option(self::CONTENT_TYPE, strtolower(trim($m[1], '; ')), $this); xapp_set_option(self::CHARSET, strtolower(trim($m[2], '; ')), $this); } $obj = new XO(); $obj->name = $mixed[$i][0]; $obj->value = $mixed[$i][1]; $obj->replace = null; $obj->code = null; if ($replace !== null) { $obj->replace = (bool) $replace; } if (isset($mixed[$i][2])) { $obj->replace = (bool) $mixed[$i][2]; } if ($code !== null) { $obj->code = (int) $code; } if (isset($mixed[$i][3])) { $obj->code = (int) $mixed[$i][3]; } $this->_header[] = $obj; } else { throw new Xapp_Rpc_Response_Exception(_("response header miss match when passing keys and values as arrays"), 1450503); } } } else { if (xapp_is_option(self::HEADER, $this)) { $header = xapp_get_option(self::HEADER, $this); if (!is_array($header[0])) { $header = array($header); } xapp_unset_option(self::HEADER, $this); $this->header($header); } } return $this->_header; }
/** * handle exception by constructing json error object, setting it to response and instantly * flushing the error to output. if omit option is found in global rpc array the error message * will be omitted * * @error 14608 * @param Exception $error expects instance of Exception * @return void */ public function error(Exception $error) { if (xapp_get_option(self::COMPLY_TO_JSONRPC_1_2, $this)) { if (array_key_exists($error->getCode(), $this->codeMap)) { xapp_set_option(Xapp_Rpc_Response::STATUS_CODE, $this->codeMap[$error->getCode()], $response); } else { xapp_set_option(Xapp_Rpc_Response::STATUS_CODE, 500, $response); } } $this->response()->setVersion(xapp_get_option(self::VERSION, $this)); $this->response()->set('error', $this->compileError($error)); if ($this->request()->has('id')) { $this->response()->set('id', $this->request()->get('id')); } $this->response()->body($this->response()->data()); $this->response()->flush(); }
/** * setter/getter for response instance. if you set response instance not via class options * but at later stage you must make sure that response instance has all options set required * to work with server instance since server instance passes options to response instance * in class constructors * * @error 14207 * @param Xapp_Rpc_Response $response expects request instance when to set instance * @return null|Xapp_Rpc_Response */ public function response(Xapp_Rpc_Response $response = null) { if ($response !== null) { xapp_set_option(self::RESPONSE, $response, $this); } return xapp_get_option(self::RESPONSE, $this); }