/** * Always call this when defining `__construct()` in sub-classes. */ public function __construct() { $this->db = ConnectionManager::getConnection(); // Modal? if (Request::$headers->has('X-Modal')) { $this->isModal = Request::$headers->get('X-Modal') == true; } // Get current project. if (Request::$properties->has('pslug')) { $this->currentProject = Project::find('slug', Request::$properties->get('pslug')) ?: null; $GLOBALS['current_project'] = $this->currentProject; $this->before('*', function () { if (!$this->hasPermission('view', $this->currentProject)) { return $this->show404(); } }); } else { $GLOBALS['current_project'] = null; } // Get current user. if ($sessionHash = Request::$cookies->get('traq')) { if ($this->currentProject) { $user = User::select('u.*')->addSelect('pur.project_role_id')->leftJoin('u', UserRole::tableName(), 'pur', 'pur.project_id = :project_id AND pur.user_id = u.id'); $user->where('u.session_hash = :session_hash'); $user->setParameter('project_id', $this->currentProject['id']); $user->setParameter('session_hash', $sessionHash); $this->currentUser = $user->fetch() ?: null; } else { $this->currentUser = User::find('session_hash', $sessionHash) ?: null; } $GLOBALS['current_user'] = $this->currentUser; } else { $GLOBALS['current_user'] = null; } $GLOBALS['permissions'] = Permission::getPermissions($this->currentUser, $this->currentProject); // Add Traq as first breadcrumb. $this->addCrumb(setting('title'), $this->generateUrl('root')); // Check if the user has permission to view the current project if (isset($this->currentProject)) { $this->before('*', function () { if (!$this->hasPermission('view')) { return $this->show403(); } }); } // If the user has a `sha1` hashed password, require them to change it because // as of Traq 4.1, only mcrypt passwords will work. if ($this->currentUser['password_ver'] == 'sha1') { $this->before('*', function () { if (Request::$properties['controller'] != 'Traq\\Controllers\\UserCP' && Request::$properties['controller'] != 'Traq\\Controllers\\Sessions') { return $this->redirectTo('usercp_password'); } }); } }
public static function create() { $conn = ConnectionManager::getConnection(); $schema = new DbSchema(); $users = $schema->createTable("users"); $users->addColumn("id", "integer", ["unsigned" => true]); $users->addColumn("username", "string", ["length" => 32]); $users->setPrimaryKey(["id"]); $users->addUniqueIndex(["username"]); foreach ($schema->toSql($conn->getDatabasePlatform()) as $query) { $conn->query($query); } }
/** * Migrate database and create admin account. */ public function installAction() { // Create database connection and load migrations $connection = ConnectionManager::create($_SESSION['db']); $this->loadMigrations(); // Migrate the database. $m = new Migrator(); $m->migrate('up'); // Create admin account $admin = new User($_SESSION['admin'] + ['name' => $_SESSION['admin']['username'], 'group_id' => 1]); $admin->save(); // Set config file contents $this->set("config", $this->makeConfig()); // Insert defaults $seeder = new Seeder(); $seeder->seed(); // Remove database and account details from the session. unset($_SESSION['db'], $_SESSION['admin']); $this->title("Complete"); return $this->render("complete.phtml"); }
public function __construct() { global $autoloader; parent::__construct(); static::$loader = $autoloader; require __DIR__ . '/version.php'; // Connect to the database $db = $this->config['db'][$this->config['environment']]; // $GLOBALS['db'] = DriverManager::getConnection([ $GLOBALS['db'] = ConnectionManager::create(['dbname' => $db['database'], 'user' => $db['username'], 'password' => $db['password'], 'host' => $db['host'], 'driver' => $db['driver'], 'prefix' => $db['prefix']]); define('PREFIX', $db['prefix']); unset($db); // Alias some commonly used classes class_alias('Avalon\\Templating\\View', 'View'); class_alias('Avalon\\Http\\Request', 'Request'); class_alias('Avalon\\Hook', 'Hook'); class_alias('Traq\\Helpers\\Errors', 'Errors'); class_alias('Traq\\Helpers\\Format', 'Format'); class_alias('Traq\\Helpers\\Ticketlist', 'Ticketlist'); class_alias('Traq\\Helpers\\TicketFilters', 'TicketFilters'); class_alias('Avalon\\Helpers\\HTML', 'HTML'); class_alias('Avalon\\Helpers\\Form', 'Form'); class_alias('Avalon\\Helpers\\TWBS', 'TWBS'); class_alias('Avalon\\Helpers\\Gravatar', 'Gravatar'); // Load commonly used functions require __DIR__ . '/common.php'; View::loadFunctions(); // If a theme is set, prepend it's views directory if (setting('theme') !== 'default') { View::addPath(__DIR__ . '/../' . setting('theme') . '/views', true); } $this->loadTranslations(); $this->loadPlugins(); // Set mailer config if (isset($this->config['email'])) { Notification::setConfig($this->config['email']); } }
/** * Check the database form fields and connection. * * @access protected */ protected function checkDatabaseInformation() { $this->title("Database Information"); $errors = []; $driver = Request::$post->get('driver'); // Check fields if ($driver == "pdo_pgsql" || $driver == "pdo_mysql") { if (!Request::$post->get('host')) { $errors[] = "Server is required"; } if (!Request::$post->get('user')) { $errors[] = "Username is required"; } if (!Request::$post->get('dbname')) { $errors[] = "Database name is required"; } } elseif ($driver == "pdo_sqlite") { if (!Request::$post->get('path')) { $errors[] = "Database path is required"; } } // Check connection if (!count($errors)) { $info = ['driver' => $driver]; switch ($driver) { case "pdo_pgsql": case "pdo_mysql": $info = $info + ['host' => Request::$post->get('host'), 'user' => Request::$post->get('user'), 'password' => Request::$post->get('password'), 'dbname' => Request::$post->get('dbname')]; break; case "pdo_sqlite": $info['path'] = Request::$post->get('path'); break; } try { // Lets try to do a few things with the database to confirm a connection. $db = ConnectionManager::create($info); $sm = $db->getSchemaManager(); $sm->listTables(); } catch (DBALException $e) { $errors[] = "Unable to connect to database: " . $e->getMessage(); } } if (count($errors)) { $this->title("Database Information"); return $this->render("steps/database_information.phtml", ['errors' => $errors]); } $_SESSION['db'] = $info; }
/** * @return \Doctrine\DBAL\Connection */ public static function connection() { return ConnectionManager::getConnection(static::$_connectionName); }
/** * Connect to the database. */ protected function configureDatabase() { $db = $this->config['db'][$this->config['environment']]; if (!isset($db['prefix'])) { $db['prefix'] = ''; } ConnectionManager::getConnection() ?: ConnectionManager::create($db); define('PREFIX', $db['prefix']); unset($db); }
<?php require dirname(__DIR__) . '/vendor/autoload.php'; use PhailSafe\TestSuite; use Avalon\Database\ConnectionManager; use Avalon\Tests\Schema; use Avalon\Tests\Models\User; ConnectionManager::create(['driver' => 'pdo_sqlite', 'memory' => true]); Schema::create(); TestSuite::tests(function ($t) { $t->group("Model Tests", function ($group) { $group->test("Create model", function ($test) { $user = User::create(['username' => "tester"]); $test->assertInstanceOf('Avalon\\Tests\\Models\\User', $user); }); $group->test("Save model", function ($test) { $user = new User(['username' => "another_tester"]); $test->assertTrue($user->save()); }); $group->test("Finder user", function ($test) { User::insert(['id' => 301, 'username' => time()]); $user = User::find(301); $test->assertEqual(301, $user->id); $test->assertFalse(User::find(404)); }); }); });
/** * Configure database connection. */ protected function configureDatabase() { if (isset($this->config['database'][$this->config['environment']])) { ConnectionManager::create($this->config['database'][$this->config['environment']]); } }
/** * Get the database connection object. * * @return PDO */ function db() { return ConnectionManager::getConnection(); }
/** * Shortcut for creating a query builder. */ function queryBuilder() { return ConnectionManager::getConnection()->createQueryBuilder(); }