rtoss - Blame information for rev 23

Subversion Repositories:
Rev:
Rev Author Line No. Line
4 roytam 1 <?php
23 roytam 2  
3 $dir='files/';
4 $datecache='./files.date';
5 $feedcache='./rss.xml';
6  
4 roytam 7 function properSize($size,$maxcnt=0) {
23 roytam 8         $suffix=''; $suxAry=array('KB','MB','GB','TB');
4 roytam 9         $ccnt=$maxcnt?$maxcnt:count($suxAry);
10         for($cnt=0;$cnt<$ccnt;$cnt++)
11                 if($size > 1024) {$size/=1024; $suffix=$suxAry[$cnt];}
23 roytam 12         return sprintf('%.1f',$size).$suffix;
4 roytam 13 }
14  
15 function fullURL() {
23 roytam 16         $filename=preg_replace('/.*\/+/','',$_SERVER['PHP_SELF']);
17         $path=preg_replace("/$filename$/",'',$_SERVER['PHP_SELF']);
18         return 'http://'.$_SERVER['HTTP_HOST'].$path;
4 roytam 19 }
20  
21 function _c_sort($a,$b) {
22         global $files;
23         if($files[$a]<$files[$b]) return 1;
24         elseif($files[$a]>$files[$b]) return -1;
25         else return 0;
26 }
27  
23 roytam 28 function writeFile($file,$data) {
29         $rp = fopen($file, 'w');
30         flock($rp, LOCK_EX); // 鎖定檔案
31         @fputs($rp,$data);
32         flock($rp, LOCK_UN); // 解鎖
33         fclose($rp);
34         chmod($file,0666);
35 }
36  
4 roytam 37 $url=fullURL();
38  
23 roytam 39 $filesdate=date('YmdHis', filemtime($dir));
40 $cachedate=@file_get_contents($datecache);
41 if($filesdate<=$cachedate && file_exists($feedcache)) {
42         header('Content-type: application/xml');
43         readfile($feedcache);
44         exit;
45 }
46  
47 require_once('./name.inc.php');
4 roytam 48 $files=array();
49  
50 clearstatcache();
23 roytam 51 if ($dh = opendir('./'.$dir)) {
4 roytam 52         while (($file = readdir($dh)) !== false) {
23 roytam 53                 $files[$file]=@filemtime($dir.$file);
4 roytam 54         }
55         closedir($dh);
56 }
23 roytam 57 uksort($files,'_c_sort');
58 $dat='<?xml version="1.0" encoding="utf-8"?>
4 roytam 59 <?xml-stylesheet href="rss2.xsl" type="text/xsl" media="screen"?>
60  
61 <rss version="2.0">
62  
63 <channel>
23 roytam 64   <title>yourname File Releases</title>
4 roytam 65   <link>'.$url.'</link>
23 roytam 66   <description>Latest files of yourname</description>
4 roytam 67   <ttl>720</ttl>
68 ';
69 foreach($files as $file => $props) {
70         foreach($ary as $name => $regexp) {
71                 if(preg_match($regexp,$file,$opts)) {
23 roytam 72                         $dat.="  <item>\n    <title>".htmlspecialchars(str_replace('%s',isset($opts[1])?$opts[1]:'',$name))."</title>\n    <link>".htmlspecialchars($url.$dir.$file)."</link>\n    <description>".htmlspecialchars(str_replace('%s',isset($opts[1])?$opts[1]:'',$name))."</description>\n    <pubDate>".date('r',$props)."</pubDate>\n  </item>\n";
4 roytam 73                         break;
74                 }
75         }
76 }
77  
23 roytam 78 $dat.='</channel>
79 </rss>';
4 roytam 80  
23 roytam 81 writeFile($feedcache,$dat);
82 writeFile($datecache,$filesdate);
83  
84 header('Content-type: application/xml');
85 echo $dat;
4 roytam 86 ?>