Failed Conditions
Push — master ( fc54b8...947180 )
by Yangsin
124:44 queued 119:37
created

InstallController::step1()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2.0023

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 11
cts 12
cp 0.9167
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 12
nc 2
nop 2
crap 2.0023
1
<?php
2
/*
3
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.lockon.co.jp/
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
 */
23
24
25
namespace Eccube\Controller\Install;
26
27
use Doctrine\DBAL\Migrations\Configuration\Configuration;
28
use Doctrine\DBAL\Migrations\Migration;
29
use Doctrine\DBAL\Migrations\MigrationException;
30
use Doctrine\ORM\EntityManager;
31
use Doctrine\ORM\Tools\SchemaTool;
32
use Eccube\Common\Constant;
33
use Eccube\InstallApplication;
34
use Eccube\Util\Str;
35
use Symfony\Component\Filesystem\Filesystem;
36
use Symfony\Component\Finder\Finder;
37
use Symfony\Component\Form\Form;
38
use Symfony\Component\HttpFoundation\Request;
39
use Symfony\Component\Yaml\Yaml;
40
41
class InstallController
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
42
{
43
    private $app;
44
45
    private $PDO;
46
47
    private $config_path;
48
49
    private $dist_path;
50
51
    private $cache_path;
52
53
    private $session_data;
54
55
    private $required_modules = array('pdo', 'phar', 'mbstring', 'zlib', 'ctype', 'session', 'JSON', 'xml', 'libxml', 'OpenSSL', 'zip', 'cURL', 'fileinfo');
56
57
    private $recommended_module = array('hash', 'mcrypt');
58
59
    const SESSION_KEY = 'eccube.session.install';
60
61 7
    public function __construct()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
62
    {
63 7
        $this->config_path = __DIR__ . '/../../../../app/config/eccube';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
64 7
        $this->dist_path = __DIR__ . '/../../Resource/config';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
65 7
        $this->cache_path = __DIR__ . '/../../../../app/cache';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
66
    }
67
68 4
    private function isValid(Request $request, Form $form)
69
    {
70 4
        $session = $request->getSession();
71 4
        if ('POST' === $request->getMethod()) {
72
            $form->handleRequest($request);
73
            if ($form->isValid()) {
74
                $sessionData = $session->get(self::SESSION_KEY) ?: array();
75
                $formData = array_replace_recursive($sessionData, $form->getData());
76
                $session->set(self::SESSION_KEY, $formData);
77
78
                return true;
79
            }
80
        }
81
82 4
        return false;
83
    }
84
85 5
    private function getSessionData(Request $request)
86
    {
87 5
        return $this->session_data = $request->getSession()->get(self::SESSION_KEY);
88
    }
89
90
    // 最初からやり直す場合、SESSION情報をクリア
91 1
    public function index(InstallApplication $app, Request $request)
0 ignored issues
show
introduced by
Declare public methods first, then protected ones and finally private ones
Loading history...
introduced by
You must use "/**" style comments for a function comment
Loading history...
92
    {
93 1
        $request->getSession()->remove(self::SESSION_KEY);
94
95 1
        return $app->redirect($app->url('install_step1'));
96
    }
97
98
    // ようこそ
99 1
    public function step1(InstallApplication $app, Request $request)
0 ignored issues
show
introduced by
You must use "/**" style comments for a function comment
Loading history...
100
    {
101 1
        $form = $app['form.factory']
102 1
            ->createBuilder('install_step1')
103 1
            ->getForm();
104 1
        $sessionData = $this->getSessionData($request);
105 1
        $form->setData($sessionData);
106
107 1
        if ($this->isValid($request, $form)) {
108
            return $app->redirect($app->url('install_step2'));
109
        }
110
111 1
        $this->checkModules($app);
112
113 1
        return $app['twig']->render('step1.twig', array(
114 1
            'form' => $form->createView(),
115 1
            'publicPath' => '..' . RELATIVE_PUBLIC_DIR_PATH . '/',
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
116
        ));
117
    }
118
119
    // 権限チェック
120 1
    public function step2(InstallApplication $app, Request $request)
0 ignored issues
show
introduced by
You must use "/**" style comments for a function comment
Loading history...
121
    {
122 1
        $this->getSessionData($request);
123
124 1
        $protectedDirs = $this->getProtectedDirs();
125
126
        // 権限がある場合, キャッシュディレクトリをクリア
127 1
        if (empty($protectedDirs)) {
128 1
            $finder = Finder::create()
129 1
                ->in($this->cache_path)
130 1
                ->directories()
131 1
                ->depth(0);
132 1
            $fs = new Filesystem();
133 1
            $fs->remove($finder);
134
        }
135
136 1
        return $app['twig']->render('step2.twig', array(
137 1
            'protectedDirs' => $protectedDirs,
138 1
            'publicPath' => '..' . RELATIVE_PUBLIC_DIR_PATH . '/',
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
139
        ));
140
    }
141
142
    //    サイトの設定
143 1
    public function step3(InstallApplication $app, Request $request)
0 ignored issues
show
introduced by
You must use "/**" style comments for a function comment
Loading history...
144
    {
145 1
        $form = $app['form.factory']
146 1
            ->createBuilder('install_step3')
147 1
            ->getForm();
148 1
        $sessionData = $this->getSessionData($request);
149
150 1
        if (empty($sessionData['shop_name'])) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
151
152 1
            $config_file = $this->config_path . '/config.yml';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
153 1
            $fs = new Filesystem();
154
155 1
            if ($fs->exists($config_file)) {
156
                // すでに登録されていた場合、登録データを表示
157 1
                $this->setPDO();
158 1
                $stmt = $this->PDO->query("SELECT shop_name, email01 FROM dtb_base_info WHERE id = 1;");
159
160 1
                foreach ($stmt as $row) {
161 1
                    $sessionData['shop_name'] = $row['shop_name'];
162 1
                    $sessionData['email'] = $row['email01'];
163
                }
164
165
                // セキュリティの設定
166 1
                $config_file = $this->config_path . '/path.yml';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
167 1
                $config = Yaml::parse(file_get_contents($config_file));
168 1
                $sessionData['admin_dir'] = $config['admin_route'];
169
170 1
                $config_file = $this->config_path . '/config.yml';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
171 1
                $config = Yaml::parse(file_get_contents($config_file));
172
173 1
                $allowHost = $config['admin_allow_host'];
174 1
                if (count($allowHost) > 0) {
175
                    $sessionData['admin_allow_hosts'] = Str::convertLineFeed(implode("\n", $allowHost));
176
                }
177 1
                $sessionData['admin_force_ssl'] = (bool)$config['force_ssl'];
0 ignored issues
show
Coding Style introduced by
As per coding-style, a cast statement should be followed by a single space.
Loading history...
178
179
                // メール設定
180 1
                $config_file = $this->config_path . '/mail.yml';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
181 1
                $config = Yaml::parse(file_get_contents($config_file));
182 1
                $mail = $config['mail'];
183 1
                $sessionData['mail_backend'] = $mail['transport'];
184 1
                $sessionData['smtp_host'] = $mail['host'];
185 1
                $sessionData['smtp_port'] = $mail['port'];
186 1
                $sessionData['smtp_username'] = $mail['username'];
187 1
                $sessionData['smtp_password'] = $mail['password'];
188
            } else {
189
                // 初期値にmailを設定
190
                $sessionData['mail_backend'] = 'mail';
191
            }
192
        }
193
194 1
        $form->setData($sessionData);
195 1
        if ($this->isValid($request, $form)) {
196
            $data = $form->getData();
0 ignored issues
show
Unused Code introduced by
$data is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
197
198
            return $app->redirect($app->url('install_step4'));
199
        }
200
201 1
        return $app['twig']->render('step3.twig', array(
202 1
            'form' => $form->createView(),
203 1
            'publicPath' => '..' . RELATIVE_PUBLIC_DIR_PATH . '/',
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
204
        ));
205
    }
