PageRenderTime 37ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Branch_4_6-mysql/gforge/common/include/escapingUtils.php

https://gitlab.com/oslc-cm-server/olbergers-ff5-oslc
PHP | 367 lines | 134 code | 33 blank | 200 comment | 17 complexity | 280afba4e012338ba2c72b934296b7ee MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /**
  3. * GForge escaping library
  4. *
  5. * Copyright 2003-2004 Guillaume Smet
  6. * http://gforge.org/
  7. *
  8. * @version $Id$
  9. *
  10. * This file is part of GForge.
  11. *
  12. * GForge is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * GForge is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with GForge; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. */
  26. // PHP only adds the SuperGlobals to the GLOBALS array on the first reference.
  27. // We detect their presence by checking GLOBALS. The following statements
  28. // ensure that the SuperGlobals are referenced if they exist.
  29. isset($_POST['x']);
  30. isset($_SERVER['x']);
  31. isset($_GET['x']);
  32. isset($_FILES['x']);
  33. isset($_COOKIE['x']);
  34. /**
  35. * getIntFromRequest - get an int from REQUEST
  36. *
  37. * @param string $key key of the wanted value
  38. * @param int $defaultValue if we can't find the wanted value, it returns the default value
  39. * @return int the value
  40. */
  41. function getIntFromRequest($key, $defaultValue = 0) {
  42. return _getIntFromArray(_getRequestArray(), $key, $defaultValue);
  43. }
  44. /**
  45. * getStringFromRequest - get a string from REQUEST
  46. *
  47. * @param string $key key of the wanted value
  48. * @param string $defaultValue if we can't find the wanted value, it returns the default value
  49. * @return string the value
  50. */
  51. function getStringFromRequest($key, $defaultValue = '') {
  52. return _getStringFromArray(_getRequestArray(), $key, $defaultValue);
  53. }
  54. /**
  55. * getArrayFromRequest - get an array from REQUEST
  56. * @param string $key Key of the wanted value
  57. * @param string $defaultValue if we can't find the wanted value, it returns the default value
  58. * @return array The value
  59. */
  60. function getArrayFromRequest($key, $defaultValue = array()) {
  61. return _getArrayFromArray(_getRequestArray(), $key, $defaultValue);
  62. }
  63. /**
  64. * getIntFromPost - get an int from POST
  65. *
  66. * @param string $key key of the wanted value
  67. * @param int $defaultValue if we can't find the wanted value, it returns the default value
  68. * @return int the value
  69. */
  70. function getIntFromPost($key, $defaultValue = 0) {
  71. return _getIntFromArray(_getPostArray(), $key, $defaultValue);
  72. }
  73. /**
  74. * getStringFromPost - get a string from POST
  75. *
  76. * @param string $key key of the wanted value
  77. * @param string $defaultValue if we can't find the wanted value, it returns the default value
  78. * @return string the value
  79. */
  80. function getStringFromPost($key, $defaultValue = '') {
  81. return _getStringFromArray(_getPostArray(), $key, $defaultValue);
  82. }
  83. /**
  84. * getIntFromGet - get an int from GET
  85. *
  86. * @param string $key key of the wanted value
  87. * @param int $defaultValue if we can't find the wanted value, it returns the default value
  88. * @return int the value
  89. */
  90. function getIntFromGet($key, $defaultValue = 0) {
  91. return _getIntFromArray(_getGetArray(), $key, $defaultValue);
  92. }
  93. /**
  94. * getStringFromGet - get a string from GET
  95. *
  96. * @param string $key key of the wanted value
  97. * @param string $defaultValue if we can't find the wanted value, it returns the default value
  98. * @return string the value
  99. */
  100. function getStringFromGet($key, $defaultValue = '') {
  101. return _getStringFromArray(_getGetArray(), $key, $defaultValue);
  102. }
  103. /**
  104. * getIntFromCookie - get an int set by a cookie
  105. *
  106. * @param string $key key of the wanted value
  107. * @param int $defaultValue if we can't find the wanted value, it returns the default value
  108. * @return int the value
  109. */
  110. function getIntFromCookie($key, $defaultValue = 0) {
  111. return _getIntFromArray(_getCookieArray(), $key, $defaultValue);
  112. }
  113. /**
  114. * getStringFromCookie - get a string set by a cookie
  115. *
  116. * @param string $key key of the wanted value
  117. * @param string $defaultValue if we can't find the wanted value, it returns the default value
  118. * @return string the value
  119. */
  120. function getStringFromCookie($key, $defaultValue = '') {
  121. return _getStringFromArray(_getCookieArray(), $key, $defaultValue);
  122. }
  123. /**
  124. * getUploadedFile - get the uploaded file information
  125. *
  126. * The returned array is in the format given by PHP, as described in
  127. * http://php.net/manual/en/features.file-upload.php
  128. *
  129. * If there was no such file upload control in form, empty array is
  130. * returned. If there was file upload control but no file was
  131. * entered, then $result['tmp_name'] is empty string.
  132. *
  133. * @param string name of the file
  134. * @return array uploaded file information
  135. */
  136. function getUploadedFile($key) {
  137. $filesArray = & _getFilesArray();
  138. if(isset($filesArray[$key])) {
  139. $result = $filesArray[$key];
  140. if ($result['tmp_name'] == 'none') {
  141. $result['tmp_name'] = '';
  142. }
  143. return $result;
  144. }
  145. else {
  146. return array();
  147. }
  148. }
  149. /**
  150. * getStringFromServer - get a string from Server environment
  151. *
  152. * @param string $key key of the wanted value
  153. * @return string the value
  154. */
  155. function getStringFromServer($key) {
  156. $serverArray = & _getServerArray();
  157. if(isset($serverArray[$key])) {
  158. return $serverArray[$key];
  159. }
  160. else {
  161. return '';
  162. }
  163. }
  164. /* private */
  165. /**
  166. * _getIntFromArray - get an int from an array
  167. *
  168. * @param array $array the array
  169. * @param string $key the key of the wanted value
  170. * @param int $defaultValue an int which is returned if we can't find the key in the array
  171. * @return int the wanted value
  172. */
  173. function _getIntFromArray(& $array, $key, $defaultValue = 0) {
  174. if(isset($array[$key]) && is_numeric($array[$key])) {
  175. return (int) $array[$key];
  176. }
  177. elseif(is_numeric($defaultValue)) {
  178. return (int) $defaultValue;
  179. }
  180. else {
  181. return 0;
  182. }
  183. }
  184. /**
  185. * _getStringFromArray - get a string from an array
  186. *
  187. * @param array $array the array
  188. * @param string $key the key of the wanted value
  189. * @param int $defaultValue an int which is returned if we can't find the key in the array
  190. * @return string the wanted value
  191. */
  192. function _getStringFromArray(& $array, $key, $defaultValue = '') {
  193. if(isset($array[$key])) {
  194. return $array[$key];
  195. }
  196. else {
  197. return $defaultValue;
  198. }
  199. }
  200. /**
  201. * _getArrayFromArray - get an array from another array
  202. *
  203. * @param array $array the array
  204. * @param string $key the key of the wanted value
  205. * @param int $defaultValue an array which is returned if we can't find the key in the array
  206. * @return string the wanted value
  207. */
  208. function _getArrayFromArray(& $array, $key, $defaultValue = array()) {
  209. if(isset($array[$key])) {
  210. return $array[$key];
  211. }
  212. else {
  213. return $defaultValue;
  214. }
  215. }
  216. /**
  217. * _getPredefinedArray - get one of the predefined array (GET, POST, COOKIE...)
  218. *
  219. * @param string $superGlobalName name of the super global array (_POST, _GET)
  220. * @param string $oldName name of the old array (HTTP_POST_VARS, HTTP_GET_VARS) for older php versions
  221. * @return array a predefined array
  222. */
  223. function & _getPredefinedArray($superGlobalName, $oldName) {
  224. if (isset($GLOBALS[$superGlobalName])) {
  225. $array = & $GLOBALS[$superGlobalName];
  226. } elseif (isset($GLOBALS[$oldName])) {
  227. $array = & $GLOBALS[$oldName];
  228. } else {
  229. $array = array();
  230. }
  231. return $array;
  232. }
  233. /**
  234. * _getRequestArray - wrapper to get the request array
  235. *
  236. * @return array the REQUEST array
  237. */
  238. function & _getRequestArray() {
  239. if(isset($_REQUEST)) {
  240. return $_REQUEST;
  241. } else {
  242. return array_merge($GLOBALS['HTTP_GET_VARS'], $GLOBALS['HTTP_POST_VARS'], $GLOBALS['HTTP_COOKIE_VARS']);
  243. }
  244. }
  245. /**
  246. * _getPostArray - wrapper to get the post array
  247. *
  248. * @return array the POST array
  249. */
  250. function & _getPostArray() {
  251. return _getPredefinedArray('_POST', 'HTTP_POST_VARS');
  252. }
  253. /**
  254. * _getPostArray - wrapper to get the GET array
  255. *
  256. * @return array the GET array
  257. */
  258. function & _getGetArray() {
  259. return _getPredefinedArray('_GET', 'HTTP_GET_VARS');
  260. }
  261. /**
  262. * _getFilesArray - wrapper to get the FILES array
  263. *
  264. * @return array the FILES array
  265. */
  266. function & _getFilesArray() {
  267. return _getPredefinedArray('_FILES', 'HTTP_POST_FILES');
  268. }
  269. /**
  270. * _getServerArray - wrapper to get the SERVER array
  271. *
  272. * @return array the SERVER array
  273. */
  274. function & _getServerArray() {
  275. return _getPredefinedArray('_SERVER', 'HTTP_SERVER_VARS');
  276. }
  277. /**
  278. * _getCookieArray - wrapper to get the post array
  279. *
  280. * @return array the COOKIE array
  281. */
  282. function & _getCookieArray() {
  283. return _getPredefinedArray('_COOKIE', 'HTTP_COOKIE_VARS');
  284. }
  285. /**
  286. * inputSpecialchars - escape a string which is in an input
  287. *
  288. * @param string $string string to escape
  289. * @return string escaped string
  290. */
  291. function inputSpecialchars($string) {
  292. return str_replace('"', '&quot;', $string);
  293. }
  294. /**
  295. * unInputSpecialchars - clean a string escaped with inputSpecialchars
  296. *
  297. * @param string $string escaped string
  298. * @return string clean string
  299. */
  300. function unInputSpecialchars($string) {
  301. return str_replace('&quot;', '"', $string);
  302. }
  303. /**
  304. * optionSpecialchars - escape a string which is in a <option>string</option>
  305. *
  306. * @param string $string string to escape
  307. * @return string escaped string
  308. */
  309. function optionSpecialchars($string) {
  310. return htmlSpecialchars($string);
  311. }
  312. $htmlTranslationTable = get_html_translation_table(HTML_SPECIALCHARS);
  313. unset($htmlTranslationTable['&']);
  314. /**
  315. * textareaSpecialchars - escape a string which is in a textarea
  316. *
  317. * @param string $string string to escape
  318. * @return string escaped string
  319. */
  320. function textareaSpecialchars($string) {
  321. global $htmlTranslationTable;
  322. return strtr($string, $htmlTranslationTable);
  323. }
  324. /**
  325. * unTextareaSpecialchars - clean a string escaped with textareaSpecialchars
  326. *
  327. * @param string $string escaped string
  328. * @return string clean string
  329. */
  330. function unTextareaSpecialchars($string) {
  331. global $htmlTranslationTable;
  332. return strtr($string, array_flip($htmlTranslationTable));
  333. }
  334. ?>