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

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