# wpstream/2.08/libs/plugin-admin.php

WpStream – Live Streaming, Video on Demand, Pay Per View, version 2.08. 535 lines.

- Page: https://pluginprobe.com/plugins/wpstream/2.08/code/libs/plugin-admin.php
- Raw: https://pluginprobe.com/plugins/wpstream/2.08/raw/libs/plugin-admin.php
- Modified: 2019-05-29T12:07:04+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/wpstream/2.08/code/libs/plugin-admin.php#L10-L20`.

```php
<?php
function wpstream_set_wpstream_credentials(){
    
    if($_SERVER['REQUEST_METHOD'] === 'POST'){	
        $allowed_html   =   array();
        $exclude_array  =   array();
        $allowed_html   =   array();
        
        foreach($_POST as $variable=>$value){	

            if ($variable!='submit'){
                if (!in_array($variable, $exclude_array) ){
                    update_option( sanitize_key('wpstream_'.$variable), wp_kses ($value,$allowed_html) );
                }	
            }	
        }


        update_option('wp_estate_token_expire',0);
        update_option('wp_estate_curent_token',' ');
        
        delete_transient( 'wpstream_token_request');
        
    }
       
    
    $allowed_html   =   array();
    $api_key        =   esc_html( get_option('wpstream_api_key','') );


    $wpstream_options_array =array(
        2   =>  array(
                    'label' =>  'WpStream.net Username or Email',
                    'name'  =>  'api_username',
                    'type'  =>  'text',
                ),
        3   =>  array(
                    'label' =>  'WpStream.net Password',
                    'name'  =>  'api_password',
                    'type'  =>  'password',
                ),
//        0   =>  array(
//                    'label' =>  'API Key',
//                    'name'  =>  'api_key',
//                    'type'  =>  'text',
//                ),
//        1   =>  array(
//                    'label' =>  'API Secret Key',
//                    'name'  =>  'api_secret_key',
//                    'type'  =>  'text',
//                ),

    );


    $token  = wpstream_rcapi_retrive_token();
     
    $pack_details           =   wpstream_get_pack_details_user();

    if( isset($pack_details['band']) && isset( $pack_details['storage']) ){
        print '<div class="pack_details_wrapper"><strong>'.__('Your account information: ','wpstream').'</strong> '.__('You have','wpstream').'<strong> '.wpstream_convert_band($pack_details['band']).' Gb</strong> '.__('available streaming bandwidth and','wpstream').' <strong>'.wpstream_convert_band($pack_details['storage']).' Gb</strong> '.__('available media storage','wpstream').'.</div>';
        print'<input type="hidden" id="wpstream_band" value="'.$pack_details['band'].'">';
        print'<input type="hidden" id="wpstream_storage" value="'.$pack_details['storage'].'">';
        
    }

    
    
    print   '<form method="post" action="" >';
                print '<div  class="theme_options_tab_wpstream" style="display:block;" >
                        <h1>'.__('WpStream Credentials','wpstream').'</h1>';

                        if( wpstream_client_check_api_status() ){
                            echo '<div class="api_conected">'.__('Conected to WpStream.net!','wpstream').'</div>';
                        }else{
                            echo '<div class="api_not_conected">'.__('No Connection to WpStream.net  Use your WpStream credentials to connect  below or go  ','wpstream').'<a href="https://wpstream.net/my-account" target="_blank">here</a>'.__(' to create an account.','wpstream').'</div>';
                        }
                        print '<div class="wpstream_option_wrapper">';
                            foreach ($wpstream_options_array as $key=>$option){
                                print '<div class="wpstream_option">';

                                    $options_value =  esc_html( get_option('wpstream_'.$option['name'],'') );
                                    print '<label for="'.$option['name'].'">'.$option['label'].'</label>';
                                    print '<input id="'.$option['name'].'" type="'.$option['type'].'" size="36"  name="'.$option['name'].'" value="'.esc_html($options_value).'" />';

                                print '</div>';
                            }
                        print '</div>';


                    print '<input type="submit" name="submit"  class="wpstream_button" value="'.__('Save Changes','wpstream').'" />';
               
                       

                print '</div>';
    print   '</form>';
    
    print '<div  class="theme_options_tab_wpstream" style="display:block;" >';
        $link_new = admin_url('admin.php?page=wpstream_new_general_set');
        $link_new_paid = admin_url('post-new.php?post_type=product');
        $link_new_free = admin_url('post-new.php?post_type=wpstream_product');


        print '<a href="'.esc_url($link_new_free).'" class="wpstream_no_chanel_add_channel">'.esc_html__('Create new free channel','wpstream').'</a>';
        print '<a href="'.esc_url($link_new_paid).'" class="wpstream_no_chanel_add_channel">'.esc_html__('Create pay-per-view channel','wpstream').'</a>';
        print '<a href="'.esc_url($link_new).'"      class="wpstream_no_chanel_add_channel">'.esc_html('My Channels','wpstream').'</a>';        
    print '</div>';
   

}
function wpstream_new_general_set() {  



    $no_channel=1;

    if(class_exists ('WC_Subscription')){

    }

    //event_passed
    $args = array(
        'posts_per_page'    => -1,
        'post_type'         => 'product',
        'post_status'       => 'publish',
        'meta_query'        =>      array(
                                        array(
                                                'key'     => 'event_passed',
                                                'value'   => 1,
                                                'compare' => '!=',
                                        )
                                    ),

        'tax_query'         => array(
                                'relation'  => 'AND',
                                array(
                                    'taxonomy'  =>  'product_type',
                                    'field'     =>  'slug',
                                    'terms'     => array('live_stream','subscription')
                                )
                            ),
    );



    $event_list = new WP_Query($args);
    global $live_event_for_user;

    $live_event_for_user    =   wpstream_get_live_event_for_user();


    
    $pack_details           =   wpstream_get_pack_details_user();

    if( isset($pack_details['band']) && isset( $pack_details['storage']) ){
        print '<div class="pack_details_wrapper"><strong>'.__('Your account information: ','wpstream').'</strong> '.__('You have','wpstream').'<strong> '.wpstream_convert_band($pack_details['band']).' Gb</strong> '.__('available streaming bandwidth and','wpstream').' <strong>'.wpstream_convert_band($pack_details['storage']).' Gb</strong> '.__('available media storage','wpstream').'.</div>';
        print'<input type="hidden" id="wpstream_band" value="'.$pack_details['band'].'">';
        print'<input type="hidden" id="wpstream_storage" value="'.$pack_details['storage'].'">';
        
    }

        


    if( $event_list->have_posts()){
        print '<div class="pack_details_wrapper_transparent">
        <h3>'.__('Your Pay-Per-View Channel List','wpstream').'</h3>';
        
        $link_new = admin_url('post-new.php?post_type=product');
        print '<a href="'.esc_url($link_new).'"  class="wpstream_create_new_product_link">'.esc_html__('Create new pay-per-view channel.','wpstream').'</a>';
        print '</div>';

        print '<div style="clear: both;"></div><div class="event_list_wrapper">';

            while ($event_list->have_posts()): $event_list->the_post();

                $the_id =   get_the_ID();
                $is_subscription_live_event =   esc_html(get_post_meta($the_id,'_subscript_live_event',true));
                $term_list                  =   wp_get_post_terms($the_id, 'product_type');

                if( $term_list[0]->name=='subscription' && $is_subscription_live_event=='no'){
                    continue;
                }

                wpstream_live_stream_unit($the_id);

            endwhile;

        print'</div>'; 
        $no_channel=1;
    }else{
        $no_channel=0;
    }


    $ajax_nonce = wp_create_nonce( "wpstream_start_event_nonce" );
    print '<input type="hidden" id="wpstream_start_event_nonce" value="'.$ajax_nonce.'">';
    $current_user       =   wp_get_current_user();
    $allowded_html      =   array();
    $userID             =   $current_user->ID;
    $user_live_streams  =   get_user_meta($userID,'live_shows');


    wp_reset_postdata();








    // free 

    $args_free = array(
        'posts_per_page'    => -1,
        'post_type'         => 'wpstream_product',
        'post_status'       => 'publish',
        'meta_query'        =>      array(
                                        'relation'  => 'AND',
                                        array(
                                                'key'     => 'wpstream_product_type',
                                                'value'   => 1,
                                                'compare' => '==',
                                        ),
                                       
                                    ),
     

    );
    $event_list_free = new WP_Query($args_free);


    if( $event_list_free->have_posts()){
        print '<div class="pack_details_wrapper_transparent">
        <h3>'.__('Your Free Channel List','wpestate').'</h3>';
        
        $link_new = admin_url('post-new.php?post_type=wpstream_product');
        print '<a href="'.esc_url($link_new).'" class="wpstream_create_new_product_link">'.esc_html__('Create new free channel.','wpstream').'</a>';
        print '</div>';
        print '<div style="clear: both;"></div><div class="event_list_wrapper">';

            while ($event_list_free->have_posts()): $event_list_free->the_post();


                $the_id =   get_the_ID();
             
                if( get_post_meta ($the_id,'event_passed',true)!=1){
                    wpstream_live_stream_unit($the_id);
                }

            endwhile;

        print'</div>';    
        $no_channel=1;
    }else{
        $no_channel=0;
    }



    if($no_channel==0){
        $link_new_paid = admin_url('post-new.php?post_type=product');
        $link_new_free = admin_url('post-new.php?post_type=wpstream_product');
        print '<div class="no_events_warning"> '.__('* You don\'t have any live channels!','wpstream');

        print '<a href="'.esc_url($link_new_free).'" class="wpstream_no_chanel_add_channel">'.esc_html__('Add new free channel ','wpstream').'</a>';
        print '<a href="'.esc_url($link_new_paid).'" class="wpstream_no_chanel_add_channel">'.esc_html__('Add new pay-per-view channel ','wpstream').'</a>';        
            
        print '</div>';
    }




}
// end   wpstream_new_general_set  







function wpstream_live_stream_unit($the_id){
    global $live_event_for_user;
   
    $live_class='';
    if(isset($live_event_for_user[$the_id])) {
        $live_class=" wpstream_show_started";
    }
    
  
    print '<div class="event_list_unit '.$live_class.' ">';
        $ip=   get_post_meta($the_id,'ip',true);
      
        
        if(has_post_thumbnail($the_id)){
            $thumb  =   get_the_post_thumbnail_url($the_id,'thumbnail');
        }else{
            $thumb= plugin_dir_url( dirname( __FILE__ ) ). 'img/default_150.jpg';
        }

        
        print '<div class="event_thumb_wrapper"><img class="event_thumb" src="'.$thumb.'" alt="show_imagge"></div>';
        print '<h3 data-prodid="'.$the_id.'">'.get_the_title($the_id).'  - '.esc_html__('Id','wpstream').': '.$the_id.'<a class="view_channel" href="'.get_permalink($the_id).'" target="_blank">'.esc_html__('View Channel','wprentals').'</a></h3> ';

        $live_event_stream_name =   get_post_meta($the_id,'live_event_stream_name',true);
        $live_event_array       =   get_post_meta($the_id,'live_event_uri',true);
        $live_event_uri         =   '';
        

            $pending_streaming_class            =   '';
            $wpstream_ready_to_stream_class     =   '';
            $external_software_streaming_class  =   '';
            $wpstream_no_stream_class           =   '';
            $carnat_key1                        =   '';
            $carnat_key2                        =   '';
            $uri                                =   '';
            $webcaster_url                      =   '';
            $rtmp_ip_uri                        =   '';
            $uri_ip                             =   '';
            
            if(is_array($live_event_for_user) && isset($live_event_for_user[$the_id])) {
               
                $pending_streaming_class            =   'show_stream_data pending_trigger';
                $wpstream_ready_to_stream_class     =   'hide_stream_data';
                $external_software_streaming_class  =   'show_stream_data';
                $wpstream_no_stream_class           =   'hide_stream_data';
                if(isset($live_event_for_user[$the_id]['live_uri'])){
                $explode        =   explode('wpstream.net/',  $live_event_for_user[$the_id]['live_uri']);
                $uri            =   $explode[0].'wpstream.net/wpstream/';
                $stream_name    =   get_post_meta($the_id,'live_event_stream_name',true);
                $carnat_key1    =   $live_event_for_user[$the_id]['carnat1'];
                $carnat_key2    =   $live_event_for_user[$the_id]['carnat2'];
                $webcaster_url  =   'https://'.$live_event_for_user[$the_id]['subdomain_key'].'.live.streamer.wpstream.net:8443/'.$live_event_for_user[$the_id]['carnat2'];
          
                $rtmp_ip_uri = 'http://'.$live_event_for_user[$the_id]['ip'].':8444';
                $uri_ip= 'rtmp://'.$live_event_for_user[$the_id]['ip'].'/wpstream/';
                }
                
            }else{

                $pending_streaming_class            =   'hide_stream_data';
                $wpstream_ready_to_stream_class     =   'hide_stream_data';
                $external_software_streaming_class  =   'hide_stream_data';
                $wpstream_no_stream_class           =   'show_stream_data';
            }
            
            
            
               
                print' <div class="pending_streaming '.$pending_streaming_class.' " data-server-id="'.$the_id.'" data-server-url="'.$rtmp_ip_uri.'">';
                    print'<div class="wpstream_channel_status not_ready_to_stream"><span class="dashicons dashicons-dismiss"></span>'.esc_html__('Getting ready to stream. Please wait...','wprentals').' ';
                    print '<img class="server_loading_new"  src="'.plugin_dir_url( dirname( __FILE__ ) ).'img/loading.gif" alt="loading" /></div>';
                    print '<div class="multiple_warning_events"> '.esc_html__('* You can run multiple live events on a single channel. **Once deleted, you can no longer broadcast to it.','wpstream').'</div>';
                print '</div>';
                
                
                print '<div class="wpstream_ready_to_stream '.$wpstream_ready_to_stream_class.'">';
                    print   '<div class="wpstream_channel_status"><span class="dashicons dashicons-yes"></span>'.esc_html__('Ready To Stream !','wpstream').'</div>';
                    print   '<div class="start_webcaster wpstream_button" data-webcaster-url="'.$webcaster_url.'" >'.esc_html__('Start Browser Broadcast','wpstream').'</div>';
                    print   '<div class="start_external wpstream_button"  >'.esc_html__('Broascast With 3rd Party Software','wpstream').'</div>';   
                print '</div>';
                    
                    
                    
                print '<div class="external_software_streaming '.  $external_software_streaming_class.' ">';
                    print '<div class="event_list_unit_notificationx"><strong>'.__('URL:').' </strong> <div class="wpstream_live_uri_text">' . $uri_ip.'</div><div class="copy_live_uri">'.__('copy to clipboard','wpstream').'</div>';
                    print '<div class="event_list_stream_key_wrap"><strong>'.__('Stream key:').' </strong><div class="wpstream_live_key_text">'. $carnat_key1.'?auth='.$carnat_key2.'</div><div class="copy_live_key">'.__('copy to clipboard','wpstream').'</div></div>';
                    print '<div class="warning_stream">'.sprintf(esc_html__('For professional results use %s with above credentials. You may also use the "RTMP Camera" app on Android phones and tablets or the "Broadcast Me" app on iPhone and iPad.','wpstream'),'<a href="https://obsproject.com/download" target="_blank">OBS Studio software</a>').'</div>';           
                    print ' <img class="how_to" src="'.plugin_dir_url( dirname( __FILE__ ) ).'img/how_to_obs.jpg" alt="show_imagge">';
                    print'</div>';
                print'</div>';
                
                
            

                print '<div class="wpstream_no_stream '.$wpstream_no_stream_class.' ">';
                    print '<div class="event_list_unit_notificationx"><span class="server_notification">'.__('Channel is inactive.','wpestream').' <img class="server_loading" src="'.plugins_url().'/wpstream/img/loading.gif" alt="loading" /></span></div>';
                    print '<div class="record_wrapper">Record ? <input type="checkbox" name="record_event" class="record_event"/>Yes</div>';
                    print '<input class="start_event wpstream_button"  type="button" data-show-id="'.$the_id.'" value="'.esc_html__('Broadcast To Channel','wpstream').'">';
                    print '<input class="close_event wpstream_button"  type="button" data-show-id="'.$the_id.'" value="'.esc_html__('Delete Channel','wpstream').'">';
                    print '<div class="multiple_warning_events"> '.esc_html__('* You can run multiple live events on a single channel. **Once deleted, you can no longer broadcast to it.','wpstream').'</div>';
                print '</div>';
           
       
    print '</div>';
}





function wpstream_convert_band($megabits){
    $gigabit    =   $megabits   *   0.001;
    $gigabit    =   number_format($gigabit,2);
    return $gigabit;
}




function wpstream_check_open_port($host){
  
    $host= str_replace("rtmp://", "",$host);
    $host= str_replace("/", "",$host);
    
    $port='1935';
    $connection = fsockopen($host, $port);
    if (is_resource($connection)){
        print '<div class="server_ready_live">'.__('Ready for Live Streaming','wpstream').'</div>';
        fclose($connection);
    }else{
        print '<div class="server_not_ready_live">'.__('Not Ready Yet!','wpstream').'</div>';
    }
    
}


function wpstream_media_management(){
    if( !wpstream_client_check_api_status() ){
        echo '<div class="api_not_conected" style="margin-top:30px;">'.__('No Connection to WpStream.net  Use your WpStream credentials to connect  below or go  ','wpstream').'<a href="https://wpstream.net/my-account" target="_blank">here</a>'.__(' to create an account.','wpstream').'</div>';
        return;     
    }
                        
                        
    $pack_details           =   wpstream_get_pack_details_user();

    if( isset($pack_details['band']) && isset( $pack_details['storage']) ){
        print '<div class="pack_details_wrapper"><strong>'.__('Your account information: ','wpstream').'</strong> '.__('You have','wpstream').'<strong> '.wpstream_convert_band($pack_details['band']).' Gb </strong> '.__('available streaming bandwidth and','wpstream').' <strong>'.wpstream_convert_band($pack_details['storage']).' Gb </strong> '.__('available media storage','wpstream').'.</div>';
        print'<input type="hidden" id="wpstream_band" value="'.$pack_details['band'].'">';
        print'<input type="hidden" id="wpstream_storage" value="'.$pack_details['storage'].'">';
    }
    
    print '<div id="wpstream_media_upload"><h3>'.__('Media Upload','wpstream').'</h3>'.wpstream_present_media_upload().'</div>';
    
    print '<div id="wpstream_file_management"><h3 id="video_management_title">'.__('Video Management','wpstream').'</h3>'.wpstream_present_file_management().'</div>';
    
}



function wpstream_present_media_upload(){
    $to_return='';
    $formInputs=wpstream_get_signed_form_upload_data();
    
    if($formInputs === 'notenough'){
        $to_return.='<div class="wpstream_upload_alert">'.esc_html__('You do not have enough bandwidth or storage to upload a video!','wpstream').'</div>';
        return $to_return;
    }
    
    
    $to_return.='<div class="wpstream_upload_container">';
    $to_return.='<div id="wpstream_uploaded_mes">'.esc_html__('Please select or drop a video file. Do not close this window during the upload!','wpstream').'</div>';
    $to_return.='<form action="https://wpstream-videos.s3.amazonaws.com/"
                  method="POST"
                  enctype="multipart/form-data"
                  class="direct-upload">';
    
    $to_return.='<input id="wpstream_upload" type="file" class="inputfile inputfile-1" value="Pick a video file" name="file" multiple>';
    $to_return.='<label for="wpstream_upload"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="17" viewBox="0 0 20 17"><path d="M10 0l-5.2 4.9h3.3v5.1h3.8v-5.1h3.3l-5.2-4.9zm9.3 11.5l-3.2-2.1h-2l3.4 2.6h-3.5c-.1 0-.2.1-.2.1l-.8 2.3h-6l-.8-2.2c-.1-.1-.1-.2-.2-.2h-3.6l3.4-2.6h-2l-3.2 2.1c-.4.3-.7 1-.6 1.5l.6 3.1c.1.5.7.9 1.2.9h16.3c.6 0 1.1-.4 1.3-.9l.6-3.1c.1-.5-.2-1.2-.7-1.5z"/></svg> <span id="wpstream_label_action">Choose a file&hellip;</span></label>';
    
    
    $to_return.='<div class="wpstream_file_drop_color">';
    $to_return.='<div class="wpstream_form_ex">'.esc_html__('Drop a video file here!','wpstream').'</div>';      
    $to_return.='<div class="wpstream_form_ex_details">'.esc_html__('The Video File must be encoded with the following settings:

Container: MP4,
Video codec: H264,
Audio codec: AAC. Media will fail to play back if it does not follow the above settings. 
You may use a tool like MediaInfo to verify your file. Also you may convert it with specialized software like HandBrake.','wpstream').'<strong>'.esc_html__('Accepted file extensions: .mp4, .mov .','wpstream').'</strong></div>';    
    if(is_array($formInputs)){      
        foreach ($formInputs as $name => $value) { 
                $to_return.='<input type="hidden" name="'. $name.'" value="'.$value.'">';
        }                     
    }                  
    
    $to_return.='
    <div class="progress-bar-area"></div></div>
    </form>';
    
    $to_return.='</div>';
    return $to_return;
            
}













function wpstream_present_file_management(){
   $video_list_raw = wpstream_get_videos_from_storage_raw_data();
   




    $to_return='';
    if(is_array($video_list_raw)){
        foreach ($video_list_raw as $key =>$video){
             $to_return.='<div class="wpstream_video_wrapper">';

                $to_return.='<div class="wpstream_video_title">';
                $to_return.='<div class="wpstream_video_notice"></div></div>';
                $to_return.='<div class="wpstream_video_title"><strong class="storage_file_name">'.esc_html__('File Name :','wpstream').'</strong>'.'<span class="storage_file_name_real">'.$video['video_name_storage'].'</span></div>';
                $to_return.=' <div class="wpstream_delete_media" ';
                $to_return.=' onclick="return confirm(\' Are you sure you wish to delete '.$video['video_name_storage'].'?\')" data-filename="'.$video['video_name_storage'].'">'.esc_html__('delete file','wpstream').'</div>';
                $to_return.='<div class="wpstream_get_download_link" data-filename="'.$video['video_name_storage'].'">'.esc_html__('get download link','wpstream').'</div>';
                $to_return.='<a href="" class="wpstream_download_link">'.esc_html__('Click to download! The url will work for the next 20 minutes!','wpstream').'</a>';

                $add_free_video_url=admin_url('post-new.php?post_type=wpstream_product').'&new_video_name='. rawurlencode($video['video_name_storage']);
                $add_paid_video_url=admin_url('post-new.php?post_type=product').'&new_video_name='. rawurlencode($video['video_name_storage']);
            
                
                
                $to_return .='<a class="create_new_free_video" href="'.esc_url($add_free_video_url).'">'.esc_html__('Create new free VOD from this video').'</a>'; 
                $to_return .='<a class="create_new_ppv_video" href="'.esc_url($add_paid_video_url).'">'.esc_html__('Create pay-per-view VOD from this video').'</a>'; 
                
             $to_return.='</div>';

        }
   } else {
       $to_return.= '<div class="wpstream_video_wrapper">'.esc_html__('You don\'t have any videos.','wpstream').'</div>';
   }
   return $to_return;
}
```
