PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/php/dir.php

https://github.com/pipifuyj/global
PHP | 96 lines | 94 code | 0 blank | 2 comment | 8 complexity | 8cd6794c52951142522899e35e401a63 MD5 | raw file
Possible License(s): AGPL-3.0, LGPL-2.1
  1. <?php
  2. //version: Liu ChuanRen, 02/24/08
  3. require_once(dirname(__file__)."/../gui/window.php");
  4. class dir{
  5. var $dirname=".";
  6. var $queryString="";
  7. var $window;
  8. /*chuanren*/
  9. var $request;
  10. var $requestName="dir";
  11. var $requestValue;
  12. function url($values){
  13. $names=array("action","file");
  14. $url="?".$this->queryString."&";
  15. while(list($k,$v)=each($names)){
  16. if(isset($values[$v])){
  17. $url.=$this->requestName."[$v]=".$values[$v];
  18. }else{
  19. $url.=$this->requestName."[$v]=".$this->requestValue[$v];
  20. }
  21. $url.="&";
  22. }
  23. return $url;
  24. }
  25. function ls(){
  26. $result="";
  27. $dir=dir($this->dirname);
  28. $result.="<table><thead><tr><td colspan=\"4\"><form action=\"".$this->url(array("action"=>"upload"))."\" method=\"post\" enctype=\"multipart/form-data\"><input name=\"".$this->requestName."[file]\" type=file /><input type=submit value='Upload New File' /></form></td></tr><tr align=center><td>Type</td><td>Size(B)</td><td>Time</td><td>Name</td></tr></thead><tbody>\r\n";
  29. while($name=$dir->read()){
  30. if(ereg("^\.{1,2}$",$name))continue;
  31. $f=$this->dirname."/".$name;
  32. $type=is_dir($f)?"Dir":"File";
  33. $size=filesize($f);
  34. $time=date("Y-m-d H:i:s",filemtime($f));
  35. $result.="<tr ondblclick=\"if(confirm('Delete This Item?\\r\\nUnrecoverable!'))location='".$this->url(array("action"=>"rm","file"=>$name))."';\"><td>$type</td><td>$size</td><td>$time</td><td><a href=\"".$this->url(array("action"=>"download","file"=>$name))."&global[mainOnly]=yes\">$name</a></td></tr>\r\n";
  36. }
  37. $dir->close();
  38. $result.="</tbody><tfoot><tr><td colspan='4'>dblclick a file row to DELETE it!</td></tr></tfoot></table>";
  39. print($result);
  40. }
  41. function rm(){
  42. $url=$this->url(array("action"=>"ls"));
  43. if(unlink($this->dirname."/".$this->requestValue['file'])){
  44. window::alert("<a href=\"$url\">Delete Successfully.</a>");
  45. }else{
  46. window::alert("<a href=\"$url\">Failed.</a>");
  47. }
  48. }
  49. function download(){
  50. ob_clean();
  51. header("Content-type: application/octet-stream");
  52. header("Content-Disposition: attatchment;filename=\"".$this->requestValue['file']."\"");
  53. $result=file_get_contents($this->dirname."/".$this->requestValue['file']);
  54. print($result);
  55. die;
  56. }
  57. function upload(){
  58. $name=$this->dirname."/".$this->requestValue['file'];
  59. $url=$this->url(array("action"=>"ls"));
  60. if(file_exists($name)){
  61. window::alert("<a href=\"$url\">This File Already Exists.</a>");
  62. }else{
  63. if(copy($_FILES[$this->requestName]['tmp_name']['file'],$name)){
  64. window::alert("<a href=\"$url\">Upload Successfully.</a>");
  65. }else{
  66. window::alert("<a href=\"$url\">Failed.</a>");
  67. }
  68. }
  69. }
  70. function handleRequest($request){
  71. $this->request=$request;
  72. $this->requestValue=$this->request[$this->requestName];
  73. if($_FILES[$this->requestName])$this->requestValue['file']=$_FILES[$this->requestName]['name']['file'];
  74. if(!$this->requestValue['action'])$this->requestValue['action']="ls";
  75. $url=$this->url(array("action"=>"ls"));
  76. if(!ereg("^[0-9a-zA-Z\.\_]{0,20}$",$this->requestValue['file'])||ereg("php",$this->requestValue['file'])){
  77. $this->requestValue['action']=null;
  78. window::alert("<a href=\"$url\">Your File Name(".$this->requestValue['file'].") Is Illegal.</a>");
  79. }
  80. switch($this->requestValue['action']){
  81. case 'ls':
  82. $this->ls();
  83. break;
  84. case 'rm':
  85. $this->rm();
  86. break;
  87. case 'download':
  88. $this->download();
  89. break;
  90. case 'upload':
  91. $this->upload();
  92. break;
  93. }
  94. }
  95. }
  96. ?>