importExportController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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('import_export') . ' · ');
  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('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('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('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('feeds_imported_with_errors') :
  103. _t('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. * @todo move into lib_rss.php
  113. */
  114. private function guessFileType($filename) {
  115. if (substr_compare($filename, '.zip', -4) === 0) {
  116. return 'zip';
  117. } elseif (substr_compare($filename, '.opml', -5) === 0 ||
  118. substr_compare($filename, '.xml', -4) === 0) {
  119. return 'opml';
  120. } elseif (substr_compare($filename, '.json', -5) === 0 &&
  121. strpos($filename, 'starred') !== false) {
  122. return 'json_starred';
  123. } elseif (substr_compare($filename, '.json', -5) === 0) {
  124. return 'json_feed';
  125. } else {
  126. return 'unknown';
  127. }
  128. }
  129. /**
  130. * This method parses and imports an OPML file.
  131. *
  132. * @param string $opml_file the OPML file content.
  133. * @return boolean true if an error occured, false else.
  134. */
  135. private function importOpml($opml_file) {
  136. $opml_array = array();
  137. try {
  138. $opml_array = libopml_parse_string($opml_file);
  139. } catch (LibOPML_Exception $e) {
  140. Minz_Log::warning($e->getMessage());
  141. return true;
  142. }
  143. $this->catDAO->checkDefault();
  144. return $this->addOpmlElements($opml_array['body']);
  145. }
  146. /**
  147. * This method imports an OPML file based on its body.
  148. *
  149. * @param array $opml_elements an OPML element (body or outline).
  150. * @param string $parent_cat the name of the parent category.
  151. * @return boolean true if an error occured, false else.
  152. */
  153. private function addOpmlElements($opml_elements, $parent_cat = null) {
  154. $error = false;
  155. $nb_feeds = count($this->feedDAO->listFeeds());
  156. $nb_cats = count($this->catDAO->listCategories(false));
  157. $limits = Minz_Configuration::limits();
  158. foreach ($opml_elements as $elt) {
  159. $is_error = false;
  160. if (isset($elt['xmlUrl'])) {
  161. // If xmlUrl exists, it means it is a feed
  162. if ($nb_feeds >= $limits['max_feeds']) {
  163. Minz_Log::warning(_t('sub.feeds.over_max',
  164. $limits['max_feeds']));
  165. $is_error = true;
  166. continue;
  167. }
  168. $is_error = $this->addFeedOpml($elt, $parent_cat);
  169. if (!$is_error) {
  170. $nb_feeds += 1;
  171. }
  172. } else {
  173. // No xmlUrl? It should be a category!
  174. $limit_reached = ($nb_cats >= $limits['max_categories']);
  175. if ($limit_reached) {
  176. Minz_Log::warning(_t('sub.categories.over_max',
  177. $limits['max_categories']));
  178. }
  179. $is_error = $this->addCategoryOpml($elt, $parent_cat, $limit_reached);
  180. if (!$is_error) {
  181. $nb_cats += 1;
  182. }
  183. }
  184. if (!$error && $is_error) {
  185. // oops: there is at least one error!
  186. $error = $is_error;
  187. }
  188. }
  189. return $error;
  190. }
  191. /**
  192. * This method imports an OPML feed element.
  193. *
  194. * @param array $feed_elt an OPML element (must be a feed element).
  195. * @param string $parent_cat the name of the parent category.
  196. * @return boolean true if an error occured, false else.
  197. */
  198. private function addFeedOpml($feed_elt, $parent_cat) {
  199. $default_cat = $this->catDAO->getDefault();
  200. if (is_null($parent_cat)) {
  201. // This feed has no parent category so we get the default one
  202. $parent_cat = $default_cat->name();
  203. }
  204. $cat = $this->catDAO->searchByName($parent_cat);
  205. if (is_null($cat)) {
  206. // If there is not $cat, it means parent category does not exist in
  207. // database.
  208. // If it happens, take the default category.
  209. $cat = $default_cat;
  210. }
  211. // We get different useful information
  212. $url = Minz_Helper::htmlspecialchars_utf8($feed_elt['xmlUrl']);
  213. $name = Minz_Helper::htmlspecialchars_utf8($feed_elt['text']);
  214. $website = '';
  215. if (isset($feed_elt['htmlUrl'])) {
  216. $website = Minz_Helper::htmlspecialchars_utf8($feed_elt['htmlUrl']);
  217. }
  218. $description = '';
  219. if (isset($feed_elt['description'])) {
  220. $description = Minz_Helper::htmlspecialchars_utf8($feed_elt['description']);
  221. }
  222. $error = false;
  223. try {
  224. // Create a Feed object and add it in DB
  225. $feed = new FreshRSS_Feed($url);
  226. $feed->_category($cat->id());
  227. $feed->_name($name);
  228. $feed->_website($website);
  229. $feed->_description($description);
  230. // addFeedObject checks if feed is already in DB so nothing else to
  231. // check here
  232. $id = $this->feedDAO->addFeedObject($feed);
  233. $error = ($id === false);
  234. } catch (FreshRSS_Feed_Exception $e) {
  235. Minz_Log::warning($e->getMessage());
  236. $error = true;
  237. }
  238. return $error;
  239. }
  240. /**
  241. * This method imports an OPML category element.
  242. *
  243. * @param array $cat_elt an OPML element (must be a category element).
  244. * @param string $parent_cat the name of the parent category.
  245. * @param boolean $cat_limit_reached indicates if category limit has been reached.
  246. * if yes, category is not added (but we try for feeds!)
  247. * @return boolean true if an error occured, false else.
  248. */
  249. private function addCategoryOpml($cat_elt, $parent_cat, $cat_limit_reached) {
  250. // Create a new Category object
  251. $cat = new FreshRSS_Category(Minz_Helper::htmlspecialchars_utf8($cat_elt['text']));
  252. $error = true;
  253. if (!$cat_limit_reached) {
  254. $id = $this->catDAO->addCategoryObject($cat);
  255. $error = ($id === false);
  256. }
  257. if (isset($cat_elt['@outlines'])) {
  258. // Our cat_elt contains more categories or more feeds, so we
  259. // add them recursively.
  260. // Note: FreshRSS does not support yet category arborescence
  261. $res = $this->addOpmlElements($cat_elt['@outlines'], $cat->name());
  262. if (!$error && $res) {
  263. $error = true;
  264. }
  265. }
  266. return $error;
  267. }
  268. /**
  269. * This method import a JSON-based file (Google Reader format).
  270. *
  271. * @param string $article_file the JSON file content.
  272. * @param boolean $starred true if articles from the file must be starred.
  273. * @return boolean true if an error occured, false else.
  274. */
  275. private function importJson($article_file, $starred = false) {
  276. $article_object = json_decode($article_file, true);
  277. if (is_null($article_object)) {
  278. Minz_Log::warning('Try to import a non-JSON file');
  279. return true;
  280. }
  281. $is_read = FreshRSS_Context::$conf->mark_when['reception'] ? 1 : 0;
  282. $google_compliant = strpos($article_object['id'], 'com.google') !== false;
  283. $error = false;
  284. $article_to_feed = array();
  285. $nb_feeds = count($this->feedDAO->listFeeds());
  286. $limits = Minz_Configuration::limits();
  287. // First, we check feeds of articles are in DB (and add them if needed).
  288. foreach ($article_object['items'] as $item) {
  289. $key = $google_compliant ? 'htmlUrl' : 'feedUrl';
  290. $feed = new FreshRSS_Feed($item['origin'][$key]);
  291. $feed = $this->feedDAO->searchByUrl($feed->url());
  292. if (is_null($feed)) {
  293. // Feed does not exist in DB,we should to try to add it.
  294. if ($nb_feeds >= $limits['max_feeds']) {
  295. // Oops, no more place!
  296. Minz_Log::warning(_t('sub.feeds.over_max', $limits['max_feeds']));
  297. } else {
  298. $feed = $this->addFeedJson($item['origin'], $google_compliant);
  299. }
  300. if (is_null($feed)) {
  301. // Still null? It means something went wrong.
  302. $error = true;
  303. } else {
  304. // Nice! Increase the counter.
  305. $nb_feeds += 1;
  306. }
  307. }
  308. if (!is_null($feed)) {
  309. $article_to_feed[$item['id']] = $feed->id();
  310. }
  311. }
  312. // Then, articles are imported.
  313. $prepared_statement = $this->entryDAO->addEntryPrepare();
  314. $this->entryDAO->beginTransaction();
  315. foreach ($article_object['items'] as $item) {
  316. if (!isset($article_to_feed[$item['id']])) {
  317. // Related feed does not exist for this entry, do nothing.
  318. continue;
  319. }
  320. $feed_id = $article_to_feed[$item['id']];
  321. $author = isset($item['author']) ? $item['author'] : '';
  322. $key_content = ($google_compliant && !isset($item['content'])) ?
  323. 'summary' : 'content';
  324. $tags = $item['categories'];
  325. if ($google_compliant) {
  326. // Remove tags containing "/state/com.google" which are useless.
  327. $tags = array_filter($tags, function($var) {
  328. return strpos($var, '/state/com.google') === false;
  329. });
  330. }
  331. $entry = new FreshRSS_Entry(
  332. $feed_id, $item['id'], $item['title'], $author,
  333. $item[$key_content]['content'], $item['alternate'][0]['href'],
  334. $item['published'], $is_read, $starred
  335. );
  336. $entry->_id(min(time(), $entry->date(true)) . uSecString());
  337. $entry->_tags($tags);
  338. $entry = Minz_ExtensionManager::callHook('entry_before_insert', $entry);
  339. if (is_null($entry)) {
  340. // An extension has returned a null value, there is nothing to insert.
  341. continue;
  342. }
  343. $values = $entry->toArray();
  344. $id = $this->entryDAO->addEntry($values, $prepared_statement);
  345. if (!$error && ($id === false)) {
  346. $error = true;
  347. }
  348. }
  349. $this->entryDAO->commit();
  350. return $error;
  351. }
  352. /**
  353. * This method import a JSON-based feed (Google Reader format).
  354. *
  355. * @param array $origin represents a feed.
  356. * @param boolean $google_compliant takes care of some specific values if true.
  357. * @return FreshRSS_Feed if feed is in database at the end of the process,
  358. * else null.
  359. */
  360. private function addFeedJson($origin, $google_compliant) {
  361. $default_cat = $this->catDAO->getDefault();
  362. $return = null;
  363. $key = $google_compliant ? 'htmlUrl' : 'feedUrl';
  364. $url = $origin[$key];
  365. $name = $origin['title'];
  366. $website = $origin['htmlUrl'];
  367. try {
  368. // Create a Feed object and add it in database.
  369. $feed = new FreshRSS_Feed($url);
  370. $feed->_category($default_cat->id());
  371. $feed->_name($name);
  372. $feed->_website($website);
  373. // addFeedObject checks if feed is already in DB so nothing else to
  374. // check here.
  375. $id = $this->feedDAO->addFeedObject($feed);
  376. if ($id !== false) {
  377. $feed->_id($id);
  378. $return = $feed;
  379. }
  380. } catch (FreshRSS_Feed_Exception $e) {
  381. Minz_Log::warning($e->getMessage());
  382. }
  383. return $return;
  384. }
  385. /**
  386. * This action handles export action.
  387. *
  388. * This action must be reached by a POST request.
  389. *
  390. * Parameters are:
  391. * - export_opml (default: false)
  392. * - export_starred (default: false)
  393. * - export_feeds (default: array()) a list of feed ids
  394. */
  395. public function exportAction() {
  396. if (!Minz_Request::isPost()) {
  397. Minz_Request::forward(array('c' => 'importExport', 'a' => 'index'), true);
  398. }
  399. $this->view->_useLayout(false);
  400. $export_opml = Minz_Request::param('export_opml', false);
  401. $export_starred = Minz_Request::param('export_starred', false);
  402. $export_feeds = Minz_Request::param('export_feeds', array());
  403. $export_files = array();
  404. if ($export_opml) {
  405. $export_files['feeds.opml'] = $this->generateOpml();
  406. }
  407. if ($export_starred) {
  408. $export_files['starred.json'] = $this->generateEntries('starred');
  409. }
  410. foreach ($export_feeds as $feed_id) {
  411. $feed = $this->feedDAO->searchById($feed_id);
  412. if ($feed) {
  413. $filename = 'feed_' . $feed->category() . '_'
  414. . $feed->id() . '.json';
  415. $export_files[$filename] = $this->generateEntries('feed', $feed);
  416. }
  417. }
  418. $nb_files = count($export_files);
  419. if ($nb_files > 1) {
  420. // If there are more than 1 file to export, we need a zip archive.
  421. try {
  422. $this->exportZip($export_files);
  423. } catch (Exception $e) {
  424. # Oops, there is no Zip extension!
  425. Minz_Request::bad(_t('export_no_zip_extension'),
  426. array('c' => 'importExport', 'a' => 'index'));
  427. }
  428. } elseif ($nb_files === 1) {
  429. // Only one file? Guess its type and export it.
  430. $filename = key($export_files);
  431. $type = $this->guessFileType($filename);
  432. $this->exportFile('freshrss_' . $filename, $export_files[$filename], $type);
  433. } else {
  434. // Nothing to do...
  435. Minz_Request::forward(array('c' => 'importExport', 'a' => 'index'), true);
  436. }
  437. }
  438. /**
  439. * This method returns the OPML file based on user subscriptions.
  440. *
  441. * @return string the OPML file content.
  442. */
  443. private function generateOpml() {
  444. $list = array();
  445. foreach ($this->catDAO->listCategories() as $key => $cat) {
  446. $list[$key]['name'] = $cat->name();
  447. $list[$key]['feeds'] = $this->feedDAO->listByCategory($cat->id());
  448. }
  449. $this->view->categories = $list;
  450. return $this->view->helperToString('export/opml');
  451. }
  452. /**
  453. * This method returns a JSON file content.
  454. *
  455. * @param string $type must be "starred" or "feed"
  456. * @param FreshRSS_Feed $feed feed of which we want to get entries.
  457. * @return string the JSON file content.
  458. */
  459. private function generateEntries($type, $feed = NULL) {
  460. $this->view->categories = $this->catDAO->listCategories();
  461. if ($type == 'starred') {
  462. $this->view->list_title = _t('starred_list');
  463. $this->view->type = 'starred';
  464. $unread_fav = $this->entryDAO->countUnreadReadFavorites();
  465. $this->view->entries = $this->entryDAO->listWhere(
  466. 's', '', FreshRSS_Entry::STATE_ALL, 'ASC', $unread_fav['all']
  467. );
  468. } elseif ($type == 'feed' && !is_null($feed)) {
  469. $this->view->list_title = _t('feed_list', $feed->name());
  470. $this->view->type = 'feed/' . $feed->id();
  471. $this->view->entries = $this->entryDAO->listWhere(
  472. 'f', $feed->id(), FreshRSS_Entry::STATE_ALL, 'ASC',
  473. FreshRSS_Context::$conf->posts_per_page
  474. );
  475. $this->view->feed = $feed;
  476. }
  477. return $this->view->helperToString('export/articles');
  478. }
  479. /**
  480. * This method zips a list of files and returns it by HTTP.
  481. *
  482. * @param array $files list of files where key is filename and value the content.
  483. * @throws Exception if Zip extension is not loaded.
  484. */
  485. private function exportZip($files) {
  486. if (!extension_loaded('zip')) {
  487. throw new Exception();
  488. }
  489. // From https://stackoverflow.com/questions/1061710/php-zip-files-on-the-fly
  490. $zip_file = tempnam('tmp', 'zip');
  491. $zip = new ZipArchive();
  492. $zip->open($zip_file, ZipArchive::OVERWRITE);
  493. foreach ($files as $filename => $content) {
  494. $zip->addFromString($filename, $content);
  495. }
  496. // Close and send to user
  497. $zip->close();
  498. header('Content-Type: application/zip');
  499. header('Content-Length: ' . filesize($zip_file));
  500. header('Content-Disposition: attachment; filename="freshrss_export.zip"');
  501. readfile($zip_file);
  502. unlink($zip_file);
  503. }
  504. /**
  505. * This method returns a single file (OPML or JSON) by HTTP.
  506. *
  507. * @param string $filename
  508. * @param string $content
  509. * @param string $type the file type (opml, json_feed or json_starred).
  510. * If equals to unknown, nothing happens.
  511. */
  512. private function exportFile($filename, $content, $type) {
  513. if ($type === 'unknown') {
  514. return;
  515. }
  516. $content_type = '';
  517. if ($type === 'opml') {
  518. $content_type = "text/opml";
  519. } elseif ($type === 'json_feed' || $type === 'json_starred') {
  520. $content_type = "text/json";
  521. }
  522. header('Content-Type: ' . $content_type . '; charset=utf-8');
  523. header('Content-disposition: attachment; filename=' . $filename);
  524. print($content);
  525. }
  526. }