Completed
Push — master ( 90d5d6...f37dd9 )
by Ghazi
01:47
created

BigBlueButton::getIsMeetingRunningUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * BigBlueButton open source conferencing system - https://www.bigbluebutton.org/.
4
 *
5
 * Copyright (c) 2016-2018 BigBlueButton Inc. and by respective authors (see below).
6
 *
7
 * This program is free software; you can redistribute it and/or modify it under the
8
 * terms of the GNU Lesser General Public License as published by the Free Software
9
 * Foundation; either version 3.0 of the License, or (at your option) any later
10
 * version.
11
 *
12
 * BigBlueButton is distributed in the hope that it will be useful, but WITHOUT ANY
13
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
14
 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public License along
17
 * with BigBlueButton; if not, see <http://www.gnu.org/licenses/>.
18
 */
19
namespace BigBlueButton;
20
21
use BigBlueButton\Core\ApiMethod;
22
use BigBlueButton\Parameters\CreateMeetingParameters;
23
use BigBlueButton\Parameters\DeleteRecordingsParameters;
24
use BigBlueButton\Parameters\EndMeetingParameters;
25
use BigBlueButton\Parameters\GetMeetingInfoParameters;
26
use BigBlueButton\Parameters\GetRecordingsParameters;
27
use BigBlueButton\Parameters\HooksCreateParameters;
28
use BigBlueButton\Parameters\HooksDestroyParameters;
29
use BigBlueButton\Parameters\IsMeetingRunningParameters;
30
use BigBlueButton\Parameters\JoinMeetingParameters;
31
use BigBlueButton\Parameters\PublishRecordingsParameters;
32
use BigBlueButton\Parameters\UpdateRecordingsParameters;
33
use BigBlueButton\Responses\ApiVersionResponse;
34
use BigBlueButton\Responses\CreateMeetingResponse;
35
use BigBlueButton\Responses\DeleteRecordingsResponse;
36
use BigBlueButton\Responses\EndMeetingResponse;
37
use BigBlueButton\Responses\GetDefaultConfigXMLResponse;
38
use BigBlueButton\Responses\GetMeetingInfoResponse;
39
use BigBlueButton\Responses\GetMeetingsResponse;
40
use BigBlueButton\Responses\GetRecordingsResponse;
41
use BigBlueButton\Responses\HooksCreateResponse;
42
use BigBlueButton\Responses\HooksDestroyResponse;
43
use BigBlueButton\Responses\HooksListResponse;
44
use BigBlueButton\Responses\IsMeetingRunningResponse;
45
use BigBlueButton\Responses\JoinMeetingResponse;
46
use BigBlueButton\Responses\PublishRecordingsResponse;
47
use BigBlueButton\Responses\SetConfigXMLResponse;
48
use BigBlueButton\Responses\UpdateRecordingsResponse;
49
use BigBlueButton\Util\UrlBuilder;
50
use SimpleXMLElement;
51
52
/**
53
 * Class BigBlueButton
54
 * @package BigBlueButton
55
 */
