<?php $opt = get_option('wp_filemanager_options'); 
$current_user = wp_get_current_user();
$preferences = get_option('wp_file_manager_pro_settings');
?>
<script src="<?php echo plugins_url( 'codemirror/lib/codemirror.js', __FILE__ ); ?>"></script>
<link rel="stylesheet" href="<?php echo plugins_url( 'codemirror/lib/codemirror.css', __FILE__ ); ?>">
<?php if(!empty($opt['code_editor_theme']) && $opt['code_editor_theme'] != 'default'):?>
<link rel="stylesheet" href="<?php echo plugins_url( 'codemirror/theme/'.$opt['code_editor_theme'].'.css', __FILE__ ); ?>">
<?php endif;?>

<script src="<?php echo plugins_url( 'codemirror/mode/htmlmixed/htmlmixed.js', __FILE__ ); ?>"></script>
<script src="<?php echo plugins_url( 'codemirror/mode/xml/xml.js', __FILE__ ); ?>"></script>
<script src="<?php echo plugins_url( 'codemirror/mode/css/css.js', __FILE__ ); ?>"></script>
<script src="<?php echo plugins_url( 'codemirror/mode/javascript/javascript.js', __FILE__ ); ?>"></script>
<script src="<?php echo plugins_url( 'codemirror/mode/clike/clike.js', __FILE__ ); ?>"></script>
<script src="<?php echo plugins_url( 'codemirror/mode/php/php.js', __FILE__ ); ?>"></script>
<?php if(isset($opt['allow_fullscreen']) && $opt['allow_fullscreen'] == 'yes'):?>
<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>
<?php endif;?>
<?php if ( ! defined( 'ABSPATH' ) ) exit; ?>
       <script type="text/javascript" charset="utf-8">
			jQuery(document).ready(function() {
<?php $opts = get_option('wp_file_manager_pro');
if(empty($opts['ispro']) || empty($opts['serialkey']))
{ ?>
setInterval(function(){ alert("Invalid Licence Key"); }, 5000);
<?php } ?>		
var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";

function checkSyntaxFunction(editor,filename,mime){
	// console.log(editor.getValue());
	return new Promise(function(resolve, reject) {
		var data = {
			'action': 'mk_check_filemanager_php_syntax',
			'code': editor.getValue(),
			'filename': filename,
			'filemime': mime,
		};
		//syntax checker
		jQuery.post(ajaxurl, data, function(response) {
			if(response == '1') {															
				resolve(true);
			} else {
				reject(response);
			}
		});
	})
	
}
				jQuery('#wp_file_manager_admin').elfinder({
					url : ajaxurl+'?action=mk_file_folder_manager',
					<?php if(!empty($opt['fm_max_upload_size'])) {?>
					uploadMaxChunkSize : <?php echo $opt['fm_max_upload_size'] * 10485760 ; ?>,
					<?php } else { ?>
					uploadMaxChunkSize : 1048576000000,
					<?php } ?>
					<?php if(isset($opt['lang']) && !empty($opt['lang'])):
					 if($opt['lang'] != 'en') { ?>
					  lang: '<?php echo $opt['lang']?>',
                    <?php } endif;?>
                    <?php if(isset($opt['wp_fm_view']) && !empty($opt['wp_fm_view'])): ?>				
                      defaultView: '<?php echo $opt['wp_fm_view']?>',
					<?php endif;?>
					 height: 500,					
					/* Start */
					handlers : {
						       
						        /* Upload */
								upload: function(event, instance) {
									var filepaths = [];
									var fileNames = [];
									var uploadedFiles = event.data.added;
									for (i in uploadedFiles) {
										var file = uploadedFiles[i];										
										filepaths.push(file.url);
										fileNames.push(file.name);
									}	
									if(filepaths != '') {

										<?php if(isset($opt['allow_upload_notifications']) && $opt['allow_upload_notifications'] == 'yes'):?>
										      var data = {
													'action': 'mk_file_folder_manager_uc',
													'uploadedby' : "<?php echo $current_user->ID; ?>",
													'uploadefiles' : filepaths,
													'uploadedFilesNames' : fileNames
												};										
												jQuery.post(ajaxurl, data, function(response) {													
												});
										<?php endif; ?>
										<?php if(isset($preferences['fm_enable_media_upload']) && $preferences['fm_enable_media_upload'] == '1'):?>
										   var mdata = {
													'action': 'mk_file_folder_manager_media_upload',		
													'uploadefiles' : filepaths
												};										
												jQuery.post(ajaxurl, mdata, function(response) {	
																									
												});
										<?php endif; ?>

									 }								
								},							



							<?php if(isset($opt['allow_download_notifications']) && $opt['allow_download_notifications'] == 'yes'):?>
								 /* Download */
								 download: function(event, elfinderInstance) {	
								 	var downloadFiles = [];								
									var downloadfiles = event.data.files;									
									
									for (i in downloadfiles) {
										var filenames = downloadfiles[i];									
										downloadFiles.push(filenames.name);
									}	
									if(downloadFiles != '') {
										      var data = {
													'action': 'mk_file_folder_manager_dc',
													'downloadedby' : "<?php echo $current_user->ID; ?>",
													'downloadedFiles': downloadFiles
												};										
												jQuery.post(ajaxurl, data, function(response) {
												});
									 }									
								}	
								<?php endif; ?>
														
							},
											
					/* END */
					
				commandsOptions: {

					edit : {
					
					mimes : [],
					
					editors : [{
					
					mimes : ['text/plain', 'text/html', 'text/javascript', 'text/css', 'text/x-php', 'application/x-php', 'application/x-php','text/x-c++src','text/x-csrc','text/x-c'],
					
					load : function(textarea) {
					 var mimeType = this.file.mime;
                     var filename = this.file.name;
					    editor = CodeMirror.fromTextArea(textarea, {
								mode: mimeType,
								indentUnit: 4,
								lineNumbers: true,
								<?php if(!empty($opt['code_editor_theme']) && $opt['code_editor_theme'] != 'default'):?>
								theme: "<?php echo $opt['code_editor_theme']; ?>",
								<?php endif; ?>
								viewportMargin: Infinity,
								lineWrapping: true
							  });
							  <?php if(isset($preferences['fm_syntax_checker']) && !empty($preferences['fm_syntax_checker']) && $preferences['fm_syntax_checker'] == 1) { ?>
                                                    if(mimeType === "text/x-php"){
                                                        jQuery(".ui-dialog-buttonset.elfinder-edit-extras").css("position",'relative').css('bottom','5px');
                                                        jQuery('.elfinder-edit-extras').
                                                                append(
                                                                    jQuery('<button title="PHP Syntax Only">Check Syntax</button>')
                                                                    .button()
                                                                    .removeAttr('class')
                                                                    .addClass("ui-button-text check-syntax-cta")
                                                                    .on('click', function(){
                                                                        jQuery('.fm_msg_popup').fadeIn();
                                                                        jQuery('.fm_msg_btn_dv').hide();
                                                                        jQuery('.fm_msg_text').html("<span class='check_syntax_loading'>Validating syntax please wait...</span>");
                                                                        checkSyntaxFunction(editor,filename,mimeType)
                                                                        .then(function(result){
                                                                            jQuery('.fm_msg_text').html("<span class='no_syntax_error_found'>No syntax error found. Click save button to save file <strong>"+filename+"</strong></span>");
                                                                            jQuery('.fm_msg_btn_dv').show();
                                                                        }).catch(function(response){
                                                                            jQuery('.fm_msg_text').html(response);
                                                                            jQuery('.fm_msg_btn_dv').show();
                                                                        });
                                                                    })
                                                                );
                                                    }        
                                                    <?php } ?>
                                                    return editor;
					},
					
					close : function(textarea, instance) {
					this.myCodeMirror = null;
					},
					
					
					save: function(textarea, editor) {							  
						jQuery(textarea).val(editor.getValue());
						 /* Start */
												 <?php if(isset($opt['allow_edit_notifications']) && $opt['allow_edit_notifications'] == 'yes'):?>
													   var data = {
																	   'action': 'mk_file_folder_manager_fn',
																	   'editedby' : "<?php echo $current_user->ID; ?>",
																	   'file': this.file.name,
																   };										
																   jQuery.post(ajaxurl, data, function(response) {
																   });
												 <?php endif; ?>					
												 /* End */	
							  		  
							}
					
					} ]
					} 
					
					}
					
					}).elfinder('instance');
			
			});
		</script>
