aboutsummaryrefslogtreecommitdiffhomepage
path: root/application
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-06-17 19:08:02 +0200
committerArthurHoaro <arthur@hoa.ro>2020-07-23 21:19:21 +0200
commit78657347c5b463d7c22bfc8c87b7db39fe058833 (patch)
treea61c513ce05a597119cb5e9e65a33a94e01663d9 /application
parentc70ff64a61d62cc8d35a62f30596ecc2a3c578a3 (diff)
downloadShaarli-78657347c5b463d7c22bfc8c87b7db39fe058833.tar.gz
Shaarli-78657347c5b463d7c22bfc8c87b7db39fe058833.tar.zst
Shaarli-78657347c5b463d7c22bfc8c87b7db39fe058833.zip
Process bookmarks import through Slim controller
Diffstat (limited to 'application')
-rw-r--r--application/front/controller/admin/ExportController.php17
-rw-r--r--application/front/controller/admin/ImportController.php81
-rw-r--r--application/netscape/NetscapeBookmarkUtils.php15
3 files changed, 91 insertions, 22 deletions
diff --git a/application/front/controller/admin/ExportController.php b/application/front/controller/admin/ExportController.php
index 8e0e5a56..7afbfc23 100644
--- a/application/front/controller/admin/ExportController.php
+++ b/application/front/controller/admin/ExportController.php
@@ -33,6 +33,8 @@ class ExportController extends ShaarliAdminController
33 */ 33 */
34 public function export(Request $request, Response $response): Response 34 public function export(Request $request, Response $response): Response
35 { 35 {
36 $this->checkToken($request);
37
36 $selection = $request->getParam('selection'); 38 $selection = $request->getParam('selection');
37 39
38 if (empty($selection)) { 40 if (empty($selection)) {
@@ -74,19 +76,4 @@ class ExportController extends ShaarliAdminController
74 76
75 return $response->write($this->render('export.bookmarks')); 77 return $response->write($this->render('export.bookmarks'));
76 } 78 }
77
78 /**
79 * @param mixed[] $data Variables passed to the template engine
80 *
81 * @return mixed[] Template data after active plugins render_picwall hook execution.
82 */
83 protected function executeHooks(array $data): array
84 {
85 $this->container->pluginManager->executeHooks(
86 'render_tools',
87 $data
88 );
89
90 return $data;
91 }
92} 79}
diff --git a/application/front/controller/admin/ImportController.php b/application/front/controller/admin/ImportController.php
new file mode 100644
index 00000000..8c5305b9
--- /dev/null
+++ b/application/front/controller/admin/ImportController.php
@@ -0,0 +1,81 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller\Admin;
6
7use Psr\Http\Message\UploadedFileInterface;
8use Slim\Http\Request;
9use Slim\Http\Response;
10
11/**
12 * Class ImportController
13 *
14 * Slim controller used to display Shaarli data import page,
15 * and import bookmarks from Netscape Bookmarks file.
16 */
17class ImportController extends ShaarliAdminController
18{
19 /**
20 * GET /admin/import - Display import page
21 */
22 public function index(Request $request, Response $response): Response
23 {
24 $this->assignView(
25 'maxfilesize',
26 get_max_upload_size(
27 ini_get('post_max_size'),
28 ini_get('upload_max_filesize'),
29 false
30 )
31 );
32 $this->assignView(
33 'maxfilesizeHuman',
34 get_max_upload_size(
35 ini_get('post_max_size'),
36 ini_get('upload_max_filesize'),
37 true
38 )
39 );
40 $this->assignView('pagetitle', t('Import') .' - '. $this->container->conf->get('general.title', 'Shaarli'));
41
42 return $response->write($this->render('import'));
43 }
44
45 /**
46 * POST /admin/import - Process import file provided and create bookmarks
47 */
48 public function import(Request $request, Response $response): Response
49 {
50 $this->checkToken($request);
51
52 $file = ($request->getUploadedFiles() ?? [])['filetoupload'] ?? null;
53 if (!$file instanceof UploadedFileInterface) {
54 $this->saveErrorMessage(t('No import file provided.'));
55
56 return $this->redirect($response, '/admin/import');
57 }
58
59
60 // Import bookmarks from an uploaded file
61 if (0 === $file->getSize()) {
62 // The file is too big or some form field may be missing.
63 $msg = sprintf(
64 t(
65 'The file you are trying to upload is probably bigger than what this webserver can accept'
66 .' (%s). Please upload in smaller chunks.'
67 ),
68 get_max_upload_size(ini_get('post_max_size'), ini_get('upload_max_filesize'))
69 );
70 $this->saveErrorMessage($msg);
71
72 return $this->redirect($response, '/admin/import');
73 }
74
75 $status = $this->container->netscapeBookmarkUtils->import($request->getParams(), $file);
76
77 $this->saveSuccessMessage($status);
78
79 return $this->redirect($response, '/admin/import');
80 }
81}
diff --git a/application/netscape/NetscapeBookmarkUtils.php b/application/netscape/NetscapeBookmarkUtils.php
index 8557cca2..b150f649 100644
--- a/application/netscape/NetscapeBookmarkUtils.php
+++ b/application/netscape/NetscapeBookmarkUtils.php
@@ -6,6 +6,7 @@ use DateTime;
6use DateTimeZone; 6use DateTimeZone;
7use Exception; 7use Exception;
8use Katzgrau\KLogger\Logger; 8use Katzgrau\KLogger\Logger;
9use Psr\Http\Message\UploadedFileInterface;
9use Psr\Log\LogLevel; 10use Psr\Log\LogLevel;
10use Shaarli\Bookmark\Bookmark; 11use Shaarli\Bookmark\Bookmark;
11use Shaarli\Bookmark\BookmarkServiceInterface; 12use Shaarli\Bookmark\BookmarkServiceInterface;
@@ -79,20 +80,20 @@ class NetscapeBookmarkUtils
79 /** 80 /**
80 * Imports Web bookmarks from an uploaded Netscape bookmark dump 81 * Imports Web bookmarks from an uploaded Netscape bookmark dump
81 * 82 *
82 * @param array $post Server $_POST parameters 83 * @param array $post Server $_POST parameters
83 * @param array $files Server $_FILES parameters 84 * @param UploadedFileInterface $file File in PSR-7 object format
84 * 85 *
85 * @return string Summary of the bookmark import status 86 * @return string Summary of the bookmark import status
86 */ 87 */
87 public function import($post, $files) 88 public function import($post, UploadedFileInterface $file)
88 { 89 {
89 $start = time(); 90 $start = time();
90 $filename = $files['filetoupload']['name']; 91 $filename = $file->getClientFilename();
91 $filesize = $files['filetoupload']['size']; 92 $filesize = $file->getSize();
92 $data = file_get_contents($files['filetoupload']['tmp_name']); 93 $data = (string) $file->getStream();
93 94
94 if (preg_match('/<!DOCTYPE NETSCAPE-Bookmark-file-1>/i', $data) === 0) { 95 if (preg_match('/<!DOCTYPE NETSCAPE-Bookmark-file-1>/i', $data) === 0) {
95 return self::importStatus($filename, $filesize); 96 return $this->importStatus($filename, $filesize);
96 } 97 }
97 98
98 // Overwrite existing bookmarks? 99 // Overwrite existing bookmarks?