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

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