Advertisement
Guest User

Database API

a guest
Nov 15th, 2016
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.24 KB | None | 0 0
  1. <?php
  2.  
  3. // Define include flag
  4. $INCLUDE_DATABASE = true;
  5.  
  6. // Include required files
  7. if(!isset($INCLUDE_CONFIG)){
  8. include "../config.php";
  9. }
  10. if(!isset($INCLUDE_UTILS)){
  11. include "utils.php";
  12. }
  13. if(!isset($INCLUDE_SQL)){
  14. include "sql.php";
  15. }
  16.  
  17. // Class definition
  18. $db = new Database();
  19.  
  20. class Database {
  21.  
  22. private $host = "";
  23. private $user = "";
  24. private $pass = "";
  25. private $name = "";
  26.  
  27. public $connection;
  28.  
  29. /*
  30. * Load configs into class
  31. *
  32. */
  33. function __construct(){
  34. global $config;
  35. $this->host = $config->database->host;
  36. $this->name = $config->database->name;
  37. $this->user = $config->database->user;
  38. $this->pass = $config->database->pass;
  39. }
  40.  
  41. /*
  42. * Executes a query and returns the result
  43. *
  44. * @param $query sql query
  45. *
  46. */
  47. public function query($query){
  48. $this->open();
  49. $this->select_db($this->name);
  50. $res = mysqli_query($this->connection, $query);
  51. $this->close();
  52.  
  53. return $res;
  54. }
  55.  
  56. /*
  57. * Establishes a connection to the database
  58. *
  59. */
  60. public function open(){
  61. $this->connection = mysqli_connect($this->host,$this->user,$this->pass) or die("Connection failed");
  62. }
  63.  
  64. /*
  65. * Slects a database
  66. *
  67. */
  68. public function select_db($dbname){
  69. mysqli_select_db($this->connection,$dbname);
  70. }
  71.  
  72. /*
  73. * Closes the database connection
  74. *
  75. */
  76. public function close(){
  77. mysqli_close($this->connection);
  78. }
  79.  
  80. /*
  81. * Inserts a new row into a table if the user have permission to do so
  82. *
  83. * @param $keys keys to be inserted
  84. * @param $values the corresponding values
  85. * @param $table the table to be inserted
  86. * @param $permissionLevel min required permission level
  87. * @return success
  88. *
  89. */
  90. public function insert($keys, $values, $table, $permissionLevel){
  91. global $config, $utils;
  92. $pLevel = $utils->getSessionVariable("permissionLevel");
  93.  
  94. if($utils->checkPermissions($pLevel, $permissionLevel)){
  95. $query = "INSERT INTO ${table} ${keys} VALUES ${values}";
  96.  
  97. $res = $this->query($query);
  98.  
  99. return $res;
  100. }
  101.  
  102. return $config->permission->denied;
  103. }
  104.  
  105. /*
  106. * Fetches data from a table
  107. *
  108. * @param $keys keys to be fetched
  109. * @param $table the table to be fetched from
  110. * @param $join tables to join
  111. * @param $where fetch condition
  112. * @param $limit limit the amount of results
  113. * @param $permissionLevel min required permission level
  114. * @return fetched data
  115. *
  116. */
  117. public function fetch($keys, $table, $join, $where, $limit, $permissionLevel){
  118. global $config, $utils;
  119. $pLevel = $utils->getSessionVariable("permissionLevel");
  120.  
  121. if($utils->checkPermissions($pLevel, $permissionLevel)){
  122. $query = "SELECT ${keys} FROM ${table}";
  123.  
  124. if($join){
  125. $query .= $join;
  126. }
  127. if($where){
  128. $query .= " WHERE " . $where;
  129. }
  130. if($limit){
  131. $query .= $limit;
  132. }
  133.  
  134. $res = $this->query($query);
  135.  
  136. return $res;
  137. }
  138.  
  139. return $config->permission->denied;
  140. }
  141.  
  142. /*
  143. * Updates a row in table
  144. *
  145. * @param $keyval key-value-pairs to update
  146. * @param $table the table to update
  147. * @param $where update condition
  148. * @param $permissionLevel min required permission level
  149. * @return success
  150. *
  151. */
  152. public function update($keyval, $table, $where, $permissionLevel){
  153. global $config, $utils;
  154. $pLevel = $utils->getSessionVariable("permissionLevel");
  155.  
  156. if($utils->checkPermissions($pLevel, $permissionLevel) && $where){
  157. $query = "UPDATE ${table} SET ${keyval} WHERE ${where}";
  158.  
  159. $res = $this->query($query);
  160.  
  161. return $res;
  162. }
  163.  
  164. return $config->permission->denied;
  165. }
  166.  
  167. /*
  168. * Returns a value in a mysqli result
  169. *
  170. * @param $res mysql result
  171. * @return data of the first column
  172. *
  173. */
  174. public function getValue($result, $key){
  175. if($result && mysqli_num_rows($result) !== 0){
  176. return mysqli_fetch_assoc($result)[$key];
  177. }
  178.  
  179. return FALSE;
  180. }
  181.  
  182. /*
  183. * Returns an array of assoc arrays
  184. *
  185. * @param $res mysql result
  186. * @return array of assoc arrays
  187. *
  188. */
  189. public function getAssoc($result){
  190. if($result && mysqli_num_rows($result) !== 0){
  191. $entrys = array();
  192. $entry = NULL;
  193.  
  194. while($entry = mysqli_fetch_assoc($result)){
  195. array_push($entrys, $entry);
  196. }
  197.  
  198. return $entrys;
  199. }
  200.  
  201. return array();
  202. }
  203.  
  204. /*
  205. * Creates a unique id in the table
  206. *
  207. * @param $table unique id in table
  208. * @return uid
  209. *
  210. */
  211. public function generateUID($table){
  212. global $config, $utils, $db;
  213.  
  214. $id = $utils->generateID($config->id->length);
  215.  
  216. $res = $db->fetch("uid", $table, "", "uid=${id}", 1, $config->permission->UNCONFIRMED);
  217.  
  218. if($this->getValue($res, "uid")){
  219. return $this->generateUID($table);
  220. }
  221.  
  222. return $id;
  223. }
  224.  
  225. }
  226. ?>
  227. <?php
  228.  
  229. // Define include flag
  230. $INCLUDE_SQL = true;
  231.  
  232. // Class definition
  233. $sql = new SQL();
  234.  
  235. class SQL {
  236.  
  237. /*
  238. * Variables into sql values string
  239. *
  240. * @param variable parameter list (values)
  241. * @return sql values string
  242. *
  243. */
  244. public function values(){
  245. $value = "";
  246. $parameterList = func_get_args();
  247.  
  248. for($i = 0; $i < count($parameterList); $i++){
  249. $value .= "'{$parameterList[$i]}'";
  250.  
  251. if($i < count($parameterList) - 1){
  252. $value .= ", ";
  253. }
  254. }
  255. return $this->clamp($value);
  256. }
  257.  
  258. /*
  259. * Creates a key-value-pair
  260. *
  261. * @param $key
  262. * @param $value
  263. * @param $quotes bool value in quotes
  264. *
  265. */
  266. public function keyValue($key, $value, $quotes=FALSE){
  267. return $quotes ? " ${key}='${value}'" : " ${key}=${value}";
  268. }
  269.  
  270. /*
  271. * Clamps string into brackets
  272. *
  273. * @param $sql sql string
  274. * @return clamped string
  275. *
  276. */
  277. public function clamp($sql){
  278. return " (${sql})";
  279. }
  280.  
  281. /*
  282. * Creates sql like statement
  283. *
  284. * @param $column
  285. * @param $query string to compare
  286. * @return sql like statement
  287. *
  288. */
  289. public function like($column, $query){
  290. return " ${column} LIKE '%${query}%'";
  291. }
  292.  
  293. /*
  294. * Concatenates two conditions with an 'and'
  295. *
  296. * @param $condition1
  297. * @param $condition2
  298. * @return concat 'and' sql string
  299. *
  300. */
  301. public function logicalAnd($condition1, $condition2){
  302. return " ${condition1} AND ${condition2}";
  303. }
  304.  
  305. /*
  306. * Concatenates two conditions with an 'or'
  307. *
  308. * @param $condition1
  309. * @param $condition2
  310. * @return concat 'or' sql string
  311. *
  312. */
  313. public function logicalOr($condition1, $condition2){
  314. return " ${condition1} OR ${condition2}";
  315. }
  316.  
  317. /*
  318. * Creates sql inner join statement
  319. *
  320. * @param $table to join
  321. * @param $condition
  322. * @return sql inner join statement
  323. *
  324. */
  325. public function innerJoin($table, $condition){
  326. return " INNER JOIN ${table} ON ${condition}";
  327. }
  328.  
  329. /*
  330. * Creates sql limit statement with an max value cap
  331. *
  332. * @param $limit
  333. * @return sql limit statement
  334. *
  335. */
  336. public function limit($limit){
  337. global $config;
  338. $limit = $limit <= $config->baseLimit ? $limit : $config->baseLimit;
  339. return " LIMIT ${limit}";
  340. }
  341.  
  342. }
  343.  
  344. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement