fm_core.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. if (is_dir($item)) {
  68. exec (VESTA_CMD . "v-delete-fs-directory {$this->user} {$dir}", $output, $return_var);
  69. }
  70. else {
  71. exec (VESTA_CMD . "v-delete-fs-file {$this->user} {$dir}", $output, $return_var);
  72. }
  73. $error = self::check_return_code($return_var, $output);
  74. if (empty($error)) {
  75. return array(
  76. 'result' => true
  77. );
  78. }
  79. else {
  80. return array(
  81. 'result' => false,
  82. 'message' => $error
  83. );
  84. }
  85. /*if (is_readable($item)) {
  86. unlink($item);
  87. }
  88. if (is_readable($item)) {
  89. return array(
  90. 'result' => false,
  91. 'message' => 'item was not deleted'
  92. );
  93. }
  94. return array(
  95. 'result' => true
  96. );*/
  97. }
  98. function copyFile($item, $dir, $target_dir, $filename) {
  99. $src = $this->formatFullPath($item);
  100. $dst = $this->formatFullPath($target_dir);
  101. exec (VESTA_CMD . "v-copy-fs-file {$this->user} {$src} {$dst}", $output, $return_var);
  102. $error = self::check_return_code($return_var, $output);
  103. if (empty($error)) {
  104. return array(
  105. 'result' => true
  106. );
  107. }
  108. else {
  109. return array(
  110. 'result' => false,
  111. 'message' => $error
  112. );
  113. }
  114. }
  115. function copyDirectory($item, $dir, $target_dir, $filename) {
  116. $src = $this->formatFullPath($item);
  117. $dst = $this->formatFullPath($target_dir);
  118. exec (VESTA_CMD . "v-copy-fs-directory {$this->user} {$src} {$dst}", $output, $return_var);
  119. $error = self::check_return_code($return_var, $output);
  120. if (empty($error)) {
  121. return array(
  122. 'result' => true
  123. );
  124. }
  125. else {
  126. return array(
  127. 'result' => false,
  128. 'message' => $error
  129. );
  130. }
  131. }
  132. static function check_return_code($return_var, $output) {
  133. if ($return_var != 0) {
  134. $error = implode('<br>', $output);
  135. return $error;
  136. //if (empty($error)) $error = __('Error code:',$return_var);
  137. //$_SESSION['error_msg'] = $error;
  138. }
  139. return null;
  140. }
  141. function createFile($dir, $filename) {
  142. $dir = $this->formatFullPath($dir . '/' . $filename);
  143. exec (VESTA_CMD . "v-add-fs-file {$this->user} {$dir}", $output, $return_var);
  144. $error = self::check_return_code($return_var, $output);
  145. if (empty($error)) {
  146. return array(
  147. 'result' => true
  148. );
  149. }
  150. else {
  151. return array(
  152. 'result' => false,
  153. 'message' => $error
  154. );
  155. }
  156. }
  157. function packItem($item, $dir, $target_dir, $filename) {
  158. $item = $this->formatFullPath($item);
  159. $dst_item = $this->formatFullPath($target_dir);
  160. //print VESTA_CMD . "v-add-fs-archive {$this->user} {$item} {$dst_item}";die();
  161. exec (VESTA_CMD . "v-add-fs-archive {$this->user} {$item} {$dst_item}", $output, $return_var);
  162. $error = self::check_return_code($return_var, $output);
  163. if (empty($error)) {
  164. return array(
  165. 'result' => true
  166. );
  167. }
  168. else {
  169. return array(
  170. 'result' => false,
  171. 'message' => $error
  172. );
  173. }
  174. }
  175. function backupItem($item) {
  176. $src_item = $this->formatFullPath($item);
  177. $dst_item_name = $item . '~' . date('Ymd_His');
  178. $dst_item = $this->formatFullPath($dst_item_name);
  179. //print VESTA_CMD . "v-add-fs-archive {$this->user} {$item} {$dst_item}";die();
  180. exec (VESTA_CMD . "v-copy-fs-file {$this->user} {$src_item} {$dst_item}", $output, $return_var);
  181. $error = self::check_return_code($return_var, $output);
  182. if (empty($error)) {
  183. return array(
  184. 'result' => true,
  185. 'filename' => $dst_item_name
  186. );
  187. }
  188. else {
  189. return array(
  190. 'result' => false,
  191. 'message' => $error
  192. );
  193. }
  194. $error = self::check_return_code($return_var, $output);
  195. if (empty($error)) {
  196. return array(
  197. 'result' => true
  198. );
  199. }
  200. else {
  201. return array(
  202. 'result' => false,
  203. 'message' => $error
  204. );
  205. }
  206. }
  207. function unpackItem($item, $dir, $target_dir, $filename) {
  208. $item = $this->formatFullPath($item);
  209. $dst_item = $this->formatFullPath($target_dir);
  210. exec (VESTA_CMD . "v-extract-fs-archive {$this->user} {$item} {$dst_item}", $output, $return_var);
  211. $error = self::check_return_code($return_var, $output);
  212. if (empty($error)) {
  213. return array(
  214. 'result' => true
  215. );
  216. }
  217. else {
  218. return array(
  219. 'result' => false,
  220. 'message' => $error
  221. );
  222. }
  223. }
  224. function renameFile($dir, $item, $target_name) {
  225. $item = $this->formatFullPath($dir . '/' . $item);
  226. $dst_item = $this->formatFullPath($dir . '/' . $target_name);
  227. // var_dump(VESTA_CMD . "v-move-fs-file {$this->user} {$item} {$dst_item}");die();
  228. exec (VESTA_CMD . "v-move-fs-file {$this->user} {$item} {$dst_item}", $output, $return_var);
  229. $error = self::check_return_code($return_var, $output);
  230. if (empty($error)) {
  231. return array(
  232. 'result' => true
  233. );
  234. }
  235. else {
  236. return array(
  237. 'result' => false,
  238. 'message' => $error
  239. );
  240. }
  241. }
  242. function renameDirectory($dir, $item, $target_name) {
  243. $item = $this->formatFullPath($dir . $item);
  244. $dst_item = $this->formatFullPath($dir . $target_name);
  245. if ($item == $dst_item) {
  246. return array(
  247. 'result' => true
  248. );
  249. }
  250. exec (VESTA_CMD . "v-move-fs-directory {$this->user} {$item} {$dst_item}", $output, $return_var);
  251. $error = self::check_return_code($return_var, $output);
  252. if (empty($error)) {
  253. return array(
  254. 'result' => true
  255. );
  256. }
  257. else {
  258. return array(
  259. 'result' => false,
  260. 'message' => $error
  261. );
  262. }
  263. }
  264. function createDir($dir, $dirname) {
  265. $dir = $this->formatFullPath($dir . '/' . $dirname);
  266. exec (VESTA_CMD . "v-add-fs-directory {$this->user} {$dir}", $output, $return_var);
  267. $error = self::check_return_code($return_var, $output);
  268. if (empty($error)) {
  269. return array(
  270. 'result' => true
  271. );
  272. }
  273. else {
  274. return array(
  275. 'result' => false,
  276. 'message' => $error
  277. );
  278. }
  279. }
  280. function getDirectoryListing($dir = '') {
  281. $dir = $this->formatFullPath($dir);
  282. exec (VESTA_CMD . "v-list-fs-directory {$this->user} {$dir}", $output, $return_var);
  283. return $this->parseListing($output);
  284. }
  285. public function ls($dir = '') {
  286. $listing = $this->getDirectoryListing($dir);
  287. return $data = array(
  288. 'result' => true,
  289. 'listing' => $listing
  290. );
  291. }
  292. public function open_file($dir = '') {
  293. $listing = $this->getDirectoryListing($dir);
  294. return $data = array(
  295. 'result' => true,
  296. 'listing' => $listing
  297. );
  298. }
  299. public function parseListing($raw) {
  300. $data = array();
  301. foreach ($raw as $o) {
  302. $info = explode($this->delimeter, $o);
  303. $data[] = array(
  304. 'type' => $info[$this->info_positions['TYPE']],
  305. 'permissions' => $info[$this->info_positions['PERMISSIONS']],
  306. 'date' => $info[$this->info_positions['DATE']],
  307. 'time' => $info[$this->info_positions['TIME']],
  308. 'owner' => $info[$this->info_positions['OWNER']],
  309. 'group' => $info[$this->info_positions['GROUP']],
  310. 'size' => $info[$this->info_positions['SIZE']],
  311. 'name' => $info[$this->info_positions['NAME']]
  312. );
  313. }
  314. return $data;
  315. }
  316. }