~xibo-maintainers/xibo/tempel

« back to all changes in this revision

Viewing changes to lib/Helper/Install.php

  • Committer: Dan Garner
  • Date: 2015-08-11 09:29:02 UTC
  • mto: This revision was merged to the branch mainline in revision 453.
  • Revision ID: git-v1:a86fb4369b7395c13367577d23b14c0ab4528c1a
Transitions fixes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
namespace Xibo\Helper;
22
22
 
23
23
use Xibo\Exception\InstallationError;
24
 
use Xibo\Service\ConfigService;
25
 
use Xibo\Service\SanitizerServiceInterface;
26
 
use Xibo\Service\SanitizeService;
27
 
use Xibo\Storage\StorageServiceInterface;
 
24
use Xibo\Storage\PDOConnect;
28
25
 
29
 
/**
30
 
 * Class Install
31
 
 * @package Xibo\Helper
32
 
 */
33
26
class Install
34
27
{
35
28
    // DB Details
45
38
    public $existing_db_pass;
46
39
    public $existing_db_name;
47
40
 
48
 
    /** @var SanitizerServiceInterface */
49
 
    private $sanitizer;
50
 
 
51
 
    /**
52
 
     * Install constructor.
53
 
     * @param SanitizeService $sanitizer
54
 
     */
55
 
    public function __construct($sanitizer)
56
 
    {
57
 
        $this->sanitizer = $sanitizer;
58
 
    }
59
 
 
60
 
    /**
61
 
     * @return array
62
 
     */
63
41
    public function Step1()
64
42
    {
65
43
        return [
66
 
            'config' => new ConfigService()
 
44
            'config' => new Config()
67
45
        ];
68
46
    }
69
47
 
70
 
    /**
71
 
     * @return array
72
 
     */
73
48
    public function Step2()
74
49
    {
75
50
        return [];
76
51
    }
77
52
 
78
 
    /**
79
 
     * @param StorageServiceInterface $store
80
 
     * @throws InstallationError
81
 
     */
82
 
    public function Step3($store)
 
53
    public function Step3()
83
54
    {
84
55
        // Have we been told to create a new database
85
 
        $this->db_create = $this->sanitizer->getInt('db_create');
 
56
        $this->db_create = Sanitize::getInt('db_create');
86
57
 
87
58
        // Check all parameters have been specified
88
 
        $this->db_admin_user = $this->sanitizer->getString('admin_username');
89
 
        $this->db_admin_pass = $this->sanitizer->getString('admin_password');
90
 
 
91
 
        $this->new_db_host = $this->sanitizer->getString('host');
92
 
        $this->new_db_user = $this->sanitizer->getString('db_username');
93
 
        $this->new_db_pass = $this->sanitizer->getString('db_password');
94
 
        $this->new_db_name = $this->sanitizer->getString('db_name');
95
 
 
96
 
        $this->existing_db_host = $this->sanitizer->getString('existing_host');
97
 
        $this->existing_db_user = $this->sanitizer->getString('existing_db_username');
98
 
        $this->existing_db_pass = $this->sanitizer->getString('existing_db_password');
99
 
        $this->existing_db_name = $this->sanitizer->getString('existing_db_name');
 
59
        $this->db_admin_user = Sanitize::getString('admin_username');
 
60
        $this->db_admin_pass = Sanitize::getString('admin_password');
 
61
 
 
62
        $this->new_db_host = Sanitize::getString('host');
 
63
        $this->new_db_user = Sanitize::getString('db_username');
 
64
        $this->new_db_pass = Sanitize::getString('db_password');
 
65
        $this->new_db_name = Sanitize::getString('db_name');
 
66
 
 
67
        $this->existing_db_host = Sanitize::getString('existing_host');
 
68
        $this->existing_db_user = Sanitize::getString('existing_db_username');
 
69
        $this->existing_db_pass = Sanitize::getString('existing_db_password');
 
70
        $this->existing_db_name = Sanitize::getString('existing_db_name');
100
71
 
101
72
        // If an administrator user name / password has been specified then we should create a new DB
102
73
        if ($this->db_create == 1) {
119
90
            // Try to create the new database
120
91
            // Try and connect using these details and create the new database
121
92
            try {
122
 
                $store->connect($this->new_db_host, $this->db_admin_user, $this->db_admin_pass);
 
93
                PDOConnect::connect($this->new_db_host, $this->db_admin_user, $this->db_admin_pass);
123
94
            } catch (\PDOException $e) {
124
95
                throw new InstallationError(sprintf(__('Could not connect to MySQL with the administrator details. Please check and try again. Error Message = [%s]'), $e->getMessage()));
125
96
            }
126
97
 
127
98
            // Try to create the new database
128
99
            try {
129
 
                $dbh = $store->getConnection();
130
 
                $dbh->exec(sprintf('CREATE DATABASE `%s` CHARACTER SET utf8 COLLATE utf8_general_ci', $this->new_db_name));
 
100
                $dbh = PDOConnect::init();
 
101
                $dbh->exec(sprintf('CREATE DATABASE `%s`', $this->new_db_name));
131
102
            } catch (\PDOException $e) {
132
103
                throw new InstallationError(sprintf(__('Could not create a new database with the administrator details [%s]. Please check and try again. Error Message = [%s]'), $this->db_admin_user, $e->getMessage()));
133
104
            }
134
105
 
135
106
            // Try to create the new user
136
107
            try {
137
 
                $dbh = $store->getConnection();
 
108
                $dbh = PDOConnect::init();
138
109
 
139
110
                // Create the user and grant privileges
140
111
                if ($this->new_db_host == 'localhost') {
141
 
                    $sql = sprintf('GRANT ALL PRIVILEGES ON `%s`.* to %s@%s IDENTIFIED BY %s',
142
 
                        $this->new_db_name,
143
 
                        $dbh->quote($this->new_db_user),
144
 
                        $dbh->quote($this->new_db_host),
145
 
                        $dbh->quote($this->new_db_pass)
 
112
                    $dbh->exec(sprintf('GRANT ALL PRIVILEGES ON `%s`.* to %s@%s IDENTIFIED BY %s',
 
113
                            $this->new_db_name,
 
114
                            $dbh->quote($this->new_db_user),
 
115
                            $dbh->quote($this->new_db_host),
 
116
                            $dbh->quote($this->new_db_pass))
146
117
                    );
147
 
 
148
 
                    $dbh->exec($sql);
149
118
                } else {
150
 
                    $sql = sprintf('GRANT ALL PRIVILEGES ON `%s`.* to %s@\'%%\' IDENTIFIED BY %s',
151
 
                        $this->new_db_name,
152
 
                        $dbh->quote($this->new_db_user),
153
 
                        $dbh->quote($this->new_db_pass)
 
119
                    $dbh->exec(sprintf("GRANT ALL PRIVILEGES ON `%s`.* to %s@%% IDENTIFIED BY %s",
 
120
                            $this->new_db_name,
 
121
                            $dbh->quote($this->new_db_user),
 
122
                            $dbh->quote($this->new_db_pass))
154
123
                    );
155
 
 
156
 
                    $dbh->exec($sql);
157
124
                }
158
125
 
159
126
                // Flush
160
127
                $dbh->exec('FLUSH PRIVILEGES');
161
128
            } catch (\PDOException $e) {
162
 
                throw new InstallationError(sprintf(__('Could not create a new user with the administrator details. Please check and try again. Error Message = [%s]. SQL = [%s].'), $e->getMessage(), $sql));
 
129
                throw new InstallationError(sprintf(__('Could not create a new user with the administrator details. Please check and try again. Error Message = [%s]'), $e->getMessage()));
163
130
            }
164
131
 
165
132
            // Set our DB details
169
136
            $this->existing_db_name = $this->new_db_name;
170
137
 
171
138
            // Close the connection
172
 
            $store->close();
173
 
 
 
139
            PDOConnect::close();
174
140
        } else {
175
141
            // Check details for a new database
176
142
            if ($this->existing_db_host == '')
188
154
 
189
155
        // Try and make a connection with this database
190
156
        try {
191
 
            $store->connect($this->existing_db_host, $this->existing_db_user, $this->existing_db_pass, $this->existing_db_name);
 
157
            PDOConnect::connect($this->existing_db_host, $this->existing_db_user, $this->existing_db_pass, $this->existing_db_name);
192
158
        } catch (\PDOException $e) {
193
159
            throw new InstallationError(sprintf(__('Could not connect to MySQL with the administrator details. Please check and try again. Error Message = [%s]'), $e->getMessage()));
194
160
        }
195
161
 
196
162
        // We should have a database that we can access and populate with our tables.
197
 
        $sql_files = array('structure.sql', 'data.sql', 'constraints.sql');
 
163
        $sql_files = array('structure.sql', 'data.sql');
198
164
        $sqlStatementCount = 0;
199
165
        $sql_file = '';
200
166
        $sql = '';
201
167
 
202
168
        try {
203
 
            $dbh = $store->getConnection();
 
169
            $dbh = PDOConnect::init();
204
170
 
205
171
            foreach ($sql_files as $filename) {
206
172
                $delimiter = ';';
228
194
        $secretKey = Install::generateSecret();
229
195
 
230
196
        // Escape the password before we write it to disk
231
 
        $dbh = $store->getConnection();
 
197
        $dbh = PDOConnect::init();
232
198
        $existing_db_pass = addslashes($this->existing_db_pass);
233
199
 
234
200
        $settings = <<<END
256
222
 
257
223
define('SECRET_KEY', '$secretKey');
258
224
 
259
 
// Additional Monolog handlers/processors to be registered
260
 
// \$logHandlers = [];
261
 
// \$logProcessors = [];
262
 
 
263
 
// Additional Middleware
264
 
// \$middleware = [];
265
 
// \$authentication = ;
266
 
 
267
225
END;
268
226
 
269
227
        if (!fwrite($fh, $settings))
275
233
        // This is handled by the calling function (i.e. there is no output from this call, we just reload and move on)
276
234
    }
277
235
 
278
 
    /**
279
 
     * @return array
280
 
     */
281
236
    public function Step4()
282
237
    {
283
238
        return [];
284
239
    }
285
240
 
286
 
    /**
287
 
     * @param StorageServiceInterface $store
288
 
     * @throws InstallationError
289
 
     */
290
 
    public function Step5($store)
 
241
    public function Step5()
291
242
    {
292
243
        // Configure the user account
293
 
        $username = $this->sanitizer->getString('admin_username');
294
 
        $password = $this->sanitizer->getString('admin_password');
 
244
        $username = Sanitize::getString('admin_username');
 
245
        $password = Sanitize::getString('admin_password');
295
246
 
296
247
        if ($username == '')
297
248
            throw new InstallationError(__('Missing the admin username.'));
301
252
 
302
253
        // Update user id 1 with these details.
303
254
        try {
304
 
            $dbh = $store->getConnection();
 
255
            $dbh = PDOConnect::init();
305
256
 
306
257
            $sth = $dbh->prepare('UPDATE `user` SET UserName = :username, UserPassword = :password WHERE UserID = 1 LIMIT 1');
307
258
            $sth->execute(array(
320
271
        }
321
272
    }
322
273
 
323
 
    /**
324
 
     * @return array
325
 
     */
326
274
    public function Step6()
327
275
    {
328
276
        return [
330
278
        ];
331
279
    }
332
280
 
333
 
    /**
334
 
     * @param StorageServiceInterface $store
335
 
     * @throws InstallationError
336
 
     */
337
 
    public function Step7($store)
 
281
    public function Step7()
338
282
    {
339
 
        $server_key = $this->sanitizer->getString('server_key');
340
 
        $library_location = $this->sanitizer->getString('library_location');
341
 
        $stats = $this->sanitizer->getCheckbox('stats');
 
283
        $server_key = Sanitize::getString('server_key');
 
284
        $library_location = Sanitize::getString('library_location');
 
285
        $stats = Sanitize::getCheckbox('stats');
342
286
 
343
287
        if ($server_key == '')
344
288
            throw new InstallationError(__('Missing the server key.'));
376
320
        }
377
321
 
378
322
        try {
379
 
            $dbh = $store->getConnection();
 
323
            $dbh = PDOConnect::init();
380
324
 
381
325
            // Library Location
382
326
            $sth = $dbh->prepare('UPDATE `setting` SET `value` = :value WHERE `setting`.`setting` = \'LIBRARY_LOCATION\' LIMIT 1');
503
447
        return $all;
504
448
    }
505
449
 
506
 
    /**
507
 
     * @param int $length
508
 
     * @return string
509
 
     */
510
450
    public static function generateSecret($length = 12)
511
451
    {
512
452
        # Generates a random 12 character alphanumeric string to use as a salt