Merge branch 'MDL-62904-34' of git://github.com/junpataleta/moodle into MOODLE_34_STABLE
[moodle.git] / badges / backpackemailverify.php
blobd9dfda2530972ca8579f47c0be8b9664d8541d5e
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/>.
17 /**
18 * Endpoint for the verification email link.
20 * @package core
21 * @subpackage badges
22 * @copyright 2016 Jake Dallimore <jrhdallimore@gmail.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once(__DIR__ . '/../config.php');
26 require_once($CFG->libdir . '/badgeslib.php');
27 require_once(__DIR__ . '/lib/backpacklib.php');
29 $data = optional_param('data', '', PARAM_RAW);
30 require_login();
31 $PAGE->set_url('/badges/openbackpackemailverify.php');
32 $PAGE->set_context(context_user::instance($USER->id));
33 $redirect = '/badges/mybackpack.php';
35 // Confirm the secret and create the backpack connection.
36 $storedsecret = get_user_preferences('badges_email_verify_secret');
37 if (!is_null($storedsecret)) {
38 if ($data === $storedsecret) {
39 $storedemail = get_user_preferences('badges_email_verify_address');
41 $data = new stdClass();
42 $data->backpackurl = BADGE_BACKPACKURL;
43 $data->email = $storedemail;
44 $bp = new OpenBadgesBackpackHandler($data);
46 // Make sure we have all the required information before trying to save the connection.
47 $backpackuser = $bp->curl_request('user');
48 if (isset($backpackuser->status) && $backpackuser->status === 'okay' && isset($backpackuser->userId)) {
49 $backpackuid = $backpackuser->userId;
50 } else {
51 redirect(new moodle_url($redirect), get_string('backpackconnectionunexpectedresult', 'badges'),
52 null, \core\output\notification::NOTIFY_ERROR);
55 $obj = new stdClass();
56 $obj->userid = $USER->id;
57 $obj->email = $data->email;
58 $obj->backpackurl = $data->backpackurl;
59 $obj->backpackuid = $backpackuid;
60 $obj->autosync = 0;
61 $obj->password = '';
62 $DB->insert_record('badge_backpack', $obj);
64 // Remove the verification vars and redirect to the mypackpack page.
65 unset_user_preference('badges_email_verify_secret');
66 unset_user_preference('badges_email_verify_address');
67 redirect(new moodle_url($redirect), get_string('backpackemailverifysuccess', 'badges'),
68 null, \core\output\notification::NOTIFY_SUCCESS);
69 } else {
70 // Stored secret doesn't match the supplied secret. Take user back to the mybackpack page and present a warning message.
71 redirect(new moodle_url($redirect), get_string('backpackemailverifytokenmismatch', 'badges'),
72 null, \core\output\notification::NOTIFY_ERROR);
74 } else {
75 // Stored secret is null. Either the email address has already been verified, or there is no record of a verification attempt
76 // for the current user. Either way, just redirect to the mybackpack page.
77 redirect(new moodle_url($redirect));