importExportController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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. // addFeedObject checks if feed is already in DB so nothing else to
  229. // check here
  230. $id = $this->feedDAO->addFeedObject($feed);
  231. $error = ($id === false);
  232. } catch (FreshRSS_Feed_Exception $e) {
  233. Minz_Log::warning($e->getMessage());
  234. $error = true;
  235. }
  236. return $error;
  237. }
  238. /**
  239. * This method imports an OPML category element.
  240. *
  241. * @param array $cat_elt an OPML element (must be a category element).
  242. * @param string $parent_cat the name of the parent category.
  243. * @param boolean $cat_limit_reached indicates if category limit has been reached.
  244. * if yes, category is not added (but we try for feeds!)
  245. * @return boolean true if an error occured, false else.
  246. */
  247. private function addCategoryOpml($cat_elt, $parent_cat, $cat_limit_reached) {
  248. // Create a new Category object
  249. $cat = new FreshRSS_Category(Minz_Helper::htmlspecialchars_utf8($cat_elt['text']));
  250. $error = true;
  251. if (!$cat_limit_reached) {
  252. $id = $this->catDAO->addCategoryObject($cat);
  253. $error = ($id === false);
  254. }
  255. if (isset($cat_elt['@outlines'])) {
  256. // Our cat_elt contains more categories or more feeds, so we
  257. // add them recursively.
  258. // Note: FreshRSS does not support yet category arborescence
  259. $res = $this->addOpmlElements($cat_elt['@outlines'], $cat->name());
  260. if (!$error && $res) {
  261. $error = true;
  262. }
  263. }
  264. return $error;
  265. }
  266. /**
  267. * This method import a JSON-based file (Google Reader format).
  268. *
  269. * @param string $article_file the JSON file content.
  270. * @param boolean $starred true if articles from the file must be starred.
  271. * @return boolean true if an error occured, false else.
  272. */
  273. private function importJson($article_file, $starred = false) {
  274. $article_object = json_decode($article_file, true);
  275. if (is_null($article_object)) {
  276. Minz_Log::warning('Try to import a non-JSON file');
  277. return true;
  278. }
  279. $is_read = FreshRSS_Context::$user_conf->mark_when['reception'] ? 1 : 0;
  280. $google_compliant = strpos($article_object['id'], 'com.google') !== false;
  281. $error = false;
  282. $article_to_feed = array();
  283. $nb_feeds = count($this->feedDAO->listFeeds());
  284. $limits = FreshRSS_Context::$system_conf->limits;
  285. // First, we check feeds of articles are in DB (and add them if needed).
  286. foreach ($article_object['items'] as $item) {
  287. $key = $google_compliant ? 'htmlUrl' : 'feedUrl';
  288. $feed = new FreshRSS_Feed($item['origin'][$key]);
  289. $feed = $this->feedDAO->searchByUrl($feed->url());
  290. if (is_null($feed)) {
  291. // Feed does not exist in DB,we should to try to add it.
  292. if ($nb_feeds >= $limits['max_feeds']) {
  293. // Oops, no more place!
  294. Minz_Log::warning(_t('feedback.sub.feed.over_max', $limits['max_feeds']));
  295. } else {
  296. $feed = $this->addFeedJson($item['origin'], $google_compliant);
  297. }
  298. if (is_null($feed)) {
  299. // Still null? It means something went wrong.
  300. $error = true;
  301. } else {
  302. // Nice! Increase the counter.
  303. $nb_feeds += 1;
  304. }
  305. }
  306. if (!is_null($feed)) {
  307. $article_to_feed[$item['id']] = $feed->id();
  308. }
  309. }
  310. // Then, articles are imported.
  311. $prepared_statement = $this->entryDAO->addEntryPrepare();
  312. $this->entryDAO->beginTransaction();
  313. foreach ($article_object['items'] as $item) {
  314. if (!isset($article_to_feed[$item['id']])) {
  315. // Related feed does not exist for this entry, do nothing.
  316. continue;
  317. }
  318. $feed_id = $article_to_feed[$item['id']];
  319. $author = isset($item['author']) ? $item['author'] : '';
  320. $key_content = ($google_compliant && !isset($item['content'])) ?
  321. 'summary' : 'content';
  322. $tags = $item['categories'];
  323. if ($google_compliant) {
  324. // Remove tags containing "/state/com.google" which are useless.
  325. $tags = array_filter($tags, function($var) {
  326. return strpos($var, '/state/com.google') === false;
  327. });
  328. }
  329. $entry = new FreshRSS_Entry(
  330. $feed_id, $item['id'], $item['title'], $author,
  331. $item[$key_content]['content'], $item['alternate'][0]['href'],
  332. $item['published'], $is_read, $starred
  333. );
  334. $entry->_id(min(time(), $entry->date(true)) . uSecString());
  335. $entry->_tags($tags);
  336. $values = $entry->toArray();
  337. $id = $this->entryDAO->addEntry($values, $prepared_statement);
  338. if (!$error && ($id === false)) {
  339. $error = true;
  340. }
  341. }
  342. $this->entryDAO->commit();
  343. return $error;
  344. }
  345. /**
  346. * This method import a JSON-based feed (Google Reader format).
  347. *
  348. * @param array $origin represents a feed.
  349. * @param boolean $google_compliant takes care of some specific values if true.
  350. * @return FreshRSS_Feed if feed is in database at the end of the process,
  351. * else null.
  352. */
  353. private function addFeedJson($origin, $google_compliant) {
  354. $default_cat = $this->catDAO->getDefault();
  355. $return = null;
  356. $key = $google_compliant ? 'htmlUrl' : 'feedUrl';
  357. $url = $origin[$key];
  358. $name = $origin['title'];
  359. $website = $origin['htmlUrl'];
  360. try {
  361. // Create a Feed object and add it in database.
  362. $feed = new FreshRSS_Feed($url);
  363. $feed->_category($default_cat->id());
  364. $feed->_name($name);
  365. $feed->_website($website);
  366. // addFeedObject checks if feed is already in DB so nothing else to
  367. // check here.
  368. $id = $this->feedDAO->addFeedObject($feed);
  369. if ($id !== false) {
  370. $feed->_id($id);
  371. $return = $feed;
  372. }
  373. } catch (FreshRSS_Feed_Exception $e) {
  374. Minz_Log::warning($e->getMessage());
  375. }
  376. return $return;
  377. }
  378. /**
  379. * This action handles export action.
  380. *
  381. * This action must be reached by a POST request.
  382. *
  383. * Parameters are:
  384. * - export_opml (default: false)
  385. * - export_starred (default: false)
  386. * - export_feeds (default: array()) a list of feed ids
  387. */
  388. public function exportAction() {
  389. if (!Minz_Request::isPost()) {
  390. Minz_Request::forward(array('c' => 'importExport', 'a' => 'index'), true);
  391. }
  392. $this->view->_useLayout(false);
  393. $export_opml = Minz_Request::param('export_opml', false);
  394. $export_starred = Minz_Request::param('export_starred', false);
  395. $export_feeds = Minz_Request::param('export_feeds', array());
  396. $export_files = array();
  397. if ($export_opml) {
  398. $export_files['feeds.opml'] = $this->generateOpml();
  399. }
  400. if ($export_starred) {
  401. $export_files['starred.json'] = $this->generateEntries('starred');
  402. }
  403. foreach ($export_feeds as $feed_id) {
  404. $feed = $this->feedDAO->searchById($feed_id);
  405. if ($feed) {
  406. $filename = 'feed_' . $feed->category() . '_'
  407. . $feed->id() . '.json';
  408. $export_files[$filename] = $this->generateEntries('feed', $feed);
  409. }
  410. }
  411. $nb_files = count($export_files);
  412. if ($nb_files > 1) {
  413. // If there are more than 1 file to export, we need a zip archive.
  414. try {
  415. $this->exportZip($export_files);
  416. } catch (Exception $e) {
  417. # Oops, there is no Zip extension!
  418. Minz_Request::bad(_t('feedback.import_export.export_no_zip_extension'),
  419. array('c' => 'importExport', 'a' => 'index'));
  420. }
  421. } elseif ($nb_files === 1) {
  422. // Only one file? Guess its type and export it.
  423. $filename = key($export_files);
  424. $type = $this->guessFileType($filename);
  425. $this->exportFile('freshrss_' . $filename, $export_files[$filename], $type);
  426. } else {
  427. // Nothing to do...
  428. Minz_Request::forward(array('c' => 'importExport', 'a' => 'index'), true);
  429. }
  430. }
  431. /**
  432. * This method returns the OPML file based on user subscriptions.
  433. *
  434. * @return string the OPML file content.
  435. */
  436. private function generateOpml() {
  437. $list = array();
  438. foreach ($this->catDAO->listCategories() as $key => $cat) {
  439. $list[$key]['name'] = $cat->name();
  440. $list[$key]['feeds'] = $this->feedDAO->listByCategory($cat->id());
  441. }
  442. $this->view->categories = $list;
  443. return $this->view->helperToString('export/opml');
  444. }
  445. /**
  446. * This method returns a JSON file content.
  447. *
  448. * @param string $type must be "starred" or "feed"
  449. * @param FreshRSS_Feed $feed feed of which we want to get entries.
  450. * @return string the JSON file content.
  451. */
  452. private function generateEntries($type, $feed = NULL) {
  453. $this->view->categories = $this->catDAO->listCategories();
  454. if ($type == 'starred') {
  455. $this->view->list_title = _t('sub.import_export.starred_list');
  456. $this->view->type = 'starred';
  457. $unread_fav = $this->entryDAO->countUnreadReadFavorites();
  458. $this->view->entries = $this->entryDAO->listWhere(
  459. 's', '', FreshRSS_Entry::STATE_ALL, 'ASC', $unread_fav['all']
  460. );
  461. } elseif ($type == 'feed' && !is_null($feed)) {
  462. $this->view->list_title = _t('sub.import_export.feed_list', $feed->name());
  463. $this->view->type = 'feed/' . $feed->id();
  464. $this->view->entries = $this->entryDAO->listWhere(
  465. 'f', $feed->id(), FreshRSS_Entry::STATE_ALL, 'ASC',
  466. FreshRSS_Context::$user_conf->posts_per_page
  467. );
  468. $this->view->feed = $feed;
  469. }
  470. return $this->view->helperToString('export/articles');
  471. }
  472. /**
  473. * This method zips a list of files and returns it by HTTP.
  474. *
  475. * @param array $files list of files where key is filename and value the content.
  476. * @throws Exception if Zip extension is not loaded.
  477. */
  478. private function exportZip($files) {
  479. if (!extension_loaded('zip')) {
  480. throw new Exception();
  481. }
  482. // From https://stackoverflow.com/questions/1061710/php-zip-files-on-the-fly
  483. $zip_file = tempnam('tmp', 'zip');
  484. $zip = new ZipArchive();
  485. $zip->open($zip_file, ZipArchive::OVERWRITE);
  486. foreach ($files as $filename => $content) {
  487. $zip->addFromString($filename, $content);
  488. }
  489. // Close and send to user
  490. $zip->close();
  491. header('Content-Type: application/zip');
  492. header('Content-Length: ' . filesize($zip_file));
  493. header('Content-Disposition: attachment; filename="freshrss_export.zip"');
  494. readfile($zip_file);
  495. unlink($zip_file);
  496. }
  497. /**
  498. * This method returns a single file (OPML or JSON) by HTTP.
  499. *
  500. * @param string $filename
  501. * @param string $content
  502. * @param string $type the file type (opml, json_feed or json_starred).
  503. * If equals to unknown, nothing happens.
  504. */
  505. private function exportFile($filename, $content, $type) {
  506. if ($type === 'unknown') {
  507. return;
  508. }
  509. $content_type = '';
  510. if ($type === 'opml') {
  511. $content_type = "text/opml";
  512. } elseif ($type === 'json_feed' || $type === 'json_starred') {
  513. $content_type = "text/json";
  514. }
  515. header('Content-Type: ' . $content_type . '; charset=utf-8');
  516. header('Content-disposition: attachment; filename=' . $filename);
  517. print($content);
  518. }
  519. }