Completed
Push — master ( 20d163...ca6f65 )
by Jesus
01:59
created

bigbluebutton::can_join_meeting()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 6
nop 1
dl 0
loc 23
rs 9.2408
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
 * The mod_bigbluebuttonbn locallib/bigbluebutton.
19
 *
20
 * @package   mod_bigbluebuttonbn
21
 * @copyright 2010 onwards, Blindside Networks Inc
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 * @author    Jesus Federico  (jesus [at] blindsidenetworks [dt] com)
24
 */
25
26
namespace mod_bigbluebuttonbn\locallib;
27
28
use context_module;
29
30
defined('MOODLE_INTERNAL') || die();
31
global $CFG;
32
require_once($CFG->dirroot . '/mod/bigbluebuttonbn/locallib.php');
33
34
/**
35
 * Wrapper for executing http requests on a BigBlueButton server.
36
 *
37
 * @copyright 2010 onwards, Blindside Networks Inc
38
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39
 */
40
class bigbluebutton {
41
42
    /**
43
     * Returns the right URL for the action specified.
44
     *
45
     * @param string $action
46
     * @param array  $data
47
     * @param array  $metadata
48
     * @return string
49
     */
50
    public static function action_url($action = '', $data = array(), $metadata = array()) {
51
        $baseurl = self::sanitized_url() . $action . '?';
52
        $metadata = array_combine(
53
            array_map(
54
                function($k) {
55
                    return 'meta_' . $k;
56
                }
57
                , array_keys($metadata)
58
            ),
59
            $metadata
60
        );
61
        $params = http_build_query($data + $metadata, '', '&');
62
        return $baseurl . $params . '&checksum=' . sha1($action . $params . self::sanitized_secret());
63
    }
64
65
    /**
66
     * Makes sure the url used doesn't is in the format required.
67
     *
68
     * @return string
69
     */
70
    public static function sanitized_url() {
71
        $serverurl = trim(config::get('server_url'));
72
        if (substr($serverurl, -1) == '/') {
73
            $serverurl = rtrim($serverurl, '/');
74
        }
75
        if (substr($serverurl, -4) == '/api') {
76
            $serverurl = rtrim($serverurl, '/api');
77
        }
78
        return $serverurl . '/api/';
79
    }
80
81
    /**
82
     * Makes sure the shared_secret used doesn't have trailing white characters.
83
     *
84
     * @return string
85
     */
86
    public static function sanitized_secret() {
87
        return trim(config::get('shared_secret'));
88
    }
89
90
    /**
91
     * Returns the BigBlueButton server root URL.
92
     *
93
     * @return string
94
     */
95
    public static function root() {
96
        $pserverurl = parse_url(trim(config::get('server_url')));
97
        $pserverurlport = "";
98
        if (isset($pserverurl['port'])) {
99
            $pserverurlport = ":" . $pserverurl['port'];
100
        }
101
        return $pserverurl['scheme'] . "://" . $pserverurl['host'] . $pserverurlport . "/";
102
    }
103
104
    /**
105
     * Get BBB session information from viewinstance
106
     *
107
     * @param object $viewinstance
108
     * @return mixed
109
     * @throws \coding_exception
110
     * @throws \dml_exception
111
     * @throws \moodle_exception
112
     * @throws \require_login_exception
113
     * @throws \required_capability_exception
114
     */
115
    public static function build_bbb_session_fromviewinstance($viewinstance) {
116
        $cm = $viewinstance['cm'];
117
        $course = $viewinstance['course'];
118
        $bigbluebuttonbn = $viewinstance['bigbluebuttonbn'];
119
        return self::build_bbb_session($cm, $course, $bigbluebuttonbn);
120
    }
121
122
    /**
123
     * Get BBB session from parameters
124
     *
125
     * @param \course_modinfo $cm
126
     * @param object $course
127
     * @param object $bigbluebuttonbn
128
     * @return mixed
129
     * @throws \coding_exception
130
     * @throws \dml_exception
131
     * @throws \moodle_exception
132
     * @throws \require_login_exception
133
     * @throws \required_capability_exception
134
     */
135
    public static function build_bbb_session($cm, $course, $bigbluebuttonbn) {
136
        global $CFG;
137
        $context = context_module::instance($cm->id);
138
        require_login($course->id, false, $cm, true, true);
139
        require_capability('mod/bigbluebuttonbn:join', $context);
140
141
        // Add view event.
142
        bigbluebuttonbn_event_log(\mod_bigbluebuttonbn\event\events::$events['view'], $bigbluebuttonbn);
143
144
        // Create array bbbsession with configuration for BBB server.
145
        $bbbsession['course'] = $course;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$bbbsession was never initialized. Although not strictly required by PHP, it is generally a good practice to add $bbbsession = 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...
146
        $bbbsession['coursename'] = $course->fullname;
147
        $bbbsession['cm'] = $cm;
148
        $bbbsession['bigbluebuttonbn'] = $bigbluebuttonbn;
149
        self::view_bbbsession_set($context, $bbbsession);
150
151
        $serverversion = bigbluebuttonbn_get_server_version();
152
        $bbbsession['serverversion'] = (string) $serverversion;
153
154
        // Operation URLs.
155
        $bbbsession['bigbluebuttonbnURL'] = $CFG->wwwroot . '/mod/bigbluebuttonbn/view.php?id=' . $cm->id;
156
        $bbbsession['logoutURL'] = $CFG->wwwroot . '/mod/bigbluebuttonbn/bbb_view.php?action=logout&id=' . $cm->id .
157
            '&bn=' . $bbbsession['bigbluebuttonbn']->id;
158
        $bbbsession['recordingReadyURL'] = $CFG->wwwroot . '/mod/bigbluebuttonbn/bbb_broker.php?action=recording_' .
159
            'ready&bigbluebuttonbn=' . $bbbsession['bigbluebuttonbn']->id;
160
        $bbbsession['meetingEventsURL'] = $CFG->wwwroot . '/mod/bigbluebuttonbn/bbb_broker.php?action=meeting' .
161
            '_events&bigbluebuttonbn=' . $bbbsession['bigbluebuttonbn']->id;
162
        $bbbsession['joinURL'] = $CFG->wwwroot . '/mod/bigbluebuttonbn/bbb_view.php?action=join&id=' . $cm->id .
163
            '&bn=' . $bbbsession['bigbluebuttonbn']->id;
164
165
        return $bbbsession;
166
    }
167
168
    /**
169
     * Build standard array with configurations required for BBB server.
170
     *
171
     * @param \context $context
172
     * @param array $session
173
     * @throws \coding_exception
174
     * @throws \dml_exception
175
     */
176 View Code Duplication
    public static function view_bbbsession_set($context, &$session) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
177
178
        global $CFG, $USER;
179
180
        $session['username'] = fullname($USER);
181
        $session['userID'] = $USER->id;
182
        $session['administrator'] = is_siteadmin($session['userID']);
183
        $participantlist = bigbluebuttonbn_get_participant_list($session['bigbluebuttonbn'], $context);
184
        $session['moderator'] = bigbluebuttonbn_is_moderator($context, $participantlist);
185
        $session['managerecordings'] = ($session['administrator']
186
            || has_capability('mod/bigbluebuttonbn:managerecordings', $context));
187
        $session['importrecordings'] = ($session['managerecordings']);
188
        $session['modPW'] = $session['bigbluebuttonbn']->moderatorpass;
189
        $session['viewerPW'] = $session['bigbluebuttonbn']->viewerpass;
190
        $session['meetingid'] = $session['bigbluebuttonbn']->meetingid.'-'.$session['course']->id.'-'.
191
            $session['bigbluebuttonbn']->id;
192
        $session['meetingname'] = $session['bigbluebuttonbn']->name;
193
        $session['meetingdescription'] = $session['bigbluebuttonbn']->intro;
194
        $session['userlimit'] = intval((int) config::get('userlimit_default'));
195
        if ((boolean) config::get('userlimit_editable')) {
196
            $session['userlimit'] = intval($session['bigbluebuttonbn']->userlimit);
197
        }
198
        $session['voicebridge'] = $session['bigbluebuttonbn']->voicebridge;
199
        if ($session['bigbluebuttonbn']->voicebridge > 0) {
200
            $session['voicebridge'] = 70000 + $session['bigbluebuttonbn']->voicebridge;
201
        }
202
        $session['wait'] = $session['bigbluebuttonbn']->wait;
203
        $session['record'] = $session['bigbluebuttonbn']->record;
204
205
        $session['recordallfromstart'] = $CFG->bigbluebuttonbn_recording_all_from_start_default;
206
        if ($CFG->bigbluebuttonbn_recording_all_from_start_editable) {
207
            $session['recordallfromstart'] = $session['bigbluebuttonbn']->recordallfromstart;
208
        }
209
210
        $session['recordhidebutton'] = $CFG->bigbluebuttonbn_recording_hide_button_default;
211
        if ($CFG->bigbluebuttonbn_recording_hide_button_editable) {
212
            $session['recordhidebutton'] = $session['bigbluebuttonbn']->recordhidebutton;
213
        }
214
215
        $session['welcome'] = $session['bigbluebuttonbn']->welcome;
216
        if (!isset($session['welcome']) || $session['welcome'] == '') {
217
            $session['welcome'] = get_string('mod_form_field_welcome_default', 'bigbluebuttonbn');
218
        }
219
        if ($session['bigbluebuttonbn']->record) {
220
            // Check if is enable record all from start.
221
            if ($session['recordallfromstart']) {
222
                $session['welcome'] .= '<br><br>'.get_string('bbbrecordallfromstartwarning',
223
                        'bigbluebuttonbn');
224
            } else {
225
                $session['welcome'] .= '<br><br>'.get_string('bbbrecordwarning', 'bigbluebuttonbn');
226
            }
227
        }
228
        $session['openingtime'] = $session['bigbluebuttonbn']->openingtime;
229
        $session['closingtime'] = $session['bigbluebuttonbn']->closingtime;
230
        $session['muteonstart'] = $session['bigbluebuttonbn']->muteonstart;
231
        $session['context'] = $context;
232
        $session['origin'] = 'Moodle';
233
        $session['originVersion'] = $CFG->release;
234
        $parsedurl = parse_url($CFG->wwwroot);
235
        $session['originServerName'] = $parsedurl['host'];
236
        $session['originServerUrl'] = $CFG->wwwroot;
237
        $session['originServerCommonName'] = '';
238
        $session['originTag'] = 'moodle-mod_bigbluebuttonbn ('.get_config('mod_bigbluebuttonbn', 'version').')';
239
        $session['bnserver'] = bigbluebuttonbn_is_bn_server();
240
        $session['clienttype'] = config::get('clienttype_default');
241
242
        if (config::get('clienttype_editable')) {
243
            $session['clienttype'] = $session['bigbluebuttonbn']->clienttype;
244
        }
245
246
        if (!config::clienttype_enabled()) {
247
            $session['clienttype'] = BIGBLUEBUTTON_CLIENTTYPE_FLASH;
248
        }
249
    }
