summaryrefslogtreecommitdiff
path: root/lib/sql.php
blob: 839d4694c7514809efa07ef5715b92d8be476dae (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php

require("conf/sql.php");

$sql_queries = 0;
$sql_connected = false;
$sql_conn = null;

function sql_connect() {
	global $sql_server, $sql_user, $sql_password, $sql_database, $sql_connected, $sql_conn;
	if ($sql_connected == true) return;

	try {
		$sql_conn = new PDO("mysql:host=$sql_server;dbname=$sql_database;charset=utf8",
							$sql_user, $sql_password, [
								PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
								PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
								PDO::ATTR_EMULATE_PREPARES   => false,
							]);
	} catch(PDOException $e) {
		$title = "Cannot connect to SQL server";
		$error = "An error has occurred with the SQL server ! " . $e->getMessage();
		require("tpl/general/empty.php");
	}
	$sql_connected = true;
}

function sql($r, ...$args) {
	global $sql_queries, $sql_connected, $sql_conn;
	if ($sql_connected != true) sql_connect();
	$sql_queries++;

	$stmt = $sql_conn->prepare($r);
	try {
		$stmt->execute($args);
		return $stmt;
	} catch(PDOException $e) {
		$title = "SQL error.";
		$request = $r;
		$sql_error = $e->getMessage();
		require("tpl/general/sqlerror.php");
	}
}

function esca($v) {
	if (get_magic_quotes_gpc()) {
		return stripslashes($v);
	} else {
		return $v;
	}
}
function escs($v) {
	return $v;
}
function esc($v) {
	return escs(esca($v));
}