Honeypot::enable()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Original class from msurguy/Honeypot. Licensed under the MIT License. Instead of using Laravel Crypt class,
5
 * this implementation simply returns the decrypted value base64-encoded, as the encoded value.
6
 * @see https://github.com/msurguy/Honeypot
7
 */
8
class Honeypot
9
{
10
    protected $disabled = false;
11
    /**
12
     * Enable the Honeypot validation
13
     */
14
    public function enable(): void
15
    {
16
        $this->disabled = false;
17
    }
18
    /**
19
     * Disable the Honeypot validation
20
     */
21
    public function disable(): void
22
    {
23
        $this->disabled = true;
24
    }
25
    /**
26
     * Generate a new honeypot and return the form HTML
27
     * @param  string $honey_name
28
     * @param  string $honey_time
29
     * @return string
30
     */
31
    public function generate($honey_name, $honey_time): string
32
    {
33
        // Encrypt the current time
34
        $honey_time_encrypted = $this->getEncryptedTime();
35
        return '<div id="' . $honey_name . '_wrap" style="display:none;">' . "\r\n" .
36
               '<input name="' . $honey_name . '" type="text" value="" id="' . $honey_name . '"/>' . "\r\n" .
37
               '<input name="' . $honey_time . '" type="text" value="' . $honey_time_encrypted . '"/>' . "\r\n" .
38
                '</div>';
39
    }
40
    /**
41
    * Validate honeypot is empty
42
    *
43
    * @param  mixed $value
44
    * @return boolean
45
    */
46
    public function validateHoneypot($value): bool
47
    {
48
        if ($this->disabled) {
49
            return true;
50
        }
51
        return $value == '';
52
    }
53
    /**
54
     * Validate honey time was within the time limit
55
     *
56
     * @param  string $value base64 encoded time value
57
     * @param  int $minDelta minimum time difference in seconds
58
     * @return boolean
59
     */
60
    public function validateHoneytime($value, $minDelta): bool
61
    {
62
        if ($this->disabled) {
63
            return true;
64
        }
65
66
        // Get the decrypted time
67
        $value = $this->decryptTime($value);
68
        // The current time should be greater than the time the form was built + minimum
69
        return (is_numeric($value) && time() > ($value + $minDelta));
70
    }
71
    /**
72
     * Get encrypted time
73
     * @return string
74
     */
75
    public function getEncryptedTime(): string
76
    {
77
        return base64_encode(time());
78
    }
79
    /**
80
     * Decrypt the given time
81
     *
82
     * @param  mixed $time
83
     * @return int|null
84
     */
85
    public function decryptTime($time): ?int
86
    {
87
        try {
88
            return intval(base64_decode($time));
89
        } catch (\Exception $exception) {
90
            return null;
91
        }
92
    }
93
}
94