206
207
    //    データベースの設定
208 1
    public function step4(InstallApplication $app, Request $request)
0 ignored issues
show
introduced by
You must use "/**" style comments for a function comment
Loading history...
209
    {
210 1
        $form = $app['form.factory']
211 1
            ->createBuilder('install_step4')
212 1
            ->getForm();
213
214 1
        $sessionData = $this->getSessionData($request);
215
216 1
        if (empty($sessionData['database'])) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
217
218 1
            $config_file = $this->config_path.'/database.yml';
219 1
            $fs = new Filesystem();
220
221 1
            if ($fs->exists($config_file)) {
222
                // すでに登録されていた場合、登録データを表示
223
224
                // データベース設定
225 1
                $config = Yaml::parse(file_get_contents($config_file));
226 1
                $database = $config['database'];
227 1
                $sessionData['database'] = $database['driver'];
228 1
                if ($database['driver'] != 'pdo_sqlite') {
229 1
                    $sessionData['database_host'] = $database['host'];
230 1
                    $sessionData['database_port'] = $database['port'];
231 1
                    $sessionData['database_name'] = $database['dbname'];
232 1
                    $sessionData['database_user'] = $database['user'];
233 1
                    $sessionData['database_password'] = $database['password'];
234
                }
235
            }
236
        }
