PluginProbe
WpStream – Live Streaming, Video on Demand, Pay Per View / 4.11.1
WpStream – Live Streaming, Video on Demand, Pay Per View v4.11.1
4.14.1 4.14.0 4.13.2 4.13.1 4.13 4.12.5 4.12.4 4.12.3 4.12.2 4.12.1 4.12 4.4.4 4.4.5 4.4.6 4.4.7 4.4.8 4.4.9 4.5 4.5.1 4.5.11 4.5.11.1 4.5.11.2 4.5.11.4 4.5.11.5 4.5.11.6 All 181 releases
← All changes | includes/class-wpstream-live-api-connection.php +559 -226 4.4.4 → 4.11.1 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,19 @@
71 79 *
72 80 *
73 81 * */
74 82
75 - function wpstream_baker_do_curl_base($url,$curl_post_fields){
76 -
83 + function wpstream_baker_do_curl_base($url,$curl_post_fields, $expect_json = false, $quiet = false, $timeout = 10){
84 + $curl = curl_init();
85 + $base_api_url = defined('WPSTREAM_TEST_API' ) ? WPSTREAM_TEST_API : WPSTREAM_API;
86 + $api_url = $base_api_url . '/' . $url;
77 87
78 - $curl = curl_init();
79 - $api_url = WPSTREAM_API.'/'.$url;
80 -
81 -
82 -
83 88 curl_setopt_array($curl, array(
84 89 CURLOPT_URL =>$api_url,
85 90 CURLOPT_RETURNTRANSFER => true,
86 91 CURLOPT_ENCODING => "",
87 92 CURLOPT_MAXREDIRS => 10,
88 - CURLOPT_TIMEOUT => 10,
93 + CURLOPT_TIMEOUT => $timeout,
89 94 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
90 95 CURLOPT_CUSTOMREQUEST => "POST",
91 96 CURLOPT_POSTFIELDS => http_build_query($curl_post_fields),
92 97 CURLOPT_HTTPHEADER => array(
@@ -96,16 +101,90 @@
96 101 ));
97 102
98 103 $response = curl_exec($curl);
99 104 $err = curl_error($curl);
105 + $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
106 + curl_close($curl);
100 107
101 - curl_close($curl);
108 + $curl_failed = 0;
102 109
110 + $logger = new WpStream_Logger();
111 +
103 112 if ($err) {
104 - echo "cURL Error #:" . $err;
105 - }
113 + // do not echo every time, some operations must return JSON
114 + if (!$quiet){
115 + echo '<div class="api_not_conected wpstream_error_curl">Critical: Could Not Connect to WpStream - '.$err.'</div>';
116 + }
117 +
118 + $curl_failed = $err;
119 +
120 + $log_entry = new WpStream_Log_Entry([
121 + 'type' => 'error',
122 + 'description' => 'CURL error: ' . $err . ' on endpoint ' . $url,
123 + ]);
124 + $logger->add( $log_entry );
125 +
126 + $response = json_encode( array(
127 + 'success' => false,
128 + 'error' => $err,
129 + ));
130 + }
131 + else if ($http_code != 200) {
132 + $log_entry = new WpStream_Log_Entry([
133 + 'type' => 'error',
134 + 'description' => 'HTTP error: ' . $http_code . ' on endpoint ' . $url,
135 + ]);
136 + $logger->add( $log_entry );
137 +
138 + if (!$quiet){
139 + switch ($http_code) {
140 + case 0:
141 + $message = "CURL failed with code 0. Please address CURL connectivity with your hosting provider.";
142 + break;
143 + case 429:
144 + $message = "API: Too many Requests";
145 + break;
146 + case 403:
147 + $message = "API: Access Forbidden with response: " . $response . ' and HTTP code: ' . $http_code;
148 + break;
149 + default:
150 + $message = "API - Unexpected response: " . $http_code;
151 + break;
152 + }
153 + echo '<div class="api_not_conected wpstream_error_curl">'.$message.'</div>';
154 + }
155 + $curl_failed = $http_code;
156 +
157 + $response = json_encode( array(
158 + 'success' => false,
159 + 'error' => $http_code,
160 + ));
161 + }
162 + else if ($expect_json){
163 + $curl_response_decoded = json_decode($response,JSON_OBJECT_AS_ARRAY);
164 + if (JSON_ERROR_NONE !== json_last_error()) {
165 + $log_entry = new WpStream_Log_Entry([
166 + 'type' => 'error',
167 + 'description' => 'Malformed JSON response: ' . json_last_error_msg() . ' on endpoint ' . $url,
168 + ]);
169 + $logger->add( $log_entry );
170 +
171 + if (!$quiet) {
172 + echo '<div class="api_not_conected wpstream_error_curl">Critical: Malformed API response #: ' . json_last_error() . '</div>';
173 + }
174 +
175 + $curl_failed = json_last_error();
176 +
177 + $response = json_encode(array(
178 + 'success' => false,
179 + 'error' => json_last_error(),
180 + ));
181 + }
182 + }
183 +
184 + update_option( "wpstream_curl_failed", $curl_failed );
185 +
106 186 return $response;
107 -
108 187 }
109 188
110 189
111 190
@@ -181,11 +260,17 @@
181 260 * returns live url
182 261 */
183 262
184 263 public function wpstream_check_event_status(){
264 + check_ajax_referer( 'wpstream_start_event_nonce', 'nonce' );
185 265 $channel_id = intval($_POST['channel_id']);
186 -
187 - $response = $this->wpstream_check_event_status_api_call($channel_id);
266 + $notes = 'wpstream_check_event_status_note';
267 + if(isset($_POST['notes'])){
268 + $notes = sanitize_text_field($_POST['notes']);
269 + }
270 +
271 +
272 + $response = $this->wpstream_check_event_status_api_call($channel_id,$notes);
188 273
189 274
190 275
191 276 if( isset($response['success']) && $response['success']){
@@ -229,8 +314,32 @@
229 314 print json_encode($response);
230 315 die();
231 316
232 317 }
318 +
319 + public function wpstream_check_whipurl() {
320 + check_ajax_referer( 'wpstream_start_event_nonce', 'nonce' );
321 + $channel_id = intval($_POST['channel_id']);
322 +
323 + $whip_url = get_post_meta($channel_id, 'whipUrl', true);
324 +
325 + if ( $whip_url ) {
326 + print json_encode(
327 + array(
328 + 'success' => true,
329 + 'whip_url' => $whip_url,
330 + )
331 + );
332 + } else {
333 + print json_encode(
334 + array(
335 + 'success' => false,
336 + 'error' => esc_html__('WHIP URL not found for this channel.', 'wpstream'),
337 + )
338 + );
339 + }
340 + die();
341 + }
233 342
234 343 /**
235 344 * edited 4.0
236 345 *
@@ -240,18 +349,22 @@
240 349 * returns live url
241 350 */
242 351
243 352
244 - public function wpstream_check_event_status_api_call($channel_id){
353 + public function wpstream_check_event_status_api_call($channel_id,$notes){
245 354
246 355 $access_token = $this->wpstream_get_token();
247 356 $domain = parse_url ( get_site_url() );
248 357 $url = 'channel/info';
249 358
359 + // do not make the call if no token is available
360 + if (!$access_token) return false;
361 +
250 362 $curl_post_fields=array(
251 363 'access_token' => $access_token,
252 364 'channel_id' => $channel_id,
253 365 'domain' => $domain['host'],
366 + 'notes' => $notes
254 367 );
255 368
256 369
257 370
@@ -256,9 +369,9 @@
256 369
257 370
258 371
259 372
260 - $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields);
373 + $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields,true, false,WPSTREAM_TIMEOUT_CONST);
261 374
262 375
263 376 $curl_response_decoded = json_decode($curl_response,JSON_OBJECT_AS_ARRAY);
264 377
@@ -320,10 +433,11 @@
320 433 * returns live url
321 434 */
322 435
323 436
324 - public function wpstream_update_local_event_settings(){
437 + public function wpstream_update_local_event_settings(){
325 438
439 + check_ajax_referer( 'wpstream_start_event_nonce', 'security' );
326 440 if(!is_user_logged_in()){
327 441 exit('not logged in');
328 442 }
329 443 if( !current_user_can('administrator') ){
@@ -336,22 +450,171 @@
336 450 $to_save_option=array();
337 451 foreach($option_array as $key=>$value){
338 452 $to_save_option[sanitize_key($key)]=sanitize_text_field($value);
339 453 }
340 -
341 -
342 -
454 +
455 + if( $to_save_option['low_latency'] == 1 || $to_save_option['adaptive_bitrate'] ==1 ){
456 + $to_save_option['encrypt']=0;
457 + }
458 +
459 + if( $to_save_option['encrypt']==1){
460 + $to_save_option['low_latency'] = 0;
461 + $to_save_option['adaptive_bitrate'] =0;
462 + }
463 +
464 +
343 465
344 466 update_post_meta ($show_id,'local_event_options',$to_save_option);
345 - print 'do update with';
346 - print_r($to_save_option);
347 -
348 467 $this->wpstream_update_chanel_on_baker($show_id,$to_save_option);
349 468
350 469 }
351 -
352 -
353 470
471 + public function wpstream_update_local_event_settings_with_global() {
472 + check_ajax_referer( 'wpstream_start_event_nonce', 'security' );
473 +
474 + check_ajax_referer( 'wpstream_start_event_nonce', 'security' );
475 + if(!is_user_logged_in()){
476 + wp_send_json_error(['success' => false, 'message' => __('Not logged in', 'wpstream')]);
477 + }
478 + if( !current_user_can('administrator') ){
479 + wp_send_json_error(['success' => false, 'message' => __('Not admin', 'wpstream')]);
480 + }
481 +
482 + if ( !isset($_POST['show_id']) || !isset($_POST['use_global']) ) {
483 + wp_send_json_error(['success' => false, 'message' => __('Missing parameters', 'wpstream')]);
484 + }
485 +
486 + $show_id = intval($_POST['show_id']);
487 + $default_channel_settings = get_option('wpstream_user_streaming_global_channel_options') ;
488 +
489 + $this->wpstream_update_chanel_on_baker($show_id, $default_channel_settings);
490 + }
491 +
492 + public function wpstream_update_use_global_event_options() {
493 + check_ajax_referer( 'wpstream_start_event_nonce', 'security' );
494 + if(!is_user_logged_in()){
495 + wp_send_json_error(['success' => false, 'message' => __('Not logged in', 'wpstream')]);
496 + }
497 + if( !current_user_can('administrator') ){
498 + wp_send_json_error(['success' => false, 'message' => __('Not admin', 'wpstream')]);
499 + }
500 +
501 + if ( !isset($_POST['show_id']) || !isset($_POST['use_global']) ) {
502 + wp_send_json_error(['success' => false, 'message' => __('Missing parameters', 'wpstream')]);
503 + }
504 +
505 + $show_id = intval($_POST['show_id']);
506 + $use_global_event_options = intval($_POST['use_global']);
507 +
508 + $result = update_post_meta(
509 + $show_id,
510 + 'use_global_event_options',
511 + $use_global_event_options
512 + );
513 +
514 + if ( !$result ) {
515 + wp_send_json_error(['success' => false, 'message' => __('Failed to update event', 'wpstream')]);
516 + }
517 +
518 + $local_event_options = get_post_meta($show_id,'local_event_options',true);
519 +
520 + if( $use_global_event_options ) {
521 + $global_event_options = get_option('wpstream_user_streaming_global_channel_options');
522 +
523 + $this->wpstream_update_chanel_on_baker( $show_id, $global_event_options );
524 + } else {
525 + $global_event_options = get_option('wpstream_user_streaming_global_channel_options');
526 +
527 + // if the local options are not set, we need to set them to the global ones
528 + if ( !is_array($local_event_options) ) {
529 + update_post_meta( $show_id, 'local_event_options', $global_event_options );
530 + }
531 +
532 + $this->wpstream_update_chanel_on_baker( $show_id, $global_event_options );
533 + }
534 + }
535 +
536 + public function wpstream_update_default_channel_settings() {
537 + check_ajax_referer( 'wpstream-settings-nonce', 'security' );
538 +
539 + $options_array= $_POST['option'];
540 + $sanitized_options = array();
541 + foreach( $options_array as $key => $value ) {
542 + $sanitized_options[ sanitize_key( $key ) ] = sanitize_text_field( $value );
543 + }
544 +
545 + if ( $sanitized_options['low_latency'] == 1 || $sanitized_options['adaptive_bitrate'] == 1 ) {
546 + $sanitized_options['encrypt'] = 0;
547 + }
548 +
549 + if ( $sanitized_options['encrypt'] == 1 ) {
550 + $sanitized_options['low_latency'] = 0;
551 + $sanitized_options['adaptive_bitrate'] = 0;
552 + }
553 +
554 + $successful_update = update_option( 'wpstream_user_streaming_global_channel_options', $sanitized_options );
555 +
556 + if ( $successful_update ) {
557 + wp_send_json(
558 + array(
559 + 'success' => true,
560 + )
561 + );
562 + } else {
563 + wp_send_json_error(
564 + array(
565 + 'success' => false,
566 + )
567 + );
568 + }
569 +
570 + wp_die();
571 + }
572 +
573 + public function wpstream_update_settings() {
574 + check_ajax_referer( 'wpstream-settings-nonce', 'security' );
575 +
576 + $option_name = sanitize_key( $_POST['option_name'] );
577 + $option_type = sanitize_key( $_POST['option_type'] );
578 +
579 + switch( $option_type ) {
580 + case 'checkbox':
581 + $option_value = filter_var( $_POST['option_value'], FILTER_VALIDATE_INT );
582 + break;
583 + case 'text':
584 + case 'select':
585 + $option_value = sanitize_text_field( $_POST['option_value'] );
586 + break;
587 + case 'multiple-select':
588 + if ( !is_array( $_POST['option_value'] ) ) {
589 + $option_value = array();
590 + break;
591 + }
592 + $option_value = array_map( 'sanitize_text_field', $_POST['option_value'] );
593 + break;
594 + default:
595 + $option_value = sanitize_text_field( $_POST['option_value'] );
596 + }
597 +
598 + $successful_update = update_option( 'wpstream_' . $option_name, $option_value );
599 +
600 + if( $successful_update ) {
601 + wp_send_json(
602 + array(
603 + 'success' => true,
604 + )
605 + );
606 + } else {
607 + wp_send_json_error(
608 + array(
609 + 'success' => false,
610 + )
611 + );
612 + }
613 +
614 + wp_die();
615 + }
616 +
354 617 /**
355 618 * Update channel settings on baker
356 619 *
357 620 * @since 4.2
@@ -362,8 +625,10 @@
362 625 public function wpstream_update_chanel_on_baker($channel_id,$to_save_option){
363 626
364 627 $access_token = $this->wpstream_get_token();
365 628 if($access_token==''){
629 + // cleanup any previous echo before sending json
630 + ob_end_clean();
366 631 echo json_encode( array(
367 632 'is_record' => '',
368 633 'conected' => false,
369 634 'event_data' => '',
@@ -392,12 +657,9 @@
392 657
393 658 $corsorigin='*';
394 659 if( isset($local_event_options['domain_lock']) && intval( $local_event_options['domain_lock']) ==0 ){
395 660 $corsorigin='*';
396 - }
397 -
398 -
399 - if($corsorigin!='*'){
661 + } else{
400 662 $corsorigin=$domain_scheme.'://'.$domain['host'];
401 663 }
402 664
403 665
@@ -408,8 +670,25 @@
408 670 if($to_save_option['record']){
409 671 $to_record="true";
410 672 }
411 673
674 + if( $to_save_option['low_latency'] == 1 || $to_save_option['adaptive_bitrate'] ==1 ){
675 + $to_save_option['encrypt']=0;
676 + }
677 +
678 + if( $to_save_option['encrypt']==1){
679 + $to_save_option['low_latency'] = 0;
680 + $to_save_option['adaptive_bitrate'] =0;
681 + }
682 +
683 +
684 + $abr='none';
685 + if($to_save_option['adaptive_bitrate'] ==1 ){
686 + $abr='common';
687 + }
688 +
689 +
690 +
412 691 $curl_post_fields=array(
413 692 'access_token' => $access_token,
414 693 'channel_id' => $channel_id,
415 694 'domain' => $domain['host'],
@@ -416,14 +695,18 @@
416 695 'allow_access_from' => $corsorigin,
417 696 'record' => $to_record,
418 697 'encrypt' => boolval($to_save_option['encrypt']),
419 698 'autostart' => boolval($to_save_option['autostart']),
699 + 'low_latency' => boolval($to_save_option['low_latency']),
700 + 'abr' => $abr,
420 701 'hls_keys_url_prefix' => get_site_url().'?wpstream_livedrm=',
421 - 'allow_key_access_from' => $domain_ip,
702 + 'allow_key_access_from' => $domain_ip,
703 + 'to_save_option' => $to_save_option,
422 704 );
423 705
424 - print_r($curl_post_fields);
425 - $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields);
706 +
707 +
708 + $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields,true);
426 709 print_r($curl_response);
427 710 exit();
428 711 }
429 712
@@ -441,8 +724,10 @@
441 724 public function wpstream_turn_of_channel(){
442 725
443 726 $access_token = $this->wpstream_get_token();
444 727 if($access_token==''){
728 + // cleanup any previous echo before sending json
729 + ob_end_clean();
445 730 echo json_encode( array(
446 731 'is_record' => '',
447 732 'conected' => false,
448 733 'event_data' => '',
@@ -472,19 +757,23 @@
472 757 );
473 758
474 759
475 760
476 - $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields);
761 + $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields,true);
477 762 $curl_response_decoded = json_decode($curl_response,JSON_OBJECT_AS_ARRAY);
478 763
479 764
480 765
481 766 if( isset($curl_response_decoded['success']) && $curl_response_decoded['success']===true ){
767 + // cleanup any previous echo before sending json
768 + ob_end_clean();
482 769 echo json_encode( array(
483 770 'conected' => true,
484 771 'answer' => $curl_response_decoded,
485 772 ));
486 773 }else{
774 + // cleanup any previous echo before sending json
775 + ob_end_clean();
487 776 echo json_encode( array(
488 777 'conected' => false,
489 778 'error' => esc_html__('Channel is already turned off or does not exist!','wpstream'),
490 779 'answer' => $curl_response_decoded,
@@ -510,8 +799,10 @@
510 799 public function wpstream_give_me_live_uri(){
511 800
512 801 $access_token = $this->wpstream_get_token();
513 802 if($access_token==''){
803 + // cleanup any previous echo before sending json
804 + ob_end_clean();
514 805 echo json_encode( array(
515 806 'is_record' => '',
516 807 'conected' => false,
517 808 'event_data' => '',
@@ -530,8 +821,9 @@
530 821 }
531 822
532 823
533 824 $channel_id = intval($_POST['show_id']);
825 + $basic_streaming = filter_var($_POST['basic_streaming'], FILTER_VALIDATE_BOOLEAN);
534 826 $on_boarding = '';
535 827 if(isset($_POST['start_onboarding'])){
536 828 $on_boarding = sanitize_text_field($_POST['start_onboarding']);
537 829 }
@@ -536,44 +828,79 @@
536 828 $on_boarding = sanitize_text_field($_POST['start_onboarding']);
537 829 }
538 830
539 831
540 - $local_event_options = get_post_meta($channel_id,'local_event_options',true);
541 - if(!is_array($local_event_options)){
542 - $local_event_options = get_option('wpstream_user_streaming_global_channel_options') ;
543 - }
832 + $local_event_options = get_post_meta($channel_id,'local_event_options',true);
833 + $use_global_event_options = get_post_meta($channel_id,'use_global_event_options',true);
834 + $is_local_event_options_enabled = ( is_array( $local_event_options ) && empty( $use_global_event_options ) ) ||
835 + ( !empty($use_global_event_options) && intval( $use_global_event_options ) === 0 );
836 + if( !$is_local_event_options_enabled ) {
837 + $local_event_options = get_option('wpstream_user_streaming_global_channel_options') ;
838 + }
544 839
840 + $is_autostart="false";
841 + $is_encrypt="false";
842 + $low_latency="false";
843 + $adaptive_bitrate="false";
844 + $is_record="false";
845 + $corsorigin='*';
545 846
546 - // set encrypt option
547 - $is_autostart="false";
548 - if( intval( $local_event_options['autostart']) ==1 ){
549 - $is_autostart="true";
550 - }
847 + if ( is_array($local_event_options) ) {
848 + if ( isset($local_event_options['autostart']) &&
849 + intval( $local_event_options['autostart']) == 1 ) {
850 + $is_autostart = "true";
851 + }
551 852
853 + if ( isset($local_event_options['encrypt']) &&
854 + intval( $local_event_options['encrypt']) == 1 ) {
855 + $is_encrypt = "true";
856 + }
552 857
553 - // set encrypt option
554 - $is_encrypt="false";
555 - if( intval( $local_event_options['encrypt']) ==1 ){
556 - $is_encrypt="true";
557 - }
858 + if ( isset($local_event_options['low_latency']) &&
859 + intval( $local_event_options['low_latency']) == 1 ) {
860 + $low_latency = "true";
861 + }
558 862
559 - // set record option
560 - $is_record="false";
561 - if( intval( $local_event_options['record']) ==1 ){
562 - $is_record="true";
563 - }
863 + if ( isset($local_event_options['adaptive_bitrate']) &&
864 + intval( $local_event_options['adaptive_bitrate']) == 1 ) {
865 + $adaptive_bitrate = "true";
866 + }
564 867
565 - $corsorigin='';
566 - if( isset($local_event_options['domain_lock']) && intval( $local_event_options['domain_lock']) ==0 ){
567 - $corsorigin='*';
568 - }
569 -
868 + if ( isset($local_event_options['record']) &&
869 + intval( $local_event_options['record']) == 1 ) {
870 + $is_record = "true";
871 + }
872 +
873 + if ( !isset($local_event_options['domain_lock']) || intval( $local_event_options['domain_lock']) == 0 ) {
874 + $corsorigin = '*';
875 + }
876 + }
877 +
878 + if( $adaptive_bitrate=="true" || $low_latency=="true" ) {
879 + $is_encrypt="false";
880 + }
881 +
882 + if( $is_encrypt=="true" ) {
883 + $adaptive_bitrate="false";
884 + $low_latency="false";
885 + }
886 +
887 + $event_data = $this->wpstream_request_live_stream_uri(
888 + $channel_id,
889 + $is_autostart,
890 + $is_record,
891 + $is_encrypt,
892 + $low_latency,
893 + $adaptive_bitrate,
894 + $userID,
895 + $corsorigin,
896 + $on_boarding,
897 + $basic_streaming
898 + );
570 899
571 - $event_data = $this->wpstream_request_live_stream_uri($channel_id,$is_autostart,$is_record,$is_encrypt,$userID,$corsorigin,$on_boarding);
572 -
573 -
574 -
575 900 if( isset($event_data['success']) && $event_data['success']===true ){
901 + // cleanup any previous echo before sending json
902 + ob_end_clean();
576 903 echo json_encode( array(
577 904 'is_record' => $is_record,
578 905 'conected' => true,
579 906 'event_data' => $event_data,
@@ -590,8 +917,10 @@
590 917
591 918 }
592 919
593 920 }
921 + // cleanup any previous echo before sending json
922 + ob_end_clean();
594 923 echo json_encode( array(
595 924 'is_record' => $is_record,
596 925 'conected' => false,
597 926 'event_data' => $event_data,
@@ -602,9 +931,20 @@
602 931 }
603 932 die();
604 933 }
605 934
935 + public function wpstream_is_streamify_user() {
936 + return false;
937 +// global $wpstream_plugin;
938 +// $pack_details = $wpstream_plugin->main->quota_manager->get_live_quota_data( 'wpstream_is_streamify_user' );
939 +//
940 +// if ( isset( $pack_details['available_data'] ) && $pack_details['available_data'] <= 0 ) {
941 +// return true;
942 +// }
943 +// return false;
944 + }
606 945
946 +
607 947 /**
608 948 *
609 949 * Edited 4.0
610 950 *
@@ -614,10 +954,21 @@
614 954 * returns live url
615 955 */
616 956
617 957
618 - public function wpstream_request_live_stream_uri($schannel_id,$is_autostart,$is_record,$is_encrypt,$request_by_userid,$corsorigin,$on_boarding){
619 -
958 + public function wpstream_request_live_stream_uri(
959 + $schannel_id,
960 + $is_autostart,
961 + $is_record,
962 + $is_encrypt,
963 + $low_latency,
964 + $adaptive_bitrate,
965 + $request_by_userid,
966 + $corsorigin,
967 + $on_boarding,
968 + $basic_streaming
969 + ) {
970 +
620 971 $domain = parse_url ( get_site_url() );
621 972 $domain_scheme = 'http';
622 973 if(is_ssl()){
623 974 $domain_scheme='https';
@@ -631,10 +982,21 @@
631 982 if($corsorigin!='*'){
632 983 $corsorigin=$domain_scheme.'://'.$domain['host'];
633 984 }
634 985
986 + $abr='none';
987 + if($adaptive_bitrate=="true"){
988 + $abr='common';
989 + }
635 990
636 -
991 + if($low_latency=="true"){
992 + $low_latency=true;
993 + }else{
994 + $low_latency = false;
995 + }
996 +
997 + $basic_streaming = $basic_streaming ? 'true' : 'false';
998 +
637 999 $url = 'channel/start';
638 1000 $access_token = $this->wpstream_get_token();
639 1001
640 1002 $metadata_array=array(
@@ -643,9 +1005,12 @@
643 1005
644 1006 if($on_boarding!=''){
645 1007 $metadata_array['on_boarding']='yes';
646 1008 }
647 -
1009 + $permalink = get_permalink($schannel_id);
1010 + if ($permalink !== false) {
1011 + $metadata_array['permalink'] = $permalink;
1012 + }
648 1013
649 1014 $curl_post_fields=array(
650 1015 'access_token' => $access_token,
651 1016 'channel_id' => $schannel_id,
@@ -650,24 +1015,24 @@
650 1015 'access_token' => $access_token,
651 1016 'channel_id' => $schannel_id,
652 1017 'domain' => $domain['host'],
653 1018 'allow_access_from' => $corsorigin,
654 - 'record' => $is_record,
655 - 'encrypt' => $is_encrypt,
656 - 'low_latency' => false,
657 - 'abr' => 'none',
1019 + 'record' => $basic_streaming ? $is_record : 'false',
1020 + 'encrypt' => $basic_streaming ? $is_encrypt : 'false',
1021 + 'low_latency' => $basic_streaming ? $low_latency : 'false',
1022 + 'abr' => $basic_streaming ? $abr : 'none',
658 1023 'hls_keys_url_prefix' => get_site_url().'?wpstream_livedrm=',
659 1024 'allow_key_access_from' => $domain_ip,
660 1025 'metadata' => json_encode($metadata_array),
661 - 'autostart' => $is_autostart,
1026 + 'autostart' => $basic_streaming ? $is_autostart : 'false',
1027 + 'basic_streaming' => $basic_streaming,
662 1028 // 'fakeError' => 'init'
663 1029 );
664 1030
665 1031
666 -
667 - $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields);
1032 + $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields,true);
668 1033 $curl_response_decoded = json_decode($curl_response,JSON_OBJECT_AS_ARRAY);
669 -
1034 + // $curl_response_decoded['curl_post_fields']=$curl_post_fields;
670 1035 return $curl_response_decoded;
671 1036
672 1037
673 1038
@@ -690,31 +1055,23 @@
690 1055 * returns token
691 1056 */
692 1057 public function wpstream_get_token(){
693 1058 $token = get_transient('wpstream_token_api');
694 - if ( false === $token || $token==='' ) {
1059 + if ( false === $token || $token === '' || $token=== NULL ) {
695 1060 $token = $this->wpstream_club_get_token();
696 - set_transient( 'wpstream_token_api', $token ,3500);
1061 + if ($token !== false){
1062 + set_transient( 'wpstream_token_api', $token ,3500);
1063 + }
1064 + else {
1065 + // cache the failed response for a second, otherwise it'll make a shitload of requests
1066 + set_transient( 'wpstream_token_api', 'failed' , 1);
1067 + }
697 1068 }
698 -
699 - return $token;
700 -
1069 + $ret = $token === 'failed' ? false : $token;
1070 + return $ret;
701 1071 }
702 1072
703 -
704 - public function wpstream_get_token_30(){
705 - $token = get_transient('wpstream_token_request_30');
706 - if ( false === $token || $token==='' ) {
707 - $token = $this->wpstream_club_get_token_30();
708 - set_transient( 'wpstream_token_request_30', $token ,600);
709 - }
710 -
711 - return $token;
712 -
713 - }
714 -
715 -
716 - /**
1073 + /**
717 1074 * Edited 4.0
718 1075 *
719 1076 * Request auth token from wpstream.net
720 1077 *
@@ -721,13 +1078,11 @@
721 1078 * @since 3.0.1
722 1079 * returns token fron wpstream
723 1080 */
724 1081 protected function wpstream_club_get_token(){
1082 + $username = get_option('wpstream_api_username','');
1083 + $password = get_option('wpstream_api_password','');
725 1084
726 -
727 - $username = esc_html ( get_option('wpstream_api_username','') );
728 - $password = esc_html ( get_option('wpstream_api_password','') );
729 -
730 1085 if ( $username=='' || $password==''){
731 1086 return;
732 1087 }
733 1088
@@ -734,14 +1089,14 @@
734 1089 $url='access_token';
735 1090 $curl_post_fields=array(
736 1091 'grant_type' => 'password',
737 1092 'username' => $username,
738 - 'password' => $password
1093 + 'password' => $password
739 1094 );
740 1095
741 1096
742 1097
743 - $curl_response=$this->wpstream_baker_do_curl_base($url,$curl_post_fields);
1098 + $curl_response=$this->wpstream_baker_do_curl_base($url,$curl_post_fields, true);
744 1099
745 1100 $response= json_decode($curl_response);
746 1101
747 1102 if( isset($response->access_token) && $response->access_token!='' ){
@@ -758,74 +1113,8 @@
758 1113 * Return token for api version 3.0
759 1114 *
760 1115 */
761 1116
762 -
763 -
764 -
765 - protected function wpstream_club_get_token_30(){
766 -
767 - $client_id = esc_html ( get_option('wpstream_api_key','') );
768 - $client_secret = esc_html ( get_option('wpstream_api_secret_key','') );
769 - $username = esc_html ( get_option('wpstream_api_username','') );
770 - $password = esc_html ( get_option('wpstream_api_password','') );
771 -
772 - if ( $username=='' || $password==''){
773 - return;
774 - }
775 -
776 -
777 -
778 -
779 -
780 - $curl = curl_init();
781 -
782 - $json = array(
783 - 'grant_type'=>'password',
784 - 'username' =>$username,
785 - 'password' =>$password,
786 - 'client_id'=>'qxZ6fCoOMj4cNK8SXRHa5nug6vnswlFWSF37hsW3',
787 - 'client_secret'=>'L1fzLosJf9TlwnCCTZ5pkKmdqqkHShKEi0d4oFNE'
788 - );
789 -
790 - curl_setopt_array($curl, array(
791 - CURLOPT_URL => WPSTREAM_CLUBLINKSSL."://www.".WPSTREAM_CLUBLINK."/?oauth=token",
792 - CURLOPT_RETURNTRANSFER => true,
793 - CURLOPT_ENCODING => "",
794 - CURLOPT_MAXREDIRS => 10,
795 - CURLOPT_TIMEOUT => 15,
796 - CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
797 - CURLOPT_CUSTOMREQUEST => "POST",
798 - CURLOPT_POSTFIELDS=> json_encode($json),
799 - CURLOPT_HTTPHEADER => array(
800 - "cache-control: no-cache",
801 - "content-type: application/json",
802 - ),
803 - ));
804 -
805 - $response = curl_exec($curl);
806 -
807 - if(!$response){
808 - //print '<div class="api_not_conected" style="margin:15px;">We could not connect to WpStream.net. Make sure you have the php Curl library enabled and your hosting allows outgoing HTTP Connection. </div>';
809 - // echo '<div class="api_not_conected">'.__(' 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>';
810 - }
811 -
812 - $err = curl_error($curl);
813 -
814 -
815 -
816 -
817 -
818 - curl_close($curl);
819 - $response= json_decode($response);
820 -
821 - if(isset($response->access_token)){
822 - return $response->access_token;
823 - }else{
824 - return;
825 - }
826 - }
827 -
828 1117
829 1118
830 1119 /**
831 1120 * edited 4.0
@@ -834,38 +1123,50 @@
834 1123 * @since 3.0.1
835 1124 * returns pack data
836 1125 */
837 1126
838 - public function wpstream_request_pack_data_per_user(){
1127 + public function wpstream_request_pack_data_per_user($context = ''){
1128 + $url = 'user/quota';
1129 + $access_token = $this->wpstream_get_token();
839 1130
840 - $event_data_for_transient = get_transient( 'wpstream_request_pack_data_per_user_transient' );
841 -
842 - if($event_data_for_transient===false){
843 - $url = 'user/quota';
844 - $access_token = $this->wpstream_get_token();
845 - $curl_post_fields=array(
846 - 'access_token'=>$access_token
847 - );
1131 + // do not make the call if no token is available
1132 + if (!$access_token) return false;
848 1133
849 - $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields);
850 - $curl_response_decoded = json_decode($curl_response,JSON_OBJECT_AS_ARRAY);
1134 + $curl_post_fields=array(
1135 + 'access_token'=>$access_token,
1136 + 'context' => $context,
1137 + 'plugin_version' => WPSTREAM_PLUGIN_VERSION
1138 + );
851 1139
852 - if( isset($curl_response_decoded['success']) && $curl_response_decoded['success']===true ){
853 - set_transient( 'wpstream_request_pack_data_per_user_transient', $curl_response_decoded, 15 );
854 -
855 - update_option('wpstream_api_username_from_token',$curl_response_decoded['username']);
856 - return $curl_response_decoded;
857 - }else{
858 - return false;
859 - }
860 -
861 - }else{
862 - return $event_data_for_transient;
863 - }
1140 + $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields,true, false, WPSTREAM_TIMEOUT_CONST);
1141 + $curl_response_decoded = json_decode($curl_response,JSON_OBJECT_AS_ARRAY);
864 1142
1143 + if( isset($curl_response_decoded['success']) && $curl_response_decoded['success']===true ){
1144 + set_transient( 'wpstream_request_pack_data_per_user_transient', $curl_response_decoded, 60 );
1145 +
1146 + update_option('wpstream_api_username_from_token',$curl_response_decoded['username']);
1147 + return $curl_response_decoded;
1148 + } else {
1149 + return false;
1150 + }
1151 +
865 1152 }
866 1153
867 1154
1155 + public function wpstream_check_user_quota() {
1156 + $pack_data = $this->wpstream_request_pack_data_per_user('wpstream_check_user_quota');
1157 + if ( ! $pack_data || ! isset( $pack_data['success'] ) || ! $pack_data['success'] ) {
1158 + print json_encode(
1159 + array(
1160 + 'success' => false,
1161 + 'error' => esc_html__('Couldn\'t get user quota.', 'wpstream'),
1162 + )
1163 + );
1164 + } else {
1165 + print json_encode($pack_data);
1166 + }
1167 + die();
1168 + }
868 1169
869 1170
870 1171 /**
871 1172 * Edited 4.0
@@ -892,16 +1193,21 @@
892 1193 *
893 1194 * @since 3.0.1
894 1195 * returns true or false
895 1196 */
896 -
897 - public function wpstream_get_live_event_for_user(){
1197 + public function wpstream_get_live_event_for_user($with_exit='yes'){
898 1198 $current_user = wp_get_current_user();
899 1199 $userID = $current_user->ID;
900 1200
901 1201 global $wpstream_plugin;
902 1202 if( !$wpstream_plugin->main->wpstream_check_user_can_stream() ){
903 - exit('You are not allowed to start a live stream !');
1203 + if($with_exit=='yes'){
1204 + // esc_html_e ('You are not allowed to start a live stream !','wpstream');
1205 + return;
1206 + }else{
1207 + return;
1208 + }
1209 +
904 1210 }
905 1211
906 1212
907 1213 $event_data = $this->wpstream_request_live_stream_for_user($userID);
@@ -916,8 +1222,9 @@
916 1222
917 1223
918 1224
919 1225
1226 +
920 1227
921 1228
922 1229
923 1230
@@ -934,24 +1241,25 @@
934 1241 */
935 1242 public function wpstream_request_live_stream_for_user($user_id){
936 1243
937 1244 global $wpstream_plugin;
938 - if( !$wpstream_plugin->main->wpstream_check_user_can_stream() ){
939 - exit('You are not allowed to stream. Code 741.');
940 - }
1245 +
941 1246
942 -
943 1247 $domain = parse_url ( get_site_url() );
944 1248
945 1249
946 1250 $url = 'channel/list';
947 1251 $access_token = $this->wpstream_get_token();
1252 +
1253 + // do not make the call if no token is available
1254 + if (!$access_token) return false;
1255 +
948 1256 $curl_post_fields=array(
949 1257 'access_token' => $access_token,
950 1258 'domain' => $domain['host'],
951 1259 'status' => 'active'
952 1260 );
953 - $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields);
1261 + $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields,true);
954 1262 $curl_response_decoded = json_decode($curl_response,JSON_OBJECT_AS_ARRAY);
955 1263
956 1264
957 1265
@@ -976,9 +1284,9 @@
976 1284 global $wpstream_plugin;
977 1285 $return_array=array();
978 1286
979 1287 $result = get_transient('wpstream_live_stream_for_user_for_shortcode');
980 - $result==false;
1288 +
981 1289 if($result===false){
982 1290 $result = $this->wpstream_request_live_stream_for_user('');
983 1291 set_transient('wpstream_live_stream_for_user_for_shortcode',$result,30);
984 1292 }
@@ -1019,12 +1327,19 @@
1019 1327
1020 1328
1021 1329 $url = 'video/upload';
1022 1330 $access_token = $this->wpstream_get_token();
1331 +
1332 + // do not make the call if no token is available
1333 + if (!$access_token) return array(
1334 + 'success' => false,
1335 + 'error' => 'not_connected',
1336 + );
1337 +
1023 1338 $curl_post_fields=array(
1024 1339 'access_token' => $access_token,
1025 1340 );
1026 - $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields);
1341 + $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields,true);
1027 1342 $curl_response_decoded = json_decode($curl_response,JSON_OBJECT_AS_ARRAY);
1028 1343
1029 1344
1030 1345 return $curl_response_decoded;
@@ -1047,21 +1362,24 @@
1047 1362 *
1048 1363 */
1049 1364 public function wpstream_get_videos(){
1050 1365 if( !current_user_can('administrator') ){
1051 - exit('not admin on wpstream_get_videos');
1366 + return;
1052 1367 }
1368 +
1369 +
1053 1370 $video_options = array();
1054 1371 $video_array = $this->wpstream_get_videos_from_api();
1055 -
1056 - $video_list_raw_array = $video_array['items'];
1372 + $video_list_raw_array = false;
1057 1373
1058 - $keys = array_column($video_list_raw_array, 'time');
1059 - array_multisort($keys, SORT_DESC , $video_list_raw_array);
1374 + if ( is_array($video_array) && isset($video_array['items']) && is_array($video_array['items']) ) {
1375 + $video_list_raw_array = $video_array['items'];
1376 + }
1060 1377
1061 -
1062 -
1063 - if(is_array($video_list_raw_array)){
1378 + if(is_array($video_list_raw_array)){
1379 + $keys = array_column($video_list_raw_array, 'time');
1380 + array_multisort($keys, SORT_DESC , $video_list_raw_array);
1381 +
1064 1382 foreach ($video_list_raw_array as $key => $videos){
1065 1383 if($videos['name']!=''):
1066 1384 $video_options[$videos['name']]=$videos['name'];
1067 1385 endif;
@@ -1068,9 +1386,8 @@
1068 1386 }
1069 1387
1070 1388 }
1071 1389 return $video_options;
1072 -
1073 1390 }
1074 1391
1075 1392
1076 1393
@@ -1082,22 +1399,27 @@
1082 1399 * returns aws data
1083 1400 *
1084 1401 */
1085 1402 public function wpstream_get_videos_from_api( ){
1086 -
1403 +
1087 1404 if( !current_user_can('administrator') ){
1088 1405 exit('not admin on wpstream_get_videos_from_api');
1089 1406 }
1090 1407
1408 +
1091 1409
1092 1410 $url = 'video/list';
1093 1411 $access_token = $this->wpstream_get_token();
1412 +
1413 + // do not make the call if no token is available
1414 + if (!$access_token) return false;
1415 +
1094 1416 $curl_post_fields=array(
1095 1417 'access_token' => $access_token,
1096 1418 );
1097 1419
1098 1420 $current_page= get_current_screen();
1099 - $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields);
1421 + $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields,true);
1100 1422 $curl_response_decoded = json_decode($curl_response,JSON_OBJECT_AS_ARRAY);
1101 1423
1102 1424
1103 1425
@@ -1128,8 +1450,12 @@
1128 1450 $video_name = sanitize_text_field($_POST['video_name']);
1129 1451
1130 1452 $url = 'video/download';
1131 1453 $access_token = $this->wpstream_get_token();
1454 +
1455 + // do not make the call if no token is available
1456 + if (!$access_token) return false;
1457 +
1132 1458 $curl_post_fields=array(
1133 1459 'access_token' => $access_token,
1134 1460 'name' => $video_name,
1135 1461 );
@@ -1157,13 +1483,17 @@
1157 1483
1158 1484
1159 1485 $url = 'video/delete';
1160 1486 $access_token = $this->wpstream_get_token();
1487 +
1488 + // do not make the call if no token is available
1489 + if (!$access_token) return false;
1490 +
1161 1491 $curl_post_fields=array(
1162 1492 'access_token' => $access_token,
1163 1493 'name'=>$video_name,
1164 1494 );
1165 - $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields);
1495 + $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields,true);
1166 1496 $curl_response_decoded = json_decode($curl_response,JSON_OBJECT_AS_ARRAY);
1167 1497
1168 1498
1169 1499 print $curl_response;
@@ -1173,12 +1503,15 @@
1173 1503
1174 1504 }
1175 1505
1176 1506
1177 -
1178 -
1179 -
1507 + public function wpstream_check_pending_videos() {
1508 + if (!current_user_can('administrator')) {
1509 + wp_send_json_error(__('Unauthorized', 'wpstream'));
1510 + }
1180 1511
1181 -
1182 -
1183 -
1184 -}// end class
1512 + $videos_list_raw = $this->wpstream_get_videos_from_api();
1513 + wp_send_json_success($videos_list_raw);
1514 + }
1515 +
1516 +
1517 +}// end class