2 * Worldvisions Weaver Software
:
3 * Copyright (C
) 1997-2005 Net Integration Technologies
, Inc
.
5 * Dave
's cheesy PlaNit thing
11 include( 'config
.inc
.php
' );
13 require_once( 'rss_fetch
.inc
' );
15 function parse_atom_date( $s )
17 list( $tmp, $junk ) = split( 'Z
', $s );
20 list( $tmp2, $junk ) = split( '\+
', $tmp );
23 list( $year, $month, $rest, $junk ) = split( '-', $tmp2 );
24 $tmp2 = "$year-$month-$rest";
26 list( $d, $t, $junk ) = split( 'T
', $tmp2 );
37 global $articles_per_person;
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 )
48 foreach( $feed['urls
'] as $url ) {
49 $allrss[$url] = fetch_rss( $url );
50 if( $allrss[$url] === false ) {
51 $feeds[$userid]['broken
'] = true;
55 $cutoffs[$url] = $articles_per_person;
56 foreach( $allrss[$url]->items as $itemkey => $item ) {
57 // is this item too old?
58 $tmp = $item['pubdate
'];
60 $tmp = parse_atom_date( $item['created
'] );
62 $tmp = parse_atom_date( $item['modified
'] );
64 $timestamp = strtotime( $tmp );
65 if( $timestamp === false || $timestamp == '' )
67 if( time()-$timestamp > 3600*24*$maximum_days )
70 $allitems[count( $allitems )] =
72 timestamp => $timestamp,
79 // sort the allitems list in reverse chronological order
80 function itemcmp( $a, $b )
82 return $b['timestamp
'] - $a['timestamp
'];
84 usort( $allitems, 'itemcmp
' );
88 function escape( $string )
90 return( ereg_replace( '<', '<
;',
91 ereg_replace( '>', '>
;',
92 ereg_replace( '"', '"',
93 ereg_replace( '&', '&',
97 function do_entry( $iteminfo, $lastperson, $entryformatting, $fd, $rssfd )
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'];
116 $content = $item['description'];
118 while( substr( $content, 0, 4 ) == '<br>' )
119 $content = trim( substr( $content, 4 ) );
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>' );
129 $title = $item['title'];
131 $title = gmdate( 'F j, Y', $timestamp );
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'];
147 $link = $item['link_'];
149 $link = $channellink;
153 foreach( $entryformatting as $line ) {
154 // check for special inline constants
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,
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 )
173 else if( $tmp == 'ifmugshot' ) {
177 else if( $tmp == 'entrycontent' )
178 fwrite( $fd, $content );
180 if( $tmp == 'endif' )
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 )
193 " <link
>$link</link
>\n" .
195 escape( $content ) . "\n
" .
196 " </description
>\n" .
201 function do_feeditem( $userid, $feed, $feedformatting, $fd )
205 if( $feed['broken'] )
208 foreach( $feed['urls'] as $url ) {
209 $page = $allrss[$url]->channel['link'];
211 foreach( $feedformatting as $line ) {
212 // check for special inline constants
214 $line = preg_replace( '/(?im)<## feedpage ##>/', $page,
215 preg_replace( '/(?im)<## feeduserid ##>/', $userid,
216 preg_replace( '/(?im)<## feedxml ##>/', $url,
219 // there are no interesting flow control commands here
220 fwrite( $fd, $line );
225 function read_layout()
239 $fd = @fopen( $layout, 'r' );
240 if( $fd === false ) {
241 print( "Couldn
't find $layout.\n" );
246 $fd = @fopen( $tmppage, 'w
' );
247 $rssfd = @fopen( $tmprss, 'w
' );
248 if( $fd === false || $rssfd === false ) {
249 print( "PlaNit is broken right now.\n" );
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
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,
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' ) {
274 unset( $feedformatting );
276 else if( $tmp == 'loopentries' ) {
278 unset( $entryformatting );
280 else if( $tmp == 'ifcssoverride' ) {
285 if( $tmp == 'endif' )
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 );
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'];
316 if( $mode == 'feedloop' )
317 $feedformatting[ count( $feedformatting ) ] = $line;
318 else if( $mode == 'entryloop' )
319 $entryformatting[ count( $entryformatting ) ] = $line;
331 if( $adminpass == $pass ) {
332 // $tmppage should have a sensible default if it wasn't set.
334 $tmppage = $staticpage . '.tmp';
336 $tmprss = $staticrss . '.tmp';
338 // set up the urls array for each userid that only had one url
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/*" );
354 foreach( $feeds[$force]['urls'] as $url ) {
355 system( 'rm -f cache/' . md5( $url ) );
361 if( $beslow == 1 ||
$bereallyslow == 1 ) {
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" );
377 fwrite( $fd, strftime( '%Y%m%d %H:%M' ) . " -- pla -- " .
378 "$REMOTE_ADDR -- $HTTP_REFERER\n" );