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