Failed Conditions
Pull Request — master (#1798)
by Kentaro
31:49
created

eccube_install.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
if (php_sapi_name() !== 'cli') {
4
    exit(1);
5
}
6
7
require __DIR__.'/autoload.php';
8
set_time_limit(0);
9
ini_set('display_errors', 1);
10
11
define('COMPOSER_FILE', 'composer.phar');
12
define('COMPOSER_SETUP_FILE', 'composer-setup.php');
13
14
setUseAnsi($argv);
15
16
$argv = is_array($argv) ? $argv : array();
17
18
$argv[1] = isset($argv[1]) ? $argv[1] : null;
19
$argv[2] = isset($argv[2]) ? $argv[2] : null;
20
21
if (in_array('--help', $argv) || empty($argv[1])) {
22
    displayHelp($argv);
23
    exit(1);
24
}
25
out('EC-CUBE3 installer use database driver of ', null, false);
26
27
$database_driver = 'pdo_sqlite';
28
switch($argv[1]) {
29
    case 'mysql':
30
        $database_driver = 'pdo_mysql';
31
        break;
32
    case 'pgsql':
33
        $database_driver = 'pdo_pgsql';
34
        break;
35
    default:
36
    case 'sqlite':
37
    case 'sqlite3':
38
    case 'sqlite3-in-memory':
39
        $database_driver = 'pdo_sqlite';
40
}
41
out($database_driver);
42
43
$database = getDatabaseConfig($database_driver);
44
$connectionParams = $database['database'];
45
46
if ($argv[2] != 'none') {
47
    composerSetup();
48
    composerInstall();
49
}
50
51
createConfigFiles($database_driver);
52
createDatabase($connectionParams);
53
54
$app = createApplication();
55
initializeDatabase($app);
56
out('EC-CUBE3 install finished successfully!', 'success');
57
if (PHP_VERSION_ID >= 50400) {
58
    out('PHP built-in web server to run applications, `php -S localhost:8080 -t html`', 'info');
59
    out('Open your browser and access the http://localhost:8080/', 'info');
60
}
61
exit(0);
62
63
function displayHelp($argv)
64
{
65
    echo <<<EOF
66
EC-CUBE3 Installer
67
------------------
68
Usage:
69
${argv[0]} [mysql|pgsql|sqlite3] [none] [options]
70
71
Arguments[1]:
72
Specify database types
73
74
Arguments[2]:
75
Specifying the "none" to skip the installation of Composer
76
77
Options:
78
--help               this help
79
--ansi               force ANSI color output
80
--no-ansi            disable ANSI color output
81
82
Environment variables:
83
ADMIN_USER=admin
84
ADMIN_PASS=password
85
[email protected]
86
SHOP_NAME=EC-CUBE SHOP
87
ADMIN_ROUTE=admin
88
DBSERVER=127.0.0.1
89
DBNAME=cube3_dev
90
DBUSER=cube3_dev_user
91
DBPASS=password
92
DBPORT=<database port>
93
MAIL_BACKEND=smtp
94
MAIL_HOST=localhost
95
MAIL_PORT=25
96
MAIL_USER=<SMTP AUTH user>
97
MAIL_PASS=<SMTP AUTH password>
98
99
EOF;
100
}
101
102
function composerSetup()
103
{
104
    if (!file_exists(__DIR__.'/'.COMPOSER_FILE)) {
105
        if (!file_exists(__DIR__.'/'.COMPOSER_SETUP_FILE)) {
106
            copy('https://getcomposer.org/installer', COMPOSER_SETUP_FILE);
107
        }
108
109
        $sha = hash_file('SHA384', COMPOSER_SETUP_FILE).PHP_EOL;
110
        out(COMPOSER_SETUP_FILE.': '.$sha);
111
112
        $command = 'php '.COMPOSER_SETUP_FILE;
113
        out("execute: $command", 'info');
114
        passthru($command);
115
        unlink(COMPOSER_SETUP_FILE);
116
    } else {
117
        $command = 'php '.COMPOSER_FILE.' self-update';
118
        passthru($command);
119
    }
120
}
121
122
function composerInstall()
123
{
124
    $command = 'php '.COMPOSER_FILE.' install --dev --no-interaction';
125
    passthru($command);
126
}
127
128
function createDatabase(array $connectionParams)
129
{
130
    $dbname = $connectionParams['dbname'];
131
    switch ($connectionParams['driver']) {
132
        case 'pdo_pgsql':
133
            $connectionParams['dbname'] = 'postgres';
134
            $connectionParams['user'] = (getenv('ROOTUSER') ? getenv('ROOTUSER') : (getenv('DBUSER') ? getenv('DBUSER') : 'postgres'));
135
            $connectionParams['password'] = (getenv('ROOTPASS') ? getenv('ROOTPASS') : (getenv('DBPASS') ? getenv('DBPASS') : 'password'));
136
            break;
137
        case 'pdo_mysql':
138
            $connectionParams['dbname'] = 'mysql';
139
            $connectionParams['user'] = (getenv('ROOTUSER') ? getenv('ROOTUSER') : (getenv('DBUSER') ? getenv('DBUSER') : 'root'));
140
            if (getenv('TRAVIS')) {
141
                $connectionParams['password'] = '';
142
            } else {
143
                $connectionParams['password'] = (getenv('ROOTPASS') ? getenv('ROOTPASS') : (getenv('DBPASS') ? getenv('DBPASS') : 'password'));
144
            }
145
            break;
146
        default:
147
        case 'pdo_sqlite':
148
            $connectionParams['dbname'] = $database['database']['path'];
149
            if (file_exists($dbname)) {
150
                out('remove database to '.$dbname, 'info');
151
                unlink($dbname);
152
            }
153
            break;
154
    }
155
156
    $config = new \Doctrine\DBAL\Configuration();
157
    $conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);
158
    $sm = $conn->getSchemaManager();
159
    out('Created database connection...', 'info');
160
161
    if ($connectionParams['driver'] != 'pdo_sqlite') {
162
        $databases = $sm->listDatabases();
163
        if (in_array($dbname, $databases)) {
164
            out('database exists '.$dbname, 'info');
165
            out('drop database to '.$dbname, 'info');
166
            $sm->dropDatabase($dbname);
167
        }
168
    }
169
    out('create database to '.$dbname, 'info');
170
    $sm->createDatabase($dbname);
171
}
172
173
/**
174
 * @return \Eccube\Application
175
 */
