Completed
Push — master ( 2254e1...62d7c6 )
by Henri
02:48
created

Honeypot::validateHoneypot()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
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
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
9
{
10
    protected $disabled = false;
11
    /**
12
     * Enable the Honeypot validation
13
     */
14
    public function enable()
15
    {
16
        $this->disabled = false;
17
    }
18
    /**
19
     * Disable the Honeypot validation
20
     */
21
    public function disable()
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)
32
    {
33
        // Encrypt the current time
34
        $honey_time_encrypted = $this->getEncryptedTime();
35
        $html = '<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
        return $html;
40
    }
41
    /**
42
    * Validate honeypot is empty
43
    *
44
    * @param  mixed $value
45
    * @return boolean
46
    */
47
    public function validateHoneypot($value)
48
    {
49
        if ($this->disabled) {
50
            return true;
51
        }
52
        return $value == '';
53
    }
54
    /**
55
     * Validate honey time was within the time limit
56
     *
57
     * @param  mixed $value
58
     * @param  array $parameters
59
     * @return boolean
60
     */
61
    public function validateHoneytime($value, $parameters)
62
    {
63
        if ($this->disabled) {
64
            return true;
65
        }
66
        
67
        // Get the decrypted time
68
        $value = $this->decryptTime($value);
69
        // The current time should be greater than the time the form was built + the speed option
70
        return ( is_numeric($value) && time() > ($value + $parameters[0]) );
71
    }
72
    /**
73
     * Get encrypted time
74
     * @return string
75
     */
76
    public function getEncryptedTime()
77
    {
78
        return base64_encode(time());
79
    }
80
    /**
81
     * Decrypt the given time
82
     *
83
     * @param  mixed $time
84
     * @return string|null
85
     */
86
    public function decryptTime($time)
87
    {
88
        try {
89
            return base64_decode($time);
90
        } catch (\Exception $exception) {
91
            return null;
92
        }
93
    }
94
}
95