ftp.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. <?php
  2. if(!DEFINED('EGP'))
  3. exit(header('Refresh: 0; URL=http://'.$_SERVER['SERVER_NAME'].'/404'));
  4. class ftp
  5. {
  6. var $steck = false;
  7. var $dayru = array(
  8. 'Mon' => 'Понедельник',
  9. 'Tue' => 'Вторник',
  10. 'Wed' => 'Среда',
  11. 'Thu' => 'Четверг',
  12. 'Fri' => 'Пятница',
  13. 'Sat' => 'Суббота',
  14. 'Sun' => 'Воскресенье'
  15. );
  16. var $mounthint = array(
  17. 'Jan' => '01',
  18. 'Feb' => '02',
  19. 'Mar' => '03',
  20. 'Apr' => '04',
  21. 'May' => '05',
  22. 'Jun' => '06',
  23. 'Jul' => '07',
  24. 'Aug' => '08',
  25. 'Sep' => '09',
  26. 'Oct' => '10',
  27. 'Nov' => '11',
  28. 'Dec' => '12'
  29. );
  30. var $mounthru = array(
  31. 'Jan' => 'Янв',
  32. 'Feb' => 'Фев',
  33. 'Mar' => 'Мар',
  34. 'Apr' => 'Апр',
  35. 'May' => 'Май',
  36. 'Jun' => 'Июн',
  37. 'Jul' => 'Июл',
  38. 'Aug' => 'Авг',
  39. 'Sep' => 'Сен',
  40. 'Oct' => 'Окт',
  41. 'Nov' => 'Ноя',
  42. 'Dec' => 'Дек'
  43. );
  44. var $aEdits = array(
  45. 'txt',
  46. 'cfg',
  47. 'conf',
  48. 'json',
  49. 'xml',
  50. 'ini',
  51. 'gam',
  52. 'php',
  53. 'html',
  54. 'inf',
  55. 'js',
  56. 'css',
  57. 'sma',
  58. 'log'
  59. );
  60. public function auth($host, $user, $password, $port = 21)
  61. {
  62. $ftp_connect = @ftp_connect($host, $port);
  63. if(!$ftp_connect)
  64. return false;
  65. if(!@ftp_login($ftp_connect, $user, $password))
  66. return false;
  67. @ftp_pasv($ftp_connect, true);
  68. $this->steck = $ftp_connect;
  69. return true;
  70. }
  71. public function read($path)
  72. {
  73. $path = ($path == '') ? '/' : $path;
  74. $path = str_replace('//', '/', $path);
  75. $aDir = array();
  76. $aFile = array();
  77. $aInfo = array();
  78. $rawlist = array();
  79. $data = ftp_rawlist($this->steck, $path);
  80. if(is_array($data))
  81. foreach($data as $index)
  82. {
  83. $vinfo = preg_split('/[\s]+/', $index, 9);
  84. if($vinfo[0] !== 'total')
  85. {
  86. $aInfo['chmod'] = $vinfo[0];
  87. $aInfo['num'] = $vinfo[1];
  88. $aInfo['owner'] = $vinfo[2];
  89. $aInfo['group'] = $vinfo[3];
  90. $aInfo['size'] = $vinfo[4];
  91. $aInfo['month'] = $vinfo[5];
  92. $aInfo['day'] = $vinfo[6];
  93. $aInfo['time'] = $vinfo[7];
  94. $aInfo['name'] = $vinfo[8];
  95. $rawlist[$aInfo['name']] = $aInfo;
  96. }
  97. }
  98. foreach($rawlist as $name => $data)
  99. {
  100. if($data['chmod']{0} == 'd')
  101. $aDir[$name] = $data;
  102. elseif($data['chmod']{0} == '-')
  103. $aFile[$name] = $data;
  104. }
  105. $aData = array(
  106. 'folder' => $aDir,
  107. 'file' => $aFile,
  108. 'path' => $path
  109. );
  110. return $aData;
  111. }
  112. public function view($view, $server)
  113. {
  114. global $html;
  115. if($view['path'] != '/')
  116. {
  117. $html->get('filetp_back', 'sections/servers/games/filetp');
  118. $html->set('back', $this->path($view['path']));
  119. $html->pack('list');
  120. }
  121. foreach($view as $type => $aVal)
  122. {
  123. if(!is_array($aVal))
  124. continue;
  125. foreach($aVal as $name => $info)
  126. {
  127. $html->get('filetp_list', 'sections/servers/games/filetp');
  128. $html->set('id', $server);
  129. $html->set('name', $name);
  130. $path = $view['path'];
  131. if($path{0} != '/') $path = '/'.$path;
  132. if($path != '/') $path = $path.'/';
  133. $html->set('path', $path);
  134. $html->set('chmod', $this->cti($info['chmod']).' '.$info['chmod']);
  135. $html->set('owner', $info['owner']);
  136. $html->set('group', $info['group']);
  137. if($type == 'folder')
  138. {
  139. $html->unit('folder', 1);
  140. $html->unit('file');
  141. $html->set('size', '');
  142. }else{
  143. $type = explode('.', $name);
  144. if(in_array(end($type), $this->aEdits))
  145. $html->unit('edit', 1);
  146. else
  147. $html->unit('edit');
  148. $html->unit('file', 1);
  149. $html->unit('folder');
  150. $html->set('size', sys::size($info['size']));
  151. }
  152. $html->set('month', $this->mounthru[$info['month']]);
  153. $html->set('day', $info['day']);
  154. $html->set('time', $info['time']);
  155. $html->pack('list');
  156. }
  157. }
  158. return isset($html->arr['list']) ? $html->arr['list'] : '';
  159. }
  160. public function mkdir($path, $folders)
  161. {
  162. if(!@ftp_chdir($this->steck, $path))
  163. sys::outjs(array('e' => 'Ошибка: не удалось создать папку'));
  164. $aFolder = explode('/', $folders);
  165. foreach($aFolder as $folder)
  166. {
  167. if($folder == '')
  168. continue;
  169. if(!@ftp_chdir($this->steck, $folder))
  170. {
  171. if(!@ftp_mkdir($this->steck, $folder))
  172. sys::outjs(array('e' => 'Ошибка: не удалось создать папку '.$folder));
  173. @ftp_chdir($this->steck, $folder);
  174. }
  175. }
  176. sys::outjs(array('s' => 'ok'));
  177. }
  178. public function touch($path, $file, $text)
  179. {
  180. $aData = explode('/', $file);
  181. $path_file = '';
  182. if(count($aData))
  183. {
  184. $file = end($aData);
  185. unset($aData[count($aData)-1]);
  186. foreach($aData as $val)
  187. $path_file .= $val.'/';
  188. }
  189. $dir = str_replace('//', '', $path.'/'.$path_file);
  190. $dir = ($dir == '') ? '/' : $dir;
  191. if(!@ftp_chdir($this->steck, $dir))
  192. sys::outjs(array('e' => 'Ошибка: не удалось создать файл'));
  193. $temp = sys::temp($text);
  194. if(@ftp_put($this->steck, $file, $temp, FTP_BINARY))
  195. {
  196. unlink($temp);
  197. sys::outjs(array('s' => 'ok'));
  198. }
  199. unlink($temp);
  200. sys::outjs(array('e' => 'Ошибка: не удалось создать файл'));
  201. }
  202. public function edit_file($path, $file)
  203. {
  204. $name = md5(time().$file.'ftp');
  205. if(@ftp_get($this->steck, TEMP.$name, $path.'/'.$file, FTP_BINARY))
  206. {
  207. $data = file_get_contents(TEMP.$name);
  208. unlink(TEMP.$name);
  209. sys::outjs(array('s' => $data));
  210. }
  211. sys::outjs(array('e' => 'Не удалось открыть файл'));
  212. }
  213. public function rename($path, $oldname, $newname)
  214. {
  215. if(@ftp_rename($this->steck, $path.'/'.$oldname, $path.'/'.$newname))
  216. sys::outjs(array('s' => 'ok'));
  217. sys::outjs(array('e' => 'Не удалось сменить имя'));
  218. }
  219. public function rmdir($path, $folder)
  220. {
  221. if(@ftp_rmdir($this->steck, $path.'/'.$folder))
  222. sys::outjs(array('s' => 'ok'));
  223. sys::outjs(array('e' => 'Ошибка: не удалось удалить папку.'));
  224. }
  225. public function rmfile($file)
  226. {
  227. if(@ftp_delete($this->steck, $file))
  228. sys::outjs(array('s' => 'ok'));
  229. sys::outjs(array('e' => 'Ошибка: не удалось удалить файл'));
  230. }
  231. public function chmod($path, $name, $chmod)
  232. {
  233. if(ftp_site($this->steck, 'CHMOD 0'.$chmod.' '.$path.'/'.$name))
  234. sys::outjs(array('s' => 'ok'));
  235. sys::outjs(array('e' => 'Ошибка: не удалось изменить права.'));
  236. }
  237. public function search($str, $server)
  238. {
  239. global $html, $mcache;
  240. $nmch = md5($str.$server);
  241. $cache = $mcache->get($nmch);
  242. if(!is_array($cache))
  243. {
  244. $aData = ftp_rawlist($this->steck, '/', true);
  245. if(!is_array($aData))
  246. sys::out('Ничего не найдено');
  247. // Файлы
  248. $aFile = array();
  249. // Файлы в корне
  250. $end = array_search('', $aData);
  251. for($i = 0; $i < $end; $i+=1)
  252. {
  253. $aInfo = preg_split('/[\s]+/', $aData[$i], 9);
  254. $info = '';
  255. for($n = 0; $n < 8; $n+=1)
  256. $info .= $aInfo[$n].' ';
  257. $aFile['/'][] = array('info' => $info, 'name' => $aInfo[8]);
  258. }
  259. // Перебор директорий и файлов в них
  260. foreach($aData as $index)
  261. {
  262. $begin = array_search('', $aData);
  263. unset($aData[$begin]);
  264. $end = array_search('', $aData);
  265. if(!$begin)
  266. break;
  267. $dir = substr($aData[$begin+1], 0, -1);
  268. for($i = $begin+2; $i < $end; $i+=1)
  269. {
  270. $aInfo = preg_split('/[\s]+/', $aData[$i], 9);
  271. $info = '';
  272. for($n = 0; $n < 8; $n+=1)
  273. $info .= $aInfo[$n].' ';
  274. $aFile[$dir][] = array('info' => $info, 'name' => $aInfo[8]);
  275. }
  276. }
  277. $mcache->set($nmch, $aFile, false, 20);
  278. }else
  279. $aFile = $cache;
  280. $aFind = array();
  281. // Поиск
  282. foreach($aFile as $dir => $files)
  283. {
  284. foreach($files as $file)
  285. {
  286. $find = sys::first(explode('.', $file['name']));
  287. if(preg_match('/'.$str.'/i', $find))
  288. $aFind[] = array('dir' => $dir, 'info' => $file['info'], 'file' => $file['name'], 'find' => sys::find($file['name'], $str));
  289. }
  290. }
  291. unset($aFile);
  292. foreach($aFind as $data)
  293. {
  294. $info = preg_split('/[\s]+/', trim($data['info']), 8);
  295. $html->get('filetp_find', 'sections/servers/games/filetp');
  296. $html->set('id', $server);
  297. $html->set('find', $data['find']);
  298. $html->set('name', $data['file']);
  299. $path = $data['dir'];
  300. if($path{0} != '/') $path = '/'.$path;
  301. if($path != '/') $path = $path.'/';
  302. $html->set('path', $path);
  303. $html->set('chmod', $this->cti($info[0]).' '.$info[0]);
  304. $html->set('owner', $info[2]);
  305. $html->set('group', $info[3]);
  306. if($info[0]{0} == 'd')
  307. {
  308. $html->unit('folder', 1);
  309. $html->unit('file');
  310. $html->set('size', '');
  311. }else{
  312. $type = explode('.', $data['file']);
  313. if(in_array(end($type), $this->aEdits))
  314. $html->unit('edit', 1);
  315. else
  316. $html->unit('edit');
  317. $html->unit('file', 1);
  318. $html->unit('folder');
  319. $html->set('size', sys::size($info[4]));
  320. }
  321. $html->set('month', $this->mounthru[$info[5]]);
  322. $html->set('day', $info[6]);
  323. $html->set('time', $info[7]);
  324. $html->pack('list');
  325. }
  326. if(isset($html->arr['list']))
  327. sys::out($html->arr['list']);
  328. sys::out('Ничего не найдено');
  329. }
  330. public function logs($data, $uid)
  331. {
  332. global $html;
  333. $aLine = explode("\n", $data);
  334. $actions = array('i' => 'загрузка', 'o' => 'скачивание', 'd' => 'удаление');
  335. $acticon = array('i' => '<i class="fa fa-upload"></i>', 'o' => '<i class="fa fa-download"></i>', 'd' => '<i class="fa fa-times"></i>');
  336. unset($aLine[count($aLine)-1]);
  337. rsort($aLine);
  338. foreach($aLine as $line)
  339. {
  340. $aData = explode('\\', $line);
  341. $html->get('filetp_logs', 'sections/servers/games/filetp');
  342. $html->set('month', $this->mounthint[$aData[0]]);
  343. $html->set('day', $aData[1]);
  344. $html->set('time', $aData[2]);
  345. $html->set('year', $aData[3]);
  346. $html->set('who', $this->who($aData[4]));
  347. $html->set('size', sys::size($aData[5]));
  348. $html->set('file', str_replace('/servers/'.$uid.'/', '', $aData[6]));
  349. $html->set('action', $actions[$aData[7]]);
  350. $html->set('acticon', $acticon[$aData[7]]);
  351. $html->pack('logs');
  352. }
  353. return isset($html->arr['logs']) ? $html->arr['logs'] : 'Список логов отсутствует';
  354. }
  355. private function path($path)
  356. {
  357. $path = str_replace('//', '/', $path);
  358. $path = explode('/', $path);
  359. unset($path[count($path)-1]);
  360. $newpath = '/';
  361. foreach($path as $index => $val)
  362. if(count($path)-1 == $index) $newpath .= $val; else $newpath .= $val.'/';
  363. return str_replace('//', '/', $newpath);
  364. }
  365. private function cti($chmod)
  366. {
  367. $intchmod = array('-' => '0', 'r' => '4', 'w' => '2', 'x' => '1');
  368. $chmod = substr(strtr($chmod, $intchmod), 1);
  369. $split = str_split($chmod, 3);
  370. return array_sum(str_split($split[0])).array_sum(str_split($split[1])).array_sum(str_split($split[2]));
  371. }
  372. private function who($address)
  373. {
  374. global $cfg, $uip;
  375. if($address == $cfg['ip'])
  376. return 'панель управления';
  377. if($address == $uip)
  378. return 'вы';
  379. return $address;
  380. }
  381. }
  382. ?>