rss.php
223 lines
| 6.0 KiB
| text/x-php
|
PhpLexer
/ rss / rss.php
| r7 | <?php | |||
| require_once('../LocalSettings.php'); | ||||
| /* Get the database working */ | ||||
| r50 | $link = mysqli_connect(DB_SERVER, DB_READ_USER, DB_READ_PASS, DB_NAME) | |||
| or trigger_error('Problem connecting to the SQL database server: ' . mysqli_error($link), E_USER_ERROR); | ||||
| r7 | ||||
| r50 | function numeric_entities($string) | |||
| { | ||||
| r7 | $mapping = array(); | |||
| r50 | foreach (get_html_translation_table(HTML_ENTITIES, ENT_QUOTES) as $char => $entity) | |||
| { | ||||
| r7 | $mapping[$entity] = '&#' . ord($char) . ';'; | |||
| } | ||||
| return str_replace(array_keys($mapping), $mapping, $string); | ||||
| } | ||||
| r50 | function Excerpt($excerpt) | |||
| { | ||||
| $excerpt = strip_tags($excerpt); | ||||
| if (strlen($excerpt) > 455) | ||||
| { | ||||
| $excerpt = substr($excerpt, 0, 452) . '...'; | ||||
| r7 | } | |||
| return $excerpt; | ||||
| } | ||||
| r50 | function datesort($a , $b) | |||
| { | ||||
| if ($a->udate == $b->udate) | ||||
| { | ||||
| return 0; | ||||
| } | ||||
| return ($a->udate > $b->udate) ? -1 : 1; | ||||
| r7 | } | |||
| function utfentities($string) | ||||
| { | ||||
| return htmlentities($string, ENT_COMPAT, 'UTF-8'); | ||||
| } | ||||
| r50 | function _query($s, $byid = false) | |||
| { | ||||
| r7 | global $link; | |||
| r50 | $r = mysqli_query($link, $s) or die(mysqli_error($link)); | |||
| $ret = array(); | ||||
| $i = 0; | ||||
| r37 | ||||
| r50 | if ($byid) | |||
| { | ||||
| while ($row = mysqli_fetch_object($r)) | ||||
| { | ||||
| r7 | $ret[$row->id] = $row; | |||
| r50 | } | |||
| } | ||||
| else | ||||
| { | ||||
| while ($row = mysqli_fetch_object($r)) | ||||
| { | ||||
| r7 | $ret[$i++] = $row; | |||
| r50 | } | |||
| r7 | } | |||
| r50 | ||||
| r7 | return $ret; | |||
| } | ||||
| @$f_title = $type = $_GET['type']; | ||||
| $f_items = array(); | ||||
| $f_desc = 'News and Comics from Megatokyo.'; | ||||
| /*if( $type == 'status' or $type == '') { | ||||
| // Status Box Updates | ||||
| $f_title = 'Megatokyo Status Updates'; | ||||
| $s = _query( "SELECT MAX(id)+1 as m FROM strip" ); // Permalinks to strips? | ||||
| $s = _query( "SELECT DISTINCT SUBTIME( published, eta ) as dd, UNIX_TIMESTAMP(published) as udate, text, UNIX_TIMESTAMP(eta) as eta, percentage | ||||
| FROM status s | ||||
| ORDER BY udate DESC | ||||
| LIMIT 5"); | ||||
| foreach( $s as $k=>$v ) { | ||||
| $s[$k]->link = 'http://www.megatokyo.com/'; | ||||
| $s[$k]->title = "Update: $v->percentage%: " . htmlentities($v->text, ENT_COMPAT, ini_get('default_charset')); | ||||
| r37 | ||||
| r7 | $d = date( 'l F jS, H:i', $v->eta ); | |||
| $dd = (int) (( $v->eta - $v->udate )/3600); | ||||
| $s[$k]->desc = "<![CDATA[Status Update. | ||||
| <br/>$v->percentage% complete. | ||||
| <br/>Next comic should be posted at: $d. ($dd hours)]]>"; | ||||
| $s[$k]->date = date( DATE_RFC822, $v->udate ); | ||||
| $s[$k]->guid = $s[$k]->link; | ||||
| } | ||||
| $f_items = array_merge( $f_items, $s ); | ||||
| }*/ | ||||
| r50 | if ($type == 'strips' or $type == '') | |||
| { | ||||
| r7 | $f_title = 'Megatokyo Comics'; | |||
| r50 | ||||
| $comic_query = 'SELECT s.id, UNIX_TIMESTAMP(s.published) as udate, t.description as chapdesc, t.name as chapname, s.title | ||||
| FROM strip s JOIN strip_t ON s.type = t.id | ||||
| WHERE s.published <= NOW() | ||||
| ORDER BY s.id DESC LIMIT 5'; | ||||
| $s = _query($comic_query) or die(mysqli_error($link)); | ||||
| foreach($s as $k => $v) | ||||
| { | ||||
| r7 | $s[$k]->link = SITE_HOST . SITE_PATH . "/strip/$v->id"; | |||
| $s[$k]->title = "Comic [$v->id] \"" . numeric_entities(utfentities($v->title)) . '"'; | ||||
| $s[$k]->desc = "<![CDATA[$v->chapdesc comic $v->id | ||||
| <br/>[<a href=\"http://www.megatokyo.com/\">read...</a>] | ||||
| <br/>[<a href=\"" . $s[$k]->link . "\">permalink</a>] | ||||
| ]]>"; | ||||
| r50 | $s[$k]->date = date(DATE_RSS, $v->udate); | |||
| r7 | $s[$k]->guid = $s[$k]->link; | |||
| $s[$k]->perm = 'true'; | ||||
| } | ||||
| r50 | ||||
| $f_items = array_merge($f_items, $s); | ||||
| r7 | } | |||
| r50 | if ($type == 'rants' or $type == '') | |||
| { | ||||
| r7 | $f_title = 'Megatokyo Rants'; | |||
| r50 | ||||
| $rant_query = 'SELECT r.id, r.body, UNIX_TIMESTAMP(r.published) as udate, c.name, r.title | ||||
| FROM rant r JOIN contributor c ON r.author = c.id | ||||
| WHERE r.published <= NOW() AND r.status = "published" | ||||
| ORDER BY udate DESC LIMIT 5'; | ||||
| $s = _query($rant_query) or die(mysqli_error($link)); | ||||
| foreach($s as $k => $v) | ||||
| { | ||||
| r7 | $s[$k]->link = SITE_HOST . SITE_PATH . "/rant/$v->id"; | |||
| r50 | $s[$k]->author = utfentities($v->name); | |||
| $s[$k]->title = "Rant [$v->id] \"" . utfentities($v->title) . '"'; | ||||
| r7 | $s[$k]->desc = "<![CDATA[" . Excerpt($v->body) . "<p>[<a href=\"" . $s[$k]->link . "\">permalink</a>]</p>]]>"; | |||
| r50 | $s[$k]->date = date(DATE_RSS, $v->udate); | |||
| r7 | $s[$k]->guid = $s[$k]->link; | |||
| $s[$k]->perm = 'true'; | ||||
| } | ||||
| r50 | ||||
| $f_items = array_merge($f_items, $s); | ||||
| r7 | } | |||
| r50 | if ($type == '') | |||
| r7 | { | |||
| $f_title = 'Megatokyo Comics and News'; | ||||
| r50 | ||||
| /* | ||||
| r7 | $s = _query("SELECT UNIX_TIMESTAMP(published) as udate, body, url FROM rss_comment ORDER BY published DESC LIMIT 5"); | |||
| r37 | ||||
| r50 | foreach($s as $k => $v) | |||
| r7 | { | |||
| $s[$k]->link = $v->url; | ||||
| $s[$k]->title = $v->body; | ||||
| $s[$k]->date = date( DATE_RSS, $v->udate ); | ||||
| $s[$k]->guid = date( DATE_W3C, $s[$k]->udate); | ||||
| $s[$k]->desc = ''; | ||||
| $s[$k]->perm = 'false'; | ||||
| } | ||||
| r50 | ||||
| r7 | $f_items = array_merge( $f_items, $s ); | |||
| r50 | */ | |||
| r7 | } | |||
| r50 | usort($f_items, 'datesort'); | |||
| r7 | ||||
| $f_date = $f_items[0]->date; | ||||
| r50 | $rfc1123_date = gmdate('D, d M Y H:i:s', $f_items[0]->udate) . ' GMT'; | |||
| $etag = md5($rfc1123_date); | ||||
| r7 | ||||
| /* Conditional Get */ | ||||
| r50 | header("Last-Modified: $rfc1123_date"); | |||
| header("ETag: \"$etag\""); | ||||
| r7 | ||||
| r50 | if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $_SERVER['HTTP_IF_MODIFIED_SINCE'] == $rfc1123_date) | |||
| { | ||||
| r7 | header('HTTP/1.0 304 Not Modified'); | |||
| exit; | ||||
| } | ||||
| r50 | ||||
| if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == $etag) | ||||
| { | ||||
| r7 | header('HTTP/1.0 304 Not Modified'); | |||
| exit; | ||||
| } | ||||
| header("XX-Powered-By: Nikuman"); | ||||
| r50 | header("Content-Type: application/rss+xml; charset=utf-8"); | |||
| r7 | ||||
| echo '<?xml version="1.0" encoding="utf-8"?>', "\n"; | ||||
| ?> | ||||
| <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"> | ||||
| <channel> | ||||
| <title><?php echo $f_title; ?></title> | ||||
| <link>http://www.megatokyo.com</link> | ||||
| <atom:link href="<?php echo SITE_HOST . $_SERVER['REQUEST_URI'] ?>" rel="self" type="application/rss+xml" /> | ||||
| <description><?php echo $f_desc; ?></description> | ||||
| <language>en-us</language> | ||||
| <copyright>Fred Gallagher</copyright> | ||||
| <managingEditor>piro@megatokyo.com (Fred Gallagher)</managingEditor> | ||||
| <pubDate><?php echo $f_date; ?></pubDate> | ||||
| <lastBuildDate><?php echo $f_date; ?></lastBuildDate> | ||||
| <ttl>15</ttl> | ||||
| r37 | ||||
| r7 | <?php if( count( $f_items )) foreach( $f_items as $v ): ?> | |||
| <item> | ||||
| <title><?php echo $v->title; ?></title> | ||||
| <link><?php echo $v->link; ?></link> | ||||
| <description><?php echo $v->desc; ?></description> | ||||
| <guid isPermaLink="<?php echo $v->perm; ?>"><?php echo $v->guid; ?></guid> | ||||
| <pubDate><?php echo $v->date; ?></pubDate> | ||||
| </item> | ||||
| <?php endforeach; ?> | ||||
| </channel> | ||||
| r37 | </rss> | |||
