PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/MantisBT/core/form_api.php

https://bitbucket.org/crypticrod/sr_wp_code
PHP | 203 lines | 87 code | 37 blank | 79 comment | 33 complexity | 41bf04892699d1fd3047ac7f8a625711 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1, GPL-3.0, LGPL-2.0, AGPL-3.0
  1. <?php
  2. # MantisBT - a php based bugtracking system
  3. # MantisBT is free software: you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation, either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # MantisBT is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with MantisBT. If not, see <http://www.gnu.org/licenses/>.
  15. /**
  16. * Form API for handling tasks necessary to form security and validation.
  17. * Security methods are targetted to work with both GET and POST form types,
  18. * and should allow multiple simultaneous edits of the form to be submitted.
  19. *
  20. * @package CoreAPI
  21. * @subpackage FormAPI
  22. * @copyright Copyright (C) 2000 - 2002 Kenzaburo Ito - kenito@300baud.org
  23. * @copyright Copyright (C) 2002 - 2011 MantisBT Team - mantisbt-dev@lists.sourceforge.net
  24. * @link http://www.mantisbt.org
  25. *
  26. * @uses session_api.php
  27. */
  28. /**
  29. * Helper function to generate a form action value when forms are designed
  30. * to be submitted to the same url that's is currently being used, such as
  31. * helper_ensure_confirmed() or auth_reauthenticate().
  32. * @return string Form action value
  33. */
  34. function form_action_self() {
  35. $t_self = trim( str_replace( "\0", '', $_SERVER['SCRIPT_NAME'] ) );
  36. return basename( $t_self );
  37. }
  38. /**
  39. * Generate a random security token, prefixed by date, store it in the
  40. * user's session, and then return the string to be used as a form element
  41. * element with the security token as the value.
  42. * @param string Form name
  43. * @return string Security token string
  44. */
  45. function form_security_token( $p_form_name ) {
  46. if ( PHP_CLI == php_mode() || OFF == config_get_global( 'form_security_validation' ) ) {
  47. return '';
  48. }
  49. $t_tokens = session_get( 'form_security_tokens', array() );
  50. # Create a new array for the form name if necessary
  51. if( !isset( $t_tokens[$p_form_name] ) || !is_array( $t_tokens[$p_form_name] ) ) {
  52. $t_tokens[$p_form_name] = array();
  53. }
  54. # Generate a random security token prefixed by date.
  55. # mt_rand() returns an int between 0 and RAND_MAX as extra entropy
  56. $t_date = date( 'Ymd' );
  57. $t_string = $t_date . sha1( time() . mt_rand() );
  58. # Add the token to the user's session
  59. if ( !isset( $t_tokens[$p_form_name][$t_date] ) ) {
  60. $t_tokens[$p_form_name][$t_date] = array();
  61. }
  62. $t_tokens[$p_form_name][$t_date][$t_string] = true;
  63. session_set( 'form_security_tokens', $t_tokens );
  64. # The token string
  65. return $t_string;
  66. }
  67. /**
  68. * Get a hidden form element containing a generated form security token.
  69. * @param string Form name
  70. * @return string Hidden form element to output
  71. */
  72. function form_security_field( $p_form_name ) {
  73. if ( PHP_CLI == php_mode() || OFF == config_get_global( 'form_security_validation' ) ) {
  74. return '';
  75. }
  76. $t_string = form_security_token( $p_form_name );
  77. # Create the form element HTML string for the security token
  78. $t_form_token = $p_form_name . '_token';
  79. $t_element = '<input type="hidden" name="%s" value="%s"/>';
  80. $t_element = sprintf( $t_element, $t_form_token, $t_string );
  81. return $t_element;
  82. }
  83. /**
  84. * Get a URL parameter containing a generated form security token.
  85. * @param string Form name
  86. * @return string Hidden form element to output
  87. */
  88. function form_security_param( $p_form_name ) {
  89. if ( PHP_CLI == php_mode() || OFF == config_get_global( 'form_security_validation' ) ) {
  90. return '';
  91. }
  92. $t_string = form_security_token( $p_form_name );
  93. # Create the GET parameter to be used in a URL for a secure link
  94. $t_form_token = $p_form_name . '_token';
  95. $t_param = '&%s=%s';
  96. $t_param = sprintf( $t_param, $t_form_token, $t_string );
  97. return $t_param;
  98. }
  99. /**
  100. * Validate the security token for the given form name based on tokens
  101. * stored in the user's session. While checking stored tokens, any that
  102. * are more than 3 days old will be purged.
  103. * @param string Form name
  104. * @return boolean Form is valid
  105. */
  106. function form_security_validate( $p_form_name ) {
  107. if ( PHP_CLI == php_mode() || OFF == config_get_global( 'form_security_validation' ) ) {
  108. return true;
  109. }
  110. $t_tokens = session_get( 'form_security_tokens', array() );
  111. # Short-circuit if we don't have any tokens for the given form name
  112. if( !isset( $t_tokens[$p_form_name] ) || !is_array( $t_tokens[$p_form_name] ) || count( $t_tokens[$p_form_name] ) < 1 ) {
  113. trigger_error( ERROR_FORM_TOKEN_INVALID, ERROR );
  114. return false;
  115. }
  116. # Get the form input
  117. $t_form_token = $p_form_name . '_token';
  118. $t_input = gpc_get_string( $t_form_token, '' );
  119. # No form input
  120. if( '' == $t_input ) {
  121. trigger_error( ERROR_FORM_TOKEN_INVALID, ERROR );
  122. return false;
  123. }
  124. # Get the date claimed by the token
  125. $t_date = utf8_substr( $t_input, 0, 8 );
  126. # Check if the token exists
  127. if ( isset( $t_tokens[$p_form_name][$t_date][$t_input] ) ) {
  128. return true;
  129. }
  130. # Token does not exist
  131. trigger_error( ERROR_FORM_TOKEN_INVALID, ERROR );
  132. return false;
  133. }
  134. /**
  135. * Purge form security tokens that are older than 3 days, or used
  136. * for form validation.
  137. * @param string Form name
  138. */
  139. function form_security_purge( $p_form_name ) {
  140. if ( PHP_CLI == php_mode() || OFF == config_get_global( 'form_security_validation' ) ) {
  141. return;
  142. }
  143. $t_tokens = session_get( 'form_security_tokens', array() );
  144. # Short-circuit if we don't have any tokens for the given form name
  145. if( !isset( $t_tokens[$p_form_name] ) || !is_array( $t_tokens[$p_form_name] ) || count( $t_tokens[$p_form_name] ) < 1 ) {
  146. return;
  147. }
  148. # Get the form input
  149. $t_form_token = $p_form_name . '_token';
  150. $t_input = gpc_get_string( $t_form_token, '' );
  151. # Get the date claimed by the token
  152. $t_date = utf8_substr( $t_input, 0, 8 );
  153. # Generate a date string of three days ago
  154. $t_purge_date = date( 'Ymd', time() - ( 3 * 24 * 60 * 60 ) );
  155. # Purge old token data, and the currently-used token
  156. unset( $t_tokens[$p_form_name][$t_date][$t_input] );
  157. foreach( $t_tokens as $t_form_name => $t_dates ) {
  158. foreach( $t_dates as $t_date => $t_date_tokens ) {
  159. if ( $t_date < $t_purge_date ) {
  160. unset( $t_tokens[$t_form_name][$t_date] );
  161. }
  162. }
  163. }
  164. session_set( 'form_security_tokens', $t_tokens );
  165. return;
  166. }