InstallController::createDatabaseYamlFile()   B
last analyzed

Complexity

Conditions 7
Paths 12

Size

Total Lines 52
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 36
nc 12
nop 1
dl 0
loc 52
ccs 0
cts 32
cp 0
crap 56
rs 7.2396
c 1
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.co.jp/
9
 *
10
 * This program is free software; you can redistribute it and/or
11
 * modify it under the terms of the GNU General Public License
12
 * as published by the Free Software Foundation; either version 2
13
 * of the License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program; if not, write to the Free Software
22
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
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
44
    const MCRYPT = 'mcrypt';
45
46
    private $app;
47
    private $PDO;
48
    private $config_path;
49
    private $dist_path;
50
    private $cache_path;
51
    private $session_data;
52
    private $required_modules = array('pdo', 'phar', 'mbstring', 'zlib', 'ctype', 'session', 'JSON', 'xml', 'libxml', 'OpenSSL', 'zip', 'cURL', 'fileinfo');
53
    private $recommended_module = array('hash', self::MCRYPT);
54
55
    const SESSION_KEY = 'eccube.session.install';
56
57 7
    public function __construct()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
58
    {
59 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...
60 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...
61 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...
62
    }
63
64 4
    private function isValid(Request $request, Form $form)
65
    {
66 4
        $session = $request->getSession();
67 4
        if ('POST' === $request->getMethod()) {
68
            $form->handleRequest($request);
69
            if ($form->isValid()) {
70
                $sessionData = $session->get(self::SESSION_KEY) ?: array();
71
                $formData = array_replace_recursive($sessionData, $form->getData());
72
                $session->set(self::SESSION_KEY, $formData);
73
74
                return true;
75
            }
76
        }
77
78 4
        return false;
79
    }
80
81 5
    private function getSessionData(Request $request)
82
    {
83 5
        return $this->session_data = $request->getSession()->get(self::SESSION_KEY);
84
    }
85
86
    // 最初からやり直す場合、SESSION情報をクリア
87 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...
88
    {
89 1
        $request->getSession()->remove(self::SESSION_KEY);
90
91 1
        return $app->redirect($app->url('install_step1'));
92
    }
93
94
    // ようこそ
95 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...
96
    {
97 1
        $form = $app['form.factory']
98 1
            ->createBuilder('install_step1')
99 1
            ->getForm();
100 1
        $sessionData = $this->getSessionData($request);
101 1
        $form->setData($sessionData);
102
103 1
        if ($this->isValid($request, $form)) {
104
            return $app->redirect($app->url('install_step2'));
105
        }
106
107 1
        $this->checkModules($app);
108
109 1
        return $app['twig']->render('step1.twig', array(
110 1
                'form' => $form->createView(),
111 1
                'publicPath' => '..' . RELATIVE_PUBLIC_DIR_PATH . '/',
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
112
        ));
113
    }
114
115
    // 権限チェック
116 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...
117
    {
118 1
        $this->getSessionData($request);
119
120 1
        $protectedDirs = $this->getProtectedDirs();
121
122
        // 権限がある場合, キャッシュディレクトリをクリア
123 1
        if (empty($protectedDirs)) {
124 1
            $finder = Finder::create()
125 1
                ->in($this->cache_path)
126 1
                ->directories()
127 1
                ->depth(0);
128 1
            $fs = new Filesystem();
129 1
            $fs->remove($finder);
130
        }
131
132 1
        return $app['twig']->render('step2.twig', array(
133 1
                'protectedDirs' => $protectedDirs,
134 1
                'publicPath' => '..' . RELATIVE_PUBLIC_DIR_PATH . '/',
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
135
        ));
136
    }
137
138
    //    サイトの設定
