Merge branch 'wip-mdl-45889-m26' of https://github.com/rajeshtaneja/moodle into MOODL...
[moodle.git] / badges / backpackconnect.php
blobbbcc78f64d8de3a457fefc0bc62d0c7edb47b4b3
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 * AJAX script for validating backpack connection.
20 * @package core
21 * @subpackage badges
22 * @copyright 2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 * @author Simon Coggins <simon.coggins@totaralms.com>
27 define('AJAX_SCRIPT', true);
29 require_once(dirname(dirname(__FILE__)) . '/config.php');
30 require_once($CFG->dirroot . '/badges/lib/backpacklib.php');
31 require_once($CFG->libdir . '/filelib.php');
33 require_sesskey();
34 require_login();
35 $PAGE->set_url('/badges/backpackconnect.php');
36 $PAGE->set_context(context_system::instance());
37 echo $OUTPUT->header();
39 // Use PHP input filtering as there is no PARAM type for
40 // the type of cleaning that is required (ASCII chars 32-127 only).
41 $assertion = filter_input(
42 INPUT_POST,
43 'assertion',
44 FILTER_UNSAFE_RAW,
45 FILTER_FLAG_STRIP_LOW|FILTER_FLAG_STRIP_HIGH
48 // Audience is the site url scheme + host + port only.
49 $wwwparts = parse_url($CFG->wwwroot);
50 $audience = $wwwparts['scheme'] . '://' . $wwwparts['host'];
51 $audience .= isset($wwwparts['port']) ? $wwwparts['port'] : '';
52 $params = 'assertion=' . urlencode($assertion) . '&audience=' .
53 urlencode($audience);
55 $curl = new curl();
56 $url = 'https://verifier.login.persona.org/verify';
57 $options = array(
58 'FRESH_CONNECT' => true,
59 'RETURNTRANSFER' => true,
60 'FORBID_REUSE' => true,
61 'SSL_VERIFYPEER' => true,
62 'SSL_VERIFYHOST' => 2,
63 'HEADER' => 0,
64 'HTTPHEADER' => array('Content-type: application/x-www-form-urlencoded'),
65 'CONNECTTIMEOUT' => 0,
66 'TIMEOUT' => 10, // Fail if data not returned within 10 seconds.
68 $result = $curl->post($url, $params, $options);
70 // Handle time-out and failed request.
71 if ($curl->errno != 0) {
72 if ($curl->errno == CURLE_OPERATION_TIMEOUTED) {
73 $reason = get_string('error:requesttimeout', 'badges');
74 } else {
75 $reason = get_string('error:requesterror', 'badges', $curl->errno);
77 badges_send_response('failure', $reason);
80 $data = json_decode($result);
82 if (!isset($data->status) || $data->status != 'okay') {
83 $reason = isset($data->reason) ? $data->reason : get_string('error:connectionunknownreason', 'badges');
84 badges_send_response('failure', $reason);
87 // Make sure email matches a backpack.
88 $check = new stdClass();
89 $check->backpackurl = BADGE_BACKPACKURL;
90 $check->email = $data->email;
92 $bp = new OpenBadgesBackpackHandler($check);
93 $request = $bp->curl_request('user');
94 if (isset($request->status) && $request->status == 'missing') {
95 $reason = get_string('error:backpackemailnotfound', 'badges', $data->email);
96 badges_send_response('failure', $reason);
97 } else if (empty($request->userId)) {
98 $reason = get_string('error:backpackdatainvalid', 'badges');
99 badges_send_response('failure', $reason);
100 } else {
101 $backpackuid = $request->userId;
104 // Insert record.
105 $obj = new stdClass();
106 $obj->userid = $USER->id;
107 $obj->email = $data->email;
108 $obj->backpackurl = BADGE_BACKPACKURL;
109 $obj->backpackuid = $backpackuid;
110 $obj->autosync = 0;
111 $obj->password = '';
112 $DB->insert_record('badge_backpack', $obj);
114 // Return success indicator and email address.
115 badges_send_response('success', $data->email);
119 * Return a JSON response containing the response provided.
121 * @param string $status Status of the response, typically 'success' or 'failure'.
122 * @param string $responsetext On success, the email address of the user,
123 * otherwise a reason for the failure.
124 * @return void Outputs the JSON and terminates the script.
126 function badges_send_response($status, $responsetext) {
127 $out = new stdClass();
128 $out->status = $status;
129 if ($status == 'success') {
130 $out->email = $responsetext;
131 } else {
132 $out->reason = $responsetext;
133 send_header_404();
135 echo json_encode($out);
136 exit;