importExportController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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::error('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::error('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::error('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);
  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. // Then, articles are imported.
  317. $prepared_statement = $this->entryDAO->addEntryPrepare();
  318. $this->entryDAO->beginTransaction();
  319. foreach ($article_object['items'] as $item) {
  320. if (!isset($article_to_feed[$item['id']])) {
  321. // Related feed does not exist for this entry, do nothing.
  322. continue;
  323. }
  324. $feed_id = $article_to_feed[$item['id']];
  325. $author = isset($item['author']) ? $item['author'] : '';
  326. $key_content = ($google_compliant && !isset($item['content'])) ?
  327. 'summary' : 'content';
  328. $tags = $item['categories'];
  329. if ($google_compliant) {
  330. // Remove tags containing "/state/com.google" which are useless.
  331. $tags = array_filter($tags, function($var) {
  332. return strpos($var, '/state/com.google') === false;
  333. });
  334. }
  335. $entry = new FreshRSS_Entry(
  336. $feed_id, $item['id'], $item['title'], $author,
  337. $item[$key_content]['content'], $item['alternate'][0]['href'],
  338. $item['published'], $is_read, $starred
  339. );
  340. $entry->_id(min(time(), $entry->date(true)) . uSecString());
  341. $entry->_tags($tags);
  342. $entry = Minz_ExtensionManager::callHook('entry_before_insert', $entry);
  343. if (is_null($entry)) {
  344. // An extension has returned a null value, there is nothing to insert.
  345. continue;
  346. }
  347. $values = $entry->toArray();
  348. $id = $this->entryDAO->addEntry($values, $prepared_statement);
  349. if (!$error && ($id === false)) {
  350. $error = true;
  351. }
  352. }
  353. $this->entryDAO->commit();
  354. return $error;
  355. }
  356. /**
  357. * This method import a JSON-based feed (Google Reader format).
  358. *
  359. * @param array $origin represents a feed.
  360. * @param boolean $google_compliant takes care of some specific values if true.
  361. * @return FreshRSS_Feed if feed is in database at the end of the process,
  362. * else null.
  363. */
  364. private function addFeedJson($origin, $google_compliant) {
  365. $default_cat = $this->catDAO->getDefault();
  366. $return = null;
  367. $key = $google_compliant ? 'htmlUrl' : 'feedUrl';
  368. $url = $origin[$key];
  369. $name = $origin['title'];
  370. $website = $origin['htmlUrl'];
  371. try {
  372. // Create a Feed object and add it in database.
  373. $feed = new FreshRSS_Feed($url);
  374. $feed->_category($default_cat->id());
  375. $feed->_name($name);
  376. $feed->_website($website);
  377. // Call the extension hook
  378. $feed = Minz_ExtensionManager::callHook('feed_before_insert', $feed);
  379. if (!is_null($feed)) {
  380. // addFeedObject checks if feed is already in DB so nothing else to
  381. // check here.
  382. $id = $this->feedDAO->addFeedObject($feed);
  383. if ($id !== false) {
  384. $feed->_id($id);
  385. $return = $feed;
  386. }
  387. }
  388. } catch (FreshRSS_Feed_Exception $e) {
  389. Minz_Log::warning($e->getMessage());
  390. }
  391. return $return;
  392. }
  393. /**
  394. * This action handles export action.
  395. *
  396. * This action must be reached by a POST request.
  397. *
  398. * Parameters are:
  399. * - export_opml (default: false)
  400. * - export_starred (default: false)
  401. * - export_feeds (default: array()) a list of feed ids
  402. */
  403. public function exportAction() {
  404. if (!Minz_Request::isPost()) {
  405. Minz_Request::forward(array('c' => 'importExport', 'a' => 'index'), true);
  406. }
  407. $this->view->_useLayout(false);
  408. $export_opml = Minz_Request::param('export_opml', false);
  409. $export_starred = Minz_Request::param('export_starred', false);
  410. $export_feeds = Minz_Request::param('export_feeds', array());
  411. $export_files = array();
  412. if ($export_opml) {
  413. $export_files['feeds.opml'] = $this->generateOpml();
  414. }
  415. if ($export_starred) {
  416. $export_files['starred.json'] = $this->generateEntries('starred');
  417. }
  418. foreach ($export_feeds as $feed_id) {
  419. $feed = $this->feedDAO->searchById($feed_id);
  420. if ($feed) {
  421. $filename = 'feed_' . $feed->category() . '_'
  422. . $feed->id() . '.json';
  423. $export_files[$filename] = $this->generateEntries('feed', $feed);
  424. }
  425. }
  426. $nb_files = count($export_files);
  427. if ($nb_files > 1) {
  428. // If there are more than 1 file to export, we need a zip archive.
  429. try {
  430. $this->exportZip($export_files);
  431. } catch (Exception $e) {
  432. # Oops, there is no Zip extension!
  433. Minz_Request::bad(_t('feedback.import_export.export_no_zip_extension'),
  434. array('c' => 'importExport', 'a' => 'index'));
  435. }
  436. } elseif ($nb_files === 1) {
  437. // Only one file? Guess its type and export it.
  438. $filename = key($export_files);
  439. $type = $this->guessFileType($filename);
  440. $this->exportFile('freshrss_' . $filename, $export_files[$filename], $type);
  441. } else {
  442. // Nothing to do...
  443. Minz_Request::forward(array('c' => 'importExport', 'a' => 'index'), true);
  444. }
  445. }
  446. /**
  447. * This method returns the OPML file based on user subscriptions.
  448. *
  449. * @return string the OPML file content.
  450. */
  451. private function generateOpml() {
  452. $list = array();
  453. foreach ($this->catDAO->listCategories() as $key => $cat) {
  454. $list[$key]['name'] = $cat->name();
  455. $list[$key]['feeds'] = $this->feedDAO->listByCategory($cat->id());
  456. }
  457. $this->view->categories = $list;
  458. return $this->view->helperToString('export/opml');
  459. }
  460. /**
  461. * This method returns a JSON file content.
  462. *
  463. * @param string $type must be "starred" or "feed"
  464. * @param FreshRSS_Feed $feed feed of which we want to get entries.
  465. * @return string the JSON file content.
  466. */
  467. private function generateEntries($type, $feed = NULL) {
  468. $this->view->categories = $this->catDAO->listCategories();
  469. if ($type == 'starred') {
  470. $this->view->list_title = _t('sub.import_export.starred_list');
  471. $this->view->type = 'starred';
  472. $unread_fav = $this->entryDAO->countUnreadReadFavorites();
  473. $this->view->entries = $this->entryDAO->listWhere(
  474. 's', '', FreshRSS_Entry::STATE_ALL, 'ASC', $unread_fav['all']
  475. );
  476. } elseif ($type == 'feed' && !is_null($feed)) {
  477. $this->view->list_title = _t('sub.import_export.feed_list', $feed->name());
  478. $this->view->type = 'feed/' . $feed->id();
  479. $this->view->entries = $this->entryDAO->listWhere(
  480. 'f', $feed->id(), FreshRSS_Entry::STATE_ALL, 'ASC',
  481. FreshRSS_Context::$user_conf->posts_per_page
  482. );
  483. $this->view->feed = $feed;
  484. }
  485. return $this->view->helperToString('export/articles');
  486. }
  487. /**
  488. * This method zips a list of files and returns it by HTTP.
  489. *
  490. * @param array $files list of files where key is filename and value the content.
  491. * @throws Exception if Zip extension is not loaded.
  492. */
  493. private function exportZip($files) {
  494. if (!extension_loaded('zip')) {
  495. throw new Exception();
  496. }
  497. // From https://stackoverflow.com/questions/1061710/php-zip-files-on-the-fly
  498. $zip_file = tempnam('tmp', 'zip');
  499. $zip = new ZipArchive();
  500. $zip->open($zip_file, ZipArchive::OVERWRITE);
  501. foreach ($files as $filename => $content) {
  502. $zip->addFromString($filename, $content);
  503. }
  504. // Close and send to user
  505. $zip->close();
  506. header('Content-Type: application/zip');
  507. header('Content-Length: ' . filesize($zip_file));
  508. header('Content-Disposition: attachment; filename="freshrss_export.zip"');
  509. readfile($zip_file);
  510. unlink($zip_file);
  511. }
  512. /**
  513. * This method returns a single file (OPML or JSON) by HTTP.
  514. *
  515. * @param string $filename
  516. * @param string $content
  517. * @param string $type the file type (opml, json_feed or json_starred).
  518. * If equals to unknown, nothing happens.
  519. */
  520. private function exportFile($filename, $content, $type) {
  521. if ($type === 'unknown') {
  522. return;
  523. }
  524. $content_type = '';
  525. if ($type === 'opml') {
  526. $content_type = "text/opml";
  527. } elseif ($type === 'json_feed' || $type === 'json_starred') {
  528. $content_type = "text/json";
  529. }
  530. header('Content-Type: ' . $content_type . '; charset=utf-8');
  531. header('Content-disposition: attachment; filename=' . $filename);
  532. print($content);
  533. }
  534. }