Pārlūkot izejas kodu

Meilleure gestion des erreurs lors de l'ajout d'un flux RSS + les vidéos apparaissent maintenant dans les articles + si connexion paramétrée l'actualisation des flux n'est plus permis

Marien Fressinaud 13 gadi atpakaļ
vecāks
revīzija
864318bf68

+ 165 - 167
app/controllers/feedController.php

@@ -2,96 +2,108 @@
 
 class feedController extends ActionController {
 	public function firstAction () {
-		$catDAO = new CategoryDAO ();
-		$catDAO->checkDefault ();
-	}
-
-	public function addAction () {
 		if (login_is_conf ($this->view->conf) && !is_logged ()) {
 			Error::error (
 				403,
 				array ('error' => array (Translate::t ('access_denied')))
 			);
-		} else {
-			if (Request::isPost ()) {
-				$url = Request::param ('url_rss');
-				$cat = Request::param ('category');
-				$user = Request::param ('username');
-				$pass = Request::param ('password');
-				$params = array ();
-
-				try {
-					$feed = new Feed ($url);
-					$feed->_category ($cat);
-
-					$httpAuth = '';
-					if ($user != '' || $pass != '') {
-						$httpAuth = $user . ':' . $pass;
-					}
-					$feed->_httpAuth ($httpAuth);
-
-					$feed->load ();
-
-					$feedDAO = new FeedDAO ();
-					$values = array (
-						'id' => $feed->id (),
-						'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'])) {
-						$notif = array (
-							'type' => 'bad',
-							'content' => Translate::t ('already_subscribed', $feed->name ())
-						);
-						Session::_param ('notification', $notif);
-					} elseif ($feedDAO->addFeed ($values)) {
-						$entryDAO = new EntryDAO ();
-						$entries = $feed->entries ();
-
-						foreach ($entries as $entry) {
-							$values = $entry->toArray ();
-							$entryDAO->addEntry ($values);
-						}
-
-						// notif
-						$notif = array (
-							'type' => 'good',
-							'content' => Translate::t ('feed_added', $feed->name ())
-						);
-						Session::_param ('notification', $notif);
-						$params['id'] = $feed->id ();
-					} else {
-						// notif
-						$notif = array (
-							'type' => 'bad',
-							'content' => Translate::t ('feed_not_added', $feed->name ())
-						);
-						Session::_param ('notification', $notif);
-					}
-				} catch (FeedException $e) {
-					Log::record ($e->getMessage (), Log::ERROR);
+		$catDAO = new CategoryDAO ();
+		$catDAO->checkDefault ();
+	}
+
+	public function addAction () {
+		if (Request::isPost ()) {
+			$url = Request::param ('url_rss');
+			$cat = Request::param ('category');
+			$user = Request::param ('username');
+			$pass = Request::param ('password');
+			$params = array ();
+
+			try {
+				$feed = new Feed ($url);
+				$feed->_category ($cat);
+
+				$httpAuth = '';
+				if ($user != '' || $pass != '') {
+					$httpAuth = $user . ':' . $pass;
+				}
+				$feed->_httpAuth ($httpAuth);
+
+				$feed->load ();
+
+				$feedDAO = new FeedDAO ();
+				$values = array (
+					'id' => $feed->id (),
+					'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' => Translate::t ('internal_problem_feed')
+						'content' => Translate::t ('already_subscribed', $feed->name ())
 					);
 					Session::_param ('notification', $notif);
-				} catch (Exception $e) {
-					// notif
+				} elseif (!$feedDAO->addFeed ($values)) {
+					// problème au niveau de la base de données
 					$notif = array (
 						'type' => 'bad',
-						'content' => Translate::t ('invalid_url', $url)
+						'content' => Translate::t ('feed_not_added', $feed->name ())
 					);
 					Session::_param ('notification', $notif);
-				}
+				} else {
+					$entryDAO = new EntryDAO ();
+					$entries = $feed->entries ();
 
-				Request::forward (array ('c' => 'configure', 'a' => 'feed', 'params' => $params), true);
+					// on ajoute les articles en masse sans vérification
+					foreach ($entries as $entry) {
+						$values = $entry->toArray ();
+						$entryDAO->addEntry ($values);
+					}
+
+					// ok, ajout terminé
+					$notif = array (
+						'type' => 'good',
+						'content' => Translate::t ('feed_added', $feed->name ())
+					);
+					Session::_param ('notification', $notif);
+
+					// permet de rediriger vers la page de conf du flux
+					$params['id'] = $feed->id ();
+				}
+			} catch (BadUrlException $e) {
+				Log::record ($e->getMessage (), Log::ERROR);
+				$notif = array (
+					'type' => 'bad',
+					'content' => Translate::t ('invalid_url', $url)
+				);
+				Session::_param ('notification', $notif);
+			} catch (FeedException $e) {
+				Log::record ($e->getMessage (), Log::ERROR);
+				$notif = array (
+					'type' => 'bad',
+					'content' => Translate::t ('internal_problem_feed')
+				);
+				Session::_param ('notification', $notif);
+			} catch (FileNotExistException $e) {
+				// Répertoire de cache n'existe pas
+				Log::record ($e->getMessage (), Log::ERROR);
+				$notif = array (
+					'type' => 'bad',
+					'content' => Translate::t ('internal_problem_feed')
+				);
+				Session::_param ('notification', $notif);
 			}
+
+			Request::forward (array ('c' => 'configure', 'a' => 'feed', 'params' => $params), true);
 		}
 	}
 
