4
0

importExportController.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  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. //Sort with categories first
  229. usort($opml_elements, function ($a, $b) {
  230. return strcmp(
  231. (isset($a['xmlUrl']) ? 'Z' : 'A') . $a['text'],
  232. (isset($b['xmlUrl']) ? 'Z' : 'A') . $b['text']);
  233. });
  234. foreach ($opml_elements as $elt) {
  235. if (isset($elt['xmlUrl'])) {
  236. // If xmlUrl exists, it means it is a feed
  237. if (FreshRSS_Context::$isCli && $nb_feeds >= $limits['max_feeds']) {
  238. Minz_Log::warning(_t('feedback.sub.feed.over_max',
  239. $limits['max_feeds']));
  240. $ok = false;
  241. continue;
  242. }
  243. if ($this->addFeedOpml($elt, $parent_cat)) {
  244. $nb_feeds++;
  245. } else {
  246. $ok = false;
  247. }
  248. } else {
  249. // No xmlUrl? It should be a category!
  250. $limit_reached = ($nb_cats >= $limits['max_categories']);
  251. if (!FreshRSS_Context::$isCli && $limit_reached) {
  252. Minz_Log::warning(_t('feedback.sub.category.over_max',
  253. $limits['max_categories']));
  254. $ok = false;
  255. continue;
  256. }
  257. if ($this->addCategoryOpml($elt, $parent_cat, $limit_reached)) {
  258. $nb_cats++;
  259. } else {
  260. $ok = false;
  261. }
  262. }
  263. }
  264. return $ok;
  265. }
  266. /**
  267. * This method imports an OPML feed element.
  268. *
  269. * @param array $feed_elt an OPML element (must be a feed element).
  270. * @param string $parent_cat the name of the parent category.
  271. * @return boolean false if an error occured, true otherwise.
  272. */
  273. private function addFeedOpml($feed_elt, $parent_cat) {
  274. if ($parent_cat == null) {
  275. // This feed has no parent category so we get the default one
  276. $this->catDAO->checkDefault();
  277. $default_cat = $this->catDAO->getDefault();
  278. $parent_cat = $default_cat->name();
  279. }
  280. $cat = $this->catDAO->searchByName($parent_cat);
  281. if ($cat == null) {
  282. // If there is not $cat, it means parent category does not exist in
  283. // database.
  284. // If it happens, take the default category.
  285. $this->catDAO->checkDefault();
  286. $cat = $this->catDAO->getDefault();
  287. }
  288. // We get different useful information
  289. $url = Minz_Helper::htmlspecialchars_utf8($feed_elt['xmlUrl']);
  290. $name = Minz_Helper::htmlspecialchars_utf8($feed_elt['text']);
  291. $website = '';
  292. if (isset($feed_elt['htmlUrl'])) {
  293. $website = Minz_Helper::htmlspecialchars_utf8($feed_elt['htmlUrl']);
  294. }
  295. $description = '';
  296. if (isset($feed_elt['description'])) {
  297. $description = Minz_Helper::htmlspecialchars_utf8($feed_elt['description']);
  298. }
  299. $error = false;
  300. try {
  301. // Create a Feed object and add it in DB
  302. $feed = new FreshRSS_Feed($url);
  303. $feed->_category($cat->id());
  304. $feed->_name($name);
  305. $feed->_website($website);
  306. $feed->_description($description);
  307. // Call the extension hook
  308. $feed = Minz_ExtensionManager::callHook('feed_before_insert', $feed);
  309. if ($feed != null) {
  310. // addFeedObject checks if feed is already in DB so nothing else to
  311. // check here
  312. $id = $this->feedDAO->addFeedObject($feed);
  313. $error = ($id === false);
  314. } else {
  315. $error = true;
  316. }
  317. } catch (FreshRSS_Feed_Exception $e) {
  318. if (FreshRSS_Context::$isCli) {
  319. fwrite(STDERR, 'FreshRSS error during OPML feed import: ' . $e->getMessage() . "\n");
  320. } else {
  321. Minz_Log::warning($e->getMessage());
  322. }
  323. $error = true;
  324. }
  325. if ($error) {
  326. if (FreshRSS_Context::$isCli) {
  327. fwrite(STDERR, 'FreshRSS error during OPML feed import from URL: ' . $url . ' in category ' . $cat->id() . "\n");
  328. } else {
  329. Minz_Log::warning('Error during OPML feed import from URL: ' . $url . ' in category ' . $cat->id());
  330. }
  331. }
  332. return !$error;
  333. }
  334. /**
  335. * This method imports an OPML category element.
  336. *
  337. * @param array $cat_elt an OPML element (must be a category element).
  338. * @param string $parent_cat the name of the parent category.
  339. * @param boolean $cat_limit_reached indicates if category limit has been reached.
  340. * if yes, category is not added (but we try for feeds!)
  341. * @return boolean false if an error occured, true otherwise.
  342. */
  343. private function addCategoryOpml($cat_elt, $parent_cat, $cat_limit_reached) {
  344. // Create a new Category object
  345. $catName = Minz_Helper::htmlspecialchars_utf8($cat_elt['text']);
  346. $cat = new FreshRSS_Category($catName);
  347. $error = true;
  348. if (FreshRSS_Context::$isCli || !$cat_limit_reached) {
  349. $id = $this->catDAO->addCategoryObject($cat);
  350. $error = ($id === false);
  351. }
  352. if ($error) {
  353. if (FreshRSS_Context::$isCli) {
  354. fwrite(STDERR, 'FreshRSS error during OPML category import from URL: ' . $catName . "\n");
  355. } else {
  356. Minz_Log::warning('Error during OPML category import from URL: ' . $catName);
  357. }
  358. }
  359. if (isset($cat_elt['@outlines'])) {
  360. // Our cat_elt contains more categories or more feeds, so we
  361. // add them recursively.
  362. // Note: FreshRSS does not support yet category arborescence
  363. $error &= !$this->addOpmlElements($cat_elt['@outlines'], $catName);
  364. }
  365. return !$error;
  366. }
  367. private function ttrssXmlToJson($xml) {
  368. $table = (array)simplexml_load_string($xml, null, LIBXML_NOCDATA);
  369. $table['items'] = isset($table['article']) ? $table['article'] : array();
  370. unset($table['article']);
  371. for ($i = count($table['items']) - 1; $i >= 0; $i--) {
  372. $item = (array)($table['items'][$i]);
  373. $item['updated'] = isset($item['updated']) ? strtotime($item['updated']) : '';
  374. $item['published'] = $item['updated'];
  375. $item['content'] = array('content' => isset($item['content']) ? $item['content'] : '');
  376. $item['categories'] = isset($item['tag_cache']) ? array($item['tag_cache']) : array();
  377. if (!empty($item['marked'])) {
  378. $item['categories'][] = 'user/-/state/com.google/starred';
  379. }
  380. if (!empty($item['published'])) {
  381. $item['categories'][] = 'user/-/state/com.google/broadcast';
  382. }
  383. if (!empty($item['label_cache'])) {
  384. $labels_cache = json_decode($item['label_cache'], true);
  385. if (is_array($labels_cache)) {
  386. foreach ($labels_cache as $label_cache) {
  387. if (!empty($label_cache[1])) {
  388. $item['categories'][] = 'user/-/label/' . trim($label_cache[1]);
  389. }
  390. }
  391. }
  392. }
  393. $item['alternate'][0]['href'] = isset($item['link']) ? $item['link'] : '';
  394. $item['origin'] = array(
  395. 'title' => isset($item['feed_title']) ? $item['feed_title'] : '',
  396. 'feedUrl' => isset($item['feed_url']) ? $item['feed_url'] : '',
  397. );
  398. $item['id'] = isset($item['guid']) ? $item['guid'] : (isset($item['feed_url']) ? $item['feed_url'] : $item['published']);
  399. $table['items'][$i] = $item;
  400. }
  401. return json_encode($table);
  402. }
  403. /**
  404. * This method import a JSON-based file (Google Reader format).
  405. *
  406. * @param string $article_file the JSON file content.
  407. * @param boolean $starred true if articles from the file must be starred.
  408. * @return boolean false if an error occured, true otherwise.
  409. */
  410. private function importJson($article_file, $starred = false) {
  411. $article_object = json_decode($article_file, true);
  412. if ($article_object == null) {
  413. if (FreshRSS_Context::$isCli) {
  414. fwrite(STDERR, 'FreshRSS error trying to import a non-JSON file' . "\n");
  415. } else {
  416. Minz_Log::warning('Try to import a non-JSON file');
  417. }
  418. return false;
  419. }
  420. $items = isset($article_object['items']) ? $article_object['items'] : $article_object;
  421. $mark_as_read = FreshRSS_Context::$user_conf->mark_when['reception'] ? 1 : 0;
  422. $error = false;
  423. $article_to_feed = array();
  424. $nb_feeds = count($this->feedDAO->listFeeds());
  425. $newFeedGuids = array();
  426. $limits = FreshRSS_Context::$system_conf->limits;
  427. // First, we check feeds of articles are in DB (and add them if needed).
  428. foreach ($items as $item) {
  429. if (!isset($item['origin'])) {
  430. $item['origin'] = array('title' => 'Import');
  431. }
  432. if (!empty($item['origin']['feedUrl'])) {
  433. $feedUrl = $item['origin']['feedUrl'];
  434. } elseif (!empty($item['origin']['streamId']) && strpos($item['origin']['streamId'], 'feed/') === 0) {
  435. $feedUrl = substr($item['origin']['streamId'], 5); //Google Reader
  436. $item['origin']['feedUrl'] = $feedUrl;
  437. } elseif (!empty($item['origin']['htmlUrl'])) {
  438. $feedUrl = $item['origin']['htmlUrl'];
  439. } else {
  440. $feedUrl = 'http://import.localhost/import.xml';
  441. $item['origin']['feedUrl'] = $feedUrl;
  442. $item['origin']['disable'] = true;
  443. }
  444. $feed = new FreshRSS_Feed($feedUrl);
  445. $feed = $this->feedDAO->searchByUrl($feed->url());
  446. if ($feed == null) {
  447. // Feed does not exist in DB,we should to try to add it.
  448. if ((!FreshRSS_Context::$isCli) && ($nb_feeds >= $limits['max_feeds'])) {
  449. // Oops, no more place!
  450. Minz_Log::warning(_t('feedback.sub.feed.over_max', $limits['max_feeds']));
  451. } else {
  452. $feed = $this->addFeedJson($item['origin']);
  453. }
  454. if ($feed == null) {
  455. // Still null? It means something went wrong.
  456. $error = true;
  457. } else {
  458. $nb_feeds++;
  459. }
  460. }
  461. if ($feed != null) {
  462. $article_to_feed[$item['id']] = $feed->id();
  463. if (!isset($newFeedGuids['f_' . $feed->id()])) {
  464. $newFeedGuids['f_' . $feed->id()] = array();
  465. }
  466. $newFeedGuids['f_' . $feed->id()][] = safe_ascii($item['id']);
  467. }
  468. }
  469. $tagDAO = FreshRSS_Factory::createTagDao();
  470. $labels = $tagDAO->listTags();
  471. $knownLabels = array();
  472. foreach ($labels as $label) {
  473. $knownLabels[$label->name()]['id'] = $label->id();
  474. $knownLabels[$label->name()]['articles'] = array();
  475. }
  476. unset($labels);
  477. // For each feed, check existing GUIDs already in database.
  478. $existingHashForGuids = array();
  479. foreach ($newFeedGuids as $feedId => $newGuids) {
  480. $existingHashForGuids[$feedId] = $this->entryDAO->listHashForFeedGuids(substr($feedId, 2), $newGuids);
  481. }
  482. unset($newFeedGuids);
  483. // Then, articles are imported.
  484. $newGuids = array();
  485. $this->entryDAO->beginTransaction();
  486. foreach ($items as $item) {
  487. if (empty($article_to_feed[$item['id']])) {
  488. // Related feed does not exist for this entry, do nothing.
  489. continue;
  490. }
  491. $feed_id = $article_to_feed[$item['id']];
  492. $author = isset($item['author']) ? $item['author'] : '';
  493. $is_starred = false;
  494. $is_read = null;
  495. $tags = empty($item['categories']) ? array() : $item['categories'];
  496. $labels = array();
  497. for ($i = count($tags) - 1; $i >= 0; $i --) {
  498. $tag = trim($tags[$i]);
  499. if (strpos($tag, 'user/-/') !== false) {
  500. if ($tag === 'user/-/state/com.google/starred') {
  501. $is_starred = true;
  502. } elseif ($tag === 'user/-/state/com.google/read') {
  503. $is_read = true;
  504. } elseif ($tag === 'user/-/state/com.google/unread') {
  505. $is_read = false;
  506. } elseif (strpos($tag, 'user/-/label/') === 0) {
  507. $tag = trim(substr($tag, 13));
  508. if ($tag != '') {
  509. $labels[] = $tag;
  510. }
  511. }
  512. unset($tags[$i]);
  513. }
  514. }
  515. if ($starred && !$is_starred) {
  516. //If the article has no label, mark it as starred (old format)
  517. $is_starred = empty($labels);
  518. }
  519. if ($is_read === null) {
  520. $is_read = $mark_as_read;
  521. }
  522. if (isset($item['alternate'][0]['href'])) {
  523. $url = $item['alternate'][0]['href'];
  524. } elseif (isset($item['url'])) {
  525. $url = $item['url']; //FeedBin
  526. } else {
  527. $url = '';
  528. }
  529. if (!empty($item['content']['content'])) {
  530. $content = $item['content']['content'];
  531. } elseif (!empty($item['summary']['content'])) {
  532. $content = $item['summary']['content'];
  533. } elseif (!empty($item['content'])) {
  534. $content = $item['content']; //FeedBin
  535. } else {
  536. $content = '';
  537. }
  538. $content = sanitizeHTML($content, $url);
  539. if (!empty($item['published'])) {
  540. $published = $item['published'];
  541. } elseif (!empty($item['timestampUsec'])) {
  542. $published = substr($item['timestampUsec'], 0, -6);
  543. } elseif (!empty($item['updated'])) {
  544. $published = $item['updated'];
  545. } else {
  546. $published = 0;
  547. }
  548. if (!ctype_digit('' . $published)) {
  549. $published = strtotime($published);
  550. }
  551. $entry = new FreshRSS_Entry(
  552. $feed_id, $item['id'], $item['title'], $author,
  553. $content, $url, $published, $is_read, $is_starred
  554. );
  555. $entry->_id(uTimeString());
  556. $entry->_tags($tags);
  557. if (isset($newGuids[$entry->guid()])) {
  558. continue; //Skip subsequent articles with same GUID
  559. }
  560. $newGuids[$entry->guid()] = true;
  561. $entry = Minz_ExtensionManager::callHook('entry_before_insert', $entry);
  562. if ($entry == null) {
  563. // An extension has returned a null value, there is nothing to insert.
  564. continue;
  565. }
  566. $values = $entry->toArray();
  567. $ok = false;
  568. if (isset($existingHashForGuids['f_' . $feed_id][$entry->guid()])) {
  569. $ok = $this->entryDAO->updateEntry($values);
  570. } else {
  571. $ok = $this->entryDAO->addEntry($values);
  572. }
  573. foreach ($labels as $labelName) {
  574. if (empty($knownLabels[$labelName]['id'])) {
  575. $labelId = $tagDAO->addTag(array('name' => $labelName));
  576. $knownLabels[$labelName]['id'] = $labelId;
  577. $knownLabels[$labelName]['articles'] = array();
  578. }
  579. $knownLabels[$labelName]['articles'][] = array(
  580. //'id' => $entry->id(), //ID changes after commitNewEntries()
  581. 'id_feed' => $entry->feed(),
  582. 'guid' => $entry->guid(),
  583. );
  584. }
  585. $error |= ($ok === false);
  586. }
  587. $this->entryDAO->commit();
  588. $this->entryDAO->beginTransaction();
  589. $this->entryDAO->commitNewEntries();
  590. $this->feedDAO->updateCachedValues();
  591. $this->entryDAO->commit();
  592. $this->entryDAO->beginTransaction();
  593. foreach ($knownLabels as $labelName => $knownLabel) {
  594. $labelId = $knownLabel['id'];
  595. foreach ($knownLabel['articles'] as $article) {
  596. $entryId = $this->entryDAO->searchIdByGuid($article['id_feed'], $article['guid']);
  597. if ($entryId != null) {
  598. $tagDAO->tagEntry($labelId, $entryId);
  599. } else {
  600. Minz_Log::warning('Could not add label "' . $labelName . '" to entry "' . $article['guid'] . '" in feed ' . $article['id_feed']);
  601. }
  602. }
  603. }
  604. $this->entryDAO->commit();
  605. return !$error;
  606. }
  607. /**
  608. * This method import a JSON-based feed (Google Reader format).
  609. *
  610. * @param array $origin represents a feed.
  611. * @return FreshRSS_Feed if feed is in database at the end of the process,
  612. * else null.
  613. */
  614. private function addFeedJson($origin) {
  615. $return = null;
  616. if (!empty($origin['feedUrl'])) {
  617. $url = $origin['feedUrl'];
  618. } elseif (!empty($origin['htmlUrl'])) {
  619. $url = $origin['htmlUrl'];
  620. } else {
  621. return null;
  622. }
  623. if (!empty($origin['htmlUrl'])) {
  624. $website = $origin['htmlUrl'];
  625. } elseif (!empty($origin['feedUrl'])) {
  626. $website = $origin['feedUrl'];
  627. }
  628. $name = empty($origin['title']) ? '' : $origin['title'];
  629. try {
  630. // Create a Feed object and add it in database.
  631. $feed = new FreshRSS_Feed($url);
  632. $feed->_category(FreshRSS_CategoryDAO::DEFAULTCATEGORYID);
  633. $feed->_name($name);
  634. $feed->_website($website);
  635. if (!empty($origin['disable'])) {
  636. $feed->_ttl(-1 * FreshRSS_Context::$user_conf->ttl_default);
  637. }
  638. // Call the extension hook
  639. $feed = Minz_ExtensionManager::callHook('feed_before_insert', $feed);
  640. if ($feed != null) {
  641. // addFeedObject checks if feed is already in DB so nothing else to
  642. // check here.
  643. $id = $this->feedDAO->addFeedObject($feed);
  644. if ($id !== false) {
  645. $feed->_id($id);
  646. $return = $feed;
  647. }
  648. }
  649. } catch (FreshRSS_Feed_Exception $e) {
  650. if (FreshRSS_Context::$isCli) {
  651. fwrite(STDERR, 'FreshRSS error during JSON feed import: ' . $e->getMessage() . "\n");
  652. } else {
  653. Minz_Log::warning($e->getMessage());
  654. }
  655. }
  656. return $return;
  657. }
  658. /**
  659. * This action handles export action.
  660. *
  661. * This action must be reached by a POST request.
  662. *
  663. * Parameters are:
  664. * - export_opml (default: false)
  665. * - export_starred (default: false)
  666. * - export_labelled (default: false)
  667. * - export_feeds (default: array()) a list of feed ids
  668. */
  669. public function exportAction() {
  670. if (!Minz_Request::isPost()) {
  671. return Minz_Request::forward(
  672. array('c' => 'importExport', 'a' => 'index'),
  673. true
  674. );
  675. }
  676. $username = Minz_Session::param('currentUser');
  677. $export_service = new FreshRSS_Export_Service($username);
  678. $export_opml = Minz_Request::param('export_opml', false);
  679. $export_starred = Minz_Request::param('export_starred', false);
  680. $export_labelled = Minz_Request::param('export_labelled', false);
  681. $export_feeds = Minz_Request::param('export_feeds', array());
  682. $max_number_entries = 50;
  683. $exported_files = [];
  684. if ($export_opml) {
  685. list($filename, $content) = $export_service->generateOpml();
  686. $exported_files[$filename] = $content;
  687. }
  688. // Starred and labelled entries are merged in the same `starred` file
  689. // to avoid duplication of content.
  690. if ($export_starred && $export_labelled) {
  691. list($filename, $content) = $export_service->generateStarredEntries('ST');
  692. $exported_files[$filename] = $content;
  693. } elseif ($export_starred) {
  694. list($filename, $content) = $export_service->generateStarredEntries('S');
  695. $exported_files[$filename] = $content;
  696. } elseif ($export_labelled) {
  697. list($filename, $content) = $export_service->generateStarredEntries('T');
  698. $exported_files[$filename] = $content;
  699. }
  700. foreach ($export_feeds as $feed_id) {
  701. $result = $export_service->generateFeedEntries($feed_id, $max_number_entries);
  702. if (!$result) {
  703. // It means the actual feed_id doesn't correspond to any existing feed
  704. continue;
  705. }
  706. list($filename, $content) = $result;
  707. $exported_files[$filename] = $content;
  708. }
  709. $nb_files = count($exported_files);
  710. if ($nb_files <= 0) {
  711. // There's nothing to do, there're no files to export
  712. return Minz_Request::forward(
  713. array('c' => 'importExport', 'a' => 'index'),
  714. true
  715. );
  716. }
  717. if ($nb_files === 1) {
  718. // If we only have one file, we just export it as it is
  719. $filename = key($exported_files);
  720. $content = $exported_files[$filename];
  721. } else {
  722. // More files? Let's compress them in a Zip archive
  723. if (!extension_loaded('zip')) {
  724. // Oops, there is no ZIP extension!
  725. return Minz_Request::bad(
  726. _t('feedback.import_export.export_no_zip_extension'),
  727. array('c' => 'importExport', 'a' => 'index')
  728. );
  729. }
  730. list($filename, $content) = $export_service->zip($exported_files);
  731. }
  732. $content_type = self::filenameToContentType($filename);
  733. header('Content-Type: ' . $content_type);
  734. header('Content-disposition: attachment; filename="' . $filename . '"');
  735. $this->view->_layout(false);
  736. $this->view->content = $content;
  737. }
  738. /**
  739. * Return the Content-Type corresponding to a filename.
  740. *
  741. * If the type of the filename is not supported, it returns
  742. * `application/octet-stream` by default.
  743. *
  744. * @param string $filename
  745. *
  746. * @return string
  747. */
  748. private static function filenameToContentType($filename) {
  749. $filetype = self::guessFileType($filename);
  750. switch ($filetype) {
  751. case 'zip':
  752. return 'application/zip';
  753. case 'opml':
  754. return 'application/xml; charset=utf-8';
  755. case 'json_starred':
  756. case 'json_feed':
  757. return 'application/json; charset=utf-8';
  758. default:
  759. return 'application/octet-stream';
  760. }
  761. }
  762. }