home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / PHP / filemanager.php3.txt < prev    next >
Encoding:
Text File  |  2002-05-06  |  9.6 KB  |  382 lines

  1. filemanager 
  2.  
  3. Provides a simple web-based file manager and text editor. No security included. 
  4.  
  5. <?php 
  6.  
  7. #    filemanager by Lacey Pevey
  8. #    v0.02 - 1999/12/05 13:46 CST
  9. #
  10. #    You are free to use and distribute this code provided this tiny
  11. #    header is included. No warranties, fitness for purpose, etc. etc.
  12. #
  13. #    Please send comments/bugfixes/enhancements to
  14. #    lpevey@mailhost.tcs.tulane.edu.
  15.  
  16. $home = "/home"; //include user's home directory (or whatever you want to give them access to)
  17.  
  18. include("header.inc"); 
  19.  
  20. ?>
  21.  
  22. <script language=javascript>
  23. <!--
  24. function submit(form) {
  25.     form.submit()
  26. }
  27. //-->
  28. </script>
  29.  
  30. <?php
  31.  
  32. if (isset($exit)) { unset($edit); unset($upload); }
  33.  
  34. if (empty($pwd)) $pwd = "/";
  35.  
  36. if ($action == "upload") exec("cp $userfile $home$pwd$userfile_name");
  37.  
  38. $file = $home.$pwd.$fn;
  39.  
  40. function read_file($file) {
  41.  
  42.     if (!($fp = fopen($file, 'r' ))) return false;
  43.  
  44.     $contents = fread($fp, filesize($file));
  45.  
  46.     fclose($fp);
  47.  
  48.     return $contents;
  49. }
  50.  
  51. function write_file($file, $contents) {
  52.  
  53.     if ($fp = fopen($file, "w")) {
  54.  
  55.         fputs($fp, $contents, strlen($contents));
  56.  
  57.         fclose($fp);
  58.  
  59.         return 1;
  60.  
  61.     } else { 
  62.  
  63.         return 0; 
  64.     }
  65. }
  66.  
  67. //-------------------------------------------------------------------------------------------------
  68.  
  69. if ($copy) {
  70.  
  71.     if ($action == "copy") {
  72.  
  73.         exec("cp -R ".$home.$oldname." ".$home.$newname);
  74.         
  75.     } else {
  76.  
  77. ?>
  78. <table align=center bgcolor=#999999 border=2 cellspacing=0 cellpadding=3>
  79.     <tr>
  80.         <form name=copy method=post action=<?php echo $PHP_SELF; ?>>
  81.             <td>
  82.             <input type=hidden name=oldname value="<?php echo $pwd.$fn; ?>">
  83.             <input type=hidden name=action value=copy>
  84.             <input type=hidden name="pwd" value="<?php echo $pwd; ?>">
  85.             <font face=Verdana><small>Clipboard Contents: <?php echo $pwd.$fn; ?></small></font><br>
  86.             <input type=submit name="copy" value="Copy as:">
  87.             <input type=text size="56" name="newname" value="<?php echo $pwd.$fn; ?>"><br>
  88.             <input type=submit name="exit" value="Cancel"></td>
  89.         </form>
  90.     </tr>
  91. </table><br><br>
  92. <?php
  93.  
  94.     }
  95. }
  96.  
  97. //-------------------------------------------------------------------------------------------------
  98.  
  99. if ($move) {
  100.  
  101.     if ($action == "move") {
  102.  
  103.         exec("mv ".$home.$oldname." ".$home.$newname);
  104.         
  105.     } else {
  106.  
  107. ?>
  108. <table align=center bgcolor=#999999 border=2 cellspacing=0 cellpadding=3>
  109.     <tr>
  110.         <td>
  111.         <form name=move method=post action=<?php echo $PHP_SELF; ?>>
  112.             <input type=hidden name="pwd" value="<?php echo $pwd; ?>">
  113.             <font face=Verdana><small>Clipboard Contents: <?php echo $pwd.$fn; ?></small></font><br>
  114.             <input type=submit name="move" value="Move to:">
  115.             <input type=text size="56" name="newname" value="<?php echo $pwd.$fn; ?>"><br>
  116.             <input type=hidden name=oldname value="<?php echo $pwd.$fn; ?>">
  117.             <input type=hidden name=action value=move>
  118.             <input type=submit name="exit" value="Cancel"></td></form>
  119.     </tr>
  120. </table><br><br>
  121. <?php
  122.  
  123.     }
  124. }
  125.  
  126. //-------------------------------------------------------------------------------------------------
  127.  
  128. if ($rename) {
  129.  
  130.     if ($action == "rename") {
  131.  
  132.         exec("mv ".$home.$oldname." ".$home.$newname);
  133.         
  134.     } else {
  135.  
  136. ?>
  137. <table align=center bgcolor=#999999 border=2 cellspacing=0 cellpadding=3>
  138.     <tr>
  139.         <td>
  140.         <form name=move method=post action=<?php echo $PHP_SELF; ?>>
  141.             <input type=hidden name="pwd" value="<?php echo $pwd; ?>">
  142.             <font face=Verdana><small>Clipboard Contents: <?php echo $pwd.$fn; ?></small></font><br>
  143.             <input type=submit name="rename" value="Rename to:">
  144.             <input type=text size="56" name="newname" value="<?php echo $pwd.$fn; ?>"><br>
  145.             <input type=hidden name=oldname value="<?php echo $pwd.$fn; ?>">
  146.             <input type=hidden name=action value=rename>
  147.             <input type=submit name="exit" value="Cancel"></td></form>
  148.     </tr>
  149. </table><br><br>
  150. <?php
  151.  
  152.     }
  153. }
  154.  
  155. //-------------------------------------------------------------------------------------------------
  156.  
  157. if ($newfile) {
  158.  
  159.     if (isset($fn)) {
  160.  
  161.         write_file($file, "");
  162.  
  163.         $edit = 1;
  164.  
  165.     } else {
  166.  
  167.         echo "You must specify a name for the file you wish to create.";
  168.     }
  169.  
  170.     echo $result;
  171.  
  172. }
  173.  
  174. //-------------------------------------------------------------------------------------------------
  175.  
  176. if ($newdir) {
  177.  
  178.     if (isset($newdirname)) {
  179.  
  180.         exec("mkdir ".$home.$pwd.$newdirname);
  181.  
  182.     } else {
  183.  
  184.         echo "You must specify a name for the directory you wish to create.";
  185.     }
  186.  
  187.     echo $result;
  188. }
  189.  
  190. //-------------------------------------------------------------------------------------------------
  191.  
  192. if ($delete) {
  193.  
  194.     if ($action == "delete") {
  195.  
  196.         exec("rm -R $file");
  197.         
  198.     } else {
  199.  
  200. ?>
  201. <table align=center bgcolor=#999999 border=2 cellspacing=0 cellpadding=3>
  202.     <tr>
  203.         <td>
  204.         <form name=delete method=post action=<?php echo $PHP_SELF; ?>>
  205.             <font face=Verdana><small>Delete <?php echo $pwd.$fn; ?>?</small></font>
  206.             <input type=submit name="delete" value="Yes">
  207.             <input type=submit name="exit" value="No">
  208.             <input type=hidden name="pwd" value="<?php echo $pwd; ?>">
  209.             <input type=hidden name="fn" value="<?php echo $fn; ?>">
  210.              <input type=hidden name=action value=delete></td></form>
  211.     </tr>
  212. </table><br><br>
  213. <?php
  214.  
  215.     }
  216. }
  217.  
  218. //-------------------------------------------------------------------------------------------------
  219.  
  220. if (isset($edit)) {
  221.  
  222.     if (!(isset($fn))) echo "No filename was specified.";
  223.  
  224.     else {
  225.  
  226.         if ($save) {
  227.  
  228.             if ((isset($file)) && (isset($contents))) {
  229.  
  230.                 $contents = stripslashes($contents);
  231.  
  232.                 write_file($file, $contents);
  233.  
  234.             } else {
  235.  
  236.                 echo "Error saving file to disk";
  237.             }
  238.         }
  239. ?>
  240. <table bgcolor="#999999" border=2 cellspacing=0 cellpadding=3 align=center>
  241.     <tr>
  242.         <td>Current File: <?php echo $fn; ?></td>
  243.     </tr
  244.     <tr>
  245.         <td align=center>
  246.         <form method="post" action="<?php echo $PHP_SELF; ?>">
  247.             <textarea name="contents" cols=100 rows=25><?php $contents = read_file($home.$pwd.$fn); echo htmlentities($contents); ?></textarea><br>
  248.             <input type=submit name="save" value="Save">
  249.             <input type=submit name="exit" value="Exit">
  250.             <input type=hidden name="pwd" value="<?php echo $pwd; ?>">
  251.             <input type=hidden name="fn" value="<?php echo $fn; ?>">
  252.             <input type=hidden name="edit" value="Edit"></td></form>
  253.     </tr>
  254. </table></form><br>
  255. <?php
  256.  
  257.     }
  258. }
  259.  
  260. //-------------------------------------------------------------------------------------------------
  261.  
  262. exec("ls -l ".$home.$pwd, $result, $id);
  263.  
  264. $up_pwd = eregi_replace("/$", "", $pwd);
  265.  
  266. $up_pwd = eregi_replace("[_a-z0-9\.-]+$", "", $up_pwd);
  267.  
  268. echo "
  269. <table align=center bgcolor=#999999 border=2 cellspacing=0 cellpadding=3>
  270.     <tr>
  271.         <td>Current Directory: ".$pwd."</td>
  272.     </tr>
  273.     <tr>
  274.         <td>
  275.             <table>
  276.                 <tr>
  277.                     <td colspan=2><font face=Verdana><small>Folders:</small></font><td>
  278.                 </tr>
  279.                 <tr>
  280.                     <td><form name=up action=$PHP_SELF method=post><input type=hidden name=pwd value=$up_pwd><a href=\"javascript:submit(document.forms['up']);\"><img src=/img/up.gif border=0 alt=up></a> </form></td>
  281.                     <td><form method=post action=$PHP_SELF name=folders>
  282.                     <select name=pwd>";
  283.  
  284. $i = 1;
  285.  
  286. while (isset($result[$i])) {
  287.  
  288.     $result[$i] = eregi_replace(" +", ",", $result[$i]);
  289.  
  290.     $line = explode(",", $result[$i]);
  291.  
  292.     if (ereg("^d", $line[0])) {
  293.  
  294.         echo "<option value=\"".$pwd.$line[8]."/\">".$line[8]."\n";
  295.     }
  296.  
  297.     $i++;
  298. }
  299. ?>
  300.                     </select>
  301.                     <input type=submit value="Open">
  302.                     <input type=submit name="move" value="Move">
  303.                     <input type=submit name="copy" value="Copy">
  304.                     <input type=submit name="delete" value="Delete">
  305.                     <input type=submit name="rename" value="Rename">
  306.                     </form></td>
  307.                 </tr>
  308.             </table></td>
  309.     </tr>
  310.     <tr>
  311.         <td>
  312.             <form method=post action="<?php echo $PHP_SELF; ?>" name="files">
  313.             <font face=Verdana><small>Files:</small></font><br>
  314.             <select name=fn>
  315. <?php
  316.  
  317. $i = 1;
  318.  
  319. while (isset($result[$i])) {
  320.  
  321.     $result[$i] = eregi_replace(" +", ",", $result[$i]);
  322.  
  323.     $line = explode(",", $result[$i]);
  324.  
  325.     if (!(ereg("^d", $line[0]))) {
  326.  
  327.         echo "<option value=\"".$line[8]."\">".$line[8]."\n";
  328.     }
  329.  
  330.     $i++;
  331. }
  332. ?>
  333.             </select>
  334.             <input type=hidden name="pwd" value="<?php echo $pwd; ?>">
  335.             <input type=submit name="edit" value="Edit">
  336.             <input type=submit name="move" value="Move">
  337.             <input type=submit name="copy" value="Copy">
  338.             <input type=submit name="delete" value="Delete">
  339.             <input type=submit name="rename" value="Rename">
  340.             </form></td>
  341.     </tr>
  342.     <tr>
  343.         <td>
  344.             <table width=100%>
  345.                 <tr>
  346.                     <td width=50% nowrap>    
  347.                         <form method=post action="<?php echo $PHP_SELF; ?>" name="newfile">
  348.                         <input type=hidden name="pwd" value="<?php echo $pwd; ?>">
  349.                         <font face=Verdana><small>New File:</small></font><br>
  350.                          <input type=text name=fn size=20 maxlength=100>
  351.                         <input type=submit name=newfile value="Create">
  352.                         </form></td>
  353.                     <td width=50% nowrap>
  354.                         <form method=post action="<?php echo $PHP_SELF; ?>" name="newfolder">
  355.                         <input type=hidden name="pwd" value="<?php echo $pwd; ?>">
  356.                         <font face=Verdana><small>New Folder:</small></font><br>
  357.                          <input type=text name=newdirname size=20 maxlength=100>
  358.                         <input type=submit name=newdir value="Create">
  359.                         </form></td>
  360.                 </tr>
  361.             </table></td>
  362.     </tr>
  363.     <tr>
  364.         <td>
  365.             <form method=post enctype="multipart/form-data" action=<?php echo $PHP_SELF; ?>>
  366.             <font face=Verdana><small>File Upload:</small></font><br>
  367.             <input type=hidden name="pwd" value=<?php echo $pwd; ?>>
  368.             <input type=hidden name="action" value=upload>
  369.             <input type=file name=userfile size=40>
  370.             <input name=upload value=" Upload " type=submit>
  371.             </form></td>
  372.     </tr>
  373. </table>
  374. <br>
  375. <br>
  376.  
  377. <?php 
  378.  
  379. include("footer.inc"); 
  380.  
  381. ?>
  382.