fm_core.php 11 KB

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