56
class BigBlueButton
57
{
58
    protected $securitySecret;
59
    protected $bbbServerBaseUrl;
60
    protected $urlBuilder;
61
    protected $jSessionId;
62
63
    public function __construct()
64
    {
65
        // Keeping backward compatibility with older deployed versions
66
        $this->securitySecret   = (getenv('BBB_SECURITY_SALT') === false) ? getenv('BBB_SECRET') : $this->securitySecret = getenv('BBB_SECURITY_SALT');
67
        $this->bbbServerBaseUrl = getenv('BBB_SERVER_BASE_URL');
68
        $this->urlBuilder       = new UrlBuilder($this->securitySecret, $this->bbbServerBaseUrl);
69
    }
70
71
    /**
72
     * @return ApiVersionResponse
73
     *
74
     * @throws \RuntimeException
75
     */
76
    public function getApiVersion()
77
    {
78
        $xml = $this->processXmlResponse($this->urlBuilder->buildUrl());
79
80
        return new ApiVersionResponse($xml);
81
    }
82
83
    /* __________________ BBB ADMINISTRATION METHODS _________________ */
84
    /* The methods in the following section support the following categories of the BBB API:
85
    -- create
86
    -- getDefaultConfigXML
87
    -- setConfigXML
88
    -- join
89
    -- end
90
    */
91
92
    /**
93
     * @param  CreateMeetingParameters $createMeetingParams
94
     * @return string
95
     */
96
    public function getCreateMeetingUrl($createMeetingParams)
97
    {
98
        return $this->urlBuilder->buildUrl(ApiMethod::CREATE, $createMeetingParams->getHTTPQuery());
99
    }
100
101
    /**
102
     * @param  CreateMeetingParameters $createMeetingParams
103
     * @return CreateMeetingResponse
104
     * @throws \RuntimeException
105
     */
106
    public function createMeeting($createMeetingParams)
107
    {
108
        $xml = $this->processXmlResponse($this->getCreateMeetingUrl($createMeetingParams), $createMeetingParams->getPresentationsAsXML());
0 ignored issues
show
Security Bug introduced by
It seems like $createMeetingParams->getPresentationsAsXML() targeting BigBlueButton\Parameters...getPresentationsAsXML() can also be of type false; however, BigBlueButton\BigBlueButton::processXmlResponse() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
109
110
        return new CreateMeetingResponse($xml);
111
    }
112
113
    /**
114
     * @return string
115
     */
116
    public function getDefaultConfigXMLUrl()
117
    {
118
        return $this->urlBuilder->buildUrl(ApiMethod::GET_DEFAULT_CONFIG_XML);
119
    }
120
121
    /**
122
     * @return GetDefaultConfigXMLResponse
123
     * @throws \RuntimeException
124
     */
125
    public function getDefaultConfigXML()
126
    {
127
        $xml = $this->processXmlResponse($this->getDefaultConfigXMLUrl());
128
129
        return new GetDefaultConfigXMLResponse($xml);
130
    }
131
132
    /**
133
     * @return string
134
     */
135
    public function setConfigXMLUrl()
136
    {
137
        return $this->urlBuilder->buildUrl(ApiMethod::SET_CONFIG_XML, '', false);
138
    }
139
140
    /**
141
     * @param  $setConfigXMLParams
142
     * @return SetConfigXMLResponse
143
     * @throws \RuntimeException
144
     */
145
    public function setConfigXML($setConfigXMLParams)
146
    {
147
        $setConfigXMLPayload = $this->urlBuilder->buildQs(ApiMethod::SET_CONFIG_XML, $setConfigXMLParams->getHTTPQuery());
148
149
        $xml = $this->processXmlResponse($this->setConfigXMLUrl(), $setConfigXMLPayload, 'application/x-www-form-urlencoded');
150
151
        return new SetConfigXMLResponse($xml);
152
    }
153
154
    /**
155
     * @param $joinMeetingParams JoinMeetingParameters
156
     *
157
     * @return string
158
     */
159
    public function getJoinMeetingURL($joinMeetingParams)
160
    {
161
        return $this->urlBuilder->buildUrl(ApiMethod::JOIN, $joinMeetingParams->getHTTPQuery());
162
    }
163
164
    /**
165
     * @param $joinMeetingParams JoinMeetingParameters
166
     *
167
     * @return JoinMeetingResponse
168
     * @throws \RuntimeException
169
     */
170
    public function joinMeeting($joinMeetingParams)
171
    {
172
        $xml = $this->processXmlResponse($this->getJoinMeetingURL($joinMeetingParams));
173
174
        return new JoinMeetingResponse($xml);
175
    }
176
177
    /**
178
     * @param $endParams EndMeetingParameters
179
     *
180
     * @return string
181
     */
182
    public function getEndMeetingURL($endParams)
183
    {
184
        return $this->urlBuilder->buildUrl(ApiMethod::END, $endParams->getHTTPQuery());
185
    }
186
187
    /**
188
     * @param $endParams EndMeetingParameters
189
     *
190
     * @return EndMeetingResponse
191
     * @throws \RuntimeException
192
     * */
193
    public function endMeeting($endParams)
194
    {
195
        $xml = $this->processXmlResponse($this->getEndMeetingURL($endParams));
196
197
        return new EndMeetingResponse($xml);
198
    }
199
200
    /* __________________ BBB MONITORING METHODS _________________ */
201
    /* The methods in the following section support the following categories of the BBB API:
202
    -- isMeetingRunning
203
    -- getMeetings
204
    -- getMeetingInfo
205
    */
206
207
    /**
208
     * @param $meetingParams IsMeetingRunningParameters
209
     * @return string
210
     */
211
    public function getIsMeetingRunningUrl($meetingParams)
212
    {
213
        return $this->urlBuilder->buildUrl(ApiMethod::IS_MEETING_RUNNING, $meetingParams->getHTTPQuery());
214
    }
215
216
    /**
217
     * @param $meetingParams
218
     * @return IsMeetingRunningResponse
219
     * @throws \RuntimeException
220
     */
221
    public function isMeetingRunning($meetingParams)
222
    {
223
        $xml = $this->processXmlResponse($this->getIsMeetingRunningUrl($meetingParams));
224
225
        return new IsMeetingRunningResponse($xml);
226
    }
227
228
    /**
229
     * @return string
230
     */
231
    public function getMeetingsUrl()
232
    {
233
        return $this->urlBuilder->buildUrl(ApiMethod::GET_MEETINGS);
234
    }
235
236
    /**
237
     * @return GetMeetingsResponse
238
     * @throws \RuntimeException
239
     */
240
    public function getMeetings()
241
    {
242
        $xml = $this->processXmlResponse($this->getMeetingsUrl());
243
244
        return new GetMeetingsResponse($xml);
245
    }
246
247
    /**
248
     * @param $meetingParams GetMeetingInfoParameters
249
     * @return string
250
     */
251
    public function getMeetingInfoUrl($meetingParams)
252
    {
253
        return $this->urlBuilder->buildUrl(ApiMethod::GET_MEETING_INFO, $meetingParams->getHTTPQuery());
254
    }
255
256
    /**
257
     * @param $meetingParams GetMeetingInfoParameters
258
     * @return GetMeetingInfoResponse
259
     * @throws \RuntimeException
260
     */
261
    public function getMeetingInfo($meetingParams)
262
    {
263
        $xml = $this->processXmlResponse($this->getMeetingInfoUrl($meetingParams));
264
265
        return new GetMeetingInfoResponse($xml);
266
    }
267
268
    /* __________________ BBB RECORDING METHODS _________________ */
269
    /* The methods in the following section support the following categories of the BBB API:
270
    -- getRecordings
271
    -- publishRecordings
272
    -- deleteRecordings
273
    */
274
275
    /**
276
     * @param $recordingsParams GetRecordingsParameters
277
     * @return string
278
     */
279
    public function getRecordingsUrl($recordingsParams)
280
    {
281
        return $this->urlBuilder->buildUrl(ApiMethod::GET_RECORDINGS, $recordingsParams->getHTTPQuery());
282
    }
283
284
    /**
285
     * @param $recordingParams
286
     * @return GetRecordingsResponse
287
     * @throws \RuntimeException
288
     */
289
    public function getRecordings($recordingParams)
290
    {
291
        $xml = $this->processXmlResponse($this->getRecordingsUrl($recordingParams));
292
293
        return new GetRecordingsResponse($xml);
294
    }
295
296
    /**
297
     * @param $recordingParams PublishRecordingsParameters
298
     * @return string
299
     */
300
    public function getPublishRecordingsUrl($recordingParams)
301
    {
302
        return $this->urlBuilder->buildUrl(ApiMethod::PUBLISH_RECORDINGS, $recordingParams->getHTTPQuery());
303
    }
304
305
    /**
306
     * @param $recordingParams PublishRecordingsParameters
307
     * @return PublishRecordingsResponse
308
     * @throws \RuntimeException
309
     */
310
    public function publishRecordings($recordingParams)
311
    {
312
        $xml = $this->processXmlResponse($this->getPublishRecordingsUrl($recordingParams));
313
314
        return new PublishRecordingsResponse($xml);
315
    }
316
317
    /**
318
     * @param $recordingParams DeleteRecordingsParameters
319
     * @return string
320
     */
321
    public function getDeleteRecordingsUrl($recordingParams)
322
    {
323
        return $this->urlBuilder->buildUrl(ApiMethod::DELETE_RECORDINGS, $recordingParams->getHTTPQuery());
324
    }
325
326
    /**
327
     * @param $recordingParams DeleteRecordingsParameters
328
     * @return DeleteRecordingsResponse
329
     * @throws \RuntimeException
330
     */
331
    public function deleteRecordings($recordingParams)
332
    {
333
        $xml = $this->processXmlResponse($this->getDeleteRecordingsUrl($recordingParams));
334
335
        return new DeleteRecordingsResponse($xml);
336
    }
337
338
    /**
339
     * @param $recordingParams UpdateRecordingsParameters
340
     * @return string
341
     */
342
    public function getUpdateRecordingsUrl($recordingParams)
343
    {
344
        return $this->urlBuilder->buildUrl(ApiMethod::UPDATE_RECORDINGS, $recordingParams->getHTTPQuery());
345
    }
346
347
    /**
348
     * @param $recordingParams UpdateRecordingsParameters
349
     * @return UpdateRecordingsResponse
350
     * @throws \RuntimeException
351
     */
352
    public function updateRecordings($recordingParams)
353
    {
354
        $xml = $this->processXmlResponse($this->getUpdateRecordingsUrl($recordingParams));
355
356
        return new UpdateRecordingsResponse($xml);
357
    }
358
359
    /* ____________________ WEB HOOKS METHODS ___________________ */
360
361
    /**
362
     * @param $hookCreateParams HooksCreateParameters
363
     * @return string
364
     */
365
    public function getHooksCreateUrl($hookCreateParams)
366
    {
367
        return $this->urlBuilder->buildUrl(ApiMethod::HOOKS_CREATE, $hookCreateParams->getHTTPQuery());
368
    }
369
370
    /**
371
     * @param $hookCreateParams
372
     * @return HooksCreateResponse
373
     */
374
    public function hooksCreate($hookCreateParams)
375
    {
376
        $xml = $this->processXmlResponse($this->getHooksCreateUrl($hookCreateParams));
377
378
        return new HooksCreateResponse($xml);
379
    }
380
381
    /**
382
     * @return string
383
     */
384
    public function getHooksListUrl()
385
    {
386
        return $this->urlBuilder->buildUrl(ApiMethod::HOOKS_LIST);
387
    }
388
389
    /**
390
     * @return HooksListResponse
391
     */
392
    public function hooksList()
393
    {
394
        $xml = $this->processXmlResponse($this->getHooksListUrl());
395
396
        return new HooksListResponse($xml);
397
    }
398
399
    /**
400
     * @param $hooksDestroyParams HooksDestroyParameters
401
     * @return string
402
     */
403
    public function getHooksDestroyUrl($hooksDestroyParams)
404
    {
405
        return $this->urlBuilder->buildUrl(ApiMethod::HOOKS_DESTROY, $hooksDestroyParams->getHTTPQuery());
406
    }
407
408
    /**
409
     * @param $hooksDestroyParams
410
     * @return HooksDestroyResponse
411
     */
412
    public function hooksDestroy($hooksDestroyParams)
413
    {
414
        $xml = $this->processXmlResponse($this->getHooksDestroyUrl($hooksDestroyParams));
415
416
        return new HooksDestroyResponse($xml);
417
    }
418
419
    /* ____________________ SPECIAL METHODS ___________________ */
420
    /**
421
     * @return string
422
     */
423
    public function getJSessionId()
424
    {
425
        return $this->jSessionId;
426
    }
427
428
    /**
429
     * @param string $jSessionId
430
     */
431
    public function setJSessionId($jSessionId)
432
    {
433
        $this->jSessionId = $jSessionId;
434
    }
435
436
    /* ____________________ INTERNAL CLASS METHODS ___________________ */
437
438
    /**
439
     * A private utility method used by other public methods to process XML responses.
440
     *
441
     * @param  string            $url
442
     * @param  string            $payload
443
     * @param  string            $contentType
444
     * @return SimpleXMLElement
445
     * @throws \RuntimeException
446
     */
447
    private function processXmlResponse($url, $payload = '', $contentType = 'application/xml')
448
    {
449
        if (extension_loaded('curl')) {
450
            $ch = curl_init();
451
            if (!$ch) {
452
                throw new \RuntimeException('Unhandled curl error: ' . curl_error($ch));
453
            }
454
            $timeout = 10;
455
456
            // Needed to store the JSESSIONID
457
            $cookiefile     = tmpfile();
458
            $cookiefilepath = stream_get_meta_data($cookiefile)['uri'];
459
460
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
461
            curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8');
462
            curl_setopt($ch, CURLOPT_URL, $url);
463
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
464
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
465
            curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefilepath);
466
            curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefilepath);
467
            if (!empty($payload)) {
468
                curl_setopt($ch, CURLOPT_HEADER, 0);
469
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
470
                curl_setopt($ch, CURLOPT_POST, 1);
471
                curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
472
                curl_setopt($ch, CURLOPT_HTTPHEADER, [
473
                    'Content-type: ' . $contentType,
474
                    'Content-length: ' . mb_strlen($payload),
475
                ]);
476
            }
477
            $data = curl_exec($ch);
478
            if ($data === false) {
479
                throw new \RuntimeException('Unhandled curl error: ' . curl_error($ch));
480
            }
481
            curl_close($ch);
482
483
            $cookies = file_get_contents($cookiefilepath);
484
            if (strpos($cookies, 'JSESSIONID') !== false) {
485
                preg_match('/(?:JSESSIONID\s*)(?<JSESSIONID>.*)/', $cookies, $output_array);
486
                $this->setJSessionId($output_array['JSESSIONID']);
487
            }
488
489
            return new SimpleXMLElement($data);
490
        } else {
491
            throw new \RuntimeException('Post XML data set but curl PHP module is not installed or not enabled.');
492
        }
493
    }
494
}
495