Files
VestaCP/web/file_manager/fm_core.php

384 lines
11 KiB
PHP
Raw Normal View History

2015-05-29 02:07:55 +03:00
<?php
class FileManager {
2015-05-29 02:07:55 +03:00
protected $delimeter = '|';
protected $info_positions = array(
'TYPE' => 0,
'PERMISSIONS' => 1,
'DATE' => 2,
'TIME' => 3,
'OWNER' => 4,
'GROUP' => 5,
'SIZE' => 6,
'NAME' => 7
);
2015-05-29 02:07:55 +03:00
protected $user = null;
public $ROOT_DIR = null;
2015-05-29 02:07:55 +03:00
public function setRootDir($root = null) {
if (null != $root) {
$root = realpath($root);
2015-05-29 02:07:55 +03:00
}
$this->ROOT_DIR = $root;
}
2015-05-29 02:07:55 +03:00
public function __construct($user) {
$this->user = $user;
}
2015-05-29 02:07:55 +03:00
/*public function init() {
$path = !empty($_REQUEST['dir']) ? $_REQUEST['dir'] : '';
$start_url = !empty($path) ? $this->ROOT_DIR . '/' . $path : $this->ROOT_DIR;
$listing = $this->getDirectoryListing($path);
2015-05-29 02:07:55 +03:00
return $data = array(
'result' => true,
'ROOT_DIR' => $this->ROOT_DIR,
'TAB_A_PATH' => $start_url,
'TAB_B_PATH' => $this->ROOT_DIR, // second tab always loads home dir
'listing' => $listing
);
}*/
2015-09-10 14:35:17 +03:00
public function checkFileType($dir) {
$dir = $this->formatFullPath($dir);
exec(VESTA_CMD . "v-get-fs-file-type {$this->user} {$dir}", $output, $return_var);
$error = self::check_return_code($return_var, $output);
2015-09-10 14:35:17 +03:00
if (empty($error)) {
return array(
2015-09-18 17:45:03 +03:00
'result' => true,
'data' => implode('', $output)
2015-09-10 14:35:17 +03:00
);
}
else {
2015-09-10 14:35:17 +03:00
return array(
'result' => false,
'message' => $error
);
}
}
2015-05-29 02:07:55 +03:00
public function formatFullPath($path_part = '') {
if (substr($path_part, 0, strlen($this->ROOT_DIR)) === $this->ROOT_DIR) {
$path = $path_part;
}
else {
2015-05-29 02:07:55 +03:00
$path = $this->ROOT_DIR . '/' . $path_part;
}
//var_dump($path);die();
2015-09-07 17:35:40 +03:00
//$path = str_replace(' ', '\ ', $path);
return escapeshellarg($path);
2015-05-29 02:07:55 +03:00
}
2015-09-07 17:35:40 +03:00
function deleteItem($dir, $item) {
$dir = $this->formatFullPath($item);
exec (VESTA_CMD . "v-delete-fs-directory {$this->user} {$dir}", $output, $return_var);
2015-09-07 17:35:40 +03:00
$error = self::check_return_code($return_var, $output);
2015-09-07 17:35:40 +03:00
if (empty($error)) {
return array(
'result' => true
);
}
else {
2015-09-07 17:35:40 +03:00
return array(
'result' => false,
'message' => $error
);
}
2015-09-07 17:35:40 +03:00
/*if (is_readable($item)) {
2015-05-29 02:07:55 +03:00
unlink($item);
}
if (is_readable($item)) {
return array(
'result' => false,
'message' => 'item was not deleted'
);
}
return array(
'result' => true
2015-09-07 17:35:40 +03:00
);*/
2015-05-29 02:07:55 +03:00
}
2015-09-07 17:35:40 +03:00
function copyFile($item, $dir, $target_dir, $filename) {
$src = $this->formatFullPath($item);
$dst = $this->formatFullPath($target_dir);
exec (VESTA_CMD . "v-copy-fs-file {$this->user} {$src} {$dst}", $output, $return_var);
2015-09-07 17:35:40 +03:00
$error = self::check_return_code($return_var, $output);
2015-09-07 17:35:40 +03:00
if (empty($error)) {
return array(
'result' => true
);
}
else {
2015-09-07 17:35:40 +03:00
return array(
'result' => false,
'message' => $error
);
}
}
2015-09-07 17:35:40 +03:00
function copyDirectory($item, $dir, $target_dir, $filename) {
$src = $this->formatFullPath($item);
$dst = $this->formatFullPath($target_dir);
exec (VESTA_CMD . "v-copy-fs-directory {$this->user} {$src} {$dst}", $output, $return_var);
2015-09-07 17:35:40 +03:00
$error = self::check_return_code($return_var, $output);
2015-09-07 17:35:40 +03:00
if (empty($error)) {
2015-05-29 02:07:55 +03:00
return array(
2015-09-07 17:35:40 +03:00
'result' => true
);
}
else {
2015-09-07 17:35:40 +03:00
return array(
'result' => false,
'message' => $error
2015-05-29 02:07:55 +03:00
);
}
2015-09-07 17:35:40 +03:00
}
static function check_return_code($return_var, $output) {
if ($return_var != 0) {
$error = implode('<br>', $output);
return $error;
//if (empty($error)) $error = __('Error code:',$return_var);
//$_SESSION['error_msg'] = $error;
}
return null;
}
2015-05-29 02:07:55 +03:00
function createFile($dir, $filename) {
2015-09-07 17:35:40 +03:00
$dir = $this->formatFullPath($dir . '/' . $filename);
exec (VESTA_CMD . "v-add-fs-file {$this->user} {$dir}", $output, $return_var);
2015-09-07 17:35:40 +03:00
$error = self::check_return_code($return_var, $output);
2015-09-07 17:35:40 +03:00
if (empty($error)) {
return array(
'result' => true
);
}
else {
2015-09-07 17:35:40 +03:00
return array(
'result' => false,
'message' => $error
);
}
}
2015-09-07 17:35:40 +03:00
function packItem($item, $dir, $target_dir, $filename) {
$item = $this->formatFullPath($item);
$dst_item = $this->formatFullPath($target_dir);
2015-11-05 03:17:21 +02:00
$dst_item = str_replace('.tar.gz', '', $dst_item);
2015-11-05 03:17:21 +02:00
//$item = str_replace($dir . '/', '', $item);
//var_dump(VESTA_CMD . "v-add-fs-archive {$this->user} {$dst_item} {$item}");die();
exec (VESTA_CMD . "v-add-fs-archive {$this->user} {$dst_item} {$item}", $output, $return_var);
2015-09-07 17:35:40 +03:00
$error = self::check_return_code($return_var, $output);
2015-09-07 17:35:40 +03:00
if (empty($error)) {
2015-05-29 02:07:55 +03:00
return array(
2015-09-07 17:35:40 +03:00
'result' => true
2015-05-29 02:07:55 +03:00
);
}
else {
2015-09-07 17:35:40 +03:00
return array(
'result' => false,
'message' => $error
);
}
}
2015-10-05 23:32:25 +03:00
function backupItem($item) {
2015-10-05 23:32:25 +03:00
$src_item = $this->formatFullPath($item);
2015-10-05 23:32:25 +03:00
$dst_item_name = $item . '~' . date('Ymd_His');
2015-10-05 23:32:25 +03:00
$dst_item = $this->formatFullPath($dst_item_name);
//print VESTA_CMD . "v-add-fs-archive {$this->user} {$item} {$dst_item}";die();
exec (VESTA_CMD . "v-copy-fs-file {$this->user} {$src_item} {$dst_item}", $output, $return_var);
2015-10-05 23:32:25 +03:00
$error = self::check_return_code($return_var, $output);
2015-10-05 23:32:25 +03:00
if (empty($error)) {
return array(
'result' => true,
'filename' => $dst_item_name
);
}
else {
2015-10-05 23:32:25 +03:00
return array(
'result' => false,
'message' => $error
);
}
$error = self::check_return_code($return_var, $output);
if (empty($error)) {
return array(
'result' => true
);
}
else {
return array(
'result' => false,
'message' => $error
);
}
}
2015-09-07 17:35:40 +03:00
function unpackItem($item, $dir, $target_dir, $filename) {
$item = $this->formatFullPath($item);
$dst_item = $this->formatFullPath($target_dir);
exec (VESTA_CMD . "v-extract-fs-archive {$this->user} {$item} {$dst_item}", $output, $return_var);
2015-09-07 17:35:40 +03:00
$error = self::check_return_code($return_var, $output);
2015-09-07 17:35:40 +03:00
if (empty($error)) {
return array(
'result' => true
);
}
else {
2015-09-07 17:35:40 +03:00
return array(
'result' => false,
'message' => $error
);
}
2015-05-29 02:07:55 +03:00
}
2015-09-07 17:35:40 +03:00
function renameFile($dir, $item, $target_name) {
$item = $this->formatFullPath($dir . '/' . $item);
$dst_item = $this->formatFullPath($dir . '/' . $target_name);
// var_dump(VESTA_CMD . "v-move-fs-file {$this->user} {$item} {$dst_item}");die();
2015-09-07 17:35:40 +03:00
exec (VESTA_CMD . "v-move-fs-file {$this->user} {$item} {$dst_item}", $output, $return_var);
2015-09-07 17:35:40 +03:00
$error = self::check_return_code($return_var, $output);
2015-09-07 17:35:40 +03:00
if (empty($error)) {
return array(
'result' => true
);
}
else {
2015-09-07 17:35:40 +03:00
return array(
'result' => false,
'message' => $error
);
2015-05-29 02:07:55 +03:00
}
2015-09-07 17:35:40 +03:00
}
function renameDirectory($dir, $item, $target_name) {
$item = $this->formatFullPath($dir . $item);
$dst_item = $this->formatFullPath($dir . $target_name);
if ($item == $dst_item) {
2015-05-29 02:07:55 +03:00
return array(
2015-09-07 17:35:40 +03:00
'result' => true
2015-05-29 02:07:55 +03:00
);
}
2015-09-07 17:35:40 +03:00
exec (VESTA_CMD . "v-move-fs-directory {$this->user} {$item} {$dst_item}", $output, $return_var);
$error = self::check_return_code($return_var, $output);
2015-09-07 17:35:40 +03:00
if (empty($error)) {
return array(
'result' => true
);
}
else {
2015-09-07 17:35:40 +03:00
return array(
'result' => false,
'message' => $error
);
}
2015-05-29 02:07:55 +03:00
}
2015-05-29 02:07:55 +03:00
function createDir($dir, $dirname) {
2015-09-07 17:35:40 +03:00
$dir = $this->formatFullPath($dir . '/' . $dirname);
exec (VESTA_CMD . "v-add-fs-directory {$this->user} {$dir}", $output, $return_var);
2015-05-29 02:07:55 +03:00
$error = self::check_return_code($return_var, $output);
2015-09-07 17:35:40 +03:00
if (empty($error)) {
2015-05-29 02:07:55 +03:00
return array(
2015-09-07 17:35:40 +03:00
'result' => true
);
}
else {
2015-09-07 17:35:40 +03:00
return array(
'result' => false,
'message' => $error
2015-05-29 02:07:55 +03:00
);
}
}
2015-05-29 02:07:55 +03:00
function getDirectoryListing($dir = '') {
$dir = $this->formatFullPath($dir);
exec (VESTA_CMD . "v-list-fs-directory {$this->user} {$dir}", $output, $return_var);
return $this->parseListing($output);
}
2015-05-29 02:07:55 +03:00
public function ls($dir = '') {
$listing = $this->getDirectoryListing($dir);
return $data = array(
'result' => true,
'listing' => $listing
);
}
2015-05-29 02:07:55 +03:00
public function open_file($dir = '') {
$listing = $this->getDirectoryListing($dir);
return $data = array(
'result' => true,
'listing' => $listing
);
}
2015-05-29 02:07:55 +03:00
public function parseListing($raw) {
$data = array();
foreach ($raw as $o) {
$info = explode($this->delimeter, $o);
$data[] = array(
'type' => $info[$this->info_positions['TYPE']],
'permissions' => $info[$this->info_positions['PERMISSIONS']],
'date' => $info[$this->info_positions['DATE']],
'time' => $info[$this->info_positions['TIME']],
'owner' => $info[$this->info_positions['OWNER']],
'group' => $info[$this->info_positions['GROUP']],
'size' => $info[$this->info_positions['SIZE']],
'name' => $info[$this->info_positions['NAME']]
);
}
2015-05-29 02:07:55 +03:00
return $data;
}
}