/includes/formvars.php

https://github.com/craigk5n/webcalendar · PHP · 203 lines · 95 code · 13 blank · 95 comment · 23 complexity · f64dcb2f6b67522c799451f50de3ddeb MD5 · raw file

  1. <?php
  2. /**
  3. * WebCalendar's functions to retrieve Predefined Variables
  4. *
  5. * See http://www.php.net/manual/en/reserved.variables.php
  6. * for a complete description and examples
  7. *
  8. * @author Craig Knudsen <cknudsen@cknudsen.com>
  9. * @copyright Craig Knudsen, <cknudsen@cknudsen.com>, http://www.k5n.us/cknudsen
  10. * @license http://www.gnu.org/licenses/gpl.html GNU GPL
  11. * @package WebCalendar
  12. */
  13. /**
  14. * This function examines the data for a form POST or GET to check
  15. * for malicious hacks. If one is found, we just exit since this
  16. * should not happen with normal use.
  17. */
  18. function preventHacking_helper($matches) {
  19. return chr(hexdec($matches[1]));
  20. }
  21. function preventHacking ( $name, $instr ) {
  22. $bannedTags = [
  23. 'APPLET', 'BODY', 'EMBED', 'FORM', 'HEAD',
  24. 'HTML', 'IFRAME', 'LINK', 'META', 'NOEMBED',
  25. 'NOFRAMES', 'NOSCRIPT', 'OBJECT', 'SCRIPT'];
  26. $failed = false;
  27. if ( is_array ( $instr ) ) {
  28. for ( $j = 0; $j < count ( $instr ); $j++ ) {
  29. // First, replace any escape characters like '\x3c'
  30. $teststr = preg_replace_callback("#(\\\x[0-9A-F]{2})#i",
  31. 'preventHacking_helper', $instr[$j]);
  32. for ( $i = 0; $i < count ( $bannedTags ) && ! $failed; $i++ ) {
  33. if ( preg_match ( "/<\s*$bannedTags[$i]/i", $teststr ) ) {
  34. $failed = true;
  35. }
  36. }
  37. }
  38. if ( $failed ) {
  39. die_miserable_death ( translate ( 'Fatal Error' ) . ': '
  40. . translate ( 'Invalid data format for' ) . ' ' . $name );
  41. }
  42. } else {
  43. // Not an array
  44. // First, replace any escape characters like '\x3c'
  45. $teststr = preg_replace_callback("#(\\\x[0-9A-F]{2})#i",
  46. 'preventHacking_helper', $instr);
  47. for ( $i = 0; $i < count ( $bannedTags ) && ! $failed; $i++ ) {
  48. if ( preg_match ( "/<\s*$bannedTags[$i]/i", $teststr ) ) {
  49. $failed = true;
  50. }
  51. }
  52. if ( $failed ) {
  53. die_miserable_death ( translate ( 'Fatal Error' ) . ': '
  54. . translate ( 'Invalid data format for' ) . ' ' . $name );
  55. }
  56. }
  57. }
  58. /**
  59. * Gets the value resulting from an HTTP POST method.
  60. *
  61. * @param string $name Name used in the HTML form
  62. * @param string $defVal Value to return if form field is empty
  63. * @param string $chkXSS Switch to control XSS checking
  64. *
  65. * @return string The value used in the HTML form
  66. *
  67. * @see getGetValue
  68. */
  69. function getPostValue($name, $defVal = NULL, $chkXSS = false)
  70. {
  71. $postName = $defVal;
  72. if (isset($_POST) && is_array($_POST) && isset($_POST[$name])) {
  73. $postName =
  74. (is_array($_POST[$name]) ? array_map('addslashes', $_POST[$name]) :
  75. addslashes($_POST[$name]));
  76. }
  77. $cleanXSS = $chkXSS ? chkXSS($postName) : true;
  78. preventHacking($name, $postName);
  79. return $cleanXSS ? $postName : NULL;
  80. }
  81. /**
  82. * Gets the value resulting from an HTTP GET method.
  83. *
  84. * Since this function is used in more than one place, with different names,
  85. * let's make it a separate 'include' file on it's own.
  86. *
  87. * If you need to enforce a specific input format (such as numeric input), then
  88. * use the {@link getValue()} function.
  89. *
  90. * @param string $name Name used in the HTML form or found in the URL
  91. *
  92. * @return string The value used in the HTML form (or URL)
  93. *
  94. * @see getPostValue
  95. */
  96. function getGetValue($name)
  97. {
  98. $getName = null;
  99. if (isset($_GET) && is_array($_GET) && isset($_GET[$name])) {
  100. $getName = is_array($_GET[$name]) ? array_map('addslashes', $_GET[$name]) :
  101. addslashes($_GET[$name]);
  102. }
  103. preventHacking($name, $getName);
  104. return $getName;
  105. }
  106. /**
  107. * Gets the value resulting from either HTTP GET method or HTTP POST method.
  108. *
  109. * <b>Note:</b> If you need to get an integer value, you can use the
  110. * getIntValue function.
  111. *
  112. * @param string $name Name used in the HTML form or found in the URL
  113. * @param string $format A regular expression format that the input must match.
  114. * If the input does not match, an empty string is
  115. * returned and a warning is sent to the browser. If The
  116. * <var>$fatal</var> parameter is true, then execution
  117. * will also stop when the input does not match the
  118. * format.
  119. * @param bool $fatal Is it considered a fatal error requiring execution to
  120. * stop if the value retrieved does not match the format
  121. * regular expression?
  122. *
  123. * @return string The value used in the HTML form (or URL)
  124. *
  125. * @uses getGetValue
  126. * @uses getPostValue
  127. */
  128. function getValue($name, $format = '', $fatal = false)
  129. {
  130. $val = getPostValue($name);
  131. if (!isset($val))
  132. $val = getGetValue($name);
  133. if (!isset($val))
  134. return '';
  135. if (!empty($format) && !preg_match('/^' . $format . '$/', $val)) {
  136. // does not match
  137. if ($fatal) {
  138. die_miserable_death(translate('Fatal Error') . ': '
  139. . translate('Invalid data format for') . $name);
  140. }
  141. // ignore value
  142. return '';
  143. }
  144. preventHacking($name, $val);
  145. return $val;
  146. }
  147. /**
  148. * Gets an integer value resulting from an HTTP GET or HTTP POST method.
  149. *
  150. * @param string $name Name used in the HTML form or found in the URL
  151. * @param bool $fatal Is it considered a fatal error requiring execution to
  152. * stop if the value retrieved does not match the format
  153. * regular expression?
  154. *
  155. * @return string The value used in the HTML form (or URL)
  156. *
  157. * @uses getValue
  158. */
  159. function getIntValue($name, $fatal = false) {
  160. return getValue($name, '-?[0-9]+', $fatal);
  161. }
  162. /**
  163. * Checks string for certain XSS attack strings.
  164. *
  165. *
  166. * @param string $name Name used in the HTML form or found in the URL
  167. * @param bool $fatal Is it considered a fatal error requiring execution to
  168. * stop if the value retrieved does not match the format
  169. * regular expression?
  170. *
  171. * @return string The value used in the HTML form (or URL)
  172. *
  173. * @uses getValue
  174. */
  175. function chkXSS($name) {
  176. global $login;
  177. $cleanXSS = true;
  178. //add more array elements as needed
  179. foreach (array(
  180. 'Ajax.Request',
  181. 'onerror') as $i) {
  182. if (preg_match("/$i/i", $name)) {
  183. activity_log(0, $login, $login, SECURITY_VIOLATION,
  184. 'Hijack attempt:' . $i);
  185. $cleanXSS = false;
  186. }
  187. }
  188. return $cleanXSS;
  189. }
  190. ?>