PluginProbe
WpStream – Live Streaming, Video on Demand, Pay Per View / 4.11.1
WpStream – Live Streaming, Video on Demand, Pay Per View v4.11.1
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.11.1, at includes/class-wpstream-live-api-connection.php

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