Completed
Pull Request — master (#33)
by Jesus
02:27
created

bbb_broker.php ➔ bigbluebuttonbn_broker_meeting_info()   C

Complexity

Conditions 16
Paths 32

Size

Total Lines 50
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 16
eloc 38
nc 32
nop 2
dl 0
loc 50
rs 5.3533
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
defined('MOODLE_INTERNAL') || die();
26
27
require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
28
require_once(dirname(__FILE__).'/locallib.php');
29
30
use \Firebase\JWT\JWT;
31
32
global $PAGE, $USER, $CFG, $SESSION, $DB;
33
34
$params['action'] = optional_param('action', '', PARAM_TEXT);
35
$params['callback'] = optional_param('callback', '', PARAM_TEXT);
36
$params['id'] = optional_param('id', '', PARAM_TEXT);
37
$params['idx'] = optional_param('idx', '', PARAM_TEXT);
38
$params['bigbluebuttonbn'] = optional_param('bigbluebuttonbn', 0, PARAM_INT);
39
$params['signed_parameters'] = optional_param('signed_parameters', '', PARAM_TEXT);
40
41
$error = '';
42
43
if (empty($params['action'])) {
44
    $error = bigbluebuttonbn_add_error($error, 'Parameter [action] was not included');
45
} else {
46
    $error = bigbluebuttonbn_validate_parameters($params);
47
48
    if (empty($error)) {
49
        if (isset($params['bigbluebuttonbn']) && $params['bigbluebuttonbn'] != 0) {
50
            $bigbluebuttonbn = $DB->get_record('bigbluebuttonbn', array('id' => $params['bigbluebuttonbn']), '*', MUST_EXIST);
51
            $course = $DB->get_record('course', array('id' => $bigbluebuttonbn->course), '*', MUST_EXIST);
52
            $cm = get_coursemodule_from_instance('bigbluebuttonbn', $bigbluebuttonbn->id, $course->id, false, MUST_EXIST);
53
            $context = context_module::instance($cm->id);
54
        }
55
56
        if ($params['action'] != 'recording_ready' && $params['action'] != 'meeting_events') {
57
            if (isset($SESSION->bigbluebuttonbn_bbbsession) && !is_null($SESSION->bigbluebuttonbn_bbbsession)) {
58
                $bbbsession = $SESSION->bigbluebuttonbn_bbbsession;
59
            } else {
60
                $error = bigbluebuttonbn_add_error($error, 'No session variable set');
61
            }
62
        }
63
    }
64
}
65
66
header('Content-Type: application/javascript; charset=utf-8');
67
if (empty($error)) {
68
    if (!isloggedin() && $PAGE->course->id == SITEID) {
69
        $userid = guest_user()->id;
70
    } else {
71
        $userid = $USER->id;
72
    }
73
    $hascourseaccess = ($PAGE->course->id == SITEID) || can_access_course($PAGE->course, $userid);
74
75
    if (!$hascourseaccess) {
76
        header('HTTP/1.0 401 Unauthorized');
77
78
        return;
79
    } else {
80
        $instancetypeprofiles = bigbluebuttonbn_get_instancetypeprofiles();
81
        $features = $instancetypeprofiles[0]['features'];
82 View Code Duplication
        if (isset($bbbsession['bigbluebuttonbn']->type)) {
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...
83
            $features = $instancetypeprofiles[$bbbsession['bigbluebuttonbn']->type]['features'];
84
        }
85
        $showroom = (in_array('all', $features) || in_array('showroom', $features));
86
        $showrecordings = (in_array('all', $features) || in_array('showrecordings', $features));
87
        $importrecordings = (in_array('all', $features) || in_array('importrecordings', $features));
88
89
        try {
90
            switch (strtolower($params['action'])) {
91
                case 'meetinginfo':
92
                    bigbluebuttonbn_broker_meeting_info($bbbsession, $params);
93
                    break;
94
                case 'meeting_end':
95
                    bigbluebuttonbn_broker_meeting_end($bbbsession, $params, $bigbluebuttonbn, $cm);
96
                    break;
97
                case 'recording_links':
98
                    bigbluebuttonbn_broker_recording_links($bbbsession, $params);
99
                    break;
100
                case 'recording_info':
101
                    bigbluebuttonbn_broker_recording_info($bbbsession, $params, $showroom);
102
                    break;
103
                case 'recording_publish':
104
                case 'recording_unpublish':
105
                case 'recording_delete':
106
                    bigbluebuttonbn_broker_recording_action($bbbsession, $params, $showroom, $bigbluebuttonbn, $cm);
107
                    break;
108
                case 'recording_ready':
109
                    bigbluebuttonbn_broker_recording_ready($params, $bigbluebuttonbn);
110
                    break;
111
                case 'recording_import':
112
                    bigbluebuttonbn_broker_recording_import($bbbsession, $params, $bigbluebuttonbn, $cm);
113
                    break;
114
                case 'meeting_events':
115
                    bigbluebuttonbn_broker_meeting_events($params, $bigbluebuttonbn, $cm);
116
                    break;
117
                case 'moodle_event':
118
                    break;
119
            }
120
        } catch (Exception $e) {
121
            header('HTTP/1.0 500 Internal Server Error. '.$e->getMessage());
122
            return;
123
        }
124
    }
125
} else {
126
    header('HTTP/1.0 400 Bad Request. '.$error);
127
}
128
129
function bigbluebuttonbn_broker_meeting_info($bbbsession, $params) {
130
131
    $meetinginfo = bigbluebuttonbn_get_meetinginfo($params['id']);
132
    $meetingrunning = bigbluebuttonbn_is_meetingrunning($meetinginfo);
133
134
    $statuscanend = '';
135
    $statuscantag = '';
136
    if ($meetingrunning) {
137
        $joinbuttontext = get_string('view_conference_action_join', 'bigbluebuttonbn');
138
        $initialmessage = get_string('view_error_userlimit_reached', 'bigbluebuttonbn');
139
        $canjoin = false;
140
        if ($bbbsession['userlimit'] == 0 || $meetinginfo->participantCount < $bbbsession['userlimit']) {
141
            $initialmessage = get_string('view_message_conference_in_progress', 'bigbluebuttonbn');
142
            $canjoin = true;
143
        }
144
145
        if ($bbbsession['administrator'] || $bbbsession['moderator']) {
146
            $endbuttontext = get_string('view_conference_action_end', 'bigbluebuttonbn');
147
            $statuscanend = '"can_end": true, "endbuttontext": "'.$endbuttontext.'"';
148
        }
149
    } else {
150
        // If user is administrator, moderator or if is viewer and no waiting is required.
151
        $joinbuttontext = get_string('view_conference_action_join', 'bigbluebuttonbn');
152
        $initialmessage = get_string('view_message_conference_room_ready', 'bigbluebuttonbn');
153
        $canjoin = true;
154
        if (!$bbbsession['administrator'] && !$bbbsession['moderator'] && $bbbsession['wait']) {
155
            $initialmessage = get_string('view_message_conference_not_started', 'bigbluebuttonbn');
156
            if ($bbbsession['wait']) {
157
                $initialmessage .= ' '.get_string('view_message_conference_wait_for_moderator', 'bigbluebuttonbn');
158
            }
159
            $canjoin = false;
160
        }
161
162
        $cantag = false;
163
        if ($bbbsession['tagging'] && ($bbbsession['administrator'] || $bbbsession['moderator'])) {
164
            $cantag = true;
165
        }
166
        $statuscanend = '"can_tag": '.($cantag ? 'true' : 'false');
167
    }
168
    $statuscanjoin = '"can_join": '.($canjoin ? 'true' : 'false');
169
    echo $params['callback'].'({"running": '.($meetingrunning ? 'true' : 'false').
170
                              ',"info": '.json_encode($meetinginfo).
171
                              ',"status": {'.'"join_url": "'.$bbbsession['joinURL'].'", '.
172
                                             '"joinbuttontext": "'.$joinbuttontext.'", '.
173
                                             '"message": "'.$initialmessage.'", '.
174
                                             $statuscanjoin.', '.
175
                                             $statuscanend.', '.
176
                                             $statuscantag.', '.
177
                                          '}});';
178
}
179
180
function bigbluebuttonbn_broker_meeting_end($bbbsession, $params, $bigbluebuttonbn, $cm) {
181
182
    if (!$bbbsession['administrator'] && !$bbbsession['moderator']) {
183
        header('HTTP/1.0 401 Unauthorized. User not authorized to execute end command');
184
        return;
185
    }
186
187
    // Execute the end command.
188
    bigbluebuttonbn_end_meeting($params['id'], $bbbsession['modPW']);
189
    // Moodle event logger: Create an event for meeting ended.
190
    if (isset($bigbluebuttonbn)) {
191
        bigbluebuttonbn_event_log(BIGBLUEBUTTON_EVENT_MEETING_ENDED, $bigbluebuttonbn, $cm);
192
    }
193
    // Update the cache.
194
    bigbluebuttonbn_get_meetinginfo($params['id'], BIGBLUEBUTTONBN_FORCED);
195
196
    echo $params['callback'].'({ "status": true });';
197
}
198
199
function bigbluebuttonbn_broker_recording_links($bbbsession, $params) {
200
201
    if (!$bbbsession['managerecordings']) {
202
        header('HTTP/1.0 401 Unauthorized. User not authorized to execute end command');
203
        return;
204
    }
205
206
    $out = $params['callback'].'({"status": "false"});';
207
    if (isset($params['id']) && $params['id'] != '') {
208
        $importedall = bigbluebuttonbn_get_recording_imported_instances($params['id']);
209
        $out = $params['callback'].'({ "status": "true", "links": '.count($importedall).'});';
210
    }
211
    echo $out;
212
}
213
214
function bigbluebuttonbn_broker_recording_info($bbbsession, $params, $showroom) {
215
216
    if (!$bbbsession['managerecordings']) {
217
        header('HTTP/1.0 401 Unauthorized. User not authorized to execute end command');
218
        return;
219
    }
220
221
    // Retrieve the array of imported recordings.
222
    $bigbluebuttonbnid = null;
223
    if ($showroom) {
224
        $bigbluebuttonbnid = $bbbsession['bigbluebuttonbn']->id;
225
    }
226
    $recordings = bigbluebuttonbn_get_recordings($bbbsession['course']->id, $bigbluebuttonbnid, $showroom,
227
        $bbbsession['bigbluebuttonbn']->recordings_deleted_activities);
228
    if (isset($recordings[$params['id']])) {
229
        // Look up for an update on the imported recording.
230
        $recording = $recordings[$params['id']];
231
        $out = $params['callback'].'({ "status": "false" });';
232
        if (isset($recording) && !empty($recording) && !array_key_exists('messageKey', $recording)) {
233
            // The recording was found.
234
            $out = $params['callback'].'({ "status": "true", "published": "'.$recording['published'].'"});';
235
        }
236
        echo $out;
237
        return;
238
    }
239
240
    // As the recordingid was not identified as imported recording link, look up for a real recording.
241
    $recording = bigbluebuttonbn_get_recordings_array($params['idx'], $params['id']);
242
    $out = $params['callback'].'({"status": "false"});';
243
    if (isset($recording) && !empty($recording) && array_key_exists($params['id'], $recording)) {
244
        // The recording was found.
245
        $out = $params['callback'].'({ "status": "true", "published": "'.$recording[$params['id']]['published'].'"});';
246
    }
247
    echo $out;
248
}
249
250
function bigbluebuttonbn_broker_recording_action($bbbsession, $params, $showroom, $bigbluebuttonbn, $cm) {
251
252
    if (!$bbbsession['managerecordings']) {
253
        header('HTTP/1.0 401 Unauthorized. User not authorized to execute end command');
254
        return;
255
    }
256
257
    $callbackresponse = array();
258
    $callbackresponse['status'] = 'false';
259
    $eventlog = null;
260
261
    // Retrieve array of recordings that includes real and imported.
262
    $bigbluebuttonbnid = null;
263
    if ($showroom) {
264
        $bigbluebuttonbnid = $bbbsession['bigbluebuttonbn']->id;
265
    }
266
    $recordings = bigbluebuttonbn_get_recordings($bbbsession['course']->id, $bigbluebuttonbnid, $showroom,
267
        $bbbsession['bigbluebuttonbn']->recordings_deleted_activities);
268
269
    // Excecute action.
270
    switch (strtolower($params['action'])) {
271
        case 'recording_publish':
272
            $callbackresponse = bigbluebuttonbn_broker_recording_action_publish($bbbsession, $params, $recordings);
273
            $eventlog = BIGBLUEBUTTON_EVENT_RECORDING_PUBLISHED;
274
            break;
275
        case 'recording_unpublish':
276
            $callbackresponse = bigbluebuttonbn_broker_recording_action_unpublish($bbbsession, $params, $recordings);;
277
            $eventlog = BIGBLUEBUTTON_EVENT_RECORDING_UNPUBLISHED;
278
            break;
279
        case 'recording_delete':
280
            $callbackresponse = bigbluebuttonbn_broker_recording_action_delete($bbbsession, $params, $recordings);
281
            $eventlog = BIGBLUEBUTTON_EVENT_RECORDING_DELETED;
282
            break;
283
    }
284
285
    if (isset($bigbluebuttonbn) && $callbackresponse['status'] === 'true') {
286
        // Moodle event logger: Create an event for action performed on recording.
287
        bigbluebuttonbn_event_log($eventlog, $bigbluebuttonbn, $cm);
288
    }
289
290
    $callbackresponsedata = json_encode($callbackresponse);
291
    echo "{$params['callback']}({$callbackresponsedata});";
292
}
293
294
function bigbluebuttonbn_broker_recording_action_publish($bbbsession, $params, $recordings) {
295
296
    $status = 'true';
297
    if (isset($recordings[$params['id']]) && isset($recordings[$params['id']]['imported'])) {
298
        // Execute publish on imported recording link, if the real recording is published.
299
        $realrecordings = bigbluebuttonbn_get_recordings_array($recordings[$params['id']]['meetingID'],
300
                                                             $recordings[$params['id']]['recordID']);
301
        $status = $realrecordings[$params['id']]['published'];
302
        if ($status === 'true') {
303
            // Only if the physical recording is published, execute publish on imported recording link.
304
            bigbluebuttonbn_publish_recording_imported($params['id'], $bbbsession['bigbluebuttonbn']->id, true);
305
        }
306
    } else {
307
        // As the recordingid was not identified as imported recording link, execute publish on a real recording.
308
        bigbluebuttonbn_publish_recordings($params['id'], 'true');
309
    }
310
311
    $response = array('status' => $status);
312
    if ($status === 'false') {
313
        $response['message'] = get_string('view_recording_publish_link_error', 'bigbluebuttonbn');
314
    }
315
    return $response;
316
}
317
318
function bigbluebuttonbn_broker_recording_action_unpublish($bbbsession, $params, $recordings) {
319
    global $DB;
320
321 View Code Duplication
    if (isset($recordings[$params['id']]) && isset($recordings[$params['id']]['imported'])) {
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...
322
        // Execute unpublish on imported recording link.
323
        bigbluebuttonbn_publish_recording_imported($params['id'], $bbbsession['bigbluebuttonbn']->id, false);
324
        return array('status' => 'true');
325
    }
326
327
    // As the recordingid was not identified as imported recording link, execute unpublish on a real recording.
328
    // First: Unpublish imported links associated to the recording.
329
    $importedall = bigbluebuttonbn_get_recording_imported_instances($params['id']);
330
331
    if ($importedall > 0) {
332
        foreach ($importedall as $key => $record) {
333
            $meta = json_decode($record->meta, true);
334
            // Prepare data for the update.
335
            $meta['recording']['published'] = 'false';
336
            $importedall[$key]->meta = json_encode($meta);
337
338
            // Proceed with the update.
339
            $DB->update_record('bigbluebuttonbn_logs', $importedall[$key]);
340
        }
341
    }
342
    // Second: Execute the real unpublish.
343
    bigbluebuttonbn_publish_recordings($params['id'], 'false');
344
    return array('status' => 'true');
345
}
346
347
function bigbluebuttonbn_broker_recording_action_delete($bbbsession, $params, $recordings) {
348
    global $DB;
349
350 View Code Duplication
    if (isset($recordings[$params['id']]) && isset($recordings[$params['id']]['imported'])) {
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...
351
        // Execute delete on imported recording link.
352
        bigbluebuttonbn_delete_recording_imported($params['id'], $bbbsession['bigbluebuttonbn']->id);
353
        return array('status' => 'true');
354
    }
355
356
    // As the recordingid was not identified as imported recording link, execute delete on a real recording.
357
    // First: Delete imported links associated to the recording.
358
    $importedall = bigbluebuttonbn_get_recording_imported_instances($params['id']);
359
360
    if ($importedall > 0) {
361
        foreach (array_keys($importedall) as $key) {
362
            // Execute delete.
363
            $DB->delete_records('bigbluebuttonbn_logs', array('id' => $key));
364
        }
365
    }
366
    // Second: Execute the real delete.
367
    bigbluebuttonbn_delete_recordings($params['id']);
368
369
    return array('status' => 'true');
370
}
371
372
function bigbluebuttonbn_broker_recording_ready($params, $bigbluebuttonbn) {
373
374
    // Decodes the received JWT string.
375
    try {
376
        $decodedparameters = JWT::decode($params['signed_parameters'], bigbluebuttonbn_get_cfg_shared_secret(),
377
            array('HS256'));
378
    } catch (Exception $e) {
379
        $error = 'Caught exception: '.$e->getMessage();
380
        header('HTTP/1.0 400 Bad Request. '.$error);
381
        return;
382
    }
383
384
    // Validate that the bigbluebuttonbn activity corresponds to the meeting_id received.
385
    $meetingidelements = explode('[', $decodedparameters->meeting_id);
386
    $meetingidelements = explode('-', $meetingidelements[0]);
387
388 View Code Duplication
    if (!isset($bigbluebuttonbn) || $bigbluebuttonbn->meetingid != $meetingidelements[0]) {
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...
389
        $error = 'Caught exception: '.$e->getMessage();
0 ignored issues
show
Bug introduced by
The variable $e seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
390
        header('HTTP/1.0 410 Gone. '.$error);
391
        return;
392
    }
393
394
    // Sends the messages.
395
    try {
396
        bigbluebuttonbn_send_notification_recording_ready($bigbluebuttonbn);
397
        header('HTTP/1.0 202 Accepted');
398
    } catch (Exception $e) {
399
        $error = 'Caught exception: '.$e->getMessage();
400
        header('HTTP/1.0 503 Service Unavailable. '.$error);
401
    }
402
}
403
404
function bigbluebuttonbn_broker_recording_import($bbbsession, $params, $bigbluebuttonbn, $cm) {
405
    global $SESSION;
406
407
    if (!$bbbsession['managerecordings']) {
408
        header('HTTP/1.0 401 Unauthorized. User not authorized to execute end command');
409
        return;
410
    }
411
412
    $importrecordings = $SESSION->bigbluebuttonbn_importrecordings;
413
    if (!isset($importrecordings[$params['id']])) {
414
        $error = "Recording {$params['id']} could not be found. It can not be imported";
415
        header('HTTP/1.0 404 Not found. '.$error);
416
        return;
417
    }
418
419
    $importrecordings[$params['id']]['imported'] = true;
420
    $overrides['meetingid'] = $importrecordings[$params['id']]['meetingID'];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$overrides was never initialized. Although not strictly required by PHP, it is generally a good practice to add $overrides = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
421
    $meta = '{"recording":'.json_encode($importrecordings[$params['id']]).'}';
422
    bigbluebuttonbn_logs($bbbsession, BIGBLUEBUTTONBN_LOG_EVENT_IMPORT, $overrides, $meta);
423
    // Moodle event logger: Create an event for recording imported.
424
    if (isset($bigbluebuttonbn)) {
425
        bigbluebuttonbn_event_log(BIGBLUEBUTTON_EVENT_RECORDING_IMPORTED, $bigbluebuttonbn, $cm);
426
    }
427
428
    $callbackresponse['status'] = 'true';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$callbackresponse was never initialized. Although not strictly required by PHP, it is generally a good practice to add $callbackresponse = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
429
    $callbackresponsedata = json_encode($callbackresponse);
430
    echo "{$params['callback']}({$callbackresponsedata});";
431
}
432
433
function bigbluebuttonbn_broker_meeting_events($params, $bigbluebuttonbn, $cm) {
434
    // Decodes the received JWT string.
435
    try {
436
        $decodedparameters = JWT::decode($params['signed_parameters'], bigbluebuttonbn_get_cfg_shared_secret(),
437
            array('HS256'));
438
    } catch (Exception $e) {
439
        $error = 'Caught exception: '.$e->getMessage();
440
        header('HTTP/1.0 400 Bad Request. '.$error);
441
        return;
442
    }
443
444
    // Validate that the bigbluebuttonbn activity corresponds to the meeting_id received.
445
    $meetingidelements = explode('[', $decodedparameters->meeting_id);
446
    $meetingidelements = explode('-', $meetingidelements[0]);
447
448 View Code Duplication
    if (!isset($bigbluebuttonbn) || $bigbluebuttonbn->meetingid != $meetingidelements[0]) {
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...
449
        $error = 'Caught exception: '.$e->getMessage();
0 ignored issues
show
Bug introduced by
The variable $e seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
450
        header('HTTP/1.0 410 Gone. '.$error);
451
        return;
452
    }
453
454
    // Store the events.
455
    try {
456
        foreach ($decodedparameters->events as $event) {
457
            bigbluebuttonbn_meeting_event_log($event, $bigbluebuttonbn, $cm);
458
        }
459
        header('HTTP/1.0 202 Accepted');
460
    } catch (Exception $e) {
461
        $error = "Caught exception: {$e->getMessage()}";
462
        header("HTTP/1.0 503 Service Unavailable. {$error}");
463
    }
464
}
465