importExportController.php 23 KB

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