-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuds_class.php
More file actions
264 lines (224 loc) · 9.05 KB
/
uds_class.php
File metadata and controls
264 lines (224 loc) · 9.05 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php
// This file is part of Virtual PC module.
//
// Virtual PC 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.
//
// Virtual PC 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 this program. If not, see <http://www.gnu.org/licenses/>.
/**
* Connect to UDS Server by rest request and return json
*
* @package mod_virtualpc
* @copyright 2014 Universidad de Málaga - Enseñanza Virtual y Laboratorios Tecnólogicos
* @author Antonio Godino (asesoramiento [at] evlt [dot] uma [dot] es)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
require_once(dirname(__FILE__).'/../virtualpc/uds_class.php');
/**
* Class REST client for OpenUDS.org
*
* @package mod_virtualpc
* @copyright 2014 Universidad de Málaga - Enseñanza Virtual y Laboratorios Tecnólogicos
* @author Antonio Godino (asesoramiento [at] evlt [dot] uma [dot] es)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class my_uds extends uds {
/**
* Rest request is DELETE
*/
const DELETE = 'DELETE';
/**
* Rest request
*
* @param string $type
* @param string $urlpath
* @param string $postfields
* @return \stdClass
*/
public function rest_request ( $type, $urlpath, $postfields ) {
global $USER, $CFG, $COURSE;
$connection = curl_init();
if (preg_match('/^https/i', get_config('virtualpc', 'serverurl')) or ($this->_port == 0)) {
if (preg_match('/^https/i', $this->get_server() )) {
curl_setopt($connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($connection, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($connection, CURLOPT_CUSTOMREQUEST, self::POST);
}
$this->_requesturl = $this->get_server().
'/rest/' . $urlpath;
} else {
$this->_requesturl = $this->get_server() .':' .
$this->get_port() .
'/rest/' . $urlpath;
}
switch ($type) {
case self::POST:
curl_setopt($connection, CURLOPT_POST, true);
if (!empty($postfields)) {
curl_setopt($connection, CURLOPT_POSTFIELDS, json_encode($postfields));
}
break;
case self::PUT:
if (!empty($postfields)) {
curl_setopt($connection, CURLOPT_POSTFIELDS, json_encode($postfields));
}
curl_setopt($connection, CURLOPT_CUSTOMREQUEST, self::PUT);
break;
case self::GET:
if (!empty($postfields)) {
curl_setopt($connection, CURLOPT_POSTFIELDS, json_encode($postfields));
}
curl_setopt($connection, CURLOPT_CUSTOMREQUEST, self::GET);
break;
case self::DELETE:
if (!empty($postfields)) {
curl_setopt($connection, CURLOPT_POSTFIELDS, json_encode($postfields));
}
curl_setopt($connection, CURLOPT_CUSTOMREQUEST, self::DELETE);
break;
}
curl_setopt($connection, CURLOPT_URL, $this->_requesturl);
curl_setopt($connection, CURLOPT_HTTPHEADER, $this->get_headers());
curl_setopt($connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($connection, CURLOPT_VERBOSE, true);
if (isset($CFG->proxyhost)) {
curl_setopt($connection, CURLOPT_PROXY, $CFG->proxyhost);
if (isset($CFG->proxyport)) {
curl_setopt($connection, CURLOPT_PROXYPORT, $CFG->proxyport);
}
}
$curlresponse = curl_exec($connection);
$status = curl_getinfo($connection, CURLINFO_HTTP_CODE);
switch ($status) {
case self::HTTP_OK:
case self::HTTP_CREATED:
case self::HTTP_ACEPTED:
$jsonresponse = json_decode($curlresponse);
break;
default:
if (is_siteadmin($USER->id)) {
$msg = sprintf("Curl error (#%d): %s<br>\n HTTP_CODE error:%d", curl_errno($connection),
htmlspecialchars(curl_error($connection)), $status);
curl_close($connection);
debugging($msg, DEBUG_DEVELOPER);
if (isset($this->_server) and empty($this->_server)) {
notice (get_string('virtualpcresterror', 'virtualpc', $urlpath),
$CFG->wwwroot.'/admin/settings.php?section=modsettingvirtualpc');
}
}
$feedback = new stdClass();
$feedback->url = $this->get_server();
notice (get_string('errorconnection', 'virtualpc', $feedback),
$CFG->wwwroot . '/course/view.php?id=' . $COURSE->id);
break;
}
curl_close($connection);
return $jsonresponse;
}
}
/**
* Login UDS and set valid token
*
* @return stdClass
*/
function my_uds_login() {
global $CFG, $COURSE, $USER;
$udsinstance = new my_uds();
$postfields = array('authSmallName' => $udsinstance->get_authsmallname(),
'username' => $udsinstance->get_username(),
'password' => $udsinstance->get_password());
$urlpath = 'auth/login' . $udsinstance->get_lang();
$jsonresponse = $udsinstance->rest_request($udsinstance::POST, $urlpath,
$postfields );
if ($jsonresponse->result == $udsinstance::OK) {
$udsinstance->set_token($jsonresponse->token);
$udsinstance->add_header('X-Auth-Token: ' . $jsonresponse->token);
return $udsinstance;
} else {
if (is_siteadmin($USER->id)) {
notice(get_string('virtualpcresterror', 'virtualpc', $urlpath),
$CFG->wwwroot.'/admin/settings.php?section=modsettingvirtualpc');
} else {
$feedback = new stdClass();
$feedback->url = $udsinstance->get_server();
notice (get_string('errorconnection', 'virtualpc', $feedback),
$CFG->wwwroot . '/course/view.php?id=' . $COURSE->id);
}
}
}
/**
* Create ticket from user
*
* @param uds $udsinstance
* @param string $username
* @param $password
* @param string $idpool
* @param $transport
* @param string $fullname
* @return string
* @throws coding_exception
*/
function uds_user_tickets_create($udsinstance, $username, $password, $idpool, $transport, $fullname) {
global $CFG, $COURSE;
$urlpath = '/tickets/create';
$postfields = array(
"username" => "$username",
"password" => "$password",
"authSmallName" => $udsinstance->get_authsmallnameforactivity(),
"groups" => $udsinstance->get_groupname(),
"servicePool" => "$idpool",
"transport" => $transport,
"realname" => "$fullname",
"force" => "1",
);
$jsonresponse = $udsinstance->rest_request($udsinstance::PUT,
$urlpath, $postfields);
//print_r($jsonresponse);exit;
if (empty($jsonresponse->result)) {
$feedback = new stdClass();
$pool = uds_servicespools($udsinstance, $idpool);
$feedback->poolname = $pool->name;
$feedback->username = "$username";
notice(get_string('virtualpcerrorcreatingticketid', 'virtualpc', $feedback),
$CFG->wwwroot . '/course/view.php?id=' . $COURSE->id);
return null;
} else {
return $jsonresponse->result;
}
}
/**
* Delete users if last_access is greater than 10 minutes on users authenticator
*
* @param my_uds $udsinstance
*/
function delete_last_login_users($udsinstance)
{
$urlpath = '/authenticators/overview';
// Find all authenticators
$authenticators = $udsinstance->rest_request($udsinstance::GET, $urlpath, '');
foreach ($authenticators as $authenticator) {
// Locate authenticator label where users will be created.
if ($authenticator->small_name == $udsinstance->get_authsmallnameforactivity()) {
$urlpath = "/authenticators/$authenticator->id/users/overview";
// find all users for this authenticator
$users = $udsinstance->rest_request($udsinstance::GET, $urlpath, '');
foreach ($users as $user) {
// Delete users if last_access is greater than 10 minutes
if ($user->last_access < time() - 600) {
$urlpath = "/authenticators/$authenticator->id/users/$user->id";
$udsinstance->rest_request($udsinstance::DELETE, $urlpath, '');
}
}
break;
}
}
}