237
238 1
        $form->setData($sessionData);
239
240 1
        if ($this->isValid($request, $form)) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
241
242
            return $app->redirect($app->url('install_step5'));
243
        }
244
245 1
        return $app['twig']->render('step4.twig', array(
246 1
            'form' => $form->createView(),
247 1
            'publicPath' => '..' . RELATIVE_PUBLIC_DIR_PATH . '/',
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
248
        ));
249
    }
250
251
    //    データベースの初期化
252 1
    public function step5(InstallApplication $app, Request $request)
0 ignored issues
show
introduced by
You must use "/**" style comments for a function comment
Loading history...
253
    {
254 1
        set_time_limit(0);
255 1
        $this->app = $app;
256 1
        $form = $app['form.factory']
257 1
            ->createBuilder('install_step5')
258 1
            ->getForm();
259 1
        $sessionData = $this->getSessionData($request);
260 1
        $form->setData($sessionData);
261
262 1
        if ($this->isValid($request, $form)) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
263
264
            $this
265
                ->createDatabaseYamlFile($sessionData)
266
                ->createMailYamlFile($sessionData)
267
                ->createPathYamlFile($sessionData, $request);
268
269
            if (!$form['no_update']->getData()) {
270
                set_time_limit(0);
271
                $this->createConfigYamlFile($sessionData);
272
273
                $this
274
                    ->setPDO()
275
                    ->dropTables()
276
                    ->createTables()
277
                    ->doMigrate()
278
                    ->insert();
279
            } else {
280
                // データベースを初期化しない場合、auth_magicは初期化しない
281
                $this->createConfigYamlFile($sessionData, false);
282
283
                $this
284
                    ->setPDO()
285
                    ->update();
286
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
287
            }
288
289
290
            if (isset($sessionData['agree']) && $sessionData['agree'] == '1') {
291
                $host = $request->getSchemeAndHttpHost();
292
                $basePath = $request->getBasePath();
293
                $params = array(
294
                    'http_url' => $host . $basePath,
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
295
                    'shop_name' => $sessionData['shop_name'],
296
                );
297
298
                $this->sendAppData($params);
299
            }
300
            $this->addInstallStatus();
301
302
            $request->getSession()->remove(self::SESSION_KEY);
303
304
            return $app->redirect($app->url('install_complete'));
305
        }
306
307 1
        return $app['twig']->render('step5.twig', array(
308 1
            'form' => $form->createView(),
309 1
            'publicPath' => '..' . RELATIVE_PUBLIC_DIR_PATH . '/',
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
310
        ));
311
    }
312
313
    //    インストール完了
314 1
    public function complete(InstallApplication $app, Request $request)
0 ignored issues
show
introduced by
You must use "/**" style comments for a function comment
Loading history...
315
    {
316 1
        $config_file = $this->config_path . '/path.yml';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
317 1
        $config = Yaml::parse(file_get_contents($config_file));
318
319 1
        $host = $request->getSchemeAndHttpHost();
320 1
        $basePath = $request->getBasePath();
321
322 1
        $adminUrl = $host . $basePath . '/' . $config['admin_dir'];
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
323
324 1
        return $app['twig']->render('complete.twig', array(
325 1
            'admin_url' => $adminUrl,
326 1
            'publicPath' => '..' . RELATIVE_PUBLIC_DIR_PATH . '/',
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
327
        ));
328
    }
329
330
    private function resetNatTimer()
331
    {
332
        // NATの無通信タイマ対策(仮)
333
        echo str_repeat(' ', 4 * 1024);
334
        ob_flush();
335
        flush();
336
    }
337
338
339 1
    private function checkModules($app)
