Ejemplo n.º 1
0
 /**
  * This action subscribes to a feed.
  *
  * It can be reached by both GET and POST requests.
  *
  * GET request displays a form to add and configure a feed.
  * Request parameter is:
  *   - url_rss (default: false)
  *
  * POST request adds a feed in database.
  * Parameters are:
  *   - url_rss (default: false)
  *   - category (default: false)
  *   - new_category (required if category == 'nc')
  *   - http_user (default: false)
  *   - http_pass (default: false)
  * It tries to get website information from RSS feed.
  * If no category is given, feed is added to the default one.
  *
  * If url_rss is false, nothing happened.
  */
 public function addAction()
 {
     $url = Minz_Request::param('url_rss');
     if ($url === false) {
         // No url, do nothing
         Minz_Request::forward(array('c' => 'subscription', 'a' => 'index'), true);
     }
     $feedDAO = FreshRSS_Factory::createFeedDao();
     $this->catDAO = new FreshRSS_CategoryDAO();
     $url_redirect = array('c' => 'subscription', 'a' => 'index', 'params' => array());
     $limits = FreshRSS_Context::$system_conf->limits;
     $this->view->feeds = $feedDAO->listFeeds();
     if (count($this->view->feeds) >= $limits['max_feeds']) {
         Minz_Request::bad(_t('feedback.sub.feed.over_max', $limits['max_feeds']), $url_redirect);
     }
     if (Minz_Request::isPost()) {
         @set_time_limit(300);
         $cat = Minz_Request::param('category');
         if ($cat === 'nc') {
             // User want to create a new category, new_category parameter
             // must exist
             $new_cat = Minz_Request::param('new_category');
             if (empty($new_cat['name'])) {
                 $cat = false;
             } else {
                 $cat = $this->catDAO->addCategory($new_cat);
             }
         }
         if ($cat === false) {
             // If category was not given or if creating new category failed,
             // get the default category
             $this->catDAO->checkDefault();
             $def_cat = $this->catDAO->getDefault();
             $cat = $def_cat->id();
         }
         // HTTP information are useful if feed is protected behind a
         // HTTP authentication
         $user = trim(Minz_Request::param('http_user', ''));
         $pass = Minz_Request::param('http_pass', '');
         $http_auth = '';
         if ($user != '' && $pass != '') {
             //TODO: Sanitize
             $http_auth = $user . ':' . $pass;
         }
         $transaction_started = false;
         try {
             $feed = new FreshRSS_Feed($url);
         } catch (FreshRSS_BadUrl_Exception $e) {
             // Given url was not a valid url!
             Minz_Log::warning($e->getMessage());
             Minz_Request::bad(_t('feedback.sub.feed.invalid_url', $url), $url_redirect);
         }
         try {
             $feed->load(true);
         } catch (FreshRSS_Feed_Exception $e) {
             // Something went bad (timeout, server not found, etc.)
             Minz_Log::warning($e->getMessage());
             Minz_Request::bad(_t('feedback.sub.feed.internal_problem', _url('index', 'logs')), $url_redirect);
         } catch (Minz_FileNotExistException $e) {
             // Cache directory doesn't exist!
             Minz_Log::error($e->getMessage());
             Minz_Request::bad(_t('feedback.sub.feed.internal_problem', _url('index', 'logs')), $url_redirect);
         }
         if ($feedDAO->searchByUrl($feed->url())) {
             Minz_Request::bad(_t('feedback.sub.feed.already_subscribed', $feed->name()), $url_redirect);
         }
         $feed->_category($cat);
         $feed->_httpAuth($http_auth);
         // Call the extension hook
         $name = $feed->name();
         $feed = Minz_ExtensionManager::callHook('feed_before_insert', $feed);
         if ($feed === null) {
             Minz_Request::bad(_t('feedback.sub.feed.not_added', $name), $url_redirect);
         }
         $values = array('url' => $feed->url(), 'category' => $feed->category(), 'name' => $feed->name(), 'website' => $feed->website(), 'description' => $feed->description(), 'lastUpdate' => time(), 'httpAuth' => $feed->httpAuth());
         $id = $feedDAO->addFeed($values);
         if (!$id) {
             // There was an error in database... we cannot say what here.
             Minz_Request::bad(_t('feedback.sub.feed.not_added', $feed->name()), $url_redirect);
         }
         // Ok, feed has been added in database. Now we have to refresh entries.
         $feed->_id($id);
         $feed->faviconPrepare();
         //$feed->pubSubHubbubPrepare();	//TODO: prepare PubSubHubbub already when adding the feed
         $is_read = FreshRSS_Context::$user_conf->mark_when['reception'] ? 1 : 0;
         $entryDAO = FreshRSS_Factory::createEntryDao();
         // We want chronological order and SimplePie uses reverse order.
         $entries = array_reverse($feed->entries());
         // Calculate date of oldest entries we accept in DB.
         $nb_month_old = FreshRSS_Context::$user_conf->old_entries;
         $date_min = time() - 3600 * 24 * 30 * $nb_month_old;
         // Use a shared statement and a transaction to improve a LOT the
         // performances.
         $feedDAO->beginTransaction();
         foreach ($entries as $entry) {
             // Entries are added without any verification.
             $entry->_feed($feed->id());
             $entry->_id(min(time(), $entry->date(true)) . uSecString());
             $entry->_isRead($is_read);
             $entry = Minz_ExtensionManager::callHook('entry_before_insert', $entry);
             if ($entry === null) {
                 // An extension has returned a null value, there is nothing to insert.
                 continue;
             }
             $values = $entry->toArray();
             $entryDAO->addEntry($values);
         }
         $feedDAO->updateLastUpdate($feed->id());
         $feedDAO->commit();
         // Entries are in DB, we redirect to feed configuration page.
         $url_redirect['params']['id'] = $feed->id();
         Minz_Request::good(_t('feedback.sub.feed.added', $feed->name()), $url_redirect);
     } else {
         // GET request: we must ask confirmation to user before adding feed.
         Minz_View::prependTitle(_t('sub.feed.title_add') . ' · ');
         $this->view->categories = $this->catDAO->listCategories(false);
         $this->view->feed = new FreshRSS_Feed($url);
         try {
             // We try to get more information about the feed.
             $this->view->feed->load(true);
             $this->view->load_ok = true;
         } catch (Exception $e) {
             $this->view->load_ok = false;
         }
         $feed = $feedDAO->searchByUrl($this->view->feed->url());
         if ($feed) {
             // Already subscribe so we redirect to the feed configuration page.
             $url_redirect['params']['id'] = $feed->id();
             Minz_Request::good(_t('feedback.sub.feed.already_subscribed', $feed->name()), $url_redirect);
         }
     }
 }
