cache_expire)) return false; return unserialize(file_get_contents(dirname(__FILE__) . '/.feedCache')); } private function storeCache($content) { file_put_contents(dirname(__FILE__) . '/.feedCache', serialize($content)); return true; } private function parseRSS($url) { if (!$this->rss) { require_once dirname(__FILE__) . '/lastRSS.php'; $this->rss = new lastRSS; $this->rss->cache_dir = ''; } if ($feed = $this->rss->get($url)) { $items = array(); $filter_title = false; $encoding = strtolower($feed['encoding']); // Facebook grumpf if (preg_match('!^http://www.facebook.com/feeds/status.php!i', $url)) { $filter_title = '!^\S+\s(.*)!'; } foreach ($feed['items'] as $item) { $item['title'] = html_entity_decode($item['title'], ENT_COMPAT, 'UTF-8'); $item['description'] = html_entity_decode($item['description'], ENT_COMPAT, 'UTF-8'); if ($encoding != 'utf-8') { $item['title'] = utf8_magic_encode($item['title']); $item['description'] = utf8_magic_encode($item['description']); } if ($filter_title) $item['title'] = preg_replace($filter_title, '\\1', $item['title']); $items[] = array( 'title' => $item['title'], 'url' => $item['link'], 'date' => $item['pubDate'], 'desc' => html_entity_decode($item['description']), 'thumb' => !empty($item['media:thumbnail']['url']) ? $item['media:thumbnail']['url'] : '', ); } return $items; } return false; } private function parseHTML($path) { if ($date = @filemtime($path)) { $file = file_get_contents($path); $title = $desc = ''; if (preg_match('!(.*)$!i', $file, $match)) { $title = trim(html_entity_decode($match[1])); $title = utf8_magic_encode($title); } if (preg_match('!]*>(.*)$!msi', $file, $match)) { $desc = preg_replace('!!msiU', '', $match[1]); $desc = strip_tags($desc); $desc = preg_replace('!([ \n]){2,}!', '\\1', $desc); $desc = trim(html_entity_decode($desc)); $desc = substr($desc, 0, 600) . '...'; $desc = utf8_magic_encode($desc); } return array('date' => $date, 'desc' => $desc, 'title' => $title); } return false; } private function addItem($source_id, $date, $title, $desc, $url, $thumb='') { if (!empty($this->sources[$source_id]['options']['hide_post_url'])) $url = ''; if (empty($date)) throw new Feed_Exception("Pas de date"); $this->items[$date] = array( 'source' => $source_id, 'date' => $date, 'title' => $title, 'desc' => $desc, 'url' => $url, 'thumb' => $thumb, ); } private function loadItems() { if ($this->loaded) return true; if ($this->items = $this->getCache()) return true; foreach ($this->sources as $id=>$source) { if ($source['type'] == 'rss') { if ($items = $this->parseRSS($source['options']['feed_url'])) { foreach ($items as $item) { $this->addItem($id, $item['date'], $item['title'], $item['desc'], $item['url'], $item['thumb']); } } } elseif ($source['type'] == 'html') { if ($item = $this->parseHTML($source['options']['path'])) { $this->addItem($id, $item['date'], $item['title'], $item['desc'], $source['url']); } } } krsort($this->items); $this->storeCache($this->items); } public function addSource($type, $id, $name, $url, $options=array()) { if ($type == 'rss' && empty($options['feed_url'])) throw new Feed_Exception("You have to specify feed_url in options for source of type RSS"); if ($type == 'html' && empty($options['path'])) throw new Feed_Exception("You have to specify path in options for source of type html"); $this->sources[$id] = array( 'type' => $type, 'name' => $name, 'url' => $url, 'options' => $options, ); } public function &getSource($id) { if (isset($this->sources[$id])) return $this->sources[$id]; return false; } public function &listSources() { return $this->sources; } public function getLastItems($nb=10) { $this->loadItems(); return array_slice($this->items, 0, $nb, true); } public function getLastItemDate() { $this->loadItems(); list($item) = array_slice($this->items, 0, 1); return $item['date']; } public function setExpiration($interval) { if (!is_int($interval) || $interval < 1) throw new Feed_Exception("Expiration interval must be a number of minutes superior to zero"); $this->cache_expire = $interval * 60; } } ?>