Advertisement
Kiporralixo

Untitled

Mar 29th, 2017
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. * newPHPClass_1 [ TIPO ]
  5. * Descricao
  6. * @author Luan Martins
  7. * @copyright (c) year, Luan Martins - LM Desenvolvimentos
  8. */
  9. // vou te msotrar oque acontece
  10. class Csrf {
  11. //inicio de codigo
  12. public function get_token_id(){
  13. if(isset($_SESSION['token_id'])){
  14. return $_SESSION['token_id'];
  15. }else{
  16. $token_id = $this->random(10);
  17. $_SESSION['token_id'] = $token_id;
  18. return $token_id;
  19. }
  20. }
  21.  
  22. public function get_token(){
  23. if(isset($_SESSION['token_value'])){
  24. return $_SESSION['token_value'];
  25. }else{
  26. $token = hash('sha256', $this->random(500));
  27. $_SESSION['token_value'] = $token;
  28. return $token;
  29. }
  30. }
  31.  
  32. public function check_valid($method) {
  33. if($method == 'post' || $method == 'get') {
  34. $post = $_POST;
  35. $get = $_GET;
  36. if(isset(${$method}[$this->get_token_id()]) && (${$method}[$this->get_token_id()] == $this->get_token())) {
  37. return true;
  38. } else {
  39. return false;
  40. }
  41. } else {
  42. return false;
  43. }
  44. }
  45. public function form_names($names, $regenerate) {
  46. $values = array();
  47. foreach ($names as $n) {
  48. if($regenerate == true) {
  49. unset($_SESSION[$n]);
  50. }
  51. $s = isset($_SESSION[$n]) ? $_SESSION[$n] : $this->random(10);
  52. $_SESSION[$n] = $s;
  53. $values[$n] = $s;
  54. }
  55. return $values;
  56. }
  57. private function random($len) {
  58. if (function_exists('openssl_random_pseudo_bytes')) {
  59. $byteLen = intval(($len / 2) + 1);
  60. $return = substr(bin2hex(openssl_random_pseudo_bytes($byteLen)), 0, $len);
  61. } elseif (@is_readable('/dev/urandom')) {
  62. $f=fopen('/dev/urandom', 'r');
  63. $urandom=fread($f, $len);
  64. fclose($f);
  65. $return = '';
  66. }
  67.  
  68. if (empty($return)) {
  69. for ($i=0;$i<$len;++$i) {
  70. if (!isset($urandom)) {
  71. if ($i%2==0) {
  72. mt_srand(time()%2147 * 1000000 + (double)microtime() * 1000000);
  73. }
  74. $rand=48+mt_rand()%64;
  75. } else {
  76. $rand=48+ord($urandom[$i])%64;
  77. }
  78.  
  79. if ($rand>57)
  80. $rand+=7;
  81. if ($rand>90)
  82. $rand+=6;
  83.  
  84. if ($rand==123) $rand=52;
  85. if ($rand==124) $rand=53;
  86. $return.=chr($rand);
  87. }
  88. }
  89.  
  90. return $return;
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement