importExportController.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Controller to handle every import and export actions.
  5. */
  6. class FreshRSS_importExport_Controller extends FreshRSS_ActionController {
  7. private FreshRSS_EntryDAO $entryDAO;
  8. private FreshRSS_FeedDAO $feedDAO;
  9. private FreshRSS_CategoryDAO $categoryDAO;
  10. /**
  11. * This action is called before every other action in that class. It is
  12. * the common boilerplate for every action. It is triggered by the
  13. * underlying framework.
  14. */
  15. #[\Override]
  16. public function firstAction(): void {
  17. if (!FreshRSS_Auth::hasAccess()) {
  18. Minz_Error::error(403);
  19. }
  20. $this->entryDAO = FreshRSS_Factory::createEntryDao();
  21. $this->feedDAO = FreshRSS_Factory::createFeedDao();
  22. $this->categoryDAO = FreshRSS_Factory::createCategoryDao();
  23. }
  24. /**
  25. * This action displays the main page for import / export system.
  26. */
  27. public function indexAction(): void {
  28. $this->view->feeds = $this->feedDAO->listFeeds();
  29. FreshRSS_View::prependTitle(_t('sub.import_export.title') . ' · ');
  30. $this->listSqliteArchives();
  31. }
  32. private static function megabytes(string $size_str): float|int|string {
  33. return match (substr($size_str, -1)) {
  34. 'M', 'm' => (int)$size_str,
  35. 'K', 'k' => (int)$size_str / 1024,
  36. 'G', 'g' => (int)$size_str * 1024,
  37. default => $size_str,
  38. };
  39. }
  40. private static function minimumMemory(int|string $mb): void {
  41. $mb = (int)$mb;
  42. $ini = self::megabytes(ini_get('memory_limit') ?: '0');
  43. if ($ini < $mb) {
  44. ini_set('memory_limit', $mb . 'M');
  45. }
  46. }
  47. /**
  48. * @throws FreshRSS_Zip_Exception
  49. * @throws FreshRSS_ZipMissing_Exception
  50. * @throws Minz_ConfigurationNamespaceException
  51. * @throws Minz_PDOConnectionException
  52. */
  53. public function importFile(string $name, string $path, ?string $username = null): bool {
  54. self::minimumMemory(256);
  55. $this->entryDAO = FreshRSS_Factory::createEntryDao($username);
  56. $this->feedDAO = FreshRSS_Factory::createFeedDao($username);
  57. $this->categoryDAO = FreshRSS_Factory::createCategoryDao($username);
  58. $type_file = self::guessFileType($name);
  59. $list_files = [
  60. 'opml' => [],
  61. 'json_starred' => [],
  62. 'json_feed' => [],
  63. 'ttrss_starred' => [],
  64. ];
  65. // We try to list all files according to their type
  66. $list = [];
  67. if ('zip' === $type_file && extension_loaded('zip')) {
  68. $zip = new ZipArchive();
  69. $result = $zip->open($path);
  70. if (true !== $result) {
  71. // zip_open cannot open file: something is wrong
  72. throw new FreshRSS_Zip_Exception($result);
  73. }
  74. for ($i = 0; $i < $zip->numFiles; $i++) {
  75. if ($zip->getNameIndex($i) === false) {
  76. continue;
  77. }
  78. $type_zipfile = self::guessFileType($zip->getNameIndex($i));
  79. if ('unknown' !== $type_zipfile) {
  80. $list_files[$type_zipfile][] = $zip->getFromIndex($i);
  81. }
  82. }
  83. $zip->close();
  84. } elseif ('zip' === $type_file) {
  85. // ZIP extension is not loaded
  86. throw new FreshRSS_ZipMissing_Exception();
  87. } elseif ('txt' === $type_file) {
  88. $contents = file_get_contents($path);
  89. if (is_string($contents)) {
  90. $list_files['opml'][] = self::txtToOpml($contents);
  91. }
  92. } elseif ('unknown' !== $type_file) {
  93. $list_files[$type_file][] = file_get_contents($path);
  94. }
  95. // Import file contents.
  96. // OPML first(so categories and feeds are imported)
  97. // Starred articles then so the "favourite" status is already set
  98. // And finally all other files.
  99. $ok = true;
  100. $importService = new FreshRSS_Import_Service($username);
  101. foreach ($list_files['opml'] as $opml_file) {
  102. if ($opml_file === false) {
  103. continue;
  104. }
  105. $importService->importOpml($opml_file);
  106. if (!$importService->lastStatus()) {
  107. $ok = false;
  108. if (FreshRSS_Context::$isCli) {
  109. fwrite(STDERR, 'FreshRSS error during OPML import' . "\n");
  110. } else {
  111. Minz_Log::warning('Error during OPML import');
  112. }
  113. }
  114. }
  115. foreach ($list_files['json_starred'] as $article_file) {
  116. if (!is_string($article_file) || !$this->importJson($article_file, true)) {
  117. $ok = false;
  118. if (FreshRSS_Context::$isCli) {
  119. fwrite(STDERR, 'FreshRSS error during JSON stars import' . "\n");
  120. } else {
  121. Minz_Log::warning('Error during JSON stars import');
  122. }
  123. }
  124. }
  125. foreach ($list_files['json_feed'] as $article_file) {
  126. if (!is_string($article_file) || !$this->importJson($article_file)) {
  127. $ok = false;
  128. if (FreshRSS_Context::$isCli) {
  129. fwrite(STDERR, 'FreshRSS error during JSON feeds import' . "\n");
  130. } else {
  131. Minz_Log::warning('Error during JSON feeds import');
  132. }
  133. }
  134. }
  135. foreach ($list_files['ttrss_starred'] as $article_file) {
  136. $json = is_string($article_file) ? $this->ttrssXmlToJson($article_file) : false;
  137. if ($json === false || !$this->importJson($json, true)) {
  138. $ok = false;
  139. if (FreshRSS_Context::$isCli) {
  140. fwrite(STDERR, 'FreshRSS error during TT-RSS articles import' . "\n");
  141. } else {
  142. Minz_Log::warning('Error during TT-RSS articles import');
  143. }
  144. }
  145. }
  146. return $ok;
  147. }
  148. /**
  149. * This action handles import action.
  150. *
  151. * It must be reached by a POST request.
  152. *
  153. * Parameter is:
  154. * - file (default: nothing!)
  155. * Available file types are: zip, json or xml.
  156. */
  157. public function importAction(): void {
  158. if (!Minz_Request::isPost()) {
  159. Minz_Request::forward(['c' => 'importExport', 'a' => 'index'], true);
  160. }
  161. $file = $_FILES['file'] ?? null;
  162. $status_file = is_array($file) ? $file['error'] ?? -1 : -1;
  163. if (!is_array($file) || $status_file !== 0 || !is_string($file['name'] ?? null) || !is_string($file['tmp_name'] ?? null)) {
  164. Minz_Log::warning('File cannot be uploaded. Error code: ' . (is_numeric($status_file) ? $status_file : -1));
  165. Minz_Request::bad(_t('feedback.import_export.file_cannot_be_uploaded'), [ 'c' => 'importExport', 'a' => 'index' ]);
  166. return;
  167. }
  168. if (function_exists('set_time_limit')) {
  169. @set_time_limit(300);
  170. }
  171. $error = false;
  172. try {
  173. $error = !$this->importFile($file['name'], $file['tmp_name']);
  174. } catch (FreshRSS_ZipMissing_Exception) {
  175. Minz_Request::bad(
  176. _t('feedback.import_export.no_zip_extension'),
  177. ['c' => 'importExport', 'a' => 'index']
  178. );
  179. } catch (FreshRSS_Zip_Exception $ze) {
  180. Minz_Log::warning('ZIP archive cannot be imported. Error code: ' . $ze->zipErrorCode());
  181. Minz_Request::bad(
  182. _t('feedback.import_export.zip_error'),
  183. ['c' => 'importExport', 'a' => 'index']
  184. );
  185. }
  186. // And finally, we get import status and redirect to the home page
  187. $content_notif = $error === true ? _t('feedback.import_export.feeds_imported_with_errors') : _t('feedback.import_export.feeds_imported');
  188. Minz_Request::good(
  189. $content_notif,
  190. showNotification: FreshRSS_Context::userConf()->good_notification_timeout > 0
  191. );
  192. }
  193. /**
  194. * This method tries to guess the file type based on its name.
  195. *
  196. * It is a *very* basic guess file type function. Only based on filename.
  197. * That could be improved but should be enough for what we have to do.
  198. */
  199. private static function guessFileType(string $filename): string {
  200. if (str_ends_with($filename, '.zip')) {
  201. return 'zip';
  202. } elseif (str_ends_with($filename, '.txt')) {
  203. return 'txt';
  204. } elseif (stripos($filename, 'opml') !== false) {
  205. return 'opml';
  206. } elseif (str_ends_with($filename, '.json')) {
  207. if (str_contains($filename, 'starred')) {
  208. return 'json_starred';
  209. } else {
  210. return 'json_feed';
  211. }
  212. } elseif (str_ends_with($filename, '.xml')) {
  213. if (preg_match('/Tiny|tt-?rss/i', $filename)) {
  214. return 'ttrss_starred';
  215. } else {
  216. return 'opml';
  217. }
  218. }
  219. return 'unknown';
  220. }
  221. /**
  222. * Wraps a newline-separated list of feed URLs into a minimal OPML document
  223. * so it can be imported through the existing OPML pipeline.
  224. */
  225. private static function txtToOpml(string $contents): string {
  226. $utf8BOM = "\xEF\xBB\xBF";
  227. $contents = preg_replace('/^' . $utf8BOM . '/', '', $contents) ?? $contents;
  228. $outlines = '';
  229. foreach (preg_split('/\R/', $contents) ?: [] as $line) {
  230. $url = trim($line);
  231. if ($url === '' || str_starts_with($url, '#')) {
  232. continue;
  233. }
  234. if (filter_var($url, FILTER_VALIDATE_URL) === false) {
  235. $message = 'TXT import: skipping invalid URL “' . $url . '”';
  236. if (FreshRSS_Context::$isCli) {
  237. fwrite(STDERR, $message . "\n");
  238. } else {
  239. Minz_Log::warning($message);
  240. }
  241. continue;
  242. }
  243. $escaped = htmlspecialchars($url, ENT_COMPAT | ENT_XML1, 'UTF-8');
  244. $outlines .= '<outline type="rss" text="' . $escaped . '" xmlUrl="' . $escaped . '" />' . "\n";
  245. }
  246. return '<?xml version="1.0" encoding="UTF-8"?>' . "\n"
  247. . '<opml version="2.0"><body>' . "\n"
  248. . $outlines
  249. . '</body></opml>' . "\n";
  250. }
  251. private function ttrssXmlToJson(string $xml): string|false {
  252. $table = (array)simplexml_load_string($xml, options: LIBXML_NOBLANKS | LIBXML_NOCDATA);
  253. $table['items'] = $table['article'] ?? [];
  254. if (!is_array($table['items'])) {
  255. $table['items'] = [];
  256. }
  257. unset($table['article']);
  258. for ($i = count($table['items']) - 1; $i >= 0; $i--) {
  259. $item = (array)($table['items'][$i]);
  260. $item = array_filter($item, static fn($v) =>
  261. // Filter out empty properties, potentially reported as empty objects
  262. (is_string($v) && trim($v) !== '') || !empty($v));
  263. $item['updated'] = is_string($item['updated'] ?? null) ? strtotime($item['updated']) : '';
  264. $item['published'] = $item['updated'];
  265. $item['content'] = ['content' => $item['content'] ?? ''];
  266. $item['categories'] = is_string($item['tag_cache'] ?? null) ? [$item['tag_cache']] : [];
  267. if (!empty($item['marked'])) {
  268. $item['categories'][] = 'user/-/state/com.google/starred';
  269. }
  270. if (!empty($item['published'])) {
  271. $item['categories'][] = 'user/-/state/com.google/broadcast';
  272. }
  273. if (is_string($item['label_cache'] ?? null)) {
  274. $labels_cache = json_decode($item['label_cache'], true);
  275. if (is_array($labels_cache)) {
  276. foreach ($labels_cache as $label_cache) {
  277. if (is_array($label_cache) && !empty($label_cache[1]) && is_string($label_cache[1])) {
  278. $item['categories'][] = 'user/-/label/' . trim($label_cache[1]);
  279. }
  280. }
  281. }
  282. }
  283. $item['alternate'] = [['href' => $item['link'] ?? '']];
  284. $item['origin'] = [
  285. 'title' => $item['feed_title'] ?? '',
  286. 'feedUrl' => $item['feed_url'] ?? '',
  287. ];
  288. $item['id'] = $item['guid'] ?? ($item['feed_url'] ?? $item['published']);
  289. $item['guid'] = $item['id'];
  290. $table['items'][$i] = $item;
  291. }
  292. return json_encode($table);
  293. }
  294. /**
  295. * This method import a JSON-based file (Google Reader format).
  296. *
  297. * $article_file the JSON file content.
  298. * true if articles from the file must be starred.
  299. * @return bool false if an error occurred, true otherwise.
  300. * @throws Minz_ConfigurationNamespaceException
  301. * @throws Minz_PDOConnectionException
  302. */
  303. private function importJson(string $article_file, bool $starred = false): bool {
  304. $article_object = json_decode($article_file, true);
  305. if (!is_array($article_object)) {
  306. if (FreshRSS_Context::$isCli) {
  307. fwrite(STDERR, 'FreshRSS error trying to import a non-JSON file' . "\n");
  308. } else {
  309. Minz_Log::warning('Try to import a non-JSON file');
  310. }
  311. return false;
  312. }
  313. $items = $article_object['items'] ?? $article_object;
  314. if (!is_array($items)) {
  315. $items = [];
  316. }
  317. $mark_as_read = FreshRSS_Context::userConf()->mark_when['reception'] ? 1 : 0;
  318. $error = false;
  319. $article_to_feed = [];
  320. $nb_feeds = count($this->feedDAO->listFeeds());
  321. $newFeedGuids = [];
  322. $limits = FreshRSS_Context::systemConf()->limits;
  323. // First, we check feeds of articles are in DB (and add them if needed).
  324. foreach ($items as &$item) {
  325. if (!is_array($item)) {
  326. continue;
  327. }
  328. if (!is_string($item['guid'] ?? null) && is_string($item['id'] ?? null)) {
  329. $item['guid'] = $item['id'];
  330. }
  331. if (!is_string($item['guid'] ?? null)) {
  332. continue;
  333. }
  334. if (!is_array($item['origin'] ?? null)) {
  335. $item['origin'] = [];
  336. }
  337. if (!is_string($item['origin']['title'] ?? null) || trim($item['origin']['title']) === '') {
  338. $item['origin']['title'] = 'Import';
  339. }
  340. if (is_string($item['origin']['feedUrl'] ?? null)) {
  341. $feedUrl = $item['origin']['feedUrl'];
  342. } elseif (is_string($item['origin']['streamId'] ?? null) && str_starts_with($item['origin']['streamId'], 'feed/')) {
  343. $feedUrl = substr($item['origin']['streamId'], 5); //Google Reader
  344. $item['origin']['feedUrl'] = $feedUrl;
  345. } elseif (is_string($item['origin']['htmlUrl'] ?? null)) {
  346. $feedUrl = $item['origin']['htmlUrl'];
  347. } else {
  348. $feedUrl = 'http://import.localhost/import.xml';
  349. $item['origin']['feedUrl'] = $feedUrl;
  350. $item['origin']['disable'] = 'true';
  351. }
  352. $feed = new FreshRSS_Feed($feedUrl);
  353. $feed = $this->feedDAO->searchByUrl($feed->url());
  354. if ($feed === null) {
  355. // Feed does not exist in DB,we should to try to add it.
  356. if ((!FreshRSS_Context::$isCli) && ($nb_feeds >= $limits['max_feeds'])) {
  357. // Oops, no more place!
  358. Minz_Log::warning(_t('feedback.sub.feed.over_max', $limits['max_feeds']));
  359. } else {
  360. $origin = array_filter($item['origin'], fn($value, $key): bool => is_string($key) && is_string($value), ARRAY_FILTER_USE_BOTH);
  361. $feed = $this->addFeedJson($origin);
  362. }
  363. if ($feed === null) {
  364. // Still null? It means something went wrong.
  365. $error = true;
  366. } else {
  367. $nb_feeds++;
  368. }
  369. }
  370. if ($feed !== null) {
  371. $article_to_feed[$item['guid']] = $feed->id();
  372. if (!isset($newFeedGuids['f_' . $feed->id()])) {
  373. $newFeedGuids['f_' . $feed->id()] = [];
  374. }
  375. $newFeedGuids['f_' . $feed->id()][] = safe_ascii($item['guid']);
  376. }
  377. }
  378. $tagDAO = FreshRSS_Factory::createTagDao();
  379. $labels = FreshRSS_Context::labels();
  380. $knownLabels = [];
  381. foreach ($labels as $label) {
  382. $knownLabels[$label->name()]['id'] = $label->id();
  383. $knownLabels[$label->name()]['articles'] = [];
  384. }
  385. unset($labels);
  386. // For each feed, check existing GUIDs already in database.
  387. $existingHashForGuids = [];
  388. foreach ($newFeedGuids as $feedId => $newGuids) {
  389. $existingHashForGuids[$feedId] = $this->entryDAO->listHashForFeedGuids((int)substr($feedId, 2), $newGuids);
  390. }
  391. unset($newFeedGuids);
  392. // Then, articles are imported.
  393. $newGuids = [];
  394. $this->entryDAO->beginTransaction();
  395. foreach ($items as &$item) {
  396. if (!is_array($item) || empty($item['guid']) || !is_string($item['guid']) || empty($article_to_feed[$item['guid']])) {
  397. // Related feed does not exist for this entry, do nothing.
  398. continue;
  399. }
  400. $feed_id = $article_to_feed[$item['guid']];
  401. $author = is_string($item['author'] ?? null) ? $item['author'] : '';
  402. $is_starred = null; // null is used to preserve the current state if that item exists and is already starred
  403. $is_read = null;
  404. $tags = is_array($item['categories'] ?? null) ? $item['categories'] : [];
  405. $labels = [];
  406. for ($i = count($tags) - 1; $i >= 0; $i--) {
  407. $tag = $tags[$i];
  408. if (!is_string($tag)) {
  409. unset($tags[$i]);
  410. continue;
  411. }
  412. $tag = trim($tag);
  413. if (preg_match('%^user/[A-Za-z0-9_-]+/%', $tag)) {
  414. if (preg_match('%^user/[A-Za-z0-9_-]+/state/com.google/starred$%', $tag)) {
  415. $is_starred = true;
  416. } elseif (preg_match('%^user/[A-Za-z0-9_-]+/state/com.google/read$%', $tag)) {
  417. $is_read = true;
  418. } elseif (preg_match('%^user/[A-Za-z0-9_-]+/state/com.google/unread$%', $tag)) {
  419. $is_read = false;
  420. } elseif (preg_match('%^user/[A-Za-z0-9_-]+/label/\s*(?P<tag>.+?)\s*$%', $tag, $matches)) {
  421. $labels[] = $matches['tag'];
  422. }
  423. unset($tags[$i]);
  424. }
  425. }
  426. $tags = array_values(array_filter($tags, 'is_string'));
  427. if ($starred && !$is_starred) {
  428. //If the article has no label, mark it as starred (old format)
  429. $is_starred = empty($labels);
  430. }
  431. if ($is_read === null) {
  432. $is_read = $mark_as_read;
  433. }
  434. if (is_array($item['alternate']) && is_array($item['alternate'][0] ?? null) && is_string($item['alternate'][0]['href'] ?? null)) {
  435. $url = $item['alternate'][0]['href'];
  436. } elseif (is_string($item['url'] ?? null)) {
  437. $url = $item['url']; //FeedBin
  438. } else {
  439. $url = '';
  440. }
  441. $title = is_string($item['title'] ?? null) ? $item['title'] : $url;
  442. if (is_array($item['content'] ?? null) && is_string($item['content']['content'] ?? null)) {
  443. $content = $item['content']['content'];
  444. } elseif (is_array($item['summary']) && is_string($item['summary']['content'] ?? null)) {
  445. $content = $item['summary']['content'];
  446. } elseif (is_string($item['content'] ?? null)) {
  447. $content = $item['content']; //FeedBin
  448. } else {
  449. $content = '';
  450. }
  451. $content = FreshRSS_SimplePieCustom::sanitizeHTML($content, $url);
  452. if (is_int($item['published'] ?? null) || is_string($item['published'] ?? null)) {
  453. $published = (string)$item['published'];
  454. } elseif (is_int($item['timestampUsec'] ?? null) || is_string($item['timestampUsec'] ?? null)) {
  455. $published = substr((string)$item['timestampUsec'], 0, -6);
  456. } elseif (is_int($item['updated'] ?? null) || is_string($item['updated'] ?? null)) {
  457. $published = (string)$item['updated'];
  458. } else {
  459. $published = '0';
  460. }
  461. if (!ctype_digit($published)) {
  462. $published = (string)(strtotime($published) ?: 0);
  463. }
  464. if (strlen($published) > 10) { // Milliseconds, e.g. Feedly
  465. $published = substr($published, 0, -3);
  466. if (!is_numeric($published)) {
  467. $published = '0'; // For PHPStan
  468. }
  469. }
  470. $entry = new FreshRSS_Entry(
  471. $feed_id, $item['guid'], $title, $author,
  472. $content, $url, $published, $is_read, $is_starred
  473. );
  474. $entry->_id(uTimeString());
  475. $entry->_tags($tags);
  476. if (isset($newGuids[$entry->guid()])) {
  477. continue; //Skip subsequent articles with same GUID
  478. }
  479. $newGuids[$entry->guid()] = true;
  480. $entry = Minz_ExtensionManager::callHook(Minz_HookType::EntryBeforeInsert, $entry);
  481. if (!($entry instanceof FreshRSS_Entry)) {
  482. // An extension has returned a null value, there is nothing to insert.
  483. continue;
  484. }
  485. if (isset($existingHashForGuids['f_' . $feed_id][$entry->guid()])) {
  486. $entry = Minz_ExtensionManager::callHook(Minz_HookType::EntryBeforeUpdate, $entry);
  487. if (!($entry instanceof FreshRSS_Entry)) {
  488. // An extension has returned a null value, there is nothing to insert.
  489. continue;
  490. }
  491. $ok = $this->entryDAO->updateEntry($entry->toArray());
  492. } else {
  493. $entry->_lastSeen(time());
  494. $entry = Minz_ExtensionManager::callHook(Minz_HookType::EntryBeforeAdd, $entry);
  495. if (!($entry instanceof FreshRSS_Entry)) {
  496. // An extension has returned a null value, there is nothing to insert.
  497. continue;
  498. }
  499. $ok = $this->entryDAO->addEntry($entry->toArray());
  500. }
  501. foreach ($labels as $labelName) {
  502. if (empty($knownLabels[$labelName]['id'])) {
  503. $labelId = $tagDAO->addTag(['name' => $labelName]);
  504. $knownLabels[$labelName]['id'] = $labelId;
  505. $knownLabels[$labelName]['articles'] = [];
  506. }
  507. $knownLabels[$labelName]['articles'][] = [
  508. //'id' => $entry->id(), //ID changes after commitNewEntries()
  509. 'id_feed' => $entry->feedId(),
  510. 'guid' => $entry->guid(),
  511. ];
  512. }
  513. $error |= ($ok === false);
  514. }
  515. $this->entryDAO->commit();
  516. $this->entryDAO->beginTransaction();
  517. $this->entryDAO->commitNewEntries();
  518. $this->feedDAO->updateCachedValues();
  519. $this->entryDAO->commit();
  520. $this->entryDAO->beginTransaction();
  521. foreach ($knownLabels as $labelName => $knownLabel) {
  522. $labelId = $knownLabel['id'];
  523. if (!$labelId) {
  524. continue;
  525. }
  526. foreach ($knownLabel['articles'] as $article) {
  527. $entryId = $this->entryDAO->searchIdByGuid($article['id_feed'], $article['guid']);
  528. if ($entryId != null) {
  529. $tagDAO->tagEntry($labelId, $entryId);
  530. } else {
  531. Minz_Log::warning('Could not add label "' . $labelName . '" to entry "' . $article['guid'] . '" in feed ' . $article['id_feed']);
  532. }
  533. }
  534. }
  535. $this->entryDAO->commit();
  536. return !$error;
  537. }
  538. /**
  539. * This method import a JSON-based feed (Google Reader format).
  540. *
  541. * @param array<string,string> $origin represents a feed.
  542. * @return FreshRSS_Feed|null if feed is in database at the end of the process, else null.
  543. */
  544. private function addFeedJson(array $origin): ?FreshRSS_Feed {
  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. } else {
  558. $website = '';
  559. }
  560. $name = empty($origin['title']) ? $website : $origin['title'];
  561. $cat_id = FreshRSS_CategoryDAO::DEFAULTCATEGORYID;
  562. $cat_name = trim($origin['category'] ?? '');
  563. if ($cat_name !== '') {
  564. $new_cat = $this->categoryDAO->searchByName($cat_name);
  565. $cat_id = $new_cat?->id() ?: $this->categoryDAO->addCategory(['name' => $cat_name]) ?: FreshRSS_CategoryDAO::DEFAULTCATEGORYID;
  566. }
  567. try {
  568. // Create a Feed object and add it in database.
  569. $feed = new FreshRSS_Feed($url);
  570. $feed->_categoryId($cat_id);
  571. $feed->_name($name);
  572. $feed->_website($website);
  573. if (!empty($origin['disable'])) {
  574. $feed->_mute(true);
  575. }
  576. // Call the extension hook
  577. $feed = Minz_ExtensionManager::callHook(Minz_HookType::FeedBeforeInsert, $feed);
  578. if ($feed instanceof FreshRSS_Feed) {
  579. // addFeedObject checks if feed is already in DB so nothing else to
  580. // check here.
  581. $id = $this->feedDAO->addFeedObject($feed);
  582. if ($id !== false) {
  583. $feed->_id($id);
  584. $return = $feed;
  585. }
  586. }
  587. } catch (FreshRSS_Feed_Exception $e) {
  588. if (FreshRSS_Context::$isCli) {
  589. fwrite(STDERR, 'FreshRSS error during JSON feed import: ' . $e->getMessage() . "\n");
  590. } else {
  591. Minz_Log::warning($e->getMessage());
  592. }
  593. }
  594. return $return;
  595. }
  596. /**
  597. * This action handles export action.
  598. *
  599. * This action must be reached by a POST request.
  600. *
  601. * Parameters are:
  602. * - export_opml (default: false)
  603. * - export_starred (default: false)
  604. * - export_labelled (default: false)
  605. * - export_feeds (default: []) a list of feed ids
  606. */
  607. public function exportAction(): void {
  608. if (!Minz_Request::isPost()) {
  609. Minz_Request::forward(['c' => 'importExport', 'a' => 'index'], true);
  610. return;
  611. }
  612. $username = Minz_User::name() ?? '_';
  613. $export_service = new FreshRSS_Export_Service($username);
  614. $export_opml = Minz_Request::paramBoolean('export_opml');
  615. $export_starred = Minz_Request::paramBoolean('export_starred');
  616. $export_labelled = Minz_Request::paramBoolean('export_labelled');
  617. /** @var array<numeric-string> */
  618. $export_feeds = Minz_Request::paramArray('export_feeds');
  619. $max_number_entries = 50;
  620. $exported_files = [];
  621. if ($export_opml) {
  622. [$filename, $content] = $export_service->generateOpml();
  623. $exported_files[$filename] = $content;
  624. }
  625. // Starred and labelled entries are merged in the same `starred` file
  626. // to avoid duplication of content.
  627. if ($export_starred && $export_labelled) {
  628. [$filename, $content] = $export_service->generateStarredEntries('ST');
  629. $exported_files[$filename] = $content;
  630. } elseif ($export_starred) {
  631. [$filename, $content] = $export_service->generateStarredEntries('S');
  632. $exported_files[$filename] = $content;
  633. } elseif ($export_labelled) {
  634. [$filename, $content] = $export_service->generateStarredEntries('T');
  635. $exported_files[$filename] = $content;
  636. }
  637. foreach ($export_feeds as $feed_id) {
  638. $result = $export_service->generateFeedEntries((int)$feed_id, $max_number_entries);
  639. if ($result === null) {
  640. // It means the actual feed_id doesn’t correspond to any existing feed
  641. continue;
  642. }
  643. [$filename, $content] = $result;
  644. $exported_files[$filename] = $content;
  645. }
  646. $nb_files = count($exported_files);
  647. if ($nb_files <= 0) {
  648. // There’s nothing to do, there are no files to export
  649. Minz_Request::forward(['c' => 'importExport', 'a' => 'index'], true);
  650. return;
  651. }
  652. if ($nb_files === 1) {
  653. // If we only have one file, we just export it as it is
  654. $filename = key($exported_files);
  655. $content = $exported_files[$filename];
  656. } else {
  657. // More files? Let’s compress them in a Zip archive
  658. if (!extension_loaded('zip')) {
  659. // Oops, there is no ZIP extension!
  660. Minz_Request::bad(
  661. _t('feedback.import_export.export_no_zip_extension'),
  662. ['c' => 'importExport', 'a' => 'index']
  663. );
  664. return;
  665. }
  666. [$filename, $content] = $export_service->zip($exported_files);
  667. }
  668. if (!is_string($content)) {
  669. Minz_Request::bad(_t('feedback.import_export.zip_error'), ['c' => 'importExport', 'a' => 'index']);
  670. return;
  671. }
  672. $content_type = self::filenameToContentType($filename);
  673. header('Content-Type: ' . $content_type);
  674. header('Content-disposition: attachment; filename="' . $filename . '"');
  675. $this->view->_layout(null);
  676. $this->view->content = $content;
  677. }
  678. /**
  679. * Return the Content-Type corresponding to a filename.
  680. *
  681. * If the type of the filename is not supported, it returns
  682. * `application/octet-stream` by default.
  683. */
  684. private static function filenameToContentType(string $filename): string {
  685. $filetype = self::guessFileType($filename);
  686. return match ($filetype) {
  687. 'zip' => 'application/zip',
  688. 'opml' => 'application/xml; charset=utf-8',
  689. 'json_starred', 'json_feed' => 'application/json; charset=utf-8',
  690. default => 'application/octet-stream',
  691. };
  692. }
  693. private const REGEX_SQLITE_FILENAME = '/^(?![.-])[0-9a-zA-Z_.@ #&()~\-]{1,128}\.sqlite$/';
  694. private function listSqliteArchives(): void {
  695. $this->view->sqliteArchives = [];
  696. $files = glob(USERS_PATH . '/' . Minz_User::name() . '/*.sqlite', GLOB_NOSORT) ?: [];
  697. foreach ($files as $file) {
  698. $archive = [
  699. 'name' => basename($file),
  700. 'size' => @filesize($file),
  701. 'mtime' => @filemtime($file),
  702. ];
  703. if ($archive['size'] != false && $archive['mtime'] != false && preg_match(self::REGEX_SQLITE_FILENAME, $archive['name'])) {
  704. $this->view->sqliteArchives[] = $archive;
  705. }
  706. }
  707. // Sort by time, newest first:
  708. usort($this->view->sqliteArchives, static fn(array $a, array $b): int => $b['mtime'] <=> $a['mtime']);
  709. }
  710. public function sqliteAction(): void {
  711. if (!Minz_Request::isPost()) {
  712. Minz_Request::forward(['c' => 'importExport', 'a' => 'index'], true);
  713. }
  714. $sqlite = Minz_Request::paramString('sqlite');
  715. if (!preg_match(self::REGEX_SQLITE_FILENAME, $sqlite)) {
  716. Minz_Error::error(404);
  717. return;
  718. }
  719. $path = USERS_PATH . '/' . Minz_User::name() . '/' . $sqlite;
  720. if (!file_exists($path) || @filesize($path) == false || @filemtime($path) == false) {
  721. Minz_Error::error(404);
  722. return;
  723. }
  724. $this->view->sqlitePath = $path;
  725. $this->view->sqliteName = basename($path);
  726. if ($this->view->sqliteName === 'db.sqlite') {
  727. $username = Minz_User::name() ?? '_';
  728. $date = date('Y-m-d_H-i-s', filemtime($path) ?: time());
  729. $this->view->sqliteName = 'freshrss_' . $username . '_' . $date . '_db.sqlite';
  730. }
  731. $this->view->_layout(null);
  732. }
  733. }