国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

202 line
6.4KB

  1. <?php
  2. if (!defined('DEDEINC')) exit ('dedebiz');
  3. /**
  4. * 文件管理逻辑类
  5. *
  6. * @version $id:file_class.php 19:09 2010年7月12日 tianya $
  7. * @package DedeBIZ.Administrator
  8. * @copyright Copyright (c) 2022 DedeBIZ.COM
  9. * @license GNU GPL v2 (https://www.dedebiz.com/license)
  10. * @link https://www.dedebiz.com
  11. */
  12. class FileManagement
  13. {
  14. var $baseDir = '';
  15. var $activeDir = '';
  16. //是否允许文件管理器删除目录,默认为不允许0,如果希望管理整个目录,请把值设为1
  17. var $allowDeleteDir = 0;
  18. //初始化系统
  19. function Init()
  20. {
  21. global $cfg_basedir, $activepath;
  22. $this->baseDir = $cfg_basedir;
  23. $this->activeDir = $activepath;
  24. }
  25. //修改文件名
  26. function RenameFile($oldname, $newname)
  27. {
  28. $oldname = $this->baseDir.$this->activeDir."/".$oldname;
  29. $newname = $this->baseDir.$this->activeDir."/".$newname;
  30. $oldext = pathinfo($oldname)['extension'];
  31. $newext = pathinfo($newname)['extension'];
  32. if ($oldext != $newext) {
  33. if (preg_match('#\.(php|pl|cgi|asp|aspx|jsp|php5|php4|php3|shtm|shtml|inc|htm)$#i', trim($newname))) {
  34. ShowMsg("文件扩展名已被系统禁止", "javascript:;");
  35. exit();
  36. }
  37. }
  38. if (($newname != $oldname) && is_writable($oldname)) {
  39. rename($oldname, $newname);
  40. }
  41. ShowMsg("成功修改一个文件名", "file_manage_main.php?activepath=".$this->activeDir);
  42. return 0;
  43. }
  44. //创建新目录
  45. function NewDir($dirname)
  46. {
  47. $newdir = $dirname;
  48. $dirname = $this->baseDir.$this->activeDir."/".$dirname;
  49. if (is_writable($this->baseDir.$this->activeDir)) {
  50. MkdirAll($dirname, $GLOBALS['cfg_dir_purview']);
  51. ShowMsg("成功创建一个新目录", "file_manage_main.php?activepath=".$this->activeDir."/".$newdir);
  52. return 1;
  53. } else {
  54. ShowMsg("创建新目录失败,因为这个位置不允许写入", "file_manage_main.php?activepath=".$this->activeDir);
  55. return 0;
  56. }
  57. }
  58. /**
  59. * 移动文件
  60. *
  61. * @access public
  62. * @param string $mfile 文件
  63. * @param string $mpath 路径
  64. * @return string
  65. */
  66. function MoveFile($mfile, $mpath)
  67. {
  68. if ($mpath != "" && !preg_match("#\.\.#", $mpath)) {
  69. $oldfile = $this->baseDir.$this->activeDir."/$mfile";
  70. $mpath = str_replace("\\", "/", $mpath);
  71. $mpath = preg_replace("#\/{1,}#", "/", $mpath);
  72. if (!preg_match("#^/#", $mpath)) {
  73. $mpath = $this->activeDir."/".$mpath;
  74. }
  75. $truepath = $this->baseDir.$mpath;
  76. if (is_readable($oldfile) && is_readable($truepath) && is_writable($truepath)) {
  77. if (is_dir($truepath)) {
  78. copy($oldfile, $truepath."/$mfile");
  79. } else {
  80. MkdirAll($truepath, $GLOBALS['cfg_dir_purview']);
  81. copy($oldfile, $truepath."/$mfile");
  82. }
  83. unlink($oldfile);
  84. ShowMsg("成功移动文件", "file_manage_main.php?activepath=$mpath");
  85. return 1;
  86. } else {
  87. ShowMsg("移动文件".$oldfile." - ".$truepath."/".$mfile."失败", "file_manage_main.php?activepath=$mpath");
  88. return 0;
  89. }
  90. } else {
  91. ShowMsg("您移动的路径不合法", "-1");
  92. return 0;
  93. }
  94. }
  95. /**
  96. * 删除目录
  97. *
  98. * @param string $indir
  99. */
  100. function RmDirFiles($indir)
  101. {
  102. if (!is_dir($indir)) {
  103. return;
  104. }
  105. $dh = dir($indir);
  106. while ($filename = $dh->read()) {
  107. if ($filename == "." || $filename == "..") {
  108. continue;
  109. } else if (is_file("$indir/$filename")) {
  110. @unlink("$indir/$filename");
  111. } else {
  112. $this->RmDirFiles("$indir/$filename");
  113. }
  114. }
  115. $dh->close();
  116. @rmdir($indir);
  117. }
  118. /**
  119. * 获得某目录合符规则的文件
  120. *
  121. * @param string $indir
  122. * @param string $fileexp
  123. * @param array $filearr
  124. */
  125. function GetMatchFiles($indir, $fileexp, &$filearr)
  126. {
  127. $dh = dir($indir);
  128. while ($filename = $dh->read()) {
  129. $truefile = $indir.'/'.$filename;
  130. if ($filename == "." || $filename == "..") {
  131. continue;
  132. } else if (is_dir($truefile)) {
  133. $this->GetMatchFiles($truefile, $fileexp, $filearr);
  134. } else if (substr($filename, -strlen($fileexp)) === $fileexp) {
  135. $filearr[] = $truefile;
  136. }
  137. }
  138. $dh->close();
  139. }
  140. /**
  141. * 删除文件
  142. *
  143. * @param string $filename
  144. * @return int
  145. */
  146. function DeleteFile($filename)
  147. {
  148. $filename = str_replace("..", "", $filename);
  149. $filename = $this->baseDir.$this->activeDir."/$filename";
  150. if (is_file($filename)) {
  151. @unlink($filename);
  152. $t = "文件";
  153. } else {
  154. $t = "目录";
  155. if ($this->allowDeleteDir == 1) {
  156. $this->RmDirFiles($filename);
  157. } else {
  158. ShowMsg("系统禁止删除".$t."", "file_manage_main.php?activepath=".$this->activeDir);
  159. exit;
  160. }
  161. }
  162. ShowMsg("成功删除一个".$t."", "file_manage_main.php?activepath=".$this->activeDir);
  163. return 0;
  164. }
  165. }
  166. //目录文件大小检测类
  167. class SpaceUse
  168. {
  169. var $totalsize = 0;
  170. function checksize($indir)
  171. {
  172. $dh = dir($indir);
  173. while ($filename = $dh->read()) {
  174. if (!preg_match("#^\.#", $filename)) {
  175. if (is_dir("$indir/$filename")) {
  176. $this->checksize("$indir/$filename");
  177. } else {
  178. $this->totalsize = $this->totalsize + filesize("$indir/$filename");
  179. }
  180. }
  181. }
  182. }
  183. function setkb($size)
  184. {
  185. $size = $size / 1024;
  186. if ($size > 0) {
  187. list($t1, $t2) = explode(".", $size);
  188. $size = $t1.".".substr($t2, 0, 1);
  189. }
  190. return $size;
  191. }
  192. function setmb($size)
  193. {
  194. $size = $size / 1024 / 1024;
  195. if ($size > 0) {
  196. list($t1, $t2) = explode(".", $size);
  197. $size = $t1.".".substr($t2, 0, 2);
  198. }
  199. return $size;
  200. }
  201. }
  202. ?>