forked from ahmadi-akbar/moodle-mod_adobeconnect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect_class.php
399 lines (302 loc) · 11.1 KB
/
connect_class.php
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
<?php
// This file is part of Moodle - http://moodle.org/
//
// 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/>.
/**
* @package mod_adobeconnect
* @author Akinsaya Delamarre ([email protected])
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @copyright (C) 2015 Remote Learner.net Inc http://www.remote-learner.net
*/
class connect_class {
var $_serverurl;
var $_serverport;
var $_username;
var $_password;
var $_cookie;
var $_xmlrequest;
var $_xmlresponse;
var $_apicall;
var $_connection;
var $_https;
public function __construct($serverurl = '', $serverport = 80,
$username = '', $password = '',
$cookie = '', $https = false) {
$this->_serverurl = $serverurl;
$this->_serverport = $serverport;
$this->_username = $username;
$this->_password = $password;
$this->_cookie = $cookie;
$this->_https = $https;
}
/**
* Accessor methods
*/
public function set_serverport($serverport = '') {
$this->_serverport = $serverport;
}
public function set_serverurl($serverurl = '') {
$this->_serverurl = $serverurl;
}
public function set_username($username = '') {
$this->_username = $username;
}
public function set_password($password = '') {
$this->_password = $password;
}
public function set_cookie($cookie = '') {
$this->_cookie = $cookie;
}
public function set_xmlrequest($xml = '') {
$this->_xmlrequest = $xml;
}
public function set_connection($connection = 0) {
$this->_connection = $connection;
}
public function set_https($https = false) {
$this->_https = $https;
}
public function get_serverurl() {
return $this->_serverurl;
}
public function get_username() {
return $this->_username;
}
public function get_password() {
return $this->_password;
}
public function get_cookie() {
return $this->_cookie;
}
public function get_connection() {
return $this->_connection;
}
public function get_serverport() {
return $this->_serverport;
}
private function get_deafult_header() {
return array('Content-Type: text/xml');
}
public function get_https() {
return $this->_https;
}
/**
* Adds or replaces http:// with https:// for secured connections
* @return string - server URL with the HTTPS protocol
*/
private function make_https() {
$serverurl = $this->_serverurl;
$httpsexists = strpos($this->_serverurl, 'https://');
$httpexists = strpos($this->_serverurl, 'http://');
if (false === $httpsexists and false !== $httpexists) {
$serverurl = str_replace('http://', 'https://', $this->_serverurl);
} elseif (false === $httpsexists) {
$serverurl = 'https://' . $this->_serverurl;
}
return $serverurl;
}
/**
* Posts XML to the Adobe Connect server and returns the results
* @param int $return_header 1 to include the response header, 0 to not
* @param array $add_header an array of headers to add to the request
*/
public function send_request($return_header = 0, $add_header = array(), $stop = false) {
global $CFG;
$ch = curl_init();
$serverurl = $this->_serverurl;
if ($this->_https) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
$serverurl = $this->make_https();
}
if ($stop) {
// echo $this->_serverurl . '?session='. $this->_cookie; die();
// https://example.com/api/xml?action=principal=list
curl_setopt($ch, CURLOPT_URL, $serverurl/* . '?action=login&external-auth=use'*/);
} else {
$querystring = (!empty($this->_cookie)) ? '?session='. $this->_cookie : '';
curl_setopt($ch, CURLOPT_URL, $serverurl . $querystring);
}
// Connect through a proxy if Moodle config says we should
if(isset($CFG->proxyhost)) {
curl_setopt($ch, CURLOPT_PROXY, $CFG->proxyhost);
if(isset($CFG->proxyport)) {
curl_setopt($ch, CURLOPT_PROXYPORT, $CFG->proxyport);
}
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->_xmlrequest);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_PORT, $this->_serverport);
$header = $this->get_deafult_header();
$header = array_merge($header, $add_header);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
// Include header from response
curl_setopt($ch, CURLOPT_HEADER, $return_header);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
/**
* Sends the HTTP header login request and returns the response xml
* @param string username username to use for header x-user-id
*/
public function request_http_header_login($return_header = 0, $username = '', $stop = false) {
global $CFG;
$hearder = array();
$this->create_http_head_login_xml();
// The first parameter is 1 because we want to include the response header
// to extract the session cookie
if (!empty($username)) {
$hearder = array("$CFG->adobeconnect_admin_httpauth: " . $username);
}
$this->_xmlresponse = $this->send_request($return_header, $hearder, $stop);
$this->set_session_cookie($this->_xmlresponse);
return $this->_xmlresponse;
}
public function create_request($params = array(), $sentrequest = true) {
if (empty($params)) {
return false;
}
@date_default_timezone_set("GMT");
$writer = new XMLWriter();
$writer->openMemory();
$writer->startDocument('1.0', 'UTF-8');
$writer->startElement('params');
foreach($params as $key => $data) {
$writer->startElement('param');
$writer->writeAttribute('name', $key);
$writer->text($data);
$writer->endElement();
}
$writer->endElement();
$writer->endDocument();
$this->_xmlrequest = $writer->outputMemory();
if ($sentrequest) {
$this->_xmlresponse = $this->send_request();
}
}
/**
* Call to common-info
* @param nothing
* @return nothing
*/
public function set_session_cookie($data) {
$sessionval = false;
$sessionstart = strpos($data, 'BREEZESESSION=');
if (false !== $sessionstart) {
$sessionend = strpos($data, ';');
// $sessionend = strpos($data, 'Expires:');
$sessionlength = strlen('BREEZESESSION=');
$sessionvallength = $sessionend - ($sessionstart + $sessionlength);
$sessionval = substr($data, $sessionstart+$sessionlength, $sessionvallength);
}
$this->_cookie = $sessionval;
return $sessionval;
}
/**
* Parses through xml and looks for the 'status' parameter
* and return true if the 'code' attribute equals 'ok' otherwise
* false is returned
* @param string xml the xml to parse
*/
public function read_status_xml() {
$reader = new XMLReader();
$reader->XML($this->_xmlresponse, 'UTF-8');
$return = false;
while ($reader->read()) {
if (0 == strcmp($reader->name, 'status')) {
if (1 == $reader->nodeType) {
if (0 == strcmp('ok', $reader->getAttribute('code'))) {
$return = true;
}
}
}
}
$reader->close();
return $return;
}
/**
* Parses through xml and looks for the 'cookie' parameter
* @param string $xml the xml to parse through
* @return string $sessoin returns the session id
*/
public function read_cookie_xml($xml = '') {
global $CFG, $USER, $COURSE;
if (empty($xml)) {
if (is_siteadmin($USER->id)) {
notice(get_string('adminemptyxml', 'adobeconnect'),
$CFG->wwwroot . '/admin/settings.php?section=modsettingadobeconnect');
} else {
notice(get_string('emptyxml', 'adobeconnect'),
'', $COURSE);
}
}
$session = false;
// $accountid = false;
$reader = new XMLReader();
$reader->XML($xml, 'UTF-8');
while ($reader->read()) {
if (0 == strcmp($reader->name, 'cookie')) {
if (1 == $reader->nodeType) {
$session = $reader->readString();
}
}
}
$reader->close();
$this->_cookie = $session;
return $session;
}
public function response_to_object() {
$xml = new SimpleXMLElement($this->_xmlresponse);
return $xml;
}
public function call_success() {
global $CFG, $USER, $COURSE;
if (empty($this->_xmlresponse)) {
if (is_siteadmin($USER->id)) {
notice(get_string('adminemptyxml', 'adobeconnect'),
$CFG->wwwroot . '/admin/settings.php?section=modsettingadobeconnect');
} else {
notice(get_string('emptyxml', 'adobeconnect'),
'', $COURSE);
}
}
$xml = new SimpleXMLElement($this->_xmlresponse);
if (0 == strcmp('ok', $xml->status[0]['code'])) {
return true;
} else {
return false;
}
}
private function create_http_head_login_xml() {
@date_default_timezone_set("GMT");
$writer = new XMLWriter();
$writer->openMemory();
$writer->startDocument('1.0', 'UTF-8');
$writer->startElement('params');
$writer->startElement('param');
$writer->writeAttribute('name', 'action');
$writer->text('login');
$writer->endElement();
$writer->startElement('param');
$writer->writeAttribute('name', 'external-auth');
$writer->text('use');
$writer->endElement();
$writer->endElement();
$writer->endDocument();
$this->_xmlrequest = $writer->outputMemory();
}
}