/** * class constructor checks if passed object is a json string and decodes then first, than * calls parent constructor * * @error 16801 * @param array|object|string $object expects the json object or json string * @param null|mixed $options expects optional options */ public function __construct(&$object, $options = null) { if (Xapp_Util_Json::isJson($object)) { $object = Xapp_Util_Json::decode($object); } parent::__construct($object, $options); }
public static function read_json($storage, $type = 'php', $writable = false, $decode = true, $input = array(), $assoc = true) { $storage = preg_replace("~\\\\+([\"\\'\\x00\\\\])~", "\$1", $storage); $storage = realpath($storage); self::$_storage = '' . $storage; self::$_storageType = strtolower(trim((string) $type)); if (in_array(self::$_storageType, self::$_storageTypes)) { if (is_dir(self::$_storage)) { if (is_writeable(self::$_storage) && $writable === true) { self::$_storage = rtrim(self::$_storage, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . '.storage'; } else { throw new Xapp_Util_Exception_Storage(vsprintf('storage directory: %s is not writable', array(self::$_storage)), 1640101); } } else { if (file_exists(self::$_storage)) { if (($container = file_get_contents(self::$_storage)) !== false) { if (!empty($container)) { switch (self::$_storageType) { case 'json': if ($decode == false) { $container = str_replace(array("\n", "\r", "\t"), array('', '', ''), $container); $container = preg_replace('/[\\x00-\\x09\\x0B\\x0C\\x0E-\\x1F\\x7F]/', '', $container); $container = preg_replace('/\\r\\n?/', "", $container); $container = str_replace('\\/', '/', $container); $container = preg_replace('/[\\x00-\\x1F\\x7F]/', '', $container); return $container; } $container = Xapp_Util_Json::decode($container, $assoc); return $container; case 'php': $container = @unserialize($container); break; } if ($container === null || $container === false) { throw new Xapp_Util_Exception_Storage('unable to decode stored data', 1640103); } } if (!empty($container)) { //$this->data = $container; } else { } } else { throw new Xapp_Util_Exception_Storage('unable to read storage from file: ' . self::$_storage, 1640104); } } else { if ($writable === true && !is_writeable(dirname(self::$_storage))) { throw new Xapp_Util_Exception_Storage(vsprintf('storage directory: %s is not writable', array(self::$_storage)), 1640101); } throw new Xapp_Util_Exception_Storage('unable to read storage from file:' . self::$_storage, 1640104); } } } else { throw new Xapp_Util_Exception_Storage("storage type: {$type} is not supported", 1640105); } return null; }
public function write($data) { $path = xapp_get_option(self::CONF_FILE, $this); if (file_exists($path)) { $result = null; $pretty = true; if ($pretty) { $result = file_put_contents($path, Xapp_Util_Json::prettify($data), LOCK_EX); } else { $result = file_put_contents($path, $data, LOCK_EX); } if ($result !== false) { $result = null; return true; } else { throw new Xapp_Util_Json_Exception("unable to save to file: " . $path, 1690501); } } return $data; }
/** * json implementation of saveTo method, see Xapp_Util_Std_Store::saveTo for more details * * @error 16905 * @param string $file expects absolute file path to store object at * @param bool $pretty expects boolean value whether to store json string pretty or non pretty * @return bool * @throws Xapp_Util_Json_Exception */ public function saveTo($file, $pretty = true) { $result = null; if ((bool) $pretty) { $result = file_put_contents($file, Xapp_Util_Json::prettify(Xapp_Util_Json::encode($this->_object)), LOCK_EX); } else { $result = file_put_contents($file, Xapp_Util_Json::encode($this->_object), LOCK_EX); } if ($result !== false) { $result = null; return true; } else { throw new Xapp_Util_Json_Exception(xapp_sprintf(_("unable to save to file: %s"), $file), 1690501); } }
public function setup() { /*** * 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_Utils::_getKey('rtConfig', XApp_Service_Entry_Utils::getRunTimeConfiguration()); /*** * Now include all xapp stuff */ //pull in parts of xapp core framework XApp_Service_Entry_Utils::includeXAppCore(); //pull in registry of xapp core framework XApp_Service_Entry_Utils::includeXAppRegistry(); //pull in parts of xapp json framework XApp_Service_Entry_Utils::includeXAppJSONStoreClasses(); //pull in json utils (to read client app's resource configuration XApp_Service_Entry_Utils::includeXAppJSONTools(); //some debugging tools XApp_Service_Entry_Utils::includeXAppDebugTools(); //pull in legacy client app renderer xapp_import('xapp.app.Renderer'); self::loadCommons(); //pull in xapp commander renderer /*include_once(XAPP_BASEDIR . DIRECTORY_SEPARATOR . 'app'. DIRECTORY_SEPARATOR . 'Commander.php');*/ //pull in xapp resource renderer include_once XAPP_BASEDIR . '/Resource/Renderer.php'; //pull in xapp resource renderer xapp_import('xapp.commander.Resource.Renderer'); //pull in xapp resource renderer xapp_import('xapp.Resource.Renderer'); //pull in xide resource Renderer xapp_import('xapp.xide.Resource.Renderer'); //pull in xide resource Renderer //xapp_import('xapp.xcf.Resource.Renderer'); //pull in cms related resource renderer include_once XAPP_LIB . DIRECTORY_SEPARATOR . xapp_get_option(self::RESOURCE_RENDERER_PREFIX, $this) . DIRECTORY_SEPARATOR . 'ResourceRenderer.php'; /*** * Prepare resource renderer */ //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'; } } } //error_log('$XAPP_RESOURCE_CONFIG ' . $XAPP_RESOURCE_CONFIG . " = " . $XAPP_RESOURCE_CONFIG_PATH); if (!file_exists($XAPP_RESOURCE_CONFIG_PATH)) { $this->log('have no core resources, ' . $XAPP_RESOURCE_CONFIG_PATH . ' doesnt exists'); return null; } $resources = (object) XApp_Utils_JSONUtils::read_json($XAPP_RESOURCE_CONFIG_PATH, 'json', false, true); $pluginResources = null; /*** * Load plugin resources */ if (xapp_get_option(self::ALLOW_PLUGINS, $this) && xapp_get_option(self::PLUGIN_DIRECTORY, $this) && xapp_get_option(self::PLUGIN_DIRECTORY, $this)) { //pull in xapp plugin manager include_once XAPP_BASEDIR . '/commander/PluginManager.php'; //pull in xapp commander plugin base class include_once XAPP_BASEDIR . '/commander/Plugin.php'; //pull in RPC interface if (!class_exists('Xapp_Rpc_Interface_Callable')) { //pull in xapp commander plugin base class include_once XAPP_BASEDIR . '/Rpc/Interface/Callable.php'; } $xComPluginManager = new XApp_Commander_PluginManager(); $loadedPlugins = null; $plugins = $xComPluginManager->loadPlugins(xapp_get_option(self::PLUGIN_DIRECTORY, $this), xapp_get_option(self::PLUGIN_DIRECTORY, $this), xapp_get_option(self::PLUGIN_MASK, $this)); $pluginResources = $this->getPluginResources($plugins, $XAPP_RUN_TIME_CONFIGURATION, $XAPP_RESOURCE_CONFIG); } //error_log('flags ' . json_encode(xapp_get_option(self::FLAGS, $this))); //now merge into app resources, filtered if ($pluginResources) { foreach ($pluginResources as $pluginResourceItems) { foreach ($pluginResourceItems as $pluginResource) { $prohibited = explode(',', xapp_get_option(self::PROHIBITED_PLUGINS, $this)); if (property_exists($pluginResource, 'type') && property_exists($pluginResource, 'name')) { //is plugin item if (in_array('X' . $pluginResource->name, $prohibited)) { continue; } else { array_push($resources->items, $pluginResource); } } else { array_push($resources->items, $pluginResource); } } } } $resourceRendererOptions = array(XApp_Resource_Renderer::DOC_ROOT => xapp_get_option(self::DOC_ROOT, $this), XApp_Resource_Renderer::DOC_ROOT_PATH => xapp_get_option(self::APPDIR, $this), XApp_Resource_Renderer::RESOURCES_DATA => $resources, XApp_Resource_Renderer::RENDER_DELEGATE => xapp_get_option(self::RENDER_DELEGATE, $this)); $clz = xapp_get_option(self::RESOURCE_RENDERER_CLZ, $this); $xappResourceRenderer = new $clz($resourceRendererOptions); $xappResourceRenderer->registerDefault(); if (xapp_has_option(self::RELATIVE_VARIABLES)) { $rVariables = xapp_get_option(self::RELATIVE_VARIABLES, $this); if ($rVariables != null && count($rVariables)) { foreach ($rVariables as $variable => $value) { $xappResourceRenderer->registerRelative($variable, $value); } } } $XAPP_DOJO_PACKAGES = '[]'; $XAPP_DOJO_PACKAGE_LOCATION_PREFIX = $xappResourceRenderer->resolveRelative('%PLUGIN_PACKAGE_ROOT_URL%'); $javascriptPlugins = $xappResourceRenderer->getJavascriptPlugins(); if ($javascriptPlugins && count($javascriptPlugins)) { if (XApp_Service_Entry_Utils::isDebug()) { $dojoPackages = array(); $dojoPackagesStr = '['; $pIdx = 0; foreach ($javascriptPlugins as $plugin) { if (!is_object($plugin)) { continue; } if ($pIdx > 0) { $dojoPackagesStr .= ","; } $dojoPackagesStr .= "{name:" . "'" . $plugin->name . "',"; if (property_exists($plugin, 'packageLocation')) { $dojoPackagesStr .= "location:" . "'" . $plugin->packageLocation . "'}"; } else { $dojoPackagesStr .= "location:" . "'" . $XAPP_DOJO_PACKAGE_LOCATION_PREFIX . $plugin->name . '/client/' . "'}"; } if ($pIdx < count($javascriptPlugins) - 1) { $dojoPackagesStr .= ','; } } $dojoPackagesStr .= ']'; $XAPP_DOJO_PACKAGES = $dojoPackagesStr; } else { $packageSuffix = ""; $dojoPackages = array(); array_push($dojoPackages, array('name' => 'dojo', 'location' => 'dojo')); array_push($dojoPackages, array('name' => 'dojox', 'location' => 'dojox')); array_push($dojoPackages, array('name' => 'dijit', 'location' => 'dijit')); array_push($dojoPackages, array('name' => 'cbtree', 'location' => 'cbtree')); array_push($dojoPackages, array('name' => 'xfile', 'location' => 'xfile')); array_push($dojoPackages, array('name' => 'xide', 'location' => 'xide')); array_push($dojoPackages, array('name' => 'xwordpress', 'location' => 'xwordpress')); array_push($dojoPackages, array('name' => 'xbox', 'location' => 'xbox')); array_push($dojoPackages, array('name' => 'xjoomla', 'location' => 'xjoomla')); foreach ($javascriptPlugins as $plugin) { if (is_object($plugin)) { if (property_exists($plugin, 'packageSuffix')) { $packageSuffix = $plugin->{'packageSuffix'}; } $packageLocation = $XAPP_DOJO_PACKAGE_LOCATION_PREFIX . $plugin->name . '/client/'; if (strlen($packageSuffix)) { $packageLocation .= $packageSuffix . '/'; } if (property_exists($plugin, 'packageLocation')) { $packageLocation = $plugin->packageLocation; } array_push($dojoPackages, array('name' => $plugin->name, 'location' => $packageLocation)); } } $XAPP_DOJO_PACKAGES = json_encode($dojoPackages); } /**** * Render plugin resources */ $javaScriptHeaderStr = ''; $javaScriptHeaderStr .= 'var xappPluginResources='; $javaScriptHeaderStr .= json_encode($javascriptPlugins) . ';'; $javaScriptHeaderStr .= ''; $xappResourceRenderer->registerRelative('XAPP_PLUGIN_RESOURCES', json_encode($javascriptPlugins)); //important: get the resource variables before adding 'head' otherwise it breaks the JSON structure! $resourceVariables = (array) $xappResourceRenderer->registryToKeyValues(xapp_get_option(XApp_Resource_Renderer::RELATIVE_REGISTRY_NAMESPACE, $xappResourceRenderer)); $resourceVariables['HTML_HEADER'] = array(); $resourceVariables['XAPP_PLUGIN_RESOURCES'] = $javascriptPlugins; $resourceVariables['DOJOPACKAGES'] = array(); $resourceVariables['XFILE_CONFIG_MIXIN'] = array(); $resourceVariables['RESOURCE_VARIABLES'] = array(); $xappResourceRenderer->registerRelative('RESOURCE_VARIABLES', Xapp_Util_Json::prettify(json_encode($resourceVariables, true)), LOCK_EX); } $xappResourceRenderer->registerRelative('DOJOPACKAGES', $XAPP_DOJO_PACKAGES); $pConfigDefault = $xappResourceRenderer->resolveRelative('%PACKAGE_CONFIG%'); if (!strlen($pConfigDefault)) { $pConfigDefault = 'run-release-debug'; } //error_log('$pConfigDefault ' .$pConfigDefault); $xappResourceRenderer->registerRelative('PACKAGE_CONFIG', XApp_Service_Utils::_getKey('pConfig', $pConfigDefault)); /**** * Build XApp-App-Renderer - Config */ $opt = array(XApp_App_Commander::DOC_ROOT_PATH => xapp_get_option(self::APPDIR, $this), XApp_App_Commander::DOC_ROOT => xapp_get_option(self::DOC_ROOT, $this), XApp_App_Commander::APP_NAME => xapp_get_option(self::APP_NAME, $this), XApp_App_Commander::APP_FOLDER => xapp_get_option(self::APP_FOLDER, $this), XApp_App_Commander::CONFIG_NAME => $XAPP_RUN_TIME_CONFIGURATION, XApp_App_Commander::SERVICE_URL => xapp_get_option(self::SERVICE, $this), XApp_App_Commander::RESOURCE_RENDERER => $xappResourceRenderer); $xappAppRenderer = new XApp_App_Commander($opt); $this->appRenderer = $xappAppRenderer; $this->resourceRenderer = $xappResourceRenderer; return $xappAppRenderer; }
/** * class constructor expects an absolute file/dir pointer and an optional input * array or object to initialize function. if a directory is passed will auto create * global storage file with the name ".storage" * * @error 16401 * @param string $storage expects the absolute path to storage file or directory * @param string $type expects storage data type which defaults to php * @param array|object|null $input expects optional storage data * @throws Xapp_Util_Exception_Storage */ private function readJSON($storage, $type = 'php', $input = array()) { $storage = preg_replace("~\\\\+([\"\\'\\x00\\\\])~", "\$1", $storage); $this->_storage = $storage; $this->_storageType = strtolower(trim((string) $type)); if (in_array($this->_storageType, $this->_storageTypes)) { if (is_dir($this->_storage)) { if (is_writeable($this->_storage)) { $this->_storage = rtrim($this->_storage, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . '.storage'; } else { /*throw new Xapp_Util_Exception_Storage(vsprintf('storage directory: %s is not writable', array($this->_storage)), 1640101);*/ } } else { if (file_exists($this->_storage)) { if (($container = file_get_contents($this->_storage)) !== false) { if (!empty($container)) { switch ($this->_storageType) { case 'json': $container = Xapp_Util_Json::decode($container, true); return $container; case 'php': $container = @unserialize($container); break; default: //nothing } if ($container === null || $container === false) { throw new Xapp_Util_Exception_Storage('PluginManager : unable to decode stored data', 1640103); } } if (!empty($container)) { $this->data = $container; } else { } } else { throw new Xapp_Util_Exception_Storage('PluginManaager unable to read storage from file', 1640104); } } else { /*throw new Xapp_Util_Exception_Storage('unable to read storage from file', 1640104);*/ } } } else { throw new Xapp_Util_Exception_Storage("storage type: {$type} is not supported", 1640105); } return null; }
/** * save the data to the storage * * @error 16404 * @throws Xapp_Util_Exception_Storage * @return void */ public function save() { $return = null; switch ($this->_storageType) { case 'json': $return = file_put_contents($this->_storage, Xapp_Util_Json::prettify(Xapp_Util_Json::encode($this->getArrayCopy())), LOCK_EX); break; case 'serialized': $return = file_put_contents($this->_storage, serialize($this->getArrayCopy()), LOCK_EX); break; default: //nothing } if ($return === false) { throw new Xapp_Util_Exception_Storage('unable to save to storage', 1640401); } }
/** * @param $storage * @param $data * @param string $type * @param bool $pretty * @return null * @throws Xapp_Util_Exception_Storage */ public static function write_json($storage, $data, $type = 'json', $pretty = false, $pass = null) { $return = null; switch ($type) { case 'json': $_dataStr = is_string($data) ? $data : Xapp_Util_Json::encode($data); if (strpos($storage, '.php') != -1) { $_dataStr = '<?php ' . PHP_EOL . $_dataStr; } if (isset($pass) && strlen($pass)) { $_dataStr = '~~~~~' . self::encrypt($_dataStr, $pass); } if ($pretty === true) { $return = file_put_contents($storage, Xapp_Util_Json::prettify($_dataStr), LOCK_EX); } else { $return = file_put_contents($storage, $_dataStr, LOCK_EX); } break; case 'serialized': $return = file_put_contents($storage, serialize($data), LOCK_EX); break; default: //nothing } if ($return === false) { throw new Xapp_Util_Exception_Storage('unable to save to storage', 1640401); } return null; }