Completed
Push — master ( d21053...c59b30 )
by
unknown
08:11
created

FGSimpleCaptcha   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 77
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A FGSimpleCaptcha() 0 5 1
A GetSimpleCaptcha() 0 17 1
A SetFormKey() 0 4 1
A GetKey() 0 4 1
A Validate() 0 29 3
A Md5CaptchaAnswer() 0 4 1
A GetError() 0 4 1
1
<?PHP
2
/*
3
    Contact Form from HTML Form Guide
4
5
    This program is free software published under the
6
    terms of the GNU Lesser General Public License.
7
8
This program is distributed in the hope that it will
9
be useful - WITHOUT ANY WARRANTY; without even the
10
implied warranty of MERCHANTABILITY or FITNESS FOR A
11
PARTICULAR PURPOSE.
12
13
@copyright html-form-guide.com 2010
14
*/
15
class FGSimpleCaptcha extends FG_CaptchaHandler
16
{
17
    var $error_str;
18
    var $captcha_varname;
19
    var $uniquekey;
20
21
    function FGSimpleCaptcha($captcha_var_name)
22
    {
23
        $this->captcha_varname=$captcha_var_name;
24
        $this->uniquekey='KHJhsjsy65HGbsmnd';
25
    }
26
27
    /*Add more simple questions here.*/
28
    function GetSimpleCaptcha()
29
    {
30
        $arrQuestions = array(
31
	elgg_echo('contactform:validator:question1') => elgg_echo('contactform:validator:answer1'),
32
	elgg_echo('contactform:validator:question2') => elgg_echo('contactform:validator:answer2'),
33
	elgg_echo('contactform:validator:question3') => elgg_echo('contactform:validator:answer3'),
34
	elgg_echo('contactform:validator:question4') => elgg_echo('contactform:validator:answer4'),
35
	elgg_echo('contactform:validator:question5') => elgg_echo('contactform:validator:answer5'),
36
	);
37
38
        $question = array_rand($arrQuestions);
39
        $answer = $arrQuestions[$question];
40
41
        $_SESSION['FGCF_Captcha_Answer'] = $this->Md5CaptchaAnswer($answer);
42
43
        return $question;
44
    }
45
46
    function SetFormKey($key)
47
    {
48
        $this->uniquekey = $key;
49
    }
50
    function GetKey()
51
    {
52
        return $this->uniquekey;
53
    }
54
    function Validate()
55
    {
56
        $ret=false;
57
        if(empty($_POST[$this->captcha_varname]))
58
        {
59
            $this->error_str = elgg_echo('contactform:validator:answer');
60
            $ret = false;
61
        }
62
        else
63
        {
64
65
            $scaptcha = trim($_POST[$this->captcha_varname]);
66
67
            $scaptcha = strtolower($scaptcha);
68
69
            $user_answer = $this->Md5CaptchaAnswer($scaptcha);
70
71
            if($user_answer != $_SESSION['FGCF_Captcha_Answer'])
72
            {
73
                $this->error_str = elgg_echo('contactform:validator:failed');
74
                $ret = false;
75
            }
76
            else
77
            {
78
                $ret = true;
79
            }
80
        }//else
81
        return $ret;
82
    }
83
    function Md5CaptchaAnswer($answer)
84
    {
85
        return md5($this->GetKey().$answer);
86
    }
87
    function GetError()
88
    {
89
        return $this->error_str;
90
    }
91
}
92
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
93