forked from w3develops/w3Develops
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOssnDatabase.php
More file actions
326 lines (315 loc) · 9.07 KB
/
Copy pathOssnDatabase.php
File metadata and controls
326 lines (315 loc) · 9.07 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<?php
/**
* Open Source Social Network
*
* @package (softlab24.com).ossn
* @author OSSN Core Team <info@softlab24.com>
* @copyright (C) SOFTLAB24 LIMITED
* @license Open Source Social Network License (OSSN LICENSE) http://www.opensource-socialnetwork.org/licence
* @link https://www.opensource-socialnetwork.org/
*/
class OssnDatabase extends OssnBase {
/**
* Initialize the database
*
* return void
*/
public function __construct(){
global $Ossn;
//set the sql mode and avoid setting again and again for each request
if(!isset($Ossn->setSQLMode)){
$this->statement("SET SESSION sql_mode=(SELECT REPLACE(@@SESSION.sql_mode, 'ONLY_FULL_GROUP_BY', ''));");
$this->execute();
$Ossn->setSQLMode = true;
}
}
/**
* Connect to mysql database
*
* @return boolean
*/
public function Connect() {
$settings = ossn_database_settings();
$connect = new mysqli($settings->host, $settings->user, $settings->password, $settings->database, $settings->port);
if(!$connect->connect_errno) {
return $connect;
} else {
return false;
}
}
/**
* Prepare a query to insert data in database
*
* @param array array();
* 'names' Names of columns
* 'values' Values that need to be inserted
* 'into' Table name
*
* @return boolean
*/
public function insert($params) {
if(is_array($params)) {
if(count($params['names']) == count($params['values'])) {
$colums = "`" . implode("`, `", $params['names']) . '`';
$values = "'" . implode("', '", $params['values']) . "'";
$query = "INSERT INTO {$params['into']} ($colums) VALUES ($values);";
$this->statement($query);
if($this->execute()) {
return true;
}
}
}
return false;
}
/**
* Prepare a mysqli query
*
* @return boolean
*/
public function statement($query) {
if(!empty($query)) {
$this->query = $query;
return true;
}
return false;
}
/**
* Execute a mysqli query and store result in memory
*
* @return boolean
*/
public function execute() {
global $Ossn;
//Avoid the multiple db connections #1001
if(!isset($Ossn->dbLINK) || isset($Ossn->dbLINK) && $Ossn->dbLINK == false){
$Ossn->dbLINK = $this->Connect();
}
$this->database = $Ossn->dbLINK;
if(isset($this->query) && !empty($this->query)) {
$this->database->set_charset("utf8");
$this->exe = $this->database->query($this->query);
$exception = ossn_call_hook('database', 'execution:message', false, true);
if(!$this->exe && $exception) {
throw new OssnDatabaseException("{$this->database->error} \n {$this->query} ");
}
if(isset($this->database->insert_id)) {
$this->last_id = $this->database->insert_id;
}
unset($this->query);
//Using mysqli_close() isn't usually necessary, as non-persistent open links are automatically closed at the end of the script's execution.
//$this->database->close();
return true;
}
return false;
}
/**
* Prepare a query to update data in database
*
* @param array array();
* 'names' Names of columns
* 'values' Values that need to be updated
* 'table' Table name
* 'wheres' Specify a selection criteria to update required records
*
* @return boolean
*/
public function update($params = array()) {
if(is_array($params)) {
if(count($params['names']) == count($params['values']) && !empty($params['table'])) {
$valuec = count($params['names']);
$i = 1;
foreach($params['names'] as $key => $val) {
$data[$val] = $params['values'][$key];
}
foreach($data as $keys => $vals) {
if($i == $valuec) {
$valyes[] = "`{$keys}` = '{$vals}'";
} else {
$valyes[] = "`{$keys}` = '{$vals}',";
}
$i++;
}
$q = implode('', $valyes);
$params['wheres'] = implode(' ', $params['wheres']);
$query = "UPDATE {$params['table']} SET {$q} WHERE {$params['wheres']}";
$this->statement($query);
if($this->execute()) {
return true;
}
}
}
return false;
}
/**
* Prepare a query to select data from database
*
* @param array array();
* 'from' Names of table
* 'params' Names of columns which you want to select
* 'wheres' Specify a selection criteria to get required records
*
* @return boolean
*/
public function select($params, $multi = '') {
if(is_array($params)) {
if(!isset($params['params'])) {
$parameters = '*';
} else {
$parameters = implode(', ', $params['params']);
}
$order_by = '';
if(!empty($params['order_by'])) {
$order_by = "ORDER by {$params['order_by']}";
}
$group_by = '';
if(!empty($params['group_by'])) {
$group_by = "GROUP by {$params['group_by']}";
}
$where = '';
if(isset($params['wheres']) && is_array($params['wheres'])) {
$where = implode(' ', $params['wheres']);
}
$wheres = '';
if(!empty($params['wheres'])) {
$wheres = "WHERE({$where})";
}
$limit = '';
if(!empty($params['limit'])) {
$limit = "LIMIT {$params['limit']}";
}
$joins = '';
if(!empty($params['joins']) && !is_array($params['joins'])) {
$joins = $params['joins'];
} elseif(!empty($params['joins']) && is_array($params['joins'])) {
$joins = implode(' ', $params['joins']);
}
$query = "SELECT {$parameters} FROM {$params['from']} {$joins} {$wheres} {$group_by} {$order_by} {$limit};";
$this->statement($query);
if($this->execute()) {
return $this->fetch($multi);
}
}
return false;
}
/**
* Fetch the data from memory that is stored during execution;
*
* @param boolean $data Ture if you want to fetch all data , or false if only one row
*
* @return boolean
*/
public function fetch($data = false) {
if(isset($this->exe)) {
if($data !== true) {
if($fetch = $this->exe) {
self::destruct();
return arrayObject($fetch->fetch_assoc());
}
}
if($data === true) {
if($fetch = $this->exe) {
while($all = $fetch->fetch_assoc()) {
$alldata[] = arrayObject($all);
}
}
if(isset($alldata) && !empty($alldata)) {
self::destruct();
return arrayObject($alldata);
}
}
}
return false;
}
/**
* Prepare a query to delete data from database
*
* @param array array();
* 'from' Names of table
* 'wheres' Specify a selection criteria to get required records
*
* @return boolean
*/
public function delete($params) {
if(is_array($params)) {
$where = implode(' ', $params['wheres']);
if(!empty($params['wheres'])) {
$wheres = "WHERE({$where})";
}
//don't let any component or query to empty entire table
if(empty($params['wheres'])) {
return false;
}
$query = "DELETE FROM `{$params['from']}` {$wheres};";
$this->statement($query);
if($this->execute()) {
return true;
}
}
return false;
}
/**
* Get a guid of newly create entry
*
* @return integer
*/
public function getLastEntry() {
if(!empty($this->last_id)) {
return $this->last_id;
}
}
/**
* Create a wheres clause for database
*
* @param array $array A valid array containg wheres clauses;
* @param string $operator AND, OR, LIKE
*
* @return string
*/
public function constructWheres(array $array, $operator = "AND") {
if(!empty($array) && !empty($operator)) {
$result = implode(" {$operator} ", $array);
return $result;
}
return false;
}
/**
* Generate limit from options
*
* @param integer $data_limit How much data should be fetched?
* @param integer $page_limit Limit of data on one page
* @param integer $offset Offset value
*
* @return string|false
*/
public function generateLimit($data_limit = false, $page_limit = false, $offset = false) {
$limit = $data_limit;
//get only required result, don't bust your server memory
if(isset($offset) && $offset !== false && $page_limit !== false) {
$limitfrom = ($offset - 1) * ($page_limit);
$limitto = $page_limit;
$data_limit = "{$limitfrom}, {$limitto}";
if($offset > 1) {
if($limit > $limitfrom) {
$limitto = $limit - $limitfrom;
if($limitto <= $page_limit) {
$data_limit = "{$limitfrom}, {$limitto}";
}
}
}
if(!empty($limit) && $limit < $page_limit) {
$data_limit = $limit;
}
return $data_limit;
}
return false;
}
/**
* Manual self destruct
*
* @return void
*/
public function destruct(){
unset($this->database);
unset($this->exe);
}
} //class