CSRFToken::buildSeed()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 6
crap 2
1
<?php
2
$defflip = (!cfip()) ? exit(header('HTTP/1.1 401 Unauthorized')) : 1;
3
4
class CSRFToken Extends Base {
5
  public $valid = 0;
6
  /**
7
   * Gets a basic csrf token
8
   * @param string $user user or IP/host address
9
   * @param string $type page name or other unique per-page identifier
10
   */
11
  public function getBasic($user, $type) {
12
    $date = date('m/d/y/H/i');
13
    $d = explode('/', $date);
14
    $seed = $this->buildSeed($user.$type, $d[0], $d[1], $d[2], $d[3], $d[4]);
15
    return $this->getHash($seed);
16
  }
17
  
18
  /**
19
   * Returns +1 min up to +15 min rollovers hashes
20
   * @param string $user user or IP/host address
21
   * @param string $type page name or other unique per-page identifier
22
   * @return array 1 minute ago up to 15 minute ago hashes
23
   */
24
  
25
  public function checkAdditional($user, $type) {
26
    $date = date('m/d/y/H/i');
27
    $d = explode('/', $date);
28
    $hashes = array();
29
    for ($x = 1; $x < 16; $x++){
30
        for ($y = 4;$d[$y]-- == 0;$y--);
31
        if ($d[4] < 0) { $d[4] = 59; }
32
        $hashes[$x-1] = $this->getHash($this->buildSeed($user.$type, $d[0], $d[1], $d[2], $d[3], $d[4]));
33
    }
34
    return $hashes;
35
  }
36
  
37
  /**
38
   * Builds a seed with the given data
39
   * @param string $data
40
   * @param int $year
41
   * @param int $month
42
   * @param int $day
43
   * @param int $hour
44
   * @param int $minute
45
   * @return string seed
46
   */
47
  private function buildSeed($data, $year, $month, $day, $hour, $minute) {
48
    return $this->salty.$year.$month.$day.$data.$hour.$minute.$this->salt;
0 ignored issues
show
Bug introduced by
The property salty does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
The property salt does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
49
  }
50
  
51
  /**
52
   * Checks if the token is correct as is, if not checks for rollovers with checkAdditional()
53
   * @param string $user user or IP/host address
54
   * @param string $type page name or other unique per-page identifier
55
   * @param string $token token to check against
56
   * @return boolean
57
   */
58
  public function checkBasic($user, $type, $token) {
59
    if (empty($token)) return false;
60
    $token_now = $this->getBasic($user, $type);
61
    if ($token_now !== $token) {
62
      $tokens_check = $this->checkAdditional($user, $type);
63
      $match = 0;
64
      foreach ($tokens_check as $checkit) {
65
        if ($checkit == $token) $match = 1;
66
      }
67
      return ($match) ? true : false;
68
    } else {
69
      return true;
70
    }
71
  }
72
  
73
  /**
74
   * Convenience method to get a token expired message with a token type, and ? image with description
75
   * @param string $tokentype if you want a specific tokentype, set it here
76
   * @param string $dowhat What will be put in the string "Simply $dowhat again to...", default is try
77
   */
78
  public static function getErrorWithDescriptionHTML($tokentype="", $dowhat="try") {
79
    return ($tokentype !== "") ? "$tokentype token expired, please try again ".self::getDescriptionImageHTML($dowhat) : "Token expired, please try again ".self::getDescriptionImageHTML($dowhat);
80
  }
81
  
82
  /**
83
   * Gets the HTML image (?) with short csrf description for users for the incorrect token error message
84
   * @param dowhat string What will be put in the string "Simply $dowhat again to...", default is try
85
   * @return string HTML image with description
86
   */
87
  public static function getDescriptionImageHTML($dowhat="try") {
88
    $string = "<img src='site_assets/bootstrap/images/questionmark.png' ";
89
    $string.= "title='Tokens are used to help us mitigate attacks; Simply ";
90
    $string.= htmlentities(strip_tags($dowhat));
91
    $string.= " again to continue' width='20px' height='20px'>";
92
    return $string;
93
  }
94
  
95
  private function getHash($string) {
96
    return hash('sha256', $this->salty.$string.$this->salt);
97
  }
98
}
99
100
$csrftoken = new CSRFToken();
101
$csrftoken->setDebug($debug);
102
$csrftoken->setMysql($mysqli);
103
$csrftoken->setSalt($config['SALT']);
104
$csrftoken->setSalty($config['SALTY']);
105
$csrftoken->setMail($mail);
106
$csrftoken->setUser($user);
107
$csrftoken->setToken($oToken);
108
$csrftoken->setConfig($config);
109
$csrftoken->setErrorCodes($aErrorCodes);
110