]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/front/controller/admin/ImportController.php
Process bookmarks import through Slim controller
[github/shaarli/Shaarli.git] / application / front / controller / admin / ImportController.php
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Shaarli\Front\Controller\Admin;
6
7 use Psr\Http\Message\UploadedFileInterface;
8 use Slim\Http\Request;
9 use 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 */
17 class 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 }