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

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