4
0

importExportController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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_all = Minz_Request::param('export_all', 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. if ($export_all) {
  66. $zip->addFromString('all.json', $this->generate_articles('all'));
  67. }
  68. // Close and send to users
  69. $zip->close();
  70. header('Content-Type: application/zip');
  71. header('Content-Length: ' . filesize($file));
  72. header('Content-Disposition: attachment; filename="freshrss_export.zip"');
  73. readfile($file);
  74. unlink($file);
  75. }
  76. }
  77. private function generate_opml() {
  78. $feedDAO = new FreshRSS_FeedDAO ();
  79. $catDAO = new FreshRSS_CategoryDAO ();
  80. $list = array ();
  81. foreach ($catDAO->listCategories () as $key => $cat) {
  82. $list[$key]['name'] = $cat->name ();
  83. $list[$key]['feeds'] = $feedDAO->listByCategory ($cat->id ());
  84. }
  85. $this->view->categories = $list;
  86. return $this->view->helperToString('export/opml');
  87. }
  88. private function generate_articles($type) {
  89. // TODO: we should get articles according to $type
  90. return $this->view->helperToString('export/articles');
  91. }
  92. }