/ lib / rss.php
rss.php
 1  <?php
 2  function summarize($com) {
 3  	// strip out URLs...
 4  	$com = preg_replace('{(https?:)?//[\S"\'<]+}','',$com);
 5  	// remove linebreaks
 6  	$com = preg_replace('{<br ?/?>}',"\n",$com);
 7  	// take the first sentence that's longer than 6 letters...
 8  	$sentences = preg_split('{[\n.]+}',$com);
 9  	$com = '';
10  	foreach($sentences as $sent) {
11  		if(strlen($sent) > 6) {
12  			$com = $sent;
13  			break;
14  		}
15  	}
16  	// and get the first 60 chars of it, making sure that words don't get cut off
17  	$com = preg_replace('{^(.{60,}?)(?:[\s\n.]|$).*}','$1',$com);
18  	if(strlen($com) >= 60) $com .= '...';
19  	return $com;
20  }
21  function rss_dump() {
22  	global $log;
23  	$title = htmlspecialchars(TITLE);
24  	$link = "http:" . SELF_PATH2_ABS;
25  	$self = "http:" . DATA_SERVER . BOARD_DIR . '/index.rss';
26  	$output = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<rss version=\"2.0\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:atom=\"http://www.w3.org/2005/Atom\">\n";
27  	$output .= "<channel>\n<title>$title</title>\n<link>$link</link>\n<description>Threads on $title at 4chan.org.</description>\n";
28  	$output .= "<atom:link href=\"$self\" rel=\"self\" type=\"application/rss+xml\" />";
29  	$query = mysql_board_call("SELECT SQL_NO_CACHE * FROM `".SQLLOG."` WHERE archived=0 and resto=0 ORDER BY no DESC LIMIT 20");
30  	while($row = mysql_fetch_assoc($query)) {
31  		$output .= "<item>\n";
32  		$spacing = '';
33  		$sub = $row['sub'];
34  
35  		$spoiler = 0;
36  		if(strpos($sub, "SPOILER<>")===0) {
37  			$sub = str_replace("SPOILER<>","",$sub);
38  			$spoiler = 1;
39  		}
40  		if(!$sub)
41  			$sub = strip_tags(summarize($row['com']));
42  		if(!$sub && $row['name']) { // if they have a name
43  			if(FORCED_ANON!=1) $by = " by ". strip_tags($row['name']); // forced anon doesn't need byline
44  			$sub = "No. {$row['no']}$by";
45  		}
46  		$link = "http:" . DATA_SERVER . BOARD_DIR . '/' . RES_DIR2 . $row['no'] . PHP_EXT2;
47  
48  		if(strpos($row['com'], "[spoiler")!==false)
49  			$row['com'] = '(Spoilers)';
50  		$date = strftime("%a, %d %b %Y %X %Z", $row['time']);
51  		if($sub) // don't include title if empty
52  			$output .= "$spacing<title>$sub</title>\n";
53  		$output .= "$spacing<link>$link#{$row['no']}</link>\n";
54  		$output .= "$spacing<guid>$link</guid>\n";
55  		$output .= "$spacing<comments>$link</comments>\n";
56  		$output .= "$spacing<pubDate>$date</pubDate>\n";
57  		$srcurl = "http:" . IMG_DIR2 . $row['tim'] . $row['ext'];
58  		$thumburl = "http:" . THUMB_DIR2 . $row['tim'] . 's.jpg';
59  		if($spoiler) {
60  			$thumburl = "http:" . DATA_SERVER . 'spoiler.png';
61  		}
62  		$imglink = "<a href='$srcurl' target=_blank><img style='float:left;margin:8px' border=0 src='$thumburl'></a>";
63  
64  		if(FORCED_ANON!=1)
65  			// $output .= "$spacing<author>".strip_tags($row['name'])."</author>\n";
66  			$output .= "$spacing<dc:creator>".strip_tags($row['name'])."</dc:creator>\n";
67  		$output .= "$spacing<description><![CDATA[ $imglink {$row['com']} ]]></description>\n";
68  		$output .= "</item>\n";
69  	}
70  	$output .= "</channel>\n</rss>";
71  	print_page(INDEX_DIR.'index.rss',$output);
72  }