Ejemplo n.º 2
0
 public static function daoToFeed($listDAO, $catID = null)
 {
     $list = array();
     if (!is_array($listDAO)) {
         $listDAO = array($listDAO);
     }
     foreach ($listDAO as $key => $dao) {
         if (!isset($dao['name'])) {
             continue;
         }
         if (isset($dao['id'])) {
             $key = $dao['id'];
         }
         if ($catID === null) {
             $category = isset($dao['category']) ? $dao['category'] : 0;
         } else {
             $category = $catID;
         }
         $myFeed = new FreshRSS_Feed(isset($dao['url']) ? $dao['url'] : '', false);
         $myFeed->_category($category);
         $myFeed->_name($dao['name']);
         $myFeed->_website(isset($dao['website']) ? $dao['website'] : '', false);
         $myFeed->_description(isset($dao['description']) ? $dao['description'] : '');
         $myFeed->_lastUpdate(isset($dao['lastupdate']) ? $dao['lastupdate'] : 0);
         $myFeed->_priority(isset($dao['priority']) ? $dao['priority'] : 10);
         $myFeed->_pathEntries(isset($dao['pathentries']) ? $dao['pathentries'] : '');
         $myFeed->_httpAuth(isset($dao['httpauth']) ? base64_decode($dao['httpauth']) : '');
         $myFeed->_error(isset($dao['error']) ? $dao['error'] === 't' ? true : false : 0);
         $myFeed->_keepHistory(isset($dao['keep_history']) ? $dao['keep_history'] : -2);
         $myFeed->_ttl(isset($dao['ttl']) ? $dao['ttl'] : -2);
         $myFeed->_nbNotRead(isset($dao['cache_nbunreads']) ? $dao['cache_nbunreads'] : 0);
         $myFeed->_nbEntries(isset($dao['cache_nbentries']) ? $dao['cache_nbentries'] : 0);
         if (isset($dao['id'])) {
             $myFeed->_id($dao['id']);
         }
         $list[$key] = $myFeed;
     }
     return $list;
 }
Ejemplo n.º 3
0
 public static function example()
 {
     $f = new FreshRSS_Feed('http://example.net/', false);
     $f->faviconPrepare();
     return $f;
 }
Ejemplo n.º 4
0
 /**
  * This method import a JSON-based feed (Google Reader format).
  *
  * @param array $origin represents a feed.
  * @param boolean $google_compliant takes care of some specific values if true.
  * @return FreshRSS_Feed if feed is in database at the end of the process,
  *         else null.
  */
 private function addFeedJson($origin, $google_compliant)
 {
     $default_cat = $this->catDAO->getDefault();
     $return = null;
     $key = $google_compliant ? 'htmlUrl' : 'feedUrl';
     $url = $origin[$key];
     $name = $origin['title'];
     $website = $origin['htmlUrl'];
     try {
         // Create a Feed object and add it in database.
         $feed = new FreshRSS_Feed($url);
         $feed->_category($default_cat->id());
         $feed->_name($name);
         $feed->_website($website);
         // Call the extension hook
         $feed = Minz_ExtensionManager::callHook('feed_before_insert', $feed);
         if (!is_null($feed)) {
             // addFeedObject checks if feed is already in DB so nothing else to
             // check here.
             $id = $this->feedDAO->addFeedObject($feed);
             if ($id !== false) {
                 $feed->_id($id);
                 $return = $feed;
             }
         }
     } catch (FreshRSS_Feed_Exception $e) {
         Minz_Log::warning($e->getMessage());
     }
     return $return;
 }
