importExportController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. class FreshRSS_importExport_Controller extends Minz_ActionController {
  3. public function firstAction() {
  4. if (!$this->view->loginOk) {
  5. Minz_Error::error (
  6. 403,
  7. array ('error' => array (Minz_Translate::t ('access_denied')))
  8. );
  9. }
  10. require_once(LIB_PATH . '/lib_opml.php');
  11. }
  12. public function indexAction() {
  13. $catDAO = new FreshRSS_CategoryDAO();
  14. $this->view->categories = $catDAO->listCategories();
  15. $feedDAO = new FreshRSS_FeedDAO();
  16. $this->view->feeds = $feedDAO->listFeeds();
  17. // au niveau de la vue, permet de ne pas voir un flux sélectionné dans la liste
  18. $this->view->flux = false;
  19. Minz_View::prependTitle(Minz_Translate::t('import_export') . ' · ');
  20. }
  21. public function importAction() {
  22. if (Minz_Request::isPost() && $_FILES['file']['error'] == 0) {
  23. invalidateHttpCache();
  24. // on parse le fichier OPML pour récupérer les catégories et les flux associés
  25. try {
  26. list ($categories, $feeds) = opml_import (
  27. file_get_contents ($_FILES['file']['tmp_name'])
  28. );
  29. // On redirige vers le controller feed qui va se charger d'insérer les flux en BDD
  30. // les flux sont mis au préalable dans des variables de Request
  31. Minz_Request::_param ('categories', $categories);
  32. Minz_Request::_param ('feeds', $feeds);
  33. Minz_Request::forward (array ('c' => 'feed', 'a' => 'massiveImport'));
  34. } catch (FreshRSS_Opml_Exception $e) {
  35. Minz_Log::record ($e->getMessage (), Minz_Log::WARNING);
  36. $notif = array (
  37. 'type' => 'bad',
  38. 'content' => Minz_Translate::t ('bad_opml_file')
  39. );
  40. Minz_Session::_param ('notification', $notif);
  41. Minz_Request::forward (array (
  42. 'c' => 'configure',
  43. 'a' => 'importExport'
  44. ), true);
  45. }
  46. }
  47. }
  48. public function exportAction() {
  49. if (Minz_Request::isPost()) {
  50. $this->view->_useLayout (false);
  51. $export_opml = Minz_Request::param('export_opml', false);
  52. $export_starred = Minz_Request::param('export_starred', false);
  53. $export_feeds = Minz_Request::param('export_feeds', false);
  54. // code from https://stackoverflow.com/questions/1061710/php-zip-files-on-the-fly
  55. $file = tempnam('tmp', 'zip');
  56. $zip = new ZipArchive();
  57. $zip->open($file, ZipArchive::OVERWRITE);
  58. // Stuff with content
  59. if ($export_opml) {
  60. $zip->addFromString('feeds.opml', $this->generate_opml());
  61. }
  62. if ($export_starred) {
  63. $zip->addFromString('starred.json', $this->generate_articles('starred'));
  64. }
  65. $feedDAO = new FreshRSS_FeedDAO ();
  66. foreach ($export_feeds as $feed_id) {
  67. $feed = $feedDAO->searchById($feed_id);
  68. $zip->addFromString(
  69. 'feed_' . $feed->category() . '_' . $feed->id() . '.json',
  70. $this->generate_articles('feed', $feed)
  71. );
  72. }
  73. // Close and send to user
  74. $zip->close();
  75. header('Content-Type: application/zip');
  76. header('Content-Length: ' . filesize($file));
  77. header('Content-Disposition: attachment; filename="freshrss_export.zip"');
  78. readfile($file);
  79. unlink($file);
  80. }
  81. }
  82. private function generate_opml() {
  83. $feedDAO = new FreshRSS_FeedDAO ();
  84. $catDAO = new FreshRSS_CategoryDAO ();
  85. $list = array ();
  86. foreach ($catDAO->listCategories () as $key => $cat) {
  87. $list[$key]['name'] = $cat->name ();
  88. $list[$key]['feeds'] = $feedDAO->listByCategory ($cat->id ());
  89. }
  90. $this->view->categories = $list;
  91. return $this->view->helperToString('export/opml');
  92. }
  93. private function generate_articles($type, $feed = NULL) {
  94. $entryDAO = new FreshRSS_EntryDAO();
  95. $catDAO = new FreshRSS_CategoryDAO();
  96. $this->view->categories = $catDAO->listCategories();
  97. if ($type == 'starred') {
  98. $this->view->list_title = Minz_Translate::t("starred_list");
  99. $this->view->type = 'starred';
  100. $this->view->entries = $entryDAO->listWhere(
  101. 's', '', 'all', 'ASC',
  102. $entryDAO->countUnreadReadFavorites()['all']
  103. );
  104. } elseif ($type == 'feed' && !is_null($feed)) {
  105. $this->view->list_title = Minz_Translate::t("feed_list", $feed->name());
  106. $this->view->type = 'feed/' . $feed->id();
  107. $this->view->entries = $entryDAO->listWhere(
  108. 'f', $feed->id(), 'all', 'ASC',
  109. $this->view->conf->posts_per_page
  110. );
  111. $this->view->feed = $feed;
  112. }
  113. return $this->view->helperToString('export/articles');
  114. }
  115. }