139 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...
140
    {
141 1
        $form = $app['form.factory']
142 1
            ->createBuilder('install_step3')
143 1
            ->getForm();
144 1
        $sessionData = $this->getSessionData($request);
145
146 1
        if (empty($sessionData['shop_name'])) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
147
148 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...
149 1
            $fs = new Filesystem();
150
151 1
            if ($fs->exists($config_file)) {
152
                // すでに登録されていた場合、登録データを表示
153 1
                $this->setPDO();
154 1
                $stmt = $this->PDO->query("SELECT shop_name, email01 FROM dtb_base_info WHERE id = 1;");
155
156 1
                foreach ($stmt as $row) {
157 1
                    $sessionData['shop_name'] = $row['shop_name'];
158 1
                    $sessionData['email'] = $row['email01'];
159
                }
160
161
                // セキュリティの設定
162 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...
163 1
                $config = Yaml::parse(file_get_contents($config_file));
164 1
                $sessionData['admin_dir'] = $config['admin_route'];
165
166 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...
167 1
                $config = Yaml::parse(file_get_contents($config_file));
168
169 1
                $allowHost = $config['admin_allow_host'];
170 1
                if (count($allowHost) > 0) {
171
                    $sessionData['admin_allow_hosts'] = Str::convertLineFeed(implode("\n", $allowHost));
172
                }
173 1
                $sessionData['admin_force_ssl'] = (bool) $config['force_ssl'];
174
175
                // メール設定
176 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...
177 1
                $config = Yaml::parse(file_get_contents($config_file));
178 1
                $mail = $config['mail'];
179 1
                $sessionData['mail_backend'] = $mail['transport'];
180 1
                $sessionData['smtp_host'] = $mail['host'];
181 1
                $sessionData['smtp_port'] = $mail['port'];
182 1
                $sessionData['smtp_username'] = $mail['username'];
183 1
                $sessionData['smtp_password'] = $mail['password'];
184
            } else {
185
                // 初期値にmailを設定
186
                $sessionData['mail_backend'] = 'mail';
187
            }
188
        }
189
190 1
        $form->setData($sessionData);
191 1
        if ($this->isValid($request, $form)) {
192
            $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...
193
194
            return $app->redirect($app->url('install_step4'));
195
        }
196
197 1
        return $app['twig']->render('step3.twig', array(
198 1
                'form' => $form->createView(),
199 1
                'publicPath' => '..' . RELATIVE_PUBLIC_DIR_PATH . '/',
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
200
        ));
201
    }
202
203
    //    データベースの設定
204 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...
205
    {
206 1
        $form = $app['form.factory']
207 1
            ->createBuilder('install_step4')
208 1
            ->getForm();
209
210 1
        $sessionData = $this->getSessionData($request);
211
212 1
        if (empty($sessionData['database'])) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
213
214 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...
215 1
            $fs = new Filesystem();
216
217 1
            if ($fs->exists($config_file)) {
218
                // すでに登録されていた場合、登録データを表示
219
                // データベース設定
220 1
                $config = Yaml::parse(file_get_contents($config_file));
221 1
                $database = $config['database'];
222 1
                $sessionData['database'] = $database['driver'];
223 1
                if ($database['driver'] != 'pdo_sqlite') {
224 1
                    $sessionData['database_host'] = $database['host'];
225 1
                    $sessionData['database_port'] = $database['port'];
226 1
                    $sessionData['database_name'] = $database['dbname'];
227 1
                    $sessionData['database_user'] = $database['user'];
228 1
                    $sessionData['database_password'] = $database['password'];
229
                }
230
            }
231
        }
232
233 1
        $form->setData($sessionData);
234
235 1
        if ($this->isValid($request, $form)) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
236
237
            return $app->redirect($app->url('install_step5'));
238
        }
239
240 1
        return $app['twig']->render('step4.twig', array(
241 1
                'form' => $form->createView(),
242 1
                'publicPath' => '..' . RELATIVE_PUBLIC_DIR_PATH . '/',
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
243
        ));
244
    }
245
246
    //    データベースの初期化
247 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...
248
    {
249 1
        set_time_limit(0);
250 1
        $this->app = $app;
251 1
        $form = $app['form.factory']
252 1
            ->createBuilder('install_step5')
253 1
            ->getForm();
254 1
        $sessionData = $this->getSessionData($request);
255 1
        $form->setData($sessionData);
256
257 1
        if ($this->isValid($request, $form)) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
258
259
            $this
260
                ->createDatabaseYamlFile($sessionData)
261
                ->createMailYamlFile($sessionData)
262
                ->createPathYamlFile($sessionData, $request);
263
264
            if (!$form['no_update']->getData()) {
265
                set_time_limit(0);
266
                $this->createConfigYamlFile($sessionData);
267
268
                $this
269
                    ->setPDO()
270
                    ->dropTables()
271
                    ->createTables()
272
                    ->doMigrate()
273
                    ->insert();
274
            } else {
275
                // データベースを初期化しない場合、auth_magicは初期化しない
276
                $this->createConfigYamlFile($sessionData, false);
277
278
                $this
279
                    ->setPDO()
280
                    ->update();
281
            }
282
283
284
            if (isset($sessionData['agree']) && $sessionData['agree'] == '1') {
285
                $host = $request->getSchemeAndHttpHost();
286
                $basePath = $request->getBasePath();
287
                $params = array(
288
                    'http_url' => $host . $basePath,
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
289
                    'shop_name' => $sessionData['shop_name'],
290
                );
291
292
                $this->sendAppData($params);
293
            }
294
            $this->addInstallStatus();
295
296
            $request->getSession()->remove(self::SESSION_KEY);
297
298
            return $app->redirect($app->url('install_complete'));
299
        }
