configureController.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. class configureController extends ActionController {
  3. public function firstAction () {
  4. if (login_is_conf ($this->view->conf) && !is_logged ()) {
  5. Error::error (
  6. 403,
  7. array ('error' => array (Translate::t ('access_denied')))
  8. );
  9. }
  10. }
  11. public function categorizeAction () {
  12. $feedDAO = new FeedDAO ();
  13. $catDAO = new CategoryDAO ();
  14. $catDAO->checkDefault ();
  15. $defaultCategory = $catDAO->getDefault ();
  16. $defaultId = $defaultCategory->id ();
  17. if (Request::isPost ()) {
  18. $cats = Request::param ('categories', array ());
  19. $ids = Request::param ('ids', array ());
  20. $newCat = trim (Request::param ('new_category', ''));
  21. foreach ($cats as $key => $name) {
  22. if (strlen ($name) > 0) {
  23. $cat = new Category ($name);
  24. $values = array (
  25. 'name' => $cat->name (),
  26. 'color' => $cat->color ()
  27. );
  28. $catDAO->updateCategory ($ids[$key], $values);
  29. } elseif ($ids[$key] != $defaultId) {
  30. $feedDAO->changeCategory ($ids[$key], $defaultId);
  31. $catDAO->deleteCategory ($ids[$key]);
  32. }
  33. }
  34. if ($newCat != '') {
  35. $cat = new Category ($newCat);
  36. $values = array (
  37. 'id' => $cat->id (),
  38. 'name' => $cat->name (),
  39. 'color' => $cat->color ()
  40. );
  41. if ($catDAO->searchByName ($newCat) == false) {
  42. $catDAO->addCategory ($values);
  43. }
  44. }
  45. // notif
  46. $notif = array (
  47. 'type' => 'good',
  48. 'content' => Translate::t ('categories_updated')
  49. );
  50. Session::_param ('notification', $notif);
  51. Request::forward (array ('c' => 'configure', 'a' => 'categorize'), true);
  52. }
  53. $this->view->categories = $catDAO->listCategories ();
  54. $this->view->defaultCategory = $catDAO->getDefault ();
  55. View::prependTitle (Translate::t ('categories_management') . ' - ');
  56. }
  57. public function feedAction () {
  58. $catDAO = new CategoryDAO ();
  59. $this->view->categories = $catDAO->listCategories ();
  60. $feedDAO = new FeedDAO ();
  61. $this->view->feeds = $feedDAO->listFeeds ();
  62. $id = Request::param ('id');
  63. if ($id == false && !empty ($this->view->feeds)) {
  64. $id = current ($this->view->feeds)->id ();
  65. }
  66. $this->view->flux = false;
  67. if ($id != false) {
  68. $this->view->flux = $feedDAO->searchById ($id);
  69. if (!$this->view->flux) {
  70. Error::error (
  71. 404,
  72. array ('error' => array (Translate::t ('page_not_found')))
  73. );
  74. } else {
  75. $catDAO = new CategoryDAO ();
  76. $this->view->categories = $catDAO->listCategories ();
  77. if (Request::isPost () && $this->view->flux) {
  78. $name = Request::param ('name', '');
  79. $cat = Request::param ('category', 0);
  80. $path = Request::param ('path_entries', '');
  81. $priority = Request::param ('priority', 0);
  82. $user = Request::param ('http_user', '');
  83. $pass = Request::param ('http_pass', '');
  84. $httpAuth = '';
  85. if ($user != '' || $pass != '') {
  86. $httpAuth = $user . ':' . $pass;
  87. }
  88. $values = array (
  89. 'name' => $name,
  90. 'category' => $cat,
  91. 'pathEntries' => $path,
  92. 'priority' => $priority,
  93. 'httpAuth' => $httpAuth
  94. );
  95. if ($feedDAO->updateFeed ($id, $values)) {
  96. $this->view->flux->_category ($cat);
  97. $notif = array (
  98. 'type' => 'good',
  99. 'content' => Translate::t ('feed_updated')
  100. );
  101. } else {
  102. $notif = array (
  103. 'type' => 'bad',
  104. 'content' => Translate::t ('error_occurred_update')
  105. );
  106. }
  107. Session::_param ('notification', $notif);
  108. Request::forward (array ('c' => 'configure', 'a' => 'feed', 'params' => array ('id' => $id)), true);
  109. }
  110. View::prependTitle (Translate::t ('rss_feed_management') . ' - ' . $this->view->flux->name () . ' - ');
  111. }
  112. } else {
  113. View::prependTitle (Translate::t ('rss_feed_management') . ' - ');
  114. }
  115. }
  116. public function displayAction () {
  117. if (Request::isPost ()) {
  118. $language = Request::param ('language', 'en');
  119. $nb = Request::param ('posts_per_page', 10);
  120. $view = Request::param ('default_view', 'all');
  121. $display = Request::param ('display_posts', 'no');
  122. $lazyload = Request::param ('lazyload', 'yes');
  123. $sort = Request::param ('sort_order', 'low_to_high');
  124. $old = Request::param ('old_entries', 3);
  125. $mail = Request::param ('mail_login', false);
  126. $openArticle = Request::param ('mark_open_article', 'no');
  127. $openSite = Request::param ('mark_open_site', 'no');
  128. $openPage = Request::param ('mark_open_page', 'no');
  129. $urlShaarli = Request::param ('shaarli', '');
  130. $this->view->conf->_language ($language);
  131. $this->view->conf->_postsPerPage (intval ($nb));
  132. $this->view->conf->_defaultView ($view);
  133. $this->view->conf->_displayPosts ($display);
  134. $this->view->conf->_lazyload ($lazyload);
  135. $this->view->conf->_sortOrder ($sort);
  136. $this->view->conf->_oldEntries ($old);
  137. $this->view->conf->_mailLogin ($mail);
  138. $this->view->conf->_markWhen (array (
  139. 'article' => $openArticle,
  140. 'site' => $openSite,
  141. 'page' => $openPage,
  142. ));
  143. $this->view->conf->_urlShaarli ($urlShaarli);
  144. $values = array (
  145. 'language' => $this->view->conf->language (),
  146. 'posts_per_page' => $this->view->conf->postsPerPage (),
  147. 'default_view' => $this->view->conf->defaultView (),
  148. 'display_posts' => $this->view->conf->displayPosts (),
  149. 'lazyload' => $this->view->conf->lazyload (),
  150. 'sort_order' => $this->view->conf->sortOrder (),
  151. 'old_entries' => $this->view->conf->oldEntries (),
  152. 'mail_login' => $this->view->conf->mailLogin (),
  153. 'mark_when' => $this->view->conf->markWhen (),
  154. 'url_shaarli' => $this->view->conf->urlShaarli (),
  155. );
  156. $confDAO = new RSSConfigurationDAO ();
  157. $confDAO->update ($values);
  158. Session::_param ('conf', $this->view->conf);
  159. Session::_param ('mail', $this->view->conf->mailLogin ());
  160. Session::_param ('language', $this->view->conf->language ());
  161. Translate::reset ();
  162. // notif
  163. $notif = array (
  164. 'type' => 'good',
  165. 'content' => Translate::t ('configuration_updated')
  166. );
  167. Session::_param ('notification', $notif);
  168. Request::forward (array ('c' => 'configure', 'a' => 'display'), true);
  169. }
  170. View::prependTitle (Translate::t ('general_and_reading_management') . ' - ');
  171. }
  172. public function importExportAction () {
  173. $catDAO = new CategoryDAO ();
  174. $this->view->categories = $catDAO->listCategories ();
  175. $this->view->req = Request::param ('q');
  176. if ($this->view->req == 'export') {
  177. View::_title ('freshrss_feeds.opml');
  178. $this->view->_useLayout (false);
  179. header('Content-Type: text/xml; charset=utf-8');
  180. header('Content-disposition: attachment; filename=freshrss_feeds.opml');
  181. $feedDAO = new FeedDAO ();
  182. $catDAO = new CategoryDAO ();
  183. $list = array ();
  184. foreach ($catDAO->listCategories () as $key => $cat) {
  185. $list[$key]['name'] = $cat->name ();
  186. $list[$key]['feeds'] = $feedDAO->listByCategory ($cat->id ());
  187. }
  188. $this->view->categories = $list;
  189. } elseif ($this->view->req == 'import' && Request::isPost ()) {
  190. if ($_FILES['file']['error'] == 0) {
  191. // on parse le fichier OPML pour récupérer les catégories et les flux associés
  192. try {
  193. list ($categories, $feeds) = opml_import (
  194. file_get_contents ($_FILES['file']['tmp_name'])
  195. );
  196. // On redirige vers le controller feed qui va se charger d'insérer les flux en BDD
  197. // les flux sont mis au préalable dans des variables de Request
  198. Request::_param ('q', 'null');
  199. Request::_param ('categories', $categories);
  200. Request::_param ('feeds', $feeds);
  201. Request::forward (array ('c' => 'feed', 'a' => 'massiveImport'));
  202. } catch (OpmlException $e) {
  203. Log::record ($e->getMessage (), Log::ERROR);
  204. $notif = array (
  205. 'type' => 'bad',
  206. 'content' => Translate::t ('bad_opml_file')
  207. );
  208. Session::_param ('notification', $notif);
  209. Request::forward (array (
  210. 'c' => 'configure',
  211. 'a' => 'importExport'
  212. ), true);
  213. }
  214. }
  215. }
  216. $feedDAO = new FeedDAO ();
  217. $this->view->feeds = $feedDAO->listFeeds ();
  218. // au niveau de la vue, permet de ne pas voir un flux sélectionné dans la liste
  219. $this->view->flux = false;
  220. View::prependTitle (Translate::t ('import_export_opml') . ' - ');
  221. }
  222. public function shortcutAction () {
  223. $list_keys = array ('a', 'b', 'backspace', 'c', 'd', 'delete', 'down', 'e', 'end', 'enter',
  224. 'escape', 'f', 'g', 'h', 'i', 'insert', 'j', 'k', 'l', 'left',
  225. 'm', 'n', 'o', 'p', 'page_down', 'page_up', 'q', 'r', 'return', 'right',
  226. 's', 'space', 't', 'tab', 'u', 'up', 'v', 'w', 'x', 'y',
  227. 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8',
  228. '9', 'f1', 'f2', 'f3', 'f4', 'f5', 'f6', 'f7', 'f8', 'f9',
  229. 'f10', 'f11', 'f12');
  230. $this->view->list_keys = $list_keys;
  231. $list_names = array ('mark_read', 'mark_favorite', 'go_website', 'next_entry',
  232. 'prev_entry', 'next_page', 'prev_page');
  233. if (Request::isPost ()) {
  234. $shortcuts = Request::param ('shortcuts');
  235. $shortcuts_ok = array ();
  236. foreach ($shortcuts as $key => $value) {
  237. if (in_array ($key, $list_names)
  238. && in_array ($value, $list_keys)) {
  239. $shortcuts_ok[$key] = $value;
  240. }
  241. }
  242. $this->view->conf->_shortcuts ($shortcuts_ok);
  243. $values = array (
  244. 'shortcuts' => $this->view->conf->shortcuts ()
  245. );
  246. $confDAO = new RSSConfigurationDAO ();
  247. $confDAO->update ($values);
  248. Session::_param ('conf', $this->view->conf);
  249. // notif
  250. $notif = array (
  251. 'type' => 'good',
  252. 'content' => Translate::t ('shortcuts_updated')
  253. );
  254. Session::_param ('notification', $notif);
  255. Request::forward (array ('c' => 'configure', 'a' => 'shortcut'), true);
  256. }
  257. View::prependTitle (Translate::t ('shortcuts_management') . ' - ');
  258. }
  259. }