/** * 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; }
/** * 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 */ public function __construct($storage, $type = 'php', $input = array()) { $this->_storage = DIRECTORY_SEPARATOR . ltrim($storage, DIRECTORY_SEPARATOR); $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'; parent::__construct($input); } 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 (!is_writeable($this->_storage)) { throw new Xapp_Util_Exception_Storage(vsprintf('storage file: %s is not writable', array($this->_storage)), 1640102); } if (($container = file_get_contents($this->_storage)) !== false) { if (!empty($container)) { switch ($this->_storageType) { case 'json': $container = Xapp_Util_Json::decode($container, true); break; case 'php': $container = @unserialize($container); break; default: //nothing } if ($container === null || $container === false) { throw new Xapp_Util_Exception_Storage('unable to decode stored data', 1640103); } } if (!empty($container)) { parent::__construct(array_merge((array) $container, (array) $input)); } else { parent::__construct($input); } } else { throw new Xapp_Util_Exception_Storage('unable to read storage from file:', 1640104); } } else { if (!is_writeable(dirname($this->_storage))) { throw new Xapp_Util_Exception_Storage(vsprintf('storage directory: %s is not writable', array($this->_storage)), 1640101); } parent::__construct($input); } } } else { throw new Xapp_Util_Exception_Storage("storage type: {$type} is not supported", 1640105); } }
/** * json implementation for decoding, see Xapp_Util_Std_Store::decode for more details * * @error 16902 * @see Xapp_Util_Std_Store::decode * @param mixed $value expects the value to try to decode * @return mixed|string */ public static function decode($value) { if (is_string($value)) { if (substr(trim($value), 0, 1) === '{' || substr(trim($value), 0, 2) === '[{') { return Xapp_Util_Json::decode($value); } else { if (preg_match('/^([adObis]\\:|N\\;)/', trim($value))) { return unserialize($value); } } } return $value; }
/** * 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; }