300
301 1
        return $app['twig']->render('step5.twig', array(
302 1
                'form' => $form->createView(),
303 1
                'publicPath' => '..' . RELATIVE_PUBLIC_DIR_PATH . '/',
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
304
        ));
305
    }
306
307
    //    インストール完了
308 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...
309
    {
310 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...
311 1
        $config = Yaml::parse(file_get_contents($config_file));
312
313 1
        $host = $request->getSchemeAndHttpHost();
314 1
        $basePath = $request->getBasePath();
315
316 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...
317
318 1
        return $app['twig']->render('complete.twig', array(
319 1
                'admin_url' => $adminUrl,
320 1
                'publicPath' => '..' . RELATIVE_PUBLIC_DIR_PATH . '/',
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
321
        ));
322
    }
323
324
    private function resetNatTimer()
325
    {
326
        // NATの無通信タイマ対策(仮)
327
        echo str_repeat(' ', 4 * 1024);
328
        ob_flush();
329
        flush();
330
    }
331
332 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...
333
    {
334 1
        foreach ($this->required_modules as $module) {
335 1
            if (!extension_loaded($module)) {
336 1
                $app->addDanger('[必須] ' . $module . ' 拡張モジュールが有効になっていません。', 'install');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
337
            }
338
        }
339
340 1
        if (!extension_loaded('pdo_mysql') && !extension_loaded('pdo_pgsql')) {
341
            $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...
342
        }
343
344 1
        foreach ($this->recommended_module as $module) {
345 1
            if (!extension_loaded($module)) {
346
                if ($module == self::MCRYPT && PHP_VERSION_ID >= 70100) {
347
                    //The mcrypt extension has been deprecated in PHP 7.1.x 
348
                    //http://php.net/manual/en/migration71.deprecated.php
349
                    continue;
350
                }
351 1
                $app->addInfo('[推奨] '.$module.' 拡張モジュールが有効になっていません。', 'install');
352
            }
353
        }
354
355 1
        if ('\\' === DIRECTORY_SEPARATOR) { // for Windows
356
            if (!extension_loaded('wincache')) {
357
                $app->addInfo('[推奨] WinCache 拡張モジュールが有効になっていません。', 'install');
358
            }
359
        } else {
360 1
            if (!extension_loaded('apc')) {
361 1
                $app->addInfo('[推奨] APC 拡張モジュールが有効になっていません。', 'install');
362
            }
363
        }
364
365 1
        if (isset($_SERVER['SERVER_SOFTWARE']) && strpos('Apache', $_SERVER['SERVER_SOFTWARE']) !== false) {
366
            if (!function_exists('apache_get_modules')) {
367
                $app->addWarning('mod_rewrite が有効になっているか不明です。', 'install');
368
            } elseif (!in_array('mod_rewrite', apache_get_modules())) {
369
                $app->addDanger('[必須] ' . 'mod_rewriteを有効にしてください。', 'install');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
370
            }
371 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...
372
            // iis
373 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...
374
            // nginx
375
        }
376
    }
377
378 1
    private function setPDO()
379
    {
380 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...
381 1
        $config = Yaml::parse(file_get_contents($config_file));
382
383
        try {
384 1
            $this->PDO = \Doctrine\DBAL\DriverManager::getConnection($config['database'], new \Doctrine\DBAL\Configuration());
385 1
            $this->PDO->connect();
386
        } catch (\Exception $e) {
387
            $this->PDO->close();
388
            throw $e;
389
        }
390
391 1
        return $this;
392
    }
