importExportController.php 17 KB

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