token.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /*
  3. FusionPBX
  4. Version: MPL 1.1
  5. The contents of this file are subject to the Mozilla Public License Version
  6. 1.1 (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.mozilla.org/MPL/
  9. Software distributed under the License is distributed on an "AS IS" basis,
  10. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. for the specific language governing rights and limitations under the
  12. License.
  13. The Original Code is FusionPBX
  14. The Initial Developer of the Original Code is
  15. Mark J Crane <[email protected]>
  16. Portions created by the Initial Developer are Copyright (C) 2019-2020
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. */
  21. /**
  22. * captcha class
  23. *
  24. * @method string get
  25. */
  26. class token {
  27. /**
  28. * Called when the object is created
  29. */
  30. //public $code;
  31. /**
  32. * Class constructor
  33. */
  34. public function __construct() {
  35. }
  36. /**
  37. * Create the token
  38. * @var string $key
  39. */
  40. public function create($key) {
  41. //clear previously validated tokens
  42. $this->clear_validated();
  43. //allow only specific characters
  44. $key = preg_replace('[^a-zA-Z0-9\-_@.\/]', '', $key);
  45. //create a token for the key submitted
  46. $token = [
  47. 'name'=>hash_hmac('sha256', $key, bin2hex(random_bytes(32))),
  48. 'hash'=>hash_hmac('sha256', $key, bin2hex(random_bytes(32))),
  49. 'validated'=>false
  50. ];
  51. //save in the token session array
  52. $_SESSION['tokens'][$key][] = $token;
  53. //send the hash
  54. return $token;
  55. }
  56. /**
  57. * validate the token
  58. * @var string $key
  59. * @var string $value
  60. */
  61. public function validate($key, $value = '') {
  62. //allow only specific characters
  63. $key = preg_replace('[^a-zA-Z0-9]', '', $key);
  64. //get the token name
  65. if (!empty($_SESSION['tokens']) && is_array($_SESSION['tokens'][$key]) && @sizeof($_SESSION['tokens'][$key]) != 0) {
  66. foreach ($_SESSION['tokens'][$key] as $t => $token) {
  67. $token_name = $token['name'];
  68. if (isset($_REQUEST[$token_name])) {
  69. $value = $_REQUEST[$token_name];
  70. break;
  71. }
  72. }
  73. }
  74. //limit the value to specific characters
  75. $value = preg_replace('[^a-zA-Z0-9]', '', $value);
  76. //compare the hashed tokens
  77. if (!empty($_SESSION['tokens']) && is_array($_SESSION['tokens'][$key]) && @sizeof($_SESSION['tokens'][$key]) != 0) {
  78. foreach ($_SESSION['tokens'][$key] as $t => $token) {
  79. if (hash_equals($token['hash'], $value)) {
  80. $_SESSION['tokens'][$key][$t]['validated'] = true;
  81. return true;
  82. }
  83. }
  84. }
  85. return false;
  86. }
  87. /**
  88. * clear previously validated tokens
  89. */
  90. private function clear_validated() {
  91. if (!empty($_SESSION['tokens']) && is_array($_SESSION['tokens']) && @sizeof($_SESSION['tokens']) != 0) {
  92. foreach ($_SESSION['tokens'] as $key => $tokens) {
  93. if (is_array($tokens) && @sizeof($tokens) != 0) {
  94. foreach ($tokens as $t => $token) {
  95. if ($token['validated']) {
  96. unset($_SESSION['tokens'][$key][$t]);
  97. }
  98. }
  99. }
  100. }
  101. }
  102. }
  103. }
  104. /*
  105. //create token
  106. $object = new token;
  107. $token = $object->create('/app/bridges/bridge_edit.php');
  108. echo " <input type='hidden' name='".$token['name']."' value='".$token['hash']."'>\n";
  109. //------------------------
  110. //validate the token
  111. $token = new token;
  112. if (!$token->validate('/app/bridges/bridge_edit.php')) {
  113. $_SESSION["message"] = $text['message-invalid_token'];
  114. header('Location: bridges.php');
  115. exit;
  116. }
  117. //note: can use $_SERVER['PHP_SELF'] instead of actual file path
  118. */
  119. ?>