_url($url); } else { $this->url = $url; } } public static function example() { $f = new FreshRSS_Feed('http://example.net/', false); $f->faviconPrepare(); return $f; } public function id() { return $this->id; } public function hash() { if ($this->hash === null) { $salt = FreshRSS_Context::$system_conf->salt; $this->hash = hash('crc32b', $salt . $this->url); } return $this->hash; } public function url($includeCredentials = true) { return $includeCredentials ? $this->url : SimplePie_Misc::url_remove_credentials($this->url); } public function selfUrl() { return $this->selfUrl; } public function hubUrl() { return $this->hubUrl; } public function category() { return $this->category; } public function entries() { return $this->entries === null ? array() : $this->entries; } public function name() { return $this->name; } public function website() { return $this->website; } public function description() { return $this->description; } public function lastUpdate() { return $this->lastUpdate; } public function priority() { return $this->priority; } public function pathEntries() { return $this->pathEntries; } public function httpAuth($raw = true) { if ($raw) { return $this->httpAuth; } else { $pos_colon = strpos($this->httpAuth, ':'); $user = substr($this->httpAuth, 0, $pos_colon); $pass = substr($this->httpAuth, $pos_colon + 1); return array( 'username' => $user, 'password' => $pass ); } } public function inError() { return $this->error; } public function keepHistory() { return $this->keep_history; } public function ttl() { return $this->ttl; } public function attributes($key = '') { if ($key == '') { return $this->attributes; } else { return isset($this->attributes[$key]) ? $this->attributes[$key] : null; } } public function mute() { return $this->mute; } // public function ttlExpire() { // $ttl = $this->ttl; // if ($ttl == self::TTL_DEFAULT) { //Default // $ttl = FreshRSS_Context::$user_conf->ttl_default; // } // if ($ttl == -1) { //Never // $ttl = 64000000; //~2 years. Good enough for PubSubHubbub logic // } // return $this->lastUpdate + $ttl; // } public function nbEntries() { if ($this->nbEntries < 0) { $feedDAO = FreshRSS_Factory::createFeedDao(); $this->nbEntries = $feedDAO->countEntries($this->id()); } return $this->nbEntries; } public function nbNotRead() { if ($this->nbNotRead < 0) { $feedDAO = FreshRSS_Factory::createFeedDao(); $this->nbNotRead = $feedDAO->countNotRead($this->id()); } return $this->nbNotRead; } public function faviconPrepare() { global $favicons_dir; require_once(LIB_PATH . '/favicons.php'); $url = $this->website; if ($url == '') { $url = $this->url; } $txt = $favicons_dir . $this->hash() . '.txt'; if (!file_exists($txt)) { file_put_contents($txt, $url); } if (FreshRSS_Context::$isCli) { $ico = $favicons_dir . $this->hash() . '.ico'; $ico_mtime = @filemtime($ico); $txt_mtime = @filemtime($txt); if ($txt_mtime != false && ($ico_mtime == false || $ico_mtime < $txt_mtime || ($ico_mtime < time() - (14 * 86400)))) { // no ico file or we should download a new one. $url = file_get_contents($txt); download_favicon($url, $ico) || touch($ico); } } } public static function faviconDelete($hash) { $path = DATA_PATH . '/favicons/' . $hash; @unlink($path . '.ico'); @unlink($path . '.txt'); } public function favicon() { return Minz_Url::display('/f.php?' . $this->hash()); } public function _id($value) { $this->id = $value; } public function _url($value, $validate = true) { $this->hash = null; if ($validate) { $value = checkUrl($value); } if (empty($value)) { throw new FreshRSS_BadUrl_Exception($value); } $this->url = $value; } public function _category($value) { $value = intval($value); $this->category = $value >= 0 ? $value : 0; } public function _name($value) { $this->name = $value === null ? '' : $value; } public function _website($value, $validate = true) { if ($validate) { $value = checkUrl($value); } if (empty($value)) { $value = ''; } $this->website = $value; } public function _description($value) { $this->description = $value === null ? '' : $value; } public function _lastUpdate($value) { $this->lastUpdate = $value; } public function _priority($value) { $this->priority = intval($value); } public function _pathEntries($value) { $this->pathEntries = $value; } public function _httpAuth($value) { $this->httpAuth = $value; } public function _error($value) { $this->error = (bool)$value; } public function _keepHistory($value) { $value = intval($value); $value = min($value, 1000000); $value = max($value, self::KEEP_HISTORY_DEFAULT); $this->keep_history = $value; } public function _ttl($value) { $value = intval($value); $value = min($value, 100000000); $this->ttl = abs($value); $this->mute = $value < self::TTL_DEFAULT; } public function _attributes($key, $value) { if ($key == '') { if (is_string($value)) { $value = json_decode($value, true); } if (is_array($value)) { $this->attributes = $value; } } elseif ($value === null) { unset($this->attributes[$key]); } else { $this->attributes[$key] = $value; } } public function _nbNotRead($value) { $this->nbNotRead = intval($value); } public function _nbEntries($value) { $this->nbEntries = intval($value); } public function load($loadDetails = false, $noCache = false) { if ($this->url !== null) { if (CACHE_PATH === false) { throw new Minz_FileNotExistException( 'CACHE_PATH', Minz_Exception::ERROR ); } else { $url = htmlspecialchars_decode($this->url, ENT_QUOTES); if ($this->httpAuth != '') { $url = preg_replace('#((.+)://)(.+)#', '${1}' . $this->httpAuth . '@${3}', $url); } $feed = customSimplePie($this->attributes()); if (substr($url, -11) === '#force_feed') { $feed->force_feed(true); $url = substr($url, 0, -11); } $feed->set_feed_url($url); if (!$loadDetails) { //Only activates auto-discovery when adding a new feed $feed->set_autodiscovery_level(SIMPLEPIE_LOCATOR_NONE); } if ($this->attributes('clear_cache')) { // Do not use `$simplePie->enable_cache(false);` as it would prevent caching in multiuser context $this->clearCache(); } Minz_ExtensionManager::callHook('simplepie_before_init', $feed, $this); $mtime = $feed->init(); if ((!$mtime) || $feed->error()) { $errorMessage = $feed->error(); throw new FreshRSS_Feed_Exception( ($errorMessage == '' ? 'Unknown error for feed' : $errorMessage) . ' [' . $url . ']' ); } $links = $feed->get_links('self'); $this->selfUrl = isset($links[0]) ? $links[0] : null; $links = $feed->get_links('hub'); $this->hubUrl = isset($links[0]) ? $links[0] : null; if ($loadDetails) { // si on a utilisé l'auto-discover, notre url va avoir changé $subscribe_url = $feed->subscribe_url(false); $title = strtr(html_only_entity_decode($feed->get_title()), array('<' => '<', '>' => '>', '"' => '"')); //HTML to HTML-PRE //ENT_COMPAT except & $this->_name($title == '' ? $url : $title); $this->_website(html_only_entity_decode($feed->get_link())); $this->_description(html_only_entity_decode($feed->get_description())); } else { //The case of HTTP 301 Moved Permanently $subscribe_url = $feed->subscribe_url(true); } $clean_url = SimplePie_Misc::url_remove_credentials($subscribe_url); if ($subscribe_url !== null && $subscribe_url !== $url) { $this->_url($clean_url); } if (($mtime === true) || ($mtime > $this->lastUpdate) || $noCache) { //Minz_Log::debug('FreshRSS no cache ' . $mtime . ' > ' . $this->lastUpdate . ' for ' . $clean_url); $this->loadEntries($feed); // et on charge les articles du flux } else { //Minz_Log::debug('FreshRSS use cache for ' . $clean_url); $this->entries = array(); } $feed->__destruct(); //http://simplepie.org/wiki/faq/i_m_getting_memory_leaks unset($feed); } } } public function loadEntries($feed) { $entries = array(); $guids = array(); $hasUniqueGuids = true; foreach ($feed->get_items() as $item) { $title = html_only_entity_decode(strip_tags($item->get_title())); $authors = $item->get_authors(); $link = $item->get_permalink(); $date = @strtotime($item->get_date()); //Tag processing (tag == category) $categories = $item->get_categories(); $tags = array(); if (is_array($categories)) { foreach ($categories as $category) { $text = html_only_entity_decode($category->get_label()); //Some feeds use a single category with comma-separated tags $labels = explode(',', $text); if (is_array($labels)) { foreach ($labels as $label) { $tags[] = trim($label); } } } $tags = array_unique($tags); } $content = html_only_entity_decode($item->get_content()); if ($item->get_enclosures() != null) { $elinks = array(); foreach ($item->get_enclosures() as $enclosure) { $elink = $enclosure->get_link(); if ($elink != '' && empty($elinks[$elink])) { $content .= '
' . $enclosure->get_title() . '
'; } $enclosureContent = ''; $elinks[$elink] = true; $mime = strtolower($enclosure->get_type()); $medium = strtolower($enclosure->get_medium()); if ($medium === 'image' || strpos($mime, 'image/') === 0) { $enclosureContent .= ''; } elseif ($medium === 'audio' || strpos($mime, 'audio/') === 0) { $enclosureContent .= ''; } elseif ($medium === 'video' || strpos($mime, 'video/') === 0) { $enclosureContent .= ''; } elseif ($medium != '' || strpos($mime, 'application/') === 0 || strpos($mime, 'text/') === 0) { $enclosureContent .= ''; } else { unset($elinks[$elink]); } $thumbnailContent = ''; if ($enclosure->get_thumbnails() != null) { foreach ($enclosure->get_thumbnails() as $thumbnail) { if (empty($elinks[$thumbnail])) { $elinks[$thumbnail] = true; $thumbnailContent .= ''; } } } $content .= $thumbnailContent; $content .= $enclosureContent; if ($enclosure->get_description() != '') { $content .= '' . $enclosure->get_description() . ''; } $content .= "