true,
'new_file' => true,
'upload_file' => true,
'show_dir_size' => false,
'show_img' => true,
'show_php_ver' => true,
'show_php_ini' => false,
'show_gt' => true,
'enable_php_console' => true,
'enable_sql_console' => true,
'sql_server' => 'localhost',
'sql_username' => 'root',
'sql_password' => '',
'sql_db' => 'test_base',
'enable_proxy' => false,
'show_phpinfo' => true,
'show_xls' => true,
'fm_settings' => true,
'restore_time' => true,
'fm_restore_time' => false,
);
// Load user configuration
if (empty($_COOKIE['fm_config'])) {
$fm_config = $fm_default_config;
} else {
$fm_config = unserialize($_COOKIE['fm_config']);
if (!is_array($fm_config)) {
$fm_config = $fm_default_config;
}
}
// =============================================
// LANGUAGE DETECTION
// =============================================
// Change language from POST
if (isset($_POST['fm_lang'])) {
setcookie('fm_lang', $_POST['fm_lang'], time() + (86400 * $auth['days_authorization']));
$_COOKIE['fm_lang'] = $_POST['fm_lang'];
}
$language = $default_language;
// Detect browser language
if ($detect_lang && !empty($_SERVER['HTTP_ACCEPT_LANGUAGE']) && empty($_COOKIE['fm_lang'])) {
$lang_priority = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
if (!empty($lang_priority)) {
foreach ($lang_priority as $lang_arr) {
$lng = explode(';', $lang_arr);
$lng = substr($lng[0], 0, 2);
if (in_array($lng, $langs)) {
$language = $lng;
break;
}
}
}
}
// Use cookie language if set
$language = (empty($_COOKIE['fm_lang'])) ? $language : $_COOKIE['fm_lang'];
// =============================================
// CORE FUNCTIONS
// =============================================
/**
* Translation function
*/
function __($text) {
global $lang;
return isset($lang[$text]) ? $lang[$text] : $text;
}
/**
* Delete files and directories recursively
*/
function fm_del_files($file, $recursive = false) {
if ($recursive && @is_dir($file)) {
$els = fm_scan_dir($file, '', '', true);
foreach ($els as $el) {
if ($el != '.' && $el != '..') {
fm_del_files($file . '/' . $el, true);
}
}
}
if (@is_dir($file)) {
return rmdir($file);
} else {
return @unlink($file);
}
}
/**
* Get file permissions string
*/
function fm_rights_string($file, $if = false) {
$perms = fileperms($file);
$info = '';
if (!$if) {
if (($perms & 0xC000) == 0xC000) {
$info = 's'; // Socket
} elseif (($perms & 0xA000) == 0xA000) {
$info = 'l'; // Symbolic Link
} elseif (($perms & 0x8000) == 0x8000) {
$info = '-'; // Regular
} elseif (($perms & 0x6000) == 0x6000) {
$info = 'b'; // Block special
} elseif (($perms & 0x4000) == 0x4000) {
$info = 'd'; // Directory
} elseif (($perms & 0x2000) == 0x2000) {
$info = 'c'; // Character special
} elseif (($perms & 0x1000) == 0x1000) {
$info = 'p'; // FIFO pipe
} else {
$info = 'u'; // Unknown
}
}
// Owner
$info .= (($perms & 0x0100) ? 'r' : '-');
$info .= (($perms & 0x0080) ? 'w' : '-');
$info .= (($perms & 0x0040) ? (($perms & 0x0800) ? 's' : 'x') : (($perms & 0x0800) ? 'S' : '-'));
// Group
$info .= (($perms & 0x0020) ? 'r' : '-');
$info .= (($perms & 0x0010) ? 'w' : '-');
$info .= (($perms & 0x0008) ? (($perms & 0x0400) ? 's' : 'x') : (($perms & 0x0400) ? 'S' : '-'));
// World
$info .= (($perms & 0x0004) ? 'r' : '-');
$info .= (($perms & 0x0002) ? 'w' : '-');
$info .= (($perms & 0x0001) ? (($perms & 0x0200) ? 't' : 'x') : (($perms & 0x0200) ? 'T' : '-'));
return $info;
}
/**
* Convert rights string to octal
*/
function fm_convert_rights($mode) {
$mode = str_pad($mode, 9, '-');
$trans = array('-' => '0', 'r' => '4', 'w' => '2', 'x' => '1');
$mode = strtr($mode, $trans);
$newmode = '0';
$owner = (int)$mode[0] + (int)$mode[1] + (int)$mode[2];
$group = (int)$mode[3] + (int)$mode[4] + (int)$mode[5];
$world = (int)$mode[6] + (int)$mode[7] + (int)$mode[8];
$newmode .= $owner . $group . $world;
return intval($newmode, 8);
}
/**
* Change file permissions
*/
function fm_chmod($file, $val, $rec = false) {
$res = @chmod(realpath($file), $val);
if (@is_dir($file) && $rec) {
$els = fm_scan_dir($file);
foreach ($els as $el) {
$res = $res && fm_chmod($file . '/' . $el, $val, true);
}
}
return $res;
}
/**
* Download file
*/
function fm_download($file_name) {
if (!empty($file_name) && file_exists($file_name)) {
header("Content-Disposition: attachment; filename=" . basename($file_name));
header("Content-Type: application/octet-stream");
header("Content-Length: " . filesize($file_name));
readfile($file_name);
exit;
} else {
header('HTTP/1.0 404 Not Found', true, 404);
exit;
}
}
/**
* Calculate directory size
*/
function fm_dir_size($f, $format = true) {
if (is_file($f)) {
$size = filesize($f);
} else {
$size = 0;
$dh = opendir($f);
while (($file = readdir($dh)) !== false) {
if ($file == '.' || $file == '..') continue;
$filepath = $f . '/' . $file;
$size += is_file($filepath) ? filesize($filepath) : fm_dir_size($filepath, false);
}
closedir($dh);
}
if (!$format) return $size;
if ($size == 0) return '0 bytes';
$units = array('bytes', 'KB', 'MB', 'GB', 'TB');
$i = floor(log($size, 1024));
return round($size / pow(1024, $i), 2) . ' ' . $units[$i];
}
/**
* Scan directory
*/
function fm_scan_dir($directory, $exp = '', $type = 'all', $do_not_filter = false) {
$dir = array();
if (!empty($exp)) {
$exp = '/^' . str_replace('*', '(.*)', str_replace('.', '\\.', $exp)) . '$/';
}
if (!empty($type) && $type !== 'all') {
$func = 'is_' . $type;
}
if (@is_dir($directory)) {
$fh = opendir($directory);
while (false !== ($filename = readdir($fh))) {
if (substr($filename, 0, 1) != '.' || $do_not_filter) {
if ((empty($type) || $type == 'all' || $func($directory . '/' . $filename)) &&
(empty($exp) || preg_match($exp, $filename))) {
$dir[] = $filename;
}
}
}
closedir($fh);
natsort($dir);
}
return $dir;
}
/**
* Create navigation link
*/
function fm_link($get, $link, $name, $title = '') {
if (empty($title)) $title = $name . ' ' . basename($link);
return ' ' . $name . '';
}
/**
* Language selection form
*/
function fm_lang_form($current = 'en') {
$languages = array(
'en' => __('English'),
'de' => __('German'),
'ru' => __('Russian'),
'fr' => __('French'),
'uk' => __('Ukrainian')
);
$options = '';
foreach ($languages as $code => $name) {
$selected = ($current == $code) ? 'selected="selected"' : '';
$options .= '';
}
return '
';
}
/**
* Check if directory is root
*/
function fm_root($dirname) {
return ($dirname == '.' || $dirname == '..');
}
/**
* Execute PHP code
*/
function fm_php($string) {
$display_errors = ini_get('display_errors');
ini_set('display_errors', '1');
ob_start();
eval(trim($string));
$text = ob_get_contents();
ob_end_clean();
ini_set('display_errors', $display_errors);
return $text;
}
/**
* SQL Connection
*/
function fm_sql_connect() {
global $fm_config;
return new mysqli($fm_config['sql_server'], $fm_config['sql_username'], $fm_config['sql_password'], $fm_config['sql_db']);
}
/**
* Execute SQL query
*/
function fm_sql($query) {
$connection = fm_sql_connect();
if ($connection->connect_error) {
return $connection->connect_error;
}
$connection->set_charset('utf8');
$result = $connection->query($query);
if ($result === false) {
return mysqli_error($connection);
}
$output = '';
if ($result instanceof mysqli_result) {
$output .= '';
$first = true;
while ($row = $result->fetch_assoc()) {
if ($first) {
$output .= '';
foreach (array_keys($row) as $key) {
$output .= '| ' . htmlspecialchars($key) . ' | ';
}
$output .= '
';
$first = false;
}
$output .= '';
foreach ($row as $value) {
$output .= '| ' . htmlspecialchars($value) . ' | ';
}
$output .= '
';
}
$output .= '
';
$result->free();
} else {
$output = __('Query executed successfully');
}
$connection->close();
return $output;
}
/**
* Get image link
*/
function fm_img_link($filename) {
return './' . basename(__FILE__) . '?img=' . base64_encode($filename);
}
/**
* Home button style
*/
function fm_home_style() {
return '
.home {
background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAAdgAAAHYBTnsmCAAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAAFYSURBVDiNpZM9SwNBEIafg4iFhY2N2FhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFjY2Ih/YGFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWHxX+zu3eY0kA8vszO7O/PO7M4q/keU/6vUfQ1AKaUBWoA2YAA4gA/gBXi11l5lBpRSOgA9wCgwDgwC3UAn0A40A1XgG3gC7oBL4Nxae5sCaK31gCFgCpgFJoA+oA1oARqBBkADv4ACvoBH4AY4A46stZcOoLXWAkaAeWABmAR6gVagCWhQShml1K9S6gdQgAJegGvgBDiw1l44gNZaD5gBloB5YALoAdqAJqWUVkr9KKW0UsoopX6VUt/AM3ALnAL71tpzB9Ba6wPzwAqwCIwD3UAr0KyU0kqpL6WUVkr9KKW+HIgH4Ao4BPastRcOoLXWBxaBVWABGAO6gBagUSn0opT6UEp9KKU+lFIfSqkP4AW4A06APWvtuQNorQ2AJWAVmANGgS6gBWhUSn0opd6VUu9KqXel1LtS6g14BW6BE2DPWnvmAFprA2AZWAPmgFGgE2gGGpRS70qpd6XUm1LqTSn1ppR6A96AO+AE2LXWnjmA1toAWAHWgTlgBOgAmoAGpdSbUupNKfWqlHpVSr0qpd6Bd+AOOAZ2rbVnDvAHs5xZ3Cz7j5AAAAAASUVORK5CYII=");
background-repeat: no-repeat;
display: inline-block;
width: 16px;
height: 16px;
vertical-align: middle;
}';
}
/**
* Get site URL
*/
function fm_site_url() {
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? "https" : "http";
return $protocol . "://" . $_SERVER['HTTP_HOST'];
}
/**
* Get file manager URL
*/
function fm_url($full = false) {
$host = $full ? fm_site_url() : '.';
return $host . '/' . basename(__FILE__);
}
/**
* Home link
*/
function fm_home($full = false) {
return ' ';
}
// =============================================
// AUTHORIZATION CHECK
// =============================================
if ($auth['authorize']) {
if (isset($_POST['login']) && isset($_POST['password'])) {
if ($_POST['login'] == $auth['login'] && $_POST['password'] == $auth['password']) {
setcookie($auth['cookie_name'], $auth['login'] . '|' . md5($auth['password']), time() + (86400 * $auth['days_authorization']));
$_COOKIE[$auth['cookie_name']] = $auth['login'] . '|' . md5($auth['password']);
}
}
if (!isset($_COOKIE[$auth['cookie_name']]) || $_COOKIE[$auth['cookie_name']] != $auth['login'] . '|' . md5($auth['password'])) {
echo '
' . __('File manager') . '
';
exit;
}
if (isset($_POST['quit'])) {
setcookie($auth['cookie_name'], '', time() - 3600);
header('Location: ' . fm_site_url() . $_SERVER['REQUEST_URI']);
exit;
}
}
// =============================================
// MAIN REQUEST HANDLING
// =============================================
// Handle file download
if (isset($_GET['download'])) {
fm_download(base64_decode($_GET['download']));
}
// Handle image display
if (isset($_GET['img'])) {
$file = base64_decode($_GET['img']);
if ($info = getimagesize($file)) {
switch ($info[2]) {
case 1: $ext = 'gif'; break;
case 2: $ext = 'jpeg'; break;
case 3: $ext = 'png'; break;
case 6: $ext = 'bmp'; break;
default: exit;
}
header("Content-type: image/$ext");
readfile($file);
exit;
}
}
// Show PHP info
if (isset($_GET['phpinfo'])) {
phpinfo();
exit;
}
// =============================================
// EDIT FILE HANDLING
// =============================================
// Handle file editing
if (isset($_GET['edit'])) {
$file_to_edit = $path . $_GET['edit'];
$file_content = '';
if (file_exists($file_to_edit) && is_file($file_to_edit)) {
$file_content = file_get_contents($file_to_edit);
}
// Handle form submission for saving edited file
if (isset($_POST['file_content']) && isset($_POST['filename'])) {
$new_content = $_POST['file_content'];
$filename = $path . $_POST['filename'];
if (file_put_contents($filename, $new_content)) {
$msg = __('File updated');
echo '';
} else {
$msg = __('Error occurred');
}
}
// Display edit form
echo '
' . __('Edit') . ' - ' . htmlspecialchars($_GET['edit']) . '
';
exit;
}
// =============================================
// HTML OUTPUT
// =============================================
?>
= __('File manager') ?>
= fm_home() ?> | Version = $fm_version ?> |
PHP = phpversion() ?> |
= __('Generation time') ?>: = round(microtime(true) - $starttime, 2) ?>s |
phpinfo() |
= __('Settings') ?>
location.reload();';
}
}
if (isset($_POST['mkdir']) && $fm_config['make_directory'] && !empty($_POST['dirname'])) {
mkdir($path . $_POST['dirname'], 0755);
echo '';
}
if (isset($_POST['mkfile']) && $fm_config['new_file'] && !empty($_POST['filename'])) {
file_put_contents($path . $_POST['filename'], '');
echo '';
}
if (isset($_GET['delete']) && !fm_root($_GET['delete'])) {
fm_del_files($path . $_GET['delete'], true);
echo '';
}
?>