Added new feature - backup file, Search file/folder and send files via mail

This commit is contained in:
Prasath Mani 2017-07-07 15:59:04 +05:30
parent ff549f6419
commit 89c2905e99

View file

@ -187,6 +187,35 @@ unset($p, $use_auth, $iconv_input_encoding, $use_highlightjs, $highlightjs_style
/*************************** ACTIONS ***************************/
//AJAX Request
if (isset($_POST['ajax']) && !FM_READONLY) {
//search : get list of files from the current folder
if(isset($_POST['type']) && $_POST['type']=="search") {
$dir = $_POST['path'];;
$response = scan($dir);
echo json_encode($response);
}
//Send file to mail
if (isset($_POST['type']) && $_POST['type']=="mail") {
$isSend = send_mail($_POST['path'],$_POST['file'],'ccpprogrammers@gmail.com','Hello Word');
echo $isSend;
}
//backup files
if(isset($_POST['type']) && $_POST['type']=="backup") {
$file = $_POST['file'];
$path = $_POST['path'];
$date = date("dMy-His");
$newFile = $file.'-'.$date.'.bak';
copy($path.'/'.$file, $path.'/'.$newFile) or die("Unable to backup");
echo "Backup $newFile Created";
}
exit;
}
// Delete file / folder
if (isset($_GET['del']) && !FM_READONLY) {
$del = $_GET['del'];
@ -865,6 +894,7 @@ if (isset($_GET['view'])) {
?>
<b><a href="?p=<?php echo urlencode(trim(FM_PATH)) ?>&amp;edit=<?php echo urlencode($file) ?>" class="edit-file"><i class="fa fa-pencil-square"></i> <?php echo fm_t('Edit') ?></a></b> &nbsp;
<?php } ?>
<b><a href="javascript:mailto('<?php echo urlencode(trim(FM_ROOT_PATH.'/'.FM_PATH)) ?>','<?php echo urlencode($file) ?>')"><i class="fa fa-pencil-square"></i> <?php echo fm_t('Mail') ?></a></b> &nbsp;
<b><a href="?p=<?php echo urlencode(FM_PATH) ?>"><i class="fa fa-chevron-circle-left"></i> <?php echo fm_t('Back') ?></a></b>
</p>
<?php
@ -971,14 +1001,15 @@ if (isset($_GET['edit'])) {
?>
<div class="path">
<div class="edit-file-actions">
<a title="Cancel" href="?p=<?php echo urlencode(trim(FM_PATH)) ?>&amp;view=<?php echo urlencode($file) ?>">Cancel</a>
<a title="Cancel" href="?p=<?php echo urlencode(trim(FM_PATH)) ?>&amp;view=<?php echo urlencode($file) ?>"><i class="fa fa-reply-all"></i> Cancel</a>
<a title="Backup" href="javascript:backup('<?php echo urlencode($path) ?>','<?php echo urlencode($file) ?>')"><i class="fa fa-database"></i> Backup</a>
<?php if($is_text) { ?>
<?php if($isNormalEditor) { ?>
<a title="Advanced" href="?p=<?php echo urlencode(trim(FM_PATH)) ?>&amp;edit=<?php echo urlencode($file) ?>&amp;env=ace">Advanced Editor</a>
<button type="button" name="Save" data-url="<?php echo $file_url ?>" onclick="edit_save(this,'nrl')">Save</button>
<a title="Advanced" href="?p=<?php echo urlencode(trim(FM_PATH)) ?>&amp;edit=<?php echo urlencode($file) ?>&amp;env=ace"><i class="fa fa-paper-plane"></i> Advanced Editor</a>
<button type="button" name="Save" data-url="<?php echo $file_url ?>" onclick="edit_save(this,'nrl')"><i class="fa fa-floppy-o"></i> Save</button>
<?php } else { ?>
<a title="Plain Editor" href="?p=<?php echo urlencode(trim(FM_PATH)) ?>&amp;edit=<?php echo urlencode($file) ?>">Plain Editor</a>
<button type="button" name="Save" data-url="<?php echo $file_url ?>" onclick="edit_save(this,'ace')">Save</button>
<a title="Plain Editor" href="?p=<?php echo urlencode(trim(FM_PATH)) ?>&amp;edit=<?php echo urlencode($file) ?>"><i class="fa fa-text-height"></i> Plain Editor</a>
<button type="button" name="Save" data-url="<?php echo $file_url ?>" onclick="edit_save(this,'ace')"><i class="fa fa-floppy-o"></i> Save</button>
<?php } ?>
<?php } ?>
</div>
@ -1471,6 +1502,79 @@ function fm_enc($text)
return htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
}
/**
* This function scans the files folder recursively, and builds a large array
* @param string $dir
* @return json
*/
function scan($dir){
$files = array();
// Is there actually such a folder/file?
if(file_exists($dir)){
foreach(scandir($dir) as $f) {
if(!$f || $f[0] == '.') {
continue; // Ignore hidden files
}
if(is_dir($dir . '/' . $f)) {
// The path is a folder
$files[] = array(
"name" => $f,
"type" => "folder",
"path" => $dir . '/' . $f,
"items" => scan($dir . '/' . $f), // Recursively get the contents of the folder
);
} else {
// It is a file
$files[] = array(
"name" => $f,
"type" => "file",
"path" => $dir . '/' . $f,
"size" => filesize($dir . '/' . $f) // Gets the size of this file
);
}
}
}
return $files;
}
/**
* Send email with file attached
* @param string $msg, $to, $p
*/
function send_mail($path,$filename, $mailto, $message) {
$file = $path.'/'.$filename;
$content = file_get_contents( $file);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$name = basename($file);
// header
$header = "From: H3K File Manager <filemanager@mail.com>\r\n";
$header .= "Reply-To: ".$mailto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
// message & attachment
$subject = "File has attahced";
$nmessage = "--".$uid."\r\n";
$nmessage .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$nmessage .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$nmessage .= $message."\r\n\r\n";
$nmessage .= "--".$uid."\r\n";
$nmessage .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n";
$nmessage .= "Content-Transfer-Encoding: base64\r\n";
$nmessage .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$nmessage .= $content."\r\n\r\n";
$nmessage .= "--".$uid."--";
if (mail($mailto, $subject, $nmessage, $header)) {
return true; // Or do something here
} else {
return false;
}
}
/**
* Save message in session
* @param string $msg
@ -1590,6 +1694,9 @@ function fm_get_file_icon_class($path)
case 'csv':
$img = 'fa fa-file-text-o';
break;
case 'bak':
$img = 'fa fa-clipboard';
break;
case 'doc': case 'docx':
$img = 'fa fa-file-word-o';
break;
@ -1828,6 +1935,8 @@ function fm_show_nav_path($path)
<div class="float-right">
<?php if (!FM_READONLY): ?>
<input type="search" name="search" value="" placeholder="Find a file..." class="hidden">
<a title="<?php echo fm_t('Search') ?>" href="javascript:showSearch('<?php echo urlencode(FM_ROOT_PATH.'/'.FM_PATH) ?>')"><i class="fa fa-search"></i></a>
<a title="<?php echo fm_t('Upload files') ?>" href="?p=<?php echo urlencode(FM_PATH) ?>&amp;upload"><i class="fa fa-cloud-upload" aria-hidden="true"></i></a>
<a title="<?php echo fm_t('New folder') ?>" href="#createNewItem" ><i class="fa fa-plus-square"></i></a>
<?php endif; ?>
@ -1903,7 +2012,7 @@ body {margin:0 30px;margin-top: 45px;}.logo{color:#0df70d;float:left;font-size:2
.preview-video{position:relative;max-width:100%;height:0;padding-bottom:62.5%;margin-bottom:10px}.preview-video video{position:absolute;width:100%;height:100%;left:0;top:0;background:#000}
.compact-table{border:0;width:auto}.compact-table td,.compact-table th{width:100px;border:0;text-align:center}.compact-table tr:hover td{background-color:#fff}
.filename{max-width:420px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.break-word{word-wrap:break-word}.break-word.float-left a{color:#fff}.break-word+.float-right{padding-right:30px;}.break-word+.float-right>a{color:#fff;font-size:1.2em;margin-right:4px;}
.break-word{word-wrap:break-word}.break-word.float-left a{color:#fff}.break-word+.float-right{padding-right:30px;position:relative;}.break-word+.float-right>a{color:#fff;font-size:1.2em;margin-right:4px;}
.modal{display:none;position:fixed;z-index:1;padding-top:100px;left:0;top:0;width:100%;height:100%;overflow:auto;background-color:#000;background-color:rgba(0,0,0,.4)}
.modal-content{background-color:#fefefe;margin:auto;padding:20px;border:1px solid #888;width:80%}
.close{color:#aaa;float:right;font-size:28px;font-weight:700}.close:focus,.close:hover{color:#000;text-decoration:none;cursor:pointer}
@ -1914,17 +2023,14 @@ body {margin:0 30px;margin-top: 45px;}.logo{color:#0df70d;float:left;font-size:2
.main-nav{position:fixed;top:0;left:0;padding:10px 30px;padding-left:1px;width:100%;background:#585858;color:#fff;border:0;}
.login-form {width:320px;text-align:center;margin:0 auto;}
.login-form label,.path.login-form input {padding:5px;margin:10px}.footer-links{background:transparent;border:0}
input[type=search]{position: absolute;height: 26px;right: 100%;top: -4px;margin-right: 10px;width: 200px;border-radius: 2px;border: 0;}
.modalDialog{position:fixed;font-family:Arial,Helvetica,sans-serif;top:0;right:0;bottom:0;left:0;background:rgba(0,0,0,.8);z-index:99999;opacity:0;-webkit-transition:opacity .4s ease-in;-moz-transition:opacity .4s ease-in;transition:opacity .4s ease-in;pointer-events:none}.modalDialog:target{opacity:1;pointer-events:auto}.modalDialog>.model-wrapper{width:400px;position:relative;margin:10% auto;padding:5px 20px 13px;border-radius:5px;background:#fff}.close{background:#fff;color:#000;line-height:25px;position:absolute;right:0;text-align:center;top:0;width:24px;text-decoration:none;font-weight:700;border-radius:0 5px 0 0;font-size:18px}.close:hover{background:#00d9ff}.modalDialog p{line-height:30px}
</style>
</head>
<body>
<div id="wrapper">
<div id="createNewItem" class="modalDialog"><div class="model-wrapper"><a href="#close" title="Close" class="close">X</a><h2>Create New Item</h2><p>
<label for="newfile">Item Type &nbsp; : </label>
<input type="radio" name="newfile" id="newfile" value="file">File <input type="radio" name="newfile" value="folder" checked> Folder<br>
<label for="newfilename">Item Name : </label><input type="text" name="newfilename" id="newfilename" value=""><br>
<input type="submit" name="submit" class="group-btn" value="Create Now" onclick="newfolder('<?php echo fm_enc(FM_PATH) ?>');return false;">
</p></div></div>
<label for="newfile">Item Type &nbsp; : </label><input type="radio" name="newfile" id="newfile" value="file">File <input type="radio" name="newfile" value="folder" checked> Folder<br><label for="newfilename">Item Name : </label><input type="text" name="newfilename" id="newfilename" value=""><br><input type="submit" name="submit" class="group-btn" value="Create Now" onclick="newfolder('<?php echo fm_enc(FM_PATH) ?>');return false;"></p></div></div>
<?php
}
@ -1943,7 +2049,67 @@ function get_checkboxes(){var i=document.getElementsByName('file[]'),a=[];for(va
function select_all(){var l=get_checkboxes();change_checkboxes(l,true);}
function unselect_all(){var l=get_checkboxes();change_checkboxes(l,false);}
function invert_all(){var l=get_checkboxes();change_checkboxes(l);}
function mailto(p,f){
var http=new XMLHttpRequest();
var params="path="+p+"&file="+f+"&type=mail&ajax=true";
http.open("POST", '', true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.onreadystatechange=function(){
if(http.readyState == 4 && http.status == 200){
console.log(http.responseText);
}
}
http.send(params);
}
function showSearch(u){
var http=new XMLHttpRequest();
var params="path="+u+"&type=search&ajax=true";
http.open("POST", '', true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.onreadystatechange=function(){
if(http.readyState == 4 && http.status == 200){
window.searchObj = http.responseText;
document.querySelector('input[type=search]').classList.remove("hidden");
}
}
http.send(params);
}
var searchEl = document.querySelector('input[type=search]');
var timeout = null;
searchEl.onkeyup = function(evt) {
clearTimeout(timeout);
var data = JSON.parse(window.searchObj);
var searchTerms = document.querySelector('input[type=search]').value;
timeout = setTimeout(function () {
var res = getSearchResult(data,searchTerms);
var f1='',f2='';
res.folders.forEach(function (d) { f1 += '<li class="folders"><a href="'+ d.path +'">'+ d.name + '</a></li>'; });
res.files.forEach(function (d) { f2 += '<li class="folders"><a href="'+ d.path +'">'+ d.name + '</a></li>'; });
document.getElementById('createNewItem').innerHTML = '<div class="model-wrapper">'+f1+f2+'</div>';
window.location.hash="#createNewItem"
}, 500);
};
function getSearchResult(data,searchTerms) {
var folders=[],files=[];
data.forEach(function(d){
if(d.type === 'folder') {
getSearchResult(d.items,searchTerms);
if(d.name.toLowerCase().match(searchTerms)) {
folders.push(d);
}
}
else if(d.type === 'file') {
if(d.name.toLowerCase().match(searchTerms)) {
files.push(d);
}
}
});
return {folders: folders, files: files};
}
function checkbox_toggle(){var l=get_checkboxes();l.push(this);change_checkboxes(l);}
function backup(p,f){ var http=new XMLHttpRequest();var params="path="+p+"&file="+f+"&type=backup&ajax=true";http.open("POST", '', true);http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");http.onreadystatechange=function(){if(http.readyState == 4 && http.status == 200){alert(http.responseText);}}
http.send(params); return false; }
function edit_save(i,atr){var contents=(atr=="ace")?editor.getSession().getValue():document.getElementById('normal-editor').value;if(!!contents){var form=document.createElement("form");form.setAttribute("method", 'POST');form.setAttribute("action", '');var inputField=document.createElement("textarea");inputField.setAttribute("type", "textarea");inputField.setAttribute("name", 'savedata');var _temp=document.createTextNode(contents);inputField.appendChild(_temp);form.appendChild(inputField);document.body.appendChild(form);form.submit();}}
</script>
<?php if (isset($_GET['view']) && FM_USE_HIGHLIGHTJS): ?>