forked from fusionpbx/fusionpbx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdo.php
350 lines (319 loc) · 9.83 KB
/
pdo.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
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
<?php
/*
FusionPBX
Version: MPL 1.1
The contents of this file are subject to the Mozilla Public License Version
1.1 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
for the specific language governing rights and limitations under the
License.
The Original Code is FusionPBX
The Initial Developer of the Original Code is
Mark J Crane <[email protected]>
Portions created by the Initial Developer are Copyright (C) 2008-2016
the Initial Developer. All Rights Reserved.
Contributor(s):
Mark J Crane <[email protected]>
Raymond Chandler <[email protected]>
*/
//includes
include "root.php";
require_once "resources/functions.php";
//set defaults
if (isset($dbtype)) {
$db_type = $dbtype;
}
if (isset($dbhost)) {
$db_host = $dbhost;
}
if (isset($dbport)) {
$db_port = $dbport;
}
if (isset($dbname)) {
$db_name = $dbname;
}
if (isset($dbusername)) {
$db_username = $dbusername;
}
if (isset($dbpassword)) {
$db_password = $dbpassword;
}
if (isset($db_file_path)) {
$db_path = $db_file_path;
}
if (isset($dbfilename)) {
$db_name = $dbfilename;
}
if (isset($dbsecure)) {
$db_secure = $dbsecure;
}
if (isset($dbcertauthority)) {
$db_cert_authority = $dbcertauthority;
}
if (!function_exists('get_db_field_names')) {
function get_db_field_names($db, $table, $db_name='fusionpbx') {
$query = sprintf('SELECT * FROM %s LIMIT 1', $table);
foreach ($db->query($query, PDO::FETCH_ASSOC) as $row) {
return array_keys($row);
}
// if we're still here, we need to try something else
$fields = array();
$driver = $db->getAttribute(PDO::ATTR_DRIVER_NAME);
if ($driver == 'sqlite') {
$query = sprintf("Pragma table_info(%s);", $table);
$stmt = $db->prepare($query);
$result = $stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_NAMED);
//printf('<pre>%s</pre>', print_r($rows, true));
$row_count = count($rows);
//printf('<pre>%s</pre>', print_r($rows, true));
for ($i = 0; $i < $row_count; $i++) {
array_push($fields, $rows[$i]['name']);
}
return $fields;
} else {
$query = sprintf("SELECT * FROM information_schema.columns
WHERE table_schema='%s' AND table_name='%s';"
, $db_name, $table
);
$stmt = $db->prepare($query);
$result = $stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_NAMED);
$row_count = count($rows);
//printf('<pre>%s</pre>', print_r($rows, true));
for ($i = 0; $i < $row_count; $i++) {
array_push($fields, $rows[$i]['COLUMN_NAME']);
}
return $fields;
}
}
}
if ($db_type == "sqlite") {
//set the document_root
if (strlen($document_root) == 0) {
$document_root = $_SERVER["DOCUMENT_ROOT"];
}
//prepare the database connection
if (strlen($db_name) == 0) {
//if (strlen($_SERVER["SERVER_NAME"]) == 0) { $_SERVER["SERVER_NAME"] = "http://localhost"; }
$server_name = $_SERVER["SERVER_NAME"];
$server_name = str_replace ("www.", "", $server_name);
//$server_name = str_replace (".", "_", $server_name);
$db_name_short = $server_name;
$db_name = $server_name.'.db';
}
else {
$db_name_short = $db_name;
}
$db_path = realpath($db_path);
if (file_exists($db_path.'/'.$db_name)) {
//echo "database file exists<br>";
}
else {
if (is_writable($db_path.'/'.$db_name)) {
//use database in current location
}
else {
//not writable
echo "The database ".$db_path."/".$db_name." does not exist or is not writable.";
exit;
}
}
if (!function_exists('php_md5')) {
function php_md5($string) {
return md5($string);
}
}
if (!function_exists('php_unix_timestamp')) {
function php_unix_timestamp($string) {
return strtotime($string);
}
}
if (!function_exists('php_now')) {
function php_now() {
return date("Y-m-d H:i:s");
}
}
if (!function_exists('php_left')) {
function php_left($string, $num) {
return substr($string, 0, $num);
}
}
if (!function_exists('php_right')) {
function php_right($string, $num) {
return substr($string, (strlen($string)-$num), strlen($string));
}
}
if (!function_exists('php_sqlite_data_type')) {
function php_sqlite_data_type($string, $field) {
//get the string between the start and end characters
$start = '(';
$end = ')';
$ini = stripos($string,$start);
if ($ini == 0) return "";
$ini += strlen($start);
$len = stripos($string,$end,$ini) - $ini;
$string = substr($string,$ini,$len);
$str_data_type = '';
$string_array = explode(',', $string);
foreach($string_array as $lnvalue) {
$fieldlistarray = explode (" ", $value);
unset($fieldarray, $string, $field);
}
return $str_data_type;
}
}
//database connection
try {
//create the database connection object
//$db = new PDO('sqlite2:example.db'); //sqlite 2
//$db = new PDO('sqlite::memory:'); //sqlite 3
$db = new PDO('sqlite:'.$db_path.'/'.$db_name); //sqlite 3
//enable foreign key constraints
$db->query('PRAGMA foreign_keys = ON;');
//add additional functions to SQLite so that they are accessible inside SQL
//bool PDO::sqliteCreateFunction ( string function_name, callback callback [, int num_args] )
$db->sqliteCreateFunction('md5', 'php_md5', 1);
$db->sqliteCreateFunction('unix_timestamp', 'php_unix_timestamp', 1);
$db->sqliteCreateFunction('now', 'php_now', 0);
$db->sqliteCreateFunction('sqlitedatatype', 'php_sqlite_data_type', 2);
$db->sqliteCreateFunction('strleft', 'php_left', 2);
$db->sqliteCreateFunction('strright', 'php_right', 2);
}
catch (PDOException $error) {
print "error: " . $error->getMessage() . "<br/>";
die();
}
} //end if db_type sqlite
if ($db_type == "mysql") {
//database connection
try {
//required for mysql_real_escape_string
if (function_exists('mysql_connect')) {
$mysql_connection = @mysql_connect($db_host, $db_username, $db_password);
//$mysql_connection = mysqli_connect($db_host, $db_username, $db_password,$db_name) or die("Error " . mysqli_error($link));
}
//mysql pdo connection
if (strlen($db_host) == 0 && strlen($db_port) == 0) {
//if both host and port are empty use the unix socket
$db = new PDO("mysql:host=$db_host;unix_socket=/var/run/mysqld/mysqld.sock;dbname=$db_name;charset=utf8;", $db_username, $db_password);
}
else {
if (strlen($db_port) == 0) {
//leave out port if it is empty
$db = new PDO("mysql:host=$db_host;dbname=$db_name;charset=utf8;", $db_username, $db_password, array(
PDO::ATTR_ERRMODE,
PDO::ERRMODE_EXCEPTION
));
}
else {
$db = new PDO("mysql:host=$db_host;port=$db_port;dbname=$db_name;charset=utf8;", $db_username, $db_password, array(
PDO::ATTR_ERRMODE,
PDO::ERRMODE_EXCEPTION
));
}
}
}
catch (PDOException $error) {
print "error: " . $error->getMessage() . "<br/>";
die();
}
} //end if db_type mysql
if ($db_type == "pgsql") {
//database connection
try {
if (!isset($db_secure)) {
$db_secure = false;
}
if (strlen($db_host) > 0) {
if (strlen($db_port) == 0) { $db_port = "5432"; }
if ($db_secure == true) {
$db = new PDO("pgsql:host=$db_host port=$db_port dbname=$db_name user=$db_username password=$db_password sslmode=verify-ca sslrootcert=$db_cert_authority");
}
else {
$db = new PDO("pgsql:host=$db_host port=$db_port dbname=$db_name user=$db_username password=$db_password");
}
}
else {
$db = new PDO("pgsql:dbname=$db_name user=$db_username password=$db_password");
}
}
catch (PDOException $error) {
print "error: " . $error->getMessage() . "<br/>";
die();
}
} //end if db_type pgsql
//get the domain list
if (!is_array($_SESSION['domains']) or !isset($_SESSION["domain_uuid"])) {
//get the domain
$domain_array = explode(":", $_SERVER["HTTP_HOST"]);
//get the domains from the database
$sql = "select * from v_domains";
$prep_statement = $db->prepare($sql);
$prep_statement->execute();
$result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
foreach($result as $row) {
$domain_names[] = $row['domain_name'];
}
unset($prep_statement);
//put the domains in natural order
if (is_array($domain_names)) {
natsort($domain_names);
}
//build the domains array in the correct order
if (is_array($domain_names)) {
foreach ($domain_names as $dn) {
foreach ($result as $row) {
if ($row['domain_name'] == $dn) {
$domains[] = $row;
}
}
}
unset($result);
}
if (is_array($domains)) {
foreach($domains as $row) {
if (!isset($_SESSION['username'])) {
if (count($domains) == 1) {
$_SESSION["domain_uuid"] = $row["domain_uuid"];
$_SESSION["domain_name"] = $row['domain_name'];
}
else {
if ($row['domain_name'] == $domain_array[0] || $row['domain_name'] == 'www.'.$domain_array[0]) {
$_SESSION["domain_uuid"] = $row["domain_uuid"];
$_SESSION["domain_name"] = $row["domain_name"];
}
}
}
$_SESSION['domains'][$row['domain_uuid']] = $row;
}
unset($domains, $prep_statement);
}
}
//get the software name
if (!isset($_SESSION["software_name"])) {
$sql = "select * from v_software ";
$prep_statement = $db->prepare(check_sql($sql));
if ($prep_statement) {
$prep_statement->execute();
$row = $prep_statement->fetch(PDO::FETCH_ASSOC);
$_SESSION["software_name"] = $row['software_name'];
}
unset($prep_statement, $result);
}
//set the setting arrays
if (!isset($_SESSION['domain']['menu'])) {
$domain = new domains();
$domain->set();
}
//set the domain_uuid variable from the session
if (strlen($_SESSION["domain_uuid"]) > 0) {
$domain_uuid = $_SESSION["domain_uuid"];
}
else {
$domain_uuid = uuid();
}
?>