0 ignored issues
show
Coding Style introduced by
checkModules uses the super-global variable $_SERVER 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...
340
    {
341 1
        foreach ($this->required_modules as $module) {
342 1
            if (!extension_loaded($module)) {
343 1
                $app->addDanger('[必須] ' . $module . ' 拡張モジュールが有効になっていません。', 'install');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
344
            }
345
        }
346
347 1
        if (!extension_loaded('pdo_mysql') && !extension_loaded('pdo_pgsql')) {
348
            $app->addDanger('[必須] ' . 'pdo_pgsql又はpdo_mysql 拡張モジュールを有効にしてください。', 'install');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
349
        }
350
351 1
        foreach ($this->recommended_module as $module) {
352 1
            if (!extension_loaded($module)) {
353 1
                $app->addWarning('[推奨] ' . $module . ' 拡張モジュールが有効になっていません。', 'install');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
354
            }
355
        }
356
357 1
        if ('\\' === DIRECTORY_SEPARATOR) { // for Windows
358
            if (!extension_loaded('wincache')) {
359
                $app->addWarning('[推奨] WinCache 拡張モジュールが有効になっていません。', 'install');
360
            }
361
        } else {
362 1
            if (!extension_loaded('apc')) {
363 1
                $app->addWarning('[推奨] APC 拡張モジュールが有効になっていません。', 'install');
364
            }
365
        }
366
367 1
        if (isset($_SERVER['SERVER_SOFTWARE']) && strpos('Apache', $_SERVER['SERVER_SOFTWARE']) !== false) {
368
            if (!function_exists('apache_get_modules')) {
369
                $app->addWarning('mod_rewrite が有効になっているか不明です。', 'install');
370
            } elseif (!in_array('mod_rewrite', apache_get_modules())) {
371
                $app->addDanger('[必須] ' . 'mod_rewriteを有効にしてください。', 'install');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
372
            }
373 1
        } elseif (isset($_SERVER['SERVER_SOFTWARE']) && strpos('Microsoft-IIS', $_SERVER['SERVER_SOFTWARE']) !== false) {
0 ignored issues
show
Unused Code introduced by
This elseif statement is empty, and could be removed.

This check looks for the bodies of elseif statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These elseif bodies can be removed. If you have an empty elseif but statements in the else branch, consider inverting the condition.

Loading history...
374
            // iis
375 1
        } elseif (isset($_SERVER['SERVER_SOFTWARE']) && strpos('nginx', $_SERVER['SERVER_SOFTWARE']) !== false) {
0 ignored issues
show
Unused Code introduced by
This elseif statement is empty, and could be removed.

This check looks for the bodies of elseif statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These elseif bodies can be removed. If you have an empty elseif but statements in the else branch, consider inverting the condition.

Loading history...
376
            // nginx
377
        }
378
    }
379
380 1
    private function setPDO()
381
    {
382 1
        $config_file = $this->config_path . '/database.yml';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
383 1
        $config = Yaml::parse(file_get_contents($config_file));
384
385
        try {
386 1
            $this->PDO = \Doctrine\DBAL\DriverManager::getConnection($config['database'], new \Doctrine\DBAL\Configuration());
387 1
            $this->PDO->connect();
388
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
389
        } catch (\Exception $e) {
390
            $this->PDO->close();
391
            throw $e;
392
        }
393
394 1
        return $this;
395
    }
396
397 View Code Duplication
    private function dropTables()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
398
    {
399
        $this->resetNatTimer();
400
401
        $em = $this->getEntityManager();
402
        $metadatas = $em->getMetadataFactory()->getAllMetadata();
403
        $schemaTool = new SchemaTool($em);
404
405
        $schemaTool->dropSchema($metadatas);
406
407
        $em->getConnection()->executeQuery('DROP TABLE IF EXISTS doctrine_migration_versions');
408
409
        return $this;
410
    }
411
412
    /**
413
     * @return EntityManager
414
     */
415
    private function getEntityManager()
416
    {
417
        $config_file = $this->config_path . '/database.yml';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
418
        $database = Yaml::parse(file_get_contents($config_file));
419
420
        $this->app->register(new \Silex\Provider\DoctrineServiceProvider(), array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
421
            'db.options' => $database['database']
422
        ));
423
424
        $this->app->register(new \Dflydev\Silex\Provider\DoctrineOrm\DoctrineOrmServiceProvider(), array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
425
            'orm.proxies_dir' => __DIR__ . '/../../app/cache/doctrine',
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
426
            'orm.em.options' => array(
427
                'mappings' => array(
428
                    array(
429
                        'type' => 'yml',
430
                        'namespace' => 'Eccube\Entity',
431
                        'path' => array(
432
                            __DIR__ . '/../../Resource/doctrine',
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
433
                            __DIR__ . '/../../Resource/doctrine/master',
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
434
                        ),
435
                    ),
436
437
                ),
438
            )
439
        ));
440
441
        return $em = $this->app['orm.em'];
0 ignored issues
show
Unused Code introduced by
$em is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
442
    }
443
444 View Code Duplication
    private function createTables()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
