public static function load() { if (Auth::getInstance()->isAuthorized()) { return; } if (!isset($_COOKIE['resume']) or strlen($_COOKIE['resume']) != 48) { return; } try { // find session in db $session = DB::getInstance()->fetchRow("SELECT `ip`, `user` FROM `user_session` WHERE `session`=?", [$_COOKIE['resume']]); if (empty($session)) { throw new Exception('Long session not found in database'); } // check ip if ($session['ip'] & ip2long(Users::IP_MASK) != ip2long($_SERVER['REMOTE_ADDR']) & ip2long(Users::IP_MASK)) { throw new Exception('Long session IP does not match'); } // find user $user = User::getById($session['user']); $user->login(); } catch (Exception $ex) { self::remove(); } }
/** * Init */ public static function init() { self::$provisions = []; // default database dependency try { if (DB::getInstance()->fetchOne('SELECT \'pong\'') === 'pong') { self::$provisions['database'] = ['db']; } } catch (Exception $ex) { } self::smartPluginsEnable(); }
/** * Get current tables as SHOW CREATE TABLE text * @param bool $asArray * @return string|array */ public static function getCurrentSQL($asArray = false) { $db = \Difra\DB::getInstance(); $tables = $db->fetchColumn('SHOW TABLES'); if (empty($tables)) { return false; } $tablesSQL = []; foreach ($tables as $table) { $t = $db->fetchRow("SHOW CREATE TABLE `{$table}`"); $tablesSQL[] = array_pop($t); } if ($asArray) { return $tablesSQL; } else { return implode(";\n", $tablesSQL); } }
/** * @return DB\Adapters\Common * @throws \Difra\Exception */ public static function getDB() { return DB::getInstance('vault'); }
public function setField($name, $value) { DB::getInstance(Users::getDB())->query('REPLACE INTO `user_field` SET `user`=:user,`name`=:name,`value`=:value', ['user' => $this->getId(), 'name' => $name, 'value' => $value]); }
/** * Flag password change code as used * @param string $key * @throws UsersException */ public static function setUsed($key) { if (!$key) { throw new UsersException(self::RECOVER_INVALID); } DB::getInstance(Users::getDB())->query('UPDATE `user_recover` SET `used`=1 WHERE `recover`=?', [(string) $key]); }
/** * Get database connection name */ public static function getDB() { return DB::getInstance(self::DB); }
/** * Activate user * @param $key * @return bool * @throws Exception */ public static function activate($key) { $key = trim((string) $key); if (!$key) { throw new UsersException(self::ACTIVATE_NOTFOUND); } $db = DB::getInstance(Users::getDB()); $data = $db->fetchRow('SELECT * FROM `user` WHERE `activation`=? LIMIT 1', [(string) $key]); if (empty($data)) { throw new UsersException(self::ACTIVATE_NOTFOUND); } if ($data['active']) { throw new UsersException(self::ACTIVATE_USED); } // if ($data['registered'] < date('Y-m-d H:i:s', time() - Users::ACTIVATE_TTL)) { // throw new UsersException(self::ACTIVATE_TIMEOUT); // } $db->query("UPDATE `user` SET `active`='1',`activation`=NULL WHERE `activation`=?", [$key]); if (Config::getInstance()->getValue('auth', 'login_on_activate')) { $user = User::getById($data['id']); $user->login(); } return true; }