<?
/*
Plugin Name: 7T-FileManager
Plugin URI: http://www.7tech.co.in/wordpress/file-manager-plugin-for-wordpress
Description: Simple to use file manager with code editor to edit files. Create new files and folders. You can also upload, download, copy, delete multiple files.
Author: Ankur sharma (ankursharma@7tech.co.in)
Version: 1.01
Author URI: http://www.ankursharma.net
*/

add_action("init","fm7t_init");
function fm7t_init()
{
	if(isset($_REQUEST['ajax_request']) && $_REQUEST['ajax_request'] && current_user_can('administrator')){
		include(plugin_dir_path( __FILE__ ).'functions.php'); 
		$fm=new FileManagerMain();
		$ajax_result=array("success"=>0,"data"=>"","error"=>"");
		if(isset($_POST['get_left_col_sub_dir'])){
			$ajax_result["success"]=1;
			$ajax_result['data']=$fm->get_left_col_sub_dir($_POST['get_left_col_sub_dir']);
		} else if(isset($_POST['get_right_col_sub_dir'])){
			$ajax_result["success"]=1;
			if(isset($_POST['get_left_sub_dir_too']) && $_POST['get_left_sub_dir_too']){
				$ajax_result['left_sub_dir']=$fm->get_left_col_sub_dir($_POST['get_right_col_sub_dir']);
			}
			$ajax_result['data']=$fm->get_right_col_sub_dir($_POST['get_right_col_sub_dir']);
		}else if(isset($_POST['create_new_file'])){
			$path = $fm->get_root_path().implode("/",$_POST['path'])."/";
			if( !is_file($path.$_POST['create_new_file']) && !is_dir($path.$_POST['create_new_file'])){
				$FileHandle = @fopen($path.$_POST['create_new_file'], 'w') ;
				fclose($FileHandle);
				$ajax_result["success"]=1;
				$ajax_result['data']=$fm->get_right_col_sub_dir($_POST['path']);
			}else{
				$ajax_result['error']="File already exist!";
			}		
		}else if(isset($_POST['create_new_folder'])){
			$path = $fm->get_root_path().implode("/",$_POST['path'])."/";
			if( !is_dir($path.$_POST['create_new_folder']) && !is_file($path.$_POST['create_new_folder'])){
				$ajax_result["success"]=mkdir($path.$_POST['create_new_folder'], 0755);
				$ajax_result['data']=$fm->get_right_col_sub_dir($_POST['path']);
				$ajax_result['left_sub_dir']=$fm->get_left_col_sub_dir($_POST['path']);
			}else{
				$ajax_result['error']="Folder already exist!";
			}	
		}else if(isset($_POST['delete_files'])){
			$path = $fm->get_root_path().implode("/",$_POST['path'])."/";
			$ajax_result["success"]=1;
			foreach($_POST['delete_files'] as $filename){
				if(is_dir($path.$filename)){
					if(!$fm->rrmdir( $path.$filename)){ 
						$ajax_result["success"]=0; $ajax_result['error'].='Folder "'.$filename.'" can not be deleted! '."\n"; 
					}
				}else if(is_file($path.$filename)){
					if(!unlink( $path.$filename)){ 
						$ajax_result["success"]=0; $ajax_result['error'].='File "'.$filename.'" can not be deleted! '."\n"; 
					}			
				}else{
					$ajax_result["success"]=0;
					$ajax_result['error']='File/Folder "'.$filename.'" does not exist! '."\n";
				}
			}
			$ajax_result['data']=$fm->get_right_col_sub_dir($_POST['path']);
			$ajax_result['left_sub_dir']=$fm->get_left_col_sub_dir($_POST['path']);		
		}else if(isset($_POST['upload_files_path'])){
			define('NO_DIRECT_ACCESS', true);
			$hideresult=1;
			$targetDir =  $fm->get_root_path().$_POST['upload_files_path']."/";
			include(plugin_dir_path( __FILE__ )."upload.php");			
			$ajax_result["success"]=1;
		}else if(isset($_REQUEST['filepath_for_edit'])){
			$targetFile =  $fm->get_root_path().$_REQUEST['filepath_for_edit'].'/'.$_REQUEST['filename'];
			if(is_file($targetFile)){ 
				if(isset($_POST['reload']) && $_POST['reload']){
					ob_start();
					readfile($targetFile);
					$ajax_result["data"]=ob_get_contents();
					ob_end_clean();
					$ajax_result["success"]=1;
				}else if(isset($_POST['save'])){
					if($file = @fopen($targetFile,"w")){
						fwrite($file,$_POST['save']);
						fclose($file);
						$ajax_result["success"]=1;
					}else{
						$ajax_result["success"]=0;
						$ajax_result["error"]="Can not open file.";
					}
				}else{
					define('NO_DIRECT_ACCESS', true);
					$ext=strrpos($_REQUEST['filename'],".")>0?strtolower(substr($_REQUEST['filename'],strrpos($_REQUEST['filename'],".")+1)):"html";
					$ext=$ext=="txt"?"html":$ext;
					include(plugin_dir_path( __FILE__ ).'file_editor.php');
					die();
				}
			}else{
				$ajax_result["success"]=0;
				$ajax_result["error"]="Wrong file path.";
			}
		}else if(isset($_POST['new_name'])){
			$path = $fm->get_root_path().implode("/",$_POST['path'])."/";
			if(file_exists($path.$_POST['new_name'])){
				$ajax_result["success"]=0;
				$ajax_result['error']='File/Folder "'.$_POST['new_name'].'" already exist! '."\n";
			}else{
				rename($path.$_POST['old_name'],$path.$_POST['new_name']);
				$ajax_result["success"]=1;
				$ajax_result['data']=$fm->get_right_col_sub_dir($_POST['path']);
				$ajax_result['left_sub_dir']=$fm->get_left_col_sub_dir($_POST['path']);	
			}
		}else if(isset($_POST['change_permission'])){
			$path = $fm->get_root_path().implode("/",$_POST['path'])."/";
			foreach($_POST['change_permission'] as $filename){
				if(file_exists($path.$filename)){
					chmod($path.$filename, octdec($_POST['permission_val']));
				}
			}
			$ajax_result["success"]=1;
			$ajax_result['data']=$fm->get_right_col_sub_dir($_POST['path']);
		}else if(isset($_REQUEST['download_file'])){
			$filename=$_REQUEST['download_file'];
			$fullpath=$fm->get_root_path().$_REQUEST['download_file_path']."/".$filename;
			header("Content-Type: application/octet-stream");
			header("Content-Length: ".filesize($fullpath));
			header("Content-Disposition: attachment; filename=$filename");
			readfile($fullpath);
			die();
		}else if(isset($_POST['destination_path_for_copy'])){
			$path = $fm->get_root_path().implode("/",$_POST['path'])."/";
			$dpath = $fm->get_root_path().((substr($_POST['destination_path_for_copy'],0,1)=="/")?substr($_POST['destination_path_for_copy'],1):$_POST['destination_path_for_copy']);
			$dpath = str_replace("\\","/",$dpath);
			$dpath = (substr($dpath,-1)=="/")?$dpath:$dpath.'/';
			$ajax_result["success"]=1;
			$count=0;
			foreach($_POST['files'] as $fname)
			{
				if(file_exists($dpath.$fname)){
					$ajax_result["success"]=0;
					$ajax_result["error"]="File exist";
				}
			}
			if($ajax_result["success"]!=0){
				if(!file_exists($dpath)){
					@mkdir( $dpath,0755,true);
				}
				foreach($_POST['files'] as $fname)
				{
					if(!$fm->copy_dir($path.$fname,$dpath.$fname)){
						$ajax_result["success"]=0;
						$ajax_result["error"]="Error while copying ";
					}else{
						$count++;
					}		
				}
			}
			if($count){
				$ajax_result['data']=$fm->get_right_col_sub_dir($_POST['path']);
				$ajax_result['left_sub_dir']=$fm->get_left_col_sub_dir(explode("/",$dpath));	
			}
		}
		echo json_encode($ajax_result);
		exit();
	}

	add_action('admin_menu','fm7t_admin_menu');
}



