Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Security.class.php 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. class Security {
  3. public $csrfTokenName;
  4. public $passwordMinLength;
  5. private $session;
  6. private $config;
  7. function __construct() {
  8. global $session, $config;
  9. $this->csrfTokenName = 'csrf_token';
  10. $this->session = $session;
  11. $this->config = $config;
  12. $this->passwordMinLength = $this->config['settings']['password-min-lenght'];
  13. }
  14. public function setCSRFToken() {
  15. //if ($this->session->sessionExists($this->csrfTokenName)) {
  16. if (function_exists('random_bytes')) { //Only PHP 7
  17. $this->session->refreshSession($this->csrfTokenName, bin2hex(random_bytes(32)));
  18. } else {
  19. $this->session->refreshSession($this->csrfTokenName, bin2hex(openssl_random_pseudo_bytes(32)));
  20. }
  21. //}
  22. return $this->session->getSessionValue($this->csrfTokenName);
  23. }
  24. public function getCSRFToken() {
  25. return $this->session->getSessionValue($this->csrfTokenName);
  26. }
  27. public function compareCSRFToken($token=null) {
  28. $sessionToken = $this->getCSRFToken();
  29. if ($sessionToken !== false) {
  30. return hash_equals($sessionToken, $token);
  31. }
  32. return false;
  33. }
  34. public function secureString($string, $action='e') {
  35. $output = false;
  36. $encryptMethod = "AES-256-CBC";
  37. $secretKey = $this->config['settings']['secret-key'];
  38. $secretIv = $this->config['settings']['secret-iv'];
  39. $key = hash('sha256', $secretKey);
  40. $iv = substr(hash('sha256', $secretIv), 0, 16);
  41. if ($action == 'e') {
  42. $output = openssl_encrypt($string, $encryptMethod, $key, 0, $iv);
  43. $output = base64_encode($output);
  44. } else if ($action == 'd') {
  45. $output = openssl_decrypt(base64_decode($string), $encryptMethod, $key, 0, $iv);
  46. }
  47. return $output;
  48. }
  49. public function validatePassword($password='') {
  50. if(!preg_match( '/[^A-Za-z0-9]+/', $password) || strlen($password) < $this->passwordMinLength) {
  51. return false;
  52. }
  53. return true;
  54. }
  55. public function getGUID() {
  56. if (function_exists('com_create_guid') === true) {
  57. return trim(com_create_guid(), '{}');
  58. }
  59. return sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535));
  60. }
  61. }