@@ -175,114 +187,100 @@ class feedController extends ActionController {
 	}
 
 	public function massiveImportAction () {
-		if (login_is_conf ($this->view->conf) && !is_logged ()) {
-			Error::error (
-				403,
-				array ('error' => array (Translate::t ('access_denied')))
-			);
-		} else {
-			$entryDAO = new EntryDAO ();
-			$feedDAO = new FeedDAO ();
-
-			$categories = Request::param ('categories', array ());
-			$feeds = Request::param ('feeds', array ());
-
-			$this->addCategories ($categories);
-
-			$nb_month_old = $this->view->conf->oldEntries ();
-			$date_min = time () - (60 * 60 * 24 * 30 * $nb_month_old);
-
-			$error = false;
-			$i = 0;
-			foreach ($feeds as $feed) {
-				try {
-					$feed->load ();
-
-					// Enregistrement du flux
-					$values = array (
-						'id' => $feed->id (),
-						'url' => $feed->url (),
-						'category' => $feed->category (),
-						'name' => $feed->name (),
-						'website' => $feed->website (),
-						'description' => $feed->description (),
-						'lastUpdate' => 0
-					);
+		$entryDAO = new EntryDAO ();
+		$feedDAO = new FeedDAO ();
+
+		$categories = Request::param ('categories', array ());
+		$feeds = Request::param ('feeds', array ());
+
+		$this->addCategories ($categories);
+
+		$nb_month_old = $this->view->conf->oldEntries ();
+		$date_min = time () - (60 * 60 * 24 * 30 * $nb_month_old);
+
+		$error = false;
+		$i = 0;
+		foreach ($feeds as $feed) {
+			try {
+				$feed->load ();
 
-					if (!$feedDAO->searchByUrl ($values['url'])) {
-						if (!$feedDAO->addFeed ($values)) {
-							$error = true;
-						}
+				// Enregistrement du flux
+				$values = array (
+					'id' => $feed->id (),
+					'url' => $feed->url (),
+					'category' => $feed->category (),
+					'name' => $feed->name (),
+					'website' => $feed->website (),
+					'description' => $feed->description (),
+					'lastUpdate' => 0
+				);
+
+				if (!$feedDAO->searchByUrl ($values['url'])) {
+					if (!$feedDAO->addFeed ($values)) {
+						$error = true;
 					}
-				} catch (FeedException $e) {
-					$error = true;
-					Log::record ($e->getMessage (), Log::ERROR);
 				}
+			} catch (FeedException $e) {
+				$error = true;
+				Log::record ($e->getMessage (), Log::ERROR);
 			}
+		}
 
-			if ($error) {
-				$res = Translate::t ('feeds_imported_with_errors');
-			} else {
-				$res = Translate::t ('feeds_imported');
-			}
-			$notif = array (
-				'type' => 'good',
-				'content' => $res
-			);
-			Session::_param ('notification', $notif);
-
-			Request::forward (array (
-				'c' => 'configure',
-				'a' => 'importExport'
-			), true);
+		if ($error) {
+			$res = Translate::t ('feeds_imported_with_errors');
+		} else {
+			$res = Translate::t ('feeds_imported');
 		}
+		$notif = array (
+			'type' => 'good',
+			'content' => $res
+		);
+		Session::_param ('notification', $notif);
+
+		Request::forward (array (
+			'c' => 'configure',
+			'a' => 'importExport'
+		), true);
 	}
 
 	public function deleteAction () {
-		if (login_is_conf ($this->view->conf) && !is_logged ()) {
-			Error::error (
-				403,
-				array ('error' => array (Translate::t ('access_denied')))
-			);
-		} else {
-			$type = Request::param ('type', 'feed');
-			$id = Request::param ('id');
+		$type = Request::param ('type', 'feed');
+		$id = Request::param ('id');
 
-			$feedDAO = new FeedDAO ();
-			if ($type == 'category') {
-				if ($feedDAO->deleteFeedByCategory ($id)) {
-					$notif = array (
-						'type' => 'good',
-						'content' => Translate::t ('category_emptied')
-					);
-				} else {
-					$notif = array (
-						'type' => 'bad',
-						'content' => Translate::t ('error_occured')
-					);
-				}
+		$feedDAO = new FeedDAO ();
+		if ($type == 'category') {
+			if ($feedDAO->deleteFeedByCategory ($id)) {
+				$notif = array (
+					'type' => 'good',
+					'content' => Translate::t ('category_emptied')
+				);
 			} else {
-				if ($feedDAO->deleteFeed ($id)) {
-					$notif = array (
-						'type' => 'good',
-						'content' => Translate::t ('feed_deleted')
-					);
-				} else {
-					$notif = array (
-						'type' => 'bad',
-						'content' => Translate::t ('error_occured')
-					);
-				}
+				$notif = array (
+					'type' => 'bad',
+					'content' => Translate::t ('error_occured')
+				);
 			}
-
-			Session::_param ('notification', $notif);
-
-			if ($type == 'category') {
-				Request::forward (array ('c' => 'configure', 'a' => 'categorize'), true);
+		} else {
+			if ($feedDAO->deleteFeed ($id)) {
+				$notif = array (
+					'type' => 'good',
+					'content' => Translate::t ('feed_deleted')
+				);
 			} else {
-				Request::forward (array ('c' => 'configure', 'a' => 'feed'), true);
+				$notif = array (
+					'type' => 'bad',
+					'content' => Translate::t ('error_occured')
+				);
 			}
 		}
+
+		Session::_param ('notification', $notif);
+
+		if ($type == 'category') {
+			Request::forward (array ('c' => 'configure', 'a' => 'categorize'), true);
+		} else {
+			Request::forward (array ('c' => 'configure', 'a' => 'feed'), true);
+		}
 	}
 
 	private function addCategories ($categories) {

+ 1 - 1
app/layout/nav_menu.phtml

@@ -1,6 +1,7 @@
 <div class="nav_menu">
 	<a class="btn toggle_aside" href="#aside_flux"><i class="icon i_category"></i></a>
 
+	<?php if (!login_is_conf ($this->conf) || is_logged ()) { ?>
 	<a id="actualize" class="btn" href="<?php echo _url ('feed', 'actualize'); ?>"><i class="icon i_refresh"></i></a>
 
 	<?php
@@ -15,7 +16,6 @@
 		}
 	?>
 
-	<?php if (!login_is_conf ($this->conf) || is_logged ()) { ?>
 	<div class="stick">
 		<a class="read_all btn" href="<?php echo _url ('entry', 'read', 'is_read', 1, 'get', $get); ?>"><?php echo Translate::t ('mark_read'); ?></a>
 		<div class="dropdown">

+ 6 - 0
app/models/Exception/FeedException.php

@@ -5,3 +5,9 @@ class FeedException extends Exception {
 		parent::__construct ($message);
 	}
 }
+
+class BadUrlException extends FeedException {
+	public function __construct ($url) {
+		parent::__construct ('`' . $url . '` is not a valid URL');
+	}
+}

+ 12 - 2
app/models/Feed.php

@@ -99,7 +99,7 @@ class Feed extends Model {
 		if (!is_null ($value) && filter_var ($value, FILTER_VALIDATE_URL)) {
 			$this->url = $value;
 		} else {
-			throw new Exception ();
+			throw new BadUrlException ($value);
 		}
 	}
 	public function _category ($value) {
@@ -155,15 +155,23 @@ class Feed extends Model {
 
 				$feed->set_feed_url ($url);
 				$feed->set_cache_location (CACHE_PATH);
+				$feed->strip_htmltags (array (
+					'base', 'blink', 'body', 'doctype',
+					'font', 'form', 'frame', 'frameset', 'html',
+					'input', 'marquee', 'meta', 'noscript',
+					'param', 'script', 'style'
+				));
 				$feed->init ();
 
-				if ($feed->error()) {
+				if ($feed->error ()) {
 					throw new FeedException ($feed->error);
 				}
 
+				// si on a utilisé l'auto-discover, notre url va avoir changé
 				$subscribe_url = $feed->subscribe_url ();
 				if (!is_null ($subscribe_url) && $subscribe_url != $this->url) {
 					if ($this->httpAuth != '') {
+						// on enlève les id si authentification HTTP
 						$subscribe_url = preg_replace ('#((.+)://)((.+)@)(.+)#', '${1}${5}', $subscribe_url);
 					}
 					$this->_url ($subscribe_url);
@@ -172,6 +180,8 @@ class Feed extends Model {
 				$this->_name (!is_null ($title) ? $title : $this->url);
 				$this->_website ($feed->get_link ());
 				$this->_description ($feed->get_description ());
+
+				// et on charge les articles du flux
 				$this->loadEntries ($feed);
 			}
 		}