fm_core.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 formatFullPath($path_part = '') {
  38. if (substr($path_part, 0, strlen($this->ROOT_DIR)) === $this->ROOT_DIR) {
  39. $path = $path_part;
  40. }
  41. else {
  42. $path = $this->ROOT_DIR . '/' . $path_part;
  43. }
  44. //var_dump($path);die();
  45. return escapeshellarg($path);
  46. }
  47. function deleteItems($dir, $item) {
  48. if (is_readable($item)) {
  49. unlink($item);
  50. }
  51. if (is_readable($item)) {
  52. return array(
  53. 'result' => false,
  54. 'message' => 'item was not deleted'
  55. );
  56. }
  57. return array(
  58. 'result' => true
  59. );
  60. }
  61. function copyFile($dir, $target_dir, $filename) {
  62. // todo: checks
  63. // todo: vesta method "create file"
  64. if (empty($dir)) {
  65. $dir = $this->ROOT_DIR;
  66. }
  67. if (empty($target_dir)) {
  68. $target_dir = $this->ROOT_DIR;
  69. }
  70. copy($dir . '/' . $filename, $target_dir.'/'.$filename);
  71. if (!is_readable($target_dir . '/' .$filename)) {
  72. return array(
  73. 'result' => false,
  74. 'message' => 'item was not created'
  75. );
  76. }
  77. return array(
  78. 'result' => true,
  79. 'bla' => $target_dir.'/'.$filename,
  80. 'bla2' => $dir . '/' . $filename
  81. );
  82. }
  83. function createFile($dir, $filename) {
  84. // todo: checks
  85. // todo: vesta method "create file"
  86. if (empty($dir)) {
  87. $dir = $this->ROOT_DIR;
  88. }
  89. file_put_contents($dir . '/' . $filename, '');
  90. if (!is_readable($dir . '/' .$filename)) {
  91. return array(
  92. 'result' => false,
  93. 'message' => 'item was not created'
  94. );
  95. }
  96. return array(
  97. 'result' => true
  98. );
  99. }
  100. function renameItem($dir, $item, $target_name) {
  101. if (empty($dir)) {
  102. $dir = $this->ROOT_DIR;
  103. }
  104. if (is_readable($dir . '/' . $item)) {
  105. rename($dir . '/' . $item, $dir . '/' . $target_name);
  106. }
  107. if (!is_readable($dir . '/' .$target_name)) {
  108. return array(
  109. 'result' => false,
  110. 'message' => 'item was not renamed'
  111. );
  112. }
  113. return array(
  114. 'result' => true
  115. );
  116. }
  117. function createDir($dir, $dirname) {
  118. // todo: checks
  119. // todo: vesta method "create file"
  120. if (empty($dir)) {
  121. $dir = $this->ROOT_DIR;
  122. }
  123. mkdir($dir . '/' . $dirname);
  124. if (!is_readable($dir . '/' .$dirname)) {
  125. return array(
  126. 'result' => false,
  127. 'message' => 'item was not created'
  128. );
  129. }
  130. return array(
  131. 'result' => true
  132. );
  133. }
  134. function getDirectoryListing($dir = '') {
  135. $dir = $this->formatFullPath($dir);
  136. exec (VESTA_CMD . "v-list-fs-directory {$this->user} {$dir}", $output, $return_var);
  137. return $this->parseListing($output);
  138. }
  139. public function ls($dir = '') {
  140. $listing = $this->getDirectoryListing($dir);
  141. return $data = array(
  142. 'result' => true,
  143. 'listing' => $listing
  144. );
  145. }
  146. public function open_file($dir = '') {
  147. $listing = $this->getDirectoryListing($dir);
  148. return $data = array(
  149. 'result' => true,
  150. 'listing' => $listing
  151. );
  152. }
  153. public function parseListing($raw) {
  154. $data = array();
  155. foreach ($raw as $o) {
  156. $info = explode($this->delimeter, $o);
  157. $data[] = array(
  158. 'type' => $info[$this->info_positions['TYPE']],
  159. 'permissions' => $info[$this->info_positions['PERMISSIONS']],
  160. 'date' => $info[$this->info_positions['DATE']],
  161. 'time' => $info[$this->info_positions['TIME']],
  162. 'owner' => $info[$this->info_positions['OWNER']],
  163. 'group' => $info[$this->info_positions['GROUP']],
  164. 'size' => $info[$this->info_positions['SIZE']],
  165. 'name' => $info[$this->info_positions['NAME']]
  166. );
  167. }
  168. return $data;
  169. }
  170. }