. */ if (!file_exists(dirname(__FILE__) . '/config.php')) { echo '

Planet not configured

Copy config.php.dist to config.php and edit it.

'; exit; } require dirname(__FILE__) . '/config.php'; if (!file_exists(CACHE_PATH)) mkdir(CACHE_PATH, 0700); // Cache management if (!isset($_GET['p']) && !isset($_GET['update'])) { if (isset($_GET['rss'])) { $file = 'rss'; $ext = 'xml'; } else { $ext = 'html'; $file = 'home'; } if (!empty($_GET['lang']) && isset($langs[$_GET['lang']])) $file = CACHE_PATH . '/'.$file.'-'.$_GET['lang'].'.'.$ext; else $file = CACHE_PATH . '/'.$file.'.'.$ext; if (@filemtime($file) > time() - CACHE_EXPIRE) { echo file_get_contents($file); exit; } define('CACHE_FILE', $file); } function escape($str) { return sqlite_escape_string($str); } class planet { public $by_page = 20; public $feeds = array(); private $db = false; private $stream_options = array(); public function __construct() { $this->stream_options = array( 'http' => array( 'method' => 'GET', 'user_agent' => 'lightPlanet/1.0 (feed parser; bot)', 'max_redirects' => 5, 'timeout' => 5, ) ); if (!file_exists(DB_FILE)) { $this->db = new SQLiteDatabase(DB_FILE, 0600); $this->db->queryExec(' CREATE TABLE articles ( id INTEGER PRIMARY KEY NOT NULL, title VARCHAR(255) NOT NULL, link VARCHAR(255) NOT NULL, feed VARCHAR(255) NOT NULL, date INTEGER NOT NULL, lang VARCHAR(2) NOT NULL, content TEXT NOT NULL ); CREATE INDEX date ON articles (date); CREATE INDEX feed ON articles (feed); CREATE INDEX link ON articles (link); '); } else { $this->db = new SQLiteDatabase(DB_FILE, 0600); } } public function updateFeeds() { // Clean of old articles $this->db->unbufferedQuery('DELETE FROM articles WHERE id IN (SELECT id FROM articles ORDER BY date DESC LIMIT '.CLEAN_OLD_ARTICLES.','.CLEAN_OLD_ARTICLES.');'); $rss = new RSS_PHP; $errors = array(); foreach ($this->feeds as $id=>$feed) { $context = stream_context_create($this->stream_options); $content = @file_get_contents($feed['feed'], false, $context); if (empty($content)) { $errors[] = "Cannot fetch ".$feed['feed']; continue; } $rss->loadXML($content); if (isset($feed['lang'])) $lang = $feed['lang']; else $lang = current($rss->getValuesByTagName('language')); if (empty($lang)) $lang = DEFAULT_LANG; $lang = substr($lang, 0, 2); foreach ($rss->getItems() as $item) { if (empty($item['link'])) die('pas de link'); if (!empty($item['content:encoded'])) $content =& $item['content:encoded']; elseif (!empty($item['description'])) $content =& $item['description']; else $content = ''; if (!empty($item['pubDate'])) { $date = strtotime($item['pubDate']); } else { $date = false; } $res = $this->db->arrayQuery("SELECT id, date, title, content FROM articles WHERE feed='".escape($id)."' AND link='".escape($item['link'])."';", SQLITE_ASSOC); if (!empty($res[0]['id'])) { list($row) = $res; $update = false; if ($date && $row['date'] != $date) $update = true; elseif ($row['title'] != $item['title']) $update = true; elseif ($row['content'] != $content) $update = true; if ($update) { if (!$date) $date = time(); $this->db->unbufferedQuery("UPDATE articles SET link='".escape($item['link'])."', date='".(int)$date."', title='".escape($item['title'])."', content='".escape($content)."' WHERE id='".(int)$res[0]['id']."';"); } } else { if (!$date) $date = time(); $this->db->unbufferedQuery("INSERT INTO articles (id, title, link, feed, date, lang, content) VALUES (NULL, '".escape($item['title'])."', '".escape($item['link'])."', '".escape($id)."', '".(int)$date."', '".escape($lang)."', '".escape($content)."');"); } } } } public function getAvailableLangs() { $res = $this->db->arrayQuery('SELECT lang, COUNT(id) AS nb FROM articles WHERE lang != "" GROUP BY lang ORDER BY lang;', SQLITE_ASSOC); $langs = array(); foreach ($res as $row) { $langs[$row['lang']] = $row['nb']; } return $langs; } public function countArticles($lang=false) { $res = $this->db->arrayQuery('SELECT COUNT(id) FROM articles'. ($lang ? ' WHERE lang="'.$lang.'";' : ';'), SQLITE_NUM); return $res[0][0]; } public function getArticles($begin=0, $lang=false) { $query = 'SELECT * FROM articles '; if ($lang) $query .= 'WHERE lang="'.$lang.'" '; $query .= 'ORDER BY date DESC LIMIT '.(int)$begin.', '.(int)$this->by_page.';'; return $this->db->arrayQuery($query); } } $planet = new planet; $planet->feeds =& $planet_feeds; // Feeds update if ((isset($_GET['update']) || AUTO_UPDATE) && @filemtime(CACHE_PATH . '/last_check') < (time() - FEEDS_CHECK * 3600)) { require 'RSS_PHP/rss_php.php'; $planet->updateFeeds(); touch(CACHE_PATH . '/last_check'); if (isset($_GET['update'])) die("Updated"); } function secureContent($str) { // Forbidden attributes $attrs = 'onabort|onblue|onchange|onclick|ondblclick|onerror'. 'onfocus|onkeydown|onkeyup|onload|onmousedown|onmousemove|onmouseover'. 'onmouseup|onreset|onresize|onselect|onsubmit|onunload|target'; // Allowed tags // -> ok // "> -> $tags = 'b|i|em|strong|li|ul|ol|blockquote|p|a|object|param|embed|video|audio|img|dl|dt|dd|span|br|h[1-6]|pre|code|table|tr|td|th|tbody|thead'; $str = preg_replace('§<+?((?!/?('.$tags.')(\s+.*|\s*)/?>).*)>+?§U', '', $str); $str = preg_replace('/<(.*?)>/ie', "'<' . preg_replace(array('/javascript:[^\"\']*/i', '/(" . $attrs . ")[ \\t\\n]*=[ \\t\\n]*[\"\'][^\"\']*[\"\']/i', '/\s+/'), array('', '', ' '), stripslashes('\\1')) . '>'", $str); return $str; } function escapeHTML($str) { $str = htmlspecialchars($str); $str = preg_replace('/&([a-z]+|#[0-9]+);/', '&\\1;', $str); return $str; } ob_start(); if (isset($_GET['rss'])) { header("Content-type: text/xml; charset=UTF-8"); echo ''; if (!empty($_GET['lang']) && isset($langs[$_GET['lang']])) $lang = $_GET['lang']; else $lang = false; $planet_articles = $planet->getArticles(0, $lang); $url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['REQUEST_URI']) . '/'; echo ' '.htmlspecialchars($planet_title).' '.$url.''; if (!empty($lang)) echo ' '.$lang.''; echo ' '.date(DATE_W3C, time()).' '; foreach ($planet_articles as $art) { echo ' '; } echo ' '; foreach ($planet_articles as $art) { $creator = isset($planet_feeds[$art['feed']]['name']) ? $planet_feeds[$art['feed']]['name'] : ''; echo ' '.escapeHTML($art['title']).' '.escapeHTML($art['link']).' '.date(DATE_W3C, $art['date']).' '.$art['lang'].' '.$creator.' '.escapeHTML(substr(strip_tags($art['content']), 0, 500)).'... '; } echo ' '; } else { $planet_langs = $planet->getAvailableLangs(); if (!empty($_GET['lang']) && isset($langs[$_GET['lang']])) $lang = $_GET['lang']; else $lang = false; $begin = 0; if (!empty($_GET['p']) && is_numeric($_GET['p'])) $begin = ((int)$_GET['p'] * $planet->by_page) - $planet->by_page; $nb_pages = ceil($planet->countArticles($lang) / $planet->by_page); $planet_articles = $planet->getArticles($begin, $lang); header("Content-type: text/html; charset=UTF-8"); ?> <?php echo $planet_title; ?>

'.escapeHTML($art['title']).'

'.escapeHTML($planet_feeds[$art['feed']]['name']).'

'.strftime('%d %B %Y, %H:%M', $art['date']).'

'; if (isset($langs[$art['lang']])) echo '
'.$langs[$art['lang']].'
'; echo '
'.secureContent($art['content']).'
'; } if ($nb_pages > 1) { echo ''; } ?>

Blogs

Is a blog missing? Just send me a message.