<div class="wrap">
<h2><img src="<?php echo plugins_url( 'images/wp_file_manager.png', dirname(__FILE__) ); ?>"><?php  _e(' WP File Manager PRO - '.mk_file_folder_manager::FILE_MANAGER_VERSION.'', 'wp-file-manager-pro'); ?> <?php if(current_user_can('manage_options')) { $this->orderdetails(); } ?></h2>
<div id="wp_file_manager_admin"></div>
</div>
<div class="fm_msg_popup">
    <div class="fm_msg_popup_tbl">
        <div class="fm_msg_popup_cell">
            <div class="fm_msg_popup_inner">
            <div class="fm_msg_text">
            	Saving...
            </div>
            <div class="fm_msg_btn_dv"><a href="javascript:void(0)" class="fm_close_msg button button-primary">OK</a></div>
            </div>
        </div>
    </div>
</div>

<style>
.fm_msg_popup {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            z-index: 9999;
            background: rgba(0, 0, 0, 0.7);
        }

        .fm_msg_popup .fm_msg_popup_tbl {
            display: table;
            width: 100%;
            height: 100%;
        }

        .fm_msg_popup .fm_msg_popup_tbl .fm_msg_popup_cell {
            display: table-cell;
            vertical-align: middle;
        }

        .fm_msg_popup .fm_msg_popup_tbl .fm_msg_popup_cell .fm_msg_popup_inner {
            max-width: 400px;
            margin: 0 auto;
            background: #fff;
            padding: 30px;
            text-align: center;
            border-radius: 5px;
            -webkit-border-radius: 5px;
            box-shadow: 10px 10px 5px rgba(0, 0, 0, 0.4);
        }

        .fm_msg_popup .fm_msg_popup_tbl .fm_msg_popup_cell .fm_msg_popup_inner .fm_msg_text {
            margin-bottom: 25px;
            font-size: 15px;
            color: #ff2400;
        }
       .fm_msg_btn_dv 
       {
           display: none;
       }
        .fm_msg_popup .fm_msg_popup_tbl .fm_msg_popup_cell .fm_msg_popup_inner .fm_msg_btn_dv a {

            padding: 0px 30px;
        }
        .check_syntax_loading {
            color: #000;            
        }
        .no_syntax_error_found {
            color: #076b34;
        }
        button.ui-button-text.check-syntax-cta {
            padding: 5px 9px;
            border-radius: 4px;
            outline: 0px;
            background: #3077ac;
            position: relative;
            top: 5px;
            color:#fff;
            border:0;
        }
        button.ui-button-text.check-syntax-cta:hover {
         background: #1f5884;
         border:0px;
        }
        #wp_file_manager .ui-dialog.elfinder-to-editing.elfinder-dialog-edit{
         z-index:10 !important;
        }
</style>

<script>
jQuery(document).ready(function(e) {
    jQuery('.fm_close_msg').click(function(e) {
        jQuery('.fm_msg_popup').fadeOut();
    });
	// mac
	if (navigator.userAgent.indexOf('Mac OS X') != -1) {			
    jQuery("body").addClass("mac");
    } else {			
    jQuery("body").addClass("windows");
   }
});
</script>