Added .gitignore
[moodle-linuxchix.git] / lib / form / recaptcha.php
blobd92b610f10539a2f2580e967671ac50697129d72
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4 /**
5 * textarea_counter.php
7 * @category Admin
8 * @package admin
9 * @author Nicolas Connault <nicolasconnault@gmail.com>
10 * @version $Id$
14 /**
15 * @category Admin
16 * @package admin
18 class MoodleQuickForm_recaptcha extends HTML_QuickForm_input {
20 /**
21 * html for help button, if empty then no help
23 * @var string
25 var $_helpbutton='';
27 var $_https=false;
29 /**
30 * <code>
31 * $form->addElement('textarea_counter', 'message', 'Message',
32 * array('cols'=>60, 'rows'=>10), 160);
33 * </code>
35 function MoodleQuickForm_recaptcha($elementName = null, $elementLabel = null, $attributes = null) {
36 parent::HTML_QuickForm_input($elementName, $elementLabel, $attributes);
37 $this->_type = 'recaptcha';
38 if (!empty($attributes['https'])) {
39 $this->_https = $attributes['https'];
43 /**
44 * Returns the recaptcha element in HTML
46 * @since 1.0
47 * @access public
48 * @return string
50 function toHtml() {
51 global $CFG;
52 require_once $CFG->libdir . '/recaptchalib.php';
54 $html = '<script type="text/javascript">
55 var RecaptchaOptions = {
56 theme : \'custom\',
57 tabindex : 2,
58 custom_theme_widget : \'recaptcha_widget\'
60 </script>' . "\n";
62 $attributes = $this->getAttributes();
63 if (empty($attributes['error_message'])) {
64 $attributes['error_message'] = null;
65 $this->setAttributes($attributes);
67 $error = $attributes['error_message'];
68 unset($attributes['error_message']);
70 $strincorrectpleasetryagain = get_string('incorrectpleasetryagain', 'auth');
71 $strenterthewordsabove = get_string('enterthewordsabove', 'auth');
72 $strenterthenumbersyouhear = get_string('enterthenumbersyouhear', 'auth');
73 $strgetanothercaptcha = get_string('getanothercaptcha', 'auth');
74 $strgetanaudiocaptcha = get_string('getanaudiocaptcha', 'auth');
75 $strgetanimagecaptcha = get_string('getanimagecaptcha', 'auth');
77 $html .= '
78 <div id="recaptcha_widget" style="display:none">
80 <div id="recaptcha_image"></div>
81 <div class="recaptcha_only_if_incorrect_sol" style="color:red">' . $strincorrectpleasetryagain . '</div>
83 <span class="recaptcha_only_if_image"><label for="recaptcha_response_field">' . $strenterthewordsabove . '</label></span>
84 <span class="recaptcha_only_if_audio"><label for="recaptcha_response_field">' . $strenterthenumbersyouhear . '</label></span>
86 <input type="text" id="recaptcha_response_field" name="recaptcha_response_field" />
87 <input type="hidden" name="recaptcha_element" value="dummyvalue" /> <!-- Dummy value to fool formslib -->
88 <div><a href="javascript:Recaptcha.reload()">' . $strgetanothercaptcha . '</a></div>
89 <div class="recaptcha_only_if_image"><a href="javascript:Recaptcha.switch_type(\'audio\')">' . $strgetanaudiocaptcha . '</a></div>
90 <div class="recaptcha_only_if_audio"><a href="javascript:Recaptcha.switch_type(\'image\')">' . $strgetanimagecaptcha . '</a></div>
91 </div>';
93 return $html . recaptcha_get_html($CFG->recaptchapublickey, $error, $this->_https);
96 /**
97 * set html for help button
99 * @access public
100 * @param array $help array of arguments to make a help button
101 * @param string $function function name to call to get html
103 function setHelpButton($helpbuttonargs, $function='helpbutton'){
104 if (!is_array($helpbuttonargs)){
105 $helpbuttonargs=array($helpbuttonargs);
106 }else{
107 $helpbuttonargs=$helpbuttonargs;
109 //we do this to to return html instead of printing it
110 //without having to specify it in every call to make a button.
111 if ('helpbutton' == $function){
112 $defaultargs=array('', '', 'moodle', true, false, '', true);
113 $helpbuttonargs=$helpbuttonargs + $defaultargs ;
115 $this->_helpbutton=call_user_func_array($function, $helpbuttonargs);
119 * get html for help button
121 * @access public
122 * @return string html for help button
124 function getHelpButton(){
125 return $this->_helpbutton;
128 function verify($challenge_field, $response_field) {
129 global $CFG;
130 require_once $CFG->libdir . '/recaptchalib.php';
131 $response = recaptcha_check_answer($CFG->recaptchaprivatekey,
132 $_SERVER['REMOTE_ADDR'],
133 $challenge_field,
134 $response_field,
135 $this->_https);
136 if (!$response->is_valid) {
137 $attributes = $this->getAttributes();
138 $attributes['error_message'] = $response->error;
139 $this->setAttributes($attributes);
140 return $response->error;
142 return true;