/
rss.php
168 lines (143 loc) · 4.14 KB
/
rss.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/*
* Worldvisions Weaver Software:
* Copyright (C) 1997-2005 Net Integration Technologies, Inc.
*
* Dave's cheesy webloggy thing's RSS spewer
*
*/
Header( 'Content-type: text/xml' );
include( 'config.inc.php' );
include( 'defs.inc.php' );
include( 'parser.inc.php' );
function escape( $string )
{
global $absoluteurl;
global $topdir;
return( ereg_replace( '<', '<',
ereg_replace( '>', '>',
ereg_replace( '"', '"',
ereg_replace( '&', '&',
ereg_replace( '\.\/', "$absoluteurl/$topdir/",
ereg_replace( '\.\.\/', "$absoluteurl/", $string ) ) ) ) ) ) );
}
function tags_match( $file )
{
if( $_GET["tags"] == "" ) return true;
$want_tags = split( "[ \t\r\n,]", $_GET["tags"] );
$lines = file( $file );
foreach( $lines as $line ) {
if( substr( $line, 0, 2 ) == "::" ) {
$tags = split( "[ \t\r\n,]", substr( $line, 2 ) );
var_dump( $tags );
foreach( $tags as $tag ) {
if( in_array( $tag, $want_tags ) ) return true;
}
}
}
return false;
}
function do_entry( $file, $yearmonth, $day )
{
global $months;
global $absoluteurl;
global $topdir;
global $firstdate;
$s = "";
$monthnum = substr( $yearmonth, 4, 2 );
$year = substr( $yearmonth, 0, 4 );
// $title = "$months[$monthnum] $day, $year";
if( !tags_match( $file ) )
return "";
$title = sprintf( "%04d-%02d-%02d", $year, $monthnum, $day );
$intitle = get_entrytitle( $yearmonth, $day );
$mtime = filemtime( $file );
if ( $intitle )
$title = $intitle;
$s .= " <item>\n" .
" <title>$title</title>\n" .
" <pubDate>" . date( "r", $mtime ) . "</pubDate>\n" .
" <link>$absoluteurl/$topdir/?m=$yearmonth#$day</link>\n" .
" <guid isPermaLink=\"true\">$absoluteurl/$topdir/?m=$yearmonth#$day</guid>\n" .
" <description>";
$entrylines = do_entrycontent( $yearmonth, $day );
if (preg_match('@^\s*<b>.*</b>\s*$@', $entrylines[0]))
$entrylines[0] = "";
foreach( $entrylines as $eline ) {
$s .= escape( $eline );
}
$s .= " </description>\n" .
" </item>\n";
return $s;
}
function which_entries()
{
$months = glob( "??????/" );
$entries = glob( "??????/??" );
rsort( $months );
rsort( $entries );
$maxmonths = 6;
$maxentries = 50;
$months = array_slice( $months, 0, $maxmonths );
$lastmonth = $months[count( $months ) - 1];
$entries = array_slice( $entries, 0, $maxentries );
$today = strftime("%Y%m%d");
for( $i=0; $i<count($entries); $i++ ) {
$m = substr( $entries[$i], 0, 6);
$d = substr( $entries[$i], 7, 2);
if( "$m$d" <= $today ) {
$entries = array_slice( $entries, $i, count($entries) );
break;
}
}
for( $i=0; $i<count($entries); $i++ ) {
if( $entries[$i] < $lastmonth ) {
$entries = array_slice( $entries, 0, $i );
break;
}
}
return $entries;
}
function do_page($entries)
{
foreach( $entries as $file ) {
$m = substr( $file, 0, 6);
$d = substr( $file, 7, 2);
print do_entry( $file, $m, $d );
}
}
$entries = which_entries();
if( count($entries) > 0 ) {
$lastmtime = 0;
$lastentry = "";
foreach( $entries as $e ) {
$mt = filemtime( $e );
if( $lastmtime < $mt ) {
$lastmtime = $mt;
$lastentry = $e;
}
}
$lastdate = gmdate("D, d M Y H:i:s", $lastmtime) . " GMT";
header("Last-Modified: $lastdate");
header("X-Last-Modified-Entry: $lastentry");
# implement if-modified-since header
$ims = preg_replace('/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"]);
if ($ims == $lastdate) {
header("HTTP/1.0 304 Not Modified");
exit;
}
}
print "<?xml version=\"1.0\"?>\n";
?><rss version="2.0">
<channel>
<title><?php print $name; ?></title>
<description><?php print $name; ?> - NITLog</description>
<link><?php print "$absoluteurl/$topdir/"; ?></link>
<language>en-ca</language>
<generator>NITLog</generator>
<docs>http://blogs.law.harvard.edu/tech/rss</docs>
<?php
do_page($entries);
?>
</channel>
</rss>