/** * Loads the configured driver and validates it. * * @param array|string custom configuration or config group name * @return void */ public function __construct($config = FALSE) { if (is_string($config)) { $name = $config; // Test the config group name if (($config = Ko::config('cache')->{$config}) === NULL) { throw new KoException('Cache: Undefined group :name.', array(':name' => $name)); } } if (!is_array($config)) { // Load the default group $config = Ko::config('cache')->default; } // Cache the config in the object $this->_config = $config; // Set driver name $driver = 'Cache_' . ucfirst($this->_config['driver']); // Load the driver if (!Ko::autoload($driver)) { throw new KoException('Class :name not found.', array(':name' => $driver)); } // Initialize the driver $this->_driver = new $driver($this->_config); // Validate the driver if (!$this->_driver instanceof Cache_Driver) { throw new KoException('Cache: Not valid driver ' . $this->_config['driver']); } }
/** * Sets up the database configuration, loads the Database_Driver. * * @throws KoException */ public function __construct($config = array()) { if (empty($config)) { $config = Ko::config('database.default'); } elseif (is_string($config)) { $name = $config; if (($config = Ko::config('database.' . $config)) === NULL) { throw new KoException('database.undefined_group :group', array(':group' => $name)); } } // Merge the default config with the passed config $this->_config = array_merge($this->_config, $config); $driver = 'Database_' . ucfirst($this->_config['type']); if (!Ko::autoload($driver)) { throw new KoException('core.driver_not_found :error', array(':error' => $this->_config['type'] . get_class($this))); } $this->_driver = new $driver($this->_config); if (!$this->_driver instanceof Database_Driver) { throw new KoException('core.driver_implements :error', array(':error' => $this->_config['type'] . get_class($this) . 'Database_Driver')); } }
/** * Constructs a new Captcha object. * * @throws KoException * @param string config group name * @return void */ public function __construct($config = NULL) { if (empty($config)) { $config = Ko::config('captcha.default'); } elseif (is_string($config)) { if (($config = Ko::config('captcha.' . $config)) === NULL) { throw new KoException('captcha.undefined_group :group', array(':group' => $config)); } } $config_default = Ko::config('captcha.default'); // Merge the default config with the passed config self::$config = array_merge($config_default, $config); // If using a background image, check if it exists if (!empty($config['background'])) { self::$config['background'] = str_replace('\\', '/', realpath($config['background'])); if (!is_file(self::$config['background'])) { throw new KoException('captcha.file_not_found :background', array(':background' => self::$config['background'])); } } // If using any fonts, check if they exist if (!empty($config['fonts'])) { self::$config['fontpath'] = str_replace('\\', '/', realpath($config['fontpath'])) . '/'; foreach ($config['fonts'] as $font) { if (!is_file(self::$config['fontpath'] . $font)) { throw new KoException('captcha.file_not_found :font', array(':font' => self::$config['fontpath'] . $font)); } } } // Set driver name $driver = 'Captcha_' . ucfirst($config['style']); // Load the driver if (!Ko::autoload($driver)) { throw new KoException('core.driver_not_found :style', array(':style' => $driver)); } $this->driver = new $driver(); // Validate the driver if (!$this->driver instanceof Captcha_Driver) { throw new KoException('core.driver_implements :driver', array(':driver' => $config['style'])); } }
/** * Create a new session. * * @return void */ public function create() { // Validate the session name if (!preg_match('~^(?=.*[a-z])[a-z0-9_]++$~iD', self::$config['name'])) { throw new KoException('The session_name, :session:, is invalid. It must contain only alphanumeric characters and underscores. Also at least one letter must be present.', array(':session:' => self::$config['name'])); } // Destroy any current sessions $this->destroy(); if (self::$config['driver'] !== 'native') { $driver = 'Session_' . ucfirst(self::$config['driver']); if (!Ko::autoload($driver)) { throw new KoException('The :driver: driver for the :library: library could not be found', array(':driver:' => self::$config['driver'], ':library:' => get_class($this))); } self::$driver = new $driver(); if (!self::$driver instanceof Session_Driver) { throw new KoException('The :driver: driver for the :library: library must implement the :interface: interface', array(':driver:' => self::$config['driver'], ':library:' => get_class($this), ':interface:' => 'Session_Driver')); } session_set_save_handler(array(self::$driver, 'open'), array(self::$driver, 'close'), array(self::$driver, 'read'), array(self::$driver, 'write'), array(self::$driver, 'destroy'), array(self::$driver, 'gc')); } // Name the session, this will also be the name of the cookie session_name(self::$config['name']); // Set the session cookie parameters // session_set_cookie_params(self::$config['lifetime']); // Start the session! session_start(); // Put session_id in the session variable $_SESSION['session_id'] = session_id(); // Update last activity $_SESSION['last_activity'] = time(); }
public function result_array($object = NULL, $type = MYSQLI_ASSOC) { $rows = array(); if (is_string($object)) { $fetch = $object; } elseif (is_bool($object)) { if ($object === TRUE) { $fetch = 'fetch_object'; // NOTE - The class set by $type must be defined before fetching the result, // autoloading is disabled to save a lot of stupid overhead. $type = (is_string($type) and Ko::autoload($type)) ? $type : 'stdClass'; } else { $fetch = 'fetch_array'; } } else { // Use the default config values $fetch = $this->fetch_type; if ($fetch == 'fetch_object') { $type = (is_string($type) and Ko::autoload($type)) ? $type : 'stdClass'; } } if ($this->result->num_rows) { // Reset the pointer location to make sure things work properly $this->result->data_seek(0); while ($row = $this->result->{$fetch}($type)) { $rows[] = $row; } } return isset($rows) ? $rows : array(); }