176
function createApplication()
177
{
178
    $app = \Eccube\Application::getInstance();
179
    $app['debug'] = true;
180
    $app->initDoctrine();
181
    $app->initSecurity();
182
    $app->register(new \Silex\Provider\FormServiceProvider());
183
    $app->register(new \Eccube\ServiceProvider\EccubeServiceProvider());
184
    $app->boot();
185
    return $app;
186
}
187
188
function initializeDatabase(\Eccube\Application $app)
189
{
190
    // Get an instance of your entity manager
191
    $entityManager = $app['orm.em'];
192
193
    $pdo = $entityManager->getConnection()->getWrappedConnection();
194
195
    // Clear Doctrine to be safe
196
    $entityManager->getConnection()->getConfiguration()->setSQLLogger(null);
197
    $entityManager->clear();
198
    gc_collect_cycles();
199
200
    // Schema Tool to process our entities
201
    $tool = new \Doctrine\ORM\Tools\SchemaTool($entityManager);
202
    $classes = $entityManager->getMetaDataFactory()->getAllMetaData();
203
204
    // Drop all classes and re-build them for each test case
205
    out('Dropping database schema...', 'info');
206
    $tool->dropSchema($classes);
207
    out('Creating database schema...', 'info');
208
    $tool->createSchema($classes);
209
    out('Database schema created successfully!', 'success');
210
    $config = new \Doctrine\DBAL\Migrations\Configuration\Configuration($app['db']);
211
    $config->setMigrationsNamespace('DoctrineMigrations');
212
213
    $migrationDir = __DIR__.'/src/Eccube/Resource/doctrine/migration';
214
    $config->setMigrationsDirectory($migrationDir);
215
    $config->registerMigrationsFromDirectory($migrationDir);
216
217
    $migration = new \Doctrine\DBAL\Migrations\Migration($config);
218
    $migration->migrate();
219
    out('Database migration successfully!', 'success');
220
221
    $login_id = (getenv('ADMIN_USER') ? getenv('ADMIN_USER') : 'admin');
222
    $login_password = (getenv('ADMIN_PASS') ? getenv('ADMIN_PASS') : 'password');
223
    $passwordEncoder = new \Eccube\Security\Core\Encoder\PasswordEncoder($app['config']);
224
    $salt = \Eccube\Util\Str::random(32);
225
    $encodedPassword = $passwordEncoder->encodePassword($login_password, $salt);
226
227
    out('Creating admin accounts...', 'info');
228
    $sql = "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');";
229
    $stmt = $pdo->prepare($sql);
230
    $stmt->execute(
231
        array(
232
            ':login_id' => $login_id,
233
            ':admin_pass' => $encodedPassword,
234
            ':salt' => $salt
235
        )
236
    );
237
    $stmt->closeCursor();
238
239
    $shop_name = (getenv('SHOP_NAME') ? getenv('SHOP_NAME') : 'EC-CUBE SHOP');
0 ignored issues
show
$shop_name 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...
240
    $admin_mail = (getenv('ADMIN_MAIL') ? getenv('ADMIN_MAIL') : '[email protected]');
241
    $sql = "INSERT INTO dtb_base_info (id, shop_name, email01, email02, email03, email04, update_date, option_product_tax_rule) VALUES (1, :shop_name, :admin_mail1, :admin_mail2, :admin_mail3, :admin_mail4, current_timestamp, 0)";
242
    $stmt = $pdo->prepare($sql);
243
    $stmt->execute(
244
        array(
245
            ':shop_name' => $login_id,
246
            ':admin_mail1' => $admin_mail,
247
            ':admin_mail2' => $admin_mail,
248
            ':admin_mail3' => $admin_mail,
249
            ':admin_mail4' => $admin_mail,
250
        )
251
    );
252
    $stmt->closeCursor();
253
}
254
255
function createConfigFiles($database_driver)
256
{
257
    $config_path = __DIR__.'/app/config/eccube';
258
    createYaml(getConfig(), $config_path.'/config.yml');
259
    createYaml(getDatabaseConfig($database_driver), $config_path.'/database.yml');
260
    createYaml(getMailConfig(), $config_path.'/mail.yml');
261
    createYaml(getPathConfig(), $config_path.'/path.yml');
262
}
263
264
function createYaml($config, $path)
265
{
266
    $content = \Symfony\Component\Yaml\Yaml::dump($config);
267
    $fs = new \Symfony\Component\Filesystem\Filesystem();
268
    $fs->dumpFile($path, $content);
269
}
270
271
function getConfig()
272
{
273
    $config = array (
274
        'auth_magic' => \Eccube\Util\Str::random(32),
275
        'password_hash_algos' => 'sha256',
276
        'shop_name' => (getenv('SHOP_NAME') ? getenv('SHOP_NAME') : 'admin'),
277
        'force_ssl' => NULL,
278
        'admin_allow_host' =>
279
        array (
280
        ),
281
        'cookie_lifetime' => 0,
282
        'locale' => 'ja',
283
        'timezone' => 'Asia/Tokyo',
284
        'eccube_install' => 1,
285
    );
286
    return $config;
287
}
288
289
function getDatabaseConfig($database_driver)
290
{
291
    $database = array (
292
        'database' =>
293
        array (
294
            'driver' => $database_driver,
295
        )
296
    );
297
298
    switch ($database_driver) {
299
        case 'pdo_sqlite':
300
            $database['database']['dbname'] = $database['database']['path'] = __DIR__.'/app/config/eccube/eccube.db';
301
302
            break;
303
        case 'pdo_mysql':
304
            $database['database']['host'] = (getenv('DBSERVER') ? getenv('DBSERVER') : 'localhost');
305
306
            $database['database']['dbname'] = (getenv('DBNAME') ? getenv('DBNAME') : 'cube3_dev');
307
            $database['database']['user'] = (getenv('DBUSER') ? getenv('DBUSER') : 'cube3_dev_user');
308
            $database['database']['port'] = (getenv('DBPORT') ? getenv('DBPORT') : '3306');
309
           if (getenv('TRAVIS')) {
310
               $database['database']['password'] = '';
311
           } else {
312
               $database['database']['password'] = (getenv('DBPASS') ? getenv('DBPASS') : 'password');
313
           }
314
            break;
315
        case 'pdo_pgsql':
316
            $database['database']['host'] = (getenv('DBSERVER') ? getenv('DBSERVER') : 'localhost');
317
318
            $database['database']['dbname'] = (getenv('DBNAME') ? getenv('DBNAME') : 'cube3_dev');
319
            $database['database']['user'] = (getenv('DBUSER') ? getenv('DBUSER') : 'cube3_dev_user');
320
            $database['database']['password'] = (getenv('DBPASS') ? getenv('DBPASS') : 'password');
321
            $database['database']['port'] = (getenv('DBPORT') ? getenv('DBPORT') : '5432');
322
            break;
323
    }
324
    $database['database']['charset'] = 'utf8';
325
    $database['database']['defaultTableOptions'] = array('collate' => 'utf8_general_ci');
326
    return $database;
327
}
328
329
function getMailConfig()
330
{
331
    $mail = array (
332
        'mail' =>
333
        array (
334
            'transport' => (getenv('MAIL_BACKEND') ? getenv('MAIL_BACKEND') : 'smtp'),
335
            'host' => (getenv('MAIL_HOST') ? getenv('MAIL_HOST') : 'localhost'),
336
            'port' => (getenv('MAIL_PORT') ? getenv('MAIL_PORT') : 25),
337
            'username' => (getenv('MAIL_USER') ? getenv('MAIL_USER') : null),
338
            'password' => (getenv('MAIL_PASS') ? getenv('MAIL_PASS') : null),
339
            'encryption' => NULL,
340
            'auth_mode' => NULL,
341
            'charset_iso_2022_jp' => false,
342
        ),
343
    );
344
    return $mail;
345
}
346
347
function getPathConfig()
348
{
349
    $root_dir = realpath(__DIR__);
350
    // TODO path.yml.dist から取得したい
351
    // TODO root_urlpath を指定できるようにする
352
    $path = array (
353
        'root' => '/',
354
        'admin_dir' => (getenv('ADMIN_ROUTE') ? getenv('ADMIN_ROUTE') : 'admin'),
355
        'tpl' => '/user_data/packages/default/',
356
        'admin_tpl' => '/user_data/packages/admin/',
357
        'image_path' => '/upload/save_image/',
358
        'root_dir' => $root_dir,
359
        'root_urlpath' => NULL,
360
        'template_code' => 'default',
361
        'admin_route' => 'admin',
362
        'user_data_route' => 'user_data',
363
        'public_path' => '/html',
364
        'public_path_realdir' => $root_dir.'/html',
365
        'image_save_realdir' => $root_dir.'/html/upload/save_image',
366
        'image_temp_realdir' => $root_dir.'/html/upload/temp_image',
367
        'user_data_realdir' => $root_dir.'/html/user_data',
368
        'block_default_realdir' => $root_dir.'/src/Eccube/Resource/template/default/Block',
369
        'block_realdir' => $root_dir.'/app/template/default/Block',
370
        'template_default_realdir' => $root_dir.'/src/Eccube/Resource/template/default',
371
        'template_default_html_realdir' => $root_dir.'/html/template/default',
372
        'template_admin_realdir' => $root_dir.'/src/Eccube/Resource/template/admin',
373
        'template_admin_html_realdir' => $root_dir.'/html/template/admin',
374
        'template_realdir' => $root_dir.'/app/template/default',
375
        'template_html_realdir' => $root_dir.'/html/template/default',
376
        'template_temp_realdir' => $root_dir.'/app/cache/eccube/template',
377
        'csv_temp_realdir' => $root_dir.'/app/cache/eccube/csv',
378
        'plugin_realdir' => $root_dir.'/app/Plugin',
379
        'plugin_temp_realdir' => $root_dir.'/app/cache/plugin',
380
        'plugin_html_realdir' => $root_dir.'/html/plugin',
381
        'admin_urlpath' => '/template/admin',
382
        'front_urlpath' => '/template/default',
383
        'image_save_urlpath' => '/upload/save_image',
384
        'image_temp_urlpath' => '/upload/temp_image',
385
        'user_data_urlpath' => '/user_data',
386
        'plugin_urlpath' => '/plugin',
387
    );
388
    return $path;
389
}
390
391
/**
392
 * @link https://github.com/composer/windows-setup/blob/master/src/php/installer.php
393
 */
