GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — integration (#2604)
by Brendan
05:28
created

InstallerPage::__appendError()   D

Complexity

Conditions 9
Paths 36

Size

Total Lines 33
Code Lines 18

Duplication

Lines 6
Ratio 18.18 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 9
eloc 18
c 2
b 1
f 0
nc 36
nop 3
dl 6
loc 33
rs 4.909
1
<?php
2
3
/**
4
 * @package install
5
 */
6
namespace SymphonyCms\Installer\Lib;
7
8
use DateTimeObj;
9
use Exception;
10
use HTMLPage;
11
use Lang;
12
use Symphony;
13
use Widget;
14
use XMLElement;
15
16
/**
17
 * @package content
18
 */
19
class InstallerPage extends HTMLPage
20
{
21
    protected $_params;
22
    protected $_page_title;
23
    protected $_template;
24
25
    public function __construct($template, $params = array())
26
    {
27
        parent::__construct();
28
29
        $this->_template = $template;
30
        $this->_params = $params;
31
32
        $this->_page_title = __('Install Symphony');
33
    }
34
35
    public function generate($page = null)
36
    {
37
        $this->Html->setDTD('<!DOCTYPE html>');
38
        $this->Html->setAttribute('lang', Lang::get());
39
40
        $this->addHeaderToPage('Cache-Control', 'no-cache, must-revalidate, max-age=0');
41
        $this->addHeaderToPage('Last-Modified', gmdate('D, d M Y H:i:s') . ' GMT');
42
43
        $this->setTitle($this->_page_title);
44
        $this->addElementToHead(new XMLElement('meta', null, array('charset' => 'UTF-8')), 1);
45
46
        $this->addStylesheetToHead(APPLICATION_URL . '/assets/css/installer.min.css', 'screen', 30);
47
48
        return parent::generate($page);
49
    }
50
51
    protected function __build($version = VERSION, XMLElement $extra = null)
52
    {
53
        parent::__build();
54
55
        $this->Form = Widget::Form(INSTALL_URL . '/index.php', 'post');
56
57
        $title = new XMLElement('h1', $this->_page_title);
58
        $version = new XMLElement('em', __('Version %s', array($version)));
59
60
        $title->appendChild($version);
61
62
        if (!is_null($extra)) {
63
            $title->appendChild($extra);
64
        }
65
66
        $this->Form->appendChild($title);
67
68
        if (isset($this->_params['show-languages']) && $this->_params['show-languages']) {
69
            $languages = new XMLElement('ul');
70
71
            foreach (Lang::getAvailableLanguages(false) as $code => $lang) {
72
                $languages->appendChild(new XMLElement(
73
                    'li',
74
                    Widget::Anchor(
75
                        $lang,
76
                        '?lang=' . $code
77
                    ),
78
                    ($_REQUEST['lang'] === $code || ($_REQUEST['lang'] === null && $code === 'en')) ? array('class' => 'selected') : array()
79
                ));
80
            }
81
82
            $languages->appendChild(new XMLElement(
83
                'li',
84
                Widget::Anchor(
85
                    __('Symphony is also available in other languages'),
86
                    'http://getsymphony.com/download/extensions/translations/'
87
                ),
88
                array('class' => 'more')
89
            ));
90
91
            $this->Form->appendChild($languages);
92
        }
93
94
        $this->Body->appendChild($this->Form);
95
96
        $function = 'view' . str_replace('-', '', ucfirst($this->_template));
97
        $this->$function();
98
    }
99
100
    protected function viewMissinglog()
101
    {
102
        $h2 = new XMLElement('h2', __('Missing log file'));
103
104
        // What folder wasn't writable? The docroot or the logs folder?
105
        // RE: #1706
106
        if (is_writeable(DOCROOT) === false) {
107
            $folder = DOCROOT;
108
        } elseif (is_writeable(MANIFEST) === false) {
109
            $folder = MANIFEST;
110
        } elseif (is_writeable(INSTALL_LOGS) === false) {
111
            $folder = INSTALL_LOGS;
112
        }
113
114
        $p = new XMLElement(
115
            'p',
116
            __(
117
                'Symphony tried to create a log file and failed. Make sure the %s folder is writable.',
118
                array('<code>' . $folder . '</code>')
0 ignored issues
show
Bug introduced by
The variable $folder does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
119
            )
120
        );
121
122
        $this->Form->appendChild($h2);
123
        $this->Form->appendChild($p);
124
    }
125
126
    protected function viewRequirements()
127
    {
128
        $h2 = new XMLElement('h2', __('System Requirements'));
129
130
        $this->Form->appendChild($h2);
131
132
        if (!empty($this->_params['errors'])) {
133
            $div = new XMLElement('div');
134
            $this->__appendError(
135
                array_keys($this->_params['errors']),
136
                $div,
137
                __('Symphony needs the following requirements to be met before things can be taken to the “next level”.')
138
            );
139
140
            $this->Form->appendChild($div);
141
        }
142
    }
143
144
    private function __appendError(array $codes, XMLElement &$element, $message = null)
145
    {
146
        if (is_null($message)) {
147
            $message = __('The following errors have been reported:');
148
        }
149
150
        foreach ($codes as $i => $c) {
151
            if (!isset($this->_params['errors'][$c])) {
152
                unset($codes[$i]);
153
            }
154
        }
155
156
        if (!empty($codes)) {
157
            if (count($codes) > 1) {
158
                $ul = new XMLElement('ul');
159
160
                foreach ($codes as $c) {
161 View Code Duplication
                    if (isset($this->_params['errors'][$c])) {
162
                        $ul->appendChild(new XMLElement('li', $this->_params['errors'][$c]['details']));
163
                    }
164
                }
165
166
                $element = Widget::Error($element, $message);
167
                $element->appendChild($ul);
168
            } else {
169
                $code = array_pop($codes);
170
171 View Code Duplication
                if (isset($this->_params['errors'][$code])) {
172
                    $element = Widget::Error($element, $this->_params['errors'][$code]['details']);
173
                }
174
            }
175
        }
176
    }
177
178
    protected function viewLanguages()
179
    {
180
        $h2 = new XMLElement('h2', __('Language selection'));
181
        $p = new XMLElement(
182
            'p',
183
            __('This installation can speak in different languages. Which one are you fluent in?')
184
        );
185
186
        $this->Form->appendChild($h2);
187
        $this->Form->appendChild($p);
188
189
        $languages = array();
190
191
        foreach (Lang::getAvailableLanguages(false) as $code => $lang) {
192
            $languages[] = array($code, ($code === 'en'), $lang);
193
        }
194
195
        if (count($languages) > 1) {
196
            $languages[0][1] = false;
197
            $languages[1][1] = true;
198
        }
199
200
        $this->Form->appendChild(Widget::Select('lang', $languages));
201
202
        $Submit = new XMLElement('div', null, array('class' => 'submit'));
203
        $Submit->appendChild(Widget::Input('action[proceed]', __('Proceed with installation'), 'submit'));
204
205
        $this->Form->appendChild($Submit);
206
    }
207
208 View Code Duplication
    protected function viewFailure()
209
    {
210
        $h2 = new XMLElement('h2', __('Installation Failure'));
211
        $p = new XMLElement('p', __('An error occurred during installation.'));
212
213
        // Attempt to get log information from the log file
214
        try {
215
            $log = file_get_contents(INSTALL_LOGS . '/install');
216
        } catch (Exception $ex) {
217
            $log = (string)$ex;
218
        }
219
220
        $code = new XMLElement('code', $log);
221
222
        $this->Form->appendChild($h2);
223
        $this->Form->appendChild($p);
224
        $this->Form->appendChild(
225
            new XMLElement('pre', $code)
226
        );
227
    }
228
229
    protected function viewSuccess()
230
    {
231
        $symphonyUrl = URL . '/' . Symphony::Configuration()->get('admin-path', 'symphony');
232
        $this->Form->setAttribute('action', $symphonyUrl);
233
234
        $div = new XMLElement('div');
235
        $div->appendChild(
236
            new XMLElement('h2', __('The floor is yours'))
237
        );
238
        $div->appendChild(
239
            new XMLElement(
240
                'p',
241
                __('Thanks for taking the quick, yet epic installation journey with us. It’s now your turn to shine!')
242
            )
243
        );
244
        $this->Form->appendChild($div);
245
246
        $ul = new XMLElement('ul');
247
        foreach ($this->_params['disabled-extensions'] as $handle) {
248
            $ul->appendChild(
249
                new XMLElement('li', '<code>' . $handle . '</code>')
250
            );
251
        }
252
253
        if ($ul->getNumberOfChildren() !== 0) {
254
            $this->Form->appendChild(
255
                new XMLElement(
256
                    'p',
257
                    __('Looks like the following extensions couldn’t be enabled and must be manually installed. It’s a minor setback in our otherwise prosperous future together.')
258
                )
259
            );
260
            $this->Form->appendChild($ul);
261
        }
262
263
        $this->Form->appendChild(
264
            new XMLElement(
265
                'p',
266
                __(
267
                    'I think you and I will achieve great things together. Just one last thing: how about we remove the %s folder and secure the safety of our relationship?',
268
                    array('<code>' . basename(INSTALL) . '</code>')
269
                )
270
            )
271
        );
272
273
        $submit = new XMLElement('div', null, array('class' => 'submit'));
274
        $submit->appendChild(Widget::Input('submit', __('Okay, now take me to the login page'), 'submit'));
275
276
        $this->Form->appendChild($submit);
277
    }
278
279
    protected function viewConfiguration()
0 ignored issues
show
Coding Style introduced by
viewConfiguration uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
280
    {
281
        /* -----------------------------------------------
282
         * Populating fields array
283
         * -----------------------------------------------
284
         */
285
286
        $fields = isset($_POST['fields']) ? $_POST['fields'] : $this->_params['default-config'];
287
288
        /* -----------------------------------------------
289
         * Welcome
290
         * -----------------------------------------------
291
         */
292
293
        $div = new XMLElement('div');
294
        $div->appendChild(
295
            new XMLElement('h2', __('Find something sturdy to hold on to because things are about to get awesome.'))
296
        );
297
        $div->appendChild(
298
            new XMLElement(
299
                'p',
300
                __('Think of this as a pre-game warm up. You know you’re going to kick-ass, so you’re savouring every moment before the show. Welcome to the Symphony install page.')
301
            )
302
        );
303
304
        $this->Form->appendChild($div);
305
306
        if (!empty($this->_params['errors'])) {
307
            $this->Form->appendChild(
308
                Widget::Error(
309
                    new XMLElement('p'),
310
                    __('Oops, a minor hurdle on your path to glory! There appears to be something wrong with the details entered below.')
311
                )
312
            );
313
        }
314
315
        /* -----------------------------------------------
316
         * Environment settings
317
         * -----------------------------------------------
318
         */
319
320
        $fieldset = new XMLElement('fieldset');
321
        $div = new XMLElement('div');
322
        $this->__appendError(array('no-write-permission-root', 'no-write-permission-workspace'), $div);
323
        if ($div->getNumberOfChildren() > 0) {
324
            $fieldset->appendChild($div);
325
            $this->Form->appendChild($fieldset);
326
        }
327
328
        /* -----------------------------------------------
329
         * Website & Locale settings
330
         * -----------------------------------------------
331
         */
332
333
        $Environment = new XMLElement('fieldset');
334
        $Environment->appendChild(new XMLElement('legend', __('Website Preferences')));
335
336
        $label = Widget::Label(
337
            __('Name'),
338
            Widget::Input('fields[general][sitename]', $fields['general']['sitename'])
339
        );
340
341
        $this->__appendError(array('general-no-sitename'), $label);
342
        $Environment->appendChild($label);
343
344
        $label = Widget::Label(
345
            __('Admin Path'),
346
            Widget::Input('fields[symphony][admin-path]', $fields['symphony']['admin-path'])
347
        );
348
349
        $this->__appendError(array('no-symphony-path'), $label);
350
        $Environment->appendChild($label);
351
352
        $Fieldset = new XMLElement('fieldset', null, array('class' => 'frame'));
353
        $Fieldset->appendChild(new XMLElement('legend', __('Date and Time')));
354
        $Fieldset->appendChild(new XMLElement(
355
            'p',
356
            __('Customise how Date and Time values are displayed throughout the Administration interface.')
357
        ));
358
359
        // Timezones
360
        $options = DateTimeObj::getTimezonesSelectOptions((
361
        isset($fields['region']['timezone']) && !empty($fields['region']['timezone'])
362
            ? $fields['region']['timezone']
363
            : date_default_timezone_get()
364
        ));
365
        $Fieldset->appendChild(Widget::Label(__('Region'), Widget::Select('fields[region][timezone]', $options)));
366
367
        // Date formats
368
        $options = DateTimeObj::getDateFormatsSelectOptions($fields['region']['date_format']);
369
        $Fieldset->appendChild(Widget::Label(
370
            __('Date Format'),
371
            Widget::Select('fields[region][date_format]', $options)
372
        ));
373
374
        // Time formats
375
        $options = DateTimeObj::getTimeFormatsSelectOptions($fields['region']['time_format']);
376
        $Fieldset->appendChild(Widget::Label(
377
            __('Time Format'),
378
            Widget::Select('fields[region][time_format]', $options)
379
        ));
380
381
        $Environment->appendChild($Fieldset);
382
        $this->Form->appendChild($Environment);
383
384
        /* -----------------------------------------------
385
         * Database settings
386
         * -----------------------------------------------
387
         */
388
389
        $Database = new XMLElement('fieldset');
390
        $Database->appendChild(new XMLElement('legend', __('Database Connection')));
391
        $Database->appendChild(new XMLElement('p', __('Please provide Symphony with access to a database.')));
392
393
        // Database name
394
        $label = Widget::Label(__('Database'), Widget::Input('fields[database][db]', $fields['database']['db']));
395
396
        $this->__appendError(array('database-incorrect-version', 'unknown-database'), $label);
397
        $Database->appendChild($label);
398
399
        // Database credentials
400
        $Div = new XMLElement('div', null, array('class' => 'two columns'));
401
        $Div->appendChild(Widget::Label(
402
            __('Username'),
403
            Widget::Input('fields[database][user]', $fields['database']['user']),
404
            'column'
405
        ));
406
        $Div->appendChild(Widget::Label(
407
            __('Password'),
408
            Widget::Input('fields[database][password]', $fields['database']['password'], 'password'),
409
            'column'
410
        ));
411
412
        $this->__appendError(array('database-invalid-credentials'), $Div);
413
        $Database->appendChild($Div);
414
415
        // Advanced configuration
416
        $Fieldset = new XMLElement('fieldset', null, array('class' => 'frame'));
417
        $Fieldset->appendChild(new XMLElement('legend', __('Advanced Configuration')));
418
        $Fieldset->appendChild(new XMLElement(
419
            'p',
420
            __('Leave these fields unless you are sure they need to be changed.')
421
        ));
422
423
        // Advanced configuration: Host, Port
424
        $Div = new XMLElement('div', null, array('class' => 'two columns'));
425
        $Div->appendChild(Widget::Label(
426
            __('Host'),
427
            Widget::Input('fields[database][host]', $fields['database']['host']),
428
            'column'
429
        ));
430
        $Div->appendChild(Widget::Label(
431
            __('Port'),
432
            Widget::Input('fields[database][port]', $fields['database']['port']),
433
            'column'
434
        ));
435
436
        $this->__appendError(array('no-database-connection'), $Div);
437
        $Fieldset->appendChild($Div);
438
439
        // Advanced configuration: Table Prefix
440
        $label = Widget::Label(
441
            __('Table Prefix'),
442
            Widget::Input('fields[database][tbl_prefix]', $fields['database']['tbl_prefix'])
443
        );
444
445
        $this->__appendError(array('database-table-prefix'), $label);
446
        $Fieldset->appendChild($label);
447
448
        $Database->appendChild($Fieldset);
449
        $this->Form->appendChild($Database);
450
451
        /* -----------------------------------------------
452
         * Permission settings
453
         * -----------------------------------------------
454
         */
455
456
        $Permissions = new XMLElement('fieldset');
457
        $Permissions->appendChild(new XMLElement('legend', __('Permission Settings')));
458
        $Permissions->appendChild(new XMLElement(
459
            'p',
460
            __('Set the permissions Symphony uses when saving files/directories.')
461
        ));
462
463
        $Div = new XMLElement('div', null, array('class' => 'two columns'));
464
        $Div->appendChild(Widget::Label(
465
            __('Files'),
466
            Widget::Input('fields[file][write_mode]', $fields['file']['write_mode']),
467
            'column'
468
        ));
469
        $Div->appendChild(Widget::Label(
470
            __('Directories'),
471
            Widget::Input('fields[directory][write_mode]', $fields['directory']['write_mode']),
472
            'column'
473
        ));
474
475
        $Permissions->appendChild($Div);
476
        $this->Form->appendChild($Permissions);
477
478
        /* -----------------------------------------------
479
         * User settings
480
         * -----------------------------------------------
481
         */
482
483
        $User = new XMLElement('fieldset');
484
        $User->appendChild(new XMLElement('legend', __('User Information')));
485
        $User->appendChild(new XMLElement(
486
            'p',
487
            __('Once installation is complete, you will be able to log in to the Symphony admin area with these user details.')
488
        ));
489
490
        // Username
491
        $label = Widget::Label(
492
            __('Username'),
493
            Widget::Input('fields[user][username]', $fields['user']['username'])
494
        );
495
496
        $this->__appendError(array('user-no-username'), $label);
497
        $User->appendChild($label);
498
499
        // Password
500
        $Div = new XMLElement('div', null, array('class' => 'two columns'));
501
        $Div->appendChild(Widget::Label(
502
            __('Password'),
503
            Widget::Input('fields[user][password]', $fields['user']['password'], 'password'),
504
            'column'
505
        ));
506
        $Div->appendChild(Widget::Label(
507
            __('Confirm Password'),
508
            Widget::Input('fields[user][confirm-password]', $fields['user']['confirm-password'], 'password'),
509
            'column'
510
        ));
511
512
        $this->__appendError(array('user-no-password', 'user-password-mismatch'), $Div);
513
        $User->appendChild($Div);
514
515
        // Personal information
516
        $Fieldset = new XMLElement('fieldset', null, array('class' => 'frame'));
517
        $Fieldset->appendChild(new XMLElement('legend', __('Personal Information')));
518
        $Fieldset->appendChild(new XMLElement('p', __('Please add the following personal details for this user.')));
519
520
        // Personal information: First Name, Last Name
521
        $Div = new XMLElement('div', null, array('class' => 'two columns'));
522
        $Div->appendChild(Widget::Label(
523
            __('First Name'),
524
            Widget::Input('fields[user][firstname]', $fields['user']['firstname']),
525
            'column'
526
        ));
527
        $Div->appendChild(Widget::Label(
528
            __('Last Name'),
529
            Widget::Input('fields[user][lastname]', $fields['user']['lastname']),
530
            'column'
531
        ));
532
533
        $this->__appendError(array('user-no-name'), $Div);
534
        $Fieldset->appendChild($Div);
535
536
        // Personal information: Email Address
537
        $label = Widget::Label(__('Email Address'), Widget::Input('fields[user][email]', $fields['user']['email']));
538
539
        $this->__appendError(array('user-invalid-email'), $label);
540
        $Fieldset->appendChild($label);
541
542
        $User->appendChild($Fieldset);
543
        $this->Form->appendChild($User);
544
545
        /* -----------------------------------------------
546
         * Submit area
547
         * -----------------------------------------------
548
         */
549
550
        $this->Form->appendChild(new XMLElement('h2', __('Install Symphony')));
551
        $this->Form->appendChild(new XMLElement(
552
            'p',
553
            __(
554
                'The installation process goes by really quickly. Make sure to take a deep breath before you press that sweet button.',
555
                array('<code>' . basename(INSTALL_URL) . '</code>')
556
            )
557
        ));
558
559
        $Submit = new XMLElement('div', null, array('class' => 'submit'));
560
        $Submit->appendChild(Widget::Input('lang', Lang::get(), 'hidden'));
561
562
        $Submit->appendChild(Widget::Input('action[install]', __('Install Symphony'), 'submit'));
563
564
        $this->Form->appendChild($Submit);
565
    }
566
}
567