/** * Get instance of class * * @param bool $check If true - checks, if instance was already created, if not - instance of cs\False_class will be returned * * @return False_class|static */ static function instance($check = false) { static $instance; if (defined('STOP')) { return False_class::instance(); } if ($check) { return isset($instance) ? $instance : False_class::instance(); } if (isset($instance)) { return $instance; } $class = ltrim(get_called_class(), '\\'); if (substr($class, 0, 2) == 'cs' && class_exists('cs\\custom' . substr($class, 2), false)) { $instance = 'cs\\custom' . substr($class, 2); $instance = $instance::instance(); } else { $instance = new static(); } $instance->construct(); return $instance; }
/** * Processing of all storage requests * * @param int $connection * * @return Storage\_Abstract|False_class */ protected function connecting($connection) { /** * If connection found in list of failed connections - return instance of False_class */ if (isset($this->failed_connections[$connection])) { return False_class::instance(); } /** * If connection already exists - return reference on the instance of Storage engine object */ if (isset($this->connections[$connection])) { return $this->connections[$connection]; } $Config = Config::instance(); /** * If connection to the local storage */ if ($connection == 0) { $Core = Core::instance(); $storage['connection'] = $Core->storage_type; $storage['url'] = $Core->storage_url; $storage['host'] = $Core->storage_host; $storage['user'] = $Core->storage_user; $storage['password'] = $Core->storage_password; } elseif (isset($Config->storage[$connection])) { $storage =& $Config->storage[$connection]; } else { return False_class::instance(); } /** * Create new Storage connection */ $engine_class = '\\cs\\Storage\\' . $storage['connection']; $this->connections[$connection] = new $engine_class($storage['url'], $storage['host'], $storage['user'], $storage['password']); /** * If successfully - add connection to the list of success connections and return instance of DB engine object */ if (is_object($this->connections[$connection]) && $this->connections[$connection]->connected()) { $this->successful_connections[] = $connection . '/' . $storage['host'] . '/' . $storage['connection']; unset($storage); $this->{$connection} = $this->connections[$connection]; return $this->connections[$connection]; /** * If failed - add connection to the list of failed connections and display connection error */ } else { unset($this->{$connection}); $this->failed_connections[$connection] = $connection . '/' . $storage['host'] . '/' . $storage['connection']; unset($storage); trigger_error(Language::instance()->error_storage . ' ' . $this->failed_connections[$connection], E_USER_WARNING); $return = False_class::instance(); $return->error = 'Connection failed'; return $return; } }
/** * Processing of all DB request * * @param int $connection Database id * @param array|bool $mirror * * @return DB\_Abstract|False_class */ protected function connecting($connection, $mirror = true) { /** * If connection found in list of failed connections - return instance of False_class */ if (isset($this->failed_connections[$connection])) { return False_class::instance(); } /** * If we want to get data and connection with DB mirror already exists - return reference on the instance of DB engine object */ if ($mirror === true && isset($this->mirrors[$connection])) { return $this->mirrors[$connection]; } /** * If connection already exists - return reference on the instance of DB engine object */ if (isset($this->connections[$connection])) { return $this->connections[$connection]; } $Config = Config::instance(); $Core = Core::instance(); $L = Language::instance(); /** * If connection to the core DB and it is not connection to the mirror */ if ($connection == 0 && !is_array($mirror)) { $db['type'] = $Core->db_type; $db['name'] = $Core->db_name; $db['user'] = $Core->db_user; $db['password'] = $Core->db_password; $db['host'] = $Core->db_host; $db['charset'] = $Core->db_charset; $db['prefix'] = $Core->db_prefix; } else { /** * If it is connection to the DB mirror */ if (is_array($mirror)) { $db =& $mirror; } else { if (!isset($Config->db[$connection]) || !is_array($Config->db[$connection])) { return False_class::instance(); } $db =& $Config->db[$connection]; } } /** * Create new DB connection */ $engine_class = '\\cs\\DB\\' . $db['type']; $this->connections[$connection] = new $engine_class($db['name'], $db['user'], $db['password'], $db['host'], $db['charset'], $db['prefix']); unset($engine_class); /** * If successfully - add connection to the list of success connections and return instance of DB engine object */ if (is_object($this->connections[$connection]) && $this->connections[$connection]->connected()) { $this->successful_connections[] = ($connection == 0 ? $L->core_db . '(' . $Core->db_type . ')' : $connection) . '/' . $db['host'] . '/' . $db['type']; unset($db); $this->{$connection} = $this->connections[$connection]; return $this->connections[$connection]; /** * If failed - add connection to the list of failed connections and try to connect to the DB mirror if it is allowed */ } else { unset($this->{$connection}); $this->failed_connections[$connection] = ($connection == 0 ? $L->core_db . '(' . $Core->db_type . ')' : $connection) . '/' . $db['host'] . '/' . $db['type']; unset($db); if ($mirror === true && ($connection == 0 && isset($Config->db[0]['mirrors']) && is_array($Config->db[0]['mirrors']) && count($Config->db[0]['mirrors']) || isset($Config->db[$connection]['mirrors']) && is_array($Config->db[$connection]['mirrors']) && count($Config->db[$connection]['mirrors']))) { $dbx = $connection == 0 ? $Config->db[0]['mirrors'] : $Config->db[$connection]['mirrors']; foreach ($dbx as $i => &$mirror_data) { $mirror_connection = $this->connecting($connection . ' (' . $mirror_data['name'] . ')', $mirror_data); if (is_object($mirror_connection) && $mirror_connection->connected()) { $this->mirrors[$connection] = $mirror_connection; $this->{$connection} = $this->connections[$connection]; return $this->mirrors[$connection]; } } unset($dbx, $i, $mirror_data, $mirror_connection); } /** * If mirror connection is not allowed - display connection error */ $return = False_class::instance(); if (!is_array($mirror)) { code_header(500); if ($connection == 0) { trigger_error($L->error_core_db, E_USER_ERROR); } else { trigger_error($L->error_db . ' ' . $this->failed_connections[$connection], E_USER_ERROR); } $return->error = 'Connection failed'; } return $return; } }