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

Honeypot   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 0
dl 0
loc 87
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A enable() 0 4 1
A disable() 0 4 1
A generate() 0 10 1
A validateHoneypot() 0 7 2
A validateHoneytime() 0 11 3
A getEncryptedTime() 0 4 1
A decryptTime() 0 8 2
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