Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like InstallController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use InstallController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 41 | class InstallController |
||
|
|
|||
| 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() |
|
| 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) |
|
| 89 | |||
| 90 | // 最初からやり直す場合、SESSION情報をクリア |
||
| 91 | 1 | public function index(InstallApplication $app, Request $request) |
|
| 97 | |||
| 98 | // ようこそ |
||
| 99 | 2 | public function step1(InstallApplication $app, Request $request) |
|
| 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 . '/', |
|
| 116 | 1 | )); |
|
| 117 | 2 | } |
|
| 118 | |||
| 119 | // 権限チェック |
||
| 120 | 1 | public function step2(InstallApplication $app, Request $request) |
|
| 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 | 1 | } |
|
| 135 | |||
| 136 | 1 | return $app['twig']->render('step2.twig', array( |
|
| 137 | 1 | 'protectedDirs' => $protectedDirs, |
|
| 138 | 1 | 'publicPath' => '..' . RELATIVE_PUBLIC_DIR_PATH . '/', |
|
| 139 | 1 | )); |
|
| 140 | } |
||
| 141 | |||
| 142 | // サイトの設定 |
||
| 143 | 1 | public function step3(InstallApplication $app, Request $request) |
|
| 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'])) { |
|
| 151 | |||
| 152 | 1 | $config_file = $this->config_path . '/config.yml'; |
|
| 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 | 1 | } |
|
| 164 | |||
| 165 | // セキュリティの設定 |
||
| 166 | 1 | $config_file = $this->config_path . '/path.yml'; |
|
| 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'; |
|
| 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']; |
|
| 178 | |||
| 179 | // メール設定 |
||
| 180 | 1 | $config_file = $this->config_path . '/mail.yml'; |
|
| 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 | 1 | } else { |
|
| 189 | // 初期値にmailを設定 |
||
| 190 | $sessionData['mail_backend'] = 'mail'; |
||
| 191 | } |
||
| 192 | 1 | } |
|
| 193 | |||
| 194 | 1 | $form->setData($sessionData); |
|
| 195 | 1 | if ($this->isValid($request, $form)) { |
|
| 196 | $data = $form->getData(); |
||
| 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 . '/', |
|
| 204 | 1 | )); |
|
| 205 | } |
||
| 206 | |||
| 207 | // データベースの設定 |
||
| 208 | 1 | public function step4(InstallApplication $app, Request $request) |
|
| 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'])) { |
|
| 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 | 1 | } |
|
| 235 | 1 | } |
|
| 236 | 1 | } |
|
| 237 | |||
| 238 | 1 | $form->setData($sessionData); |
|
| 239 | |||
| 240 | 1 | if ($this->isValid($request, $form)) { |
|
| 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 . '/', |
|
| 248 | 1 | )); |
|
| 249 | } |
||
| 250 | |||
| 251 | // データベースの初期化 |
||
| 252 | 1 | public function step5(InstallApplication $app, Request $request) |
|
| 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)) { |
|
| 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 | |||
| 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, |
||
| 295 | 'shop_name' => $sessionData['shop_name'], |
||
| 296 | ); |
||
| 297 | |||
| 298 | $this->sendAppData($params); |
||
| 299 | } |
||
| 300 | 1 | $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 . '/', |
|
| 310 | 1 | )); |
|
| 311 | } |
||
| 312 | |||
| 313 | // インストール完了 |
||
| 314 | 1 | public function complete(InstallApplication $app, Request $request) |
|
| 315 | { |
||
| 316 | 1 | $config_file = $this->config_path . '/path.yml'; |
|
| 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']; |
|
| 323 | |||
| 324 | 1 | return $app['twig']->render('complete.twig', array( |
|
| 325 | 1 | 'admin_url' => $adminUrl, |
|
| 326 | 1 | 'publicPath' => '..' . RELATIVE_PUBLIC_DIR_PATH . '/', |
|
| 327 | 1 | )); |
|
| 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) |
|
| 340 | { |
||
| 341 | 1 | foreach ($this->required_modules as $module) { |
|
| 342 | 1 | if (!extension_loaded($module)) { |
|
| 343 | $app->addDanger('[必須] ' . $module . ' 拡張モジュールが有効になっていません。', 'install'); |
||
| 344 | } |
||
| 345 | 1 | } |
|
| 346 | |||
| 347 | 1 | if (!extension_loaded('pdo_mysql') && !extension_loaded('pdo_pgsql')) { |
|
| 348 | $app->addDanger('[必須] ' . 'pdo_pgsql又はpdo_mysql 拡張モジュールを有効にしてください。', 'install'); |
||
| 349 | } |
||
| 350 | |||
| 351 | 1 | foreach ($this->recommended_module as $module) { |
|
| 352 | 1 | if (!extension_loaded($module)) { |
|
| 353 | $app->addWarning('[推奨] ' . $module . ' 拡張モジュールが有効になっていません。', 'install'); |
||
| 354 | } |
||
| 355 | 1 | } |
|
| 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 | 1 | } |
|
| 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'); |
||
| 372 | } |
||
| 373 | 1 | } elseif (isset($_SERVER['SERVER_SOFTWARE']) && strpos('Microsoft-IIS', $_SERVER['SERVER_SOFTWARE']) !== false) { |
|
| 374 | // iis |
||
| 375 | 1 | } elseif (isset($_SERVER['SERVER_SOFTWARE']) && strpos('nginx', $_SERVER['SERVER_SOFTWARE']) !== false) { |
|
| 376 | // nginx |
||
| 377 | } |
||
| 378 | 1 | } |
|
| 379 | |||
| 380 | 1 | private function setPDO() |
|
| 381 | { |
||
| 382 | 1 | $config_file = $this->config_path . '/database.yml'; |
|
| 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 | |||
| 389 | 1 | } 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() |
|
| 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'; |
||
| 418 | $database = Yaml::parse(file_get_contents($config_file)); |
||
| 419 | |||
| 420 | $this->app->register(new \Silex\Provider\DoctrineServiceProvider(), array( |
||
| 421 | 'db.options' => $database['database'] |
||
| 422 | )); |
||
| 423 | |||
| 424 | $this->app->register(new \Dflydev\Silex\Provider\DoctrineOrm\DoctrineOrmServiceProvider(), array( |
||
| 425 | 'orm.proxies_dir' => __DIR__ . '/../../app/cache/doctrine', |
||
| 426 | 'orm.em.options' => array( |
||
| 427 | 'mappings' => array( |
||
| 428 | array( |
||
| 429 | 'type' => 'yml', |
||
| 430 | 'namespace' => 'Eccube\Entity', |
||
| 431 | 'path' => array( |
||
| 432 | __DIR__ . '/../../Resource/doctrine', |
||
| 433 | __DIR__ . '/../../Resource/doctrine/master', |
||
| 434 | ), |
||
| 435 | ), |
||
| 436 | |||
| 437 | ), |
||
| 438 | ) |
||
| 439 | )); |
||
| 440 | |||
| 441 | return $em = $this->app['orm.em']; |
||
| 442 | } |
||
| 443 | |||
| 444 | View Code Duplication | private function createTables() |
|
| 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'; |
||
| 462 | $database = Yaml::parse(file_get_contents($config_file)); |
||
| 463 | $config['database'] = $database['database']; |
||
| 464 | |||
| 465 | $config_file = $this->config_path . '/config.yml'; |
||
| 466 | $baseConfig = Yaml::parse(file_get_contents($config_file)); |
||
| 467 | $config['config'] = $baseConfig; |
||
| 468 | |||
| 469 | $this->PDO->beginTransaction(); |
||
| 470 | |||
| 471 | try { |
||
| 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( |
||
| 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'; |
||
| 522 | $database = Yaml::parse(file_get_contents($config_file)); |
||
| 523 | $config['database'] = $database['database']; |
||
| 524 | |||
| 525 | $config_file = $this->config_path . '/config.yml'; |
||
| 526 | $baseConfig = Yaml::parse(file_get_contents($config_file)); |
||
| 527 | $config['config'] = $baseConfig; |
||
| 528 | |||
| 529 | $this->PDO->beginTransaction(); |
||
| 530 | |||
| 531 | try { |
||
| 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 | |||
| 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( |
||
| 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'; |
||
| 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) { |
||
| 613 | } |
||
| 614 | |||
| 615 | return $this; |
||
| 616 | } |
||
| 617 | |||
| 618 | 1 | private function getProtectedDirs() |
|
| 619 | { |
||
| 620 | 1 | $protectedDirs = array(); |
|
| 621 | 1 | $base = __DIR__ . '/../../../..'; |
|
| 622 | $dirs = array( |
||
| 623 | 1 | '/html', |
|
| 624 | 1 | '/app', |
|
| 625 | 1 | '/app/template', |
|
| 626 | 1 | '/app/cache', |
|
| 627 | 1 | '/app/config', |
|
| 628 | 1 | '/app/config/eccube', |
|
| 629 | 1 | '/app/log', |
|
| 630 | 1 | '/app/Plugin', |
|
| 631 | 1 | ); |
|
| 632 | |||
| 633 | 1 | foreach ($dirs as $dir) { |
|
| 634 | 1 | if (!is_writable($base . $dir)) { |
|
| 635 | $protectedDirs[] = $dir; |
||
| 636 | } |
||
| 637 | 1 | } |
|
| 638 | |||
| 639 | 1 | return $protectedDirs; |
|
| 640 | } |
||
| 641 | |||
| 642 | private function createConfigYamlFile($data, $auth = true) |
||
| 687 | |||
| 688 | private function addInstallStatus() |
||
| 689 | { |
||
| 690 | $config_file = $this->config_path . '/config.yml'; |
||
| 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'; |
||
| 703 | if ($fs->exists($config_file)) { |
||
| 704 | $fs->remove($config_file); |
||
| 705 | } |
||
| 753 | |||
| 754 | private function createMailYamlFile($data) |
||
| 780 | |||
| 781 | private function createPathYamlFile($data, Request $request) |
||
| 809 | |||
| 810 | private function sendAppData($params) |
||
| 857 | |||
| 858 | |||
| 859 | /** |
||
| 860 | * マイグレーション画面を表示する. |
||
| 861 | * |
||
| 862 | * @param InstallApplication $app |
||
| 863 | * @param Request $request |
||
| 864 | * |
||
| 865 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 866 | */ |
||
| 867 | public function migration(InstallApplication $app, Request $request) |
||
| 873 | |||
| 874 | /** |
||
| 875 | * インストール済プラグインの一覧を表示する. |
||
| 876 | * プラグインがインストールされていない場合は, マイグレーション実行画面へリダイレクトする. |
||
| 877 | * |
||
| 878 | * @param InstallApplication $app |
||
| 879 | * @param Request $request |
||
| 880 | * |
||
| 881 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 882 | */ |
||
| 883 | public function migration_plugin(InstallApplication $app, Request $request) |
||
| 903 | |||
| 904 | /** |
||
| 905 | * マイグレーションを実行し, 完了画面を表示させる |
||
| 906 | * |
||
| 907 | * @param InstallApplication $app |
||
| 908 | * @param Request $request |
||
| 909 | * |
||
| 910 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 911 | */ |
||
| 912 | public function migration_end(InstallApplication $app, Request $request) |
||
| 925 | } |
||
| 926 |