forked from BitweaverCMS/adodb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelperFunctions.php
436 lines (396 loc) · 12.9 KB
/
helperFunctions.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
<?php
/**
* Synonym for ADOLoadCode. Private function. Do not use.
*
* @deprecated
*/
function ADOLoadDB($dbType)
{
return ADOLoadCode($dbType);
}
/**
* Load the code for a specific database driver. Private function. Do not use.
*/
function ADOLoadCode($dbType)
{
global $ADODB_LASTDB;
if (!$dbType) {
return false;
}
$db = strtolower($dbType);
switch ($db) {
case 'ado':
if (PHP_VERSION >= 5) {
$db = 'ado5';
}
$class = 'ado';
break;
case 'ifx':
case 'maxsql':
$class = $db = 'mysqlt';
break;
case 'postgres':
case 'postgres8':
case 'pgsql':
$class = $db = 'postgres7';
break;
default:
$class = $db;
break;
}
$file = ADODB_DIR.'/drivers/adodb-'.$db.'.inc.php';
@include_once $file;
$ADODB_LASTDB = $class;
if (class_exists('ADODB_'.$class)) {
return $class;
}
//ADOConnection::outp(adodb_pr(get_declared_classes(),true));
if (!file_exists($file)) {
ADOConnection::outp("Missing file: $file");
} else {
ADOConnection::outp("Syntax error in file: $file");
}
return false;
}
/**
* Instantiate a new Connection class for a specific database driver.
*
* @param [db] is the database Connection object to create. If undefined,
* use the last database driver that was loaded by ADOLoadCode().
*
* @return ADOConnection the freshly created instance of the Connection class.
*/
function ADONewConnection($db = '')
{
global $ADODB_NEWCONNECTION, $ADODB_LASTDB;
if (!defined('ADODB_ASSOC_CASE')) {
define('ADODB_ASSOC_CASE', 2);
}
$errorfn = (defined('ADODB_ERROR_HANDLER')) ? ADODB_ERROR_HANDLER : false;
$false = false;
if (false !== ($at = strpos($db, '://'))) {
$origdsn = $db;
$fakedsn = 'fake'.substr($origdsn, $at);
if (false !== ($at2 = strpos($origdsn, '@/'))) {
// special handling of oracle, which might not have host
$fakedsn = str_replace('@/', '@adodb-fakehost/', $fakedsn);
}
if (false !== (strpos($origdsn, 'sqlite')) && false === stripos($origdsn, '%2F')) {
// special handling for SQLite, it only might have the path to the database file.
// If you try to connect to a SQLite database using a dsn like 'sqlite:///path/to/database', the 'parse_url' php function
// will throw you an exception with a message such as "unable to parse url"
list($scheme, $path) = explode('://', $origdsn);
$dsna['scheme'] = $scheme;
if ($qmark = strpos($path, '?')) {
$dsn['query'] = substr($path, $qmark + 1);
$path = substr($path, 0, $qmark);
}
$dsna['path'] = '/'.urlencode($path);
} else {
$dsna = @parse_url($fakedsn);
}
if (!$dsna) {
return $false;
}
$dsna['scheme'] = substr($origdsn, 0, $at);
if (false !== $at2) {
$dsna['host'] = '';
}
if (0 == strncmp($origdsn, 'pdo', 3)) {
$sch = explode('_', $dsna['scheme']);
if (sizeof($sch) > 1) {
$dsna['host'] = isset($dsna['host']) ? rawurldecode($dsna['host']) : '';
if ('sqlite' == $sch[1]) {
$dsna['host'] = rawurlencode($sch[1].':'.rawurldecode($dsna['host']));
} else {
$dsna['host'] = rawurlencode($sch[1].':host='.rawurldecode($dsna['host']));
}
$dsna['scheme'] = 'pdo';
}
}
$db = @$dsna['scheme'];
if (!$db) {
return $false;
}
$dsna['host'] = isset($dsna['host']) ? rawurldecode($dsna['host']) : '';
$dsna['user'] = isset($dsna['user']) ? rawurldecode($dsna['user']) : '';
$dsna['pass'] = isset($dsna['pass']) ? rawurldecode($dsna['pass']) : '';
$dsna['path'] = isset($dsna['path']) ? rawurldecode(substr($dsna['path'], 1)) : ''; // strip off initial /
if (isset($dsna['query'])) {
$opt1 = explode('&', $dsna['query']);
foreach ($opt1 as $k => $v) {
$arr = explode('=', $v);
$opt[$arr[0]] = isset($arr[1]) ? rawurldecode($arr[1]) : 1;
}
} else {
$opt = array();
}
}
/*
* phptype: Database backend used in PHP (mysql, odbc etc.)
* dbsyntax: Database used with regards to SQL syntax etc.
* protocol: Communication protocol to use (tcp, unix etc.)
* hostspec: Host specification (hostname[:port])
* database: Database to use on the DBMS server
* username: User name for login
* password: Password for login
*/
if (!empty($ADODB_NEWCONNECTION)) {
$obj = $ADODB_NEWCONNECTION($db);
}
if (empty($obj)) {
if (!isset($ADODB_LASTDB)) {
$ADODB_LASTDB = '';
}
if (empty($db)) {
$db = $ADODB_LASTDB;
}
if ($db != $ADODB_LASTDB) {
$db = ADOLoadCode($db);
}
if (!$db) {
if (isset($origdsn)) {
$db = $origdsn;
}
if ($errorfn) {
// raise an error
$ignore = false;
$errorfn('ADONewConnection', 'ADONewConnection', -998,
"could not load the database driver for '$db'",
$db, false, $ignore);
} else {
ADOConnection::outp("<p>ADONewConnection: Unable to load database driver '$db'</p>", false);
}
return $false;
}
$cls = 'ADODB_'.$db;
if (!class_exists($cls)) {
adodb_backtrace();
return $false;
}
$obj = new $cls();
}
// constructor should not fail
if ($obj) {
if ($errorfn) {
$obj->raiseErrorFn = $errorfn;
}
if (isset($dsna)) {
if (isset($dsna['port'])) {
$obj->port = $dsna['port'];
}
foreach ($opt as $k => $v) {
switch (strtolower($k)) {
case 'new':
$nconnect = true;
$persist = true;
break;
case 'persist':
case 'persistent':
$persist = $v;
break;
case 'debug':
$obj->debug = (int) $v;
break;
//ibase
case 'role':
$obj->role = $v;
break;
case 'dialect':
$obj->dialect = (int) $v;
break;
case 'charset':
$obj->charset = $v;
$obj->charSet = $v;
break;
case 'buffers':
$obj->buffers = $v;
break;
case 'fetchmode':
$obj->SetFetchMode($v);
break;
//ado
case 'charpage':
$obj->charPage = $v;
break;
//mysql, mysqli
case 'clientflags':
$obj->clientFlags = $v;
break;
//mysql, mysqli, postgres
case 'port':
$obj->port = $v;
break;
//mysqli
case 'socket':
$obj->socket = $v;
break;
//oci8
case 'nls_date_format':
$obj->NLS_DATE_FORMAT = $v;
break;
case 'cachesecs':
$obj->cacheSecs = $v;
break;
case 'memcache':
$varr = explode(':', $v);
$vlen = sizeof($varr);
if (0 == $vlen) {
break;
}
$obj->memCache = true;
$obj->memCacheHost = explode(',', $varr[0]);
if (1 == $vlen) {
break;
}
$obj->memCachePort = $varr[1];
if (2 == $vlen) {
break;
}
$obj->memCacheCompress = $varr[2] ? true : false;
break;
}
}
if (empty($persist)) {
$ok = $obj->Connect($dsna['host'], $dsna['user'], $dsna['pass'], $dsna['path']);
} elseif (empty($nconnect)) {
$ok = $obj->PConnect($dsna['host'], $dsna['user'], $dsna['pass'], $dsna['path']);
} else {
$ok = $obj->NConnect($dsna['host'], $dsna['user'], $dsna['pass'], $dsna['path']);
}
if (!$ok) {
return $false;
}
}
}
return $obj;
}
// $perf == true means called by NewPerfMonitor(), otherwise for data dictionary
function _adodb_getdriver($provider, $drivername, $perf = false)
{
switch ($provider) {
case 'odbtp':
if (0 == strncmp('odbtp_', $drivername, 6)) {
return substr($drivername, 6);
}
// no break
case 'odbc':
if (0 == strncmp('odbc_', $drivername, 5)) {
return substr($drivername, 5);
}
// no break
case 'ado':
if (0 == strncmp('ado_', $drivername, 4)) {
return substr($drivername, 4);
}
// no break
case 'native':
break;
default:
return $provider;
}
switch ($drivername) {
case 'mysqlt':
case 'mysqli':
$drivername = 'mysql';
break;
case 'postgres7':
case 'postgres8':
$drivername = 'postgres';
break;
case 'firebird15':
$drivername = 'firebird';
break;
case 'oracle':
$drivername = 'oci8';
break;
case 'access':
if ($perf) {
$drivername = '';
}
break;
case 'db2':
break;
case 'sapdb':
break;
default:
$drivername = 'generic';
break;
}
return $drivername;
}
function NewPerfMonitor(&$conn)
{
$false = false;
$drivername = _adodb_getdriver($conn->dataProvider, $conn->databaseType, true);
if (!$drivername || 'generic' == $drivername) {
return $false;
}
include_once ADODB_DIR.'/adodb-perf.inc.php';
@include_once ADODB_DIR."/perf/perf-$drivername.inc.php";
$class = "Perf_$drivername";
if (!class_exists($class)) {
return $false;
}
$perf = new $class($conn);
return $perf;
}
function NewDataDictionary(&$conn, $drivername = false)
{
$false = false;
if (!$drivername) {
$drivername = _adodb_getdriver($conn->dataProvider, $conn->databaseType);
}
include_once ADODB_DIR.'/adodb-lib.inc.php';
include_once ADODB_DIR.'/adodb-datadict.inc.php';
$path = ADODB_DIR."/datadict/datadict-$drivername.inc.php";
if (!file_exists($path)) {
ADOConnection::outp("Dictionary driver '$path' not available");
return $false;
}
include_once $path;
$class = "ADODB2_$drivername";
$dict = new $class();
$dict->dataProvider = $conn->dataProvider;
$dict->connection = $conn;
$dict->upperName = strtoupper($drivername);
$dict->quote = $conn->nameQuote;
if (!empty($conn->_connectionID)) {
$dict->serverInfo = $conn->ServerInfo();
}
return $dict;
}
/*
Perform a print_r, with pre tags for better formatting.
*/
function adodb_pr($var, $as_string = false)
{
if ($as_string) {
ob_start();
}
if (isset($_SERVER['HTTP_USER_AGENT'])) {
echo " <pre>\n";
print_r($var);
echo "</pre>\n";
} else {
print_r($var);
}
if ($as_string) {
$s = ob_get_contents();
ob_end_clean();
return $s;
}
}
/*
Perform a stack-crawl and pretty print it.
@param printOrArr Pass in a boolean to indicate print, or an $exception->trace array (assumes that print is true then).
@param levels Number of levels to display
*/
function adodb_backtrace($printOrArr = true, $levels = 9999, $ishtml = null)
{
global $ADODB_INCLUDED_LIB;
if (empty($ADODB_INCLUDED_LIB)) {
include ADODB_DIR.'/adodb-lib.inc.php';
}
return _adodb_backtrace($printOrArr, $levels, 0, $ishtml);
}