/** * Static method for creating PDOWrapper object with MYSQL connection * * @param string $host Mysql host * @param string $dbName Mysql database name * @param string $username Mysql user name * @param string $password Mysql password * @param string $charset Connection charset * @return PDOWrapper */ public static function openMysql($host, $dbName, $username, $password, $charset = "") { $wrapper = new PDOWrapper("mysql:host={$host};dbname={$dbName}", $username, $password); if ($charset && !$wrapper->getLastError()) { $wrapper->query("SET NAMES ?", array($charset)); } return $wrapper; }
/** * To initialize the first connection you need at least the PDO parameters in a assoc array * It's possible to use more parameters. Here ist the complete array * * $config = [ * EQM::CONVENTION_HANDLER => <object that implements ConventionHandlerInterface> * EQM::SQL_BUILDER => <object that implements SqlBuilderInterface> * ] * * @param \PDO $pdo a valid PDO connection object * @param array $config allowed keys [result_Set_class (class name) * convention_handler (object), sql_builder (object)] * @param string $connectionName optional for multiple connections 'default' is the standard */ public static function initialize($pdo, $config = [], $connectionName = 'default') { parent::initialize($pdo, $config, $connectionName); static::$metaCache[$connectionName] = []; if (array_key_exists(static::CONVENTION_HANDLER, $config) && $config[static::CONVENTION_HANDLER] instanceof ConventionHandlerInterface) { static::$conventionHandler[$connectionName] = $config[EQM::CONVENTION_HANDLER]; } else { static::$conventionHandler[$connectionName] = new DefaultConventionHandler(); } if (array_key_exists(static::SQL_BUILDER, $config) && $config[static::SQL_BUILDER] instanceof SqlBuilderInterface) { static::$sqlBuilder[$connectionName] = $config[static::SQL_BUILDER]; } else { static::$sqlBuilder[$connectionName] = new MySqlBuilder(); } }
<?php #Specifying that the return is JSON header('Content-Type: application/json'); #Include passwords and keys require '_secret_keys.php'; #Include functions require 'libs/functions.php'; #Include database abstraction require 'vendor/mikehenrty/thin-pdo-wrapper/src/PDOWrapper.php'; #Set up database abstraction $pdo = PDOWrapper::instance(); $pdo->configMaster($db[0], $db[1], $db[2], $db[3]); #setup in _secret_keys.php #Require a key (private endpoints) function requireKey() { global $argumentsU; global $arguments; global $pdo; if (!array_key_exists("key", $arguments)) { throw new Exception('Key required to access this private endpoint (arguments: ' . $argumentsU . ')'); } if (strlen($arguments["key"]) != 20) { throw new Exception('Key provided is of an incorrect length (' . $arguments["key"] . ' (' . strlen($arguments["key"]) . '))'); } } #Require initial data function requirePost($fields) { foreach ($fields as $field => $type) {
public function testDBConnect() { new DBConfig(); $pdo = PDOWrapper::getInstance(DBConfig::$dsn, DBConfig::$username, DBConfig::$password); $this->assertEquals(true, $pdo instanceof PDO); }
/** * method instance. * - static, for singleton, for creating a global instance of this object * * @return - PDOWrapper Object */ public static function instance() { if (!isset(self::$instance)) { self::$instance = new PDOWrapper(); } return self::$instance; }
/** * Constructor. * Note: * This function is set to private in order to prevent others from calling this function. Only this class itself can call this function. * You can find an other example(in Japanese) at http://d.hatena.ne.jp/ja9/20090515/1242374821 * * @param String $dsn dsn string to connect database * @param String $username username to connect * @param String $password password to connect */ private function __construct($dsn, $username, $password) { self::$instance = new PDO($dsn, $username, $password); }
/** * Checks login/pasword against the DB records and changes to the new one * returns result wrapped within HTML * * @param string $login * @param string $old_password * @param string $new_password1 * @param string $new_password2 * @return bool true on success, false elsewhere */ private function tryChangePassword($login, $old_password, $new_password1, $new_password2, &$result) { // check if login correct if (!preg_match(self::REGEXP_USERNAME, $login)) { $result = sprintf(self::HTML_MESSAGE_FAIL, 'Неверное имя пользователя или пароль'); return false; } // check if current password ok if (!$this->checkPassword($login, $old_password)) { $result = sprintf(self::HTML_MESSAGE_FAIL, 'Неверное имя пользователя или пароль'); return false; } // check if new passwords are same if ($new_password1 != $new_password2) { $result = sprintf(self::HTML_MESSAGE_FAIL, 'Пароли не совпадают'); return false; } try { $DB = new PDOWrapper($this->CONFIG['database']['server_driver'], $this->CONFIG['database']['server_host'], $this->CONFIG['database']['server_login'], $this->CONFIG['database']['server_password'], $this->CONFIG['database']['server_db_name']); if (isset($this->CONFIG['secret_field']) && $this->CONFIG['secret_field'] > '') { $secret = $DB->querySingle("select `{$this->CONFIG['secret_field']}` from `{$this->CONFIG['table']}` where `{$this->CONFIG['login_field']}` = '{$login}'"); } else { $secret = $this->CONFIG['secret_default']; } $new_hash = $this->generateHash($new_password1, $secret); $DB->exec("update `{$this->CONFIG['table']}` set `{$this->CONFIG['md5_field']}` = '{$new_hash}' where `{$this->CONFIG['login_field']}` = '{$login}'"); } catch (Exception $e) { $result = '[JuliaCMS][AUTH] WARNING: failed changing password: '******'Пароль успешно изменен'); return true; }