445
    {
446
        $this->resetNatTimer();
447
448
        $em = $this->getEntityManager();
449
        $metadatas = $em->getMetadataFactory()->getAllMetadata();
450
        $schemaTool = new SchemaTool($em);
451
452
        $schemaTool->createSchema($metadatas);
453
454
        return $this;
455
    }
456
457
    private function insert()
458
    {
459
        $this->resetNatTimer();
460
461
        $config_file = $this->config_path . '/database.yml';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
462
        $database = Yaml::parse(file_get_contents($config_file));
463
        $config['database'] = $database['database'];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$config was never initialized. Although not strictly required by PHP, it is generally a good practice to add $config = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
464
465
        $config_file = $this->config_path . '/config.yml';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
466
        $baseConfig = Yaml::parse(file_get_contents($config_file));
467
        $config['config'] = $baseConfig;
468
469
        $this->PDO->beginTransaction();
470
471
        try {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
472
473
            $config = array(
474
                'auth_type' => '',
475
                'auth_magic' => $config['config']['auth_magic'],
476
                'password_hash_algos' => 'sha256',
477
            );
478
            $passwordEncoder = new \Eccube\Security\Core\Encoder\PasswordEncoder($config);
479
            $salt = \Eccube\Util\Str::random(32);
480
481
            $encodedPassword = $passwordEncoder->encodePassword($this->session_data['login_pass'], $salt);
482
            $sth = $this->PDO->prepare('INSERT INTO dtb_base_info (
483
                id,
484
                shop_name,
485
                email01,
486
                email02,
487
                email03,
488
                email04,
489
                update_date,
490
                option_product_tax_rule
491
            ) VALUES (
492
                1,
493
                :shop_name,
494
                :admin_mail,
495
                :admin_mail,
496
                :admin_mail,
497
                :admin_mail,
498
                current_timestamp,
499
                0);');
500
            $sth->execute(array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
501
                ':shop_name' => $this->session_data['shop_name'],
502
                ':admin_mail' => $this->session_data['email']
503
            ));
504
505
            $sth = $this->PDO->prepare("INSERT INTO dtb_member (member_id, login_id, password, salt, work, del_flg, authority, creator_id, rank, update_date, create_date,name,department) VALUES (2, :login_id, :admin_pass , :salt , '1', '0', '0', '1', '1', current_timestamp, current_timestamp,'管理者','EC-CUBE SHOP');");
506
            $sth->execute(array(':login_id' => $this->session_data['login_id'], ':admin_pass' => $encodedPassword, ':salt' => $salt));
507
508
            $this->PDO->commit();
509
        } catch (\Exception $e) {
510
            $this->PDO->rollback();
511
            throw $e;
512
        }
513
514
        return $this;
515
    }
516
517
    private function update()
518
    {
519
        $this->resetNatTimer();
520
521
        $config_file = $this->config_path . '/database.yml';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
522
        $database = Yaml::parse(file_get_contents($config_file));
523
        $config['database'] = $database['database'];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$config was never initialized. Although not strictly required by PHP, it is generally a good practice to add $config = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
524
525
        $config_file = $this->config_path . '/config.yml';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
526
        $baseConfig = Yaml::parse(file_get_contents($config_file));
527
        $config['config'] = $baseConfig;
528
529
        $this->PDO->beginTransaction();
530
531
        try {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
532
533
            $config = array(
534
                'auth_type' => '',
535
                'auth_magic' => $config['config']['auth_magic'],
536
                'password_hash_algos' => 'sha256',
537
            );
538
            $passwordEncoder = new \Eccube\Security\Core\Encoder\PasswordEncoder($config);
539
            $salt = \Eccube\Util\Str::random(32);
540
541
            $stmt = $this->PDO->prepare("SELECT member_id FROM dtb_member WHERE login_id = :login_id;");
542
            $stmt->execute(array(':login_id' => $this->session_data['login_id']));
543
            $rs = $stmt->fetch();
544
545
            $encodedPassword = $passwordEncoder->encodePassword($this->session_data['login_pass'], $salt);
546
547
            if ($rs) {
548
                // 同一の管理者IDであればパスワードのみ更新
549
                $sth = $this->PDO->prepare("UPDATE dtb_member set password = :admin_pass, salt = :salt, update_date = current_timestamp WHERE login_id = :login_id;");
550
                $sth->execute(array(':admin_pass' => $encodedPassword, ':salt' => $salt, ':login_id' => $this->session_data['login_id']));
551
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
552
            } else {
553
                // 新しい管理者IDが入力されたらinsert
554
                $sth = $this->PDO->prepare("INSERT INTO dtb_member (login_id, password, salt, work, del_flg, authority, creator_id, rank, update_date, create_date,name,department) VALUES (:login_id, :admin_pass , :salt , '1', '0', '0', '1', '1', current_timestamp, current_timestamp,'管理者','EC-CUBE SHOP');");
555
                $sth->execute(array(':login_id' => $this->session_data['login_id'], ':admin_pass' => $encodedPassword, ':salt' => $salt));
556
            }
557
558
            $sth = $this->PDO->prepare('UPDATE dtb_base_info set
559
                shop_name = :shop_name,
560
                email01 = :admin_mail,
561
                email02 = :admin_mail,
562
                email03 = :admin_mail,
563
                email04 = :admin_mail,
564
                update_date = current_timestamp
565
            WHERE id = 1;');
566
            $sth->execute(array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
567
                ':shop_name' => $this->session_data['shop_name'],
568
                ':admin_mail' => $this->session_data['email']
569
            ));
570
571
            $this->PDO->commit();
572
        } catch (\Exception $e) {
573
            $this->PDO->rollback();
574
            throw $e;
575
        }
576
577
        return $this;
578
    }
