PluginProbe
WpStream – Live Streaming, Video on Demand, Pay Per View / 4.8
WpStream – Live Streaming, Video on Demand, Pay Per View v4.8
4.14.1 4.14.0 4.13.2 4.13.1 4.13 4.12.5 4.12.4 4.12.3 4.12.2 4.12.1 4.12 4.4.4 4.4.5 4.4.6 4.4.7 4.4.8 4.4.9 4.5 4.5.1 4.5.11 4.5.11.1 4.5.11.2 4.5.11.4 4.5.11.5 4.5.11.6 All 181 releases
wpstream / includes / class-wpstream-live-api-connection.php

class-wpstream-live-api-connection.php in WpStream – Live Streaming, Video on Demand, Pay Per View 4.8, at includes/class-wpstream-live-api-connection.php

1,569 lines 46.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class Wpstream_Live_Api_Connection {
4
5
6
7
8 public function __construct() {
9 add_action( 'wp_ajax_wpstream_give_me_live_uri', array($this,'wpstream_give_me_live_uri') );
10 add_action( 'wp_ajax_wpstream_turn_of_channel', array($this,'wpstream_turn_of_channel') );
11 add_action( 'wp_ajax_wpstream_update_local_event_settings',array($this,'wpstream_update_local_event_settings'));
12 add_action( 'wp_ajax_wpstream_update_use_global_event_options',array($this,'wpstream_update_use_global_event_options'));
13 add_action( 'wp_ajax_wpstream_update_default_channel_settings', array( $this, 'wpstream_update_default_channel_settings' ) );
14 add_action( 'wp_ajax_wpstream_update_settings', array( $this, 'wpstream_update_settings' ) );
15
16 add_action( 'wp_ajax_wpstream_check_dns_sync', array($this,'wpstream_check_dns_sync') );
17 add_action( 'wp_ajax_wpstream_check_event_status', array($this,'wpstream_check_event_status') );
18 add_action( 'wp_ajax_wpstream_check_whipurl', array($this, 'wpstream_check_whipurl') );
19 add_action( 'wp_ajax_wpstream_check_user_quota', array($this, 'wpstream_check_user_quota') );
20
21 add_action( 'wp_ajax_wpstream_close_event', array($this,'wpstream_close_event') );
22 add_action( 'wp_ajax_wpstream_get_download_link', array($this,'wpstream_get_download_link') );
23 add_action( 'wp_ajax_wpstream_get_delete_file', array($this,'wpstream_get_delete_file') );
24
25 add_action( 'admin_notices',array($this, 'wpstream_admin_notices') );
26
27 add_action( 'wp_ajax_wpstream_check_pending_videos', array($this,'wpstream_check_pending_videos') );
28
29
30 }
31
32
33 /*
34 * Admin Notices
35 *
36 *
37 *
38 * */
39 function wpstream_admin_notices(){
40 global $pagenow;
41
42
43
44
45 if($pagenow!='admin.php'){
46 return;
47 }
48
49 $permited_pages=array('wpstream_plugin_options','wpstream_live_channels','wpstream_recordings','wpstream_settings');
50 if (!empty($_GET['page'])) {
51 $page = esc_html($_GET['page']) ;
52 if( !in_array($page, $permited_pages)){
53 return;
54 }
55 }
56
57 if(in_array('curl', get_loaded_extensions())){
58 //cURL module has been loaded
59 } else{
60 print '<div class="api_not_conected wpstream_notice_top">We could not connect to WpStream.net. Make sure you have the php Curl library enabled and your hosting allows outgoing HTTP Connection. </div>';
61 }
62
63 $token = $this->wpstream_get_token();
64 if($token=='' and $page!='wpstream_plugin_options'){
65 // echo 'wpstream_curl_failed: ' . get_option('wpstream_curl_failed');
66 $text = get_option('wpstream_curl_failed') === "0" ?
67 'Not connected to WpStream. Please check your credentials <a href="/wp-admin/admin.php?page=wpstream_credentials">here</a>.' :
68 'Not connected to WpStream. Please note the errors above and contact support.';
69
70 echo '<div class="api_not_conected wpstream_notice_top">'.__($text,'wpstream').'</div>';
71 }
72
73 }
74
75
76 /*
77 * Curl request
78 *
79 *
80 *
81 * */
82
83 function wpstream_baker_do_curl_base($url,$curl_post_fields, $expect_json = false, $quiet = false, $timeout = 10){
84 $curl = curl_init();
85 $api_url = WPSTREAM_API.'/'.$url;
86
87 curl_setopt_array($curl, array(
88 CURLOPT_URL =>$api_url,
89 CURLOPT_RETURNTRANSFER => true,
90 CURLOPT_ENCODING => "",
91 CURLOPT_MAXREDIRS => 10,
92 CURLOPT_TIMEOUT => $timeout,
93 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
94 CURLOPT_CUSTOMREQUEST => "POST",
95 CURLOPT_POSTFIELDS => http_build_query($curl_post_fields),
96 CURLOPT_HTTPHEADER => array(
97 "cache-control: no-cache",
98 "content-type: application/x-www-form-urlencoded"
99 ),
100 ));
101
102 $response = curl_exec($curl);
103 $err = curl_error($curl);
104 $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
105 curl_close($curl);
106
107 $curl_failed = 0;
108
109 $logger = new WpStream_Logger();
110
111 if ($err) {
112 // do not echo every time, some operations must return JSON
113 if (!$quiet){
114 echo '<div class="api_not_conected wpstream_error_curl">Critical: Could Not Connect to WpStream - '.$err.'</div>';
115 }
116
117 $curl_failed = $err;
118
119 $log_entry = new WpStream_Log_Entry([
120 'type' => 'error',
121 'description' => 'CURL error: ' . $err . ' on endpoint ' . $url,
122 ]);
123 $logger->add( $log_entry );
124
125 $response = json_encode( array(
126 'success' => false,
127 'error' => $err,
128 ));
129 }
130 else if ($http_code != 200) {
131 $log_entry = new WpStream_Log_Entry([
132 'type' => 'error',
133 'description' => 'HTTP error: ' . $http_code . ' on endpoint ' . $url,
134 ]);
135 $logger->add( $log_entry );
136
137 if (!$quiet){
138 switch ($http_code) {
139 case 0:
140 $message = "CURL failed with code 0. Please address CURL connectivity with your hosting provider.";
141 break;
142 case 429:
143 $message = "API: Too many Requests";
144 break;
145 default:
146 $message = "API - Unexpected response: " . $http_code;
147 break;
148 }
149 echo '<div class="api_not_conected wpstream_error_curl">'.$message.'</div>';
150 }
151 $curl_failed = $http_code;
152
153 $response = json_encode( array(
154 'success' => false,
155 'error' => $http_code,
156 ));
157 }
158 else if ($expect_json){
159 $curl_response_decoded = json_decode($response,JSON_OBJECT_AS_ARRAY);
160 if (JSON_ERROR_NONE !== json_last_error()) {
161 $log_entry = new WpStream_Log_Entry([
162 'type' => 'error',
163 'description' => 'Malformed JSON response: ' . json_last_error_msg() . ' on endpoint ' . $url,
164 ]);
165 $logger->add( $log_entry );
166
167 if (!$quiet) {
168 echo '<div class="api_not_conected wpstream_error_curl">Critical: Malformed API response #: ' . json_last_error() . '</div>';
169 }
170
171 $curl_failed = json_last_error();
172
173 $response = json_encode(array(
174 'success' => false,
175 'error' => json_last_error(),
176 ));
177 }
178 }
179
180 update_option( "wpstream_curl_failed", $curl_failed );
181
182 return $response;
183 }
184
185
186
187
188
189
190
191 /**
192 * retreive server id based on show id
193 *
194 * @since 3.0.1
195 * returns live url
196 */
197
198 function retrive_server_id_based_on_show_id($show_id){
199
200 $transient_name = 'server_id_to_return_'.$show_id;
201 $server_id_to_return = get_transient( $transient_name );
202
203 if ( false === $server_id_to_return ) {
204 $token = $this->wpstream_get_token();
205 $values_array=array(
206 "show_id" => intval($show_id),
207 );
208 $values_array=array();
209 $show_id=intval($show_id);
210
211 $url="https://rest-baker.wpstream.net/?&apiFunctionName=server_id_by_show_id&show_id=".$show_id."&access_token=".$token;
212 $arguments = array(
213 'method' => 'GET',
214 'timeout' => 45,
215 'redirection' => 5,
216 'httpversion' => '1.0',
217 'blocking' => true,
218 'headers' => array(),
219 'body' => $values_array,
220 'cookies' => array()
221 );
222
223 $response = wp_remote_post($url,$arguments);
224
225 $received_data = wp_remote_retrieve_body($response);
226
227 $received_data_decoded=json_decode($received_data);
228
229
230 if( isset($response['response']['code']) && $response['response']['code']=='200' && $received_data_decoded->success===true && $received_data_decoded->result!=''){
231 $server_id_to_return = $received_data_decoded->result;
232 set_transient( $transient_name, json_decode($server_id_to_return), 60 );
233 return $server_id_to_return;
234
235 }else{
236 return '';
237
238 }
239 }else{
240 return $server_id_to_return;
241 }
242
243 die();
244 }
245
246
247
248 /**
249 * edited 4.0
250 *
251 *
252 *
253 * check event status from start stremaing process
254 *
255 * @since 3.0.1
256 * returns live url
257 */
258
259 public function wpstream_check_event_status(){
260 $channel_id = intval($_POST['channel_id']);
261 $notes = 'wpstream_check_event_status_note';
262 if(isset($_POST['notes'])){
263 $notes = sanitize_text_field($_POST['notes']);
264 }
265
266
267 $response = $this->wpstream_check_event_status_api_call($channel_id,$notes);
268
269
270
271 if( isset($response['success']) && $response['success']){
272 $this->api20_wpstream_update_event($response,$channel_id);
273
274 if( isset($response['broadcast_url']) && isset($response['status']) && $response['status']==='active' ){
275
276
277 $response['live_data_url'] = $response['qos_url'];
278
279 /* obsolote due to new url format
280 $local_event_options = get_post_meta ($channel_id,'local_event_options',true);
281 if( is_array( $local_event_options ) && intval( $local_event_options['autostart']) ==1 ){
282 $to_split=explode('/',$response['broadcast_url']);
283 $obs_stream = array_pop($to_split);;
284 $obs_uri = str_replace($obs_stream,'',$response['broadcast_url']);
285 }else{
286 $to_split=explode('wpstream/',$response['broadcast_url']);
287 $obs_uri = $to_split[0].'wpstream/';
288 $obs_stream = $to_split[1];
289 }
290 */
291
292
293 $to_split = explode('/',$response['broadcast_url']);
294 $obs_stream = array_pop($to_split);;
295 $obs_uri = str_replace($obs_stream,'',$response['broadcast_url']);
296
297 $response['obs_uri'] = $obs_uri;
298 $response['obs_stream'] = $obs_stream;
299
300 update_post_meta($channel_id,'obs_uri',$obs_uri);
301 update_post_meta($channel_id,'obs_stream',$obs_stream);
302 update_post_meta($channel_id,'broadcast_url',$response['broadcast_url']);
303
304 }
305
306
307 }
308
309 print json_encode($response);
310 die();
311
312 }
313
314 public function wpstream_check_whipurl() {
315 $channel_id = intval($_POST['channel_id']);
316
317 $whip_url = get_post_meta($channel_id, 'whipUrl', true);
318
319 if ( $whip_url ) {
320 print json_encode(
321 array(
322 'success' => true,
323 'whip_url' => $whip_url,
324 )
325 );
326 } else {
327 print json_encode(
328 array(
329 'success' => false,
330 'error' => esc_html__('WHIP URL not found for this channel.', 'wpstream'),
331 )
332 );
333 }
334 die();
335 }
336
337 /**
338 * edited 4.0
339 *
340 * check event status from API
341 *
342 * @since 3.0.1
343 * returns live url
344 */
345
346
347 public function wpstream_check_event_status_api_call($channel_id,$notes){
348
349 $access_token = $this->wpstream_get_token();
350 $domain = parse_url ( get_site_url() );
351 $url = 'channel/info';
352
353 // do not make the call if no token is available
354 if (!$access_token) return false;
355
356 $curl_post_fields=array(
357 'access_token' => $access_token,
358 'channel_id' => $channel_id,
359 'domain' => $domain['host'],
360 'notes' => $notes
361 );
362
363
364
365
366
367 $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields,true, false,WPSTREAM_TIMEOUT_CONST);
368
369
370 $curl_response_decoded = json_decode($curl_response,JSON_OBJECT_AS_ARRAY);
371
372 return $curl_response_decoded;
373
374
375 }
376
377
378
379
380
381
382
383
384
385
386
387
388 public function wpstream_reset_event_data($event_id){
389 update_post_meta($event_id,'stats_url','');
390 update_post_meta($event_id,'hls_playback_url','');
391 update_post_meta($event_id,'server_id', '' );
392 }
393
394 /**
395 * edited 4.0
396 * update event metadata
397 *
398 * @since 3.0.1
399 * returns live url
400 */
401
402
403
404 function api20_wpstream_update_event($response,$channel_id){
405
406 if( is_array($response) ) {
407 $event_data_for_transient = array();
408 $transient_name = 'event_data_to_return_'.$channel_id;
409
410 foreach($response as $key=>$value){
411 update_post_meta($channel_id,$key,$value);
412 $event_data_for_transient[$key]=$value;
413 }
414 set_transient($transient_name,$event_data_for_transient,45);
415 return $event_data_for_transient;
416 }else{
417 return false;
418 }
419
420
421 }
422
423 /**
424 * Update event settings
425 *
426 * @since 3.0.1
427 * returns live url
428 */
429
430
431 public function wpstream_update_local_event_settings(){
432
433 check_ajax_referer( 'wpstream_start_event_nonce', 'security' );
434 if(!is_user_logged_in()){
435 exit('not logged in');
436 }
437 if( !current_user_can('administrator') ){
438 exit('not admin');
439 }
440
441 $show_id = intval($_POST['show_id']);
442 $option_array = $_POST['option'];
443
444 $to_save_option=array();
445 foreach($option_array as $key=>$value){
446 $to_save_option[sanitize_key($key)]=sanitize_text_field($value);
447 }
448
449 if( $to_save_option['low_latency'] == 1 || $to_save_option['adaptive_bitrate'] ==1 ){
450 $to_save_option['encrypt']=0;
451 }
452
453 if( $to_save_option['encrypt']==1){
454 $to_save_option['low_latency'] = 0;
455 $to_save_option['adaptive_bitrate'] =0;
456 }
457
458
459
460 update_post_meta ($show_id,'local_event_options',$to_save_option);
461 $this->wpstream_update_chanel_on_baker($show_id,$to_save_option);
462
463 }
464
465 public function wpstream_update_local_event_settings_with_global() {
466 check_ajax_referer( 'wpstream_start_event_nonce', 'security' );
467
468 check_ajax_referer( 'wpstream_start_event_nonce', 'security' );
469 if(!is_user_logged_in()){
470 wp_send_json_error(['success' => false, 'message' => __('Not logged in', 'wpstream')]);
471 }
472 if( !current_user_can('administrator') ){
473 wp_send_json_error(['success' => false, 'message' => __('Not admin', 'wpstream')]);
474 }
475
476 if ( !isset($_POST['show_id']) || !isset($_POST['use_global']) ) {
477 wp_send_json_error(['success' => false, 'message' => __('Missing parameters', 'wpstream')]);
478 }
479
480 $show_id = intval($_POST['show_id']);
481 $default_channel_settings = get_option('wpstream_user_streaming_global_channel_options') ;
482
483 $this->wpstream_update_chanel_on_baker($show_id, $default_channel_settings);
484 }
485
486 public function wpstream_update_use_global_event_options() {
487 check_ajax_referer( 'wpstream_start_event_nonce', 'security' );
488 if(!is_user_logged_in()){
489 wp_send_json_error(['success' => false, 'message' => __('Not logged in', 'wpstream')]);
490 }
491 if( !current_user_can('administrator') ){
492 wp_send_json_error(['success' => false, 'message' => __('Not admin', 'wpstream')]);
493 }
494
495 if ( !isset($_POST['show_id']) || !isset($_POST['use_global']) ) {
496 wp_send_json_error(['success' => false, 'message' => __('Missing parameters', 'wpstream')]);
497 }
498
499 $show_id = intval($_POST['show_id']);
500 $use_global_event_options = intval($_POST['use_global']);
501
502 $result = update_post_meta(
503 $show_id,
504 'use_global_event_options',
505 $use_global_event_options
506 );
507
508 if ( !$result ) {
509 wp_send_json_error(['success' => false, 'message' => __('Failed to update event', 'wpstream')]);
510 }
511
512 $local_event_options = get_post_meta($show_id,'local_event_options',true);
513
514 if( $use_global_event_options ) {
515 $global_event_options = get_option('wpstream_user_streaming_global_channel_options') ;
516
517 // if the local options are not set, we need to set them to the global ones
518 if ( !is_array($local_event_options) ) {
519 update_post_meta( $show_id, 'local_event_options', $global_event_options );
520 }
521
522 $this->wpstream_update_chanel_on_baker( $show_id, $global_event_options );
523 } else {
524 $this->wpstream_update_chanel_on_baker( $show_id, $local_event_options );
525 }
526 }
527
528 public function wpstream_update_default_channel_settings() {
529 check_ajax_referer( 'wpstream-settings-nonce', 'security' );
530
531 $options_array= $_POST['option'];
532 $sanitized_options = array();
533 foreach( $options_array as $key => $value ) {
534 $sanitized_options[ sanitize_key( $key ) ] = sanitize_text_field( $value );
535 }
536
537 if ( $sanitized_options['low_latency'] == 1 || $sanitized_options['adaptive_bitrate'] == 1 ) {
538 $sanitized_options['encrypt'] = 0;
539 }
540
541 if ( $sanitized_options['encrypt'] == 1 ) {
542 $sanitized_options['low_latency'] = 0;
543 $sanitized_options['adaptive_bitrate'] = 0;
544 }
545
546 $successful_update = update_option( 'wpstream_user_streaming_global_channel_options', $sanitized_options );
547
548 if ( $successful_update ) {
549 wp_send_json(
550 array(
551 'success' => true,
552 )
553 );
554 } else {
555 wp_send_json_error(
556 array(
557 'success' => false,
558 )
559 );
560 }
561
562 wp_die();
563 }
564
565 public function wpstream_update_settings() {
566 check_ajax_referer( 'wpstream-settings-nonce', 'security' );
567
568 $option_name = sanitize_key( $_POST['option_name'] );
569 $option_type = sanitize_key( $_POST['option_type'] );
570
571 switch( $option_type ) {
572 case 'checkbox':
573 $option_value = filter_var( $_POST['option_value'], FILTER_VALIDATE_INT );
574 break;
575 case 'text':
576 case 'select':
577 $option_value = sanitize_text_field( $_POST['option_value'] );
578 break;
579 case 'multiple-select':
580 $option_value = array_map( 'sanitize_text_field', $_POST['option_value'] );
581 break;
582 default:
583 $option_value = sanitize_text_field( $_POST['option_value'] );
584 }
585
586 $successful_update = update_option( 'wpstream_' . $option_name, $option_value );
587
588 if( $successful_update ) {
589 wp_send_json(
590 array(
591 'success' => true,
592 )
593 );
594 } else {
595 wp_send_json_error(
596 array(
597 'success' => false,
598 )
599 );
600 }
601
602 wp_die();
603 }
604
605 /**
606 * Update channel settings on baker
607 *
608 * @since 4.2
609 * returns live url
610 */
611
612
613 public function wpstream_update_chanel_on_baker($channel_id,$to_save_option){
614
615 $access_token = $this->wpstream_get_token();
616 if($access_token==''){
617 // cleanup any previous echo before sending json
618 ob_end_clean();
619 echo json_encode( array(
620 'is_record' => '',
621 'conected' => false,
622 'event_data' => '',
623 'error' => esc_html('You are not connected to wpstream.net! Please check your WpStream credentials!','wpstream'),
624 ));
625 exit();
626 }
627 $current_user = wp_get_current_user();
628 $userID = $current_user->ID;
629
630
631
632 $url = '/channel/update';
633 $local_event_options = get_post_meta($channel_id,'local_event_options',true);
634
635 $domain = parse_url ( get_site_url() );
636 $domain_scheme = 'http';
637 if(is_ssl()){
638 $domain_scheme='https';
639 }
640
641 $domain_ip= esc_html( $_SERVER['SERVER_ADDR'] );
642 if($domain_ip==''){
643 $domain_ip="0.0.0.0/0";
644 }
645
646 $corsorigin='*';
647 if( isset($local_event_options['domain_lock']) && intval( $local_event_options['domain_lock']) ==0 ){
648 $corsorigin='*';
649 } else{
650 $corsorigin=$domain_scheme.'://'.$domain['host'];
651 }
652
653
654 $url = 'channel/update';
655
656
657 $to_record="false";
658 if($to_save_option['record']){
659 $to_record="true";
660 }
661
662 if( $to_save_option['low_latency'] == 1 || $to_save_option['adaptive_bitrate'] ==1 ){
663 $to_save_option['encrypt']=0;
664 }
665
666 if( $to_save_option['encrypt']==1){
667 $to_save_option['low_latency'] = 0;
668 $to_save_option['adaptive_bitrate'] =0;
669 }
670
671
672 $abr='none';
673 if($to_save_option['adaptive_bitrate'] ==1 ){
674 $abr='common';
675 }
676
677
678
679 $curl_post_fields=array(
680 'access_token' => $access_token,
681 'channel_id' => $channel_id,
682 'domain' => $domain['host'],
683 'allow_access_from' => $corsorigin,
684 'record' => $to_record,
685 'encrypt' => boolval($to_save_option['encrypt']),
686 'autostart' => boolval($to_save_option['autostart']),
687 'low_latency' => boolval($to_save_option['low_latency']),
688 'abr' => $abr,
689 'hls_keys_url_prefix' => get_site_url().'?wpstream_livedrm=',
690 'allow_key_access_from' => $domain_ip,
691 'to_save_option' => $to_save_option,
692 );
693
694
695
696 $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields,true);
697 print_r($curl_response);
698 exit();
699 }
700
701
702
703
704
705 /**
706 *
707 * added 5.0
708 *
709 * Turn off channel
710 *
711 */
712 public function wpstream_turn_of_channel(){
713
714 $access_token = $this->wpstream_get_token();
715 if($access_token==''){
716 // cleanup any previous echo before sending json
717 ob_end_clean();
718 echo json_encode( array(
719 'is_record' => '',
720 'conected' => false,
721 'event_data' => '',
722 'error' => esc_html('You are not connected to wpstream.net! Please check your WpStream credentials!','wpstream'),
723 ));
724 exit();
725 }
726 $current_user = wp_get_current_user();
727 $userID = $current_user->ID;
728
729
730 global $wpstream_plugin;
731 if( !$wpstream_plugin->main->wpstream_check_user_can_stream() ){
732 exit('You are not allowed to stream.Code 407');
733 }
734
735
736 $channel_id = intval($_POST['show_id']);
737
738 $url = 'channel/stop';
739 $domain = parse_url ( get_site_url() );
740 $curl_post_fields=array(
741 'access_token' => $access_token,
742 'channel_id' => $channel_id,
743 'domain' => $domain['host'],
744
745 );
746
747
748
749 $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields,true);
750 $curl_response_decoded = json_decode($curl_response,JSON_OBJECT_AS_ARRAY);
751
752
753
754 if( isset($curl_response_decoded['success']) && $curl_response_decoded['success']===true ){
755 // cleanup any previous echo before sending json
756 ob_end_clean();
757 echo json_encode( array(
758 'conected' => true,
759 'answer' => $curl_response_decoded,
760 ));
761 }else{
762 // cleanup any previous echo before sending json
763 ob_end_clean();
764 echo json_encode( array(
765 'conected' => false,
766 'error' => esc_html__('Channel is already turned off or does not exist!','wpstream'),
767 'answer' => $curl_response_decoded,
768 ));
769 }
770 die();
771
772
773
774
775 }
776
777
778 /**
779 *
780 * Edited 4.0
781 *
782 * Request live url
783 *
784 * @since 3.0.1
785 * returns live url
786 */
787 public function wpstream_give_me_live_uri(){
788
789 $access_token = $this->wpstream_get_token();
790 if($access_token==''){
791 // cleanup any previous echo before sending json
792 ob_end_clean();
793 echo json_encode( array(
794 'is_record' => '',
795 'conected' => false,
796 'event_data' => '',
797 'error' => esc_html('You are not connected to wpstream.net! Please check your WpStream credentials!','wpstream'),
798 ));
799 exit();
800 }
801
802 $current_user = wp_get_current_user();
803 $userID = $current_user->ID;
804
805
806 global $wpstream_plugin;
807 if( !$wpstream_plugin->main->wpstream_check_user_can_stream() ){
808 exit('You are not allowed to stream.Code 407');
809 }
810
811
812 $channel_id = intval($_POST['show_id']);
813 $basic_streaming = filter_var($_POST['basic_streaming'], FILTER_VALIDATE_BOOLEAN);
814 $on_boarding = '';
815 if(isset($_POST['start_onboarding'])){
816 $on_boarding = sanitize_text_field($_POST['start_onboarding']);
817 }
818
819
820 $local_event_options = get_post_meta($channel_id,'local_event_options',true);
821 $use_global_event_options = get_post_meta($channel_id,'use_global_event_options',true);
822 $is_local_event_options_enabled = ( is_array( $local_event_options ) && empty( $use_global_event_options ) ) ||
823 ( !empty($use_global_event_options) && intval( $use_global_event_options ) === 0 );
824 if( !$is_local_event_options_enabled ) {
825 $local_event_options = get_option('wpstream_user_streaming_global_channel_options') ;
826 }
827
828 $is_autostart="false";
829 $is_encrypt="false";
830 $low_latency="false";
831 $adaptive_bitrate="false";
832 $is_record="false";
833 $corsorigin='*';
834
835 if ( is_array($local_event_options) ) {
836 if ( isset($local_event_options['autostart']) &&
837 intval( $local_event_options['autostart']) == 1 ) {
838 $is_autostart = "true";
839 }
840
841 if ( isset($local_event_options['encrypt']) &&
842 intval( $local_event_options['encrypt']) == 1 ) {
843 $is_encrypt = "true";
844 }
845
846 if ( isset($local_event_options['low_latency']) &&
847 intval( $local_event_options['low_latency']) == 1 ) {
848 $low_latency = "true";
849 }
850
851 if ( isset($local_event_options['adaptive_bitrate']) &&
852 intval( $local_event_options['adaptive_bitrate']) == 1 ) {
853 $adaptive_bitrate = "true";
854 }
855
856 if ( isset($local_event_options['record']) &&
857 intval( $local_event_options['record']) == 1 ) {
858 $is_record = "true";
859 }
860
861 if ( !isset($local_event_options['domain_lock']) || intval( $local_event_options['domain_lock']) == 0 ) {
862 $corsorigin = '*';
863 }
864 }
865
866 if( $adaptive_bitrate=="true" || $low_latency=="true" ) {
867 $is_encrypt="false";
868 }
869
870 if( $is_encrypt=="true" ) {
871 $adaptive_bitrate="false";
872 $low_latency="false";
873 }
874
875 $event_data = $this->wpstream_request_live_stream_uri(
876 $channel_id,
877 $is_autostart,
878 $is_record,
879 $is_encrypt,
880 $low_latency,
881 $adaptive_bitrate,
882 $userID,
883 $corsorigin,
884 $on_boarding,
885 $basic_streaming
886 );
887
888
889
890 if( isset($event_data['success']) && $event_data['success']===true ){
891 // cleanup any previous echo before sending json
892 ob_end_clean();
893 echo json_encode( array(
894 'is_record' => $is_record,
895 'conected' => true,
896 'event_data' => $event_data,
897
898 ));
899 }else{
900 $default_error= 'Failed to turn channel ON. Please try again in a few minutes.';
901 if( isset($event_data['error'])){
902 $plumer_error = $event_data['error'];
903 switch ($plumer_error) {
904 case 'NOT_ENOUGH_TRAFFIC':
905 $default_error= 'You do not have enough Streaming Data to turn ON a live channel. Please upgrade your subscription for more resources.' ;
906 break;
907
908 }
909
910 }
911 // cleanup any previous echo before sending json
912 ob_end_clean();
913 echo json_encode( array(
914 'is_record' => $is_record,
915 'conected' => false,
916 'event_data' => $event_data,
917 'error' => $default_error,
918
919
920 ));
921 }
922 die();
923 }
924
925
926 /**
927 *
928 * Edited 4.0
929 *
930 * Request live url via api
931 *
932 * @since 3.0.1
933 * returns live url
934 */
935
936
937 public function wpstream_request_live_stream_uri($schannel_id,$is_autostart,$is_record,$is_encrypt,$low_latency,$adaptive_bitrate,$request_by_userid,$corsorigin,$on_boarding, $basic_streaming){
938
939 $domain = parse_url ( get_site_url() );
940 $domain_scheme = 'http';
941 if(is_ssl()){
942 $domain_scheme='https';
943 }
944
945 $domain_ip= esc_html( $_SERVER['SERVER_ADDR'] );
946 if($domain_ip==''){
947 $domain_ip="0.0.0.0/0";
948 }
949
950 if($corsorigin!='*'){
951 $corsorigin=$domain_scheme.'://'.$domain['host'];
952 }
953
954 $abr='none';
955 if($adaptive_bitrate=="true"){
956 $abr='common';
957 }
958
959 if($low_latency=="true"){
960 $low_latency=true;
961 }else{
962 $low_latency = false;
963 }
964
965 $basic_streaming = $basic_streaming ? 'true' : 'false';
966
967 $url = 'channel/start';
968 $access_token = $this->wpstream_get_token();
969
970 $metadata_array=array(
971 'pluginVersion'=>WPSTREAM_PLUGIN_VERSION
972 );
973
974 if($on_boarding!=''){
975 $metadata_array['on_boarding']='yes';
976 }
977 $permalink = get_permalink($schannel_id);
978 if ($permalink !== false) {
979 $metadata_array['permalink'] = $permalink;
980 }
981
982 $curl_post_fields=array(
983 'access_token' => $access_token,
984 'channel_id' => $schannel_id,
985 'domain' => $domain['host'],
986 'allow_access_from' => $corsorigin,
987 'record' => $is_record,
988 'encrypt' => $is_encrypt,
989 'low_latency' => $low_latency,
990 'abr' => $abr,
991 'hls_keys_url_prefix' => get_site_url().'?wpstream_livedrm=',
992 'allow_key_access_from' => $domain_ip,
993 'metadata' => json_encode($metadata_array),
994 'autostart' => $is_autostart,
995 'basic_streaming' => $basic_streaming,
996 // 'fakeError' => 'init'
997 );
998
999
1000 $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields,true);
1001 $curl_response_decoded = json_decode($curl_response,JSON_OBJECT_AS_ARRAY);
1002 // $curl_response_decoded['curl_post_fields']=$curl_post_fields;
1003 return $curl_response_decoded;
1004
1005
1006
1007 }
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019 /**
1020 * Retrive auth token from tranzient
1021 *
1022 * @since 3.0.1
1023 * returns token
1024 */
1025 public function wpstream_get_token(){
1026 $token = get_transient('wpstream_token_api');
1027 if ( false === $token || $token==='') {
1028 $token = $this->wpstream_club_get_token();
1029 if ($token !== false){
1030 set_transient( 'wpstream_token_api', $token ,3500);
1031 }
1032 else {
1033 // cache the failed response for a second, otherwise it'll make a shitload of requests
1034 set_transient( 'wpstream_token_api', 'failed' , 1);
1035 }
1036 }
1037 $ret = $token === 'failed' ? false : $token;
1038 return $ret;
1039 }
1040
1041
1042 public function wpstream_get_token_30(){
1043 $token = get_transient('wpstream_token_request_30');
1044 if ( false === $token || $token==='' ) {
1045 $token = $this->wpstream_club_get_token_30();
1046 set_transient( 'wpstream_token_request_30', $token ,600);
1047 }
1048
1049 return $token;
1050
1051 }
1052
1053
1054 /**
1055 * Edited 4.0
1056 *
1057 * Request auth token from wpstream.net
1058 *
1059 * @since 3.0.1
1060 * returns token fron wpstream
1061 */
1062 protected function wpstream_club_get_token(){
1063 $username = get_option('wpstream_api_username','');
1064 $password = get_option('wpstream_api_password','');
1065
1066 if ( $username=='' || $password==''){
1067 return;
1068 }
1069
1070 $url='access_token';
1071 $curl_post_fields=array(
1072 'grant_type' => 'password',
1073 'username' => $username,
1074 'password' => $password
1075 );
1076
1077
1078
1079 $curl_response=$this->wpstream_baker_do_curl_base($url,$curl_post_fields, true);
1080
1081 $response= json_decode($curl_response);
1082
1083 if( isset($response->access_token) && $response->access_token!='' ){
1084 return $response->access_token;
1085 }else{
1086 return false;
1087 }
1088 }
1089
1090
1091
1092 /*
1093 *
1094 * Return token for api version 3.0
1095 *
1096 */
1097
1098
1099
1100
1101 protected function wpstream_club_get_token_30(){
1102
1103 $client_id = esc_html ( get_option('wpstream_api_key','') );
1104 $client_secret = esc_html ( get_option('wpstream_api_secret_key','') );
1105 $username = esc_html ( get_option('wpstream_api_username','') );
1106 $password = esc_html ( get_option('wpstream_api_password','') );
1107
1108 if ( $username=='' || $password==''){
1109 return;
1110 }
1111
1112
1113 $curl = curl_init();
1114
1115 $json = array(
1116 'grant_type'=>'password',
1117 'username' =>$username,
1118 'password' =>$password,
1119 'client_id'=>'qxZ6fCoOMj4cNK8SXRHa5nug6vnswlFWSF37hsW3',
1120 'client_secret'=>'L1fzLosJf9TlwnCCTZ5pkKmdqqkHShKEi0d4oFNE'
1121 );
1122
1123 curl_setopt_array($curl, array(
1124 CURLOPT_URL => WPSTREAM_CLUBLINKSSL."://www.".WPSTREAM_CLUBLINK."/?oauth=token",
1125 CURLOPT_RETURNTRANSFER => true,
1126 CURLOPT_ENCODING => "",
1127 CURLOPT_MAXREDIRS => 10,
1128 CURLOPT_TIMEOUT => 15,
1129 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
1130 CURLOPT_CUSTOMREQUEST => "POST",
1131 CURLOPT_POSTFIELDS=> json_encode($json),
1132 CURLOPT_HTTPHEADER => array(
1133 "cache-control: no-cache",
1134 "content-type: application/json",
1135 ),
1136 ));
1137
1138 $response = curl_exec($curl);
1139
1140 if(!$response){
1141 //
1142 }
1143
1144 $err = curl_error($curl);
1145
1146
1147
1148
1149
1150 curl_close($curl);
1151 $response= json_decode($response);
1152
1153 if(isset($response->access_token)){
1154 return $response->access_token;
1155 }else{
1156 return;
1157 }
1158 }
1159
1160
1161
1162 /**
1163 * edited 4.0
1164 * Return admin package data
1165 *
1166 * @since 3.0.1
1167 * returns pack data
1168 */
1169
1170 public function wpstream_request_pack_data_per_user($context = ''){
1171
1172 $event_data_for_transient = get_transient( 'wpstream_request_pack_data_per_user_transient' );
1173
1174 if($event_data_for_transient===false){
1175 $url = 'user/quota';
1176 $access_token = $this->wpstream_get_token();
1177
1178 // do not make the call if no token is available
1179 if (!$access_token) return false;
1180
1181 $curl_post_fields=array(
1182 'access_token'=>$access_token,
1183 'context' => $context,
1184 'plugin_version' => WPSTREAM_PLUGIN_VERSION
1185 );
1186
1187 $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields,true, false, WPSTREAM_TIMEOUT_CONST);
1188 $curl_response_decoded = json_decode($curl_response,JSON_OBJECT_AS_ARRAY);
1189
1190 if( isset($curl_response_decoded['success']) && $curl_response_decoded['success']===true ){
1191 set_transient( 'wpstream_request_pack_data_per_user_transient', $curl_response_decoded, 15 );
1192
1193 update_option('wpstream_api_username_from_token',$curl_response_decoded['username']);
1194 return $curl_response_decoded;
1195 }else{
1196 return false;
1197 }
1198
1199 }else{
1200 return $event_data_for_transient;
1201 }
1202
1203 }
1204
1205
1206 public function wpstream_check_user_quota() {
1207 $pack_data = $this->wpstream_request_pack_data_per_user('wpstream_check_user_quota');
1208 if ( ! $pack_data || ! isset( $pack_data['success'] ) || ! $pack_data['success'] ) {
1209 print json_encode(
1210 array(
1211 'success' => false,
1212 'error' => esc_html__('Couldn\'t get user quota.', 'wpstream'),
1213 )
1214 );
1215 } else {
1216 print json_encode($pack_data);
1217 }
1218 die();
1219 }
1220
1221
1222 /**
1223 * Edited 4.0
1224 *
1225 * Check Api Status
1226 *
1227 * @since 3.0.1
1228 * returns true or false
1229 */
1230
1231 function wpstream_client_check_api_status(){
1232 return true;
1233 }
1234
1235
1236
1237
1238
1239 /**
1240 *
1241 * Edited 4.0
1242 *
1243 * Start Get live events for users
1244 *
1245 * @since 3.0.1
1246 * returns true or false
1247 */
1248 public function wpstream_get_live_event_for_user($with_exit='yes'){
1249 $current_user = wp_get_current_user();
1250 $userID = $current_user->ID;
1251
1252 global $wpstream_plugin;
1253 if( !$wpstream_plugin->main->wpstream_check_user_can_stream() ){
1254 if($with_exit=='yes'){
1255 // esc_html_e ('You are not allowed to start a live stream !','wpstream');
1256 return;
1257 }else{
1258 return;
1259 }
1260
1261 }
1262
1263
1264 $event_data = $this->wpstream_request_live_stream_for_user($userID);
1265 $return_event = array();
1266 if(is_array($event_data)):
1267 foreach ($event_data as $key=>$event){
1268 $return_event[$event['channel_id']]=$event;
1269 }
1270 endif;
1271 return $return_event;
1272 }
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284 /**
1285 *
1286 * Edited 4.0
1287 *
1288 * Get live events for users
1289 *
1290 * @since 3.0.1
1291 * returns true or false
1292 */
1293 public function wpstream_request_live_stream_for_user($user_id){
1294
1295 global $wpstream_plugin;
1296
1297
1298 $domain = parse_url ( get_site_url() );
1299
1300
1301 $url = 'channel/list';
1302 $access_token = $this->wpstream_get_token();
1303
1304 // do not make the call if no token is available
1305 if (!$access_token) return false;
1306
1307 $curl_post_fields=array(
1308 'access_token' => $access_token,
1309 'domain' => $domain['host'],
1310 'status' => 'active'
1311 );
1312 $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields,true);
1313 $curl_response_decoded = json_decode($curl_response,JSON_OBJECT_AS_ARRAY);
1314
1315
1316
1317
1318 if( isset($curl_response_decoded['success']) && $curl_response_decoded['success']==true ){
1319 return $curl_response_decoded['channels'];
1320 }else{
1321 return false;
1322 }
1323
1324
1325 }
1326
1327
1328 /**
1329 * live events for shortocde - behind tranzisnt
1330 * returns an array with local show id for live events
1331 * @since 3.0.1
1332 * returns noda
1333 */
1334 public function api20_wpstream_request_live_stream_for_user_for_shortcode($outside=''){
1335 global $wpstream_plugin;
1336 $return_array=array();
1337
1338 $result = get_transient('wpstream_live_stream_for_user_for_shortcode');
1339
1340 if($result===false){
1341 $result = $this->wpstream_request_live_stream_for_user('');
1342 set_transient('wpstream_live_stream_for_user_for_shortcode',$result,30);
1343 }
1344
1345 if(is_array($result)):
1346 foreach($result as $key=>$event){
1347 $return_array[]=$event['channel_id'];
1348 }
1349 endif;
1350 return $return_array;
1351 }
1352
1353
1354
1355
1356 /**
1357 * Delete event
1358 *
1359 * @since 3.0.1
1360 * returns noda
1361 */
1362
1363 public function wpstream_close_event(){
1364 //not implemented yet
1365 }
1366
1367
1368 /**
1369 * Get signed upload form data
1370 *
1371 * @since 3.0.1
1372 * returns aws form
1373 */
1374 public function wpstream_get_signed_form_upload_data(){
1375 if( !current_user_can('administrator') ){
1376 exit('not admin on wpstream_get_signed_form_upload_data');
1377 }
1378
1379
1380 $url = 'video/upload';
1381 $access_token = $this->wpstream_get_token();
1382
1383 // do not make the call if no token is available
1384 if (!$access_token) return array(
1385 'success' => false,
1386 'error' => 'not_connected',
1387 );
1388
1389 $curl_post_fields=array(
1390 'access_token' => $access_token,
1391 );
1392 $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields,true);
1393 $curl_response_decoded = json_decode($curl_response,JSON_OBJECT_AS_ARRAY);
1394
1395
1396 return $curl_response_decoded;
1397 exit();
1398
1399 }
1400
1401
1402
1403
1404
1405
1406
1407 /**
1408 *
1409 * Get video from storage- clear data for front end use
1410 *
1411 * @since 3.0.1
1412 * returns aws data
1413 *
1414 */
1415 public function wpstream_get_videos(){
1416 if( !current_user_can('administrator') ){
1417 return;
1418 }
1419
1420
1421 $video_options = array();
1422 $video_array = $this->wpstream_get_videos_from_api();
1423 $video_list_raw_array = false;
1424
1425 if ( is_array($video_array) && isset($video_array['items']) && is_array($video_array['items']) ) {
1426 $video_list_raw_array = $video_array['items'];
1427 }
1428
1429 if(is_array($video_list_raw_array)){
1430 $keys = array_column($video_list_raw_array, 'time');
1431 array_multisort($keys, SORT_DESC , $video_list_raw_array);
1432
1433 foreach ($video_list_raw_array as $key => $videos){
1434 if($videos['name']!=''):
1435 $video_options[$videos['name']]=$videos['name'];
1436 endif;
1437 }
1438
1439 }
1440 return $video_options;
1441 }
1442
1443
1444
1445 /**
1446 *
1447 * Get video from storage- raw data
1448 *
1449 * @since 3.0.1
1450 * returns aws data
1451 *
1452 */
1453 public function wpstream_get_videos_from_api( ){
1454
1455 if( !current_user_can('administrator') ){
1456 exit('not admin on wpstream_get_videos_from_api');
1457 }
1458
1459
1460
1461 $url = 'video/list';
1462 $access_token = $this->wpstream_get_token();
1463
1464 // do not make the call if no token is available
1465 if (!$access_token) return false;
1466
1467 $curl_post_fields=array(
1468 'access_token' => $access_token,
1469 );
1470
1471 $current_page= get_current_screen();
1472 $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields,true);
1473 $curl_response_decoded = json_decode($curl_response,JSON_OBJECT_AS_ARRAY);
1474
1475
1476
1477
1478 if( isset($curl_response_decoded['success']) && $curl_response_decoded['success']==true ){
1479 return $curl_response_decoded;
1480 }else{
1481 return array();
1482 }
1483
1484 }
1485
1486
1487
1488 /**
1489 * Get download link from aws
1490 *
1491 * @since 3.0.1
1492 * returns aws data
1493 */
1494
1495 function wpstream_get_download_link(){
1496
1497 if( !current_user_can('administrator') ){
1498 exit('not admin on get_download_link');
1499 }
1500
1501 $video_name = sanitize_text_field($_POST['video_name']);
1502
1503 $url = 'video/download';
1504 $access_token = $this->wpstream_get_token();
1505
1506 // do not make the call if no token is available
1507 if (!$access_token) return false;
1508
1509 $curl_post_fields=array(
1510 'access_token' => $access_token,
1511 'name' => $video_name,
1512 );
1513 $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields);
1514 print $curl_response;
1515 exit();
1516
1517
1518
1519 }
1520
1521
1522 /**
1523 * Delete file from storage
1524 *
1525 * @since 3.0.1
1526 *
1527 */
1528 public function wpstream_get_delete_file(){
1529 if( !current_user_can('administrator') ){
1530 exit('not admin on get_delete_file');
1531 }
1532
1533 $video_name = esc_html($_POST['video_name']);
1534
1535
1536 $url = 'video/delete';
1537 $access_token = $this->wpstream_get_token();
1538
1539 // do not make the call if no token is available
1540 if (!$access_token) return false;
1541
1542 $curl_post_fields=array(
1543 'access_token' => $access_token,
1544 'name'=>$video_name,
1545 );
1546 $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields,true);
1547 $curl_response_decoded = json_decode($curl_response,JSON_OBJECT_AS_ARRAY);
1548
1549
1550 print $curl_response;
1551
1552 exit();
1553
1554
1555 }
1556
1557
1558 public function wpstream_check_pending_videos() {
1559 if (!current_user_can('administrator')) {
1560 wp_send_json_error(__('Unauthorized', 'wpstream'));
1561 }
1562
1563 $videos_list_raw = $this->wpstream_get_videos_from_api();
1564 wp_send_json_success($videos_list_raw);
1565 }
1566
1567
1568 }// end class
1569