token.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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
  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. * Called when there are no references to a particular object
  38. * unset the variables used in the class
  39. */
  40. public function __destruct() {
  41. foreach ($this as $key => $value) {
  42. unset($this->$key);
  43. }
  44. }
  45. /**
  46. * Create the token
  47. * @var string $key
  48. */
  49. public function create($key) {
  50. //allow only specific characters
  51. $key = preg_replace('[^a-zA-Z0-9\-_@.\/]', '', $key);
  52. //create a token and save in the token session array
  53. $_SESSION['tokens'][$key]['name'] = hash_hmac('sha256', $key, bin2hex(random_bytes(32)));
  54. $_SESSION['tokens'][$key]['hash'] = hash_hmac('sha256', $key, bin2hex(random_bytes(32)));
  55. //send the hash
  56. return $_SESSION['tokens'][$key];
  57. }
  58. /**
  59. * validate the token
  60. * @var string $key
  61. */
  62. public function validate($key, $value = null) {
  63. //allow only specific characters
  64. $key = preg_replace('[^a-zA-Z0-9]', '', $key);
  65. //get the token name
  66. $token_name = $_SESSION['tokens'][$key]['name'];
  67. if (isset($_REQUEST[$token_name])) {
  68. $value = $_REQUEST[$token_name];
  69. }
  70. else {
  71. $value;
  72. }
  73. //limit the value to specific characters
  74. $value = preg_replace('[^a-zA-Z0-9]', '', $value);
  75. //compare the hashed tokens
  76. if (hash_equals($_SESSION['tokens'][$key]['hash'], $value)) {
  77. return true;
  78. }
  79. else {
  80. return false;
  81. }
  82. }
  83. }
  84. /*
  85. //create token
  86. $object = new token;
  87. $token = $object->create('/app/bridges/bridge_edit.php');
  88. echo " <input type='hidden' name='".$token['name']."' value='".$token['hash']."'>\n";
  89. //------------------------
  90. //validate the token
  91. $token = new token;
  92. if (!$token->validate('/app/bridges/bridge_edit.php')) {
  93. $_SESSION["message"] = $text['message-invalid_token'];
  94. header('Location: bridges.php');
  95. exit;
  96. }
  97. */
  98. ?>