fm_core.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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($item, $dir, $target_dir, $filename) {
  127. $item = $this->formatFullPath($item);
  128. $dst_item = $this->formatFullPath($target_dir);
  129. $dst_item = str_replace('.tar.gz', '', $dst_item);
  130. exec (VESTA_CMD . "v-add-fs-archive {$this->user} {$dst_item} {$item}", $output, $return_var);
  131. $error = self::check_return_code($return_var, $output);
  132. if (empty($error)) {
  133. return array(
  134. 'result' => true
  135. );
  136. }
  137. else {
  138. return array(
  139. 'result' => false,
  140. 'message' => $error
  141. );
  142. }
  143. }
  144. function backupItem($item) {
  145. $src_item = $this->formatFullPath($item);
  146. $dst_item_name = $item . '~' . date('Ymd_His');
  147. $dst_item = $this->formatFullPath($dst_item_name);
  148. exec (VESTA_CMD . "v-copy-fs-file {$this->user} {$src_item} {$dst_item}", $output, $return_var);
  149. $error = self::check_return_code($return_var, $output);
  150. if (empty($error)) {
  151. return array(
  152. 'result' => true,
  153. 'filename' => $dst_item_name
  154. );
  155. }
  156. else {
  157. return array(
  158. 'result' => false,
  159. 'message' => $error
  160. );
  161. }
  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 unpackItem($item, $dir, $target_dir, $filename) {
  176. $item = $this->formatFullPath($item);
  177. $dst_item = $this->formatFullPath($target_dir);
  178. exec (VESTA_CMD . "v-extract-fs-archive {$this->user} {$item} {$dst_item}", $output, $return_var);
  179. $error = self::check_return_code($return_var, $output);
  180. if (empty($error)) {
  181. return array(
  182. 'result' => true
  183. );
  184. }
  185. else {
  186. return array(
  187. 'result' => false,
  188. 'message' => $error
  189. );
  190. }
  191. }
  192. function renameFile($dir, $item, $target_name) {
  193. $item = $this->formatFullPath($dir . '/' . $item);
  194. $dst_item = $this->formatFullPath($dir . '/' . $target_name);
  195. exec (VESTA_CMD . "v-move-fs-file {$this->user} {$item} {$dst_item}", $output, $return_var);
  196. $error = self::check_return_code($return_var, $output);
  197. if (empty($error)) {
  198. return array(
  199. 'result' => true
  200. );
  201. }
  202. else {
  203. return array(
  204. 'result' => false,
  205. 'message' => $error
  206. );
  207. }
  208. }
  209. function renameDirectory($dir, $item, $target_name) {
  210. $item = $this->formatFullPath($dir . $item);
  211. $dst_item = $this->formatFullPath($dir . $target_name);
  212. if ($item == $dst_item) {
  213. return array(
  214. 'result' => true
  215. );
  216. }
  217. exec (VESTA_CMD . "v-move-fs-directory {$this->user} {$item} {$dst_item}", $output, $return_var);
  218. $error = self::check_return_code($return_var, $output);
  219. if (empty($error)) {
  220. return array(
  221. 'result' => true
  222. );
  223. }
  224. else {
  225. return array(
  226. 'result' => false,
  227. 'message' => $error
  228. );
  229. }
  230. }
  231. function createDir($dir, $dirname) {
  232. $dir = $this->formatFullPath($dir . '/' . $dirname);
  233. exec (VESTA_CMD . "v-add-fs-directory {$this->user} {$dir}", $output, $return_var);
  234. $error = self::check_return_code($return_var, $output);
  235. if (empty($error)) {
  236. return array(
  237. 'result' => true
  238. );
  239. }
  240. else {
  241. return array(
  242. 'result' => false,
  243. 'message' => $error
  244. );
  245. }
  246. }
  247. function chmodItem($dir, $item, $permissions) {
  248. $item = $this->formatFullPath($dir . $item);
  249. $permissions = escapeshellarg($permissions);
  250. exec (VESTA_CMD . "v-change-fs-file-permission {$this->user} {$item} {$permissions}", $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 getDirectoryListing($dir = '') {
  265. $dir = $this->formatFullPath($dir);
  266. exec (VESTA_CMD . "v-list-fs-directory {$this->user} {$dir}", $output, $return_var);
  267. return $this->parseListing($output);
  268. }
  269. public function ls($dir = '') {
  270. $listing = $this->getDirectoryListing($dir);
  271. return $data = array(
  272. 'result' => true,
  273. 'listing' => $listing
  274. );
  275. }
  276. public function open_file($dir = '') {
  277. $listing = $this->getDirectoryListing($dir);
  278. return $data = array(
  279. 'result' => true,
  280. 'listing' => $listing
  281. );
  282. }
  283. public function parseListing($raw) {
  284. $data = array();
  285. foreach ($raw as $o) {
  286. $info = explode($this->delimeter, $o);
  287. $data[] = array(
  288. 'type' => $info[$this->info_positions['TYPE']],
  289. 'permissions' => str_pad($info[$this->info_positions['PERMISSIONS']], 3, "0", STR_PAD_LEFT),
  290. 'date' => $info[$this->info_positions['DATE']],
  291. 'time' => $info[$this->info_positions['TIME']],
  292. 'owner' => $info[$this->info_positions['OWNER']],
  293. 'group' => $info[$this->info_positions['GROUP']],
  294. 'size' => $info[$this->info_positions['SIZE']],
  295. 'name' => $info[$this->info_positions['NAME']]
  296. );
  297. }
  298. return $data;
  299. }
  300. }