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

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