PageRenderTime 58ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/components/charcoal/form/FormTokenComponent.class.php

https://bitbucket.org/stk2k/charcoalphp2.1
PHP | 187 lines | 109 code | 29 blank | 49 comment | 15 complexity | 6f1c0799c70fa5cb5b5d193fb070cb19 MD5 | raw file
  1. <?php
  2. /**
  3. * Form Token Component
  4. *
  5. * PHP version 5
  6. *
  7. * @package components.charcoal.http
  8. * @author CharcoalPHP Development Team
  9. * @copyright 2008 - 2013 CharcoalPHP Development Team
  10. */
  11. require_once( 'FormTokenComponentException' . CHARCOAL_CLASS_FILE_SUFFIX );
  12. require_once( 'FormTokenValidationException' . CHARCOAL_CLASS_FILE_SUFFIX );
  13. class Charcoal_FormTokenComponent extends Charcoal_CharcoalComponent implements Charcoal_IComponent
  14. {
  15. private $_token_key;
  16. private $_debug_mode;
  17. private $_token_generator;
  18. /*
  19. * Construct object
  20. */
  21. public function __construct()
  22. {
  23. parent::__construct();
  24. }
  25. /*
  26. * Initialize instance
  27. *
  28. * @param Charcoal_Config $config configuration data
  29. */
  30. public function configure( Charcoal_Config $config )
  31. {
  32. log_debug( "debug", "config: " . print_r($config,true) );
  33. $this->_token_key = $config->getString( s('token_key'), s('charcoal_token_key') )->getValue();
  34. $this->_debug_mode = $config->getBoolean( s('debug_mode'), b(FALSE) )->getValue();
  35. $this->_token_generator = $config->getString( s('token_generator'), s('simple') )->getValue();
  36. log_debug( "debug", "token key: {$this->_token_key}" );
  37. log_debug( "debug", "debug mode: {$this->_debug_mode}" );
  38. log_debug( "debug", "token generator: {$this->_token_generator}" );
  39. $this->_token_generator = Charcoal_Factory::createObject( s($this->_token_generator), s('token_generator') );
  40. }
  41. /*
  42. * Set token generator
  43. *
  44. * @param Charcoal_Config $config configuration data
  45. */
  46. public function setTokenGenerator( Charcoal_ITokenGenerator $token_generator )
  47. {
  48. $this->_token_generator = $token_generator;
  49. }
  50. /*
  51. * Get token generator
  52. *
  53. * @param Charcoal_Config $config configuration data
  54. */
  55. public function getTokenGenerator()
  56. {
  57. return $this->_token_generator;
  58. }
  59. /*
  60. * generate token
  61. *
  62. * @param Charcoal_ISequence $sequence Request object
  63. */
  64. public function generate( Charcoal_ISequence $sequence )
  65. {
  66. try{
  67. $token_key = $this->_token_key;
  68. // get token container from session.
  69. $token_list = $sequence->get( s($token_key) );
  70. if ( $token_list === NULL || !is_array($token_list) ){
  71. $token_list = array();
  72. }
  73. // Generate token
  74. $new_token = $this->_token_generator->generateToken();
  75. if ( $this->_debug_mode ){
  76. ad($new_token,array('title'=>"token generated","type"=>"div"));
  77. }
  78. log_debug( "debug", "token generated: $new_token" );
  79. // add new token to token list.
  80. $token_list[] = $new_token;
  81. if ( $this->_debug_mode ){
  82. ad($token_list,array('title'=>"token list"));
  83. }
  84. // save token list in sequence.
  85. $sequence->set( s($token_key), $token_list );
  86. log_debug( "debug", "sequence: " . print_r($sequence,true) );
  87. return $new_token;
  88. }
  89. catch( Exception $e )
  90. {
  91. _catch( $e );
  92. _throw( new Charcoal_FormTokenComponentException( s(__CLASS__.'#'.__METHOD__.' failed.'), $e ) );
  93. }
  94. }
  95. /*
  96. * validate token in request and sequence
  97. *
  98. * @param Charcoal_ISequence $sequence Sequence object
  99. * @param Charcoal_String $form_token Form token
  100. */
  101. public function validate( Charcoal_ISequence $sequence, Charcoal_String $form_token )
  102. {
  103. log_debug( "debug", "sequence: " . print_r($sequence,true) );
  104. log_debug( "debug", "form_token: " . print_r($form_token,true) );
  105. if ( $this->_debug_mode ){
  106. ad($sequence,array('title'=>"sequence"));
  107. ad($request,array('title'=>"request"));
  108. }
  109. $token_key = $this->_token_key;
  110. log_debug( "debug", "token_key: " . print_r($token_key,true) );
  111. if ( $this->_debug_mode ){
  112. ad($token_key,array('title'=>"token_key","type"=>"div"));
  113. }
  114. // get token container from session.
  115. $token_list = $sequence->get( s($token_key) );
  116. if ( $this->_debug_mode ){
  117. ad($token_list,array('title'=>"token list"));
  118. }
  119. log_debug( "debug", "token_list: " . print_r($token_list,true) );
  120. if ( $token_list === NULL || !is_array($token_list) ){
  121. $token_list = array();
  122. }
  123. // find token from token list.
  124. $token_index = NULL;
  125. foreach( $token_list as $idx => $token ){
  126. log_info( "debug", "token: $token" );
  127. if ( $this->_debug_mode ){
  128. ad($token,array('title'=>"token","type"=>"div"));
  129. }
  130. if ( $token == $form_token ){
  131. $token_index = $idx;
  132. break;
  133. }
  134. }
  135. if ( $token_index === NULL ){
  136. // illegal access
  137. log_warning( "system, debug", "token not found: $form_token" );
  138. if ( $this->_debug_mode ){
  139. ad($form_token,array('title'=>"token not found","type"=>"div"));
  140. }
  141. _throw( new Charcoal_FormTokenValidationException( s('token not found in sequence:'.$form_token) ) );
  142. }
  143. else{
  144. // authorized access
  145. log_debug( "debug", "token accepted: $form_token" );
  146. if ( $this->_debug_mode ){
  147. ad($form_token,array('title'=>"token accepted","type"=>"div"));
  148. }
  149. // erase token from token list to prevent duplicate form submission.
  150. unset( $token_list[$token_index] );
  151. }
  152. // update token list in sequence.
  153. $sequence->set( s($token_key), $token_list );
  154. // the event was successfully processed.
  155. return b(TRUE);
  156. }
  157. }
  158. return __FILE__;