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

1,172 lines 32.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class Wpstream_Live_Api_Connection {
4
5
6
7
8 public function __construct() {
9 add_action( 'wp_ajax_wpstream_give_me_live_uri', array($this,'wpstream_give_me_live_uri') );
10 add_action( 'wp_ajax_wpstream_turn_of_channel', array($this,'wpstream_turn_of_channel') );
11 add_action( 'wp_ajax_wpstream_update_local_event_settings',array($this,'wpstream_update_local_event_settings'));
12
13 add_action( 'wp_ajax_wpstream_check_dns_sync', array($this,'wpstream_check_dns_sync') );
14 add_action( 'wp_ajax_wpstream_check_event_status', array($this,'wpstream_check_event_status') );
15
16 add_action( 'wp_ajax_wpstream_close_event', array($this,'wpstream_close_event') );
17 add_action( 'wp_ajax_wpstream_get_download_link', array($this,'wpstream_get_download_link') );
18 add_action( 'wp_ajax_wpstream_get_delete_file', array($this,'wpstream_get_delete_file') );
19
20 add_action( 'admin_notices',array($this, 'wpstream_admin_notices') );
21
22
23 }
24
25
26
27
28
29
30 /*
31 * Admin Notices
32 *
33 *
34 *
35 * */
36 function wpstream_admin_notices(){
37 global $pagenow;
38
39
40
41
42 if($pagenow!='admin.php'){
43 return;
44 }
45
46 $permited_pages=array('wpstream_plugin_options','wpstream_live_channels','wpstream_recordings','wpstream_settings');
47 if (!empty($_GET['page'])) {
48 $page = esc_html($_GET['page']) ;
49 if( !in_array($page, $permited_pages)){
50 return;
51 }
52 }
53
54 if(in_array('curl', get_loaded_extensions())){
55 //cURL module has been loaded
56 } else{
57 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>';
58 }
59
60 $token = $this->wpstream_get_token();
61 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>';
63 }
64
65 }
66
67
68 /*
69 * Curl request
70 *
71 *
72 *
73 * */
74
75 function wpstream_baker_do_curl_base($url,$curl_post_fields){
76
77
78 $curl = curl_init();
79 $api_url = WPSTREAM_API.'/'.$url;
80
81 curl_setopt_array($curl, array(
82 CURLOPT_URL =>$api_url,
83 CURLOPT_RETURNTRANSFER => true,
84 CURLOPT_ENCODING => "",
85 CURLOPT_MAXREDIRS => 10,
86 CURLOPT_TIMEOUT => 10,
87 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
88 CURLOPT_CUSTOMREQUEST => "POST",
89 CURLOPT_POSTFIELDS => http_build_query($curl_post_fields),
90 CURLOPT_HTTPHEADER => array(
91 "cache-control: no-cache",
92 "content-type: application/x-www-form-urlencoded"
93 ),
94 ));
95
96 $response = curl_exec($curl);
97 $err = curl_error($curl);
98
99 curl_close($curl);
100
101 if ($err) {
102 echo "cURL Error #:" . $err;
103 }
104 return $response;
105
106 }
107
108
109
110
111
112
113
114 /**
115 * retreive server id based on show id
116 *
117 * @since 3.0.1
118 * returns live url
119 */
120
121 function retrive_server_id_based_on_show_id($show_id){
122
123 $transient_name = 'server_id_to_return_'.$show_id;
124 $server_id_to_return = get_transient( $transient_name );
125
126 if ( false === $server_id_to_return ) {
127 $token = $this->wpstream_get_token();
128 $values_array=array(
129 "show_id" => intval($show_id),
130 );
131 $values_array=array();
132 $show_id=intval($show_id);
133
134 $url="https://rest-baker.wpstream.net/?&apiFunctionName=server_id_by_show_id&show_id=".$show_id."&access_token=".$token;
135 $arguments = array(
136 'method' => 'GET',
137 'timeout' => 45,
138 'redirection' => 5,
139 'httpversion' => '1.0',
140 'blocking' => true,
141 'headers' => array(),
142 'body' => $values_array,
143 'cookies' => array()
144 );
145
146 $response = wp_remote_post($url,$arguments);
147
148 $received_data = wp_remote_retrieve_body($response);
149
150 $received_data_decoded=json_decode($received_data);
151
152
153 if( isset($response['response']['code']) && $response['response']['code']=='200' && $received_data_decoded->success===true && $received_data_decoded->result!=''){
154 $server_id_to_return = $received_data_decoded->result;
155 set_transient( $transient_name, json_decode($server_id_to_return), 60 );
156 return $server_id_to_return;
157
158 }else{
159 return '';
160
161 }
162 }else{
163 return $server_id_to_return;
164 }
165
166 die();
167 }
168
169
170
171 /**
172 * edited 4.0
173 *
174 *
175 *
176 * check event status from start stremaing process
177 *
178 * @since 3.0.1
179 * returns live url
180 */
181
182 public function wpstream_check_event_status(){
183 $channel_id = intval($_POST['channel_id']);
184
185 $response = $this->wpstream_check_event_status_api_call($channel_id);
186
187
188
189 if( isset($response['success']) && $response['success']){
190 $this->api20_wpstream_update_event($response,$channel_id);
191
192 if( isset($response['broadcast_url']) && isset($response['status']) && $response['status']==='active' ){
193
194
195 $response['live_data_url'] = $response['qos_url'];
196
197 /* obsolote due to new url format
198 $local_event_options = get_post_meta ($channel_id,'local_event_options',true);
199 if( is_array( $local_event_options ) && intval( $local_event_options['autostart']) ==1 ){
200 $to_split=explode('/',$response['broadcast_url']);
201 $obs_stream = array_pop($to_split);;
202 $obs_uri = str_replace($obs_stream,'',$response['broadcast_url']);
203 }else{
204 $to_split=explode('wpstream/',$response['broadcast_url']);
205 $obs_uri = $to_split[0].'wpstream/';
206 $obs_stream = $to_split[1];
207 }
208 */
209
210
211 $to_split = explode('/',$response['broadcast_url']);
212 $obs_stream = array_pop($to_split);;
213 $obs_uri = str_replace($obs_stream,'',$response['broadcast_url']);
214
215 $response['obs_uri'] = $obs_uri;
216 $response['obs_stream'] = $obs_stream;
217
218 update_post_meta($channel_id,'obs_uri',$obs_uri);
219 update_post_meta($channel_id,'obs_stream',$obs_stream);
220 update_post_meta($channel_id,'broadcast_url',$response['broadcast_url']);
221
222 }
223
224
225 }
226
227 print json_encode($response);
228 die();
229
230 }
231
232 /**
233 * edited 4.0
234 *
235 * check event status from API
236 *
237 * @since 3.0.1
238 * returns live url
239 */
240
241
242 public function wpstream_check_event_status_api_call($channel_id){
243
244 $access_token = $this->wpstream_get_token();
245 $domain = parse_url ( get_site_url() );
246 $url = 'channel/info';
247
248 $curl_post_fields=array(
249 'access_token' => $access_token,
250 'channel_id' => $channel_id,
251 'domain' => $domain['host'],
252 );
253
254
255
256
257
258 $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields);
259
260
261 $curl_response_decoded = json_decode($curl_response,JSON_OBJECT_AS_ARRAY);
262
263 return $curl_response_decoded;
264
265
266 }
267
268
269
270
271
272
273
274
275
276
277
278
279 public function wpstream_reset_event_data($event_id){
280 update_post_meta($event_id,'stats_url','');
281 update_post_meta($event_id,'hls_playback_url','');
282 update_post_meta($event_id,'server_id', '' );
283 }
284
285 /**
286 * edited 4.0
287 * update event metadata
288 *
289 * @since 3.0.1
290 * returns live url
291 */
292
293
294
295 function api20_wpstream_update_event($response,$channel_id){
296
297 if( is_array($response) ) {
298 $event_data_for_transient = array();
299 $transient_name = 'event_data_to_return_'.$channel_id;
300
301 foreach($response as $key=>$value){
302 update_post_meta($channel_id,$key,$value);
303 $event_data_for_transient[$key]=$value;
304 }
305 set_transient($transient_name,$event_data_for_transient,45);
306 return $event_data_for_transient;
307 }else{
308 return false;
309 }
310
311
312 }
313
314 /**
315 * Update event settings
316 *
317 * @since 3.0.1
318 * returns live url
319 */
320
321
322 public function wpstream_update_local_event_settings(){
323
324 if(!is_user_logged_in()){
325 exit('not logged in');
326 }
327 if( !current_user_can('administrator') ){
328 exit('not admin');
329 }
330
331 $show_id = intval($_POST['show_id']);
332 $option_array = $_POST['option'];
333
334 $to_save_option=array();
335 foreach($option_array as $key=>$value){
336 $to_save_option[sanitize_key($key)]=sanitize_text_field($value);
337 }
338
339
340
341
342 update_post_meta ($show_id,'local_event_options',$to_save_option);
343 print 'do update with';
344 print_r($to_save_option);
345
346 $this->wpstream_update_chanel_on_baker($show_id,$to_save_option);
347
348 }
349
350
351
352 /**
353 * Update channel settings on baker
354 *
355 * @since 4.2
356 * returns live url
357 */
358
359
360 public function wpstream_update_chanel_on_baker($channel_id,$to_save_option){
361
362 $access_token = $this->wpstream_get_token();
363 if($access_token==''){
364 echo json_encode( array(
365 'is_record' => '',
366 'conected' => false,
367 'event_data' => '',
368 'error' => esc_html('You are not connected to wpstream.net! Please check your WpStream credentials!','wpstream'),
369 ));
370 exit();
371 }
372 $current_user = wp_get_current_user();
373 $userID = $current_user->ID;
374
375
376
377 $url = '/channel/update';
378 $local_event_options = get_post_meta($channel_id,'local_event_options',true);
379
380 $domain = parse_url ( get_site_url() );
381 $domain_scheme = 'http';
382 if(is_ssl()){
383 $domain_scheme='https';
384 }
385
386 $domain_ip= esc_html( $_SERVER['SERVER_ADDR'] );
387 if($domain_ip==''){
388 $domain_ip="0.0.0.0/0";
389 }
390
391 $corsorigin='*';
392 if( isset($local_event_options['domain_lock']) && intval( $local_event_options['domain_lock']) ==0 ){
393 $corsorigin='*';
394 }
395
396
397 if($corsorigin!='*'){
398 $corsorigin=$domain_scheme.'://'.$domain['host'];
399 }
400
401
402 $url = 'channel/update';
403
404
405 $to_record="false";
406 if($to_save_option['record']){
407 $to_record="true";
408 }
409
410 $curl_post_fields=array(
411 'access_token' => $access_token,
412 'channel_id' => $channel_id,
413 'domain' => $domain['host'],
414 'allow_access_from' => $corsorigin,
415 'record' => $to_record,
416 'encrypt' => boolval($to_save_option['encrypt']),
417 'autostart' => boolval($to_save_option['autostart']),
418 'hls_keys_url_prefix' => get_site_url().'?wpstream_livedrm=',
419 'allow_key_access_from' => $domain_ip,
420 );
421
422 print_r($curl_post_fields);
423 $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields);
424 print_r($curl_response);
425 exit();
426 }
427
428
429
430
431
432 /**
433 *
434 * added 5.0
435 *
436 * Turn off channel
437 *
438 */
439 public function wpstream_turn_of_channel(){
440
441 $access_token = $this->wpstream_get_token();
442 if($access_token==''){
443 echo json_encode( array(
444 'is_record' => '',
445 'conected' => false,
446 'event_data' => '',
447 'error' => esc_html('You are not connected to wpstream.net! Please check your WpStream credentials!','wpstream'),
448 ));
449 exit();
450 }
451 $current_user = wp_get_current_user();
452 $userID = $current_user->ID;
453
454
455 global $wpstream_plugin;
456 if( !$wpstream_plugin->main->wpstream_check_user_can_stream() ){
457 exit('You are not allowed to stream.Code 407');
458 }
459
460
461 $channel_id = intval($_POST['show_id']);
462
463 $url = 'channel/stop';
464 $domain = parse_url ( get_site_url() );
465 $curl_post_fields=array(
466 'access_token' => $access_token,
467 'channel_id' => $channel_id,
468 'domain' => $domain['host'],
469
470 );
471
472
473
474 $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields);
475 $curl_response_decoded = json_decode($curl_response,JSON_OBJECT_AS_ARRAY);
476
477
478
479 if( isset($curl_response_decoded['success']) && $curl_response_decoded['success']===true ){
480 echo json_encode( array(
481 'conected' => true,
482 'answer' => $curl_response_decoded,
483 ));
484 }else{
485 echo json_encode( array(
486 'conected' => false,
487 'error' => esc_html__('Channel is already turned off or does not exist!','wpstream'),
488 'answer' => $curl_response_decoded,
489 ));
490 }
491 die();
492
493
494
495
496 }
497
498
499 /**
500 *
501 * Edited 4.0
502 *
503 * Request live url
504 *
505 * @since 3.0.1
506 * returns live url
507 */
508 public function wpstream_give_me_live_uri(){
509
510 $access_token = $this->wpstream_get_token();
511 if($access_token==''){
512 echo json_encode( array(
513 'is_record' => '',
514 'conected' => false,
515 'event_data' => '',
516 'error' => esc_html('You are not connected to wpstream.net! Please check your WpStream credentials!','wpstream'),
517 ));
518 exit();
519 }
520
521 $current_user = wp_get_current_user();
522 $userID = $current_user->ID;
523
524
525 global $wpstream_plugin;
526 if( !$wpstream_plugin->main->wpstream_check_user_can_stream() ){
527 exit('You are not allowed to stream.Code 407');
528 }
529
530
531 $channel_id = intval($_POST['show_id']);
532
533 $local_event_options = get_post_meta($channel_id,'local_event_options',true);
534 if(!is_array($local_event_options)){
535 $local_event_options = get_option('wpstream_user_streaming_global_channel_options') ;
536 }
537
538
539 // set encrypt option
540 $is_autostart="false";
541 if( intval( $local_event_options['autostart']) ==1 ){
542 $is_autostart="true";
543 }
544
545
546 // set encrypt option
547 $is_encrypt="false";
548 if( intval( $local_event_options['encrypt']) ==1 ){
549 $is_encrypt="true";
550 }
551
552 // set record option
553 $is_record="false";
554 if( intval( $local_event_options['record']) ==1 ){
555 $is_record="true";
556 }
557
558 $corsorigin='';
559 if( isset($local_event_options['domain_lock']) && intval( $local_event_options['domain_lock']) ==0 ){
560 $corsorigin='*';
561 }
562
563
564 $event_data = $this->wpstream_request_live_stream_uri($channel_id,$is_autostart,$is_record,$is_encrypt,$userID,$corsorigin);
565
566
567
568 if( isset($event_data['success']) && $event_data['success']===true ){
569 echo json_encode( array(
570 'is_record' => $is_record,
571 'conected' => true,
572 'event_data' => $event_data,
573
574 ));
575 }else{
576 $default_error= 'Failed to turn channel ON. Please try again in a few minutes.';
577 if( isset($event_data['error'])){
578 $plumer_error = $event_data['error'];
579 switch ($plumer_error) {
580 case 'NOT_ENOUGH_TRAFFIC':
581 $default_error= 'You do not have enough Streaming Data to turn ON a live channel. Please upgrade your subscription for more resources.' ;
582 break;
583
584 }
585
586 }
587 echo json_encode( array(
588 'is_record' => $is_record,
589 'conected' => false,
590 'event_data' => $event_data,
591 'error' => $default_error,
592
593
594 ));
595 }
596 die();
597 }
598
599
600 /**
601 *
602 * Edited 4.0
603 *
604 * Request live url via api
605 *
606 * @since 3.0.1
607 * returns live url
608 */
609
610
611 public function wpstream_request_live_stream_uri($schannel_id,$is_autostart,$is_record,$is_encrypt,$request_by_userid,$corsorigin){
612
613 $domain = parse_url ( get_site_url() );
614 $domain_scheme = 'http';
615 if(is_ssl()){
616 $domain_scheme='https';
617 }
618
619 $domain_ip= esc_html( $_SERVER['SERVER_ADDR'] );
620 if($domain_ip==''){
621 $domain_ip="0.0.0.0/0";
622 }
623
624 if($corsorigin!='*'){
625 $corsorigin=$domain_scheme.'://'.$domain['host'];
626 }
627
628
629
630 $url = 'channel/start';
631 $access_token = $this->wpstream_get_token();
632
633 $metadata_array=array(
634 'pluginVersion'=>WPSTREAM_PLUGIN_VERSION
635 );
636
637 $curl_post_fields=array(
638 'access_token' => $access_token,
639 'channel_id' => $schannel_id,
640 'domain' => $domain['host'],
641 'allow_access_from' => $corsorigin,
642 'record' => $is_record,
643 'encrypt' => $is_encrypt,
644 'low_latency' => false,
645 'abr' => 'none',
646 'hls_keys_url_prefix' => get_site_url().'?wpstream_livedrm=',
647 'allow_key_access_from' => $domain_ip,
648 'metadata' => json_encode($metadata_array),
649 'autostart' => $is_autostart,
650 // 'fakeError' => 'init'
651 );
652
653
654
655 $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields);
656 $curl_response_decoded = json_decode($curl_response,JSON_OBJECT_AS_ARRAY);
657
658 return $curl_response_decoded;
659
660
661
662 }
663
664
665
666
667
668
669
670
671
672
673
674 /**
675 * Retrive auth token from tranzient
676 *
677 * @since 3.0.1
678 * returns token
679 */
680 public function wpstream_get_token(){
681 $token = get_transient('wpstream_token_api');
682 if ( false === $token || $token==='' ) {
683 $token = $this->wpstream_club_get_token();
684 set_transient( 'wpstream_token_api', $token ,3500);
685 }
686
687 return $token;
688
689 }
690
691
692 public function wpstream_get_token_30(){
693 $token = get_transient('wpstream_token_request_30');
694 if ( false === $token || $token==='' ) {
695 $token = $this->wpstream_club_get_token_30();
696 set_transient( 'wpstream_token_request_30', $token ,600);
697 }
698
699 return $token;
700
701 }
702
703
704 /**
705 * Edited 4.0
706 *
707 * Request auth token from wpstream.net
708 *
709 * @since 3.0.1
710 * returns token fron wpstream
711 */
712 protected function wpstream_club_get_token(){
713
714
715 $username = esc_html ( get_option('wpstream_api_username','') );
716 $password = esc_html ( get_option('wpstream_api_password','') );
717
718 if ( $username=='' || $password==''){
719 return;
720 }
721
722 $url='access_token';
723 $curl_post_fields=array(
724 'grant_type' => 'password',
725 'username' => $username,
726 'password' => $password
727 );
728
729
730
731 $curl_response=$this->wpstream_baker_do_curl_base($url,$curl_post_fields);
732
733 $response= json_decode($curl_response);
734
735 if( isset($response->access_token) && $response->access_token!='' ){
736 return $response->access_token;
737 }else{
738 return false;
739 }
740 }
741
742
743
744 /*
745 *
746 * Return token for api version 3.0
747 *
748 */
749
750
751
752
753 protected function wpstream_club_get_token_30(){
754
755 $client_id = esc_html ( get_option('wpstream_api_key','') );
756 $client_secret = esc_html ( get_option('wpstream_api_secret_key','') );
757 $username = esc_html ( get_option('wpstream_api_username','') );
758 $password = esc_html ( get_option('wpstream_api_password','') );
759
760 if ( $username=='' || $password==''){
761 return;
762 }
763
764
765
766
767
768 $curl = curl_init();
769
770 $json = array(
771 'grant_type'=>'password',
772 'username' =>$username,
773 'password' =>$password,
774 'client_id'=>'qxZ6fCoOMj4cNK8SXRHa5nug6vnswlFWSF37hsW3',
775 'client_secret'=>'L1fzLosJf9TlwnCCTZ5pkKmdqqkHShKEi0d4oFNE'
776 );
777
778 curl_setopt_array($curl, array(
779 CURLOPT_URL => WPSTREAM_CLUBLINKSSL."://www.".WPSTREAM_CLUBLINK."/?oauth=token",
780 CURLOPT_RETURNTRANSFER => true,
781 CURLOPT_ENCODING => "",
782 CURLOPT_MAXREDIRS => 10,
783 CURLOPT_TIMEOUT => 15,
784 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
785 CURLOPT_CUSTOMREQUEST => "POST",
786 CURLOPT_POSTFIELDS=> json_encode($json),
787 CURLOPT_HTTPHEADER => array(
788 "cache-control: no-cache",
789 "content-type: application/json",
790 ),
791 ));
792
793 $response = curl_exec($curl);
794
795 if(!$response){
796 //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>';
797 // 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>';
798 }
799
800 $err = curl_error($curl);
801
802
803
804
805
806 curl_close($curl);
807 $response= json_decode($response);
808
809 if(isset($response->access_token)){
810 return $response->access_token;
811 }else{
812 return;
813 }
814 }
815
816
817
818 /**
819 * edited 4.0
820 * Return admin package data
821 *
822 * @since 3.0.1
823 * returns pack data
824 */
825
826 public function wpstream_request_pack_data_per_user(){
827
828 $event_data_for_transient = get_transient( 'wpstream_request_pack_data_per_user_transient' );
829
830 if($event_data_for_transient===false){
831 $url = 'user/quota';
832 $access_token = $this->wpstream_get_token();
833 $curl_post_fields=array(
834 'access_token'=>$access_token
835 );
836
837 $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields);
838 $curl_response_decoded = json_decode($curl_response,JSON_OBJECT_AS_ARRAY);
839
840 if( isset($curl_response_decoded['success']) && $curl_response_decoded['success']===true ){
841 set_transient( 'wpstream_request_pack_data_per_user_transient', $curl_response_decoded, 15 );
842
843 update_option('wpstream_api_username_from_token',$curl_response_decoded['username']);
844 return $curl_response_decoded;
845 }else{
846 return false;
847 }
848
849 }else{
850 return $event_data_for_transient;
851 }
852
853 }
854
855
856
857
858 /**
859 * Edited 4.0
860 *
861 * Check Api Status
862 *
863 * @since 3.0.1
864 * returns true or false
865 */
866
867 function wpstream_client_check_api_status(){
868 return true;
869 }
870
871
872
873
874
875 /**
876 *
877 * Edited 4.0
878 *
879 * Start Get live events for users
880 *
881 * @since 3.0.1
882 * returns true or false
883 */
884
885 public function wpstream_get_live_event_for_user(){
886 $current_user = wp_get_current_user();
887 $userID = $current_user->ID;
888
889 global $wpstream_plugin;
890 if( !$wpstream_plugin->main->wpstream_check_user_can_stream() ){
891 exit('You are not allowed to start a live stream !');
892 }
893
894
895 $event_data = $this->wpstream_request_live_stream_for_user($userID);
896 $return_event = array();
897 if(is_array($event_data)):
898 foreach ($event_data as $key=>$event){
899 $return_event[$event['channel_id']]=$event;
900 }
901 endif;
902 return $return_event;
903 }
904
905
906
907
908
909
910
911
912
913
914 /**
915 *
916 * Edited 4.0
917 *
918 * Get live events for users
919 *
920 * @since 3.0.1
921 * returns true or false
922 */
923 public function wpstream_request_live_stream_for_user($user_id){
924
925 global $wpstream_plugin;
926 if( !$wpstream_plugin->main->wpstream_check_user_can_stream() ){
927 exit('You are not allowed to stream. Code 741.');
928 }
929
930
931 $domain = parse_url ( get_site_url() );
932
933
934 $url = 'channel/list';
935 $access_token = $this->wpstream_get_token();
936 $curl_post_fields=array(
937 'access_token' => $access_token,
938 'domain' => $domain['host'],
939 'status' => 'active'
940 );
941 $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields);
942 $curl_response_decoded = json_decode($curl_response,JSON_OBJECT_AS_ARRAY);
943
944
945
946
947 if( isset($curl_response_decoded['success']) && $curl_response_decoded['success']==true ){
948 return $curl_response_decoded['channels'];
949 }else{
950 return false;
951 }
952
953
954 }
955
956
957 /**
958 * live events for shortocde - behind tranzisnt
959 * returns an array with local show id for live events
960 * @since 3.0.1
961 * returns noda
962 */
963 public function api20_wpstream_request_live_stream_for_user_for_shortcode($outside=''){
964 global $wpstream_plugin;
965 $return_array=array();
966
967 $result = get_transient('wpstream_live_stream_for_user_for_shortcode');
968 $result==false;
969 if($result===false){
970 $result = $this->wpstream_request_live_stream_for_user('');
971 set_transient('wpstream_live_stream_for_user_for_shortcode',$result,30);
972 }
973
974 if(is_array($result)):
975 foreach($result as $key=>$event){
976 $return_array[]=$event['channel_id'];
977 }
978 endif;
979 return $return_array;
980 }
981
982
983
984
985 /**
986 * Delete event
987 *
988 * @since 3.0.1
989 * returns noda
990 */
991
992 public function wpstream_close_event(){
993 //not implemented yet
994 }
995
996
997 /**
998 * Get signed upload form data
999 *
1000 * @since 3.0.1
1001 * returns aws form
1002 */
1003 public function wpstream_get_signed_form_upload_data(){
1004 if( !current_user_can('administrator') ){
1005 exit('not admin on wpstream_get_signed_form_upload_data');
1006 }
1007
1008
1009 $url = 'video/upload';
1010 $access_token = $this->wpstream_get_token();
1011 $curl_post_fields=array(
1012 'access_token' => $access_token,
1013 );
1014 $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields);
1015 $curl_response_decoded = json_decode($curl_response,JSON_OBJECT_AS_ARRAY);
1016
1017
1018 return $curl_response_decoded;
1019 exit();
1020
1021 }
1022
1023
1024
1025
1026
1027
1028
1029 /**
1030 *
1031 * Get video from storage- clear data for front end use
1032 *
1033 * @since 3.0.1
1034 * returns aws data
1035 *
1036 */
1037 public function wpstream_get_videos(){
1038 if( !current_user_can('administrator') ){
1039 exit('not admin on wpstream_get_videos');
1040 }
1041 $video_options = array();
1042 $video_array = $this->wpstream_get_videos_from_api();
1043
1044 $video_list_raw_array = $video_array['items'];
1045
1046 $keys = array_column($video_list_raw_array, 'time');
1047 array_multisort($keys, SORT_DESC , $video_list_raw_array);
1048
1049
1050
1051 if(is_array($video_list_raw_array)){
1052 foreach ($video_list_raw_array as $key => $videos){
1053 if($videos['name']!=''):
1054 $video_options[$videos['name']]=$videos['name'];
1055 endif;
1056 }
1057
1058 }
1059 return $video_options;
1060
1061 }
1062
1063
1064
1065 /**
1066 *
1067 * Get video from storage- raw data
1068 *
1069 * @since 3.0.1
1070 * returns aws data
1071 *
1072 */
1073 public function wpstream_get_videos_from_api( ){
1074
1075 if( !current_user_can('administrator') ){
1076 exit('not admin on wpstream_get_videos_from_api');
1077 }
1078
1079
1080 $url = 'video/list';
1081 $access_token = $this->wpstream_get_token();
1082 $curl_post_fields=array(
1083 'access_token' => $access_token,
1084 );
1085
1086 $current_page= get_current_screen();
1087 $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields);
1088 $curl_response_decoded = json_decode($curl_response,JSON_OBJECT_AS_ARRAY);
1089
1090
1091
1092
1093 if( isset($curl_response_decoded['success']) && $curl_response_decoded['success']==true ){
1094 return $curl_response_decoded;
1095 }else{
1096 return array();
1097 }
1098
1099 }
1100
1101
1102
1103 /**
1104 * Get download link from aws
1105 *
1106 * @since 3.0.1
1107 * returns aws data
1108 */
1109
1110 function wpstream_get_download_link(){
1111
1112 if( !current_user_can('administrator') ){
1113 exit('not admin on get_download_link');
1114 }
1115
1116 $video_name = sanitize_text_field($_POST['video_name']);
1117
1118 $url = 'video/download';
1119 $access_token = $this->wpstream_get_token();
1120 $curl_post_fields=array(
1121 'access_token' => $access_token,
1122 'name' => $video_name,
1123 );
1124 $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields);
1125 print $curl_response;
1126 exit();
1127
1128
1129
1130 }
1131
1132
1133 /**
1134 * Delete file from storage
1135 *
1136 * @since 3.0.1
1137 *
1138 */
1139 public function wpstream_get_delete_file(){
1140 if( !current_user_can('administrator') ){
1141 exit('not admin on get_delete_file');
1142 }
1143
1144 $video_name = esc_html($_POST['video_name']);
1145
1146
1147 $url = 'video/delete';
1148 $access_token = $this->wpstream_get_token();
1149 $curl_post_fields=array(
1150 'access_token' => $access_token,
1151 'name'=>$video_name,
1152 );
1153 $curl_response = $this->wpstream_baker_do_curl_base($url,$curl_post_fields);
1154 $curl_response_decoded = json_decode($curl_response,JSON_OBJECT_AS_ARRAY);
1155
1156
1157 print $curl_response;
1158
1159 exit();
1160
1161
1162 }
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172 }// end class