<?php
/*
Plugin Name: QA File Manager
Plugin URI: www.lazyanduseless.belike.net/wp/?p=37
Plugin Description: A simple file manager that allows you to create, edit, delete files or folders online without having to use any FTP Client. To configure this plugin, go to Options page. For advance options, open this plugin using Plugin Editor.
Version: 1.0
Author: Quang Anh Do
Author URI: www.lazyanduseless.belike.net
Copyright 2005 Quang Anh Do (email : quanganhdo@gmail.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
TO DO LIST: (ordered by priority)
1. Add the ability to change Editable & Protected file types in Options Page
2. Add Upload Function
3. Add Chmod Function
4. Who knows?
*/
$protected = array("txt", "php", "js", "html", "xml"); // Files with these extensions CAN'T be deleted using QA File Manager
$editable = array("txt", "php", "js", "html", "xml"); // File with these extensions is EDITABLE
// Display a folder row using given background color
function qafm_display_folder($currentroot, $currentdir, $bgcolor = "") {
echo "<tr>";
echo "<td bgcolor="$bgcolor"><a href="../wp-admin/index.php?page=qafileman.php&dir=$currentroot$currentdir" title="Explore $currentdir">$currentdir/</a><br></td>";
echo "<td bgcolor="$bgcolor"></td>";
echo "<td bgcolor="$bgcolor" align="center">".decoct(fileperms($currentdir))."</td>";
echo "<td bgcolor="$bgcolor"></td>";
echo "<td bgcolor="$bgcolor" align="center"></td>";
echo "</tr>";
}
// Check if a file is editable or not
function qafm_editable($filename) {
global $editable;
$dot = strpos($filename, ".");
$extension = "";
for ($i = $dot + 1; $i <= strlen($filename); $i ++) {
$extension .= $filename[$i];
}
if (in_array($extension, $editable)) {
return true;
} else {
return false;
}
}
// Check if a file is protected or not
function qafm_protected($filename) {
global $protected;
$dot = strpos($filename, ".");
$extension = "";
for ($i = $dot + 1; $i <= strlen($filename); $i ++) {
$extension .= $filename[$i];
}
if (in_array($extension, $protected)) {
return true;
} else {
return false;
}
}
// Display a file row using given background color
function qafm_display_file($currentroot, $currentfile, $bgcolor = "") {
echo "<tr>";
echo "<td bgcolor="$bgcolor">$currentfile<br></td>";
echo "<td bgcolor="$bgcolor" align="center">".filesize($currentfile)." Bytes</td>";
echo "<td bgcolor="$bgcolor" align="center">".decoct(fileperms($currentfile))."</td>";
$temp = "";
if (qafm_editable($currentfile)) {
$temp = "<a href="../wp-admin/templates.php?file=$currentroot$currentfile" title="Edit $currentfile">Edit</a>";
}
echo "<td bgcolor="$bgcolor" align="center">$temp</td>";
$temp = "";
if (!qafm_protected($currentfile)) {
$temp = "<a href="../wp-admin/index.php?page=qafileman.php&kill=$currentroot$currentfile" title="Delete $currentfile">Delete</a>";
}
echo "<td bgcolor="$bgcolor" align="center">$temp</td>";
echo "</tr>";
}
// Main function
function qafm_fileman() {
// Get option
$defbgcolor = "#CCCCCC";
$currentroot = "";
$path = get_option('qafm_path');
if ($_GET['dir']) {
$path .= $_GET['dir'];
$currentroot .= $_GET['dir']."/";
}
// Create a new file
if ($_POST['mkfile'] != "") {
touch($path."/".$_POST['mkfile']);
echo "<div class="updated"><p>".$_POST['mkfile']." has been created successfully.</p></div>";
}
// Create a new directory
if ($_POST['mkdir'] != "") {
mkdir($path."/".$_POST['mkdir']);
echo "<div class="updated"><p>".$_POST['mkdir']."/ has been created successfully.</p></div>";
}
?>
<div class="wrap">
<h2>QA File Manager</h2>
<fieldset class="options">
<legend><b>Current directory: </b><?php echo $path; ?></legend>
<form action="<?php echo $_SERVER[PHP_SELF]; ?>" method="GET">
<table>
<tr>
<th width="50%" align="left"><b>Name</b></th>
<th width="10%" align="center"><b>Size</b></th>
<th width="20%" align="center"><b>Permission</b></th>
<th colspan="2" width="20%" align="center"><b>Actions</b></th>
</tr>
<?php
// Open the directory
$handle = opendir($path);
chdir($path);
// If a 'kill' action has been called, process it
if ($_GET['kill']) {
if (!qafm_protected($_GET['kill'])){
if (file_exists($_GET['kill'])) {
unlink($_GET['kill']);
echo "<div class="updated"><p>".$_GET['kill']." has been removed.<br>Redirected to Home directory.</p></div>";
} else {
echo "<div class="updated"><p>".$_GET['kill']." doesn't exist. Why didn't you try to remove it? ;-)</p></div>";
}
} else {
echo "<div class="updated"><p>Sorry, this file is protected. You can't remove it using QA File Manager.</p></div>";
}
}
// Fetch the file list into 2 arrays
while (($currentfile = readdir($handle))!== false) {
if (is_dir($currentfile)) {
$dirs[] = $currentfile;
} else {
$files[] = $currentfile;
}
}
// 'Up one level' feature
if ($currentroot != "") {
$temp = substr($currentroot, 0, strlen($currentroot) - 2);
$lastslash = strrpos($temp, "/");
$temp = substr($temp, 0, $lastslash);
echo "<tr>";
echo "<td><a href="../wp-admin/index.php?page=qafileman.php&dir=$temp">Up one level</a></td>";
for ($i = 1; $i <= 4; $i ++) {
echo "<td></td>";
}
echo "</tr>";
}
// Display the main table
$count = 0;
foreach ($dirs as $currentdir) {
$count ++;
if (($currentdir != ".") && ($currentdir != "..")) {
if ($count % 2 == 1) {
qafm_display_folder($currentroot, $currentdir, $defbgcolor);
} else {
qafm_display_folder($currentroot, $currentdir);
}
}
}
if (count($files) != 0) {
foreach ($files as $currentfile) {
$count ++;
if ($count % 2 == 1) {
qafm_display_file($currentroot, $currentfile, $defbgcolor);
} else {
qafm_display_file($currentroot, $currentfile);
}
}
}
?>
</form>
</table>
</fieldset>
<fieldset class="options">
<legend>More Actions</legend>
<form action="<?php echo $_SERVER[REQUEST_URI]; ?>" method="POST">
<table>
<tr valign="top">
<th align="right" scope="row">Create a new file:</th>
<td><input type="text" name="mkfile"><br><br></td>
</tr>
<tr valign="top">
<th align="right" scope="row">Create a new directory:</th>
<td><input type="text" name="mkdir"><br><br></td>
</tr>
</table>
<p class="submit"><input type="submit" value="Process"></p>
</fieldset>
</form>
</div>
<?php
}
function qafm_fileman_options() {
if ($_POST['qafm_path']) {
update_option('qafm_path', $_POST['qafm_path']);
}
?>
<div class = "wrap">
<h2>QA File Manager Options</h2>
<form method="post" id="qafm_fileman_options">
<fieldset class="options">
<legend>Preferences</legend>
<table>
<tr align="left" valign="top">
<th scope="row">Default path:</th>
<td><input name="qafm_path" type="text" id="qafm_path" value="<?php echo get_option('qafm_path') ;?>" size="20"/>
<br/>Your WordPress folder, or any subfolder (at your option), followed by a slash. E.g "../"</td>
</tr>
</table>
<p class="submit"><input type="submit" name="qa_update" value="Update" /></p>
</fieldset>
</form>
</div>
<?php
}
// Add these things into Admin Panel
function qafm_add_submenu() {
add_submenu_page('index.php', ' QA File Manager ', 'QA File Manager', 8, __FILE__, 'qafm_fileman');
add_options_page('QA File Manager Options', 'QA File Manager Options', 8, __FILE__, 'qafm_fileman_options');
}
add_action('admin_menu', 'qafm_add_submenu');
?>