579
580
581
    private function getMigration()
582
    {
583
        $app = \Eccube\Application::getInstance();
584
        $app->initialize();
585
        $app->boot();
586
587
        $config = new Configuration($app['db']);
588
        $config->setMigrationsNamespace('DoctrineMigrations');
589
590
        $migrationDir = __DIR__ . '/../../Resource/doctrine/migration';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
591
        $config->setMigrationsDirectory($migrationDir);
592
        $config->registerMigrationsFromDirectory($migrationDir);
593
594
        $migration = new Migration($config);
595
596
        return $migration;
597
    }
598
599
    private function doMigrate()
600
    {
601
        try {
602
            $migration = $this->getMigration();
603
604
            // DBとのコネクションを維持するためpingさせる
605
            if (is_null($this->PDO)) {
606
                $this->setPDO();
607
            }
608
            $this->PDO->ping();
609
610
            // nullを渡すと最新バージョンまでマイグレートする
611
            $migration->migrate(null, false);
612
        } catch (MigrationException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
613
        }
614
615
        return $this;
616
    }
617
618 1
    private function getProtectedDirs()
619
    {
620 1
        $protectedDirs = array();
621 1
        $base = __DIR__ . '/../../../..';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
622
        $dirs = array(
623 1
            '/html',
624
            '/app',
625
            '/app/template',
626
            '/app/cache',
627
            '/app/config',
628
            '/app/config/eccube',
629
            '/app/log',
630
            '/app/Plugin',
631
        );
632
633 1
        foreach ($dirs as $dir) {
634 1
            if (!is_writable($base . $dir)) {
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
635 1
                $protectedDirs[] = $dir;
636
            }
637
        }
638
639 1
        return $protectedDirs;
640
    }
641
642
    private function createConfigYamlFile($data, $auth = true)
643
    {
644
        $fs = new Filesystem();
645
        $config_file = $this->config_path . '/config.yml';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
646
647
        if ($fs->exists($config_file)) {
648
            $config = Yaml::parse(file_get_contents($config_file));
649
            $fs->remove($config_file);
650
        }
651
652
        if ($auth) {
653
            $auth_magic = Str::random(32);
654
        } else {
655
            if (isset($config['auth_magic'])) {
656
                $auth_magic = $config['auth_magic'];
657
            } else {
658
                $auth_magic = Str::random(32);
659
            }
660
        }
661
662
        $allowHost = Str::convertLineFeed($data['admin_allow_hosts']);
663
        if (empty($allowHost)) {
664
            $adminAllowHosts = array();
665
        } else {
666
            $adminAllowHosts = explode("\n", $allowHost);
667
        }
668
669
        $target = array('${AUTH_MAGIC}', '${SHOP_NAME}', '${ECCUBE_INSTALL}', '${FORCE_SSL}');
670
        $replace = array($auth_magic, $data['shop_name'], '0', $data['admin_force_ssl']);
671
672
        $fs = new Filesystem();
673
        $content = str_replace(
674
            $target,
675
            $replace,
676
            file_get_contents($this->dist_path . '/config.yml.dist')
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
677
        );
678
        $fs->dumpFile($config_file, $content);
679
680
        $config = Yaml::parse(file_get_contents($config_file));
681
        $config['admin_allow_host'] = $adminAllowHosts;
682
        $yml = Yaml::dump($config);
683
        file_put_contents($config_file, $yml);
684
685
        return $this;
686
    }
687
688
    private function addInstallStatus()
