importExportController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. <?php
  2. /**
  3. * Controller to handle every import and export actions.
  4. */
  5. class FreshRSS_importExport_Controller extends Minz_ActionController {
  6. /**
  7. * This action is called before every other action in that class. It is
  8. * the common boiler plate for every action. It is triggered by the
  9. * underlying framework.
  10. */
  11. public function firstAction() {
  12. if (!FreshRSS_Auth::hasAccess()) {
  13. Minz_Error::error(403);
  14. }
  15. require_once(LIB_PATH . '/lib_opml.php');
  16. $this->catDAO = new FreshRSS_CategoryDAO();
  17. $this->entryDAO = FreshRSS_Factory::createEntryDao();
  18. $this->feedDAO = FreshRSS_Factory::createFeedDao();
  19. }
  20. /**
  21. * This action displays the main page for import / export system.
  22. */
  23. public function indexAction() {
  24. $this->view->feeds = $this->feedDAO->listFeeds();
  25. Minz_View::prependTitle(_t('sub.import_export.title') . ' · ');
  26. }
  27. /**
  28. * This action handles import action.
  29. *
  30. * It must be reached by a POST request.
  31. *
  32. * Parameter is:
  33. * - file (default: nothing!)
  34. * Available file types are: zip, json or xml.
  35. */
  36. public function importAction() {
  37. if (!Minz_Request::isPost()) {
  38. Minz_Request::forward(array('c' => 'importExport', 'a' => 'index'), true);
  39. }
  40. $file = $_FILES['file'];
  41. $status_file = $file['error'];
  42. if ($status_file !== 0) {
  43. Minz_Log::warning('File cannot be uploaded. Error code: ' . $status_file);
  44. Minz_Request::bad(_t('feedback.import_export.file_cannot_be_uploaded'),
  45. array('c' => 'importExport', 'a' => 'index'));
  46. }
  47. @set_time_limit(300);
  48. $type_file = $this->guessFileType($file['name']);
  49. $list_files = array(
  50. 'opml' => array(),
  51. 'json_starred' => array(),
  52. 'json_feed' => array()
  53. );
  54. // We try to list all files according to their type
  55. $list = array();
  56. if ($type_file === 'zip' && extension_loaded('zip')) {
  57. $zip = zip_open($file['tmp_name']);
  58. if (!is_resource($zip)) {
  59. // zip_open cannot open file: something is wrong
  60. Minz_Log::warning('Zip archive cannot be imported. Error code: ' . $zip);
  61. Minz_Request::bad(_t('feedback.import_export.zip_error'),
  62. array('c' => 'importExport', 'a' => 'index'));
  63. }
  64. while (($zipfile = zip_read($zip)) !== false) {
  65. if (!is_resource($zipfile)) {
  66. // zip_entry() can also return an error code!
  67. Minz_Log::warning('Zip file cannot be imported. Error code: ' . $zipfile);
  68. } else {
  69. $type_zipfile = $this->guessFileType(zip_entry_name($zipfile));
  70. if ($type_file !== 'unknown') {
  71. $list_files[$type_zipfile][] = zip_entry_read(
  72. $zipfile,
  73. zip_entry_filesize($zipfile)
  74. );
  75. }
  76. }
  77. }
  78. zip_close($zip);
  79. } elseif ($type_file === 'zip') {
  80. // Zip extension is not loaded
  81. Minz_Request::bad(_t('feedback.import_export.no_zip_extension'),
  82. array('c' => 'importExport', 'a' => 'index'));
  83. } elseif ($type_file !== 'unknown') {
  84. $list_files[$type_file][] = file_get_contents($file['tmp_name']);
  85. }
  86. // Import file contents.
  87. // OPML first(so categories and feeds are imported)
  88. // Starred articles then so the "favourite" status is already set
  89. // And finally all other files.
  90. $error = false;
  91. foreach ($list_files['opml'] as $opml_file) {
  92. $error = $this->importOpml($opml_file);
  93. }
  94. foreach ($list_files['json_starred'] as $article_file) {
  95. $error = $this->importJson($article_file, true);
  96. }
  97. foreach ($list_files['json_feed'] as $article_file) {
  98. $error = $this->importJson($article_file);
  99. }
  100. // And finally, we get import status and redirect to the home page
  101. Minz_Session::_param('actualize_feeds', true);
  102. $content_notif = $error === true ? _t('feedback.import_export.feeds_imported_with_errors') :
  103. _t('feedback.import_export.feeds_imported');
  104. Minz_Request::good($content_notif);
  105. }
  106. /**
  107. * This method tries to guess the file type based on its name.
  108. *
  109. * Itis a *very* basic guess file type function. Only based on filename.
  110. * That's could be improved but should be enough for what we have to do.
  111. */
  112. private function guessFileType($filename) {
  113. if (substr_compare($filename, '.zip', -4) === 0) {
  114. return 'zip';
  115. } elseif (substr_compare($filename, '.opml', -5) === 0 ||
  116. substr_compare($filename, '.xml', -4) === 0) {
  117. return 'opml';
  118. } elseif (substr_compare($filename, '.json', -5) === 0 &&
  119. strpos($filename, 'starred') !== false) {
  120. return 'json_starred';
  121. } elseif (substr_compare($filename, '.json', -5) === 0) {
  122. return 'json_feed';
  123. } else {
  124. return 'unknown';
  125. }
  126. }
  127. /**
  128. * This method parses and imports an OPML file.
  129. *
  130. * @param string $opml_file the OPML file content.
  131. * @return boolean true if an error occured, false else.
  132. */
  133. private function importOpml($opml_file) {
  134. $opml_array = array();
  135. try {
  136. $opml_array = libopml_parse_string($opml_file, false);
  137. } catch (LibOPML_Exception $e) {
  138. Minz_Log::warning($e->getMessage());
  139. return true;
  140. }
  141. $this->catDAO->checkDefault();
  142. return $this->addOpmlElements($opml_array['body']);
  143. }
  144. /**
  145. * This method imports an OPML file based on its body.
  146. *
  147. * @param array $opml_elements an OPML element (body or outline).
  148. * @param string $parent_cat the name of the parent category.
  149. * @return boolean true if an error occured, false else.
  150. */
  151. private function addOpmlElements($opml_elements, $parent_cat = null) {
  152. $error = false;
  153. $nb_feeds = count($this->feedDAO->listFeeds());
  154. $nb_cats = count($this->catDAO->listCategories(false));
  155. $limits = FreshRSS_Context::$system_conf->limits;
  156. foreach ($opml_elements as $elt) {
  157. $is_error = false;
  158. if (isset($elt['xmlUrl'])) {
  159. // If xmlUrl exists, it means it is a feed
  160. if ($nb_feeds >= $limits['max_feeds']) {
  161. Minz_Log::warning(_t('feedback.sub.feed.over_max',
  162. $limits['max_feeds']));
  163. $is_error = true;
  164. continue;
  165. }
  166. $is_error = $this->addFeedOpml($elt, $parent_cat);
  167. if (!$is_error) {
  168. $nb_feeds += 1;
  169. }
  170. } else {
  171. // No xmlUrl? It should be a category!
  172. $limit_reached = ($nb_cats >= $limits['max_categories']);
  173. if ($limit_reached) {
  174. Minz_Log::warning(_t('feedback.sub.category.over_max',
  175. $limits['max_categories']));
  176. }
  177. $is_error = $this->addCategoryOpml($elt, $parent_cat, $limit_reached);
  178. if (!$is_error) {
  179. $nb_cats += 1;
  180. }
  181. }
  182. if (!$error && $is_error) {
  183. // oops: there is at least one error!
  184. $error = $is_error;
  185. }
  186. }
  187. return $error;
  188. }
  189. /**
  190. * This method imports an OPML feed element.
  191. *
  192. * @param array $feed_elt an OPML element (must be a feed element).
  193. * @param string $parent_cat the name of the parent category.
  194. * @return boolean true if an error occured, false else.
  195. */
  196. private function addFeedOpml($feed_elt, $parent_cat) {
  197. $default_cat = $this->catDAO->getDefault();
  198. if (is_null($parent_cat)) {
  199. // This feed has no parent category so we get the default one
  200. $parent_cat = $default_cat->name();
  201. }
  202. $cat = $this->catDAO->searchByName($parent_cat);
  203. if (is_null($cat)) {
  204. // If there is not $cat, it means parent category does not exist in
  205. // database.
  206. // If it happens, take the default category.
  207. $cat = $default_cat;
  208. }
  209. // We get different useful information
  210. $url = Minz_Helper::htmlspecialchars_utf8($feed_elt['xmlUrl']);
  211. $name = Minz_Helper::htmlspecialchars_utf8($feed_elt['text']);
  212. $website = '';
  213. if (isset($feed_elt['htmlUrl'])) {
  214. $website = Minz_Helper::htmlspecialchars_utf8($feed_elt['htmlUrl']);
  215. }
  216. $description = '';
  217. if (isset($feed_elt['description'])) {
  218. $description = Minz_Helper::htmlspecialchars_utf8($feed_elt['description']);
  219. }
  220. $error = false;
  221. try {
  222. // Create a Feed object and add it in DB
  223. $feed = new FreshRSS_Feed($url);
  224. $feed->_category($cat->id());
  225. $feed->_name($name);
  226. $feed->_website($website);
  227. $feed->_description($description);
  228. // Call the extension hook
  229. $feed = Minz_ExtensionManager::callHook('feed_before_insert', $feed);
  230. if (!is_null($feed)) {
  231. // addFeedObject checks if feed is already in DB so nothing else to
  232. // check here
  233. $id = $this->feedDAO->addFeedObject($feed);
  234. $error = ($id === false);
  235. } else {
  236. $error = true;
  237. }
  238. } catch (FreshRSS_Feed_Exception $e) {
  239. Minz_Log::warning($e->getMessage());
  240. $error = true;
  241. }
  242. return $error;
  243. }
  244. /**
  245. * This method imports an OPML category element.
  246. *
  247. * @param array $cat_elt an OPML element (must be a category element).
  248. * @param string $parent_cat the name of the parent category.
  249. * @param boolean $cat_limit_reached indicates if category limit has been reached.
  250. * if yes, category is not added (but we try for feeds!)
  251. * @return boolean true if an error occured, false else.
  252. */
  253. private function addCategoryOpml($cat_elt, $parent_cat, $cat_limit_reached) {
  254. // Create a new Category object
  255. $cat = new FreshRSS_Category(Minz_Helper::htmlspecialchars_utf8($cat_elt['text']));
  256. $error = true;
  257. if (!$cat_limit_reached) {
  258. $id = $this->catDAO->addCategoryObject($cat);
  259. $error = ($id === false);
  260. }
  261. if (isset($cat_elt['@outlines'])) {
  262. // Our cat_elt contains more categories or more feeds, so we
  263. // add them recursively.
  264. // Note: FreshRSS does not support yet category arborescence
  265. $res = $this->addOpmlElements($cat_elt['@outlines'], $cat->name());
  266. if (!$error && $res) {
  267. $error = true;
  268. }
  269. }
  270. return $error;
  271. }
  272. /**
  273. * This method import a JSON-based file (Google Reader format).
  274. *
  275. * @param string $article_file the JSON file content.
  276. * @param boolean $starred true if articles from the file must be starred.
  277. * @return boolean true if an error occured, false else.
  278. */
  279. private function importJson($article_file, $starred = false) {
  280. $article_object = json_decode($article_file, true);
  281. if (is_null($article_object)) {
  282. Minz_Log::warning('Try to import a non-JSON file');
  283. return true;
  284. }
  285. $is_read = FreshRSS_Context::$user_conf->mark_when['reception'] ? 1 : 0;
  286. $google_compliant = strpos($article_object['id'], 'com.google') !== false;
  287. $error = false;
  288. $article_to_feed = array();
  289. $nb_feeds = count($this->feedDAO->listFeeds());
  290. $limits = FreshRSS_Context::$system_conf->limits;
  291. // First, we check feeds of articles are in DB (and add them if needed).
  292. foreach ($article_object['items'] as $item) {
  293. $key = $google_compliant ? 'htmlUrl' : 'feedUrl';
  294. $feed = new FreshRSS_Feed($item['origin'][$key]);
  295. $feed = $this->feedDAO->searchByUrl($feed->url());
  296. if (is_null($feed)) {
  297. // Feed does not exist in DB,we should to try to add it.
  298. if ($nb_feeds >= $limits['max_feeds']) {
  299. // Oops, no more place!
  300. Minz_Log::warning(_t('feedback.sub.feed.over_max', $limits['max_feeds']));
  301. } else {
  302. $feed = $this->addFeedJson($item['origin'], $google_compliant);
  303. }
  304. if (is_null($feed)) {
  305. // Still null? It means something went wrong.
  306. $error = true;
  307. } else {
  308. // Nice! Increase the counter.
  309. $nb_feeds += 1;
  310. }
  311. }
  312. if (!is_null($feed)) {
  313. $article_to_feed[$item['id']] = $feed->id();
  314. }
  315. }
  316. $newGuids = array();
  317. foreach ($article_object['items'] as $item) {
  318. $newGuids[] = safe_ascii($item['id']);
  319. }
  320. // For this feed, check existing GUIDs already in database.
  321. $existingHashForGuids = $this->entryDAO->listHashForFeedGuids($feed->id(), $newGuids);
  322. unset($newGuids);
  323. // Then, articles are imported.
  324. $this->entryDAO->beginTransaction();
  325. foreach ($article_object['items'] as $item) {
  326. if (!isset($article_to_feed[$item['id']])) {
  327. // Related feed does not exist for this entry, do nothing.
  328. continue;
  329. }
  330. $feed_id = $article_to_feed[$item['id']];
  331. $author = isset($item['author']) ? $item['author'] : '';
  332. $key_content = ($google_compliant && !isset($item['content'])) ?
  333. 'summary' : 'content';
  334. $tags = $item['categories'];
  335. if ($google_compliant) {
  336. // Remove tags containing "/state/com.google" which are useless.
  337. $tags = array_filter($tags, function($var) {
  338. return strpos($var, '/state/com.google') === false;
  339. });
  340. }
  341. $entry = new FreshRSS_Entry(
  342. $feed_id, $item['id'], $item['title'], $author,
  343. $item[$key_content]['content'], $item['alternate'][0]['href'],
  344. $item['published'], $is_read, $starred
  345. );
  346. $entry->_id(min(time(), $entry->date(true)) . uSecString());
  347. $entry->_tags($tags);
  348. $entry = Minz_ExtensionManager::callHook('entry_before_insert', $entry);
  349. if (is_null($entry)) {
  350. // An extension has returned a null value, there is nothing to insert.
  351. continue;
  352. }
  353. $values = $entry->toArray();
  354. if (isset($existingHashForGuids[$entry->guid()])) {
  355. $id = $this->entryDAO->updateEntry($values);
  356. } else {
  357. $id = $this->entryDAO->addEntry($values);
  358. }
  359. if (!$error && ($id === false)) {
  360. $error = true;
  361. }
  362. }
  363. $this->entryDAO->commit();
  364. return $error;
  365. }
  366. /**
  367. * This method import a JSON-based feed (Google Reader format).
  368. *
  369. * @param array $origin represents a feed.
  370. * @param boolean $google_compliant takes care of some specific values if true.
  371. * @return FreshRSS_Feed if feed is in database at the end of the process,
  372. * else null.
  373. */
  374. private function addFeedJson($origin, $google_compliant) {
  375. $default_cat = $this->catDAO->getDefault();
  376. $return = null;
  377. $key = $google_compliant ? 'htmlUrl' : 'feedUrl';
  378. $url = $origin[$key];
  379. $name = $origin['title'];
  380. $website = $origin['htmlUrl'];
  381. try {
  382. // Create a Feed object and add it in database.
  383. $feed = new FreshRSS_Feed($url);
  384. $feed->_category($default_cat->id());
  385. $feed->_name($name);
  386. $feed->_website($website);
  387. // Call the extension hook
  388. $feed = Minz_ExtensionManager::callHook('feed_before_insert', $feed);
  389. if (!is_null($feed)) {
  390. // addFeedObject checks if feed is already in DB so nothing else to
  391. // check here.
  392. $id = $this->feedDAO->addFeedObject($feed);
  393. if ($id !== false) {
  394. $feed->_id($id);
  395. $return = $feed;
  396. }
  397. }
  398. } catch (FreshRSS_Feed_Exception $e) {
  399. Minz_Log::warning($e->getMessage());
  400. }
  401. return $return;
  402. }
  403. /**
  404. * This action handles export action.
  405. *
  406. * This action must be reached by a POST request.
  407. *
  408. * Parameters are:
  409. * - export_opml (default: false)
  410. * - export_starred (default: false)
  411. * - export_feeds (default: array()) a list of feed ids
  412. */
  413. public function exportAction() {
  414. if (!Minz_Request::isPost()) {
  415. Minz_Request::forward(array('c' => 'importExport', 'a' => 'index'), true);
  416. }
  417. $this->view->_useLayout(false);
  418. $export_opml = Minz_Request::param('export_opml', false);
  419. $export_starred = Minz_Request::param('export_starred', false);
  420. $export_feeds = Minz_Request::param('export_feeds', array());
  421. $day = date('Y-m-d');
  422. $export_files = array();
  423. if ($export_opml) {
  424. $export_files["feeds_${day}.opml.xml"] = $this->generateOpml();
  425. }
  426. if ($export_starred) {
  427. $export_files["starred_${day}.json"] = $this->generateEntries('starred');
  428. }
  429. foreach ($export_feeds as $feed_id) {
  430. $feed = $this->feedDAO->searchById($feed_id);
  431. if ($feed) {
  432. $filename = "feed_${day}_" . $feed->category() . '_'
  433. . $feed->id() . '.json';
  434. $export_files[$filename] = $this->generateEntries('feed', $feed);
  435. }
  436. }
  437. $nb_files = count($export_files);
  438. if ($nb_files > 1) {
  439. // If there are more than 1 file to export, we need a zip archive.
  440. try {
  441. $this->exportZip($export_files);
  442. } catch (Exception $e) {
  443. # Oops, there is no Zip extension!
  444. Minz_Request::bad(_t('feedback.import_export.export_no_zip_extension'),
  445. array('c' => 'importExport', 'a' => 'index'));
  446. }
  447. } elseif ($nb_files === 1) {
  448. // Only one file? Guess its type and export it.
  449. $filename = key($export_files);
  450. $type = $this->guessFileType($filename);
  451. $this->exportFile('freshrss_' . $filename, $export_files[$filename], $type);
  452. } else {
  453. // Nothing to do...
  454. Minz_Request::forward(array('c' => 'importExport', 'a' => 'index'), true);
  455. }
  456. }
  457. /**
  458. * This method returns the OPML file based on user subscriptions.
  459. *
  460. * @return string the OPML file content.
  461. */
  462. private function generateOpml() {
  463. $list = array();
  464. foreach ($this->catDAO->listCategories() as $key => $cat) {
  465. $list[$key]['name'] = $cat->name();
  466. $list[$key]['feeds'] = $this->feedDAO->listByCategory($cat->id());
  467. }
  468. $this->view->categories = $list;
  469. return $this->view->helperToString('export/opml');
  470. }
  471. /**
  472. * This method returns a JSON file content.
  473. *
  474. * @param string $type must be "starred" or "feed"
  475. * @param FreshRSS_Feed $feed feed of which we want to get entries.
  476. * @return string the JSON file content.
  477. */
  478. private function generateEntries($type, $feed = NULL) {
  479. $this->view->categories = $this->catDAO->listCategories();
  480. if ($type == 'starred') {
  481. $this->view->list_title = _t('sub.import_export.starred_list');
  482. $this->view->type = 'starred';
  483. $unread_fav = $this->entryDAO->countUnreadReadFavorites();
  484. $this->view->entries = $this->entryDAO->listWhere(
  485. 's', '', FreshRSS_Entry::STATE_ALL, 'ASC', $unread_fav['all']
  486. );
  487. } elseif ($type == 'feed' && !is_null($feed)) {
  488. $this->view->list_title = _t('sub.import_export.feed_list', $feed->name());
  489. $this->view->type = 'feed/' . $feed->id();
  490. $this->view->entries = $this->entryDAO->listWhere(
  491. 'f', $feed->id(), FreshRSS_Entry::STATE_ALL, 'ASC',
  492. FreshRSS_Context::$user_conf->posts_per_page
  493. );
  494. $this->view->feed = $feed;
  495. }
  496. return $this->view->helperToString('export/articles');
  497. }
  498. /**
  499. * This method zips a list of files and returns it by HTTP.
  500. *
  501. * @param array $files list of files where key is filename and value the content.
  502. * @throws Exception if Zip extension is not loaded.
  503. */
  504. private function exportZip($files) {
  505. if (!extension_loaded('zip')) {
  506. throw new Exception();
  507. }
  508. // From https://stackoverflow.com/questions/1061710/php-zip-files-on-the-fly
  509. $zip_file = tempnam('tmp', 'zip');
  510. $zip = new ZipArchive();
  511. $zip->open($zip_file, ZipArchive::OVERWRITE);
  512. foreach ($files as $filename => $content) {
  513. $zip->addFromString($filename, $content);
  514. }
  515. // Close and send to user
  516. $zip->close();
  517. header('Content-Type: application/zip');
  518. header('Content-Length: ' . filesize($zip_file));
  519. $day = date('Y-m-d');
  520. header('Content-Disposition: attachment; filename="freshrss_' . $day . '_export.zip"');
  521. readfile($zip_file);
  522. unlink($zip_file);
  523. }
  524. /**
  525. * This method returns a single file (OPML or JSON) by HTTP.
  526. *
  527. * @param string $filename
  528. * @param string $content
  529. * @param string $type the file type (opml, json_feed or json_starred).
  530. * If equals to unknown, nothing happens.
  531. */
  532. private function exportFile($filename, $content, $type) {
  533. if ($type === 'unknown') {
  534. return;
  535. }
  536. $content_type = '';
  537. if ($type === 'opml') {
  538. $content_type = 'application/xml';
  539. } elseif ($type === 'json_feed' || $type === 'json_starred') {
  540. $content_type = 'application/json';
  541. }
  542. header('Content-Type: ' . $content_type . '; charset=utf-8');
  543. header('Content-disposition: attachment; filename=' . $filename);
  544. print($content);
  545. }
  546. }