importExportController.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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. public function importFile($name, $path, $username = null) {
  28. require_once(LIB_PATH . '/lib_opml.php');
  29. $this->catDAO = new FreshRSS_CategoryDAO($username);
  30. $this->entryDAO = FreshRSS_Factory::createEntryDao($username);
  31. $this->feedDAO = FreshRSS_Factory::createFeedDao($username);
  32. $type_file = self::guessFileType($name);
  33. $list_files = array(
  34. 'opml' => array(),
  35. 'json_starred' => array(),
  36. 'json_feed' => array()
  37. );
  38. // We try to list all files according to their type
  39. $list = array();
  40. if ($type_file === 'zip' && extension_loaded('zip')) {
  41. $zip = zip_open($path);
  42. if (!is_resource($zip)) {
  43. // zip_open cannot open file: something is wrong
  44. throw new FreshRSS_Zip_Exception($zip);
  45. }
  46. while (($zipfile = zip_read($zip)) !== false) {
  47. if (!is_resource($zipfile)) {
  48. // zip_entry() can also return an error code!
  49. throw new FreshRSS_Zip_Exception($zipfile);
  50. } else {
  51. $type_zipfile = self::guessFileType(zip_entry_name($zipfile));
  52. if ($type_file !== 'unknown') {
  53. $list_files[$type_zipfile][] = zip_entry_read(
  54. $zipfile,
  55. zip_entry_filesize($zipfile)
  56. );
  57. }
  58. }
  59. }
  60. zip_close($zip);
  61. } elseif ($type_file === 'zip') {
  62. // ZIP extension is not loaded
  63. throw new FreshRSS_ZipMissing_Exception();
  64. } elseif ($type_file !== 'unknown') {
  65. $list_files[$type_file][] = file_get_contents($path);
  66. }
  67. // Import file contents.
  68. // OPML first(so categories and feeds are imported)
  69. // Starred articles then so the "favourite" status is already set
  70. // And finally all other files.
  71. $ok = true;
  72. foreach ($list_files['opml'] as $opml_file) {
  73. if (!$this->importOpml($opml_file)) {
  74. $ok = false;
  75. if (FreshRSS_Context::$isCli) {
  76. fwrite(STDERR, 'FreshRSS error during OPML import' . "\n");
  77. } else {
  78. Minz_Log::warning('Error during OPML import');
  79. }
  80. }
  81. }
  82. foreach ($list_files['json_starred'] as $article_file) {
  83. if (!$this->importJson($article_file, true)) {
  84. $ok = false;
  85. if (FreshRSS_Context::$isCli) {
  86. fwrite(STDERR, 'FreshRSS error during JSON stars import' . "\n");
  87. } else {
  88. Minz_Log::warning('Error during JSON stars import');
  89. }
  90. }
  91. }
  92. foreach ($list_files['json_feed'] as $article_file) {
  93. if (!$this->importJson($article_file)) {
  94. $ok = false;
  95. if (FreshRSS_Context::$isCli) {
  96. fwrite(STDERR, 'FreshRSS error during JSON feeds import' . "\n");
  97. } else {
  98. Minz_Log::warning('Error during JSON feeds import');
  99. }
  100. }
  101. }
  102. foreach ($list_files['ttrss_starred'] as $article_file) {
  103. $json = $this->ttrssXmlToJson($article_file);
  104. if (!$this->importJson($json, true)) {
  105. $ok = false;
  106. if (FreshRSS_Context::$isCli) {
  107. fwrite(STDERR, 'FreshRSS error during TT-RSS articles import' . "\n");
  108. } else {
  109. Minz_Log::warning('Error during TT-RSS articles import');
  110. }
  111. }
  112. }
  113. return $ok;
  114. }
  115. /**
  116. * This action handles import action.
  117. *
  118. * It must be reached by a POST request.
  119. *
  120. * Parameter is:
  121. * - file (default: nothing!)
  122. * Available file types are: zip, json or xml.
  123. */
  124. public function importAction() {
  125. if (!Minz_Request::isPost()) {
  126. Minz_Request::forward(array('c' => 'importExport', 'a' => 'index'), true);
  127. }
  128. $file = $_FILES['file'];
  129. $status_file = $file['error'];
  130. if ($status_file !== 0) {
  131. Minz_Log::warning('File cannot be uploaded. Error code: ' . $status_file);
  132. Minz_Request::bad(_t('feedback.import_export.file_cannot_be_uploaded'),
  133. array('c' => 'importExport', 'a' => 'index'));
  134. }
  135. @set_time_limit(300);
  136. $error = false;
  137. try {
  138. $error = !$this->importFile($file['name'], $file['tmp_name']);
  139. } catch (FreshRSS_ZipMissing_Exception $zme) {
  140. Minz_Request::bad(_t('feedback.import_export.no_zip_extension'),
  141. array('c' => 'importExport', 'a' => 'index'));
  142. } catch (FreshRSS_Zip_Exception $ze) {
  143. Minz_Log::warning('ZIP archive cannot be imported. Error code: ' . $ze->zipErrorCode());
  144. Minz_Request::bad(_t('feedback.import_export.zip_error'),
  145. array('c' => 'importExport', 'a' => 'index'));
  146. }
  147. // And finally, we get import status and redirect to the home page
  148. Minz_Session::_param('actualize_feeds', true);
  149. $content_notif = $error === true ? _t('feedback.import_export.feeds_imported_with_errors') : _t('feedback.import_export.feeds_imported');
  150. Minz_Request::good($content_notif);
  151. }
  152. /**
  153. * This method tries to guess the file type based on its name.
  154. *
  155. * Itis a *very* basic guess file type function. Only based on filename.
  156. * That's could be improved but should be enough for what we have to do.
  157. */
  158. private static function guessFileType($filename) {
  159. if (substr_compare($filename, '.zip', -4) === 0) {
  160. return 'zip';
  161. } elseif (substr_compare($filename, '.opml', -5) === 0) {
  162. return 'opml';
  163. } elseif (substr_compare($filename, '.json', -5) === 0) {
  164. if (strpos($filename, 'starred') !== false) {
  165. return 'json_starred';
  166. } else {
  167. return 'json_feed';
  168. }
  169. } elseif (substr_compare($filename, '.xml', -4) === 0) {
  170. if (preg_match('/Tiny|tt-?rss/i', $filename)) {
  171. return 'ttrss_starred';
  172. } else {
  173. return 'opml';
  174. }
  175. }
  176. return 'unknown';
  177. }
  178. /**
  179. * This method parses and imports an OPML file.
  180. *
  181. * @param string $opml_file the OPML file content.
  182. * @return boolean false if an error occured, true otherwise.
  183. */
  184. private function importOpml($opml_file) {
  185. $opml_array = array();
  186. try {
  187. $opml_array = libopml_parse_string($opml_file, false);
  188. } catch (LibOPML_Exception $e) {
  189. if (FreshRSS_Context::$isCli) {
  190. fwrite(STDERR, 'FreshRSS error during OPML parsing: ' . $e->getMessage() . "\n");
  191. } else {
  192. Minz_Log::warning($e->getMessage());
  193. }
  194. return false;
  195. }
  196. $this->catDAO->checkDefault();
  197. return $this->addOpmlElements($opml_array['body']);
  198. }
  199. /**
  200. * This method imports an OPML file based on its body.
  201. *
  202. * @param array $opml_elements an OPML element (body or outline).
  203. * @param string $parent_cat the name of the parent category.
  204. * @return boolean false if an error occured, true otherwise.
  205. */
  206. private function addOpmlElements($opml_elements, $parent_cat = null) {
  207. $ok = true;
  208. $nb_feeds = count($this->feedDAO->listFeeds());
  209. $nb_cats = count($this->catDAO->listCategories(false));
  210. $limits = FreshRSS_Context::$system_conf->limits;
  211. foreach ($opml_elements as $elt) {
  212. if (isset($elt['xmlUrl'])) {
  213. // If xmlUrl exists, it means it is a feed
  214. if (FreshRSS_Context::$isCli && $nb_feeds >= $limits['max_feeds']) {
  215. Minz_Log::warning(_t('feedback.sub.feed.over_max',
  216. $limits['max_feeds']));
  217. $ok = false;
  218. continue;
  219. }
  220. if ($this->addFeedOpml($elt, $parent_cat)) {
  221. $nb_feeds++;
  222. } else {
  223. $ok = false;
  224. }
  225. } else {
  226. // No xmlUrl? It should be a category!
  227. $limit_reached = ($nb_cats >= $limits['max_categories']);
  228. if (!FreshRSS_Context::$isCli && $limit_reached) {
  229. Minz_Log::warning(_t('feedback.sub.category.over_max',
  230. $limits['max_categories']));
  231. $ok = false;
  232. continue;
  233. }
  234. if ($this->addCategoryOpml($elt, $parent_cat, $limit_reached)) {
  235. $nb_cats++;
  236. } else {
  237. $ok = false;
  238. }
  239. }
  240. }
  241. return $ok;
  242. }
  243. /**
  244. * This method imports an OPML feed element.
  245. *
  246. * @param array $feed_elt an OPML element (must be a feed element).
  247. * @param string $parent_cat the name of the parent category.
  248. * @return boolean false if an error occured, true otherwise.
  249. */
  250. private function addFeedOpml($feed_elt, $parent_cat) {
  251. if ($parent_cat == null) {
  252. // This feed has no parent category so we get the default one
  253. $this->catDAO->checkDefault();
  254. $default_cat = $this->catDAO->getDefault();
  255. $parent_cat = $default_cat->name();
  256. }
  257. $cat = $this->catDAO->searchByName($parent_cat);
  258. if ($cat == null) {
  259. // If there is not $cat, it means parent category does not exist in
  260. // database.
  261. // If it happens, take the default category.
  262. $this->catDAO->checkDefault();
  263. $cat = $this->catDAO->getDefault();
  264. }
  265. // We get different useful information
  266. $url = Minz_Helper::htmlspecialchars_utf8($feed_elt['xmlUrl']);
  267. $name = Minz_Helper::htmlspecialchars_utf8($feed_elt['text']);
  268. $website = '';
  269. if (isset($feed_elt['htmlUrl'])) {
  270. $website = Minz_Helper::htmlspecialchars_utf8($feed_elt['htmlUrl']);
  271. }
  272. $description = '';
  273. if (isset($feed_elt['description'])) {
  274. $description = Minz_Helper::htmlspecialchars_utf8($feed_elt['description']);
  275. }
  276. $error = false;
  277. try {
  278. // Create a Feed object and add it in DB
  279. $feed = new FreshRSS_Feed($url);
  280. $feed->_category($cat->id());
  281. $feed->_name($name);
  282. $feed->_website($website);
  283. $feed->_description($description);
  284. // Call the extension hook
  285. $feed = Minz_ExtensionManager::callHook('feed_before_insert', $feed);
  286. if ($feed != null) {
  287. // addFeedObject checks if feed is already in DB so nothing else to
  288. // check here
  289. $id = $this->feedDAO->addFeedObject($feed);
  290. $error = ($id === false);
  291. } else {
  292. $error = true;
  293. }
  294. } catch (FreshRSS_Feed_Exception $e) {
  295. if (FreshRSS_Context::$isCli) {
  296. fwrite(STDERR, 'FreshRSS error during OPML feed import: ' . $e->getMessage() . "\n");
  297. } else {
  298. Minz_Log::warning($e->getMessage());
  299. }
  300. $error = true;
  301. }
  302. if ($error) {
  303. if (FreshRSS_Context::$isCli) {
  304. fwrite(STDERR, 'FreshRSS error during OPML feed import from URL: ' . $url . ' in category ' . $cat->id() . "\n");
  305. } else {
  306. Minz_Log::warning('Error during OPML feed import from URL: ' . $url . ' in category ' . $cat->id());
  307. }
  308. }
  309. return !$error;
  310. }
  311. /**
  312. * This method imports an OPML category element.
  313. *
  314. * @param array $cat_elt an OPML element (must be a category element).
  315. * @param string $parent_cat the name of the parent category.
  316. * @param boolean $cat_limit_reached indicates if category limit has been reached.
  317. * if yes, category is not added (but we try for feeds!)
  318. * @return boolean false if an error occured, true otherwise.
  319. */
  320. private function addCategoryOpml($cat_elt, $parent_cat, $cat_limit_reached) {
  321. // Create a new Category object
  322. $catName = Minz_Helper::htmlspecialchars_utf8($cat_elt['text']);
  323. $cat = new FreshRSS_Category($catName);
  324. $error = true;
  325. if (FreshRSS_Context::$isCli || !$cat_limit_reached) {
  326. $id = $this->catDAO->addCategoryObject($cat);
  327. $error = ($id === false);
  328. }
  329. if ($error) {
  330. if (FreshRSS_Context::$isCli) {
  331. fwrite(STDERR, 'FreshRSS error during OPML category import from URL: ' . $catName . "\n");
  332. } else {
  333. Minz_Log::warning('Error during OPML category import from URL: ' . $catName);
  334. }
  335. }
  336. if (isset($cat_elt['@outlines'])) {
  337. // Our cat_elt contains more categories or more feeds, so we
  338. // add them recursively.
  339. // Note: FreshRSS does not support yet category arborescence
  340. $error &= !$this->addOpmlElements($cat_elt['@outlines'], $catName);
  341. }
  342. return !$error;
  343. }
  344. private function ttrssXmlToJson($xml) {
  345. $table = (array)simplexml_load_string($xml, null, LIBXML_NOCDATA);
  346. $table['items'] = isset($table['article']) ? $table['article'] : array();
  347. unset($table['article']);
  348. for ($i = count($table['items']) - 1; $i >= 0; $i--) {
  349. $item = (array)($table['items'][$i]);
  350. $item['updated'] = isset($item['updated']) ? strtotime($item['updated']) : '';
  351. $item['published'] = $item['updated'];
  352. $item['content'] = array('content' => isset($item['content']) ? $item['content'] : '');
  353. $item['categories'] = isset($item['tag_cache']) ? array($item['tag_cache']) : array();
  354. if (!empty($item['marked'])) {
  355. $item['categories'][] = 'user/-/state/com.google/starred';
  356. }
  357. if (!empty($item['published'])) {
  358. $item['categories'][] = 'user/-/state/com.google/broadcast';
  359. }
  360. if (!empty($item['label_cache'])) {
  361. $labels_cache = json_decode($item['label_cache'], true);
  362. if (is_array($labels_cache)) {
  363. foreach ($labels_cache as $label_cache) {
  364. if (!empty($label_cache[1])) {
  365. $item['categories'][] = 'user/-/label/' . trim($label_cache[1]);
  366. }
  367. }
  368. }
  369. }
  370. $item['alternate'][0]['href'] = isset($item['link']) ? $item['link'] : '';
  371. $item['origin'] = array(
  372. 'title' => isset($item['feed_title']) ? $item['feed_title'] : '',
  373. 'feedUrl' => isset($item['feed_url']) ? $item['feed_url'] : '',
  374. );
  375. $item['id'] = isset($item['guid']) ? $item['guid'] : (isset($item['feed_url']) ? $item['feed_url'] : $item['published']);
  376. $table['items'][$i] = $item;
  377. }
  378. return json_encode($table);
  379. }
  380. /**
  381. * This method import a JSON-based file (Google Reader format).
  382. *
  383. * @param string $article_file the JSON file content.
  384. * @param boolean $starred true if articles from the file must be starred.
  385. * @return boolean false if an error occured, true otherwise.
  386. */
  387. private function importJson($article_file, $starred = false) {
  388. $article_object = json_decode($article_file, true);
  389. if ($article_object == null) {
  390. if (FreshRSS_Context::$isCli) {
  391. fwrite(STDERR, 'FreshRSS error trying to import a non-JSON file' . "\n");
  392. } else {
  393. Minz_Log::warning('Try to import a non-JSON file');
  394. }
  395. return false;
  396. }
  397. $is_read = FreshRSS_Context::$user_conf->mark_when['reception'] ? 1 : 0;
  398. $google_compliant = strpos($article_object['id'], 'com.google') !== false;
  399. $error = false;
  400. $article_to_feed = array();
  401. $nb_feeds = count($this->feedDAO->listFeeds());
  402. $newFeedGuids = array();
  403. $limits = FreshRSS_Context::$system_conf->limits;
  404. // First, we check feeds of articles are in DB (and add them if needed).
  405. foreach ($article_object['items'] as $item) {
  406. $key = $google_compliant ? 'htmlUrl' : 'feedUrl';
  407. $feed = new FreshRSS_Feed($item['origin'][$key]);
  408. $feed = $this->feedDAO->searchByUrl($feed->url());
  409. if ($feed == null) {
  410. // Feed does not exist in DB,we should to try to add it.
  411. if ((!FreshRSS_Context::$isCli) && ($nb_feeds >= $limits['max_feeds'])) {
  412. // Oops, no more place!
  413. Minz_Log::warning(_t('feedback.sub.feed.over_max', $limits['max_feeds']));
  414. } else {
  415. $feed = $this->addFeedJson($item['origin']);
  416. }
  417. if ($feed == null) {
  418. // Still null? It means something went wrong.
  419. $error = true;
  420. } else {
  421. $nb_feeds++;
  422. }
  423. }
  424. if ($feed != null) {
  425. $article_to_feed[$item['id']] = $feed->id();
  426. if (!isset($newFeedGuids['f_' . $feed->id()])) {
  427. $newFeedGuids['f_' . $feed->id()] = array();
  428. }
  429. $newFeedGuids['f_' . $feed->id()][] = safe_ascii($item['id']);
  430. }
  431. }
  432. $tagDAO = FreshRSS_Factory::createTagDao();
  433. $labels = $tagDAO->listTags();
  434. $knownLabels = array();
  435. foreach ($labels as $label) {
  436. $knownLabels[$label->name()]['id'] = $label->id();
  437. $knownLabels[$label->name()]['articles'] = array();
  438. }
  439. unset($labels);
  440. // For each feed, check existing GUIDs already in database.
  441. $existingHashForGuids = array();
  442. foreach ($newFeedGuids as $feedId => $newGuids) {
  443. $existingHashForGuids[$feedId] = $this->entryDAO->listHashForFeedGuids(substr($feedId, 2), $newGuids);
  444. }
  445. unset($newFeedGuids);
  446. // Then, articles are imported.
  447. $newGuids = array();
  448. $this->entryDAO->beginTransaction();
  449. foreach ($article_object['items'] as $item) {
  450. if (empty($article_to_feed[$item['id']])) {
  451. // Related feed does not exist for this entry, do nothing.
  452. continue;
  453. }
  454. $feed_id = $article_to_feed[$item['id']];
  455. $author = isset($item['author']) ? $item['author'] : '';
  456. $is_starred = $starred;
  457. $tags = $item['categories'];
  458. $labels = array();
  459. for ($i = count($tags) - 1; $i >= 0; $i --) {
  460. $tag = trim($tags[$i]);
  461. if (strpos($tag, 'user/-/') !== false) {
  462. if ($tag === 'user/-/state/com.google/starred') {
  463. $is_starred = true;
  464. } elseif (strpos($tag, 'user/-/label/') === 0) {
  465. $tag = trim(substr($tag, 13));
  466. if ($tag != '') {
  467. $labels[] = $tag;
  468. }
  469. }
  470. unset($tags[$i]);
  471. }
  472. }
  473. $url = $item['alternate'][0]['href'];
  474. if (!empty($item['content']['content'])) {
  475. $content = $item['content']['content'];
  476. } elseif (!empty($item['summary']['content'])) {
  477. $content = $item['summary']['content'];
  478. }
  479. $content = sanitizeHTML($content, $url);
  480. $entry = new FreshRSS_Entry(
  481. $feed_id, $item['id'], $item['title'], $author,
  482. $content, $url,
  483. $item['published'], $is_read, $is_starred
  484. );
  485. $entry->_id(min(time(), $entry->date(true)) . uSecString());
  486. $entry->_tags($tags);
  487. if (isset($newGuids[$entry->guid()])) {
  488. continue; //Skip subsequent articles with same GUID
  489. }
  490. $newGuids[$entry->guid()] = true;
  491. $entry = Minz_ExtensionManager::callHook('entry_before_insert', $entry);
  492. if ($entry == null) {
  493. // An extension has returned a null value, there is nothing to insert.
  494. continue;
  495. }
  496. $values = $entry->toArray();
  497. $ok = false;
  498. if (isset($existingHashForGuids['f_' . $feed_id][$entry->guid()])) {
  499. $ok = $this->entryDAO->updateEntry($values);
  500. } else {
  501. $ok = $this->entryDAO->addEntry($values);
  502. }
  503. foreach ($labels as $labelName) {
  504. if (empty($knownLabels[$labelName]['id'])) {
  505. $labelId = $tagDAO->addTag(array('name' => $labelName));
  506. $knownLabels[$labelName]['id'] = $labelId;
  507. $knownLabels[$labelName]['articles'] = array();
  508. }
  509. $knownLabels[$labelName]['articles'][] = array(
  510. //'id' => $entry->id(), //ID changes after commitNewEntries()
  511. 'id_feed' => $entry->feed(),
  512. 'guid' => $entry->guid(),
  513. );
  514. }
  515. $error |= ($ok === false);
  516. }
  517. $this->entryDAO->commit();
  518. $this->entryDAO->beginTransaction();
  519. $this->entryDAO->commitNewEntries();
  520. $this->feedDAO->updateCachedValues();
  521. $this->entryDAO->commit();
  522. $this->entryDAO->beginTransaction();
  523. foreach ($knownLabels as $labelName => $knownLabel) {
  524. $labelId = $knownLabel['id'];
  525. foreach ($knownLabel['articles'] as $article) {
  526. $entryId = $this->entryDAO->searchIdByGuid($article['id_feed'], $article['guid']);
  527. if ($entryId != null) {
  528. $tagDAO->tagEntry($labelId, $entryId);
  529. } else {
  530. Minz_Log::warning('Could not add label "' . $labelName . '" to entry "' . $article['guid'] . '" in feed ' . $article['id_feed']);
  531. }
  532. }
  533. }
  534. $this->entryDAO->commit();
  535. return !$error;
  536. }
  537. /**
  538. * This method import a JSON-based feed (Google Reader format).
  539. *
  540. * @param array $origin represents a feed.
  541. * @return FreshRSS_Feed if feed is in database at the end of the process,
  542. * else null.
  543. */
  544. private function addFeedJson($origin) {
  545. $return = null;
  546. if (!empty($origin['feedUrl'])) {
  547. $url = $origin['feedUrl'];
  548. } elseif (!empty($origin['htmlUrl'])) {
  549. $url = $origin['htmlUrl'];
  550. } else {
  551. return null;
  552. }
  553. if (!empty($origin['htmlUrl'])) {
  554. $website = $origin['htmlUrl'];
  555. } elseif (!empty($origin['feedUrl'])) {
  556. $website = $origin['feedUrl'];
  557. }
  558. $name = empty($origin['title']) ? '' : $origin['title'];
  559. try {
  560. // Create a Feed object and add it in database.
  561. $feed = new FreshRSS_Feed($url);
  562. $feed->_category(FreshRSS_CategoryDAO::DEFAULTCATEGORYID);
  563. $feed->_name($name);
  564. $feed->_website($website);
  565. // Call the extension hook
  566. $feed = Minz_ExtensionManager::callHook('feed_before_insert', $feed);
  567. if ($feed != null) {
  568. // addFeedObject checks if feed is already in DB so nothing else to
  569. // check here.
  570. $id = $this->feedDAO->addFeedObject($feed);
  571. if ($id !== false) {
  572. $feed->_id($id);
  573. $return = $feed;
  574. }
  575. }
  576. } catch (FreshRSS_Feed_Exception $e) {
  577. if (FreshRSS_Context::$isCli) {
  578. fwrite(STDERR, 'FreshRSS error during JSON feed import: ' . $e->getMessage() . "\n");
  579. } else {
  580. Minz_Log::warning($e->getMessage());
  581. }
  582. }
  583. return $return;
  584. }
  585. public function exportFile($export_opml = true, $export_starred = false, $export_labelled = false, $export_feeds = array(), $maxFeedEntries = 50, $username = null) {
  586. require_once(LIB_PATH . '/lib_opml.php');
  587. $this->catDAO = new FreshRSS_CategoryDAO($username);
  588. $this->entryDAO = FreshRSS_Factory::createEntryDao($username);
  589. $this->feedDAO = FreshRSS_Factory::createFeedDao($username);
  590. $this->entryDAO->disableBuffering();
  591. if ($export_feeds === true) {
  592. //All feeds
  593. $export_feeds = $this->feedDAO->listFeedsIds();
  594. }
  595. if (!is_array($export_feeds)) {
  596. $export_feeds = array();
  597. }
  598. $day = date('Y-m-d');
  599. $export_files = array();
  600. if ($export_opml) {
  601. $export_files["feeds_${day}.opml.xml"] = $this->generateOpml();
  602. }
  603. if ($export_starred || $export_labelled) {
  604. $export_files["starred_${day}.json"] = $this->generateEntries(
  605. ($export_starred ? 'S' : '') .
  606. ($export_labelled ? 'T' : '')
  607. );
  608. }
  609. foreach ($export_feeds as $feed_id) {
  610. $feed = $this->feedDAO->searchById($feed_id);
  611. if ($feed) {
  612. $filename = "feed_${day}_" . $feed->category() . '_'
  613. . $feed->id() . '.json';
  614. $export_files[$filename] = $this->generateEntries('f', $feed, $maxFeedEntries);
  615. }
  616. }
  617. $nb_files = count($export_files);
  618. if ($nb_files > 1) {
  619. // If there are more than 1 file to export, we need a ZIP archive.
  620. try {
  621. $this->sendZip($export_files);
  622. } catch (Exception $e) {
  623. throw new FreshRSS_ZipMissing_Exception($e);
  624. }
  625. } elseif ($nb_files === 1) {
  626. // Only one file? Guess its type and export it.
  627. $filename = key($export_files);
  628. $type = self::guessFileType($filename);
  629. $this->sendFile('freshrss_' . Minz_Session::param('currentUser', '_') . '_' . $filename, $export_files[$filename], $type);
  630. }
  631. return $nb_files;
  632. }
  633. /**
  634. * This action handles export action.
  635. *
  636. * This action must be reached by a POST request.
  637. *
  638. * Parameters are:
  639. * - export_opml (default: false)
  640. * - export_starred (default: false)
  641. * - export_feeds (default: array()) a list of feed ids
  642. */
  643. public function exportAction() {
  644. if (!Minz_Request::isPost()) {
  645. Minz_Request::forward(array('c' => 'importExport', 'a' => 'index'), true);
  646. }
  647. $this->view->_useLayout(false);
  648. $nb_files = 0;
  649. try {
  650. $nb_files = $this->exportFile(
  651. Minz_Request::param('export_opml', false),
  652. Minz_Request::param('export_starred', false),
  653. Minz_Request::param('export_labelled', false),
  654. Minz_Request::param('export_feeds', array())
  655. );
  656. } catch (FreshRSS_ZipMissing_Exception $zme) {
  657. # Oops, there is no ZIP extension!
  658. Minz_Request::bad(_t('feedback.import_export.export_no_zip_extension'),
  659. array('c' => 'importExport', 'a' => 'index'));
  660. }
  661. if ($nb_files < 1) {
  662. // Nothing to do...
  663. Minz_Request::forward(array('c' => 'importExport', 'a' => 'index'), true);
  664. }
  665. }
  666. /**
  667. * This method returns the OPML file based on user subscriptions.
  668. *
  669. * @return string the OPML file content.
  670. */
  671. private function generateOpml() {
  672. $list = array();
  673. foreach ($this->catDAO->listCategories() as $key => $cat) {
  674. $list[$key]['name'] = $cat->name();
  675. $list[$key]['feeds'] = $this->feedDAO->listByCategory($cat->id());
  676. }
  677. $this->view->categories = $list;
  678. return $this->view->helperToString('export/opml');
  679. }
  680. /**
  681. * This method returns a JSON file content.
  682. *
  683. * @param string $type must be one of:
  684. * 'S' (starred/favourite), 'f' (feed), 'T' (taggued/labelled), 'ST' (starred or labelled)
  685. * @param FreshRSS_Feed $feed feed of which we want to get entries.
  686. * @return string the JSON file content.
  687. */
  688. private function generateEntries($type, $feed = null, $maxFeedEntries = 50) {
  689. $this->view->categories = $this->catDAO->listCategories();
  690. $tagDAO = FreshRSS_Factory::createTagDao();
  691. if ($type === 's' || $type === 'S' || $type === 'T' || $type === 'ST') {
  692. $this->view->list_title = _t('sub.import_export.starred_list');
  693. $this->view->type = 'starred';
  694. $this->view->entriesId = $this->entryDAO->listIdsWhere($type, '', FreshRSS_Entry::STATE_ALL, 'ASC', -1);
  695. $this->view->entryIdsTagNames = $tagDAO->getEntryIdsTagNames($this->view->entriesId);
  696. //The following is a streamable query, i.e. must be last
  697. $this->view->entriesRaw = $this->entryDAO->listWhereRaw($type, '', FreshRSS_Entry::STATE_ALL, 'ASC', -1);
  698. } elseif ($type === 'f' && $feed != null) {
  699. $this->view->list_title = _t('sub.import_export.feed_list', $feed->name());
  700. $this->view->type = 'feed/' . $feed->id();
  701. $this->view->entriesId = $this->entryDAO->listIdsWhere($type, $feed->id(), FreshRSS_Entry::STATE_ALL, 'ASC', $maxFeedEntries);
  702. $this->view->entryIdsTagNames = $tagDAO->getEntryIdsTagNames($this->view->entriesId);
  703. //The following is a streamable query, i.e. must be last
  704. $this->view->entriesRaw = $this->entryDAO->listWhereRaw($type, $feed->id(), FreshRSS_Entry::STATE_ALL, 'ASC', $maxFeedEntries);
  705. $this->view->feed = $feed;
  706. }
  707. return $this->view->helperToString('export/articles');
  708. }
  709. /**
  710. * This method zips a list of files and returns it by HTTP.
  711. *
  712. * @param array $files list of files where key is filename and value the content.
  713. * @throws Exception if Zip extension is not loaded.
  714. */
  715. private function sendZip($files) {
  716. if (!extension_loaded('zip')) {
  717. throw new Exception();
  718. }
  719. // From https://stackoverflow.com/questions/1061710/php-zip-files-on-the-fly
  720. $zip_file = @tempnam('/tmp', 'zip');
  721. $zip = new ZipArchive();
  722. $zip->open($zip_file, ZipArchive::OVERWRITE);
  723. foreach ($files as $filename => $content) {
  724. $zip->addFromString($filename, $content);
  725. }
  726. // Close and send to user
  727. $zip->close();
  728. header('Content-Type: application/zip');
  729. header('Content-Length: ' . filesize($zip_file));
  730. $day = date('Y-m-d');
  731. header('Content-Disposition: attachment; filename="freshrss_' . Minz_Session::param('currentUser', '_') . '_' . $day . '_export.zip"');
  732. readfile($zip_file);
  733. unlink($zip_file);
  734. }
  735. /**
  736. * This method returns a single file (OPML or JSON) by HTTP.
  737. *
  738. * @param string $filename
  739. * @param string $content
  740. * @param string $type the file type (opml, json_feed or json_starred).
  741. * If equals to unknown, nothing happens.
  742. */
  743. private function sendFile($filename, $content, $type) {
  744. if ($type === 'unknown') {
  745. return;
  746. }
  747. $content_type = '';
  748. if ($type === 'opml') {
  749. $content_type = 'application/xml';
  750. } elseif ($type === 'json_feed' || $type === 'json_starred') {
  751. $content_type = 'application/json';
  752. }
  753. header('Content-Type: ' . $content_type . '; charset=utf-8');
  754. header('Content-disposition: attachment; filename=' . $filename);
  755. print($content);
  756. }
  757. }