394
function setUseAnsi($argv)
395
{
396
    // --no-ansi wins over --ansi
397
    if (in_array('--no-ansi', $argv)) {
398
        define('USE_ANSI', false);
399
    } elseif (in_array('--ansi', $argv)) {
400
        define('USE_ANSI', true);
401
    } else {
402
        // On Windows, default to no ANSI, except in ANSICON and ConEmu.
403
        // Everywhere else, default to ANSI if stdout is a terminal.
404
        define(
405
            'USE_ANSI',
406
            (DIRECTORY_SEPARATOR == '\\')
407
                ? (false !== getenv('ANSICON') || 'ON' === getenv('ConEmuANSI'))
408
                : (function_exists('posix_isatty') && posix_isatty(1))
409
        );
410
    }
411
}
412
413
/**
414
 * @link https://github.com/composer/windows-setup/blob/master/src/php/installer.php
415
 */
416
function out($text, $color = null, $newLine = true)
417
{
418
    $styles = array(
419
        'success' => "\033[0;32m%s\033[0m",
420
        'error' => "\033[31;31m%s\033[0m",
421
        'info' => "\033[33;33m%s\033[0m"
422
    );
423
    $format = '%s';
424
    if (isset($styles[$color]) && USE_ANSI) {
425
        $format = $styles[$color];
426
    }
427
    if ($newLine) {
428
        $format .= PHP_EOL;
429
    }
430
    printf($format, $text);
431
}
432