<?php if ( ! defined( 'ABSPATH' ) ) exit; ?>
<?php if(is_user_logged_in()):
$opt = get_option('wp_filemanager_options');
$permissions = false;
            $allowedroles = isset($opt['fm_user_roles']) ? $opt['fm_user_roles'] : array();
			if(empty($allowedroles))
			{
				$allowedroles = array();
			}
            $current_user = wp_get_current_user();
			$userLogin = $current_user->user_login;
			$userID = $current_user->ID; 
			$user = new WP_User( $userID );
			if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
				foreach ( $user->roles as $role ):
					$role;
				endforeach;	
			}
			$mk_count_u_roles = array_intersect($user->roles,$allowedroles);
			if($role == 'administrator'):
			 $permissions = true;
			elseif(in_array('administrator', $user->roles)):
			$permissions = true;
			elseif(in_array($role, $allowedroles)):
			 $permissions = true;
			elseif(count($mk_count_u_roles) > 0):
			 $permissions = true;	
			 else:
			 $permissions = false;
			endif;
if($permissions == true && $permissions) {
			$hide_toolbar = false;
			$hide_context_menu = false;
            $selected_user = (isset($opt['select_users']) && !empty($opt['select_users'])) ? $opt['select_users'] : array();
			$select_user_roles = (isset($opt['select_user_roles']) && !empty($opt['select_user_roles'])) ? $opt['select_user_roles'] : array();	
            if(in_array($userLogin, $selected_user)){
					$key = array_search($userLogin, $selected_user);
					if(!empty($opt['user_disable_toolbar_'.$key]) && $opt['user_disable_toolbar_'.$key] == 'yes'):		                      $hide_toolbar = true;
			        endif;					
					if(!empty($opt['user_disable_context_'.$key]) && $opt['user_disable_context_'.$key] == 'yes'):                      $hide_context_menu = true;
                    endif;
			} else if(in_array($role, $select_user_roles)) {
				    $key = array_search($role, $select_user_roles);
					if(!empty($opt['user_role_disable_toolbar_'.$key]) && $opt['user_role_disable_toolbar_'.$key] == 'yes'):		    $hide_toolbar = true;
                    endif;
					if(!empty($opt['user_role_disable_context_'.$key]) && $opt['user_role_disable_context_'.$key] == 'yes'):			$hide_context_menu = true;
                    endif;
			}
wp_enqueue_style( 'jquery-ui', plugins_url('css/jquery-ui.css', dirname( __FILE__ )));
						wp_enqueue_style( 'elfinder.min', plugins_url('lib/css/elfinder.min.css', dirname( __FILE__ ))); 
						wp_enqueue_style( 'theme', plugins_url('lib/css/theme.css', dirname( __FILE__ )));
						wp_enqueue_script( 'jquery_min', plugins_url('js/jquery-ui.min.js', dirname( __FILE__ )));	
						wp_enqueue_script( 'elfinder_min', plugins_url('lib/js/elfinder.full.js',  dirname( __FILE__ ) ));	
							if(isset($opt['lang']) && !empty($opt['lang'])):
						 if($opt['lang'] != 'en') {
						    wp_enqueue_script( 'fm_lang', plugins_url('lib/js/i18n/elfinder.'.$opt['lang'].'.js', dirname( __FILE__ ) ));	
						 }
						endif;
						if(isset($opt['theme']) && !empty($opt['theme'])){
							 if($opt['theme'] != 'default') {
								wp_enqueue_style( 'theme-latest', plugins_url('lib/themes/'.$opt['theme'].'/css/theme.css',  dirname( __FILE__ ) ));	
						       } 
						}

wp_enqueue_script( 'codemirror_js', plugins_url('lib/codemirror/lib/codemirror.js', dirname( __FILE__ ) ));						
wp_enqueue_style( 'codemirror_css', plugins_url('lib/codemirror/lib/codemirror.css', dirname( __FILE__ )));	
if(!empty($opt['code_editor_theme']) && $opt['code_editor_theme'] != 'default') {
	wp_enqueue_style( 'codemirror_theme', plugins_url('lib/codemirror/theme/'.$opt['code_editor_theme'].'.css', dirname( __FILE__ )));	
}	
wp_enqueue_script( 'fm_htmlmixed', plugins_url('lib/codemirror/mode/htmlmixed/htmlmixed.js', dirname( __FILE__ ) ));
wp_enqueue_script( 'fm_xml', plugins_url('lib/codemirror/mode/xml/xml.js', dirname( __FILE__ ) ));	
wp_enqueue_script( 'fm_css', plugins_url('lib/codemirror/mode/css/css.js', dirname( __FILE__ ) ));	
wp_enqueue_script( 'fm_javascript_js', plugins_url('lib/codemirror/mode/javascript/javascript.js', dirname( __FILE__ ) ));	
wp_enqueue_script( 'fm_php', plugins_url('lib/codemirror/mode/php/php.js', dirname( __FILE__ ) ));				

if(isset($opt['allow_fullscreen']) && $opt['allow_fullscreen'] == 'yes'):
$filemanagerReturn .='<style>
.ui-dialog.ui-widget.ui-widget-content.ui-corner-all.ui-draggable.std42-dialog.touch-punch.elfinder-dialog.ui-resizable.elfinder-dialog-active.ui-front {
  left: 0 !important;
  width: 100% !important;
}
</style>';
endif;
wp_register_script( "file_manager_pro_shortcode_admin", plugins_url('js/file_manager_pro_shortcode_admin.js', dirname( __FILE__ ) ), array('jquery') );
wp_localize_script( 'file_manager_pro_shortcode_admin', 'fmparams', array(
	'ajaxurl' => admin_url('admin-ajax.php'),
	'uploadMaxChunkSize' => (isset($opt['fm_max_upload_size']) && !empty($opt['fm_max_upload_size'])) ? $opt['fm_max_upload_size'] * 1048576 : '1048576000000',
	'lang' => (isset($opt['lang']) && !empty($opt['lang'])) ? $opt['lang'] : 'en',
	'hide_toolbar' => $hide_toolbar,
	'hide_context_menu' => $hide_context_menu,
	'allow_upload_notifications' => (isset($opt['allow_upload_notifications']) && $opt['allow_upload_notifications'] == 'yes') ? 'yes' : 'no',
	'allow_download_notifications' => (isset($opt['allow_download_notifications']) && $opt['allow_download_notifications'] == 'yes') ? 'yes' : 'no',
	'allow_edit_notifications' => (isset($opt['allow_edit_notifications']) && $opt['allow_edit_notifications'] == 'yes') ? 'yes' : 'no',
	'userID' => $userID,
    'code_editor_theme' => (!empty($opt['code_editor_theme']) && $opt['code_editor_theme'] != 'default') ? $opt['code_editor_theme'] : 'default',
    'view' => (isset($opt['wp_fm_view']) && !empty($opt['wp_fm_view'])) ? $opt['wp_fm_view'] : 'list',
	)
);        
wp_enqueue_script( 'file_manager_pro_shortcode_admin' );
$filemanagerReturn .='<div class="wrap_file_manager">
<p class="wrap_file_manager_p"><strong>Welcome: </strong>'.$current_user->display_name.'</p>
<div id="wp_file_manager"></div>
</div>';
}
else
{
  $filemanagerReturn .='<p>Sorry, you are not allowed to access this page.</p>';	
}
else:
  $filemanagerReturn .='Please login to access file manager.';
endif;?>