-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathtoken.php
More file actions
89 lines (74 loc) · 2.55 KB
/
token.php
File metadata and controls
89 lines (74 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
// This file is part of Exabis Eportfolio (extension for Moodle)
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
// (c) 2016 GTN - Global Training Network GmbH <office@gtn-solutions.com>.
define('AJAX_SCRIPT', true);
define('REQUIRE_CORRECT_ACCESS', true);
define('NO_MOODLE_COOKIES', true);
require(__DIR__ . '/inc.php');
function block_exaport_load_service($service) {
$CFG = $GLOBALS['CFG'];
$OUTPUT = $GLOBALS['OUTPUT'];
$DB = $GLOBALS['DB'];
ob_start();
try {
$_POST['service'] = $service;
require(__DIR__ . '/../../login/token.php');
} catch (moodle_exception $e) {
if ($e->errorcode == 'servicenotavailable') {
return null;
} else {
throw $e;
}
}
$ret = ob_get_clean();
$data = json_decode($ret);
if ($data && $data->token) {
return $data->token;
} else {
return null;
}
}
// Allow CORS requests.
header('Access-Control-Allow-Origin: *');
echo $OUTPUT->header();
required_param('app', PARAM_TEXT);
required_param('app_version', PARAM_TEXT);
if (optional_param('testconnection', false, PARAM_BOOL)) {
echo json_encode([
'moodleName' => $DB->get_field('course', 'fullname', ['id' => 1]),
], JSON_PRETTY_PRINT);
exit;
}
$exatokens = [];
$services = optional_param('services', '', PARAM_TEXT);
// Default services + .
$services = array_keys(
['moodle_mobile_app' => 1, 'exaportservices' => 1] + ($services ? array_flip(explode(',', $services)) : []));
foreach ($services as $service) {
$token = block_exaport_load_service($service);
$exatokens[] = [
'service' => $service,
'token' => $token,
];
}
require_once(__DIR__ . '/externallib.php');
// Get login data.
$data = \block_exaport\externallib\externallib::login();
// Add tokens.
$data['tokens'] = $exatokens;
// Clean output.
$data = external_api::clean_returnvalue(\block_exaport\externallib\externallib::login_returns(), $data);
echo json_encode($data, JSON_PRETTY_PRINT);