This repository has been archived by the owner on Sep 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.php
67 lines (67 loc) · 2.59 KB
/
query.php
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
61
62
63
64
65
66
67
<?php
/* lib/query.php
For personal use; optimized for chall.stypr.com */
class Query {
private $conn;
public function check(){
return ($this->conn) ? True : False;
}
public function connect($host, $username, $password, $db=""){
$this->conn = mysqli_connect($host, $username, $password, $db);
if(!$this->conn) return mysqli_connect_errno();
mysqli_set_charset($this->conn, "utf8"); // force utf-8
}
public function query($query, $result=0){
/* $result
0 returns bool
1 returns associative array of a single row
2 returns arrays of associative arrays of rows
*/
if(!$this->conn) return false;
$_query = mysqli_query($this->conn, $query);
if(!$_query) return false;
switch($result){
case 2:
$_result = Array();
while($_result_temp = mysqli_fetch_assoc($_query)){
$_result[] = $_result_temp;
}
return $_result;
case 1:
return mysqli_fetch_assoc($_query);
default:
return true;
}
}
public function filter($str, $type='sql'){
switch($type){
case "url":
return preg_replace("/[^a-zA-Z0-9-_&\/]/", "", $str);
case "sql":
if($this->conn){
$_filter = preg_replace("/[^a-zA-Z0-9-_:+!@#$.%^+&*(){}:\/\.\ <>가-힣一-龯]/", "", $str);
return mysqli_real_escape_string($this->conn, $_filter);
}
case "memo":
if($this->conn){
$_filter = htmlspecialchars(preg_replace("/[^a-zA-Z0-9-_:+!@#$.%^&*(){}:\/.\ <>가-힣]/", "", $str));
return mysqli_real_escape_string($this->conn, $_filter);
}
case "auth":
return @preg_replace("/[^a-zA-Z0-9-_!@$\.%^&*(){}가-힣]/", "", $str);
case "text":
return mysqli_real_escape_string($this->conn, $str);
}
}
public function __construct(){
if(!function_exists("mysqli_connect")){
die('php_mysqli extension is not installed. :(');
}
}
public function __destruct(){
if($this->conn){
mysqli_close($this->conn);
}
}
}
?>