Completed
Push — master ( f92a9e...10847d )
by Jesus
02:50
created

bbb_broker.php ➔ bigbluebuttonbn_broker_required_parameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 19
nc 1
nop 0
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
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.
13
//
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/>.
16
17
/**
18
 * Intermediator for managing actions executed by the BigBlueButton server.
19
 *
20
 * @author    Jesus Federico  (jesus [at] blindsidenetworks [dt] com)
21
 * @copyright 2015-2017 Blindside Networks Inc
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v2 or later
23
 */
24
25
require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
26
require_once(dirname(__FILE__).'/locallib.php');
27
28
use \Firebase\JWT\JWT;
29
30
global $PAGE, $USER, $CFG, $SESSION, $DB;
31
32
$params['action'] = optional_param('action', '', PARAM_TEXT);
33
$params['callback'] = optional_param('callback', '', PARAM_TEXT);
34
$params['id'] = optional_param('id', '', PARAM_TEXT);
35
$params['idx'] = optional_param('idx', '', PARAM_TEXT);
36
$params['bigbluebuttonbn'] = optional_param('bigbluebuttonbn', 0, PARAM_INT);
37
$params['signed_parameters'] = optional_param('signed_parameters', '', PARAM_TEXT);
38
$params['forced'] = optional_param('forced', 'false', PARAM_TEXT);
39
$params['meta'] = optional_param('meta', '', PARAM_TEXT);
40
41
if (empty($params['action'])) {
42
    header('HTTP/1.0 400 Bad Request. Parameter ['.$params['action'].'] was not included');
43
    return;
44
}
45
46
$error = bigbluebuttonbn_broker_validate_parameters($params);
47
if (!empty($error)) {
48
    header('HTTP/1.0 400 Bad Request. '.$error);
49
    return;
50
}
51
52
if (isset($params['bigbluebuttonbn']) && $params['bigbluebuttonbn'] != 0) {
53
    $bigbluebuttonbn = $DB->get_record('bigbluebuttonbn', array('id' => $params['bigbluebuttonbn']), '*', MUST_EXIST);
54
    $course = $DB->get_record('course', array('id' => $bigbluebuttonbn->course), '*', MUST_EXIST);
55
    $cm = get_coursemodule_from_instance('bigbluebuttonbn', $bigbluebuttonbn->id, $course->id, false, MUST_EXIST);
56
    $context = context_module::instance($cm->id);
57
}
58
59
if ($params['action'] != 'recording_ready' && $params['action'] != 'live_session_events') {
60
    if (!isset($SESSION->bigbluebuttonbn_bbbsession) || is_null($SESSION->bigbluebuttonbn_bbbsession)) {
61
        header('HTTP/1.0 400 Bad Request. No session variable set');
62
        return;
63
    }
64
    $bbbsession = $SESSION->bigbluebuttonbn_bbbsession;
65
}
66
67
if (!isloggedin() && $PAGE->course->id == SITEID) {
68
    $userid = guest_user()->id;
69
} else {
70
    $userid = $USER->id;
71
}
72
$hascourseaccess = ($PAGE->course->id == SITEID) || can_access_course($PAGE->course, $userid);
73
74
if (!$hascourseaccess) {
75
    header('HTTP/1.0 401 Unauthorized');
76
    return;
77
}
78
79
$type = null;
80
if (isset($bbbsession['bigbluebuttonbn']->type)) {
81
    $type = $bbbsession['bigbluebuttonbn']->type;
82
}
83
84
$typeprofiles = bigbluebuttonbn_get_instance_type_profiles();
85
$enabledfeatures = bigbluebuttonbn_get_enabled_features($typeprofiles, $type);
86
87
try {
88
    header('Content-Type: application/javascript; charset=utf-8');
89
    $a = strtolower($params['action']);
90
    if ($a == 'meeting_info') {
91
        $meetinginfo = bigbluebuttonbn_broker_meeting_info($bbbsession, $params, ($params['forced'] == 'true'));
92
        echo $meetinginfo;
93
        return;
94
    }
95
96
    if ($a == 'meeting_end') {
97
        $meetingend = bigbluebuttonbn_broker_meeting_end($bbbsession, $params, $bbbsession['bigbluebuttonbn'], $bbbsession['cm']);
98
        echo $meetingend;
99
        return;
100
    }
101
102
    if ($a == 'recording_links') {
103
        $recordinglinks = bigbluebuttonbn_broker_recording_links($bbbsession, $params);
104
        echo $recordinglinks;
105
        return;
106
    }
107
108
    if ($a == 'recording_info') {
109
        $recordinginfo = bigbluebuttonbn_broker_recording_info($bbbsession, $params, $enabledfeatures['showroom']);
110
        echo $recordinginfo;
111
        return;
112
    }
113
114
    if ($a == 'recording_publish' || $a == 'recording_unpublish' || $a == 'recording_delete' || $a == 'recording_edit') {
115
        $recordingaction = bigbluebuttonbn_broker_recording_action(
116
            $bbbsession, $params, $enabledfeatures['showroom'], $bbbsession['bigbluebuttonbn'], $bbbsession['cm']);
117
        echo $recordingaction;
118
        return;
119
    }
120
121
    if ($a == 'recording_import') {
122
        echo bigbluebuttonbn_broker_recording_import($bbbsession, $params);
123
        return;
124
    }
125
126
    if ($a == 'recording_ready') {
127
        bigbluebuttonbn_broker_recording_ready($params, $bigbluebuttonbn);
128
        return;
129
    }
130
131
    if ($a == 'live_session_events') {
132
        bigbluebuttonbn_broker_live_session_events($params, $bigbluebuttonbn, $cm);
133
        return;
134
    }
135
136
    header('HTTP/1.0 400 Bad request. The action '. $a . ' doesn\'t exist');
137
    return;
138
139
} catch (Exception $e) {
140
    header('HTTP/1.0 500 Internal Server Error. '.$e->getMessage());
141
    return;
142
}
143
144
function bigbluebuttonbn_broker_meeting_info($bbbsession, $params, $forced) {
145
    $callbackresponse = array();
146
147
    $info = bigbluebuttonbn_get_meeting_info($params['id'], $forced);
148
    $callbackresponse['info'] = $info;
149
150
    $running = false;
151
    if ($info['returncode'] == 'SUCCESS') {
152
        $running = ($info['running'] === 'true');
153
    }
154
    $callbackresponse['running'] = $running;
155
156
    $status = array();
157
    $status["join_url"] = $bbbsession['joinURL'];
158
    $status["join_button_text"] = get_string('view_conference_action_join', 'bigbluebuttonbn');
159
    $status["end_button_text"] = get_string('view_conference_action_end', 'bigbluebuttonbn');
160
161
    $participantcount = 0;
162
    if (isset($info['participantCount'])) {
163
        $participantcount = $info['participantCount'];
164
    }
165
    $canjoin = bigbluebuttonbn_broker_meeting_info_can_join($bbbsession, $running, $participantcount);
166
    $status["can_join"] = $canjoin["can_join"];
167
    $status["message"] = $canjoin["message"];
168
169
    $canend = bigbluebuttonbn_broker_meeting_info_can_end($bbbsession, $running);
170
    $status["can_end"] = $canend["can_end"];
171
172
    $callbackresponse['status'] = $status;
173
174
    $callbackresponsedata = json_encode($callbackresponse);
175
    return "{$params['callback']}({$callbackresponsedata});";
176
}
177
178
function bigbluebuttonbn_broker_meeting_info_can_join($bbbsession, $running, $participantcount) {
179
    $status = array("can_join" => false);
180
    if ($running) {
181
        $status["message"] = get_string('view_error_userlimit_reached', 'bigbluebuttonbn');
182 View Code Duplication
        if ($bbbsession['userlimit'] == 0 || $participantcount < $bbbsession['userlimit']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
183
            $status["message"] = get_string('view_message_conference_in_progress', 'bigbluebuttonbn');
184
            $status["can_join"] = true;
185
        }
186
        return $status;
187
    }
188
189
    // If user is administrator, moderator or if is viewer and no waiting is required.
190
    $status["message"] = get_string('view_message_conference_wait_for_moderator', 'bigbluebuttonbn');
191 View Code Duplication
    if ($bbbsession['administrator'] || $bbbsession['moderator'] || !$bbbsession['wait']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
192
        $status["message"] = get_string('view_message_conference_room_ready', 'bigbluebuttonbn');
193
        $status["can_join"] = true;
194
    }
195
    return $status;
196
}
197
198
function bigbluebuttonbn_broker_meeting_info_can_end($bbbsession, $running) {
199
    $status = array("can_end" => false);
200
    if ($running && ($bbbsession['administrator'] || $bbbsession['moderator'])) {
201
        $status["can_end"] = true;
202
    }
203
    return $status;
204
}
205
206
function bigbluebuttonbn_broker_meeting_end($bbbsession, $params, $bigbluebuttonbn, $cm) {
207
208
    if (!$bbbsession['administrator'] && !$bbbsession['moderator']) {
209
        header('HTTP/1.0 401 Unauthorized. User not authorized to execute end command');
210
        return;
211
    }
212
213
    $callbackresponse = array('status' => true);
214
215
    // Execute the end command.
216
    bigbluebuttonbn_end_meeting($params['id'], $bbbsession['modPW']);
217
    // Moodle event logger: Create an event for meeting ended.
218
    if (isset($bigbluebuttonbn)) {
219
        bigbluebuttonbn_event_log(BIGBLUEBUTTON_EVENT_MEETING_ENDED, $bigbluebuttonbn, $cm);
220
    }
221
    // Update the cache.
222
    bigbluebuttonbn_get_meeting_info($params['id'], BIGBLUEBUTTONBN_FORCED);
223
224
    $callbackresponsedata = json_encode($callbackresponse);
225
    return "{$params['callback']}({$callbackresponsedata});";
226
}
227
228
function bigbluebuttonbn_broker_recording_links($bbbsession, $params) {
229
230
    if (!$bbbsession['managerecordings']) {
231
        header('HTTP/1.0 401 Unauthorized. User not authorized to execute end command');
232
        return;
233
    }
234
235
    $callbackresponse = array('status' => false);
236
237
    if (isset($params['id']) && $params['id'] != '') {
238
        $importedall = bigbluebuttonbn_get_recording_imported_instances($params['id']);
239
        $callbackresponse['status'] = true;
240
        $callbackresponse['links'] = count($importedall);
241
    }
242
    $callbackresponsedata = json_encode($callbackresponse);
243
    return "{$params['callback']}({$callbackresponsedata});";
244
}
245
246
function bigbluebuttonbn_broker_recording_info($bbbsession, $params, $showroom) {
247
248
    if (!$bbbsession['managerecordings']) {
249
        header('HTTP/1.0 401 Unauthorized. User not authorized to execute end command');
250
        return;
251
    }
252
253
    $callbackresponse = array('status' => false);
254
255
    $courseid = $bbbsession['course']->id;
256
    $bigbluebuttonbnid = null;
257
    if ($showroom) {
258
        $bigbluebuttonbnid = $bbbsession['bigbluebuttonbn']->id;
259
    }
260
    $includedeleted = $bbbsession['bigbluebuttonbn']->recordings_deleted_activities;
261
    // Retrieve the array of imported recordings.
262
    $recordings = bigbluebuttonbn_get_recordings($courseid, $bigbluebuttonbnid, $showroom, $includedeleted);
263
    if (array_key_exists($params['id'], $recordings)) {
264
        // Look up for an update on the imported recording.
265 View Code Duplication
        if (!array_key_exists('messageKey', $recordings[$params['id']])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
266
            // The recording was found.
267
            $callbackresponse = bigbluebuttonbn_broker_recording_info_current($recordings[$params['id']], $params);
268
        }
269
        $callbackresponsedata = json_encode($callbackresponse);
270
        return "{$params['callback']}({$callbackresponsedata});";
271
    }
272
273
    // As the recordingid was not identified as imported recording link, look up for a real recording.
274
    $recordings = bigbluebuttonbn_get_recordings_array($params['idx'], $params['id']);
275 View Code Duplication
    if (array_key_exists($params['id'], $recordings)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
276
        // The recording was found.
277
        $callbackresponse = bigbluebuttonbn_broker_recording_info_current($recordings[$params['id']], $params);
278
    }
279
    $callbackresponsedata = json_encode($callbackresponse);
280
    return "{$params['callback']}({$callbackresponsedata});";
281
}
282
283
function bigbluebuttonbn_broker_recording_info_current($recording, $params) {
284
    $callbackresponse['status'] = true;
285
    $callbackresponse['published'] = (string) $recording['published'];
286
    if (!isset($params['meta'])) {
287
        return $callbackresponse;
288
    }
289
290
    $meta = json_decode($params['meta']);
291
    foreach (array_keys($meta) as $key) {
292
        if (isset($recording[$key])) {
293
            $callbackresponse[$key] = $recording[$key];
294
        }
295
    }
296
    return $callbackresponse;
297
}
298
299
function bigbluebuttonbn_broker_recording_action($bbbsession, $params, $showroom, $bigbluebuttonbn, $cm) {
300
    if (!$bbbsession['managerecordings']) {
301
        header('HTTP/1.0 401 Unauthorized. User not authorized to execute end command');
302
        return;
303
    }
304
305
    // Retrieve array of recordings that includes real and imported.
306
    $bigbluebuttonbnid = null;
307
    if ($showroom) {
308
        $bigbluebuttonbnid = $bbbsession['bigbluebuttonbn']->id;
309
    }
310
    $recordings = bigbluebuttonbn_get_recordings($bbbsession['course']->id, $bigbluebuttonbnid, $showroom,
311
        $bbbsession['bigbluebuttonbn']->recordings_deleted_activities);
312
313
    $action = strtolower($params['action']);
314
    $events = bigbluebuttonbn_events_action();
315
316
    // Excecute action.
317
    $eventlog = $events[$action];
318
    $callbackresponse = bigbluebuttonbn_broker_recording_action_perform($action, $bbbsession, $params, $recordings);
319
    if ($callbackresponse['status']) {
320
        // Moodle event logger: Create an event for action performed on recording.
321
        bigbluebuttonbn_event_log($eventlog, $bigbluebuttonbn, $cm);
322
    }
323
324
    $callbackresponsedata = json_encode($callbackresponse);
325
    return "{$params['callback']}({$callbackresponsedata});";
326
}
327
328
function bigbluebuttonbn_broker_recording_action_perform($action, $bbbsession, $params, $recordings) {
329
    if ($action == 'recording_publish') {
330
        return bigbluebuttonbn_broker_recording_action_publish($bbbsession, $params, $recordings);
331
    }
332
    if ($action == 'recording_unpublish') {
333
        return bigbluebuttonbn_broker_recording_action_unpublish($bbbsession, $params, $recordings);
334
    }
335
    if ($action == 'recording_edit') {
336
        return bigbluebuttonbn_broker_recording_action_edit($bbbsession, $params, $recordings);
337
    }
338
    if ($action == 'recording_delete') {
339
        return bigbluebuttonbn_broker_recording_action_delete($bbbsession, $params, $recordings);
340
    }
341
}
342
343
function bigbluebuttonbn_broker_recording_action_publish($bbbsession, $params, $recordings) {
344
    $status = true;
345
    if (bigbluebuttonbn_broker_recording_is_imported($recordings, $params['id'])) {
346
        // Execute publish on imported recording link, if the real recording is published.
347
        $realrecordings = bigbluebuttonbn_get_recordings_array(
348
            $recordings[$params['id']]['meetingID'], $recordings[$params['id']]['recordID']);
349
        $status = ($realrecordings[$params['id']]['published'] === 'true');
350
        if ($status) {
351
            // Only if the physical recording is published, execute publish on imported recording link.
352
            bigbluebuttonbn_publish_recording_imported($params['id'], $bbbsession['bigbluebuttonbn']->id, true);
353
        }
354
    } else {
355
        // As the recordingid was not identified as imported recording link, execute publish on a real recording.
356
        bigbluebuttonbn_publish_recordings($params['id'], 'true');
357
    }
358
359
    $response = array('status' => $status);
360
    if (!$status) {
361
        $response['message'] = get_string('view_recording_publish_link_error', 'bigbluebuttonbn');
362
    }
363
    return $response;
364
}
365
366
function bigbluebuttonbn_broker_recording_action_unpublish($bbbsession, $params, $recordings) {
367
    global $DB;
368
369 View Code Duplication
    if (bigbluebuttonbn_broker_recording_is_imported($recordings, $params['id'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
370
        // Execute unpublish on imported recording link.
371
        return array(
372
          'status' => bigbluebuttonbn_publish_recording_imported(
373
              $params['id'], $bbbsession['bigbluebuttonbn']->id, false
374
            )
375
          );
376
    }
377
378
    // As the recordingid was not identified as imported recording link, execute unpublish on a real recording.
379
    // First: Unpublish imported links associated to the recording.
380
    $importedall = bigbluebuttonbn_get_recording_imported_instances($params['id']);
381
382
    if ($importedall > 0) {
383
        foreach ($importedall as $key => $record) {
384
            $meta = json_decode($record->meta, true);
385
            // Prepare data for the update.
386
            $meta['recording']['published'] = 'false';
387
            $importedall[$key]->meta = json_encode($meta);
388
389
            // Proceed with the update.
390
            $DB->update_record('bigbluebuttonbn_logs', $importedall[$key]);
391
        }
392
    }
393
    // Second: Execute the real unpublish.
394
    return array(
395
      'status' => bigbluebuttonbn_publish_recordings($params['id'], 'false')
396
      );
397
}
398
399
function bigbluebuttonbn_broker_recording_action_edit($bbbsession, $params, $recordings) {
400
    if (bigbluebuttonbn_broker_recording_is_imported($recordings, $params['id'])) {
401
        error_log("Updating imported");
402
        // Execute update on imported recording link.
403
        return array(
404
          'status' => bigbluebuttonbn_update_recording_imported(
405
              $params['id'], $bbbsession['bigbluebuttonbn']->id, json_decode($params['meta'])
406
            )
407
          );
408
    }
409
410
    // As the recordingid was not identified as imported recording link, execute update on a real recording.
411
    // (No need to update imported links as the update only affects the actual recording).
412
    // Execute update on actual recording.
413
    error_log("Updating real");
414
    return array(
415
      'status' => bigbluebuttonbn_update_recordings($params['id'], json_decode($params['meta']))
416
      );
417
}
418
419
function bigbluebuttonbn_broker_recording_action_delete($bbbsession, $params, $recordings) {
420
    global $DB;
421
422 View Code Duplication
    if (bigbluebuttonbn_broker_recording_is_imported($recordings, $params['id'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
423
        // Execute delete on imported recording link.
424
        return array(
425
          'status' => bigbluebuttonbn_delete_recording_imported(
426
              $params['id'], $bbbsession['bigbluebuttonbn']->id
427
            )
428
          );
429
    }
430
431
    // As the recordingid was not identified as imported recording link, execute delete on a real recording.
432
    // Delete imported links associated to the recording.
433
    $importedall = bigbluebuttonbn_get_recording_imported_instances($params['id']);
434
435
    if ($importedall > 0) {
436
        foreach (array_keys($importedall) as $key) {
437
            // Execute delete on imported links.
438
            $DB->delete_records('bigbluebuttonbn_logs', array('id' => $key));
439
        }
440
    }
441
    // Execute the actual delete.
442
    return array(
443
      'status' => bigbluebuttonbn_delete_recordings($params['id'])
444
      );
445
}
446
447
function bigbluebuttonbn_broker_recording_ready($params, $bigbluebuttonbn) {
448
449
    // Decodes the received JWT string.
450
    try {
451
        $decodedparameters = JWT::decode($params['signed_parameters'], bigbluebuttonbn_get_cfg_shared_secret(),
452
            array('HS256'));
453
    } catch (Exception $e) {
454
        $error = 'Caught exception: '.$e->getMessage();
455
        header('HTTP/1.0 400 Bad Request. '.$error);
456
        return;
457
    }
458
459
    // Validate that the bigbluebuttonbn activity corresponds to the meeting_id received.
460
    $meetingidelements = explode('[', $decodedparameters->meeting_id);
461
    $meetingidelements = explode('-', $meetingidelements[0]);
462
463
    if (!isset($bigbluebuttonbn) || $bigbluebuttonbn->meetingid != $meetingidelements[0]) {
464
        header('HTTP/1.0 410 Gone. The activity may have been deleted');
465
        return;
466
    }
467
468
    // Sends the messages.
469
    try {
470
        bigbluebuttonbn_send_notification_recording_ready($bigbluebuttonbn);
471
        header('HTTP/1.0 202 Accepted');
472
    } catch (Exception $e) {
473
        $error = 'Caught exception: '.$e->getMessage();
474
        header('HTTP/1.0 503 Service Unavailable. '.$error);
475
    }
476
}
477
478
function bigbluebuttonbn_broker_recording_import($bbbsession, $params) {
479
    global $SESSION;
480
481
    if (!$bbbsession['managerecordings']) {
482
        header('HTTP/1.0 401 Unauthorized. User not authorized to execute end command');
483
        return;
484
    }
485
486
    $importrecordings = $SESSION->bigbluebuttonbn_importrecordings;
487
    if (!isset($importrecordings[$params['id']])) {
488
        $error = "Recording {$params['id']} could not be found. It can not be imported";
489
        header('HTTP/1.0 404 Not found. '.$error);
490
        return;
491
    }
492
493
    $callbackresponse = array('status' => true);
494
495
    $importrecordings[$params['id']]['imported'] = true;
496
    $overrides = array('meetingid' => $importrecordings[$params['id']]['meetingID']);
497
    $meta = '{"recording":'.json_encode($importrecordings[$params['id']]).'}';
498
    bigbluebuttonbn_logs($bbbsession, BIGBLUEBUTTONBN_LOG_EVENT_IMPORT, $overrides, $meta);
499
    // Moodle event logger: Create an event for recording imported.
500
    if (isset($bbbsession['bigbluebutton']) && isset($bbbsession['cm'])) {
501
        bigbluebuttonbn_event_log(BIGBLUEBUTTON_EVENT_RECORDING_IMPORTED, $bbbsession['bigbluebuttonbn'], $bbbsession['cm']);
502
    }
503
504
    $callbackresponsedata = json_encode($callbackresponse);
505
    return "{$params['callback']}({$callbackresponsedata});";
506
}
507
508
function bigbluebuttonbn_broker_live_session_events($params, $bigbluebuttonbn, $cm) {
509
    // Decodes the received JWT string.
510
    try {
511
        $decodedparameters = JWT::decode($params['signed_parameters'], bigbluebuttonbn_get_cfg_shared_secret(),
512
            array('HS256'));
513
    } catch (Exception $e) {
514
        $error = 'Caught exception: '.$e->getMessage();
515
        header('HTTP/1.0 400 Bad Request. '.$error);
516
        return;
517
    }
518
519
    // Validate that the bigbluebuttonbn activity corresponds to the meeting_id received.
520
    $meetingidelements = explode('[', $decodedparameters->meeting_id);
521
    $meetingidelements = explode('-', $meetingidelements[0]);
522
523
    if (!isset($bigbluebuttonbn) || $bigbluebuttonbn->meetingid != $meetingidelements[0]) {
524
        header('HTTP/1.0 410 Gone. The activity may have been deleted');
525
        return;
526
    }
527
528
    // Store the events.
529
    try {
530
        foreach ($decodedparameters->events as $event) {
531
            bigbluebuttonbn_live_session_event_log($event, $bigbluebuttonbn, $cm);
532
        }
533
        header('HTTP/1.0 202 Accepted');
534
    } catch (Exception $e) {
535
        $error = "Caught exception: {$e->getMessage()}";
536
        header("HTTP/1.0 503 Service Unavailable. {$error}");
537
    }
538
}
539
540
function bigbluebuttonbn_broker_validate_parameters($params) {
541
    $requiredparams = bigbluebuttonbn_broker_required_parameters();
542
543
    if (!isset($params['callback'])) {
544
        return 'This call must include a javascript callback.';
545
    }
546
547
    if (!isset($params['action'])) {
548
        return 'Action parameter must be included.';
549
    }
550
551
    $action = strtolower($params['action']);
552
    if (!array_key_exists($action, $requiredparams)) {
553
        return 'Action '.$params['action'].' can not be performed.';
554
    }
555
556
    return bigbluebuttonbn_broker_validate_parameters_message($params, $requiredparams[$action]);
557
}
558
559
function bigbluebuttonbn_broker_validate_parameters_message($params, $requiredparams) {
560
    foreach ($requiredparams as $param => $message) {
561
        if (!array_key_exists($param, $params) || $params[$param] == '') {
562
            return $message;
563
        }
564
    }
565
}
566
567
function bigbluebuttonbn_broker_required_parameters() {
568
    return [
569
        'server_ping' => ['id' => 'The meetingID must be specified.'],
570
        'meeting_info' => ['id' => 'The meetingID must be specified.'],
571
        'meeting_end' => ['id' => 'The meetingID must be specified.'],
572
        'recording_info' => ['id' => 'The recordingID must be specified.'],
573
        'recording_links' => ['id' => 'The recordingID must be specified.'],
574
        'recording_publish' => ['id' => 'The recordingID must be specified.'],
575
        'recording_unpublish' => ['id' => 'The recordingID must be specified.'],
576
        'recording_delete' => ['id' => 'The recordingID must be specified.'],
577
        'recording_protect' => ['id' => 'The recordingID must be specified.'],
578
        'recording_unprotect' => ['id' => 'The recordingID must be specified.'],
579
        'recording_edit' => ['id' => 'The recordingID must be specified.',
580
            'meta' => 'A meta parameter should be included'],
581
        'recording_import' => ['id' => 'The recordingID must be specified.'],
582
        'recording_ready' => [
583
            'signed_parameters' => 'A JWT encoded string must be included as [signed_parameters].'
584
          ],
585
        'live_session_events' => [
586
            'signed_parameters' => 'A JWT encoded string must be included as [signed_parameters].'
587
          ]
588
      ];
589
}
590
591
function bigbluebuttonbn_broker_recording_is_imported($recordings, $recordingid) {
592
    return (isset($recordings[$recordingid]) && isset($recordings[$recordingid]['imported']));
593
}
594