Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Define include flag
- $INCLUDE_DATABASE = true;
- // Include required files
- if(!isset($INCLUDE_CONFIG)){
- include "../config.php";
- }
- if(!isset($INCLUDE_UTILS)){
- include "utils.php";
- }
- if(!isset($INCLUDE_SQL)){
- include "sql.php";
- }
- // Class definition
- $db = new Database();
- class Database {
- private $host = "";
- private $user = "";
- private $pass = "";
- private $name = "";
- public $connection;
- /*
- * Load configs into class
- *
- */
- function __construct(){
- global $config;
- $this->host = $config->database->host;
- $this->name = $config->database->name;
- $this->user = $config->database->user;
- $this->pass = $config->database->pass;
- }
- /*
- * Executes a query and returns the result
- *
- * @param $query sql query
- *
- */
- public function query($query){
- $this->open();
- $this->select_db($this->name);
- $res = mysqli_query($this->connection, $query);
- $this->close();
- return $res;
- }
- /*
- * Establishes a connection to the database
- *
- */
- public function open(){
- $this->connection = mysqli_connect($this->host,$this->user,$this->pass) or die("Connection failed");
- }
- /*
- * Slects a database
- *
- */
- public function select_db($dbname){
- mysqli_select_db($this->connection,$dbname);
- }
- /*
- * Closes the database connection
- *
- */
- public function close(){
- mysqli_close($this->connection);
- }
- /*
- * Inserts a new row into a table if the user have permission to do so
- *
- * @param $keys keys to be inserted
- * @param $values the corresponding values
- * @param $table the table to be inserted
- * @param $permissionLevel min required permission level
- * @return success
- *
- */
- public function insert($keys, $values, $table, $permissionLevel){
- global $config, $utils;
- $pLevel = $utils->getSessionVariable("permissionLevel");
- if($utils->checkPermissions($pLevel, $permissionLevel)){
- $query = "INSERT INTO ${table} ${keys} VALUES ${values}";
- $res = $this->query($query);
- return $res;
- }
- return $config->permission->denied;
- }
- /*
- * Fetches data from a table
- *
- * @param $keys keys to be fetched
- * @param $table the table to be fetched from
- * @param $join tables to join
- * @param $where fetch condition
- * @param $limit limit the amount of results
- * @param $permissionLevel min required permission level
- * @return fetched data
- *
- */
- public function fetch($keys, $table, $join, $where, $limit, $permissionLevel){
- global $config, $utils;
- $pLevel = $utils->getSessionVariable("permissionLevel");
- if($utils->checkPermissions($pLevel, $permissionLevel)){
- $query = "SELECT ${keys} FROM ${table}";
- if($join){
- $query .= $join;
- }
- if($where){
- $query .= " WHERE " . $where;
- }
- if($limit){
- $query .= $limit;
- }
- $res = $this->query($query);
- return $res;
- }
- return $config->permission->denied;
- }
- /*
- * Updates a row in table
- *
- * @param $keyval key-value-pairs to update
- * @param $table the table to update
- * @param $where update condition
- * @param $permissionLevel min required permission level
- * @return success
- *
- */
- public function update($keyval, $table, $where, $permissionLevel){
- global $config, $utils;
- $pLevel = $utils->getSessionVariable("permissionLevel");
- if($utils->checkPermissions($pLevel, $permissionLevel) && $where){
- $query = "UPDATE ${table} SET ${keyval} WHERE ${where}";
- $res = $this->query($query);
- return $res;
- }
- return $config->permission->denied;
- }
- /*
- * Returns a value in a mysqli result
- *
- * @param $res mysql result
- * @return data of the first column
- *
- */
- public function getValue($result, $key){
- if($result && mysqli_num_rows($result) !== 0){
- return mysqli_fetch_assoc($result)[$key];
- }
- return FALSE;
- }
- /*
- * Returns an array of assoc arrays
- *
- * @param $res mysql result
- * @return array of assoc arrays
- *
- */
- public function getAssoc($result){
- if($result && mysqli_num_rows($result) !== 0){
- $entrys = array();
- $entry = NULL;
- while($entry = mysqli_fetch_assoc($result)){
- array_push($entrys, $entry);
- }
- return $entrys;
- }
- return array();
- }
- /*
- * Creates a unique id in the table
- *
- * @param $table unique id in table
- * @return uid
- *
- */
- public function generateUID($table){
- global $config, $utils, $db;
- $id = $utils->generateID($config->id->length);
- $res = $db->fetch("uid", $table, "", "uid=${id}", 1, $config->permission->UNCONFIRMED);
- if($this->getValue($res, "uid")){
- return $this->generateUID($table);
- }
- return $id;
- }
- }
- ?>
- <?php
- // Define include flag
- $INCLUDE_SQL = true;
- // Class definition
- $sql = new SQL();
- class SQL {
- /*
- * Variables into sql values string
- *
- * @param variable parameter list (values)
- * @return sql values string
- *
- */
- public function values(){
- $value = "";
- $parameterList = func_get_args();
- for($i = 0; $i < count($parameterList); $i++){
- $value .= "'{$parameterList[$i]}'";
- if($i < count($parameterList) - 1){
- $value .= ", ";
- }
- }
- return $this->clamp($value);
- }
- /*
- * Creates a key-value-pair
- *
- * @param $key
- * @param $value
- * @param $quotes bool value in quotes
- *
- */
- public function keyValue($key, $value, $quotes=FALSE){
- return $quotes ? " ${key}='${value}'" : " ${key}=${value}";
- }
- /*
- * Clamps string into brackets
- *
- * @param $sql sql string
- * @return clamped string
- *
- */
- public function clamp($sql){
- return " (${sql})";
- }
- /*
- * Creates sql like statement
- *
- * @param $column
- * @param $query string to compare
- * @return sql like statement
- *
- */
- public function like($column, $query){
- return " ${column} LIKE '%${query}%'";
- }
- /*
- * Concatenates two conditions with an 'and'
- *
- * @param $condition1
- * @param $condition2
- * @return concat 'and' sql string
- *
- */
- public function logicalAnd($condition1, $condition2){
- return " ${condition1} AND ${condition2}";
- }
- /*
- * Concatenates two conditions with an 'or'
- *
- * @param $condition1
- * @param $condition2
- * @return concat 'or' sql string
- *
- */
- public function logicalOr($condition1, $condition2){
- return " ${condition1} OR ${condition2}";
- }
- /*
- * Creates sql inner join statement
- *
- * @param $table to join
- * @param $condition
- * @return sql inner join statement
- *
- */
- public function innerJoin($table, $condition){
- return " INNER JOIN ${table} ON ${condition}";
- }
- /*
- * Creates sql limit statement with an max value cap
- *
- * @param $limit
- * @return sql limit statement
- *
- */
- public function limit($limit){
- global $config;
- $limit = $limit <= $config->baseLimit ? $limit : $config->baseLimit;
- return " LIMIT ${limit}";
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement