importExportController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. $this->catDAO = new FreshRSS_CategoryDAO();
  12. $this->entryDAO = new FreshRSS_EntryDAO();
  13. $this->feedDAO = new FreshRSS_FeedDAO();
  14. }
  15. public function indexAction() {
  16. $this->view->categories = $this->catDAO->listCategories();
  17. $this->view->feeds = $this->feedDAO->listFeeds();
  18. // au niveau de la vue, permet de ne pas voir un flux sélectionné dans la liste
  19. $this->view->flux = false;
  20. Minz_View::prependTitle(Minz_Translate::t('import_export') . ' · ');
  21. }
  22. public function importAction() {
  23. if (Minz_Request::isPost() && $_FILES['file']['error'] == 0) {
  24. @set_time_limit(300);
  25. $file = $_FILES['file'];
  26. $type_file = $this->guessFileType($file['name']);
  27. $list_files = array(
  28. 'opml' => array(),
  29. 'json_starred' => array(),
  30. 'json_feed' => array()
  31. );
  32. // We try to list all files according to their type
  33. // A zip file is first opened and then its files are listed
  34. $list = array();
  35. if ($type_file === 'zip') {
  36. $zip = zip_open($file['tmp_name']);
  37. while (($zipfile = zip_read($zip)) !== false) {
  38. $type_zipfile = $this->guessFileType(zip_entry_name($zipfile));
  39. if ($type_file !== 'unknown') {
  40. $list_files[$type_zipfile][] = zip_entry_read(
  41. $zipfile,
  42. zip_entry_filesize($zipfile)
  43. );
  44. }
  45. }
  46. zip_close($zip);
  47. } elseif ($type_file !== 'unknown') {
  48. $list_files[$type_file][] = file_get_contents($file['tmp_name']);
  49. }
  50. // Import different files.
  51. // OPML first(so categories and feeds are imported)
  52. // Starred articles then so the "favourite" status is already set
  53. // And finally all other files.
  54. $error = false;
  55. foreach ($list_files['opml'] as $opml_file) {
  56. $error = $this->importOpml($opml_file);
  57. }
  58. foreach ($list_files['json_starred'] as $article_file) {
  59. $error = $this->importArticles($article_file, true);
  60. }
  61. foreach ($list_files['json_feed'] as $article_file) {
  62. $error = $this->importArticles($article_file);
  63. }
  64. // And finally, we get import status and redirect to the home page
  65. $notif = null;
  66. if ($error === true) {
  67. $notif = array(
  68. 'type' => 'good',
  69. 'content' => Minz_Translate::t('feeds_imported_with_errors')
  70. );
  71. } else {
  72. $notif = array(
  73. 'type' => 'good',
  74. 'content' => Minz_Translate::t('feeds_imported')
  75. );
  76. }
  77. Minz_Session::_param('notification', $notif);
  78. Minz_Session::_param('actualize_feeds', true);
  79. Minz_Request::forward(array(
  80. 'c' => 'index',
  81. 'a' => 'index'
  82. ), true);
  83. }
  84. // What are you doing? you have to call this controller
  85. // with a POST request!
  86. Minz_Request::forward(array(
  87. 'c' => 'importExport',
  88. 'a' => 'index'
  89. ));
  90. }
  91. private function guessFileType($filename) {
  92. // A *very* basic guess file type function. Only based on filename
  93. // That's could be improved but should be enough, at least for a first
  94. // implementation.
  95. // TODO: improve this function?
  96. if (substr_compare($filename, '.zip', -4) === 0) {
  97. return 'zip';
  98. } elseif (substr_compare($filename, '.opml', -5) === 0 ||
  99. substr_compare($filename, '.xml', -4) === 0) {
  100. return 'opml';
  101. } elseif (strcmp($filename, 'starred.json') === 0) {
  102. return 'json_starred';
  103. } elseif (substr_compare($filename, '.json', -5) === 0 &&
  104. strpos($filename, 'feed_') === 0) {
  105. return 'json_feed';
  106. } else {
  107. return 'unknown';
  108. }
  109. }
  110. private function importOpml($opml_file) {
  111. $opml_array = array();
  112. try {
  113. $opml_array = libopml_parse_string($opml_file);
  114. } catch (LibOPML_Exception $e) {
  115. Minz_Log::warning($e->getMessage());
  116. return true;
  117. }
  118. $this->catDAO->checkDefault();
  119. return $this->addOpmlElements($opml_array['body']);
  120. }
  121. private function addOpmlElements($opml_elements, $parent_cat = null) {
  122. $error = false;
  123. foreach ($opml_elements as $elt) {
  124. $res = false;
  125. if (isset($elt['xmlUrl'])) {
  126. $res = $this->addFeedOpml($elt, $parent_cat);
  127. } else {
  128. $res = $this->addCategoryOpml($elt, $parent_cat);
  129. }
  130. if (!$error && $res) {
  131. // oops: there is at least one error!
  132. $error = $res;
  133. }
  134. }
  135. return $error;
  136. }
  137. private function addFeedOpml($feed_elt, $parent_cat) {
  138. if (is_null($parent_cat)) {
  139. // This feed has no parent category so we get the default one
  140. $parent_cat = $this->catDAO->getDefault()->name();
  141. }
  142. $cat = $this->catDAO->searchByName($parent_cat);
  143. if (!$cat) {
  144. return true;
  145. }
  146. // We get different useful information
  147. $url = html_chars_utf8($feed_elt['xmlUrl']);
  148. $name = html_chars_utf8($feed_elt['text']);
  149. $website = '';
  150. if (isset($feed_elt['htmlUrl'])) {
  151. $website = html_chars_utf8($feed_elt['htmlUrl']);
  152. }
  153. $description = '';
  154. if (isset($feed_elt['description'])) {
  155. $description = html_chars_utf8($feed_elt['description']);
  156. }
  157. $error = false;
  158. try {
  159. // Create a Feed object and add it in DB
  160. $feed = new FreshRSS_Feed($url);
  161. $feed->_category($cat->id());
  162. $feed->_name($name);
  163. $feed->_website($website);
  164. $feed->_description($description);
  165. // addFeedObject checks if feed is already in DB so nothing else to
  166. // check here
  167. $id = $this->feedDAO->addFeedObject($feed);
  168. $error = ($id === false);
  169. } catch (FreshRSS_Feed_Exception $e) {
  170. Minz_Log::warning($e->getMessage());
  171. $error = true;
  172. }
  173. return $error;
  174. }
  175. private function addCategoryOpml($cat_elt, $parent_cat) {
  176. // Create a new Category object
  177. $cat = new FreshRSS_Category(html_chars_utf8($cat_elt['text']));
  178. $id = $this->catDAO->addCategoryObject($cat);
  179. $error = ($id === false);
  180. if (isset($cat_elt['@outlines'])) {
  181. // Our cat_elt contains more categories or more feeds, so we
  182. // add them recursively.
  183. // Note: FreshRSS does not support yet category arborescence
  184. $res = $this->addOpmlElements($cat_elt['@outlines'], $cat->name());
  185. if (!$error && $res) {
  186. $error = true;
  187. }
  188. }
  189. return $error;
  190. }
  191. private function importArticles($article_file, $starred = false) {
  192. $article_object = json_decode($article_file, true);
  193. if (is_null($article_object)) {
  194. Minz_Log::warning('Try to import a non-JSON file');
  195. return true;
  196. }
  197. $is_read = $this->view->conf->mark_when['reception'] ? 1 : 0;
  198. $google_compliant = (strpos($article_object['id'], 'com.google') !== false);
  199. $error = false;
  200. foreach ($article_object['items'] as $item) {
  201. $feed = $this->addFeedArticles($item['origin'], $google_compliant);
  202. if (is_null($feed)) {
  203. $error = true;
  204. continue;
  205. }
  206. $author = isset($item['author']) ? $item['author'] : '';
  207. $key_content = $google_compliant && !isset($item['content']) ? 'summary' : 'content';
  208. $tags = $item['categories'];
  209. if ($google_compliant) {
  210. $tags = array_filter($tags, function($var) {
  211. return strpos($var, '/state/com.google') === false;
  212. });
  213. }
  214. $entry = new FreshRSS_Entry(
  215. $feed->id(), $item['id'], $item['title'], $author,
  216. $item[$key_content]['content'], $item['alternate'][0]['href'],
  217. $item['published'], $is_read, $starred
  218. );
  219. $entry->_tags($tags);
  220. $id = $this->entryDAO->addEntryObject(
  221. $entry, $this->view->conf, $feed->keepHistory()
  222. );
  223. if (!$error && ($id === false)) {
  224. $error = true;
  225. }
  226. }
  227. return $error;
  228. }
  229. private function addFeedArticles($origin, $google_compliant) {
  230. $default_cat = $this->catDAO->getDefault();
  231. $return = null;
  232. $key = $google_compliant ? 'htmlUrl' : 'feedUrl';
  233. $url = $origin[$key];
  234. $name = $origin['title'];
  235. $website = $origin['htmlUrl'];
  236. $error = false;
  237. try {
  238. // Create a Feed object and add it in DB
  239. $feed = new FreshRSS_Feed($url);
  240. $feed->_category($default_cat->id());
  241. $feed->_name($name);
  242. $feed->_website($website);
  243. // addFeedObject checks if feed is already in DB so nothing else to
  244. // check here
  245. $id = $this->feedDAO->addFeedObject($feed);
  246. if ($id !== false) {
  247. $feed->_id($id);
  248. $return = $feed;
  249. }
  250. } catch (FreshRSS_Feed_Exception $e) {
  251. Minz_Log::warning($e->getMessage());
  252. }
  253. return $return;
  254. }
  255. public function exportAction() {
  256. if (Minz_Request::isPost()) {
  257. $this->view->_useLayout(false);
  258. $export_opml = Minz_Request::param('export_opml', false);
  259. $export_starred = Minz_Request::param('export_starred', false);
  260. $export_feeds = Minz_Request::param('export_feeds', false);
  261. // code from https://stackoverflow.com/questions/1061710/php-zip-files-on-the-fly
  262. $file = tempnam('tmp', 'zip');
  263. $zip = new ZipArchive();
  264. $zip->open($file, ZipArchive::OVERWRITE);
  265. // Stuff with content
  266. if ($export_opml) {
  267. $zip->addFromString('feeds.opml', $this->generateOpml());
  268. }
  269. if ($export_starred) {
  270. $zip->addFromString('starred.json', $this->generateArticles('starred'));
  271. }
  272. foreach ($export_feeds as $feed_id) {
  273. $feed = $this->feedDAO->searchById($feed_id);
  274. $zip->addFromString(
  275. 'feed_' . $feed->category() . '_' . $feed->id() . '.json',
  276. $this->generateArticles('feed', $feed)
  277. );
  278. }
  279. // Close and send to user
  280. $zip->close();
  281. header('Content-Type: application/zip');
  282. header('Content-Length: ' . filesize($file));
  283. header('Content-Disposition: attachment; filename="freshrss_export.zip"');
  284. readfile($file);
  285. unlink($file);
  286. }
  287. }
  288. private function generateOpml() {
  289. $list = array();
  290. foreach ($this->catDAO->listCategories() as $key => $cat) {
  291. $list[$key]['name'] = $cat->name();
  292. $list[$key]['feeds'] = $this->feedDAO->listByCategory($cat->id());
  293. }
  294. $this->view->categories = $list;
  295. return $this->view->helperToString('export/opml');
  296. }
  297. private function generateArticles($type, $feed = NULL) {
  298. $this->view->categories = $this->catDAO->listCategories();
  299. if ($type == 'starred') {
  300. $this->view->list_title = Minz_Translate::t("starred_list");
  301. $this->view->type = 'starred';
  302. $this->view->entries = $this->entryDAO->listWhere(
  303. 's', '', 'all', 'ASC',
  304. $this->entryDAO->countUnreadReadFavorites()['all']
  305. );
  306. } elseif ($type == 'feed' && !is_null($feed)) {
  307. $this->view->list_title = Minz_Translate::t("feed_list", $feed->name());
  308. $this->view->type = 'feed/' . $feed->id();
  309. $this->view->entries = $this->entryDAO->listWhere(
  310. 'f', $feed->id(), 'all', 'ASC',
  311. $this->view->conf->posts_per_page
  312. );
  313. $this->view->feed = $feed;
  314. }
  315. return $this->view->helperToString('export/articles');
  316. }
  317. }