~xibo-maintainers/xibo/tempel

« back to all changes in this revision

Viewing changes to lib/Helper/Install.php

  • Committer: Dan Garner
  • Date: 2016-03-09 13:21:23 UTC
  • mto: This revision was merged to the branch mainline in revision 486.
  • Revision ID: git-v1:0239ef66bdce8b493972c57100700616e6365ef6
Working install/update

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 */
21
21
namespace Xibo\Helper;
22
22
 
23
 
use Slim\Slim;
24
23
use Xibo\Exception\InstallationError;
25
24
use Xibo\Service\ConfigService;
 
25
use Xibo\Service\SanitizerServiceInterface;
 
26
use Xibo\Service\SanitizeService;
 
27
use Xibo\Storage\StorageServiceInterface;
26
28
 
 
29
/**
 
30
 * Class Install
 
31
 * @package Xibo\Helper
 
32
 */
27
33
class Install
28
34
{
29
 
    /**
30
 
     * @var Slim
31
 
     */
32
 
    private $app;
33
 
 
34
35
    // DB Details
35
36
    public $db_create;
36
37
    public $db_admin_user;
44
45
    public $existing_db_pass;
45
46
    public $existing_db_name;
46
47
 
47
 
    public function __construct($app)
48
 
    {
49
 
        $this->app = $app;
50
 
    }
51
 
 
52
 
    /**
53
 
     * Get the App
54
 
     * @return Slim
55
 
     * @throws \Exception
56
 
     */
57
 
    public function getApp()
58
 
    {
59
 
        return $this->app;
60
 
    }
61
 
 
 
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
     */
62
63
    public function Step1()
63
64
    {
64
65
        return [
66
67
        ];
67
68
    }
68
69
 
 
70
    /**
 
71
     * @return array
 
72
     */
69
73
    public function Step2()
70
74
    {
71
75
        return [];
72
76
    }
73
77
 
74
 
    public function Step3()
 
78
    /**
 
79
     * @param StorageServiceInterface $store
 
80
     * @throws InstallationError
 
81
     */
 
82
    public function Step3($store)
75
83
    {
76
84
        // Have we been told to create a new database
77
 
        $this->db_create = $this->getSanitizer()->getInt('db_create');
 
85
        $this->db_create = $this->sanitizer->getInt('db_create');
78
86
 
79
87
        // Check all parameters have been specified
80
 
        $this->db_admin_user = $this->getSanitizer()->getString('admin_username');
81
 
        $this->db_admin_pass = $this->getSanitizer()->getString('admin_password');
82
 
 
83
 
        $this->new_db_host = $this->getSanitizer()->getString('host');
84
 
        $this->new_db_user = $this->getSanitizer()->getString('db_username');
85
 
        $this->new_db_pass = $this->getSanitizer()->getString('db_password');
86
 
        $this->new_db_name = $this->getSanitizer()->getString('db_name');
87
 
 
88
 
        $this->existing_db_host = $this->getSanitizer()->getString('existing_host');
89
 
        $this->existing_db_user = $this->getSanitizer()->getString('existing_db_username');
90
 
        $this->existing_db_pass = $this->getSanitizer()->getString('existing_db_password');
91
 
        $this->existing_db_name = $this->getSanitizer()->getString('existing_db_name');
 
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');
92
100
 
93
101
        // If an administrator user name / password has been specified then we should create a new DB
94
102
        if ($this->db_create == 1) {
111
119
            // Try to create the new database
112
120
            // Try and connect using these details and create the new database
113
121
            try {
114
 
                $this->getStore()->connect($this->new_db_host, $this->db_admin_user, $this->db_admin_pass);
 
122
                $store->connect($this->new_db_host, $this->db_admin_user, $this->db_admin_pass);
115
123
            } catch (\PDOException $e) {
116
124
                throw new InstallationError(sprintf(__('Could not connect to MySQL with the administrator details. Please check and try again. Error Message = [%s]'), $e->getMessage()));
117
125
            }
118
126
 
119
127
            // Try to create the new database
120
128
            try {
121
 
                $dbh = $this->getStore()->getConnection();
 
129
                $dbh = $store->getConnection();
122
130
                $dbh->exec(sprintf('CREATE DATABASE `%s` CHARACTER SET utf8 COLLATE utf8_general_ci', $this->new_db_name));
123
131
            } catch (\PDOException $e) {
124
132
                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()));
126
134
 
127
135
            // Try to create the new user
128
136
            try {
129
 
                $dbh = $this->getStore()->getConnection();
 
137
                $dbh = $store->getConnection();
130
138
 
131
139
                // Create the user and grant privileges
132
140
                if ($this->new_db_host == 'localhost') {
157
165
            $this->existing_db_name = $this->new_db_name;
158
166
 
159
167
            // Close the connection
160
 
            $this->getStore()->close();
 
168
            $store->close();
 
169
 
161
170
        } else {
162
171
            // Check details for a new database
163
172
            if ($this->existing_db_host == '')
175
184
 
176
185
        // Try and make a connection with this database
177
186
        try {
178
 
            $this->getStore()->connect($this->existing_db_host, $this->existing_db_user, $this->existing_db_pass, $this->existing_db_name);
 
187
            $store->connect($this->existing_db_host, $this->existing_db_user, $this->existing_db_pass, $this->existing_db_name);
179
188
        } catch (\PDOException $e) {
180
189
            throw new InstallationError(sprintf(__('Could not connect to MySQL with the administrator details. Please check and try again. Error Message = [%s]'), $e->getMessage()));
181
190
        }
187
196
        $sql = '';
188
197
 
189
198
        try {
190
 
            $dbh = $this->getStore()->getConnection();
 
199
            $dbh = $store->getConnection();
191
200
 
192
201
            foreach ($sql_files as $filename) {
193
202
                $delimiter = ';';
215
224
        $secretKey = Install::generateSecret();
216
225
 
217
226
        // Escape the password before we write it to disk
218
 
        $dbh = $this->getStore()->getConnection();
 
227
        $dbh = $store->getConnection();
219
228
        $existing_db_pass = addslashes($this->existing_db_pass);
220
229
 
221
230
        $settings = <<<END
262
271
        // This is handled by the calling function (i.e. there is no output from this call, we just reload and move on)
263
272
    }
264
273
 
 
274
    /**
 
275
     * @return array
 
276
     */
265
277
    public function Step4()
266
278
    {
267
279
        return [];
268
280
    }
269
281
 
270
 
    public function Step5()
 
282
    /**
 
283
     * @param StorageServiceInterface $store
 
284
     * @throws InstallationError
 
285
     */
 
286
    public function Step5($store)
271
287
    {
272
288
        // Configure the user account
273
 
        $username = $this->getSanitizer()->getString('admin_username');
274
 
        $password = $this->getSanitizer()->getString('admin_password');
 
289
        $username = $this->sanitizer->getString('admin_username');
 
290
        $password = $this->sanitizer->getString('admin_password');
275
291
 
276
292
        if ($username == '')
277
293
            throw new InstallationError(__('Missing the admin username.'));
281
297
 
282
298
        // Update user id 1 with these details.
283
299
        try {
284
 
            $dbh = $this->getStore()->getConnection();
 
300
            $dbh = $store->getConnection();
285
301
 
286
302
            $sth = $dbh->prepare('UPDATE `user` SET UserName = :username, UserPassword = :password WHERE UserID = 1 LIMIT 1');
287
303
            $sth->execute(array(
300
316
        }
301
317
    }
302
318
 
 
319
    /**
 
320
     * @return array
 
321
     */
303
322
    public function Step6()
304
323
    {
305
324
        return [
307
326
        ];
308
327
    }
309
328
 
310
 
    public function Step7()
 
329
    /**
 
330
     * @param StorageServiceInterface $store
 
331
     * @throws InstallationError
 
332
     */
 
333
    public function Step7($store)
311
334
    {
312
 
        $server_key = $this->getSanitizer()->getString('server_key');
313
 
        $library_location = $this->getSanitizer()->getString('library_location');
314
 
        $stats = $this->getSanitizer()->getCheckbox('stats');
 
335
        $server_key = $this->sanitizer->getString('server_key');
 
336
        $library_location = $this->sanitizer->getString('library_location');
 
337
        $stats = $this->sanitizer->getCheckbox('stats');
315
338
 
316
339
        if ($server_key == '')
317
340
            throw new InstallationError(__('Missing the server key.'));
349
372
        }
350
373
 
351
374
        try {
352
 
            $dbh = $this->getStore()->getConnection();
 
375
            $dbh = $store->getConnection();
353
376
 
354
377
            // Library Location
355
378
            $sth = $dbh->prepare('UPDATE `setting` SET `value` = :value WHERE `setting`.`setting` = \'LIBRARY_LOCATION\' LIMIT 1');
476
499
        return $all;
477
500
    }
478
501
 
 
502
    /**
 
503
     * @param int $length
 
504
     * @return string
 
505
     */
479
506
    public static function generateSecret($length = 12)
480
507
    {
481
508
        # Generates a random 12 character alphanumeric string to use as a salt