function setup() { global $settings; $exec_start = microtime(true); mkdir($settings->data_dir, 0660); touch("{$settings->data_dir}/sqlite.log"); $db = new PDO("sqlite:{$settings->data_dir}/{$settings->database_filename}"); sqlite_gateway::exec("create table users (\r\n\tid integer primary key,\r\n\tusername text,\r\n\tpassword_hash text,\r\n\troles int\r\n);", $db); sqlite_gateway::exec("insert into users (id, username, password_hash, roles) values (\r\n\t1,\r\n\t'admin',\r\n\t'" . SQLite3::escapeString(password_hash("password", PASSWORD_DEFAULT, ["cost" => $settings->password_hash_cost])) . "',\r\n\t" . (ROLE_USER | ROLE_MODERATOR | ROLE_ADMIN) . "\r\n);", $db); sqlite_gateway::exec("create table sessions (\r\n\tid integer primary key,\r\n\tkey text,\r\n\tusername text,\r\n\texpiry_timestamp int\r\n);", $db); sqlite_gateway::exec("create table code_snippets (\r\n\tid integer primary key,\r\n\ttimestamp int,\r\n\tauthor text,\r\n\ttitle text,\r\n\tdescription text,\r\n\tcode text,\r\n\tlanguage text,\r\n\ttags text\r\n);", $db); // insert test data here sqlite_gateway::exec("insert into code_snippets values (?, 1440668479, 'admin', 'test snippet', 'this is a test snippet.', 'function test(x) { console.log(x); }', 'javascript', 'test, function');", $db); // todo display a nice welcome page return microtime(true) - $exec_start; }
<?php $exec_start = microtime(true); if (!defined("WALAFUNT_ENTRY_POINT")) { exit("This script isn't meant to be run directly."); } $params = utils::apply_default_params($_GET, ["language" => "all", "tags" => "all", "sort" => "timestamp", "sort_dir" => "asc", "page" => "1"]); $tags = array_map("trim", explode(",", $params["tags"])); if ($params["language"] == "all") { $params["language"] = "*"; } if ($params["tags"] == "all") { $params["tags"] = "*"; } $offset = ($params["page"] - 1) * $settings->snippets_per_page; $snippets = sqlite_gateway::query("select * from code_snippets; limit {$settings->snippets_per_page} offset {$offset};"); if ($snippets === false) { // Make sure that the return of the query is always iterable $snippets = []; } $tag_str = templates::format_tags($tags); $components = ["{prefix}" => ucwords(($params["tags"] == "*" ? "" : $tags_str . " - ") . ($params["language"] == "*" ? "all snippets" : $params["language"] . " - ")), "{code-snippets-list}" => templates::render_snippets($snippets), "{user-panel}" => templates::render_user_panel(env::$username, env::$role)]; header("x-time-taken: " . (microtime(true) - $exec_start)); echo templates::render_html("main.html", $components);
public static function clean() { return sqlite_gateway::exec("delete from sessions where expiry_timestamp < " . time() . ";"); }
public static function get_user($user) { $safe_user = sqlite_gateway::makesafe($user); $result = sqlite_gateway::query("select * from users where username='******';"); if ($result === false) { return false; } return $result->fetch(PDO::FETCH_ASSOC); }