WvDBusMsg::is_reply() had an unnecessary hack for message #1.
[wvapps.git] / planit / index.php
blobab8b2010a519d107ba3fc12b9c9eaa49be40cd17
1 <!--
2 * Worldvisions Weaver Software:
3 * Copyright (C) 1997-2005 Net Integration Technologies, Inc.
5 * Dave's cheesy PlaNit thing
7 */
8 -->
10 <?php
11 include( 'config.inc.php' );
13 require_once( 'rss_fetch.inc' );
15 function parse_atom_date( $s )
17 list( $tmp, $junk ) = split( 'Z', $s );
18 if( !$tmp )
19 return '';
20 list( $tmp2, $junk ) = split( '\+', $tmp );
21 if( !$tmp2 )
22 return '';
23 list( $year, $month, $rest, $junk ) = split( '-', $tmp2 );
24 $tmp2 = "$year-$month-$rest";
26 list( $d, $t, $junk ) = split( 'T', $tmp2 );
27 $tmp = "$d $t GMT";
28 return $tmp;
31 function grab_feeds()
33 global $feeds;
34 global $allrss;
35 global $allitems;
36 global $cutoffs;
37 global $articles_per_person;
38 global $maximum_days;
39 global $hack;
41 // grab all the feeds, and stick them in an array, indexed by URL.
42 // also make a new array containing the URL, timestamp, and item key,
43 // for each item in all the feeds.
44 foreach( $feeds as $userid => $feed ) {
45 if( $feed['hack'] && !$hack )
46 continue;
48 foreach( $feed['urls'] as $url ) {
49 $allrss[$url] = fetch_rss( $url );
50 if( $allrss[$url] === false ) {
51 $feeds[$userid]['broken'] = true;
52 continue;
55 $cutoffs[$url] = $articles_per_person;
56 foreach( $allrss[$url]->items as $itemkey => $item ) {
57 // is this item too old?
58 $tmp = $item['pubdate'];
59 if( $tmp == '' )
60 $tmp = parse_atom_date( $item['created'] );
61 if( $tmp == '' )
62 $tmp = parse_atom_date( $item['modified'] );
64 $timestamp = strtotime( $tmp );
65 if( $timestamp === false || $timestamp == '' )
66 continue;
67 if( time()-$timestamp > 3600*24*$maximum_days )
68 continue;
70 $allitems[count( $allitems )] =
71 array( url => $url,
72 timestamp => $timestamp,
73 itemkey => $itemkey,
74 userid => $userid );
79 // sort the allitems list in reverse chronological order
80 function itemcmp( $a, $b )
82 return $b['timestamp'] - $a['timestamp'];
84 usort( $allitems, 'itemcmp' );
85 reset( $allitems );
88 function escape( $string )
90 return( ereg_replace( '<', '&lt;',
91 ereg_replace( '>', '&gt;',
92 ereg_replace( '"', '&quot;',
93 ereg_replace( '&', '&amp;',
94 $string ) ) ) ) );
97 function do_entry( $iteminfo, $lastperson, $entryformatting, $fd, $rssfd )
99 global $allrss;
100 global $allitems;
101 global $cutoffs;
102 global $feeds;
103 global $mugshotdir;
105 $rss = $allrss[$iteminfo['url']];
106 $timestamp = $iteminfo['timestamp'];
107 $item = $rss->items[$iteminfo['itemkey']];
108 $userid = $iteminfo['userid'];
110 if( $cutoffs[$iteminfo['url']] ) {
111 $cutoffs[$iteminfo['url']]--;
113 // no longer sanitize() here, that's done in magpie before caching
114 $content = $item['atom_content'];
115 if( $content == '' )
116 $content = $item['description'];
118 while( substr( $content, 0, 4 ) == '<br>' )
119 $content = trim( substr( $content, 4 ) );
121 do {
122 $len = strlen( $content );
123 $br_end = substr( $content, $len-4 );
124 if( $br_end == '<br>' )
125 $content = trim( substr( $content, 0, $len-4 ) );
126 } while( $br_end == '<br>' );
128 if( $item['title'] )
129 $title = $item['title'];
130 else
131 $title = gmdate( 'F j, Y', $timestamp );
133 unset( $mugshot );
134 if( file_exists( "$mugshotdir/$userid.jpg" ) )
135 $mugshot = "$mugshotdir/$userid.jpg";
136 else if( file_exists( "$mugshotdir/$userid.png" ) )
137 $mugshot = "$mugshotdir/$userid.png";
138 else if( file_exists( "$mugshotdir/$userid.gif" ) )
139 $mugshot = "$mugshotdir/$userid.gif";
141 $channellink = $rss->channel['link'];
142 $channelname = $feeds[$userid]['name'];
143 $entrytime = gmdate( 'F j, Y H:i', $timestamp );
145 $link = $item['link'];
146 if( $link == '' )
147 $link = $item['link_'];
148 if( $link == '' )
149 $link = $channellink;
151 $mode = 'none';
153 foreach( $entryformatting as $line ) {
154 // check for special inline constants
155 $tmp = $line;
156 $line = preg_replace( '/(?im)<## channellink ##>/', $channellink,
157 preg_replace( '/(?im)<## channelname ##>/', $channelname,
158 preg_replace( '/(?im)<## entrylink ##>/', $link,
159 preg_replace( '/(?im)<## entrytitle ##>/', $title,
160 preg_replace( '/(?im)<## mugshot ##>/', $mugshot,
161 preg_replace( '/(?im)<## feeduserid ##>/', $userid,
162 preg_replace( '/(?im)<## entrytime ##>/', $entrytime,
163 $tmp ) ) ) ) ) ) );
165 // check for flow control commands
166 if( substr( $line, 0, 4 ) === '### ' ) {
167 $tmp = trim( substr( $line, 4 ) );
168 if( $mode != 'silent' ) {
169 if( $tmp == 'ifnewperson' ) {
170 if( $userid == $lastperson )
171 $mode = 'silent';
173 else if( $tmp == 'ifmugshot' ) {
174 if( !$mugshot )
175 $mode = 'silent';
177 else if( $tmp == 'entrycontent' )
178 fwrite( $fd, $content );
180 if( $tmp == 'endif' )
181 $mode = 'none';
183 } else
184 if( $mode != 'silent' )
185 fwrite( $fd, $line );
188 // RSS formatting doesn't need to be quite that flexible.
189 fwrite( $rssfd, " <item>\n" .
190 " <title>$channelname: $title</title>\n" .
191 " <pubDate>" . gmdate( 'r', $timestamp )
192 . "</pubDate>\n" .
193 " <link>$link</link>\n" .
194 " <description>\n" .
195 escape( $content ) . "\n" .
196 " </description>\n" .
197 " </item>\n\n" );
201 function do_feeditem( $userid, $feed, $feedformatting, $fd )
203 global $allrss;
205 if( $feed['broken'] )
206 return;
208 foreach( $feed['urls'] as $url ) {
209 $page = $allrss[$url]->channel['link'];
211 foreach( $feedformatting as $line ) {
212 // check for special inline constants
213 $tmp = $line;
214 $line = preg_replace( '/(?im)<## feedpage ##>/', $page,
215 preg_replace( '/(?im)<## feeduserid ##>/', $userid,
216 preg_replace( '/(?im)<## feedxml ##>/', $url,
217 $tmp ) ) );
219 // there are no interesting flow control commands here
220 fwrite( $fd, $line );
225 function read_layout()
227 global $layout;
228 global $email;
229 global $admin;
230 global $name;
231 global $feeds;
232 global $allitems;
233 global $tmppage;
234 global $tmprss;
235 global $cssoverride;
237 $mode = 'none';
239 $fd = @fopen( $layout, 'r' );
240 if( $fd === false ) {
241 print( "Couldn't find $layout.\n" );
242 return;
243 } else
244 fclose( $fd );
246 $fd = @fopen( $tmppage, 'w' );
247 $rssfd = @fopen( $tmprss, 'w' );
248 if( $fd === false || $rssfd === false ) {
249 print( "PlaNit is broken right now.\n" );
250 return;
253 // parse the layout
254 $file = file( $layout );
255 foreach( $file as $line ) {
257 // nothing special happening
258 if( $mode == 'none' || $mode == 'silent' ) {
260 // check for special inline constants
261 $tmp = $line;
262 $line = preg_replace( '/(?im)<## email ##>/', $email,
263 preg_replace( '/(?im)<## admin ##>/', $admin,
264 preg_replace( '/(?im)<## name ##>/', $name,
265 preg_replace( '/(?im)<## cssoverride ##>/', $cssoverride,
266 $tmp ) ) ) );
268 // check for flow control commands
269 if( substr( $line, 0, 4 ) == '### ' ) {
270 $tmp = trim( substr( $line, 4 ) );
271 if( $mode != 'silent' ) {
272 if( $tmp == 'loopfeeds' ) {
273 $mode = 'feedloop';
274 unset( $feedformatting );
276 else if( $tmp == 'loopentries' ) {
277 $mode = 'entryloop';
278 unset( $entryformatting );
280 else if( $tmp == 'ifcssoverride' ) {
281 if( !$cssoverride )
282 $mode = 'silent';
285 if( $tmp == 'endif' )
286 $mode = 'none';
288 else
289 if( $mode != 'silent' )
290 fwrite( $fd, $line );
292 // in both kinds of loop, make a list of the formatting lines so we
293 // can loop through them once we've parsed the contents
294 } else if( $mode == 'feedloop' || $mode == 'entryloop' ) {
296 if( substr( $line, 0, 4 ) == '### ' ) {
297 $tmp = trim( substr( $line, 4 ) );
298 if( $tmp == 'endloopfeeds' ) {
299 foreach( $feeds as $userid => $feed ) {
300 do_feeditem( $userid, $feed, $feedformatting, $fd );
302 $mode = 'none';
303 continue;
305 } else if( $tmp == 'endloopentries' ) {
306 unset( $lastperson );
307 foreach( $allitems as $iteminfo ) {
308 do_entry( $iteminfo, $lastperson,
309 $entryformatting, $fd, $rssfd );
310 $lastperson = $iteminfo['userid'];
312 $mode = 'none';
313 continue;
316 if( $mode == 'feedloop' )
317 $feedformatting[ count( $feedformatting ) ] = $line;
318 else if( $mode == 'entryloop' )
319 $entryformatting[ count( $entryformatting ) ] = $line;
323 fclose( $fd );
324 fclose( $rssfd );
331 if( $adminpass == $pass ) {
332 // $tmppage should have a sensible default if it wasn't set.
333 if( !$tmppage )
334 $tmppage = $staticpage . '.tmp';
335 if( !$tmprss )
336 $tmprss = $staticrss . '.tmp';
338 // set up the urls array for each userid that only had one url
339 // defined.
340 foreach( $feeds as $userid => $feed ) {
341 if( $feeds[$userid]['url'] ) {
342 unset( $feeds[$userid]['urls'] );
343 $i = count( $feeds[$userid]['urls'] );
344 $feeds[$userid]['urls'][$i] = $feeds[$userid]['url'];
348 if( $bereallyslow == 1 ) {
349 system( "rm -f cache/*" );
350 $beslow = 1;
353 if( $force ) {
354 foreach( $feeds[$force]['urls'] as $url ) {
355 system( 'rm -f cache/' . md5( $url ) );
358 $beslow = 1;
361 if( $beslow == 1 || $bereallyslow == 1 ) {
362 grab_feeds();
363 read_layout();
365 copy( $tmppage, $staticpage );
366 copy( $tmprss, $staticrss );
370 readfile( $staticpage );
372 if( !$beslow && $logfile ) {
373 $fd = @fopen( $logfile, 'a' );
374 if( $fd === false ) {
375 print( "Email Dave and tell him to fix his program!\n" );
376 } else {
377 fwrite( $fd, strftime( '%Y%m%d %H:%M' ) . " -- pla -- " .
378 "$REMOTE_ADDR -- $HTTP_REFERER\n" );
379 fclose( $fd );