2008-03-10 17:32:32 +00:00
|
|
|
<?php
|
|
|
|
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* textarea_counter.php
|
|
|
|
*
|
|
|
|
* @category Admin
|
|
|
|
* @package admin
|
|
|
|
* @author Nicolas Connault <nicolasconnault@gmail.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @category Admin
|
|
|
|
* @package admin
|
|
|
|
*/
|
|
|
|
class MoodleQuickForm_recaptcha extends HTML_QuickForm_input {
|
|
|
|
|
2008-03-11 10:34:50 +00:00
|
|
|
/**
|
|
|
|
* html for help button, if empty then no help
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
var $_helpbutton='';
|
2009-11-01 11:42:23 +00:00
|
|
|
|
2008-03-31 12:13:46 +00:00
|
|
|
var $_https=false;
|
2008-03-11 10:34:50 +00:00
|
|
|
|
2008-03-10 17:32:32 +00:00
|
|
|
/**
|
|
|
|
* <code>
|
|
|
|
* $form->addElement('textarea_counter', 'message', 'Message',
|
|
|
|
* array('cols'=>60, 'rows'=>10), 160);
|
|
|
|
* </code>
|
|
|
|
*/
|
2008-03-31 12:13:46 +00:00
|
|
|
function MoodleQuickForm_recaptcha($elementName = null, $elementLabel = null, $attributes = null) {
|
2008-03-10 17:32:32 +00:00
|
|
|
parent::HTML_QuickForm_input($elementName, $elementLabel, $attributes);
|
|
|
|
$this->_type = 'recaptcha';
|
2008-03-31 12:13:46 +00:00
|
|
|
if (!empty($attributes['https'])) {
|
|
|
|
$this->_https = $attributes['https'];
|
|
|
|
}
|
2008-03-10 17:32:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the recaptcha element in HTML
|
|
|
|
*
|
|
|
|
* @since 1.0
|
|
|
|
* @access public
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function toHtml() {
|
2009-07-07 08:49:00 +00:00
|
|
|
global $CFG, $PAGE;
|
2008-03-10 17:32:32 +00:00
|
|
|
require_once $CFG->libdir . '/recaptchalib.php';
|
|
|
|
|
2009-10-01 06:54:45 +00:00
|
|
|
$recaptureoptions = Array('theme'=>'custom', 'custom_theme_widget'=>'recaptcha_widget');
|
2009-07-07 08:49:00 +00:00
|
|
|
$html = $PAGE->requires->data_for_js('RecaptchaOptions', $recaptureoptions)->asap();
|
2008-03-10 17:32:32 +00:00
|
|
|
|
2008-03-11 10:34:50 +00:00
|
|
|
$attributes = $this->getAttributes();
|
|
|
|
if (empty($attributes['error_message'])) {
|
|
|
|
$attributes['error_message'] = null;
|
|
|
|
$this->setAttributes($attributes);
|
2008-03-10 17:32:32 +00:00
|
|
|
}
|
2008-03-11 10:34:50 +00:00
|
|
|
$error = $attributes['error_message'];
|
|
|
|
unset($attributes['error_message']);
|
2009-11-01 11:42:23 +00:00
|
|
|
|
2008-03-10 17:32:32 +00:00
|
|
|
$strincorrectpleasetryagain = get_string('incorrectpleasetryagain', 'auth');
|
|
|
|
$strenterthewordsabove = get_string('enterthewordsabove', 'auth');
|
|
|
|
$strenterthenumbersyouhear = get_string('enterthenumbersyouhear', 'auth');
|
|
|
|
$strgetanothercaptcha = get_string('getanothercaptcha', 'auth');
|
|
|
|
$strgetanaudiocaptcha = get_string('getanaudiocaptcha', 'auth');
|
|
|
|
$strgetanimagecaptcha = get_string('getanimagecaptcha', 'auth');
|
|
|
|
|
|
|
|
$html .= '
|
|
|
|
<div id="recaptcha_widget" style="display:none">
|
|
|
|
|
|
|
|
<div id="recaptcha_image"></div>
|
|
|
|
<div class="recaptcha_only_if_incorrect_sol" style="color:red">' . $strincorrectpleasetryagain . '</div>
|
|
|
|
|
2008-03-13 15:45:16 +00:00
|
|
|
<span class="recaptcha_only_if_image"><label for="recaptcha_response_field">' . $strenterthewordsabove . '</label></span>
|
|
|
|
<span class="recaptcha_only_if_audio"><label for="recaptcha_response_field">' . $strenterthenumbersyouhear . '</label></span>
|
2008-03-10 17:32:32 +00:00
|
|
|
|
|
|
|
<input type="text" id="recaptcha_response_field" name="recaptcha_response_field" />
|
2008-03-13 15:45:16 +00:00
|
|
|
<input type="hidden" name="recaptcha_element" value="dummyvalue" /> <!-- Dummy value to fool formslib -->
|
2008-03-10 17:32:32 +00:00
|
|
|
<div><a href="javascript:Recaptcha.reload()">' . $strgetanothercaptcha . '</a></div>
|
|
|
|
<div class="recaptcha_only_if_image"><a href="javascript:Recaptcha.switch_type(\'audio\')">' . $strgetanaudiocaptcha . '</a></div>
|
|
|
|
<div class="recaptcha_only_if_audio"><a href="javascript:Recaptcha.switch_type(\'image\')">' . $strgetanimagecaptcha . '</a></div>
|
2009-11-01 11:42:23 +00:00
|
|
|
</div>';
|
2008-03-10 17:32:32 +00:00
|
|
|
|
2008-03-31 12:13:46 +00:00
|
|
|
return $html . recaptcha_get_html($CFG->recaptchapublickey, $error, $this->_https);
|
2008-03-10 17:32:32 +00:00
|
|
|
}
|
2009-11-01 11:42:23 +00:00
|
|
|
|
2008-03-11 10:34:50 +00:00
|
|
|
/**
|
|
|
|
* set html for help button
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param array $help array of arguments to make a help button
|
|
|
|
* @param string $function function name to call to get html
|
|
|
|
*/
|
|
|
|
function setHelpButton($helpbuttonargs, $function='helpbutton'){
|
2009-12-30 15:19:55 +00:00
|
|
|
debugging('component setHelpButton() is not used any more, please use $mform->setHelpButton() instead');
|
2008-03-11 10:34:50 +00:00
|
|
|
}
|
2009-11-01 11:42:23 +00:00
|
|
|
|
2008-03-11 10:34:50 +00:00
|
|
|
/**
|
|
|
|
* get html for help button
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return string html for help button
|
|
|
|
*/
|
|
|
|
function getHelpButton(){
|
|
|
|
return $this->_helpbutton;
|
|
|
|
}
|
|
|
|
|
|
|
|
function verify($challenge_field, $response_field) {
|
|
|
|
global $CFG;
|
|
|
|
require_once $CFG->libdir . '/recaptchalib.php';
|
|
|
|
$response = recaptcha_check_answer($CFG->recaptchaprivatekey,
|
2009-05-01 03:04:10 +00:00
|
|
|
getremoteaddr(),
|
2008-03-11 10:34:50 +00:00
|
|
|
$challenge_field,
|
2008-03-31 12:13:46 +00:00
|
|
|
$response_field,
|
|
|
|
$this->_https);
|
2008-03-11 10:34:50 +00:00
|
|
|
if (!$response->is_valid) {
|
|
|
|
$attributes = $this->getAttributes();
|
|
|
|
$attributes['error_message'] = $response->error;
|
|
|
|
$this->setAttributes($attributes);
|
|
|
|
return $response->error;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2008-03-10 17:32:32 +00:00
|
|
|
}
|