Archived
1
0
Disbranĉigi 0
Ĉi tiu deponejo arĥiviĝis je 2024-01-28. Vi povas vidi kaj elŝuti dosierojn, sed ne povas puŝi nek raporti problemojn nek tirpeti.
blagoblag/res/lib/db.php

215 lines
6.0 KiB
PHP
Raw Normal View History

2019-02-13 20:43:32 -06:00
<?php
/* This file is free software: you can redistribute it and/or modify
2019-02-18 23:24:07 -06:00
it under the terms of version 3 of the GNU Affero General Public
License as published by the Free Software Foundation.
2019-02-13 20:43:32 -06:00
This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details. */
// -------------------------------------
// connection setup
// create the dsn from values in /config.php
$dsn = $GLOBALS['db_type'] . ":"
. $GLOBALS['db_host']
. $GLOBALS['db_port']
. $GLOBALS['db_socket']
. $GLOBALS['db_name']
. "charset=utf8mb4";
$pdo = (new PDO($dsn, $GLOBALS['db_user'], $GLOBALS['db_pass'],
$GLOBALS['db_args']));
// -------------------------------------
// table setup
// create needed tables, if nonexistant
if (!db_table_existant("lusers")) {
db_create_table("lusers",
array("id int primary key","username varchar(20)",
"biography longtext","email varchar(50)","website varchar(50)",
2019-02-15 08:32:58 -06:00
"hash char(60)","full_name varchar(50)",
2019-02-18 23:24:07 -06:00
"class varchar(20)"));
user_create("root", "password", "archmage", "Sorĉistino Root",
"jadedctrl@teknik.io",
"https://git.eunichx.us/blagoblag.git",
"Use my account (password is `password`) to create your
own archmage account-- then DELETE ME. I am a security
risk that should be destroyed ASAP.");
}
2019-02-13 20:43:32 -06:00
if (!db_table_existant("posts")) {
db_create_table("posts",
2019-02-18 23:24:07 -06:00
array("id int primary key","title varchar(50)","date datetime",
"description varchar(250)", "user int","text longtext")); }
2019-02-13 20:43:32 -06:00
if (!db_table_existant("comments")) {
db_create_table("comments",
array("id int primary key","displayname varchar(20)",
"user int","text longtext", "date datetime")); }
// -------------------------------------
// STRING --> ARRAY
// Execute a DB command
function db_cmd($query) {
$stmt = $GLOBALS['pdo']->query($query);
2019-02-15 08:32:58 -06:00
$stack = array();
2019-02-13 20:43:32 -06:00
if (is_bool($stmt)) {
return $stmt;
} else {
2019-02-15 08:32:58 -06:00
return $stmt->fetchAll();
2019-02-13 20:43:32 -06:00
}
}
// -------------------------------------
// STRING STRING --> ARRAY
// Return all values of a specific column
2019-02-18 23:24:07 -06:00
function db_get_columns($table, $column,
$order = null, $ordered = null, $max = null) {
$command = "select " . $column . " from " . $table;
if (is_string($ordered)) {
$command = $command . " order by " . $ordered . " " . $order;
} else {
$command = $command . " order by " . $column . " " . $order;
}
if (is_int($max)) {
$command = $command . " limit 0," . $max;
}
$command = $command . ";";
// -----------------
$result = db_cmd($command);
2019-02-15 08:32:58 -06:00
$result_nest = function($array) {
return $array[0];
};
if (is_array($result)) {
return array_map($result_nest, $result);
} else {
return $result;
}
2019-02-18 23:24:07 -06:00
}
2019-02-13 20:43:32 -06:00
// STRING STRING VARYING --> ARRAY
// Return all rows that have an 'identifier' column set to given value
function db_get_rows($table, $identifier, $value) {
return db_cmd("select * from " . $table . " where " . $identifier .
" = " . $value . ";");
}
// STRING STRING VARYING STRING --> ARRAY
// Return the value of a specific column in a given row, identified by an
// 'identifier' column set to the given value
function db_get_cell($table, $identifier, $value, $cell) {
2019-02-15 08:32:58 -06:00
return db_get_rows($table, $identifier, $value)[0][$cell];
2019-02-13 20:43:32 -06:00
}
2019-02-18 23:24:07 -06:00
// !!!
// !!! ['id'] is used instead of $cell !!!
// STRING STRING VARYING STRING --> ARRAY
// Return the value of a specific column in a given row, identified by an
// 'identifier' column set to the given value
function db_get_cells($table, $identifier, $value, $cell) {
$id_pop = function ($row, $cell) {
return $row['id'];
};
$rows = db_get_rows($table, $identifier, $value);
return array_map($id_pop, $rows, $cell);
}
2019-02-13 20:43:32 -06:00
// --------------------------------------
// STRING STRING VARYING STRING VARYING --> NIL
// Edit the value of a cell
function db_set_cell($table, $identifier, $value, $cell, $new_value) {
if (is_string($value)) { $value = "'" . $value . "'"; }
if (is_string($new_value)) { $new_value = "'" . $new_value . "'"; }
return db_cmd("update " . $table
. " set " . $cell . " = " . $new_value
. " where " . $identifier . " = " . $value) . ";";
}
// -------------------------------------
2019-02-18 23:24:07 -06:00
// STRING STRING --> VARYING
// Return the 'biggest' value in a column, as dictated by 'desc' ordering
function db_get_biggest($table, $column) {
return db_get_columns($table, $column, "desc")[0];
}
// STRING STRING --> VARYING
// Return the 'smallest' value in a column, as dictated by 'asc' ordering
function db_get_smallest($table, $column) {
return db_get_columns($table, $column, "asc")[0];
}
// -------------------------------------
// STRING STRING --> INTEGER
// When passed a column of numbers, it'll increment the biggest number. Good
// for creating IDs. If there aren't any numbers in the column, it'll choose 1.
function db_new_id($table, $column) {
$biggest = db_get_biggest($table, $column);
if (is_nan($biggest)) {
return 1;
} else {
return $biggest + 1;
}
}
// -------------------------------------
2019-02-13 20:43:32 -06:00
// STRING ARRAY ARRAY --> BOOLEAN
// Create a table with given values to given columns.
// First array is a list of columns (as would be provided to SQL), and the
// second is the list of values (as would follow " values " in SQL)
function db_insert_row($table, $variables, $values) {
2019-02-18 23:24:07 -06:00
$variables = comma_sep($variables, ", ");
$values = comma_sep(strings_wrap($values), ", ");
2019-02-13 20:43:32 -06:00
return db_cmd("insert into " . $table
. " (". $variables .")"
. " values ("
. $values . ")" . ";");
}
// -------------------------------------
// STRING --> BOOLEAN
// Return whether or not a table of given name exists
function db_table_existant($table) {
if (!db_cmd("select * from information_schema.tables "
. " where table_name = " . string_wrap($table) . ";")) {
return false;
} else {
return true;
}
}
// STRING ARRAY --> BOOLEAN
// Create a table of given name and columns (with array of column-strings)
function db_create_table($table, $columns) {
return db_cmd("create table " . $table
. " (" . comma_sep($columns) . ");");
}
?>