~ubuntu-branches/ubuntu/hardy/gallery2/hardy-security

« back to all changes in this revision

Viewing changes to modules/captcha/classes/CaptchaValidationPlugin.inc

  • Committer: Bazaar Package Importer
  • Author(s): Michael C. Schultheiss
  • Date: 2006-04-16 16:42:35 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060416164235-8uy0u4bfjdxpge2o
Tags: 2.1.1-1
* New upstream release (Closes: #362936)
  + Bugfixes for Postgres7 (Closes: #359000, #362152)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/*
 
3
 * $RCSfile: CaptchaValidationPlugin.inc,v $
 
4
 *
 
5
 * Gallery - a web based photo album viewer and editor
 
6
 * Copyright (C) 2000-2006 Bharat Mediratta
 
7
 *
 
8
 * This program is free software; you can redistribute it and/or modify
 
9
 * it under the terms of the GNU General Public License as published by
 
10
 * the Free Software Foundation; either version 2 of the License, or (at
 
11
 * your option) any later version.
 
12
 *
 
13
 * This program is distributed in the hope that it will be useful, but
 
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
16
 * General Public License for more details.
 
17
 *
 
18
 * You should have received a copy of the GNU General Public License
 
19
 * along with this program; if not, write to the Free Software
 
20
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
 
21
 */
 
22
 
 
23
/**
 
24
 * @version $Revision: 1.3 $ $Date: 2006/01/30 22:52:14 $
 
25
 * @package Captcha
 
26
 * @author Stefan Ioachim <stefanioachim@gmail.com>
 
27
 * @author Bharat Mediratta <bharat@menalto.com>
 
28
 * @author Alan Harder <alan.harder@sun.com>
 
29
 */
 
30
 
 
31
GalleryCoreApi::requireOnce('modules/core/classes/GalleryValidationPlugin.class');
 
32
 
 
33
/**
 
34
 * Implement ValidationPlugin to present the HTML for the captcha image
 
35
 * and an input box for the user to type in the correct value.
 
36
 *
 
37
 * @package Captcha
 
38
 * @subpackage Classes
 
39
 */
 
40
class CaptchaValidationPlugin extends GalleryValidationPlugin {
 
41
 
 
42
    /**
 
43
     * @see GalleryValidationPlugin::performValidation
 
44
     */
 
45
    function performValidation(&$form, $options=array()) {
 
46
        global $gallery;
 
47
        $session =& $gallery->getSession();
 
48
 
 
49
        list ($ret, $useCaptcha, $failedAttempts) = $this->_shouldValidate($options);
 
50
        if ($ret) {
 
51
            return array($ret->wrap(__FILE__, __LINE__), null, null);
 
52
        }
 
53
 
 
54
        $error = array();
 
55
        $success = true;
 
56
        if ($useCaptcha) {
 
57
            $code = $session->get('captcha.key');
 
58
 
 
59
            if (empty($form['CaptchaValidationPlugin']['word'])) {
 
60
                $error[] = 'form[error][CaptchaValidationPlugin][missing]';
 
61
                $success = false;
 
62
            } else if ($form['CaptchaValidationPlugin']['word'] != $code) {
 
63
                $error[] = 'form[error][CaptchaValidationPlugin][invalid]';
 
64
                $success = false;
 
65
            }
 
66
        }
 
67
 
 
68
        if (isset($options['pass']) && !$options['pass']) {
 
69
            /* Increment failure count either in db or session */
 
70
            if (!empty($options['key'])) {
 
71
                $ret = GalleryCoreApi::setPluginParameter(
 
72
                        'module', 'captcha', 'failCount-' . $options['key'], $failedAttempts + 1);
 
73
                if ($ret) {
 
74
                    return array($ret->wrap(__FILE__, __LINE__), null, null);
 
75
                }
 
76
            } else {
 
77
                $session->put('captcha.failedAttempts', $failedAttempts + 1);
 
78
            }
 
79
        } else if (isset($options['pass']) && $options['pass']
 
80
                && $success && $failedAttempts > 0 && !empty($options['key'])) {
 
81
            $ret = GalleryCoreApi::removePluginParameter(
 
82
                    'module', 'captcha', 'failCount-' . $options['key']);
 
83
            if ($ret) {
 
84
                return array($ret->wrap(__FILE__, __LINE__), null, null);
 
85
            }
 
86
        }
 
87
 
 
88
        return array(null, $error, $success);
 
89
    }
 
90
 
 
91
    /**
 
92
     * @see GalleryValidationPlugin::loadTemplate
 
93
     */
 
94
    function loadTemplate(&$form, $options=array()) {
 
95
        list ($ret, $useCaptcha) = $this->_shouldValidate($options);
 
96
        if ($ret) {
 
97
            return array($ret->wrap(__FILE__, __LINE__), null, null);
 
98
        }
 
99
 
 
100
        if ($useCaptcha) {
 
101
            /* Generate a new code */
 
102
            global $gallery;
 
103
            list ($usec, $sec) = explode(' ', microtime());
 
104
            srand((float)$sec + ((float)$usec * 100000));
 
105
            $random_num = rand();
 
106
            $datekey = date('H i s');
 
107
            $rcode = hexdec(md5(GalleryUtilities::getServerVar('HTTP_USER_AGENT')
 
108
                                . $random_num . $datekey));
 
109
            $code = substr($rcode, 2, 6);
 
110
            $session =& $gallery->getSession();
 
111
            $session->put('captcha.key', $code);
 
112
 
 
113
            return array(null,
 
114
                         'modules/captcha/templates/CaptchaValidationPlugin.tpl',
 
115
                         'modules_captcha');
 
116
        }
 
117
 
 
118
        return array(null, null, null);
 
119
    }
 
120
 
 
121
    /**
 
122
     * Determine if captcha validation should be enforced
 
123
     *
 
124
     * @param array options
 
125
     * @return array object GalleryStatus a status code
 
126
     *               boolean true to validate
 
127
     *               int current failure count, if applicable
 
128
     * @access private
 
129
     */
 
130
    function _shouldValidate($options) {
 
131
        $useCaptcha = false;
 
132
        $failedAttempts = 0;
 
133
        $securityLevel = empty($options['level']) ? 'HIGH' : $options['level'];
 
134
 
 
135
        switch ($securityLevel) {
 
136
        case 'HIGH':
 
137
            /* Always require captcha to be enabled */
 
138
            $useCaptcha = true;
 
139
            break;
 
140
 
 
141
        case 'MEDIUM':
 
142
        case 'LOW':
 
143
            /*
 
144
             * Use the captcha when the number of failed attempts exceeds the module's
 
145
             * failedAttemptThreshold parameter.  Track failed attempts in db via a given
 
146
             * unique key (MEDIUM level) or in the session (LOW level).
 
147
             */
 
148
            list ($ret, $failedAttemptThreshold) = GalleryCoreApi::getPluginParameter(
 
149
                    'module', 'captcha', 'failedAttemptThreshold');
 
150
            if ($ret) {
 
151
                return array($ret->wrap(__FILE__, __LINE__), null, null);
 
152
            }
 
153
 
 
154
            if ($securityLevel == 'LOW') {
 
155
                global $gallery;
 
156
                $session =& $gallery->getSession();
 
157
                $failedAttempts = $session->get('captcha.failedAttempts');
 
158
            } else if (!empty($options['key'])) {
 
159
                list ($ret, $failedAttempts) = GalleryCoreApi::getPluginParameter(
 
160
                        'module', 'captcha', 'failCount-' . $options['key']);
 
161
                if ($ret) {
 
162
                    return array($ret->wrap(__FILE__, __LINE__), null, null);
 
163
                }
 
164
            }
 
165
 
 
166
            if ($failedAttempts > $failedAttemptThreshold) {
 
167
                $useCaptcha = true;
 
168
            }
 
169
            break;
 
170
 
 
171
        default:
 
172
            /* Bad Parameter */
 
173
            return array(GalleryCoreApi::error(ERROR_BAD_PARAMETER, __FILE__, __LINE__),
 
174
                         null, null);
 
175
        }
 
176
 
 
177
        return array(null, $useCaptcha, (int)$failedAttempts);
 
178
    }
 
179
}
 
180
?>