PluginProbe
WpStream – Live Streaming, Video on Demand, Pay Per View / 4.8.2
WpStream – Live Streaming, Video on Demand, Pay Per View v4.8.2
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
← All changes | includes/class-wpstream-live-api-connection.php +437 -109 4.5.114.8.2 View file →
@@ -1,9 +1,9 @@
1 1 <?php
2 2
3 3 class Wpstream_Live_Api_Connection {
4 4
5 -
5 +
6 6
7 7
8 8 public function __construct() {
9 9 add_action( 'wp_ajax_wpstream_give_me_live_uri', array($this,'wpstream_give_me_live_uri') );
@@ -8,12 +8,17 @@
8 8 public function __construct() {
9 9 add_action( 'wp_ajax_wpstream_give_me_live_uri', array($this,'wpstream_give_me_live_uri') );
10 10 add_action( 'wp_ajax_wpstream_turn_of_channel', array($this,'wpstream_turn_of_channel') );
11 11 add_action( 'wp_ajax_wpstream_update_local_event_settings',array($this,'wpstream_update_local_event_settings'));
12 -
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 +
13 16 add_action( 'wp_ajax_wpstream_check_dns_sync', array($this,'wpstream_check_dns_sync') );
14 17 add_action( 'wp_ajax_wpstream_check_event_status', array($this,'wpstream_check_event_status') );
15 -
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 +
16 21 add_action( 'wp_ajax_wpstream_close_event', array($this,'wpstream_close_event') );
17 22 add_action( 'wp_ajax_wpstream_get_download_link', array($this,'wpstream_get_download_link') );
18 23 add_action( 'wp_ajax_wpstream_get_delete_file', array($this,'wpstream_get_delete_file') );
19 24
@@ -18,16 +23,14 @@
18 23 add_action( 'wp_ajax_wpstream_get_delete_file', array($this,'wpstream_get_delete_file') );
19 24
20 25 add_action( 'admin_notices',array($this, 'wpstream_admin_notices') );
21 26
22 -
27 + add_action( 'wp_ajax_wpstream_check_pending_videos', array($this,'wpstream_check_pending_videos') );
28 +
29 +
23 30 }
24 -
25 -
26 31
27 32
28 -
29 -
30 33 /*
31 34 * Admin Notices
32 35 *
33 36 *
@@ -58,12 +61,17 @@
58 61 }
59 62
60 63 $token = $this->wpstream_get_token();
61 64 if($token=='' and $page!='wpstream_plugin_options'){
62 - echo '<div class="api_not_conected wpstream_notice_top">'.__(' Incorrect username or password. Please check your credentials or go <a href="https://wpstream.net/my-account/edit-account/" target="_blank">here</a> to reset your password.','wpstream').'</div>';
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>';
63 71 }
64 72
65 -}
73 + }
66 74
67 75
68 76 /*
69 77 * Curl request
@@ -71,22 +79,18 @@
71 79 *
72 80 *
73 81 * */
74 82
75 - function wpstream_baker_do_curl_base($url,$curl_post_fields){
76 -
77 -
83 + function wpstream_baker_do_curl_base($url,$curl_post_fields, $expect_json = false, $quiet = false, $timeout = 10){
78 84 $curl = curl_init();
79 85 $api_url = WPSTREAM_API.'/'.$url;
80 86
81 -
82 -
83 87 curl_setopt_array($curl, array(
84 88 CURLOPT_URL =>$api_url,
85 89 CURLOPT_RETURNTRANSFER => true,
86 90 CURLOPT_ENCODING => "",
87 91 CURLOPT_MAXREDIRS => 10,
88 - CURLOPT_TIMEOUT => 10,
92 + CURLOPT_TIMEOUT => $timeout,
89 93 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
90 94 CURLOPT_CUSTOMREQUEST => "POST",
91 95 CURLOPT_POSTFIELDS => http_build_query($curl_post_fields),
92 96 CURLOPT_HTTPHEADER => array(
@@ -96,15 +100,86 @@
96 100 ));
97 101
98 102 $response = curl_exec($curl);
99 103 $err = curl_error($curl);
104 + $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
105 + curl_close($curl);
100 106
101 - curl_close($curl);
107 + $curl_failed = 0;
102 108
109 + $logger = new WpStream_Logger();
110 +
103 111 if ($err) {
104 - echo '<div class="api_not_conected wpstream_error_curl">cURL Error #: '.$err.'</div>';
105 - }
112 + // do not echo every time, some operations must return JSON
113 + if (!$quiet){
114 + echo '<div class="api_not_conected wpstream_error_curl">Critical: Could Not Connect to WpStream - '.$err.'</div>';
115 + }
106 116
117 + $curl_failed = $err;
118 +
119 + $log_entry = new WpStream_Log_Entry([
120 + 'type' => 'error',
121 + 'description' => 'CURL error: ' . $err . ' on endpoint ' . $url,
122 + ]);
123 + $logger->add( $log_entry );
124 +
125 + $response = json_encode( array(
126 + 'success' => false,
127 + 'error' => $err,
128 + ));
129 + }
130 + else if ($http_code != 200) {
131 + $log_entry = new WpStream_Log_Entry([
132 + 'type' => 'error',
133 + 'description' => 'HTTP error: ' . $http_code . ' on endpoint ' . $url,
134 + ]);
135 + $logger->add( $log_entry );
136 +
137 + if (!$quiet){
138 + switch ($http_code) {
139 + case 0:
140 + $message = "CURL failed with code 0. Please address CURL connectivity with your hosting provider.";
141 + break;
142 + case 429:
143 + $message = "API: Too many Requests";
144 + break;
145 + default:
146 + $message = "API - Unexpected response: " . $http_code;
147 + break;
148 + }
149 + echo '<div class="api_not_conected wpstream_error_curl">'.$message.'</div>';
150 + }
151 + $curl_failed = $http_code;
152 +
153 + $response = json_encode( array(
154 + 'success' => false,
155 + 'error' => $http_code,
156 + ));
157 + }
158 + else if ($expect_json){
159 + $curl_response_decoded = json_decode($response,JSON_OBJECT_AS_ARRAY);
160 + if (JSON_ERROR_NONE !== json_last_error()) {
161 + $log_entry = new WpStream_Log_Entry([
162 + 'type' => 'error',
163 + 'description' => 'Malformed JSON response: ' . json_last_error_msg() . ' on endpoint ' . $url,
164 + ]);
165 + $logger->add( $log_entry );
166 +
167 + if (!$quiet) {
168 + echo '<div class="api_not_conected wpstream_error_curl">Critical: Malformed API response #: ' . json_last_error() . '</div>';
169 + }
170 +
171 + $curl_failed = json_last_error();
172 +
173 + $response = json_encode(array(
174 + 'success' => false,
175 + 'error' => json_last_error(),
176 + ));
177 + }
178 + }
179 +
180 + update_option( "wpstream_curl_failed", $curl_failed );
181 +
107 182 return $response;
108 183 }
109 184
110 185
@@ -234,8 +309,31 @@
234 309 print json_encode($response);
235 310 die();
236 311
237 312 }
313 +
314 + public function wpstream_check_whipurl() {
315 + $channel_id = intval($_POST['channel_id']);
316 +
317 + $whip_url = get_post_meta($channel_id, 'whipUrl', true);
318 +
319 + if ( $whip_url ) {
320 + print json_encode(
321 + array(
322 + 'success' => true,
323 + 'whip_url' => $whip_url,
324 + )
325 + );
326 + } else {
327 + print json_encode(
328 + array(
329 + 'success' => false,
330 + 'error' => esc_html__('WHIP URL not found for this channel.', 'wpstream'),
331 + )
332 + );
333 + }
334 + die();
335 + }
238 336
239 337 /**
240 338 * edited 4.0
241 339 *
@@ -251,8 +349,11 @@
251 349 $access_token = $this->wpstream_get_token();
252 350 $domain = parse_url ( get_site_url() );
253 351 $url = 'channel/info';
254 352
353 + // do not make the call if no token is available
354 + if (!$access_token) return false;
355 +
255 356 $curl_post_fields=array(
256 357 'access_token' => $access_token,
257 358 'channel_id' => $channel_id,
258 359 'domain' => $domain['host'],
@@ -262,9 +363,9 @@
262 363
263 364
264 365
265 366
266 - $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields);
367 + $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields,true, false,WPSTREAM_TIMEOUT_CONST);
267 368
268 369
269 370 $curl_response_decoded = json_decode($curl_response,JSON_OBJECT_AS_ARRAY);
270 371
@@ -359,11 +460,149 @@
359 460 update_post_meta ($show_id,'local_event_options',$to_save_option);
360 461 $this->wpstream_update_chanel_on_baker($show_id,$to_save_option);
361 462
362 463 }
363 -
364 -
365 464
465 + public function wpstream_update_local_event_settings_with_global() {
466 + check_ajax_referer( 'wpstream_start_event_nonce', 'security' );
467 +
468 + check_ajax_referer( 'wpstream_start_event_nonce', 'security' );
469 + if(!is_user_logged_in()){
470 + wp_send_json_error(['success' => false, 'message' => __('Not logged in', 'wpstream')]);
471 + }
472 + if( !current_user_can('administrator') ){
473 + wp_send_json_error(['success' => false, 'message' => __('Not admin', 'wpstream')]);
474 + }
475 +
476 + if ( !isset($_POST['show_id']) || !isset($_POST['use_global']) ) {
477 + wp_send_json_error(['success' => false, 'message' => __('Missing parameters', 'wpstream')]);
478 + }
479 +
480 + $show_id = intval($_POST['show_id']);
481 + $default_channel_settings = get_option('wpstream_user_streaming_global_channel_options') ;
482 +
483 + $this->wpstream_update_chanel_on_baker($show_id, $default_channel_settings);
484 + }
485 +
486 + public function wpstream_update_use_global_event_options() {
487 + check_ajax_referer( 'wpstream_start_event_nonce', 'security' );
488 + if(!is_user_logged_in()){
489 + wp_send_json_error(['success' => false, 'message' => __('Not logged in', 'wpstream')]);
490 + }
491 + if( !current_user_can('administrator') ){
492 + wp_send_json_error(['success' => false, 'message' => __('Not admin', 'wpstream')]);
493 + }
494 +
495 + if ( !isset($_POST['show_id']) || !isset($_POST['use_global']) ) {
496 + wp_send_json_error(['success' => false, 'message' => __('Missing parameters', 'wpstream')]);
497 + }
498 +
499 + $show_id = intval($_POST['show_id']);
500 + $use_global_event_options = intval($_POST['use_global']);
501 +
502 + $result = update_post_meta(
503 + $show_id,
504 + 'use_global_event_options',
505 + $use_global_event_options
506 + );
507 +
508 + if ( !$result ) {
509 + wp_send_json_error(['success' => false, 'message' => __('Failed to update event', 'wpstream')]);
510 + }
511 +
512 + $local_event_options = get_post_meta($show_id,'local_event_options',true);
513 +
514 + if( $use_global_event_options ) {
515 + $global_event_options = get_option('wpstream_user_streaming_global_channel_options') ;
516 +
517 + // if the local options are not set, we need to set them to the global ones
518 + if ( !is_array($local_event_options) ) {
519 + update_post_meta( $show_id, 'local_event_options', $global_event_options );
520 + }
521 +
522 + $this->wpstream_update_chanel_on_baker( $show_id, $global_event_options );
523 + } else {
524 + $this->wpstream_update_chanel_on_baker( $show_id, $local_event_options );
525 + }
526 + }
527 +
528 + public function wpstream_update_default_channel_settings() {
529 + check_ajax_referer( 'wpstream-settings-nonce', 'security' );
530 +
531 + $options_array= $_POST['option'];
532 + $sanitized_options = array();
533 + foreach( $options_array as $key => $value ) {
534 + $sanitized_options[ sanitize_key( $key ) ] = sanitize_text_field( $value );
535 + }
536 +
537 + if ( $sanitized_options['low_latency'] == 1 || $sanitized_options['adaptive_bitrate'] == 1 ) {
538 + $sanitized_options['encrypt'] = 0;
539 + }
540 +
541 + if ( $sanitized_options['encrypt'] == 1 ) {
542 + $sanitized_options['low_latency'] = 0;
543 + $sanitized_options['adaptive_bitrate'] = 0;
544 + }
545 +
546 + $successful_update = update_option( 'wpstream_user_streaming_global_channel_options', $sanitized_options );
547 +
548 + if ( $successful_update ) {
549 + wp_send_json(
550 + array(
551 + 'success' => true,
552 + )
553 + );
554 + } else {
555 + wp_send_json_error(
556 + array(
557 + 'success' => false,
558 + )
559 + );
560 + }
561 +
562 + wp_die();
563 + }
564 +
565 + public function wpstream_update_settings() {
566 + check_ajax_referer( 'wpstream-settings-nonce', 'security' );
567 +
568 + $option_name = sanitize_key( $_POST['option_name'] );
569 + $option_type = sanitize_key( $_POST['option_type'] );
570 +
571 + switch( $option_type ) {
572 + case 'checkbox':
573 + $option_value = filter_var( $_POST['option_value'], FILTER_VALIDATE_INT );
574 + break;
575 + case 'text':
576 + case 'select':
577 + $option_value = sanitize_text_field( $_POST['option_value'] );
578 + break;
579 + case 'multiple-select':
580 + $option_value = array_map( 'sanitize_text_field', $_POST['option_value'] );
581 + break;
582 + default:
583 + $option_value = sanitize_text_field( $_POST['option_value'] );
584 + }
585 +
586 + $successful_update = update_option( 'wpstream_' . $option_name, $option_value );
587 +
588 + if( $successful_update ) {
589 + wp_send_json(
590 + array(
591 + 'success' => true,
592 + )
593 + );
594 + } else {
595 + wp_send_json_error(
596 + array(
597 + 'success' => false,
598 + )
599 + );
600 + }
601 +
602 + wp_die();
603 + }
604 +
366 605 /**
367 606 * Update channel settings on baker
368 607 *
369 608 * @since 4.2
@@ -374,8 +613,10 @@
374 613 public function wpstream_update_chanel_on_baker($channel_id,$to_save_option){
375 614
376 615 $access_token = $this->wpstream_get_token();
377 616 if($access_token==''){
617 + // cleanup any previous echo before sending json
618 + ob_end_clean();
378 619 echo json_encode( array(
379 620 'is_record' => '',
380 621 'conected' => false,
381 622 'event_data' => '',
@@ -451,9 +692,9 @@
451 692 );
452 693
453 694
454 695
455 - $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields);
696 + $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields,true);
456 697 print_r($curl_response);
457 698 exit();
458 699 }
459 700
@@ -471,8 +712,10 @@
471 712 public function wpstream_turn_of_channel(){
472 713
473 714 $access_token = $this->wpstream_get_token();
474 715 if($access_token==''){
716 + // cleanup any previous echo before sending json
717 + ob_end_clean();
475 718 echo json_encode( array(
476 719 'is_record' => '',
477 720 'conected' => false,
478 721 'event_data' => '',
@@ -502,19 +745,23 @@
502 745 );
503 746
504 747
505 748
506 - $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields);
749 + $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields,true);
507 750 $curl_response_decoded = json_decode($curl_response,JSON_OBJECT_AS_ARRAY);
508 751
509 752
510 753
511 754 if( isset($curl_response_decoded['success']) && $curl_response_decoded['success']===true ){
755 + // cleanup any previous echo before sending json
756 + ob_end_clean();
512 757 echo json_encode( array(
513 758 'conected' => true,
514 759 'answer' => $curl_response_decoded,
515 760 ));
516 761 }else{
762 + // cleanup any previous echo before sending json
763 + ob_end_clean();
517 764 echo json_encode( array(
518 765 'conected' => false,
519 766 'error' => esc_html__('Channel is already turned off or does not exist!','wpstream'),
520 767 'answer' => $curl_response_decoded,
@@ -540,8 +787,10 @@
540 787 public function wpstream_give_me_live_uri(){
541 788
542 789 $access_token = $this->wpstream_get_token();
543 790 if($access_token==''){
791 + // cleanup any previous echo before sending json
792 + ob_end_clean();
544 793 echo json_encode( array(
545 794 'is_record' => '',
546 795 'conected' => false,
547 796 'event_data' => '',
@@ -560,8 +809,9 @@
560 809 }
561 810
562 811
563 812 $channel_id = intval($_POST['show_id']);
813 + $basic_streaming = filter_var($_POST['basic_streaming'], FILTER_VALIDATE_BOOLEAN);
564 814 $on_boarding = '';
565 815 if(isset($_POST['start_onboarding'])){
566 816 $on_boarding = sanitize_text_field($_POST['start_onboarding']);
567 817 }
@@ -566,65 +816,81 @@
566 816 $on_boarding = sanitize_text_field($_POST['start_onboarding']);
567 817 }
568 818
569 819
570 - $local_event_options = get_post_meta($channel_id,'local_event_options',true);
571 - if(!is_array($local_event_options)){
572 - $local_event_options = get_option('wpstream_user_streaming_global_channel_options') ;
573 - }
820 + $local_event_options = get_post_meta($channel_id,'local_event_options',true);
821 + $use_global_event_options = get_post_meta($channel_id,'use_global_event_options',true);
822 + $is_local_event_options_enabled = ( is_array( $local_event_options ) && empty( $use_global_event_options ) ) ||
823 + ( !empty($use_global_event_options) && intval( $use_global_event_options ) === 0 );
824 + if( !$is_local_event_options_enabled ) {
825 + $local_event_options = get_option('wpstream_user_streaming_global_channel_options') ;
826 + }
574 827
828 + $is_autostart="false";
829 + $is_encrypt="false";
830 + $low_latency="false";
831 + $adaptive_bitrate="false";
832 + $is_record="false";
833 + $corsorigin='*';
575 834
576 - // set encrypt option
577 - $is_autostart="false";
578 - if( intval( $local_event_options['autostart']) ==1 ){
579 - $is_autostart="true";
580 - }
835 + if ( is_array($local_event_options) ) {
836 + if ( isset($local_event_options['autostart']) &&
837 + intval( $local_event_options['autostart']) == 1 ) {
838 + $is_autostart = "true";
839 + }
581 840
841 + if ( isset($local_event_options['encrypt']) &&
842 + intval( $local_event_options['encrypt']) == 1 ) {
843 + $is_encrypt = "true";
844 + }
582 845
583 - // set encrypt option
584 - $is_encrypt="false";
585 - if( intval( $local_event_options['encrypt']) ==1 ){
586 - $is_encrypt="true";
587 - }
846 + if ( isset($local_event_options['low_latency']) &&
847 + intval( $local_event_options['low_latency']) == 1 ) {
848 + $low_latency = "true";
849 + }
588 850
589 - $low_latency="false";
590 - if( intval( $local_event_options['low_latency']) ==1 ){
591 - $low_latency="true";
592 - }
851 + if ( isset($local_event_options['adaptive_bitrate']) &&
852 + intval( $local_event_options['adaptive_bitrate']) == 1 ) {
853 + $adaptive_bitrate = "true";
854 + }
593 855
594 - $adaptive_bitrate="false";
595 - if( intval( $local_event_options['adaptive_bitrate']) ==1 ){
596 - $adaptive_bitrate="true";
597 - }
856 + if ( isset($local_event_options['record']) &&
857 + intval( $local_event_options['record']) == 1 ) {
858 + $is_record = "true";
859 + }
598 860
599 - if($adaptive_bitrate=="true" || $low_latency=="true" ){
600 - $is_encrypt="false";
601 - }
861 + if ( !isset($local_event_options['domain_lock']) || intval( $local_event_options['domain_lock']) == 0 ) {
862 + $corsorigin = '*';
863 + }
864 + }
602 865
603 - if( $is_encrypt=="true"){
604 - $adaptive_bitrate="false";
605 - $low_latency="false";
606 - }
866 + if( $adaptive_bitrate=="true" || $low_latency=="true" ) {
867 + $is_encrypt="false";
868 + }
607 869
870 + if( $is_encrypt=="true" ) {
871 + $adaptive_bitrate="false";
872 + $low_latency="false";
873 + }
608 874
609 -
610 - // set record option
611 - $is_record="false";
612 - if( intval( $local_event_options['record']) ==1 ){
613 - $is_record="true";
614 - }
615 -
616 - $corsorigin='';
617 - if( isset($local_event_options['domain_lock']) && intval( $local_event_options['domain_lock']) ==0 ){
618 - $corsorigin='*';
619 - }
620 -
621 -
622 - $event_data = $this->wpstream_request_live_stream_uri($channel_id,$is_autostart,$is_record,$is_encrypt,$low_latency,$adaptive_bitrate,$userID,$corsorigin,$on_boarding);
875 + $event_data = $this->wpstream_request_live_stream_uri(
876 + $channel_id,
877 + $is_autostart,
878 + $is_record,
879 + $is_encrypt,
880 + $low_latency,
881 + $adaptive_bitrate,
882 + $userID,
883 + $corsorigin,
884 + $on_boarding,
885 + $basic_streaming
886 + );
623 887
624 888
625 889
626 890 if( isset($event_data['success']) && $event_data['success']===true ){
891 + // cleanup any previous echo before sending json
892 + ob_end_clean();
627 893 echo json_encode( array(
628 894 'is_record' => $is_record,
629 895 'conected' => true,
630 896 'event_data' => $event_data,
@@ -641,8 +907,10 @@
641 907
642 908 }
643 909
644 910 }
911 + // cleanup any previous echo before sending json
912 + ob_end_clean();
645 913 echo json_encode( array(
646 914 'is_record' => $is_record,
647 915 'conected' => false,
648 916 'event_data' => $event_data,
@@ -665,9 +933,9 @@
665 933 * returns live url
666 934 */
667 935
668 936
669 - public function wpstream_request_live_stream_uri($schannel_id,$is_autostart,$is_record,$is_encrypt,$low_latency,$adaptive_bitrate,$request_by_userid,$corsorigin,$on_boarding){
937 + public function wpstream_request_live_stream_uri($schannel_id,$is_autostart,$is_record,$is_encrypt,$low_latency,$adaptive_bitrate,$request_by_userid,$corsorigin,$on_boarding, $basic_streaming){
670 938
671 939 $domain = parse_url ( get_site_url() );
672 940 $domain_scheme = 'http';
673 941 if(is_ssl()){
@@ -693,9 +961,10 @@
693 961 }else{
694 962 $low_latency = false;
695 963 }
696 964
697 -
965 + $basic_streaming = $basic_streaming ? 'true' : 'false';
966 +
698 967 $url = 'channel/start';
699 968 $access_token = $this->wpstream_get_token();
700 969
701 970 $metadata_array=array(
@@ -704,9 +973,12 @@
704 973
705 974 if($on_boarding!=''){
706 975 $metadata_array['on_boarding']='yes';
707 976 }
708 -
977 + $permalink = get_permalink($schannel_id);
978 + if ($permalink !== false) {
979 + $metadata_array['permalink'] = $permalink;
980 + }
709 981
710 982 $curl_post_fields=array(
711 983 'access_token' => $access_token,
712 984 'channel_id' => $schannel_id,
@@ -719,14 +991,14 @@
719 991 'hls_keys_url_prefix' => get_site_url().'?wpstream_livedrm=',
720 992 'allow_key_access_from' => $domain_ip,
721 993 'metadata' => json_encode($metadata_array),
722 994 'autostart' => $is_autostart,
995 + 'basic_streaming' => $basic_streaming,
723 996 // 'fakeError' => 'init'
724 997 );
725 998
726 999
727 -
728 - $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields);
1000 + $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields,true);
729 1001 $curl_response_decoded = json_decode($curl_response,JSON_OBJECT_AS_ARRAY);
730 1002 // $curl_response_decoded['curl_post_fields']=$curl_post_fields;
731 1003 return $curl_response_decoded;
732 1004
@@ -751,15 +1023,20 @@
751 1023 * returns token
752 1024 */
753 1025 public function wpstream_get_token(){
754 1026 $token = get_transient('wpstream_token_api');
755 - if ( false === $token || $token==='' ) {
1027 + if ( false === $token || $token==='') {
756 1028 $token = $this->wpstream_club_get_token();
757 - set_transient( 'wpstream_token_api', $token ,3500);
1029 + if ($token !== false){
1030 + set_transient( 'wpstream_token_api', $token ,3500);
1031 + }
1032 + else {
1033 + // cache the failed response for a second, otherwise it'll make a shitload of requests
1034 + set_transient( 'wpstream_token_api', 'failed' , 1);
1035 + }
758 1036 }
759 -
760 - return $token;
761 -
1037 + $ret = $token === 'failed' ? false : $token;
1038 + return $ret;
762 1039 }
763 1040
764 1041
765 1042 public function wpstream_get_token_30(){
@@ -771,10 +1048,10 @@
771 1048
772 1049 return $token;
773 1050
774 1051 }
775 -
776 1052
1053 +
777 1054 /**
778 1055 * Edited 4.0
779 1056 *
780 1057 * Request auth token from wpstream.net
@@ -782,13 +1059,11 @@
782 1059 * @since 3.0.1
783 1060 * returns token fron wpstream
784 1061 */
785 1062 protected function wpstream_club_get_token(){
1063 + $username = get_option('wpstream_api_username','');
1064 + $password = get_option('wpstream_api_password','');
786 1065
787 -
788 - $username = esc_html ( get_option('wpstream_api_username','') );
789 - $password = esc_html ( get_option('wpstream_api_password','') );
790 -
791 1066 if ( $username=='' || $password==''){
792 1067 return;
793 1068 }
794 1069
@@ -795,14 +1070,14 @@
795 1070 $url='access_token';
796 1071 $curl_post_fields=array(
797 1072 'grant_type' => 'password',
798 1073 'username' => $username,
799 - 'password' => $password
1074 + 'password' => $password
800 1075 );
801 1076
802 1077
803 1078
804 - $curl_response=$this->wpstream_baker_do_curl_base($url,$curl_post_fields);
1079 + $curl_response=$this->wpstream_baker_do_curl_base($url,$curl_post_fields, true);
805 1080
806 1081 $response= json_decode($curl_response);
807 1082
808 1083 if( isset($response->access_token) && $response->access_token!='' ){
@@ -891,20 +1166,26 @@
891 1166 * @since 3.0.1
892 1167 * returns pack data
893 1168 */
894 1169
895 - public function wpstream_request_pack_data_per_user(){
1170 + public function wpstream_request_pack_data_per_user($context = ''){
896 1171
897 1172 $event_data_for_transient = get_transient( 'wpstream_request_pack_data_per_user_transient' );
898 1173
899 1174 if($event_data_for_transient===false){
900 1175 $url = 'user/quota';
901 - $access_token = $this->wpstream_get_token();
1176 + $access_token = $this->wpstream_get_token();
1177 +
1178 + // do not make the call if no token is available
1179 + if (!$access_token) return false;
1180 +
902 1181 $curl_post_fields=array(
903 - 'access_token'=>$access_token
1182 + 'access_token'=>$access_token,
1183 + 'context' => $context,
1184 + 'plugin_version' => WPSTREAM_PLUGIN_VERSION
904 1185 );
905 1186
906 - $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields);
1187 + $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields,true, false, WPSTREAM_TIMEOUT_CONST);
907 1188 $curl_response_decoded = json_decode($curl_response,JSON_OBJECT_AS_ARRAY);
908 1189
909 1190 if( isset($curl_response_decoded['success']) && $curl_response_decoded['success']===true ){
910 1191 set_transient( 'wpstream_request_pack_data_per_user_transient', $curl_response_decoded, 15 );
@@ -921,8 +1202,22 @@
921 1202
922 1203 }
923 1204
924 1205
1206 + public function wpstream_check_user_quota() {
1207 + $pack_data = $this->wpstream_request_pack_data_per_user('wpstream_check_user_quota');
1208 + if ( ! $pack_data || ! isset( $pack_data['success'] ) || ! $pack_data['success'] ) {
1209 + print json_encode(
1210 + array(
1211 + 'success' => false,
1212 + 'error' => esc_html__('Couldn\'t get user quota.', 'wpstream'),
1213 + )
1214 + );
1215 + } else {
1216 + print json_encode($pack_data);
1217 + }
1218 + die();
1219 + }
925 1220
926 1221
927 1222 /**
928 1223 * Edited 4.0
@@ -949,16 +1244,21 @@
949 1244 *
950 1245 * @since 3.0.1
951 1246 * returns true or false
952 1247 */
953 -
954 - public function wpstream_get_live_event_for_user(){
1248 + public function wpstream_get_live_event_for_user($with_exit='yes'){
955 1249 $current_user = wp_get_current_user();
956 1250 $userID = $current_user->ID;
957 1251
958 1252 global $wpstream_plugin;
959 1253 if( !$wpstream_plugin->main->wpstream_check_user_can_stream() ){
960 - exit('You are not allowed to start a live stream !');
1254 + if($with_exit=='yes'){
1255 + // esc_html_e ('You are not allowed to start a live stream !','wpstream');
1256 + return;
1257 + }else{
1258 + return;
1259 + }
1260 +
961 1261 }
962 1262
963 1263
964 1264 $event_data = $this->wpstream_request_live_stream_for_user($userID);
@@ -973,8 +1273,9 @@
973 1273
974 1274
975 1275
976 1276
1277 +
977 1278
978 1279
979 1280
980 1281
@@ -998,14 +1299,18 @@
998 1299
999 1300
1000 1301 $url = 'channel/list';
1001 1302 $access_token = $this->wpstream_get_token();
1303 +
1304 + // do not make the call if no token is available
1305 + if (!$access_token) return false;
1306 +
1002 1307 $curl_post_fields=array(
1003 1308 'access_token' => $access_token,
1004 1309 'domain' => $domain['host'],
1005 1310 'status' => 'active'
1006 1311 );
1007 - $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields);
1312 + $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields,true);
1008 1313 $curl_response_decoded = json_decode($curl_response,JSON_OBJECT_AS_ARRAY);
1009 1314
1010 1315
1011 1316
@@ -1073,12 +1378,19 @@
1073 1378
1074 1379
1075 1380 $url = 'video/upload';
1076 1381 $access_token = $this->wpstream_get_token();
1382 +
1383 + // do not make the call if no token is available
1384 + if (!$access_token) return array(
1385 + 'success' => false,
1386 + 'error' => 'not_connected',
1387 + );
1388 +
1077 1389 $curl_post_fields=array(
1078 1390 'access_token' => $access_token,
1079 1391 );
1080 - $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields);
1392 + $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields,true);
1081 1393 $curl_response_decoded = json_decode($curl_response,JSON_OBJECT_AS_ARRAY);
1082 1394
1083 1395
1084 1396 return $curl_response_decoded;
@@ -1101,19 +1413,21 @@
1101 1413 *
1102 1414 */
1103 1415 public function wpstream_get_videos(){
1104 1416 if( !current_user_can('administrator') ){
1105 - exit('not admin on wpstream_get_videos');
1417 + return;
1106 1418 }
1107 1419
1108 1420
1109 1421 $video_options = array();
1110 1422 $video_array = $this->wpstream_get_videos_from_api();
1111 -
1112 - $video_list_raw_array = $video_array['items'];
1113 -
1114 -
1115 - if(is_array($video_list_raw_array)){
1423 + $video_list_raw_array = false;
1424 +
1425 + if ( is_array($video_array) && isset($video_array['items']) && is_array($video_array['items']) ) {
1426 + $video_list_raw_array = $video_array['items'];
1427 + }
1428 +
1429 + if(is_array($video_list_raw_array)){
1116 1430 $keys = array_column($video_list_raw_array, 'time');
1117 1431 array_multisort($keys, SORT_DESC , $video_list_raw_array);
1118 1432
1119 1433 foreach ($video_list_raw_array as $key => $videos){
@@ -1123,9 +1437,8 @@
1123 1437 }
1124 1438
1125 1439 }
1126 1440 return $video_options;
1127 -
1128 1441 }
1129 1442
1130 1443
1131 1444
@@ -1146,14 +1459,18 @@
1146 1459
1147 1460
1148 1461 $url = 'video/list';
1149 1462 $access_token = $this->wpstream_get_token();
1463 +
1464 + // do not make the call if no token is available
1465 + if (!$access_token) return false;
1466 +
1150 1467 $curl_post_fields=array(
1151 1468 'access_token' => $access_token,
1152 1469 );
1153 1470
1154 1471 $current_page= get_current_screen();
1155 - $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields);
1472 + $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields,true);
1156 1473 $curl_response_decoded = json_decode($curl_response,JSON_OBJECT_AS_ARRAY);
1157 1474
1158 1475
1159 1476
@@ -1184,8 +1501,12 @@
1184 1501 $video_name = sanitize_text_field($_POST['video_name']);
1185 1502
1186 1503 $url = 'video/download';
1187 1504 $access_token = $this->wpstream_get_token();
1505 +
1506 + // do not make the call if no token is available
1507 + if (!$access_token) return false;
1508 +
1188 1509 $curl_post_fields=array(
1189 1510 'access_token' => $access_token,
1190 1511 'name' => $video_name,
1191 1512 );
@@ -1213,13 +1534,17 @@
1213 1534
1214 1535
1215 1536 $url = 'video/delete';
1216 1537 $access_token = $this->wpstream_get_token();
1538 +
1539 + // do not make the call if no token is available
1540 + if (!$access_token) return false;
1541 +
1217 1542 $curl_post_fields=array(
1218 1543 'access_token' => $access_token,
1219 1544 'name'=>$video_name,
1220 1545 );
1221 - $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields);
1546 + $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields,true);
1222 1547 $curl_response_decoded = json_decode($curl_response,JSON_OBJECT_AS_ARRAY);
1223 1548
1224 1549
1225 1550 print $curl_response;
@@ -1229,12 +1554,15 @@
1229 1554
1230 1555 }
1231 1556
1232 1557
1233 -
1234 -
1235 -
1558 + public function wpstream_check_pending_videos() {
1559 + if (!current_user_can('administrator')) {
1560 + wp_send_json_error(__('Unauthorized', 'wpstream'));
1561 + }
1236 1562
1237 -
1238 -
1239 -
1240 -}// end class
1563 + $videos_list_raw = $this->wpstream_get_videos_from_api();
1564 + wp_send_json_success($videos_list_raw);
1565 + }
1566 +
1567 +
1568 +}// end class