importExportController.php 26 KB

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