function fm7t_admin_menu() {

	if (function_exists('add_menu_page') && function_exists('add_submenu_page')) {
		add_menu_page( "7t File Manager", "7t File Manager", 'administrator', '7t-filemanager', 'fm7t_filemanager' ); 
		$page1 = add_submenu_page( "7t-filemanager", "Manage Consults", "Manage Consults", 'administrator', '7t-filemanager', 'fm7t_filemanager');
		add_action( "admin_print_scripts-$page1", 'fm7t_admin_print_scripts' );
	}
}

function fm7t_admin_print_scripts(){
	wp_enqueue_style('thickbox');
	wp_enqueue_style('fm7t-plugin-style', plugins_url('css/style.css', __FILE__)  );
	wp_enqueue_script('thickbox');
	wp_enqueue_script('jquery-ui-selectable');	
	wp_enqueue_script('wp-plupload');
	wp_enqueue_script( 'fm7t-plugin-script', plugins_url('js/script.js', __FILE__) );	 
}

function fm7t_filemanager()
{
	include(plugin_dir_path( __FILE__ ).'functions.php'); 
	$fm=new FileManagerMain();
?>
<div class="wrap">
<div class="donation">
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="ERYZYA8TYP4KL">
<table>
<tr><td><input type="hidden" name="on0" value="Donation"><strong>Help me to make it better:</strong><br><select name="os0">
	<option value="Donate $10">$10.00 USD </option>
	<option value="Donate $20">$20.00 USD </option>
	<option value="Donate $30">$30.00 USD </option>
	<option value="Donate $40">$40.00 USD </option>
	<option value="Donate $50">$50.00 USD </option>
</select> </td><td>
<input type="hidden" name="currency_code" value="USD">
<input type="image" src="https://www.paypalobjects.com/en_GB/i/btn/btn_donateCC_LG.gif" border="0" name="submit" alt="PayPal — The safer, easier way to donate online.">
</td>
</tr>
</table>
</form>
</div>


<h2>7T File Manager</h2>
<div id="total_wrapper" plg_path="<?php echo WP_PLUGIN_URL.'/'.plugin_basename( dirname(__FILE__)); ?>">
	<div id="right-col-btn">
		<li title="Add New File" id="newfile_btn"></li>
		<li title="Add New Folder" id="newfolder_btn"></li>
		<li title="Copy Files & Folders" id="copy_btn"></li>
		<li title="Upload a File" id="upload_btn"></li>
		<li title="Download Selected Files" id="download_btn"></li>
		<li title="Delete Selected Files" id="delete_btn"></li>
		<li title="Up Directory" id="up_btn"></li>
		<li title="Reload Directory" id="refresh_btn"></li>
		<li title="Rename Selected File" id="rename_btn"></li>
		<li title="Change File/Directory Permission" id="permission_btn"></li>
		<li title="Edit File" id="edit_btn"></li>
		<li title="Edit File with CodeEditor" id="codeedit_btn"></li>
		<div class="clear"></div>
	</div>
	<div id="col_wrapper">
		<div id="left-col">
		<?php
			echo $fm->get_left_col();
		?>
		</div>
		
		<div id="right-col">
		<?php
			echo $fm->get_right_col();
		?>
		</div>
		<div class="clear"></div>
	</div>
</div>

</div>
<div><center>Powered by <a href="http://www.7tech.co.in">7tech.co.in</a></center></div>
<div id="data_container"></div>
<?php
}

