fm_core.php 10 KB

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