393
394 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...
395
    {
396
        $this->resetNatTimer();
397
398
        $em = $this->getEntityManager();
399
        $metadatas = $em->getMetadataFactory()->getAllMetadata();
400
        $schemaTool = new SchemaTool($em);
401
402
        $schemaTool->dropSchema($metadatas);
403
404
        $em->getConnection()->executeQuery('DROP TABLE IF EXISTS doctrine_migration_versions');
405
406
        return $this;
407
    }
408
409
    /**
410
     * @return EntityManager
411
     */
412
    private function getEntityManager()
413
    {
414
        $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...
415
        $database = Yaml::parse(file_get_contents($config_file));
416
417
        $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...
418
            'db.options' => $database['database']
419
        ));
420
421
        $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...
422
            '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...
423
            'orm.em.options' => array(
424
                'mappings' => array(
425
                    array(
426
                        'type' => 'yml',
427
                        'namespace' => 'Eccube\Entity',
428
                        'path' => array(
429
                            __DIR__ . '/../../Resource/doctrine',
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
430
                            __DIR__ . '/../../Resource/doctrine/master',
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
431
                        ),
432
                    ),
433
                ),
434
            )
435
        ));
436
437
        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...
438
    }
439
440 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...
441
    {
442
        $this->resetNatTimer();
443
444
        $em = $this->getEntityManager();
445
        $metadatas = $em->getMetadataFactory()->getAllMetadata();
446
        $schemaTool = new SchemaTool($em);
447
448
        $schemaTool->createSchema($metadatas);
449
450
        return $this;
451
    }
452
453
    private function insert()
454
    {
455
        $this->resetNatTimer();
456
457
        $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...
458
        $database = Yaml::parse(file_get_contents($config_file));
459
        $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...
460
461
        $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...
462
        $baseConfig = Yaml::parse(file_get_contents($config_file));
463
        $config['config'] = $baseConfig;
464
465
        $this->PDO->beginTransaction();
466
467
        try {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
468
469
            $config = array(
470
                'auth_type' => '',
471
                'auth_magic' => $config['config']['auth_magic'],
472
                'password_hash_algos' => 'sha256',
473
            );
474
            $passwordEncoder = new \Eccube\Security\Core\Encoder\PasswordEncoder($config);
475
            $salt = \Eccube\Util\Str::random(32);
476
477
            $encodedPassword = $passwordEncoder->encodePassword($this->session_data['login_pass'], $salt);
478
            $sth = $this->PDO->prepare('INSERT INTO dtb_base_info (
479
                id,
480
                shop_name,
481
                email01,
482
                email02,
483
                email03,
484
                email04,
485
                update_date,
486
                option_product_tax_rule
487
            ) VALUES (
488
                1,
489
                :shop_name,
490
                :admin_mail,
491
                :admin_mail,
492
                :admin_mail,
493
                :admin_mail,
494
                current_timestamp,
495
                0);');
496
            $sth->execute(array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
497
                ':shop_name' => $this->session_data['shop_name'],
498
                ':admin_mail' => $this->session_data['email']
499
            ));
500
501
            $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');");
502
            $sth->execute(array(':login_id' => $this->session_data['login_id'], ':admin_pass' => $encodedPassword, ':salt' => $salt));
503
504
            $this->PDO->commit();
505
        } catch (\Exception $e) {
506
            $this->PDO->rollback();
507
            throw $e;
508
        }
509
510
        return $this;
511
    }
512
513
    private function update()
