PageRenderTime 59ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/FormProcessor.php

https://bitbucket.org/afrosoft/form-manager
PHP | 148 lines | 110 code | 31 blank | 7 comment | 24 complexity | 5bf264bd6e56194487a772f98ae0a8dc MD5 | raw file
  1. <?php
  2. /*
  3. * To change this template, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. class FormStatus {
  7. const VALID = 1;
  8. const INVALID = 2;
  9. const UNSURE = 3;
  10. }
  11. /**
  12. * The class that processes form.
  13. */
  14. class FormProcessor {
  15. private $raw = array();
  16. public $processed = array();
  17. private $secret = null;
  18. private $captcha = 'CAPTCHA PRIVATE KEY HERE';
  19. private $valid = null;
  20. public function __construct($secret) {
  21. $this->secret = $secret;
  22. }
  23. public function pipe($raw) {
  24. $this->raw = array_merge($this->raw, $raw);
  25. return $this;
  26. }
  27. public function valid() {
  28. if (isset($this->valid)) {
  29. return $this->valid;
  30. } else {
  31. $this->process();
  32. return $this->valid();
  33. }
  34. }
  35. public function process($alloted_time = 900000, $kill = false) {
  36. require_once 'Encryption\TW_Encryption.php';
  37. $crypt = new TW_Encryption();
  38. if (!isset($this->raw['spinner'])) {
  39. $this->valid = FormStatus::INVALID;
  40. return false;
  41. }
  42. $timestamp_label = $this->_hash_name('timestamp', $this->raw['spinner']);
  43. if (!isset($this->raw[$timestamp_label])) {
  44. $this->valid = FormStatus::INVALID;
  45. return false;
  46. }
  47. if (($this->raw[$timestamp_label] + $alloted_time) < time()) {
  48. if ($kill) {
  49. $this->valid = FormStatus::INVALID;
  50. return false;
  51. }
  52. $this->valid = FormStatus::UNSURE;
  53. }
  54. $calc_spinner = $this->_calculate_spinner($this->raw[$timestamp_label]);
  55. if ($calc_spinner != $this->raw['spinner']) {
  56. $this->valid = FormStatus::INVALID;
  57. return false;
  58. }
  59. if (isset($this->raw['recaptcha_challenge_field']) && isset($this->raw['recaptcha_challenge_field'])) {
  60. if (!$this->_check_recaptcha($this->raw['recaptcha_challenge_field'], $this->raw['recaptcha_response_field'])) {
  61. $this->valid = FormStatus::INVALID;
  62. return false;
  63. }
  64. }
  65. foreach($this->raw as $key => $value) {
  66. if ($key == $timestamp_label || $key == 'spinner' || $key == 'recaptcha_challenge_field' || $key == 'recaptcha_response_field') {
  67. continue;
  68. }
  69. $pKey = $this->_unhash_name($key, $this->raw['spinner']);
  70. $this->processed[$pKey] = $value;
  71. }
  72. $this->valid = FormStatus::VALID;
  73. return true;
  74. }
  75. private function _calculate_spinner($timestamp) {
  76. return hash('sha256', $timestamp . $this->secret);
  77. }
  78. function _hash_name($name, $spinner) {
  79. require_once 'Encryption\TW_Encryption.php';
  80. $array = false;
  81. if (strstr($name, '[]')) {
  82. $array = true;
  83. }
  84. $crypt = new TW_Encryption();
  85. $encoded = $this->_encode_name($crypt->encrypt($spinner, $name . $this->secret));
  86. return $encoded;
  87. }
  88. private function _encode_name($name) {
  89. return strtr(base64_encode($name), array('+' => '-', '/' => '_', '=' => ''));
  90. }
  91. private function _unhash_name($hash, $spinner) {
  92. require_once 'Encryption\TW_Encryption.php';
  93. $crypt = new TW_Encryption();
  94. if (strstr($hash, '_x')) {
  95. return strtr($crypt->decrypt($spinner, substr($this->_decode_name($hash), 0, -2)), array($this->secret => '')) . '_x';
  96. }
  97. if (strstr($hash, '_y')) {
  98. return strtr($crypt->decrypt($spinner, substr($this->_decode_name($hash), 0, -2)), array($this->secret => '')) . '_y';
  99. }
  100. return strtr($crypt->decrypt($spinner, $this->_decode_name($hash)), array($this->secret => ''));
  101. }
  102. private function _decode_name($name) {
  103. if(strstr($name, '_x')) {
  104. return base64_decode(str_pad(strtr(substr($name, 0, -2), array('-' => '+', '_' => '/')), strlen($name) % 4, '=')) . '_x';
  105. }
  106. if(strstr($name, '_y')) {
  107. return base64_decode(str_pad(strtr(substr($name, 0, -2), array('-' => '+', '_' => '/')), strlen($name) % 4, '=')) . '_y';
  108. }
  109. return base64_decode(str_pad(strtr($name, array('-' => '+', '_' => '/')), strlen($name) % 4, '='));
  110. }
  111. private function _check_recaptcha($challenge, $response) {
  112. require_once 'ReCAPTCHA\recaptchalib.php';
  113. $resp = recaptcha_check_answer ($this->captcha, $_SERVER["REMOTE_ADDR"], $challenge, $response);
  114. return $resp->is_valid;
  115. }
  116. }
  117. ?>