fm_core.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <?php
  2. require_once(__DIR__.'/../inc/exec.php');
  3. class FileManager {
  4. protected $delimeter = '|';
  5. protected $info_positions = array(
  6. 'TYPE' => 0,
  7. 'PERMISSIONS' => 1,
  8. 'DATE' => 2,
  9. 'TIME' => 3,
  10. 'OWNER' => 4,
  11. 'GROUP' => 5,
  12. 'SIZE' => 6,
  13. 'NAME' => 7
  14. );
  15. protected $user = null;
  16. public $ROOT_DIR = null;
  17. static function v_exec($command, array $arguments=[], $checkReturn=true, &$output=null) {
  18. $output = '';
  19. $return_var = v_exec($command, $arguments, false, $output);
  20. return $checkReturn ? self::check_return_code($return_var, explode("\n", $output)) : null;
  21. }
  22. static function check_return_code($return_var, $output) {
  23. if ($return_var != 0) {
  24. $error = implode('<br>', $output);
  25. return $error;
  26. //if (empty($error)) $error = __('Error code:',$return_var);
  27. //$_SESSION['error_msg'] = $error;
  28. }
  29. return null;
  30. }
  31. public function setRootDir($root = null) {
  32. if (null != $root) {
  33. $root = realpath($root);
  34. }
  35. $this->ROOT_DIR = $root;
  36. }
  37. public function __construct($user) {
  38. $this->user = $user;
  39. }
  40. /*public function init() {
  41. $path = !empty($_REQUEST['dir']) ? $_REQUEST['dir'] : '';
  42. $start_url = !empty($path) ? $this->ROOT_DIR . '/' . $path : $this->ROOT_DIR;
  43. $listing = $this->getDirectoryListing($path);
  44. return $data = array(
  45. 'result' => true,
  46. 'ROOT_DIR' => $this->ROOT_DIR,
  47. 'TAB_A_PATH' => $start_url,
  48. 'TAB_B_PATH' => $this->ROOT_DIR, // second tab always loads home dir
  49. 'listing' => $listing
  50. );
  51. }*/
  52. public function checkFileType($dir) {
  53. $dir = $this->formatFullPath($dir);
  54. $error = self::v_exec('v-get-fs-file-type', [$this->user, $dir]);
  55. if (empty($error)) {
  56. return array(
  57. 'result' => true,
  58. 'data' => implode('', $output)
  59. );
  60. } else {
  61. return array(
  62. 'result' => false,
  63. 'message' => $error
  64. );
  65. }
  66. }
  67. public function formatFullPath($path_part = '') {
  68. if (substr($path_part, 0, strlen($this->ROOT_DIR)) === $this->ROOT_DIR) {
  69. $path = $path_part;
  70. } else {
  71. $path = $this->ROOT_DIR . '/' . $path_part;
  72. }
  73. //var_dump($path);die();
  74. //$path = str_replace(' ', '\ ', $path);
  75. return $path;
  76. }
  77. function deleteItem($dir, $item) {
  78. $dir = $this->formatFullPath($item);
  79. $error = self::v_exec('v-delete-fs-directory', [$this->user, $dir]);
  80. if (empty($error)) {
  81. return array(
  82. 'result' => true
  83. );
  84. } else {
  85. return array(
  86. 'result' => false,
  87. 'message' => $error
  88. );
  89. }
  90. /*if (is_readable($item)) {
  91. unlink($item);
  92. }
  93. if (is_readable($item)) {
  94. return array(
  95. 'result' => false,
  96. 'message' => 'item was not deleted'
  97. );
  98. }
  99. return array(
  100. 'result' => true
  101. );*/
  102. }
  103. function copyFile($item, $dir, $target_dir, $filename) {
  104. $src = $this->formatFullPath($item);
  105. $dst = $this->formatFullPath($target_dir);
  106. $error = self::v_exec('v-copy-fs-file', [$this->user, $src, $dst]);
  107. if (empty($error)) {
  108. return array(
  109. 'result' => true
  110. );
  111. } else {
  112. return array(
  113. 'result' => false,
  114. 'message' => $error
  115. );
  116. }
  117. }
  118. function copyDirectory($item, $dir, $target_dir, $filename) {
  119. $src = $this->formatFullPath($item);
  120. $dst = $this->formatFullPath($target_dir);
  121. $error = self::v_exec('v-copy-fs-directory', [$this->user, $src, $dst]);
  122. if (empty($error)) {
  123. return array(
  124. 'result' => true
  125. );
  126. } else {
  127. return array(
  128. 'result' => false,
  129. 'message' => $error
  130. );
  131. }
  132. }
  133. function createFile($dir, $filename) {
  134. $dir = $this->formatFullPath($dir . '/' . $filename);
  135. $error = self::v_exec('v-add-fs-file', [$this->user, $dir]);
  136. if (empty($error)) {
  137. return array(
  138. 'result' => true
  139. );
  140. } else {
  141. return array(
  142. 'result' => false,
  143. 'message' => $error
  144. );
  145. }
  146. }
  147. function packItem($item, $dir, $target_dir, $filename) {
  148. $item = $this->formatFullPath($item);
  149. $dst_item = $this->formatFullPath($target_dir);
  150. $dst_item = str_replace('.tar.gz', '', $dst_item);
  151. //$item = str_replace($dir . '/', '', $item);
  152. //var_dump(VESTA_CMD . "v-add-fs-archive {$this->user} {$dst_item} {$item}");die();
  153. $error = self::v_exec('v-add-fs-archive', [$this->user, $dst_item, $item]);
  154. if (empty($error)) {
  155. return array(
  156. 'result' => true
  157. );
  158. } else {
  159. return array(
  160. 'result' => false,
  161. 'message' => $error
  162. );
  163. }
  164. }
  165. function backupItem($item) {
  166. $src_item = $this->formatFullPath($item);
  167. $dst_item_name = $item . '~' . date('Ymd_His');
  168. $dst_item = $this->formatFullPath($dst_item_name);
  169. //print VESTA_CMD . "v-add-fs-archive {$this->user} {$item} {$dst_item}";die();
  170. $error = self::v_exec('v-copy-fs-file', [$this->user, $src_item, $dst_item]);
  171. if (empty($error)) {
  172. return array(
  173. 'result' => true,
  174. 'filename' => $dst_item_name
  175. );
  176. } else {
  177. return array(
  178. 'result' => false,
  179. 'message' => $error
  180. );
  181. }
  182. }
  183. function unpackItem($item, $dir, $target_dir, $filename) {
  184. $item = $this->formatFullPath($item);
  185. $dst_item = $this->formatFullPath($target_dir);
  186. $error = self::v_exec('v-extract-fs-archive', [$this->user, $item, $dst_item]);
  187. if (empty($error)) {
  188. return array(
  189. 'result' => true
  190. );
  191. } else {
  192. return array(
  193. 'result' => false,
  194. 'message' => $error
  195. );
  196. }
  197. }
  198. function renameFile($dir, $item, $target_name) {
  199. $item = $this->formatFullPath($dir . '/' . $item);
  200. $dst_item = $this->formatFullPath($dir . '/' . $target_name);
  201. //var_dump(VESTA_CMD . "v-move-fs-file {$this->user} {$item} {$dst_item}");die();
  202. $error = self::v_exec('v-move-fs-file', [$this->user, $item, $dst_item]);
  203. if (empty($error)) {
  204. return array(
  205. 'result' => true
  206. );
  207. } else {
  208. return array(
  209. 'result' => false,
  210. 'message' => $error
  211. );
  212. }
  213. }
  214. function renameDirectory($dir, $item, $target_name) {
  215. $item = $this->formatFullPath($dir . $item);
  216. $dst_item = $this->formatFullPath($dir . $target_name);
  217. if ($item == $dst_item) {
  218. return array(
  219. 'result' => true
  220. );
  221. }
  222. $error = self::v_exec('v-move-fs-directory', [$this->user, $item, $dst_item]);
  223. if (empty($error)) {
  224. return array(
  225. 'result' => true
  226. );
  227. } else {
  228. return array(
  229. 'result' => false,
  230. 'message' => $error
  231. );
  232. }
  233. }
  234. function createDir($dir, $dirname) {
  235. $dir = $this->formatFullPath($dir . '/' . $dirname);
  236. $error = self::v_exec('v-add-fs-directory', [$this->user, $dir]);
  237. if (empty($error)) {
  238. return array(
  239. 'result' => true
  240. );
  241. } else {
  242. return array(
  243. 'result' => false,
  244. 'message' => $error
  245. );
  246. }
  247. }
  248. function getDirectoryListing($dir = '') {
  249. $dir = $this->formatFullPath($dir);
  250. self::v_exec('v-list-fs-directory', [$this->user, $dir], false, $output);
  251. return $this->parseListing(explode("\n", $output));
  252. }
  253. public function ls($dir = '') {
  254. $listing = $this->getDirectoryListing($dir);
  255. return $data = array(
  256. 'result' => true,
  257. 'listing' => $listing
  258. );
  259. }
  260. public function open_file($dir = '') {
  261. $listing = $this->getDirectoryListing($dir);
  262. return $data = array(
  263. 'result' => true,
  264. 'listing' => $listing
  265. );
  266. }
  267. public function parseListing($raw) {
  268. $data = array();
  269. foreach ($raw as $o) {
  270. $info = explode($this->delimeter, $o);
  271. $data[] = array(
  272. 'type' => $info[$this->info_positions['TYPE']],
  273. 'permissions' => $info[$this->info_positions['PERMISSIONS']],
  274. 'date' => $info[$this->info_positions['DATE']],
  275. 'time' => $info[$this->info_positions['TIME']],
  276. 'owner' => $info[$this->info_positions['OWNER']],
  277. 'group' => $info[$this->info_positions['GROUP']],
  278. 'size' => $info[$this->info_positions['SIZE']],
  279. 'name' => $info[$this->info_positions['NAME']]
  280. );
  281. }
  282. return $data;
  283. }
  284. }