514
    {
515
        $this->resetNatTimer();
516
517
        $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...
518
        $database = Yaml::parse(file_get_contents($config_file));
519
        $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...
520
521
        $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...
522
        $baseConfig = Yaml::parse(file_get_contents($config_file));
523
        $config['config'] = $baseConfig;
524
525
        $this->PDO->beginTransaction();
526
527
        try {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
528
529
            $config = array(
530
                'auth_type' => '',
531
                'auth_magic' => $config['config']['auth_magic'],
532
                'password_hash_algos' => 'sha256',
533
            );
534
            $passwordEncoder = new \Eccube\Security\Core\Encoder\PasswordEncoder($config);
535
            $salt = \Eccube\Util\Str::random(32);
536
537
            $stmt = $this->PDO->prepare("SELECT member_id FROM dtb_member WHERE login_id = :login_id;");
538
            $stmt->execute(array(':login_id' => $this->session_data['login_id']));
539
            $rs = $stmt->fetch();
540
541
            $encodedPassword = $passwordEncoder->encodePassword($this->session_data['login_pass'], $salt);
542
543
            if ($rs) {
544
                // 同一の管理者IDであればパスワードのみ更新
545
                $sth = $this->PDO->prepare("UPDATE dtb_member set password = :admin_pass, salt = :salt, update_date = current_timestamp WHERE login_id = :login_id;");
546
                $sth->execute(array(':admin_pass' => $encodedPassword, ':salt' => $salt, ':login_id' => $this->session_data['login_id']));
547
            } else {
548
                // 新しい管理者IDが入力されたらinsert
549
                $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');");
550
                $sth->execute(array(':login_id' => $this->session_data['login_id'], ':admin_pass' => $encodedPassword, ':salt' => $salt));
551
            }
552
553
            $sth = $this->PDO->prepare('UPDATE dtb_base_info set
554
                shop_name = :shop_name,
555
                email01 = :admin_mail,
556
                email02 = :admin_mail,
557
                email03 = :admin_mail,
558
                email04 = :admin_mail,
559
                update_date = current_timestamp
560
            WHERE id = 1;');
561
            $sth->execute(array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
562
                ':shop_name' => $this->session_data['shop_name'],
563
                ':admin_mail' => $this->session_data['email']
564
            ));
565
566
            $this->PDO->commit();
567
        } catch (\Exception $e) {
568
            $this->PDO->rollback();
569
            throw $e;
570
        }
571
572
        return $this;
573
    }
574
575
    private function getMigration()
576
    {
577
        $app = \Eccube\Application::getInstance();
578
        $app->initialize();
579
        $app->boot();
580
581
        $config = new Configuration($app['db']);
582
        $config->setMigrationsNamespace('DoctrineMigrations');
583
584
        $migrationDir = __DIR__ . '/../../Resource/doctrine/migration';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
585
        $config->setMigrationsDirectory($migrationDir);
586
        $config->registerMigrationsFromDirectory($migrationDir);
587
588
        $migration = new Migration($config);
589
590
        return $migration;
591
    }
592
593
    private function doMigrate()
594
    {
595
        try {
596
            $migration = $this->getMigration();
597
598
            // DBとのコネクションを維持するためpingさせる
599
            if (is_null($this->PDO)) {
600
                $this->setPDO();
601
            }
602
            $this->PDO->ping();
603
604
            // nullを渡すと最新バージョンまでマイグレートする
605
            $migration->migrate(null, false);
606
        } catch (MigrationException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
Coding Style introduced by
Blank line found at start of control structure
Loading history...
607
            
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
608
        }
609
610
        return $this;
611
    }
612
613 1
    private function getProtectedDirs()
614
    {
615 1
        $protectedDirs = array();
616 1
        $base = __DIR__ . '/../../../..';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
617
        $dirs = array(
618 1
            '/html',
619
            '/app',
620
            '/app/template',
621
            '/app/cache',
622
            '/app/config',
623
            '/app/config/eccube',
624
            '/app/log',
625
            '/app/Plugin',
626
        );
627
628 1
        foreach ($dirs as $dir) {
629 1
            if (!is_writable($base . $dir)) {
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
630 1
                $protectedDirs[] = $dir;
631
            }
632
        }
633
634 1
        return $protectedDirs;
635
    }
636
637
    private function createConfigYamlFile($data, $auth = true)
638
    {
639
        $fs = new Filesystem();
640
        $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...
641
642
        if ($fs->exists($config_file)) {
643
            $config = Yaml::parse(file_get_contents($config_file));
644
            $fs->remove($config_file);
645
        }
646
647
        if ($auth) {
648
            $auth_magic = Str::random(32);
649
        } else {
650
            if (isset($config['auth_magic'])) {
651
                $auth_magic = $config['auth_magic'];
652
            } else {
653
                $auth_magic = Str::random(32);
654
            }
655
        }
656
657
        $allowHost = Str::convertLineFeed($data['admin_allow_hosts']);
658
        if (empty($allowHost)) {
659
            $adminAllowHosts = array();
660
        } else {
661
            $adminAllowHosts = explode("\n", $allowHost);
662
        }
663
664
        $target = array('${AUTH_MAGIC}', '${SHOP_NAME}', '${ECCUBE_INSTALL}', '${FORCE_SSL}');
665
        $replace = array($auth_magic, $data['shop_name'], '0', $data['admin_force_ssl']);
666
667
        $fs = new Filesystem();
668
        $content = str_replace(
669
            $target, $replace, 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...
670
        );
671
        $fs->dumpFile($config_file, $content);
672
673
        $config = Yaml::parse(file_get_contents($config_file));
674
        $config['admin_allow_host'] = $adminAllowHosts;
675
        $yml = Yaml::dump($config);
676
        file_put_contents($config_file, $yml);
677
678
        return $this;
679
    }
