/**
* Add a new feed to the database
*
* Adds the specified feed name and URL to the database. If no name is set
* by the user, it fetches one from the feed. If the URL specified is a HTML
* page and not a feed, it lets SimplePie do autodiscovery and uses the XML
* url returned.
*
* @since 1.0
*
* @param string $url URL to feed or website (if autodiscovering)
* @param string $name Title/Name of feed
* @param string $cat Category to add feed to
* @return bool True if succeeded, false if failed
*/
public function add($url, $name = '', $cat = 'default')
{
if (empty($url)) {
throw new Exception(_r("Couldn't add feed: No feed URL supplied"), Errors::get_code('admin.feeds.no_url'));
}
if (!preg_match('#https|http|feed#', $url)) {
if (strpos($url, '://')) {
throw new Exception(_r('Unsupported URL protocol'), Errors::get_code('admin.feeds.protocol_error'));
}
$url = 'http://' . $url;
}
require_once LILINA_INCPATH . '/contrib/simplepie/simplepie.inc';
$feed_info = new SimplePie();
$feed_info->set_useragent(LILINA_USERAGENT . ' SimplePie/' . SIMPLEPIE_BUILD);
$feed_info->set_stupidly_fast(true);
$feed_info->set_cache_location(get_option('cachedir'));
$feed_info->set_favicon_handler(get_option('baseurl') . '/lilina-favicon.php');
$feed_info->set_feed_url($url);
$feed_info->init();
$feed_error = $feed_info->error();
$feed_url = $feed_info->subscribe_url();
if (!empty($feed_error)) {
throw new Exception(sprintf(_r("Couldn't add feed: %s is not a valid URL or the server could not be accessed. Additionally, no feeds could be found by autodiscovery."), $url), Errors::get_code('admin.feeds.invalid_url'));
}
if (empty($name)) {
//Get it from the feed
$name = $feed_info->get_title();
}
$id = sha1($feed_url);
$this->feeds[$id] = array('feed' => $feed_url, 'url' => $feed_info->get_link(), 'id' => $id, 'name' => $name, 'cat' => $cat, 'icon' => $feed_info->get_favicon());
$this->feeds[$id] = apply_filters('feed-create', $this->feeds[$id], $url);
$this->save();
return array('msg' => sprintf(_r('Added feed "%1$s"'), $name), 'id' => $id);
}