diff options
| author | 2014-03-27 18:36:48 +0100 | |
|---|---|---|
| committer | 2014-03-27 18:36:48 +0100 | |
| commit | 51a70ede0299c7ec44431d0fe86616d8c427144e (patch) | |
| tree | 9d25dc6fdebd91ab80699f0b8d7fb91788a1ca4b /app/Controllers/importExportController.php | |
| parent | 72ae58d45534b4c8b49ea0ac33a9a9ec9df4bdb1 (diff) | |
| parent | c8aa451c768a3d4dfce3d19648f3c8420dedb74c (diff) | |
Merge branch '163-export' into dev
Diffstat (limited to 'app/Controllers/importExportController.php')
| -rw-r--r-- | app/Controllers/importExportController.php | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/app/Controllers/importExportController.php b/app/Controllers/importExportController.php new file mode 100644 index 000000000..f697f4c9e --- /dev/null +++ b/app/Controllers/importExportController.php @@ -0,0 +1,111 @@ +<?php + +class FreshRSS_importExport_Controller extends Minz_ActionController { + public function firstAction() { + if (!$this->view->loginOk) { + Minz_Error::error ( + 403, + array ('error' => array (Minz_Translate::t ('access_denied'))) + ); + } + + require_once(LIB_PATH . '/lib_opml.php'); + } + + public function indexAction() { + $catDAO = new FreshRSS_CategoryDAO (); + $this->view->categories = $catDAO->listCategories (); + + $feedDAO = new FreshRSS_FeedDAO (); + $this->view->feeds = $feedDAO->listFeeds (); + + // au niveau de la vue, permet de ne pas voir un flux sélectionné dans la liste + $this->view->flux = false; + + Minz_View::prependTitle (Minz_Translate::t ('import_export') . ' · '); + } + + public function importAction() { + if (Minz_Request::isPost() && $_FILES['file']['error'] == 0) { + invalidateHttpCache(); + // on parse le fichier OPML pour récupérer les catégories et les flux associés + try { + list ($categories, $feeds) = opml_import ( + file_get_contents ($_FILES['file']['tmp_name']) + ); + + // On redirige vers le controller feed qui va se charger d'insérer les flux en BDD + // les flux sont mis au préalable dans des variables de Request + Minz_Request::_param ('categories', $categories); + Minz_Request::_param ('feeds', $feeds); + Minz_Request::forward (array ('c' => 'feed', 'a' => 'massiveImport')); + } catch (FreshRSS_Opml_Exception $e) { + Minz_Log::record ($e->getMessage (), Minz_Log::WARNING); + + $notif = array ( + 'type' => 'bad', + 'content' => Minz_Translate::t ('bad_opml_file') + ); + Minz_Session::_param ('notification', $notif); + + Minz_Request::forward (array ( + 'c' => 'configure', + 'a' => 'importExport' + ), true); + } + } + } + + public function exportAction() { + if (Minz_Request::isPost()) { + $this->view->_useLayout (false); + + $export_opml = Minz_Request::param('export_opml', false); + $export_starred = Minz_Request::param('export_starred', false); + $export_all = Minz_Request::param('export_all', false); + + // code from https://stackoverflow.com/questions/1061710/php-zip-files-on-the-fly + $file = tempnam('tmp', 'zip'); + $zip = new ZipArchive(); + $zip->open($file, ZipArchive::OVERWRITE); + + // Stuff with content + if ($export_opml) { + $zip->addFromString('feeds.opml', $this->generate_opml()); + } + if ($export_starred) { + $zip->addFromString('starred.json', $this->generate_articles('starred')); + } + if ($export_all) { + $zip->addFromString('all.json', $this->generate_articles('all')); + } + + // Close and send to users + $zip->close(); + header('Content-Type: application/zip'); + header('Content-Length: ' . filesize($file)); + header('Content-Disposition: attachment; filename="freshrss_export.zip"'); + readfile($file); + unlink($file); + } + } + + private function generate_opml() { + $feedDAO = new FreshRSS_FeedDAO (); + $catDAO = new FreshRSS_CategoryDAO (); + + $list = array (); + foreach ($catDAO->listCategories () as $key => $cat) { + $list[$key]['name'] = $cat->name (); + $list[$key]['feeds'] = $feedDAO->listByCategory ($cat->id ()); + } + + $this->view->categories = $list; + return $this->view->helperToString('export/opml'); + } + + private function generate_articles($type) { + // TODO: we should get articles according to $type + return $this->view->helperToString('export/articles'); + } +} |
