PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/ajax/xajax_core/xajaxArgumentManager.inc.php

https://github.com/sarriaroman/PuntoUBP
PHP | 466 lines | 310 code | 51 blank | 105 comment | 67 complexity | a2a5ffbf84170afa95f7af2a63bc31d2 MD5 | raw file
  1. <?php
  2. /*
  3. File: xajaxArgumentManager.inc.php
  4. Contains the xajaxArgumentManager class
  5. Title: xajaxArgumentManager class
  6. Please see <copyright.inc.php> for a detailed description, copyright
  7. and license information.
  8. */
  9. /*
  10. @package xajax
  11. @version $Id: xajaxArgumentManager.inc.php 362 2007-05-29 15:32:24Z calltoconstruct $
  12. @copyright Copyright (c) 2005-2007 by Jared White & J. Max Wilson
  13. @copyright Copyright (c) 2008-2009 by Joseph Woolley, Steffen Konerow, Jared White & J. Max Wilson
  14. @license http://www.xajaxproject.org/bsd_license.txt BSD License
  15. */
  16. if (!defined('XAJAX_METHOD_UNKNOWN')) define('XAJAX_METHOD_UNKNOWN', 0);
  17. if (!defined('XAJAX_METHOD_GET')) define('XAJAX_METHOD_GET', 1);
  18. if (!defined('XAJAX_METHOD_POST')) define('XAJAX_METHOD_POST', 2);
  19. /*
  20. Class: xajaxArgumentManager
  21. This class processes the input arguments from the GET or POST data of
  22. the request. If this is a request for the initial page load, no arguments
  23. will be processed. During a xajax request, any arguments found in the
  24. GET or POST will be converted to a PHP array.
  25. */
  26. class xajaxArgumentManager
  27. {
  28. /*
  29. Array: aArgs
  30. An array of arguments received via the GET or POST parameter
  31. xjxargs.
  32. */
  33. var $aArgs;
  34. /*
  35. Boolean: bDecodeUTF8Input
  36. A configuration option used to indicate whether input data should be
  37. UTF8 decoded automatically.
  38. */
  39. var $bDecodeUTF8Input;
  40. /*
  41. String: sCharacterEncoding
  42. The character encoding in which the input data will be received.
  43. */
  44. var $sCharacterEncoding;
  45. /*
  46. Integer: nMethod
  47. Stores the method that was used to send the arguments from the client. Will
  48. be one of: XAJAX_METHOD_UNKNOWN, XAJAX_METHOD_GET, XAJAX_METHOD_POST
  49. */
  50. var $nMethod;
  51. /*
  52. Array: aSequence
  53. Stores the decoding sequence table.
  54. */
  55. var $aSequence;
  56. /*
  57. Function: convertStringToBool
  58. Converts a string to a bool var.
  59. Parameters:
  60. $sValue - (string):
  61. Returns:
  62. (bool) : true / false
  63. */
  64. function convertStringToBool($sValue)
  65. {
  66. if (0 == strcasecmp($sValue, 'true'))
  67. return true;
  68. if (0 == strcasecmp($sValue, 'false'))
  69. return false;
  70. if (is_numeric($sValue))
  71. {
  72. if (0 == $sValue)
  73. return false;
  74. return true;
  75. }
  76. return false;
  77. }
  78. function argumentStripSlashes(&$sArg)
  79. {
  80. if (false == is_string($sArg))
  81. return;
  82. $sArg = stripslashes($sArg);
  83. }
  84. function argumentDecodeXML(&$sArg)
  85. {
  86. if (false == is_string($sArg))
  87. return;
  88. if (0 == strlen($sArg))
  89. return;
  90. $nStackDepth = 0;
  91. $aStack = array();
  92. $aArg = array();
  93. $nCurrent = 0;
  94. $nLast = 0;
  95. $aExpecting = array();
  96. $nFound = 0;
  97. list($aExpecting, $nFound) = $this->aSequence['start'];
  98. $nLength = strlen($sArg);
  99. $sKey = '';
  100. $mValue = '';
  101. while ($nCurrent < $nLength)
  102. {
  103. $bFound = false;
  104. foreach ($aExpecting as $sExpecting => $nExpectedLength)
  105. {
  106. if ($sArg[$nCurrent] == $sExpecting[0])
  107. {
  108. if ($sExpecting == substr($sArg, $nCurrent, $nExpectedLength))
  109. {
  110. list($aExpecting, $nFound) = $this->aSequence[$sExpecting];
  111. switch ($nFound)
  112. {
  113. case 3: // k
  114. $sKey = '';
  115. break;
  116. case 4: // /k
  117. $sKey = str_replace(
  118. array('<'.'![CDATA[', ']]>'),
  119. '',
  120. substr($sArg, $nLast, $nCurrent - $nLast)
  121. );
  122. break;
  123. case 5: // v
  124. $mValue = '';
  125. break;
  126. case 6: // /v
  127. if ($nLast < $nCurrent)
  128. {
  129. $mValue = str_replace(
  130. array('<'.'![CDATA[', ']]>'),
  131. '',
  132. substr($sArg, $nLast, $nCurrent - $nLast)
  133. );
  134. $cType = substr($mValue, 0, 1);
  135. $sValue = substr($mValue, 1);
  136. switch ($cType) {
  137. case 'S': $mValue = false === $sValue ? '' : $sValue; break;
  138. case 'B': $mValue = $this->convertStringToBool($sValue); break;
  139. case 'N': $mValue = floatval($sValue); break;
  140. case '*': $mValue = null; break;
  141. }
  142. }
  143. break;
  144. case 7: // /e
  145. $aArg[$sKey] = $mValue;
  146. break;
  147. case 1: // xjxobj
  148. ++$nStackDepth;
  149. array_push($aStack, $aArg);
  150. $aArg = array();
  151. array_push($aStack, $sKey);
  152. $sKey = '';
  153. break;
  154. case 8: // /xjxobj
  155. if (1 < $nStackDepth) {
  156. $mValue = $aArg;
  157. $sKey = array_pop($aStack);
  158. $aArg = array_pop($aStack);
  159. --$nStackDepth;
  160. } else {
  161. $sArg = $aArg;
  162. return;
  163. }
  164. break;
  165. }
  166. $nCurrent += $nExpectedLength;
  167. $nLast = $nCurrent;
  168. $bFound = true;
  169. break;
  170. }
  171. }
  172. }
  173. if (false == $bFound)
  174. {
  175. if (0 == $nCurrent)
  176. {
  177. $sArg = str_replace(
  178. array('<'.'![CDATA[', ']]>'),
  179. '',
  180. $sArg
  181. );
  182. $cType = substr($sArg, 0, 1);
  183. $sValue = substr($sArg, 1);
  184. switch ($cType) {
  185. case 'S': $sArg = false === $sValue ? '' : $sValue; break;
  186. case 'B': $sArg = $this->convertStringToBool($sValue); break;
  187. case 'N': $sArg = floatval($sValue); break;
  188. case '*': $sArg = null; break;
  189. }
  190. return;
  191. }
  192. // for larger arg data, performance may suffer using concatenation
  193. // $sText .= $sArg[$nCurrent];
  194. $nCurrent++;
  195. }
  196. }
  197. $objLanguageManager =& xajaxLanguageManager::getInstance();
  198. trigger_error(
  199. $objLanguageManager->getText('ARGMGR:ERR:01')
  200. . $sExpecting
  201. . $objLanguageManager->getText('ARGMGR:ERR:02')
  202. . $sArg
  203. , E_USER_ERROR
  204. );
  205. }
  206. function argumentDecodeUTF8_iconv(&$mArg)
  207. {
  208. if (is_array($mArg))
  209. {
  210. foreach (array_keys($mArg) as $sKey)
  211. {
  212. $sNewKey = $sKey;
  213. $this->argumentDecodeUTF8_iconv($sNewKey);
  214. if ($sNewKey != $sKey)
  215. {
  216. $mArg[$sNewKey] = $mArg[$sKey];
  217. unset($mArg[$sKey]);
  218. $sKey = $sNewKey;
  219. }
  220. $this->argumentDecodeUTF8_iconv($mArg[$sKey]);
  221. }
  222. }
  223. else if (is_string($mArg))
  224. $mArg = iconv("UTF-8", $this->sCharacterEncoding.'//TRANSLIT', $mArg);
  225. }
  226. function argumentDecodeUTF8_mb_convert_encoding(&$mArg)
  227. {
  228. if (is_array($mArg))
  229. {
  230. foreach (array_keys($mArg) as $sKey)
  231. {
  232. $sNewKey = $sKey;
  233. $this->argumentDecodeUTF8_mb_convert_encoding($sNewKey);
  234. if ($sNewKey != $sKey)
  235. {
  236. $mArg[$sNewKey] = $mArg[$sKey];
  237. unset($mArg[$sKey]);
  238. $sKey = $sNewKey;
  239. }
  240. $this->argumentDecodeUTF8_mb_convert_encoding($mArg[$sKey]);
  241. }
  242. }
  243. else if (is_string($mArg))
  244. $mArg = mb_convert_encoding($mArg, $this->sCharacterEncoding, "UTF-8");
  245. }
  246. function argumentDecodeUTF8_utf8_decode(&$mArg)
  247. {
  248. if (is_array($mArg))
  249. {
  250. foreach (array_keys($mArg) as $sKey)
  251. {
  252. $sNewKey = $sKey;
  253. $this->argumentDecodeUTF8_utf8_decode($sNewKey);
  254. if ($sNewKey != $sKey)
  255. {
  256. $mArg[$sNewKey] = $mArg[$sKey];
  257. unset($mArg[$sKey]);
  258. $sKey = $sNewKey;
  259. }
  260. $this->argumentDecodeUTF8_utf8_decode($mArg[$sKey]);
  261. }
  262. }
  263. else if (is_string($mArg))
  264. $mArg = utf8_decode($mArg);
  265. }
  266. /*
  267. Constructor: xajaxArgumentManager
  268. Initializes configuration settings to their default values and reads
  269. the argument data from the GET or POST data.
  270. */
  271. function xajaxArgumentManager()
  272. {
  273. $this->aArgs = array();
  274. $this->bDecodeUTF8Input = false;
  275. $this->sCharacterEncoding = 'UTF-8';
  276. $this->nMethod = XAJAX_METHOD_UNKNOWN;
  277. $this->aSequence = array(
  278. '<'.'k'.'>' => array(array(
  279. '<'.'/k'.'>' => 4
  280. ), 3),
  281. '<'.'/k'.'>' => array(array(
  282. '<'.'v'.'>' => 3,
  283. '<'.'/e'.'>' => 4
  284. ), 4),
  285. '<'.'v'.'>' => array(array(
  286. '<'.'xjxobj'.'>' => 8,
  287. '<'.'/v'.'>' => 4
  288. ), 5),
  289. '<'.'/v'.'>' => array(array(
  290. '<'.'/e'.'>' => 4,
  291. '<'.'k'.'>' => 3
  292. ), 6),
  293. '<'.'e'.'>' => array(array(
  294. '<'.'k'.'>' => 3,
  295. '<'.'v'.'>' => 3,
  296. '<'.'/e'.'>' => 4
  297. ), 2),
  298. '<'.'/e'.'>' => array(array(
  299. '<'.'e'.'>' => 3,
  300. '<'.'/xjxobj'.'>' => 9
  301. ), 7),
  302. '<'.'xjxobj'.'>' => array(array(
  303. '<'.'e'.'>' => 3,
  304. '<'.'/xjxobj'.'>' => 9
  305. ), 1),
  306. '<'.'/xjxobj'.'>' => array(array(
  307. '<'.'/v'.'>' => 4
  308. ), 8),
  309. 'start' => array(array(
  310. '<'.'xjxobj'.'>' => 8
  311. ), 9)
  312. );
  313. if (isset($_POST['xjxargs'])) {
  314. $this->nMethod = XAJAX_METHOD_POST;
  315. $this->aArgs = $_POST['xjxargs'];
  316. } else if (isset($_GET['xjxargs'])) {
  317. $this->nMethod = XAJAX_METHOD_GET;
  318. $this->aArgs = $_GET['xjxargs'];
  319. }
  320. if (1 == get_magic_quotes_gpc())
  321. array_walk($this->aArgs, array(&$this, 'argumentStripSlashes'));
  322. array_walk($this->aArgs, array(&$this, 'argumentDecodeXML'));
  323. }
  324. /*
  325. Function: getInstance
  326. Returns:
  327. object - A reference to an instance of this class. This function is
  328. used to implement the singleton pattern.
  329. */
  330. function &getInstance()
  331. {
  332. static $obj;
  333. if (!$obj) {
  334. $obj = new xajaxArgumentManager();
  335. }
  336. return $obj;
  337. }
  338. /*
  339. Function: configure
  340. Accepts configuration settings from the main <xajax> object.
  341. Parameters:
  342. The <xajaxArgumentManager> tracks the following configuration settings:
  343. <decodeUTF8Input> - (boolean): See <xajaxArgumentManager->bDecodeUTF8Input>
  344. <characterEncoding> - (string): See <xajaxArgumentManager->sCharacterEncoding>
  345. */
  346. function configure($sName, $mValue)
  347. {
  348. if ('decodeUTF8Input' == $sName) {
  349. if (true === $mValue || false === $mValue)
  350. $this->bDecodeUTF8Input = $mValue;
  351. } else if ('characterEncoding' == $sName) {
  352. $this->sCharacterEncoding = $mValue;
  353. }
  354. }
  355. /*
  356. Function: getRequestMethod
  357. Returns the method that was used to send the arguments from the client.
  358. */
  359. function getRequestMethod()
  360. {
  361. return $this->nMethod;
  362. }
  363. /*
  364. Function: process
  365. Returns the array of arguments that were extracted and parsed from
  366. the GET or POST data.
  367. */
  368. function process()
  369. {
  370. if ($this->bDecodeUTF8Input)
  371. {
  372. $sFunction = '';
  373. if (function_exists('iconv'))
  374. $sFunction = "iconv";
  375. else if (function_exists('mb_convert_encoding'))
  376. $sFunction = "mb_convert_encoding";
  377. else if ($this->sCharacterEncoding == "ISO-8859-1")
  378. $sFunction = "utf8_decode";
  379. else {
  380. $objLanguageManager =& xajaxLanguageManager::getInstance();
  381. trigger_error(
  382. $objLanguageManager->getText('ARGMGR:ERR:03')
  383. , E_USER_NOTICE
  384. );
  385. }
  386. $mFunction = array(&$this, 'argumentDecodeUTF8_' . $sFunction);
  387. array_walk($this->aArgs, $mFunction);
  388. $this->bDecodeUTF8Input = false;
  389. }
  390. return $this->aArgs;
  391. }
  392. }