Online FTP Interface PHP


SUBMITTED BY: Guest

DATE: July 23, 2014, 1:09 p.m.

FORMAT: Text only

SIZE: 19.5 kB

HITS: 1152

  1. <?php
  2. /*
  3. Copyright (C) 2002 Edwin van Wijk, webftp@v-wijk.net
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. */
  16. $downloadDir = "/tmp/";
  17. $unzipCommand = "sudo -u $user /usr/bin/unzip";
  18. $port=22;
  19. function del_recursive($currentDir,$connection,$file){
  20. //echo "entering $currentDir/$file<BR>";
  21. if ($lista = @ftp_nlist($connection, "$file")){
  22. for ($x=0;$x<count($lista);$x++){
  23. //echo "tryng to delete $lista[$x]<BR>";
  24. if (!@ftp_delete($connection, "$lista[$x]"))
  25. del_recursive($currentDir,$connection,$lista[$x]);
  26. }
  27. @ftp_rmdir($connection, "$file");
  28. }
  29. }
  30. include("parser.inc.php");
  31. session_start();
  32. $HPV = $HTTP_POST_VARS;
  33. // Get the POST, GET and SESSION variables (if register_globals=off (PHP4.2.1+))
  34. // It's a bit of a dirty hack but variables are sometimes GET and sometimes POST variables
  35. $mode=(isset($HPV['mode']))?$HPV['mode']:$HTTP_GET_VARS['mode'];
  36. $action=(isset($HPV['action']))?$HPV['action']:$HTTP_GET_VARS['action'];
  37. $currentDir=(isset($HPV['currentDir']))?$HPV['currentDir']:$HTTP_GET_VARS['currentDir'];
  38. $file=(isset($HPV['file']))?$HPV['file']:$HTTP_GET_VARS['file'];
  39. $file2=(isset($HPV['file2']))?$HPV['file2']:$HTTP_GET_VARS['file2'];
  40. $permissions=(isset($HPV['permissions']))?$HPV['permissions']:$HTTP_GET_VARS['permissions'];
  41. $directory=(isset($HPV['directory']))?$HPV['directory']:$HTTP_GET_VARS['directory']; $MAX_FILE_SIZE=(isset($HPV['MAX_FILE_SIZE']))?$HPV['MAX_FILE_SIZE']:$HTTP_GET_VARS['MAX_FILE_SIZE'];
  42. $logoff=(isset($HPV['logoff']))?$HPV['logoff']:$HTTP_GET_VARS['logoff'];
  43. if(isset($HTTP_SESSION_VARS['server'])) {
  44. $server=$HTTP_SESSION_VARS['server'];
  45. $user=$HTTP_SESSION_VARS['user'];
  46. $password=$HTTP_SESSION_VARS['password'];
  47. $port=$HTTP_SESSION_VARS['port'];
  48. } else {
  49. $server=$HTTP_POST_VARS['server'];
  50. $user=$HTTP_POST_VARS['user'];
  51. $password=$HTTP_POST_VARS['password'];
  52. $port=$HTTP_POST_VARS['port'];
  53. }
  54. if (isset($logoff))
  55. {
  56. session_unregister('server');
  57. session_unregister('user');
  58. session_unregister('password');
  59. session_unregister('port');
  60. unset($server);
  61. unset($user);
  62. unset($password);
  63. unset($port);
  64. session_destroy();
  65. }
  66. if (isset($server))
  67. {
  68. session_register('server', $server);
  69. session_register('user', $user);
  70. session_register('password', $password);
  71. session_register('port', $port);
  72. // $connection = @ftp_connect($server);
  73. $connection = @ftp_connect($server, $port);
  74. $loggedOn = @ftp_login($connection, $user, $password);
  75. $systype = @ftp_systype($connection);
  76. if(!isset($mode))
  77. {
  78. $mode=1; //(FTP_ASCII = 0; FTP_BINARY=1)
  79. }
  80. if ($loggedOn)
  81. {
  82. if (isset($currentDir))
  83. {
  84. ftp_chdir($connection, $currentDir);
  85. }
  86. $currentDir = ftp_pwd($connection);
  87. $msg = "Current directory = $currentDir";
  88. // what to do now ???
  89. if(isset($action)) {
  90. switch ($action) {
  91. case "chmod": // Change permissions
  92. if(@ftp_site($connection, "chmod $permissions $file"))
  93. {
  94. $msg= "File permission changed.";
  95. } else
  96. {
  97. $msg= "Could not change permissions for " . $file;
  98. }
  99. break;
  100. case "cd": // Change directory
  101. //First try : normal directory
  102. if(@ftp_chdir($connection, $currentDir . "/" . $file))
  103. {
  104. $currentDir = @ftp_pwd($connection);
  105. $msg = "Current directory = " . $currentDir;
  106. }
  107. elseif(@ftp_chdir($connection, $file)) // Symbolic link directory
  108. {
  109. $currentDir = @ftp_pwd($connection);
  110. $msg = "Current directory = " . $currentDir;
  111. }
  112. else // link to a file so let's retrieve this...
  113. {
  114. header("Content-disposition: attachment; filename=\"$file\"");
  115. header("Content-type: application/octetstream");
  116. header("Pragma: ");
  117. header("Cache-Control: cache");
  118. header("Expires: 0");
  119. //Determine original filename
  120. $filearray = explode("/",$file);
  121. $file = $filearray[sizeof($filearray)-1];
  122. $msg = $file;
  123. $fp = fopen($downloadDir . $file, "w");
  124. if(!@ftp_fget($connection,$fp,"$file",$mode))
  125. {
  126. fclose($fp);
  127. exit;
  128. }
  129. fclose($fp);
  130. $data = readfile($downloadDir . $file);
  131. $i=0;
  132. while ($data[$i] != "")
  133. {
  134. echo $data[$i];
  135. $i++;
  136. }
  137. unlink($downloadDir . $file);
  138. exit;
  139. }
  140. break;
  141. case "get": // Download file
  142. header("Content-disposition: attachment; filename=\"$file\"");
  143. header("Content-type: application/octetstream");
  144. header("Pragma: ");
  145. header("Cache-Control: cache");
  146. header("Expires: 0");
  147. $fp = fopen($downloadDir . $file, "w");
  148. ftp_fget($connection,$fp,"$file",$mode) OR DIE("Error downloading file");
  149. fclose($fp);
  150. $data = readfile($downloadDir . $file);
  151. $i=0;
  152. while ($data[$i] != "")
  153. {
  154. echo $data[$i];
  155. $i++;
  156. }
  157. unlink($downloadDir . $file);
  158. exit;
  159. break;
  160. case "put": // Upload file
  161. if($file_size > $MAX_FILE_SIZE)
  162. {
  163. $msg = "<BFile size too big!</B> (max. " . $MAX_FILE_SIZE . "bytes)<P>";
  164. }
  165. else
  166. {
  167. if(file_exists($HTTP_POST_FILES['file']['tmp_name']))
  168. {
  169. if ($mode==1)
  170. {
  171. ftp_put($connection, $currentDir . "/" . $HTTP_POST_FILES['file']['name'], $HTTP_POST_FILES['file']['tmp_name'], 1);
  172. }
  173. else
  174. {
  175. ftp_put($connection, $currentDir . "/" . $HTTP_POST_FILES['file']['name'], $HTTP_POST_FILES['file']['tmp_name'], 0);
  176. }
  177. unlink($HTTP_POST_FILES['file']['tmp_name']);
  178. }
  179. else
  180. {
  181. $msg = "File could not be uploaded.";
  182. }
  183. }
  184. break;
  185. case "deldir"; // Delete directory
  186. if(@ftp_rmdir($connection, "$file"))
  187. {
  188. $msg = "$file deleted";
  189. }
  190. else
  191. {
  192. //Verify if has files inside and if so, call recursive del
  193. if ($lista = @ftp_nlist($connection, "$currentDir/$file")){
  194. del_recursive($currentDir,$connection,$file);
  195. $msg = "Directory $currentDir/$file deleted";
  196. }
  197. else $msg = "Could not delete $file";
  198. }
  199. break;
  200. case "delfile"; // Delete file
  201. if(@ftp_delete($connection, "$file"))
  202. {
  203. $msg = "$file deleted";
  204. }
  205. else
  206. {
  207. $msg = "Could not delete $file";
  208. }
  209. break;
  210. case "rename"; // Rename file
  211. if(@ftp_rename($connection, "$file", "$file2"))
  212. {
  213. $msg = "$file renamed to $file2";
  214. }
  215. else
  216. {
  217. $msg = "Could not rename $file to $file2";
  218. }
  219. break;
  220. case "createdir": // Create a new directory
  221. if(@ftp_mkdir($connection, "$file"))
  222. {
  223. $msg = "$file created";
  224. }
  225. else
  226. {
  227. $msg = "Could not create $file";
  228. }
  229. break;
  230. case "unzipfile";
  231. $filens = str_replace(" ","\\ ",$file);
  232. if (exec("$unzipCommand $currentDir/$filens -d $currentDir/"))
  233. {
  234. $msg = "$file unziped";
  235. } else {
  236. $msg = "Could not unzip the file";
  237. }
  238. break;
  239. }
  240. }
  241. ?>
  242. <HTML>
  243. <HEAD>
  244. <LINK REL=StyleSheet HREF="style/cm.css" TITLE=Contemporary TYPE="text/css">
  245. <SCRIPT LANGUAGE="JavaScript" SRC="include/script.js"></SCRIPT>
  246. </HEAD>
  247. <BODY>
  248. <TABLE BORDER=0 CELLPADDING=2 CELLSPACING=0 WIDTH='100%'>
  249. <TR>
  250. <TD CLASS=menu>
  251. <?php if($loggedOn) { ?>
  252. [&nbsp;&nbsp;<A CLASS=menu HREF="<?=$PHP_SELF;?>?logoff=true">Log off</A>&nbsp;&nbsp;
  253. |&nbsp;&nbsp;<A CLASS=menu HREF="javascript:changeMode('0')">ASCII Mode</A>&nbsp;&nbsp;
  254. |&nbsp;&nbsp;<A CLASS=menu HREF="javascript:changeMode('1')">Binary Mode</A>&nbsp;&nbsp;
  255. ]
  256. <?php } else { ?>
  257. [&nbsp;&nbsp;<A CLASS=menu HREF="<?=$PHP_SELF;?>?logoff=true">Retry</A>&nbsp;&nbsp;]
  258. <?php } ?>
  259. </TD>
  260. <TD CLASS=menu ALIGN=RIGHT>
  261. <FORM METHOD=POST NAME="currentMode">
  262. Current MODE :<INPUT TYPE='text' NAME='showmode' VALUE='<?=$mode==1?"FTP_BINARY":"FTP_ASCII";?>' STYLE='border: none; background-color: #cfcfbb; text-align: right; size:200px;' ALIGN=RIGHT></TD>
  263. </FORM>
  264. </TR>
  265. <TR><TD><?=$msg;?></TD><TD ALIGN=RIGHT><?php print ($loggedOn)?"Connected to $server:$port ($systype)":"Not connected";?></TD></TR>
  266. </TABLE>
  267. <FORM NAME="actionform" METHOD=POST ACTION='<?=$PHP_SELF;?>'>
  268. <INPUT TYPE='hidden' NAME='action' VALUE=''>
  269. <INPUT TYPE='hidden' NAME='currentDir' VALUE='<?=$currentDir;?>'>
  270. <INPUT TYPE='hidden' NAME='file' VALUE=''>
  271. <INPUT TYPE='hidden' NAME='file2' VALUE=''>
  272. <INPUT TYPE='hidden' NAME='permissions' VALUE=''>
  273. <INPUT TYPE='hidden' NAME='mode' VALUE='<?=$mode;?>' STYLE='border: none; background-color: #EFEFEF;'>
  274. </FORM>
  275. <HR>
  276. <TABLE CELLPADDING=2 CELLSPACING=0>
  277. <TR>
  278. <!-- Goto directory -->
  279. <FORM NAME='cdDirect' METHOD=POST ACTION='<?=$PHP_SELF;?>'>
  280. <INPUT TYPE='hidden' NAME='action' VALUE='cd'>
  281. <INPUT TYPE='hidden' NAME='currentDir' VALUE='<?=$currentDir;?>'>
  282. <TD VALIGN=TOP>
  283. <INPUT TYPE="text" NAME="file" VALUE="">
  284. </TD>
  285. <TD VALIGN=TOP>
  286. <INPUT TYPE="SUBMIT" VALUE="Go to Directory" STYLE='width=120;'>
  287. </TD>
  288. </FORM>
  289. </TR>
  290. <TR>
  291. <!-- Create directory -->
  292. <FORM METHOD=POST NAME='dirinput' ACTION="<?=$PHP_SELF;?>">
  293. <TD VALIGN=TOP>
  294. <INPUT TYPE="text" NAME="directory" VALUE="">
  295. </TD>
  296. <TD VALIGN=TOP>
  297. <INPUT TYPE="BUTTON" VALUE="Create Directory" OnClick='javascript:createDirectory(dirinput.directory.value)' STYLE='width=120;'>
  298. </TD>
  299. </FORM>
  300. </TR>
  301. <TR>
  302. <FORM NAME='putForm' ENCTYPE="multipart/form-data" METHOD=POST ACTION="<?=$PHP_SELF;?>">
  303. <INPUT TYPE="hidden" NAME="action" VALUE="put">
  304. <INPUT TYPE='hidden' NAME='currentDir' VALUE='<?=$currentDir;?>'>
  305. <INPUT TYPE="hidden" NAME="MAX_FILE_SIZE" VALUE="2000000">
  306. <INPUT TYPE='hidden' NAME='mode' VALUE='<?=$mode;?>'>
  307. <TD VALIGN=TOP>
  308. <INPUT TYPE="file" NAME="file" STYLE="width:250px;">
  309. </TD>
  310. <TD VALIGN=TOP>
  311. <INPUT TYPE="SUBMIT" VALUE="Upload file" STYLE='width=120;'>
  312. </TD>
  313. </FORM>
  314. </TR>
  315. </TABLE>
  316. <HR>
  317. <P>
  318. <?php
  319. $list=Array();
  320. $list=ftp_rawlist($connection, "");
  321. ?>
  322. <TABLE>
  323. <TR>
  324. <TD><IMG SRC="img/parent.gif" HEIGHT=20 WIDTH=20 ALIGN=TOP></TD>
  325. <TD ALIGN=LEFT COLSPAN=7><A HREF='javascript:submitForm("cd","..")'>..</A></TD>
  326. </TR>
  327. <?php
  328. $list = parse_ftp_rawlist($list, $systype);
  329. if (is_array($list))
  330. {
  331. // Directories
  332. foreach($list as $myDir)
  333. {
  334. if ($myDir["is_dir"]==1)
  335. {
  336. $fileAction = "cd";
  337. $fileName = $myDir["name"];
  338. print "<TR>\n";
  339. print "<TD><IMG SRC=img/folder.gif ALIGN=TOP></TD>\n";
  340. print "<TD><A HREF='javascript:submitForm(\"cd\",\"". $fileName . "\")'>". $myDir["name"] . "</A></TD>\n";
  341. print "<TD ALIGN=RIGHT>". $myDir["size"] . "</TD>\n";
  342. print "<TD>". $myDir["date"] . "</TD>\n";
  343. print "<TD>". $myDir["perms"] . "</TD>\n";
  344. print "<TD>". $myDir["user"] . "</TD>\n";
  345. print "<TD>". $myDir["group"] . "</TD>\n";
  346. print "<TD><A HREF='javascript:Confirmation(\"" . $PHP_SELF . "?action=deldir&file=". $myDir["name"] . "&currentDir=". $currentDir . "\")'><IMG SRC=img/delete.gif BORDER=0 ALT=\"Delete\"></A></TD>\n";
  347. print "<TD><A HREF='javascript:renameFile(\"" . $myDir["name"] . "\")'><IMG SRC=img/rename.gif BORDER=0 ALT=\"Rename\"></A></TD>\n";
  348. print "<TD>";
  349. print "<A HREF='javascript:;' OnClick='window.open(\"setpermission.php?file=" . $fileName . "&perms=" . $myDir["perms"] . "\",\"permissions\",\"width=250,height=150,scrollbars=no,menubar=no,status=yes,directories=no,location=no\")'><IMG SRC='img/settings.gif' WIDTH='20' HEIGHT='20' BORDER=0 ALT='Change permissions'></A>";
  350. print "</TD>\n";
  351. print "</TR>\n";
  352. }
  353. }
  354. // Links
  355. foreach($list as $myDir)
  356. {
  357. if ($myDir["is_link"]==1)
  358. {
  359. $fileAction = "cd";
  360. $fileName = $myDir["target"];
  361. print "<TR>\n";
  362. print "<TD><IMG SRC=img/link.gif ALIGN=TOP></TD>\n";
  363. print "<TD><A HREF='javascript:submitForm(\"cd\",\"". $fileName . "\")'>". $myDir["name"] . "</A></TD>\n";
  364. print "<TD ALIGN=RIGHT>". $myDir["size"] . "</TD>\n";
  365. print "<TD>". $myDir["date"] . "</TD>\n";
  366. print "<TD>". $myDir["perms"] . "</TD>\n";
  367. print "<TD>". $myDir["user"] . "</TD>\n";
  368. print "<TD>". $myDir["group"] . "</TD>\n";
  369. print "<TD><A HREF='javascript:Confirmation(\"" . $PHP_SELF . "?action=deldir&file=". $myDir["name"] . "&currentDir=". $currentDir . "\")'><IMG SRC=img/delete.gif BORDER=0 ALT=\"Delete\"></A></TD>\n";
  370. print "<TD><A HREF='javascript:renameFile(\"" . $myDir["name"] . "\")'><IMG SRC=img/rename.gif BORDER=0 ALT=\"Rename\"></A></TD>\n";
  371. print "<TD>";
  372. print "Symbolic link to ". $myDir["target"];
  373. print "</TD>\n";
  374. print "</TR>\n";
  375. }
  376. }
  377. // Files
  378. foreach($list as $myDir)
  379. {
  380. if ($myDir["is_link"]!=1 && $myDir["is_dir"]!=1)
  381. {
  382. $fileAction = "get";
  383. $fileName = $myDir["name"];
  384. print "<TR>\n";
  385. print "<TD><IMG SRC=img/file.gif ALIGN=TOP></TD>\n";
  386. print "<TD><A HREF='javascript:submitForm(\"get\",\"". $fileName . "\")'>". $myDir["name"] . "</A></TD>\n";
  387. print "<TD ALIGN=RIGHT>". $myDir["size"] . "</TD>\n";
  388. print "<TD>". $myDir["date"] . "</TD>\n";
  389. print "<TD>" . $myDir["perms"] . "</TD>\n";
  390. print "<TD>". $myDir["user"] . "</TD>\n";
  391. print "<TD>". $myDir["group"] . "</TD>\n";
  392. print "<TD><A HREF='javascript:Confirmation(\"" . $PHP_SELF . "?action=delfile&file=". $myDir["name"] . "&currentDir=". $currentDir . "\")'><IMG SRC=img/delete.gif BORDER=0 ALT=\"Delete\"></A></TD>\n";
  393. print "<TD><A HREF='javascript:renameFile(\"" . $myDir["name"] . "\")'><IMG SRC=img/rename.gif BORDER=0 ALT=\"Rename\"></A></TD>\n";
  394. print "<TD>";
  395. print "<A HREF='javascript:;' OnClick='window.open(\"setpermission.php?file=" . $fileName . "&perms=" . $myDir["perms"] . "\",\"permissions\",\"width=250,height=150,scrollbars=no,menubar=no,status=yes,directories=no,location=no\")'><IMG SRC='img/settings.gif' WIDTH='20' HEIGHT='20' BORDER=0 ALT='Change permissions'></A>";
  396. print "</TD>\n";
  397. if (strtolower($myDir["extension"]) == "zip")
  398. {
  399. print "<TD><A HREF='javascript:ConfirmationUnzip(\"" . $PHP_SELF . "?action=unzipfile&file=". $myDir["name"] . "&currentDir=". $currentDir . "\")'><IMG SRC=img/zip.gif BORDER=0 ALT=\"Unzip\"></A></TD>\n";
  400. } else {
  401. echo "<TD>&nbsp;</td>";
  402. }
  403. print "</TR>\n";
  404. }
  405. }
  406. }
  407. print " </TABLE>";
  408. }
  409. else
  410. {
  411. if(!isset($msg))
  412. {
  413. $msg = "Could not connect to server $server:$port with user $user<P><A HREF='" . $PHP_SELF . "?logoff=true'>Try again...</A>";
  414. }
  415. ?>
  416. <HTML>
  417. <HEAD>
  418. <LINK REL=StyleSheet HREF="style/cm.css" TITLE=Contemporary TYPE="text/css">
  419. <SCRIPT LANGUAGE="JavaScript" SRC="include/script.js"></SCRIPT>
  420. </HEAD>
  421. <BODY>
  422. <?php
  423. print $msg;
  424. }
  425. }
  426. else // Still need to logon...
  427. {
  428. ?>
  429. <HTML>
  430. <HEAD>
  431. <LINK REL=StyleSheet HREF="style/cm.css" TITLE=Contemporary TYPE="text/css">
  432. <SCRIPT LANGUAGE="JavaScript" SRC="include/script.js"></SCRIPT>
  433. </HEAD>
  434. <BODY>
  435. <TABLE BORDER=0 CELLPADDING=2 CELLSPACING=0 WIDTH='100%'>
  436. <TR>
  437. <TD CLASS=menu>
  438. <B>WebFTP Version 1.4</B>
  439. </TD>
  440. </TR>
  441. </TABLE>
  442. <FORM NAME=logon action='<?=$PHP_SELF;?>' METHOD=POST>
  443. <TABLE>
  444. <TR>
  445. <TD>Server</TD>
  446. <TD><INPUT TYPE=TEXT NAME=server SIZE=18>&nbsp;Port : <INPUT TYPE=TEXT NAME=port SIZE=6 VALUE=21></TD>
  447. </TR>
  448. <TR>
  449. <TD>User</TD>
  450. <TD>
  451. <INPUT TYPE=TEXT NAME=user SIZE=18>
  452. <INPUT TYPE="checkbox" NAME="anonymous" VALUE=1 OnClick="anonymousAccess()"> : Anonymous access
  453. </TD>
  454. </TR>
  455. <TR>
  456. <TD>Password</TD>
  457. <TD><INPUT TYPE=PASSWORD NAME=password SIZE=18></TD>
  458. </TR>
  459. <TR>
  460. <TD COLSPAN=2 ALIGN=CENTER><INPUT TYPE=SUBMIT VALUE="Log on"></TD>
  461. </TR>
  462. </TABLE>
  463. </FORM>
  464. <?php
  465. }
  466. ?>
  467. <P>
  468. <DIV ALIGN=CENTER STYLE='font-family: verdana, arial, sans-serif; font-size:10px'>
  469. <H2>Notice</H2>
  470. WebFTP comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions. Read the full GPL license <A HREF="gpl.txt">here</A>
  471. <P>
  472. WebFTP version 1.4 &copy; 2002, <A HREF="http://www.v-wijk.net/modules.php?name=Feedback">Edwin van Wijk</A>, <A HREF="http://www.v-wijk.net">www.v-wijk.net</A>
  473. </DIV>
  474. </BODY>
  475. </HTML>

comments powered by Disqus