250
251
    /**
252
     * Can join meeting.
253
     *
254
     * @param int $cmid
255
     * @return array|bool[]
256
     * @throws \coding_exception
257
     * @throws \dml_exception
258
     * @throws \moodle_exception
259
     * @throws \require_login_exception
260
     * @throws \required_capability_exception
261
     */
262
    public static function can_join_meeting($cmid) {
263
        global $CFG;
264
        $canjoin = array('can_join' => false, 'message' => '');
265
266
        $viewinstance = bigbluebuttonbn_view_validator($cmid, null);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
267
        if ($viewinstance) {
268
            $bbbsession = self::build_bbb_session_fromviewinstance($viewinstance);
0 ignored issues
show
Documentation introduced by
$viewinstance is of type array, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
269
            if ($bbbsession) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $bbbsession of type array<string,string> is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
270
                require_once($CFG->dirroot . "/mod/bigbluebuttonbn/brokerlib.php");
271
                $info = bigbluebuttonbn_get_meeting_info($bbbsession['meetingid'], false);
272
                $running = false;
273
                if ($info['returncode'] == 'SUCCESS') {
274
                    $running = ($info['running'] === 'true');
275
                }
276
                $participantcount = 0;
277
                if (isset($info['participantCount'])) {
278
                    $participantcount = $info['participantCount'];
279
                }
280
                $canjoin = bigbluebuttonbn_broker_meeting_info_can_join($bbbsession, $running, $participantcount);
281
            }
282
        }
283
        return $canjoin;
284
    }
285
}
286