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 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);
     }
 }