680
681
    private function addInstallStatus()
682
    {
683
        $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...
684
        $config = Yaml::parse(file_get_contents($config_file));
685
        $config['eccube_install'] = 1;
686
        $yml = Yaml::dump($config);
687
        file_put_contents($config_file, $yml);
688
689
        return $this;
690
    }
691
692
    private function createDatabaseYamlFile($data)
693
    {
694
        $fs = new Filesystem();
695
        $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...
696
        if ($fs->exists($config_file)) {
697
            $fs->remove($config_file);
698
        }
699
700
        if ($data['database'] != 'pdo_sqlite') {
701
            switch ($data['database'])
702
            {
703
                case 'pdo_pgsql':
704
                    if (empty($data['db_port'])) {
705
                        $data['db_port'] = '5432';
706
                    }
707
                    $data['db_driver'] = 'pdo_pgsql';
708
                    break;
709
                case 'pdo_mysql':
710
                    if (empty($data['db_port'])) {
711
                        $data['db_port'] = '3306';
712
                    }
713
                    $data['db_driver'] = 'pdo_mysql';
714
                    break;
715
            }
716
            $target = array('${DBDRIVER}', '${DBSERVER}', '${DBNAME}', '${DBPORT}', '${DBUSER}', '${DBPASS}');
717
            $replace = array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
718
                $data['db_driver'],
719
                $data['database_host'],
720
                $data['database_name'],
721
                $data['database_port'],
722
                $data['database_user'],
723
                $data['database_password']
724
            );
725
726
            $fs = new Filesystem();
727
            $content = str_replace(
728
                $target, $replace, 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...
729
            );
730
        } else {
731
            $content = Yaml::dump(
732
                    array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
733
                        'database' => array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
734
                            'driver' => 'pdo_sqlite',
735
                            'path' => realpath($this->config_path . '/eccube.db')
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
736
                        )
737
                    )
738
            );
739
        }
740
        $fs->dumpFile($config_file, $content);
741
742
        return $this;
743
    }
744
745
    private function createMailYamlFile($data)
746
    {
747
        $fs = new Filesystem();
748
        $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...
749
        if ($fs->exists($config_file)) {
750
            $fs->remove($config_file);
751
        }
752
        $target = array('${MAIL_BACKEND}', '${MAIL_HOST}', '${MAIL_PORT}', '${MAIL_USER}', '${MAIL_PASS}');
753
        $replace = array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
754
            $data['mail_backend'],
755
            $data['smtp_host'],
756
            $data['smtp_port'],
757
            $data['smtp_username'],
758
            $data['smtp_password']
759
        );
760
761
        $fs = new Filesystem();
762
        $content = str_replace(
763
            $target, $replace, 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...
764
        );
765
        $fs->dumpFile($config_file, $content);
766
767
        return $this;
768
    }
769
770
    private function createPathYamlFile($data, Request $request)
771
    {
772
        $fs = new Filesystem();
773
        $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...
774
        if ($fs->exists($config_file)) {
775
            $fs->remove($config_file);
776
        }
777
778
        $ADMIN_ROUTE = $data['admin_dir'];
779
        $TEMPLATE_CODE = 'default';
780
        $USER_DATA_ROUTE = 'user_data';
781
        $ROOT_DIR = realpath(__DIR__ . '/../../../../');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
782
        $ROOT_URLPATH = $request->getBasePath();
783
        $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...
784
785
        $target = array('${ADMIN_ROUTE}', '${TEMPLATE_CODE}', '${USER_DATA_ROUTE}', '${ROOT_DIR}', '${ROOT_URLPATH}', '${ROOT_PUBLIC_URLPATH}');
786
        $replace = array($ADMIN_ROUTE, $TEMPLATE_CODE, $USER_DATA_ROUTE, $ROOT_DIR, $ROOT_URLPATH, $ROOT_PUBLIC_URLPATH);
787
788
        $fs = new Filesystem();
789
        $content = str_replace(
790
            $target, $replace, 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...
791
        );
792
        $fs->dumpFile($config_file, $content);
793
794
        return $this;
795
    }
