コード例 #1
0
 public function check_db_common($type, $ro = true)
 {
     $res = false;
     $cfg =& $this->p['config'];
     if (!$ro && @$_POST['submit_' . $type . '_db'] === 'true') {
         $this->p['actions'][] = 'Setting new DB information';
         $driver = $_POST['driver'];
         $str = '';
         switch ($driver) {
             case 'sqlite':
                 $str = 'sqlite:' . $_POST['dbPath'];
                 break;
             case 'mysql':
                 $str = 'mysql:host=' . $_POST['dbHost'] . ';dbname=' . $_POST['dbName'];
                 break;
             default:
                 $str = $_POST['dbString'];
         }
         $cfg[$type . '_db'] = $str;
         $cfg[$type . '_db_user'] = $_POST['dbUser'];
         $cfg[$type . '_db_pass'] = $_POST['dbPass'];
         $this->saveCfg();
         $this->redirect(array('index', 'step' => $type));
     }
     $str = @$cfg[$type . '_db'];
     if (preg_match('/sqlite:/', $str)) {
         $driver = 'sqlite';
         $path = substr($str, strlen('sqlite:'));
     } else {
         if (preg_match('/mysql/', $str)) {
             $driver = 'mysql';
             $m = array();
             preg_match('/mysql:host=([^;]*);dbname=(.+)/', $str, $m);
             if (count($m) > 1) {
                 $host = $m[1];
             }
             if (count($m) > 2) {
                 $name = $m[2];
             }
         } else {
             if (strlen($str)) {
                 $driver = 'manual';
             } else {
                 $driver = 'sqlite';
             }
         }
     }
     $this->p['actions'][] = 'Trying to connect to the ' . $type . ' database';
     $db = new DbConnection(@$cfg[$type . '_db'], @$cfg[$type . '_db_user'], @$cfg[$type . '_db_pass']);
     $this->p[$type . '_db'] = $db;
     try {
         @($db->active = true);
     } catch (Exception $e) {
         $error = $e->getMessage();
     }
     if ($db->active) {
         $this->p[$type . '_db_connected'] = true;
         $this->p['actions'][] = 'Connection successful';
         $check_func = 'check_db_' . $type;
         if (!$this->{$check_func}()) {
             $this->p[$type . '_db_initialized'] = false;
             if (!$ro && @$_POST['submit_init_' . $type . '_db'] === 'true') {
                 array_pop($this->p['actions']);
                 //remove error message
                 $schema = dirname(__FILE__) . '/../data/' . $type . '/schema.' . $driver . '.sql';
                 $this->p['actions'][] = 'Initializing database from: ' . $schema;
                 try {
                     $db->executeFile($schema);
                     $this->p[$type . '_db_initialized'] = true;
                 } catch (Exception $e) {
                     $this->p['actions'][] = 'Error Initializing ' . $type . ' database: ' . $e->getMessage();
                 }
             } else {
                 $this->p['actions'][] = 'Database seems to be uninitialized';
             }
         } else {
             $this->p[$type . '_db_initialized'] = true;
         }
         if ($this->p[$type . '_db_initialized']) {
             $res = true;
             if (!$ro) {
                 $db->active = false;
                 $this->p['actions'][] = 'Opening database in production mode';
                 try {
                     if ($type == 'panel') {
                         $db =& Yii::app()->db;
                     } else {
                         $db =& Yii::app()->bridgeDb;
                     }
                     @($db->active = true);
                     if ($db->getV() != $db->version) {
                         $this->p['actions'][] = 'Applying updates';
                     }
                     @$db->dbCheck();
                 } catch (Exception $e) {
                     $this->p['actions'][] = 'Error re-opening database: ' . $e->getMessage();
                     if (!$ro && @$_POST['submit_ignore_error_' . $type . '_db'] === 'true') {
                         $this->p['actions'][] = 'Trying to force-ignore the errors.';
                         try {
                             @$db->dbCheck(true);
                         } catch (Exception $e) {
                             $this->p['actions'][] = 'Failed to ignore update errors: ' . $e->getMessage();
                             $res = false;
                         }
                     } else {
                         $res = false;
                     }
                 }
                 if ($res) {
                     $this->p['actions'][] = 'All done.';
                 }
             }
         }
     } else {
         $this->p[$type . '_db_connected'] = false;
         $this->p['actions'][] = 'Failed to connect: ' . $error;
         if (preg_match('/sqlite:/', @$cfg[$type . '_db'])) {
             $pdo = extension_loaded('pdo_sqlite');
         } else {
             if (preg_match('/mysql:/', @$cfg[$type . '_db'])) {
                 $pdo = extension_loaded('pdo_mysql');
             } else {
                 $pdo = false;
             }
         }
         $this->p[$type . '_db_pdo'] = $pdo;
     }
     $this->p[$type . '_db_driver'] = $driver;
     $this->p[$type . '_db_path'] = @$path ? $path : '';
     $this->p[$type . '_db_host'] = @$host ? $host : '127.0.0.1';
     $this->p[$type . '_db_name'] = @$name ? $name : 'multicraft_' . $type;
     return $res;
 }