1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace florinp\messenger\libs; |
4
|
|
|
|
5
|
|
|
use PDO; |
6
|
|
|
|
7
|
|
|
class database extends \PDO |
8
|
|
|
{ |
9
|
|
|
protected $extension_manager; |
10
|
|
|
|
11
|
|
|
public function __construct() |
12
|
|
|
{ |
13
|
|
|
global $phpbb_root_path; |
|
|
|
|
14
|
|
|
|
15
|
|
|
$database = $phpbb_root_path.'store/messenger.db'; |
16
|
|
|
parent::__construct('sqlite:'.$database); |
17
|
|
|
parent::setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
|
|
|
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param string $sql |
22
|
|
|
* @return array |
23
|
|
|
*/ |
24
|
|
|
public function select($sql, $array = array(), $fetchMode = PDO::FETCH_ASSOC) |
25
|
|
|
{ |
26
|
|
|
$sth = $this->prepare($sql); |
27
|
|
|
foreach ($array as $key => $value) { |
28
|
|
|
$sth->bindValue("$key", $value); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$sth->execute(); |
32
|
|
|
return $sth->fetchAll($fetchMode); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $table |
37
|
|
|
* @return bool |
38
|
|
|
*/ |
39
|
|
|
public function insert($table, $data) |
40
|
|
|
{ |
41
|
|
|
//ksort($data); |
42
|
|
|
|
43
|
|
|
$fieldNames = implode('`, `', array_keys($data)); |
44
|
|
|
$fieldValues = ':'.implode(', :', array_keys($data)); |
45
|
|
|
|
46
|
|
|
$sth = $this->prepare("INSERT INTO $table (`$fieldNames`) VALUES ($fieldValues)"); |
47
|
|
|
|
48
|
|
|
foreach ($data as $key => $value) { |
49
|
|
|
$sth->bindValue(":$key", $value); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $sth->execute(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $table |
57
|
|
|
* @param array $data |
58
|
|
|
* @param string $where |
59
|
|
|
* @return bool |
60
|
|
|
*/ |
61
|
|
|
public function update($table, $data, $where) |
62
|
|
|
{ |
63
|
|
|
ksort($data); |
64
|
|
|
|
65
|
|
|
$fieldDetails = NULL; |
66
|
|
|
foreach ($data as $key => $value) { |
67
|
|
|
$fieldDetails .= "`$key`=:$key,"; |
68
|
|
|
} |
69
|
|
|
$fieldDetails = rtrim($fieldDetails, ','); |
70
|
|
|
|
71
|
|
|
$sth = $this->prepare("UPDATE $table SET $fieldDetails WHERE $where"); |
72
|
|
|
|
73
|
|
|
foreach ($data as $key => $value) { |
74
|
|
|
$sth->bindValue(":$key", $value); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $sth->execute(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $table |
82
|
|
|
* @param string $where |
83
|
|
|
* @param int $limit |
84
|
|
|
* @return int |
85
|
|
|
*/ |
86
|
|
|
public function delete($table, $where, $limit = 1) |
87
|
|
|
{ |
88
|
|
|
return $this->exec("DELETE FROM $table WHERE $where LIMIT $limit"); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
protected function find($path, $prefix, $suffix) |
92
|
|
|
{ |
93
|
|
|
$finder = $this->extension_manager->get_finder(); |
94
|
|
|
|
95
|
|
|
return $finder |
96
|
|
|
->set_extensions(array('phpbb/messenger')) |
97
|
|
|
->prefix($prefix) |
98
|
|
|
->suffix($suffix) |
99
|
|
|
->core_path("$path/") |
100
|
|
|
->extension_directory("/$path") |
101
|
|
|
->find(); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
Instead of relying on
global
state, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state