796
797
    private function sendAppData($params)
798
    {
799
        $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...
800
        $db_config = Yaml::parse(file_get_contents($config_file));
801
802
        $this->setPDO();
803
        $stmt = $this->PDO->query('select version() as v');
804
805
        $version = '';
806
        foreach ($stmt as $row) {
807
            $version = $row['v'];
808
        }
809
810
        if ($db_config['database']['driver'] === 'pdo_mysql') {
811
            $db_ver = 'MySQL:' . $version;
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
812
        } else {
813
            $db_ver = $version;
814
        }
815
816
        $data = http_build_query(
817
            array(
818
                'site_url' => $params['http_url'],
819
                'shop_name' => $params['shop_name'],
820
                'cube_ver' => Constant::VERSION,
821
                'php_ver' => phpversion(),
822
                'db_ver' => $db_ver,
823
                'os_type' => php_uname(),
824
            )
825
        );
826
827
        $header = array(
828
            'Content-Type: application/x-www-form-urlencoded',
829
            'Content-Length: ' . strlen($data),
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
830
        );
831
        $context = stream_context_create(
832
            array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
833
                'http' => array(
834
                    'method' => 'POST',
835
                    'header' => $header,
836
                    'content' => $data,
837
                )
838
            )
839
        );
840
        file_get_contents('http://www.ec-cube.net/mall/use_site.php', false, $context);
841
842
        return $this;
843
    }
844
845
    /**
846
     * マイグレーション画面を表示する.
847
     *
848
     * @param InstallApplication $app
849
     * @param Request $request
0 ignored issues
show
introduced by
Expected 12 spaces after parameter type; 1 found
Loading history...
850
     *
851
     * @return \Symfony\Component\HttpFoundation\Response
852
     */
853
    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...
854
    {
855
        return $app['twig']->render('migration.twig', array(
856
                'publicPath' => '..' . RELATIVE_PUBLIC_DIR_PATH . '/',
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
857
        ));
858
    }
859
860
    /**
861
     * インストール済プラグインの一覧を表示する.
862
     * プラグインがインストールされていない場合は, マイグレーション実行画面へリダイレクトする.
863
     *
864
     * @param InstallApplication $app
865
     * @param Request $request
0 ignored issues
show
introduced by
Expected 12 spaces after parameter type; 1 found
Loading history...
866
     *
867
     * @return \Symfony\Component\HttpFoundation\Response
868
     */
869
    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...
870
    {
871
        $eccube = \Eccube\Application::getInstance();
872
        $eccube->initialize();
873
        $eccube->boot();
874
875
        $pluginRepository = $eccube['orm.em']->getRepository('Eccube\Entity\Plugin');
876
        $Plugins = $pluginRepository->findBy(array('del_flg' => Constant::DISABLED));
877
878
        if (empty($Plugins)) {
879
            // インストール済プラグインがない場合はマイグレーション実行画面へリダイレクト.
880
            return $app->redirect($app->url('migration_end'));
881
        } else {
882
            return $app['twig']->render('migration_plugin.twig', array(
883
                    'Plugins' => $Plugins,
884
                    'version' => Constant::VERSION,
885
                    'publicPath' => '..' . RELATIVE_PUBLIC_DIR_PATH . '/',
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
886
            ));
887
        }
888
    }
889
890
    /**
891
     * マイグレーションを実行し, 完了画面を表示させる
892
     *
893
     * @param InstallApplication $app
894
     * @param Request $request
0 ignored issues
show
introduced by
Expected 12 spaces after parameter type; 1 found
Loading history...
895
     *
896
     * @return \Symfony\Component\HttpFoundation\Response
897
     */
898
    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...
899
    {
900
        $this->doMigrate();
901
902
        $config_app = new \Eccube\Application(); // install用のappだとconfigが取れないので
903
        $config_app->initialize();
904
        $config_app->boot();
905
        \Eccube\Util\Cache::clear($config_app, true);
906
907
        return $app['twig']->render('migration_end.twig', array(
908
                'publicPath' => '..' . RELATIVE_PUBLIC_DIR_PATH . '/',
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
909
        ));
910
    }
911
}
912