fm_core.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. exec (VESTA_CMD . "v-add-fs-archive {$this->user} {$dst_item} {$items}", $output, $return_var);
  134. $error = self::check_return_code($return_var, $output);
  135. if (empty($error)) {
  136. return array(
  137. 'result' => true
  138. );
  139. }
  140. else {
  141. return array(
  142. 'result' => false,
  143. 'message' => $error
  144. );
  145. }
  146. }
  147. function backupItem($item) {
  148. $src_item = $this->formatFullPath($item);
  149. $dst_item_name = $item . '~' . date('Ymd_His');
  150. $dst_item = $this->formatFullPath($dst_item_name);
  151. exec (VESTA_CMD . "v-copy-fs-file {$this->user} {$src_item} {$dst_item}", $output, $return_var);
  152. $error = self::check_return_code($return_var, $output);
  153. if (empty($error)) {
  154. return array(
  155. 'result' => true,
  156. 'filename' => $dst_item_name
  157. );
  158. }
  159. else {
  160. return array(
  161. 'result' => false,
  162. 'message' => $error
  163. );
  164. }
  165. $error = self::check_return_code($return_var, $output);
  166. if (empty($error)) {
  167. return array(
  168. 'result' => true
  169. );
  170. }
  171. else {
  172. return array(
  173. 'result' => false,
  174. 'message' => $error
  175. );
  176. }
  177. }
  178. function unpackItem($item, $dir, $target_dir, $filename) {
  179. $item = $this->formatFullPath($item);
  180. $dst_item = $this->formatFullPath($target_dir);
  181. exec (VESTA_CMD . "v-extract-fs-archive {$this->user} {$item} {$dst_item}", $output, $return_var);
  182. $error = self::check_return_code($return_var, $output);
  183. if (empty($error)) {
  184. return array(
  185. 'result' => true
  186. );
  187. }
  188. else {
  189. return array(
  190. 'result' => false,
  191. 'message' => $error
  192. );
  193. }
  194. }
  195. function renameFile($item, $target_name) {
  196. $item = $this->formatFullPath($item);
  197. $dst_item = $this->formatFullPath($target_name);
  198. exec (VESTA_CMD . "v-move-fs-file {$this->user} {$item} {$dst_item}", $output, $return_var);
  199. $error = self::check_return_code($return_var, $output);
  200. if (empty($error)) {
  201. return array(
  202. 'result' => true
  203. );
  204. }
  205. else {
  206. return array(
  207. 'result' => false,
  208. 'message' => $error
  209. );
  210. }
  211. }
  212. function renameDirectory($item, $target_name) {
  213. $item = $this->formatFullPath($item);
  214. $dst_item = $this->formatFullPath($target_name);
  215. if ($item == $dst_item) {
  216. return array(
  217. 'result' => true
  218. );
  219. }
  220. exec (VESTA_CMD . "v-move-fs-directory {$this->user} {$item} {$dst_item}", $output, $return_var);
  221. $error = self::check_return_code($return_var, $output);
  222. if (empty($error)) {
  223. return array(
  224. 'result' => true
  225. );
  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. exec (VESTA_CMD . "v-add-fs-directory {$this->user} {$dir}", $output, $return_var);
  237. $error = self::check_return_code($return_var, $output);
  238. if (empty($error)) {
  239. return array(
  240. 'result' => true
  241. );
  242. }
  243. else {
  244. return array(
  245. 'result' => false,
  246. 'message' => $error
  247. );
  248. }
  249. }
  250. function chmodItem($dir, $item, $permissions) {
  251. $item = $this->formatFullPath($dir . $item);
  252. $permissions = escapeshellarg($permissions);
  253. exec (VESTA_CMD . "v-change-fs-file-permission {$this->user} {$item} {$permissions}", $output, $return_var);
  254. $error = self::check_return_code($return_var, $output);
  255. if (empty($error)) {
  256. return array(
  257. 'result' => true
  258. );
  259. }
  260. else {
  261. return array(
  262. 'result' => false,
  263. 'message' => $error
  264. );
  265. }
  266. }
  267. function getDirectoryListing($dir = '') {
  268. $dir = $this->formatFullPath($dir);
  269. exec (VESTA_CMD . "v-list-fs-directory {$this->user} {$dir}", $output, $return_var);
  270. return $this->parseListing($output);
  271. }
  272. public function ls($dir = '') {
  273. $listing = $this->getDirectoryListing($dir);
  274. return $data = array(
  275. 'result' => true,
  276. 'listing' => $listing
  277. );
  278. }
  279. public function open_file($dir = '') {
  280. $listing = $this->getDirectoryListing($dir);
  281. return $data = array(
  282. 'result' => true,
  283. 'listing' => $listing
  284. );
  285. }
  286. public function parseListing($raw) {
  287. $data = array();
  288. foreach ($raw as $o) {
  289. $info = explode($this->delimeter, $o);
  290. $data[] = array(
  291. 'type' => $info[$this->info_positions['TYPE']],
  292. 'permissions' => str_pad($info[$this->info_positions['PERMISSIONS']], 3, "0", STR_PAD_LEFT),
  293. 'date' => $info[$this->info_positions['DATE']],
  294. 'time' => $info[$this->info_positions['TIME']],
  295. 'owner' => $info[$this->info_positions['OWNER']],
  296. 'group' => $info[$this->info_positions['GROUP']],
  297. 'size' => $info[$this->info_positions['SIZE']],
  298. 'name' => htmlspecialchars($info[$this->info_positions['NAME']], ENT_QUOTES)
  299. );
  300. }
  301. return $data;
  302. }
  303. }