689
    {
690
        $config_file = $this->config_path . '/config.yml';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
691
        $config = Yaml::parse(file_get_contents($config_file));
692
        $config['eccube_install'] = 1;
693
        $yml = Yaml::dump($config);
694
        file_put_contents($config_file, $yml);
695
696
        return $this;
697
    }
698
699
    private function createDatabaseYamlFile($data)
700
    {
701
        $fs = new Filesystem();
702
        $config_file = $this->config_path . '/database.yml';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
703
        if ($fs->exists($config_file)) {
704
            $fs->remove($config_file);
705
        }
706
707
        if ($data['database'] != 'pdo_sqlite') {
708
            switch ($data['database']) {
709
                case 'pdo_pgsql':
710
                    if (empty($data['db_port'])) {
711
                        $data['db_port'] = '5432';
712
                    }
713
                    $data['db_driver'] = 'pdo_pgsql';
714
                    break;
715
                case 'pdo_mysql':
716
                    if (empty($data['db_port'])) {
717
                        $data['db_port'] = '3306';
718
                    }
719
                    $data['db_driver'] = 'pdo_mysql';
720
                    break;
721
            }
722
            $target = array('${DBDRIVER}', '${DBSERVER}', '${DBNAME}', '${DBPORT}', '${DBUSER}', '${DBPASS}');
723
            $replace = array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
724
                $data['db_driver'],
725
                $data['database_host'],
726
                $data['database_name'],
727
                $data['database_port'],
728
                $data['database_user'],
729
                $data['database_password']
730
            );
731
732
            $fs = new Filesystem();
733
            $content = str_replace(
734
                $target,
735
                $replace,
736
                file_get_contents($this->dist_path . '/database.yml.dist')
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
737
            );
738
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
739
        } else {
740
            $content = Yaml::dump(
741
                array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
742
                    'database' => array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
743
                        'driver' => 'pdo_sqlite',
744
                        'path' => realpath($this->config_path.'/eccube.db')
745
                    )
746
                )
747
            );
748
        }
749
        $fs->dumpFile($config_file, $content);
750
751
        return $this;
752
    }
753
754
    private function createMailYamlFile($data)
755
    {
756
        $fs = new Filesystem();
757
        $config_file = $this->config_path . '/mail.yml';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
758
        if ($fs->exists($config_file)) {
759
            $fs->remove($config_file);
760
        }
761
        $target = array('${MAIL_BACKEND}', '${MAIL_HOST}', '${MAIL_PORT}', '${MAIL_USER}', '${MAIL_PASS}');
762
        $replace = array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
763
            $data['mail_backend'],
764
            $data['smtp_host'],
765
            $data['smtp_port'],
766
            $data['smtp_username'],
767
            $data['smtp_password']
768
        );
769
770
        $fs = new Filesystem();
771
        $content = str_replace(
772
            $target,
773
            $replace,
774
            file_get_contents($this->dist_path . '/mail.yml.dist')
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
775
        );
776
        $fs->dumpFile($config_file, $content);
777
778
        return $this;
779
    }
780
781
    private function createPathYamlFile($data, Request $request)
782
    {
783
        $fs = new Filesystem();
784
        $config_file = $this->config_path . '/path.yml';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
785
        if ($fs->exists($config_file)) {
786
            $fs->remove($config_file);
787
        }
788
789
        $ADMIN_ROUTE = $data['admin_dir'];
790
        $TEMPLATE_CODE = 'default';
791
        $USER_DATA_ROUTE = 'user_data';
792
        $ROOT_DIR = realpath(__DIR__ . '/../../../../');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
793
        $ROOT_URLPATH = $request->getBasePath();
794
        $ROOT_PUBLIC_URLPATH = $ROOT_URLPATH . RELATIVE_PUBLIC_DIR_PATH;
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
795
796
        $target = array('${ADMIN_ROUTE}', '${TEMPLATE_CODE}', '${USER_DATA_ROUTE}', '${ROOT_DIR}', '${ROOT_URLPATH}', '${ROOT_PUBLIC_URLPATH}');
797
        $replace = array($ADMIN_ROUTE, $TEMPLATE_CODE, $USER_DATA_ROUTE, $ROOT_DIR, $ROOT_URLPATH, $ROOT_PUBLIC_URLPATH);
798
799
        $fs = new Filesystem();
800
        $content = str_replace(
801
            $target,
802
            $replace,
803
            file_get_contents($this->dist_path . '/path.yml.dist')
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
804
        );
805
        $fs->dumpFile($config_file, $content);
806
807
        return $this;
808
    }
