weekly release 5.0dev
[moodle.git] / webservice / recaptcha.php
blobda0bc5ee12298b264f192c710168a7fa8a88498b
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * A script to display a ReCaptcha for the site.
21 * ReCaptcha V2 is restricted by domain, so it cannot be displayed in mobile and desktop apps.
23 * This script will display and initialize the reCaptcha, setting some empty callbacks by default. The client can override
24 * those Javascript callbacks (in the "window" object).
26 * This script won't work in mobile and desktop apps unless $CFG->allowframembedding is enabled.
28 * @package core_webservice
29 * @copyright 2018 Dani Palou
30 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 define('NO_MOODLE_COOKIES', true);
35 require_once(__DIR__ . '/../config.php');
37 $lang = optional_param('lang', '', PARAM_LANG);
39 $content = '';
41 // Check that reCAPTCHA is configured.
42 if (!empty($CFG->recaptchapublickey) && !empty($CFG->recaptchaprivatekey)) {
43 require_once($CFG->libdir . '/recaptchalib_v2.php');
45 $apiurl = RECAPTCHA_API_URL;
46 $pubkey = $CFG->recaptchapublickey;
48 $jscode = "
49 // Create empty callbacks by default. They can be overridden by the client.
50 var recaptchacallback = function() {};
51 var recaptchaexpiredcallback = function() {};
52 var recaptchaerrorcallback = function() {};
54 var recaptchaloaded = function() {
55 grecaptcha.render('recaptcha_element', {
56 'sitekey' : '$pubkey',
57 'callback' : 'recaptchacallback',
58 'expired-callback' : 'recaptchaexpiredcallback',
59 'error-callback' : 'recaptchaerrorcallback'
60 });
61 }";
63 $lang = recaptcha_lang($lang);
65 $apicode = "\n<script type=\"text/javascript\" ";
66 $apicode .= "src=\"$apiurl?onload=recaptchaloaded&render=explicit&hl=$lang\" async defer>";
67 $apicode .= "</script>\n";
69 $content = html_writer::script($jscode, '');
70 $content .= html_writer::div('', 'recaptcha_element', array('id' => 'recaptcha_element'));
71 $content .= $apicode;
72 } else {
73 // To use reCAPTCHA you must have an API key.
74 require_once($CFG->libdir . '/filelib.php');
75 send_header_404();
76 throw new \moodle_exception('cannotusepage2');
79 $output = <<<OET
80 <html>
81 <head>
82 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
83 </head>
84 <body style="margin:0; padding:0">
85 $content
86 </body>
87 </html>
88 OET;
89 echo $output;