function.php
上传用户:yttaitong
上传日期:2009-05-10
资源大小:128k
文件大小:14k
源码类别:

WEB源码(ASP,PHP,...)

开发平台:

PHP

  1. <?php
  2. /*
  3. --------------------------
  4. Copyright by T.muqiao(39号天堂桥)
  5. 本程序仅供学习讨论使用,在未通知作者作为任何商业用途
  6.          视为中华人民共和国不道德公民
  7. 联系方式:442536278@qq.com
  8. -------------------------*/
  9. if(!defined('IN_SELF')){
  10. //header('HTTP/1.1 404 not found');
  11. exit("Access Denied!");
  12. }
  13. class ZipAllFloder{//压缩文件夹
  14.     
  15. var $datasec      = array();
  16. var $ctrl_dir     = array();
  17. var $eof_ctrl_dir = "x50x4bx05x06x00x00x00x00";
  18. var $old_offset   = 0; 
  19. var $dirs         = array(".","");
  20. var $dirlen       = 0;
  21.     
  22. function IniVars(){//初始化变量
  23.         $this -> datasec      = array();
  24.         $this -> ctrl_dir     = array();
  25.         $this -> eof_ctrl_dir = "x50x4bx05x06x00x00x00x00";
  26.         $this -> old_offset   = 0;
  27.         $this -> dirs         = array(".","");
  28. $this -> dirlen       = 0;
  29.     }
  30.     function ZipFolder($dir,$zipfilename){//将所有二进制数据转化为16进制数据写入到压缩文件 
  31.         if(substr($dir,-1)!="/"){
  32. $dir .= "/";
  33. }
  34. $this -> dirlen = strlen($dir); 
  35.         $this -> AddFolderContent($dir);
  36.         $out = $this -> get_file();
  37.         $this -> IniVars();
  38.         $fp = fopen($zipfilename,"w");  
  39.         fwrite($fp,$out,strlen($out));  
  40.         fclose($fp);
  41.     }
  42.     function AddFolderContent($dir){//添加文件夹中文件及文件夹内容
  43.         if(is_dir($dir)){
  44. $folder = substr($dir,$this ->dirlen);
  45.             if(!in_array($folder,$this->dirs)){
  46.                 $this->add_Dir($folder);
  47.             }
  48.             $handle = opendir($dir);
  49.             do{
  50.                 $files = readdir($handle);
  51. if (($files==".")||($files=="..")) continue; 
  52.                 if(is_dir($dir.$files)&&$files!=""){
  53.                     $this->AddFolderContent($dir.$files."/");
  54.                 }elseif(is_file($dir.$files)){
  55.                     $fp = fopen ($dir.$files,"r");
  56.                     $content = @fread ($fp,filesize($dir.$files));  
  57.                     fclose ($fp);  
  58.                     $this->add_File($content,$folder.$files);
  59.                 }
  60.             }while($files!="");  
  61.             closedir($handle);
  62.         }
  63.     }
  64. function get_file(){//获得压缩文件数据
  65.         $data = implode('', $this -> datasec);
  66.         $ctrldir = implode('', $this -> ctrl_dir);
  67.         return $data . $ctrldir . $this -> eof_ctrl_dir .
  68.         pack('v', sizeof($this -> ctrl_dir)).pack('v', sizeof($this -> ctrl_dir)).
  69.         pack('V', strlen($ctrldir)) . pack('V', strlen($data)) . "x00x00";
  70.     }
  71.     function add_dir($name){ 
  72.         $name   = str_replace("\", "/", $name); 
  73.         $fr     = "x50x4bx03x04x0ax00x00x00x00x00x00x00x00x00"; 
  74.         $fr    .= pack("V",0).pack("V",0).pack("V",0).pack("v", strlen($name) ); 
  75.         $fr    .= pack("v", 0 ).$name.pack("V", 0).pack("V", 0).pack("V", 0); 
  76.         $this -> datasec[] = $fr;
  77.         $new_offset = strlen(implode("", $this->datasec)); 
  78.         $cdrec = "x50x4bx01x02x00x00x0ax00x00x00x00x00x00x00x00x00"; 
  79.         $cdrec .= pack("V",0).pack("V",0).pack("V",0).pack("v", strlen($name) ); 
  80.         $cdrec .= pack("v", 0 ).pack("v", 0 ).pack("v", 0 ).pack("v", 0 ); 
  81.         $ext = "xffxffxffxff"; 
  82.         $cdrec .= pack("V", 16 ).pack("V", $this -> old_offset ).$name; 
  83.         $this -> ctrl_dir[] = $cdrec; 
  84.         $this -> old_offset = $new_offset; 
  85.         $this -> dirs[] = $name;
  86.     }
  87.     function add_File($data, $name, $compact = 1){
  88.         $name     = str_replace('\', '/', $name);
  89.         $dtime    = dechex($this->DosTime());
  90.         $hexdtime = 'x' . $dtime[6] . $dtime[7].'x'.$dtime[4] . $dtime[5] . 'x' . $dtime[2] 
  91. . $dtime[3].'x'.$dtime[0].$dtime[1];
  92.         eval('$hexdtime = "' . $hexdtime . '";');
  93.         if($compact){
  94.             $fr = "x50x4bx03x04x14x00x00x00x08x00".$hexdtime;
  95.         }else{
  96.             $fr = "x50x4bx03x04x0ax00x00x00x00x00".$hexdtime;
  97.         }
  98.         $unc_len = strlen($data); $crc = crc32($data);
  99.         if($compact){
  100.             $zdata = gzcompress($data); $c_len = strlen($zdata);
  101.             $zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2);
  102.         }else{
  103.             $zdata = $data;
  104.         }
  105.         $c_len=strlen($zdata);
  106.         $fr .= pack('V', $crc).pack('V', $c_len).pack('V', $unc_len);
  107.         $fr .= pack('v', strlen($name)).pack('v', 0).$name.$zdata;
  108.         $fr .= pack('V', $crc).pack('V', $c_len).pack('V', $unc_len);
  109.         $this -> datasec[] = $fr;
  110.         $new_offset        = strlen(implode('', $this->datasec));
  111.         if($compact){
  112.             $cdrec = "x50x4bx01x02x00x00x14x00x00x00x08x00";
  113.         }else{ 
  114.             $cdrec = "x50x4bx01x02x14x00x0ax00x00x00x00x00";
  115.         }
  116.         $cdrec .= $hexdtime.pack('V', $crc).pack('V', $c_len).pack('V', $unc_len);
  117.         $cdrec .= pack('v', strlen($name) ).pack('v', 0 ).pack('v', 0 );
  118.         $cdrec .= pack('v', 0 ).pack('v', 0 ).pack('V', 32 );
  119.         $cdrec .= pack('V', $this -> old_offset );
  120.         $this -> old_offset = $new_offset;
  121.         $cdrec .= $name;
  122.         $this -> ctrl_dir[] = $cdrec;
  123.         return true;
  124.     }
  125.     function DosTime() {
  126.         $timearray = getdate();
  127.         if ($timearray['year'] < 1980) {
  128.             $timearray['year'] = 1980; $timearray['mon'] = 1;
  129.             $timearray['mday'] = 1; $timearray['hours'] = 0;
  130.             $timearray['minutes'] = 0; $timearray['seconds'] = 0;
  131.         }
  132.         return (($timearray['year']-1980)<<25)|($timearray['mon']<<21)|
  133. ($timearray['mday']<<16)|($timearray['hours']<<11)|($timearray['minutes']<<5)|
  134. ($timearray['seconds']>>1);
  135.     }
  136. }
  137. class StatFolder{//统计文件夹内容
  138. var $Num       = 0;
  139. var $Size      = 0;
  140. var $TypeNum   = 0;
  141. var $FolderNum = 0;
  142. var $Type      = array();
  143. function __construct($folder){
  144. $this -> ReadFolder($folder);
  145. }
  146. function StatFolder($folder){
  147. $this -> ReadFolder($folder);
  148. }
  149. function ReadFolder($folder){
  150. if(is_dir($folder)){
  151. $handle = opendir($folder);
  152.             do{
  153.                 $files = readdir($handle);
  154. if (($files==".")||($files=="..")) continue; 
  155.                 if(is_dir($folder."/".$files)&&$files!=""){
  156. $this ->FolderNum++;
  157.                     $this ->ReadFolder($folder."/".$files);
  158.                 }elseif(is_file($folder."/".$files)){
  159. $this ->Num++;
  160. $type = fileext($files);
  161. if(!in_array($type,$this ->Type)){
  162. $this ->Type[] = $type;
  163. $this ->TypeNum++;
  164. }
  165.                     $size = filesize($folder."/".$files);  
  166.                     $this->Size +=$size;
  167. clearstatcache();
  168.                 }
  169.             }while($files!="");  
  170.             closedir($handle);
  171.         }
  172. }
  173. function GetFileNum(){
  174. return $this -> Num;
  175. }
  176. function GetTotalSize(){
  177. $switch = $this->Size/1024;
  178. if($switch<1024){
  179. $return = explode(".",$switch);
  180. if($return[0]==0){
  181. return $this->Size." bytes";
  182. }
  183. return $return[0].".".substr($return[1],0,3)." Kbytes";
  184. }else{
  185. $switch /=1024;
  186. $return = explode(".",$switch);
  187. return $return[0].".".substr($return[1],0,3)." Mbytes";
  188. }
  189. }
  190. function GetTypeNum(){
  191. return $this -> TypeNum;
  192. }
  193. function GetFolderNum(){
  194. return $this -> FolderNum;
  195. }
  196. }
  197. function matchpath($reference,$testpath){//返回$testpath与$reference参照路径匹配度 
  198. $reference = explode("/",$reference);
  199. $testpath  = explode("/",$testpath);
  200. $num1 = count($reference);
  201. $num2 = count($testpath);
  202. if($num1 < $num2){
  203. return false;
  204. }
  205. $degree = 0;
  206. for($i=0;$i<$num2;$i++){
  207. if(strtolower($testpath[$i])==strtolower($reference[$i])){
  208. $degree++;
  209. continue;
  210. }else{
  211. return false;
  212. break;
  213. }
  214. }
  215. if($degree==$num2){
  216. return true;
  217. }
  218. }
  219. function checkcore($path){//检查核心文件是否包含其中
  220. global $T_corefile;//核心文件中没有哪个项是"0"的,否则循环条件要换
  221. for($i=0;$p=$T_corefile[$i];$i++){
  222. $absps = visualpath(truepath($p,2));
  223. if(matchpath($absps,$path)){
  224. return true;
  225. break;
  226. }
  227. }
  228. }
  229. function copypath($source,$target,$uncover=true){//拷贝整个目录   默认不允许覆盖
  230. global $T_lang;
  231. if(is_dir($target)){
  232. if($uncover){
  233. $errors .= str_replace("--replace--",$target,$T_lang['func'][0]);
  234. $gocover = false;
  235. }else{
  236. $gocover = true;
  237. }
  238. }else{
  239. if(matchpath($target,$source)){//检测目录$source是否包含于$target
  240. $errors .= str_replace("--replace--",$source,$T_lang['func'][1]);
  241. $gocover = false;
  242. }else{
  243. if(@mkdir($target,0777)){
  244. $gocover = true;
  245. }else{
  246. $errors .= str_replace("--replace--",$target,$T_lang['func'][2]);
  247. $gocover = false;
  248. }
  249. }
  250. }
  251. if($gocover){
  252. $handle = opendir($source);
  253. do{
  254. $file = readdir($handle);
  255. clearstatcache();
  256. if(substr($file,-1)=="."||substr($file,-2)=="..") continue;
  257. if(is_dir($source."/".$file)&&$file!=""){
  258. $errors .= copypath($source."/".$file,$target."/".$file,$uncover);
  259. }elseif(is_file($source."/".$file)){
  260. $gocopy = true;
  261. if(file_exists($target."/".$file)){
  262. if($uncover){
  263. $gocopy = false;
  264. }else{
  265. if(@unlink($target."/".$file)){
  266. $gocopy = true;
  267. }else{
  268. $gocopy = false;
  269. }
  270. }
  271. }
  272. if($gocopy){
  273. if(@copy($source."/".$file,$target."/".$file)){
  274. $errors .= "";
  275. }else{
  276. $errors .= str_replace("--replace--",$source."/".$file,$T_lang['func'][3]);
  277. }
  278. }else{
  279. $errors .= str_replace("--replace--",$target."/".$file,$T_lang['func'][4]);
  280. }
  281. }
  282. }while($file!="");
  283. closedir($handle);
  284. }
  285. if($errors){
  286. return $errors;
  287. }else{
  288. return "";
  289. }
  290. }
  291. function movepath($source,$target,$uncover=true){//移动整个目录  默认不允许覆盖
  292. global $T_lang;
  293. if(is_dir($target)){
  294. if($uncover){
  295. $errors .= str_replace("--replace--",$target,$T_lang['func'][0]);
  296. $gocover = false;
  297. }else{
  298. $gocover = true;
  299. }
  300. }else{
  301. if(matchpath($target,$source)){//检测目录$source是否包含于$target
  302. $errors .= str_replace("--replace--",$source,$T_lang['func'][5]);
  303. $gocover = false;
  304. }else{
  305. if(@mkdir($target,0777)){
  306. $gocover = true;
  307. }else{
  308. $errors .= str_replace("--replace--",$target,$T_lang['func'][2]);
  309. $gocover = false;
  310. }
  311. }
  312. }
  313. if($gocover){
  314. $handle = opendir($source);
  315. do{
  316. $file = readdir($handle);
  317. clearstatcache();
  318. if(substr($file,-1)=="."||substr($file,-2)=="..") continue;
  319. if(is_dir($source."/".$file)&&$file!=""){
  320. $errors .= movepath($source."/".$file,$target."/".$file,$uncover);
  321. }elseif(is_file($source."/".$file)){
  322. $gocopy = true;
  323. if(file_exists($target."/".$file)){
  324. if($uncover){
  325. $gocopy = false;
  326. }else{
  327. $gocopy = true;
  328. }
  329. }
  330. if($gocopy){
  331. if(@copy($source."/".$file,$target."/".$file)){
  332. unlink($source."/".$file);
  333. }else{
  334. $errors .= str_replace("--replace--",$source."/".$file,$T_lang['func'][3]);
  335. }
  336. }else{
  337. $errors .= str_replace("--replace--",$target."/".$file,$T_lang['func'][6]);
  338. }
  339. }
  340. }while($file!="");
  341. closedir($handle);
  342. }
  343. if($errors){
  344. return $errors;
  345. }else{
  346. if(@rmdir($source."/".$file)){
  347. return "";
  348. }else{
  349. $errors .= str_replace("--replace--",$source,$T_lang['func'][7]);
  350. }
  351. }
  352. }
  353. function delpath($source){//删除整个目录
  354. global $T_lang,$T_Recycled;
  355. $handle = opendir($source);
  356. do{
  357. $file = readdir($handle);
  358. clearstatcache();
  359. if(substr($file,-1)=="."||substr($file,-2)=="..") continue;
  360. if(is_dir($source."/".$file)&&$file!=""){
  361. $errors .= delpath($source."/".$file);
  362. }elseif(is_file($source."/".$file)){
  363. if(@unlink($source."/".$file)){
  364. }else{
  365. $errors .= str_replace("--replace--",$source,$T_lang['func'][7]);
  366. }
  367. }
  368. }while($file!="");
  369. closedir($handle);
  370. if($errors){
  371. $errors .= str_replace("--replace--",$source,$T_lang['func'][8]);
  372. return $errors;
  373. }else{
  374. if($source==$T_Recycled){//清空回收站时 这个判断条件用的上
  375. return "";
  376. }else{
  377. if(@rmdir($source)){
  378. return "";
  379. }else{
  380. $errors .= str_replace("--replace--",$source,$T_lang['func'][8]);
  381. return $errors;
  382. }
  383. }
  384. }
  385. }
  386. function limitname($title,$limitlength =13){//限制标题长度
  387. $length = strlen($title);
  388. if($length>$limitlength){
  389. $gbk = 0;
  390. for($i=0;$i<$limitlength-1;$i++){
  391. $temp = substr($title,$i,1);
  392. if(ord($temp)>127) $gbk +=1;
  393. }
  394. if($gbk%2){
  395. $title = substr($title,0,$limitlength)."...";
  396. }else{
  397. $title = substr($title,0,$limitlength-1)."...";
  398. }
  399. }
  400. return $title;
  401. }
  402. function fileext($filename){
  403. return trim(substr(strrchr($filename, '.'), 1));
  404. }
  405. function checkpath($path){//检查path合法和合格性(返回符合本程序的正确路径) 
  406. $path = preg_replace("/^[.](.*)/","root\1",$path);
  407. //$path = str_replace(".","",$path);
  408. $path = str_replace("\","/",$path);
  409. $path = ereg_replace("/{1,}","/",$path);
  410. $path = preg_replace("/(root.*)?[/]$/i","\1",$path);
  411. if(is_dir(truepath($path))){
  412. return $path;
  413. }else{
  414. return false;
  415. }
  416. }
  417. function truepath($path,$type=1){//将'root'、'.'替换成真实路径
  418. global $T_basedir,$T_rootdir;
  419. if($type==1){
  420. return preg_replace("/^(root|.)(.*)/i",$T_rootdir."\2",$path);
  421. }elseif($type==2){//将程序文件前面的点换成$T_basedir
  422. return preg_replace("/^(root|.)(.*)/i",$T_basedir."\2",$path);
  423. }
  424. }
  425. function direction($path){//返回当前路径父路径
  426. if($path=="root"||$path=="."){
  427. $dirname = "root";
  428. }else{
  429. $dirname = dirname($path);
  430. $dirname = preg_replace("/^.(.*)/","root\1",$dirname);
  431. }
  432. return $dirname;
  433. }
  434. function visualpath($path){
  435. global $T_rootdir;
  436. return str_replace($T_rootdir,"root",$path);
  437. }
  438. function historypath($path){//历史路径 路径长度超过给定的$T_rlength后只显示后面的$T_rlength长度
  439. global $T_rlength,$T_images,$T_main,$T_Recycled;
  440. $ruote = explode("/",$path);
  441. $num   = count($ruote);
  442. if($num<=1){
  443. return;
  444. }else{
  445. if($num-1>$T_rlength){
  446. $i = $num-$T_rlength;
  447. }else{
  448. $i = 1;
  449. }
  450.     for(;$i<=$num-1;$i++){
  451. $rs .= "<a href="{$T_main}?path=";
  452. $single = "root";
  453. for($j=1;$j<=$i;$j++){
  454. $single .= "/".$ruote[$j];
  455. }
  456. $rs .= urlencode($single)."">";
  457. $rs .=limitname($ruote[$i])."</a>>>";
  458. }
  459. return $rs;
  460. }
  461. }
  462. ?>