809
810
    private function sendAppData($params)
811
    {
812
        $config_file = $this->config_path . '/database.yml';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
813
        $db_config = Yaml::parse(file_get_contents($config_file));
814
815
        $this->setPDO();
816
        $stmt = $this->PDO->query('select version() as v');
817
818
        $version = '';
819
        foreach ($stmt as $row) {
820
            $version = $row['v'];
821
        }
822
823
        if ($db_config['database']['driver'] === 'pdo_mysql') {
824
            $db_ver = 'MySQL:' . $version;
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
825
        } else {
826
            $db_ver = $version;
827
        }
828
829
        $data = http_build_query(
830
            array(
831
                'site_url' => $params['http_url'],
832
                'shop_name' => $params['shop_name'],
833
                'cube_ver' => Constant::VERSION,
834
                'php_ver' => phpversion(),
835
                'db_ver' => $db_ver,
836
                'os_type' => php_uname(),
837
            )
838
        );
839
840
        $header = array(
841
            'Content-Type: application/x-www-form-urlencoded',
842
            'Content-Length: ' . strlen($data),
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
843
        );
844
        $context = stream_context_create(
845
            array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
846
                'http' => array(
847
                    'method' => 'POST',
848
                    'header' => $header,
849
                    'content' => $data,
850
                )
851
            )
852
        );
853
        file_get_contents('http://www.ec-cube.net/mall/use_site.php', false, $context);
854
855
        return $this;
856
    }
857
858
859
    /**
860
     * マイグレーション画面を表示する.
861
     *
862
     * @param InstallApplication $app
863
     * @param Request $request
0 ignored issues
show
introduced by
Expected 12 spaces after parameter type; 1 found
Loading history...
864
     *
865
     * @return \Symfony\Component\HttpFoundation\Response
866
     */
867
    public function migration(InstallApplication $app, Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
introduced by
Declare public methods first, then protected ones and finally private ones
Loading history...
868
    {
869
        return $app['twig']->render('migration.twig', array(
870
            'publicPath' => '..' . RELATIVE_PUBLIC_DIR_PATH . '/',
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
871
        ));
872
    }
873
874
    /**
875
     * インストール済プラグインの一覧を表示する.
876
     * プラグインがインストールされていない場合は, マイグレーション実行画面へリダイレクトする.
877
     *
878
     * @param InstallApplication $app
879
     * @param Request $request
0 ignored issues
show
introduced by
Expected 12 spaces after parameter type; 1 found
Loading history...
880
     *
881
     * @return \Symfony\Component\HttpFoundation\Response
882
     */
883
    public function migration_plugin(InstallApplication $app, Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Coding Style introduced by
Method name "InstallController::migration_plugin" is not in camel caps format
Loading history...
884
    {
885
        $eccube = \Eccube\Application::getInstance();
886
        $eccube->initialize();
887
        $eccube->boot();
888
889
        $pluginRepository = $eccube['orm.em']->getRepository('Eccube\Entity\Plugin');
890
        $Plugins = $pluginRepository->findBy(array('del_flg' => Constant::DISABLED));
891
892
        if (empty($Plugins)) {
893
            // インストール済プラグインがない場合はマイグレーション実行画面へリダイレクト.
894
            return $app->redirect($app->url('migration_end'));
895
        } else {
896
            return $app['twig']->render('migration_plugin.twig', array(
897
                'Plugins' => $Plugins,
898
                'version' => Constant::VERSION,
899
                'publicPath' => '..' . RELATIVE_PUBLIC_DIR_PATH . '/',
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
900
            ));
901
        }
902
    }
903
904
    /**
905
     * マイグレーションを実行し, 完了画面を表示させる
906
     *
907
     * @param InstallApplication $app
908
     * @param Request $request
0 ignored issues
show
introduced by
Expected 12 spaces after parameter type; 1 found
Loading history...
909
     *
910
     * @return \Symfony\Component\HttpFoundation\Response
911
     */
912
    public function migration_end(InstallApplication $app, Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Coding Style introduced by
Method name "InstallController::migration_end" is not in camel caps format
Loading history...
913
    {
914
        $this->doMigrate();
915
916
        $config_app = new \Eccube\Application(); // install用のappだとconfigが取れないので
917
        $config_app->initialize();
918
        $config_app->boot();
919
        \Eccube\Util\Cache::clear($config_app, true);
920
921
        return $app['twig']->render('migration_end.twig', array(
922
            'publicPath' => '..' . RELATIVE_PUBLIC_DIR_PATH . '/',
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
923
        ));
924
    }
925
}
926