1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2017 |
4
|
|
|
* |
5
|
|
|
* @package Majima |
6
|
|
|
* @author David Neustadt <[email protected]> |
7
|
|
|
* @copyright 2017 David Neustadt |
8
|
|
|
* @license MIT |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Majima\Controller; |
12
|
|
|
|
13
|
|
|
use Symfony\Component\DependencyInjection\Container; |
14
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
15
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
16
|
|
|
use Symfony\Component\HttpFoundation\Request; |
17
|
|
|
use Symfony\Component\HttpFoundation\Response; |
18
|
|
|
use Symfony\Component\Routing\RouterInterface; |
19
|
|
|
use Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class InstallController |
23
|
|
|
* @package Majima\Controller |
24
|
|
|
*/ |
25
|
|
|
class InstallController |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var Container |
29
|
|
|
*/ |
30
|
|
|
protected $container; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var RouterInterface |
34
|
|
|
*/ |
35
|
|
|
protected $router; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* InstallController constructor. |
39
|
|
|
* @param Container $container |
40
|
|
|
* @param RouterInterface $router |
41
|
|
|
*/ |
42
|
|
|
public function __construct(Container $container, RouterInterface $router) |
43
|
|
|
{ |
44
|
|
|
$this->container = $container; |
45
|
|
|
$this->router = $router; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param Request $request |
50
|
|
|
* @return Response |
51
|
|
|
*/ |
52
|
|
|
public function indexAction(Request $request) |
53
|
|
|
{ |
54
|
|
|
if ($this->container->hasParameter('db_config_loaded')) { |
55
|
|
|
return new RedirectResponse($this->router->generate('index_index')); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$template = file_get_contents(BASE_DIR . join(DIRECTORY_SEPARATOR, ['majima', 'Resources', 'views', 'Install', 'index.html'])); |
|
|
|
|
59
|
|
|
|
60
|
|
|
$params = [ |
61
|
|
|
'db_host' => $request->get('db_host'), |
62
|
|
|
'db_port' => $request->get('db_port'), |
63
|
|
|
'db_name' => $request->get('db_name'), |
64
|
|
|
'db_user' => $request->get('db_user'), |
65
|
|
|
'db_passw' => $request->get('db_passw'), |
66
|
|
|
'admin_user' => $request->get('admin_user'), |
67
|
|
|
'admin_passw' => $request->get('admin_passw'), |
68
|
|
|
]; |
69
|
|
|
|
70
|
|
|
foreach ($params as $param) { |
71
|
|
|
if (empty($param)) { |
72
|
|
|
return new Response( |
73
|
|
|
$template, |
74
|
|
|
200, |
75
|
|
|
['Content-Type' => 'text/html'] |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$defaultEncoder = new MessageDigestPasswordEncoder('sha512', true, 5000); |
81
|
|
|
$adminUser = $params['admin_user']; |
82
|
|
|
$adminSha1 = sha1(rand(100000, 999999)); |
83
|
|
|
$adminPassword = $defaultEncoder->encodePassword($params['admin_passw'], $adminSha1); |
84
|
|
|
|
85
|
|
|
unset($params['admin_user']); |
86
|
|
|
unset($params['admin_passw']); |
87
|
|
|
|
88
|
|
|
$configPath = BASE_DIR . join(DIRECTORY_SEPARATOR, ['var', 'conf', 'config.json']); |
89
|
|
|
|
90
|
|
|
file_put_contents($configPath, json_encode($params, JSON_PRETTY_PRINT)); |
91
|
|
|
|
92
|
|
|
$fs = new Filesystem(); |
93
|
|
|
$fs->remove($this->container->getParameter('kernel.cache_dir')); |
94
|
|
|
|
95
|
|
|
$sql = file_get_contents(BASE_DIR . join(DIRECTORY_SEPARATOR, ['majima', 'Resources', 'sql', 'install.sql'])); |
96
|
|
|
|
97
|
|
|
$pdo = new \PDO( |
98
|
|
|
sprintf( |
99
|
|
|
"mysql:host=%s;port=%s;dbname=%s", |
100
|
|
|
$params['db_host'], |
101
|
|
|
$params['db_port'], |
102
|
|
|
$params['db_name'] |
103
|
|
|
), |
104
|
|
|
$params['db_user'], |
105
|
|
|
$params['db_passw'] |
106
|
|
|
); |
107
|
|
|
$pdo->exec($sql); |
108
|
|
|
|
109
|
|
|
$qb = new \FluentPDO($pdo); |
110
|
|
|
$qb->insertInto('users') |
111
|
|
|
->values([ |
112
|
|
|
'name' => $adminUser, |
113
|
|
|
'password' => $adminPassword, |
114
|
|
|
'salt' => $adminSha1, |
115
|
|
|
]) |
116
|
|
|
->execute(); |
117
|
|
|
|
118
|
|
|
$qb->insertInto('users_roles') |
119
|
|
|
->values([ |
120
|
|
|
'userID' => 1, |
121
|
|
|
'role' => 'ROLE_ADMIN', |
122
|
|
|
]) |
123
|
|
|
->execute(); |
124
|
|
|
|
125
|
|
|
$template = file_get_contents(BASE_DIR . join(DIRECTORY_SEPARATOR, ['majima', 'Resources', 'views', 'Install', 'success.html'])); |
126
|
|
|
return new Response( |
127
|
|
|
$template, |
128
|
|
|
200, |
129
|
|
|
['Content-Type' => 'text/html'] |
130
|
|
|
); |
131
|
|
|
} |
132
|
|
|
} |