Ejemplo n.º 5
0
function getFeed($outline, $cat_id)
{
    $url = (string) $outline['xmlUrl'];
    $url = htmlspecialchars($url, ENT_COMPAT, 'UTF-8');
    $title = '';
    if (isset($outline['text'])) {
        $title = (string) $outline['text'];
    } elseif (isset($outline['title'])) {
        $title = (string) $outline['title'];
    }
    $title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8');
    $feed = new FreshRSS_Feed($url);
    $feed->_category($cat_id);
    $feed->_name($title);
    if (isset($outline['htmlUrl'])) {
        $feed->_website(htmlspecialchars((string) $outline['htmlUrl'], ENT_COMPAT, 'UTF-8'));
    }
    if (isset($outline['description'])) {
        $feed->_description(sanitizeHTML((string) $outline['description']));
    }
    return $feed;
}
Ejemplo n.º 6
0
 public function addAction()
 {
     @set_time_limit(300);
     if (Minz_Request::isPost()) {
         $this->catDAO = new FreshRSS_CategoryDAO();
         $this->catDAO->checkDefault();
         $url = Minz_Request::param('url_rss');
         $cat = Minz_Request::param('category', false);
         if ($cat === false) {
             $def_cat = $this->catDAO->getDefault();
             $cat = $def_cat->id();
         }
         $user = Minz_Request::param('http_user');
         $pass = Minz_Request::param('http_pass');
         $params = array();
         $transactionStarted = false;
         try {
             $feed = new FreshRSS_Feed($url);
             $feed->_category($cat);
             $httpAuth = '';
             if ($user != '' || $pass != '') {
                 $httpAuth = $user . ':' . $pass;
             }
             $feed->_httpAuth($httpAuth);
             $feed->load(true);
             $feedDAO = new FreshRSS_FeedDAO();
             $values = array('url' => $feed->url(), 'category' => $feed->category(), 'name' => $feed->name(), 'website' => $feed->website(), 'description' => $feed->description(), 'lastUpdate' => time(), 'httpAuth' => $feed->httpAuth());
             if ($feedDAO->searchByUrl($values['url'])) {
                 // on est déjà abonné à ce flux
                 $notif = array('type' => 'bad', 'content' => Minz_Translate::t('already_subscribed', $feed->name()));
                 Minz_Session::_param('notification', $notif);
             } else {
                 $id = $feedDAO->addFeed($values);
                 if (!$id) {
                     // problème au niveau de la base de données
                     $notif = array('type' => 'bad', 'content' => Minz_Translate::t('feed_not_added', $feed->name()));
                     Minz_Session::_param('notification', $notif);
                 } else {
                     $feed->_id($id);
                     $feed->faviconPrepare();
                     $is_read = $this->view->conf->mark_when['reception'] ? 1 : 0;
                     $entryDAO = new FreshRSS_EntryDAO();
                     $entries = array_reverse($feed->entries());
                     //We want chronological order and SimplePie uses reverse order
                     // on calcule la date des articles les plus anciens qu'on accepte
                     $nb_month_old = $this->view->conf->old_entries;
                     $date_min = time() - 3600 * 24 * 30 * $nb_month_old;
                     $transactionStarted = true;
                     $feedDAO->beginTransaction();
                     // on ajoute les articles en masse sans vérification
                     foreach ($entries as $entry) {
                         $values = $entry->toArray();
                         $values['id_feed'] = $feed->id();
                         $values['id'] = min(time(), $entry->date(true)) . uSecString();
                         $values['is_read'] = $is_read;
                         $entryDAO->addEntry($values);
                     }
                     $feedDAO->updateLastUpdate($feed->id());
                     $feedDAO->commit();
                     $transactionStarted = false;
                     // ok, ajout terminé
                     $notif = array('type' => 'good', 'content' => Minz_Translate::t('feed_added', $feed->name()));
                     Minz_Session::_param('notification', $notif);
                     // permet de rediriger vers la page de conf du flux
                     $params['id'] = $feed->id();
                 }
             }
         } catch (FreshRSS_BadUrl_Exception $e) {
             Minz_Log::record($e->getMessage(), Minz_Log::WARNING);
             $notif = array('type' => 'bad', 'content' => Minz_Translate::t('invalid_url', $url));
             Minz_Session::_param('notification', $notif);
         } catch (FreshRSS_Feed_Exception $e) {
             Minz_Log::record($e->getMessage(), Minz_Log::WARNING);
             $notif = array('type' => 'bad', 'content' => Minz_Translate::t('internal_problem_feed'));
             Minz_Session::_param('notification', $notif);
         } catch (Minz_FileNotExistException $e) {
             // Répertoire de cache n'existe pas
             Minz_Log::record($e->getMessage(), Minz_Log::ERROR);
             $notif = array('type' => 'bad', 'content' => Minz_Translate::t('internal_problem_feed'));
             Minz_Session::_param('notification', $notif);
         }
         if ($transactionStarted) {
             $feedDAO->rollBack();
         }
         Minz_Request::forward(array('c' => 'configure', 'a' => 'feed', 'params' => $params), true);
     }
 }