PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/gplusone/JSON.php

https://gitlab.com/elasa/shop2.elasa.ir
PHP | 378 lines | 252 code | 16 blank | 110 comment | 31 complexity | 4eb51ef2b89d4eb635313d1500ffba12 MD5 | raw file
  1. <?php
  2. /*
  3. ***************************************************************************
  4. * Copyright (C) 2007 by Cesar D. Rodas *
  5. * crodas@phpy.org *
  6. * *
  7. * Permission is hereby granted, free of charge, to any person obtaining *
  8. * a copy of this software and associated documentation files (the *
  9. * "Software"), to deal in the Software without restriction, including *
  10. * without limitation the rights to use, copy, modify, merge, publish, *
  11. * distribute, sublicense, and/or sell copies of the Software, and to *
  12. * permit persons to whom the Software is furnished to do so, subject to *
  13. * the following conditions: *
  14. * *
  15. * The above copyright notice and this permission notice shall be *
  16. * included in all copies or substantial portions of the Software. *
  17. * *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, *
  19. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*
  21. * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR *
  22. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
  23. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
  24. * OTHER DEALINGS IN THE SOFTWARE. *
  25. ***************************************************************************
  26. */
  27. /**
  28. * Serialize and Unserialize PHP Objects and array
  29. * into JSON notation.
  30. *
  31. * @category Javascript
  32. * @package JSON
  33. * @author Cesar D. Rodas <crodas@phpy.org>
  34. * @copyright 2007 Cesar D. Rodas
  35. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  36. * @version 1.0
  37. * @link http://cesars.users.phpclasses.org/json
  38. */
  39. define('IN_NOWHERE',0);
  40. define('IN_STRING',1);
  41. define('IN_OBJECT',2);
  42. define('IN_ATOMIC',3);
  43. define('IN_ASSIGN',4);
  44. define('IN_ENDSTMT',5);
  45. define('IN_ARRAY',6);
  46. /**
  47. * JSON
  48. *
  49. * This class serilize an PHP OBJECT or an ARRAY into JSON
  50. * notation. Also convert a JSON text into a PHP OBJECT or
  51. * array.
  52. *
  53. * @category Javascript
  54. * @package JSON
  55. * @author Cesar D. Rodas <crodas@phpy.org>
  56. * @copyright 2007 Cesar D. Rodas
  57. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  58. * @version 1.0
  59. * @link http://cesars.users.phpclasses.org/json
  60. */
  61. class JSON
  62. {
  63. /**
  64. * Was parsed with an error?
  65. *
  66. * var bool
  67. * @access private
  68. */
  69. var $error;
  70. function Json() {
  71. $this->error = false;
  72. }
  73. /**
  74. * Serialize
  75. *
  76. * Serialize a PHP OBJECT or an ARRAY into
  77. * JSON notation.
  78. *
  79. * param mixed $obj Object or array to serialize
  80. * return string JSON.
  81. */
  82. function serialize($obj) {
  83. if ( is_object($obj) ) {
  84. $e = get_object_vars($obj);
  85. /* bug reported by Ben Rowe */
  86. /* Adding default empty array if the */
  87. /* object doesn't have any property */
  88. $properties = array();
  89. foreach ($e as $k => $v) {
  90. $properties[] = $this->_serialize( $k,$v );
  91. }
  92. return "{".implode(",",$properties)."}";
  93. } else if ( is_array($obj) ) {
  94. return $this->_serialize('',$obj);
  95. }
  96. }
  97. /**
  98. * UnSerialize
  99. *
  100. * Transform an JSON text into a PHP object
  101. * and return it.
  102. * @access public
  103. * @param string $text JSON text
  104. * @return mixed PHP Object, array or false.
  105. */
  106. function unserialize( $text ) {
  107. $this->error = false;
  108. return !$this->error ? $this->_unserialize($text) : false;
  109. }
  110. /**
  111. * UnSerialize
  112. *
  113. * Transform an JSON text into a PHP object
  114. * and return it.
  115. * @access private
  116. * @param string $text JSON text
  117. * @return mixed PHP Object, array or false.
  118. */
  119. function _unserialize($text) {
  120. $ret = new stdClass;
  121. while ( $f = $this->getNextToken($text,$i,$type) ) {
  122. switch ( $type ) {
  123. case IN_ARRAY:
  124. $tmp = $this->_unserializeArray($text);
  125. $ret = $tmp[0];
  126. break;
  127. case IN_OBJECT:
  128. $g=0;
  129. do {
  130. $varName = $this->getNextToken($f,$g,$xType);
  131. if ( $xType != IN_STRING ) {
  132. return false; /* error parsing */
  133. }
  134. $this->getNextToken($f,$g,$xType);
  135. if ( $xType != IN_ASSIGN) return false;
  136. $value = $this->getNextToken($f,$g,$xType);
  137. if ( $xType == IN_OBJECT) {
  138. $ret->$varName = $this->unserialize( "{".$value."}" );
  139. $g--;
  140. } else if ($xType == IN_ARRAY) {
  141. $ret->$varName = $this->_unserializeArray( $value);
  142. $g--;
  143. } else
  144. $ret->$varName = $value;
  145. $this->getNextToken($f,$g,$xType);
  146. } while ( $xType == IN_ENDSTMT);
  147. break;
  148. default:
  149. $this->error = true;
  150. break 2;
  151. }
  152. }
  153. return $ret;
  154. }
  155. /**
  156. * JSON Array Parser
  157. *
  158. * This method transform an json-array into a PHP
  159. * array
  160. * @access private
  161. * @param string $text String to parse
  162. * @return Array PHP Array
  163. */
  164. function _unserializeArray($text) {
  165. $r = array();
  166. do {
  167. $f = $this->getNextToken($text,$i,$type);
  168. $f = preg_replace_callback('/\\\u[a-z0-9]{4}/',create_function('$m','return chr(base_convert($m[0],16,10));'),$f);
  169. switch ( $type ) {
  170. case IN_STRING:
  171. case IN_ATOMIC:
  172. $r[] = $f;
  173. break;
  174. case IN_OBJECT:
  175. $r[] = $this->unserialize("{".$f."}");
  176. $i--;
  177. break;
  178. case IN_ARRAY:
  179. $r[] = $this->_unserializeArray($f);
  180. $i--;
  181. break;
  182. }
  183. $this->getNextToken($text,$i,$type);
  184. } while ( $type == IN_ENDSTMT);
  185. return $r;
  186. }
  187. /**
  188. * Tokenizer
  189. *
  190. * Return to the Parser the next valid token and the type
  191. * of the token. If the tokenizer fails it returns false.
  192. *
  193. * @access private
  194. * @param string $e Text to extract token
  195. * @param integer $i Start position to search next token
  196. * @param integer $state Variable to get the token type
  197. * @return string|bool Token in string or false on error.
  198. */
  199. function getNextToken($e, &$i, &$state) {
  200. $state = IN_NOWHERE;
  201. $end = -1;
  202. $start = -1;
  203. while ( $i < strlen($e) && $end == -1 ) {
  204. switch( $e[$i] ) {
  205. /* objects */
  206. case "{":
  207. case "[":
  208. $_tag = $e[$i];
  209. $_endtag = $_tag == "{" ? "}" : "]";
  210. if ( $state == IN_NOWHERE ) {
  211. $start = $i+1;
  212. switch ($state) {
  213. case IN_NOWHERE:
  214. $aux = 1; /* for loop objects */
  215. $state = $_tag == "{" ? IN_OBJECT : IN_ARRAY;
  216. break;
  217. default:
  218. break 2; /* exit from switch and while */
  219. }
  220. while ( ++$i && $i < strlen($e) && $aux != 0 ) {
  221. switch( $e[$i] ) {
  222. case $_tag:
  223. $aux++;
  224. break;
  225. case $_endtag:
  226. $aux--;
  227. break;
  228. }
  229. }
  230. $end = $i-1;
  231. }
  232. break;
  233. case '"':
  234. case "'":
  235. $state = IN_STRING;
  236. $buf = "";
  237. while ( ++$i && $i < strlen($e) && $e[$i] != '"' ) {
  238. if ( $e[$i] == "\\")
  239. $i++;
  240. $buf .= $e[$i];
  241. }
  242. $i++;
  243. return eval('return "'.str_replace('"','\"',$buf).'";');
  244. break;
  245. case ":":
  246. $state = IN_ASSIGN;
  247. $end = 1;
  248. break;
  249. case "n":
  250. if ( substr($e,$i,4) == "null" ) {
  251. $i=$i+4;
  252. $state = IN_ATOMIC;
  253. return NULL;
  254. }
  255. else break 2; /* exit from switch and while */
  256. case "t":
  257. if ( substr($e,$i,4) == "true") {
  258. $state = IN_ATOMIC;
  259. $i=$i+4;
  260. return true;
  261. }else break 2; /* exit from switch and while */
  262. break;
  263. case "f":
  264. if ( substr($e,$i,4) == "false") {
  265. $state = IN_ATOMIC;
  266. $i=$i+4;
  267. return false;
  268. }
  269. else break 2; /* exit from switch and while */
  270. break;
  271. case ",":
  272. $state = IN_ENDSTMT;
  273. $end = 1;
  274. break;
  275. case " ":
  276. case "\t":
  277. case "\r":
  278. case "\n":
  279. break;
  280. case "+":
  281. case "-":
  282. case 0:
  283. case 1:
  284. case 2:
  285. case 3:
  286. case 4:
  287. case 5:
  288. case 6:
  289. case 7:
  290. case 8:
  291. case 9:
  292. case '.':
  293. $state = IN_ATOMIC;
  294. $start = (int)$i;
  295. if ( $e[$i] == "-" || $e[$i] == "+")
  296. $i++;
  297. for ( ; $i < strlen($e) && (is_numeric($e[$i]) || $e[$i] == "." || strtolower($e[$i]) == "e") ;$i++){
  298. $n = $i+1 < strlen($e) ? $e[$i+1] : "";
  299. $a = strtolower($e[$i]);
  300. if ( $a == "e" && ($n == "+" || $n == "-"))
  301. $i++;
  302. else if ( $a == "e")
  303. $this->error=true;
  304. }
  305. $end = $i;
  306. break 2; /* break while too */
  307. default:
  308. $this->error = true;
  309. }
  310. $i++;
  311. }
  312. return $start == -1 || $end == -1 ? false : substr($e, $start, $end - $start);
  313. }
  314. /**
  315. * Internal Serializer
  316. *
  317. * @param string $key Variable name
  318. * @param mixed $value Value of the variable
  319. * @access private
  320. * @return string Serialized variable
  321. */
  322. function _serialize ( $key = '', &$value ) {
  323. $r = '';
  324. if ( $key != '')$r .= "\"${key}\" : ";
  325. if ( is_numeric($value) ) {
  326. $r .= ''.$value.'';
  327. } else if ( is_string($value) ) {
  328. $r .= '"'.$this->toString($value).'"';
  329. } else if ( is_object($value) ) {
  330. $r .= $this->serialize($value);
  331. } else if ( is_null($value) ) {
  332. $r .= "null";
  333. } else if ( is_bool($value) ) {
  334. $r .= $value ? "true":"false";
  335. } else if ( is_array($value) ) {
  336. foreach($value as $k => $v)
  337. $f[] = $this->_serialize('',$v);
  338. $r .= "[".implode(",",$f)."]";
  339. unset($f);
  340. }
  341. return $r;
  342. }
  343. /**
  344. * Convert String variables
  345. *
  346. * @param string $e Variable with an string value
  347. * @access private
  348. * @return string Serialized variable
  349. */
  350. function toString($e) {
  351. $rep = array("\\","\r","\n","\t","'",'"');
  352. $val = array("\\\\",'\r','\n','\t','\'','\"');
  353. $e = str_replace($rep, $val, $e);
  354. return $e;
  355. }
  356. }
  357. ?>