aboutsummaryrefslogtreecommitdiffstats
path: root/mayor-orig/www/include/base/mysql.php
blob: b2e22f828af09848a4b4b6a6f7bb5e0e3c5fe27e (plain)
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
<?php

    error_reporting(E_ALL && ~E_NOTICE);
//    error_reporting(E_ALL);

    //if (!defined("MYSQLI_ENABLED")) define("MYSQLI_ENABLED",function_exists('mysqli_connect'));
    if (!defined("MYSQLI_ENABLED")) define("MYSQLI_ENABLED",false); // egyelőre kikapcsoljuk, mert az Illyés-ben pl egfeküdt a szerver ettől
    if (is_array($AUTH)) foreach ($AUTH as $_policy => $config) {
	if ($config['backend'] == 'mysql') {
	    $MYSQL_DATA[$_policy . ' auth'] = array(
		'user' => $config['mysql user'],
		'host' => $config['mysql host'],
		'pw' => $config['mysql pw'],
		'db' => $config['mysql db'],
	    );
	    if ($MYSQL_DATA[$_policy . ' auth']['host']=='') 
		if ($MYSQL_DATA['host']=='')
		    $MYSQL_DATA[$_policy . ' auth']['host'] = 'localhost';
		else
		    $MYSQL_DATA[$_policy . ' auth']['host'] = $MYSQL_DATA['host'];
	}
    }    
//-----------------------------------------------------------------------//

    function db_selectDb($db,$lr) {
	if (MYSQLI_ENABLED===true)
	    return mysqli_select_db($lr,$db);
	else
	    return mysql_select_db($db,$lr);
    }

    function db_connect($modul, $SET = array('priv' => null, 'force' => true, 'host'=> null, 'username' => null, 'password' => null, 'db' => null, 'fv' => null)) {

	global $MYSQL_DATA;

	$lr = false;
	extract($SET);
	if (!isset($force)) $force = true;
	if (!isset($priv))  $priv = '';

	//if ($MYSQL_DATA['persistent']===true) $mysql_connect = 'mysql_pconnect'; else $mysql_connect = 'mysql_connect';
	$mysql_connect = 'mysql_connect';
	/* setting host */
	if (!isset($host)) {
	    if ($MYSQL_DATA[$modul]['host'.$priv]=='') {
		if ($MYSQL_DATA[$modul]['host']=='') {
		    if ($MYSQL_DATA['host']=='') {
			$host = 'localhost';
		    } else {
			$host = $MYSQL_DATA['host'];
		    }
		} else {
		    $host = $MYSQL_DATA[$modul]['host'];
		}
	    } else {
		$host = $MYSQL_DATA[$modul]['host'.$priv];
	    }
	}
	/* --- */
	if ($priv == 'root') {
	    if (MYSQLI_ENABLED===true)
		$lr = @mysqli_connect($host, $username, $password); // force new ???
	    else
		$lr = @$mysql_connect($host, $username, $password, $force);
	} else {
	    /* --- */
	    if (is_array($MYSQL_DATA[$modul])) {
		if (isset($priv) && $priv != '' && isset($MYSQL_DATA[$modul]['user'.$priv])) { // Először megpróbálunk $priv szerinti privilégiummal csatlakozni
		    if (MYSQLI_ENABLED===true)
			$lr = @mysqli_connect($host, $MYSQL_DATA[$modul]['user'.$priv], $MYSQL_DATA[$modul]['pw'.$priv], $MYSQL_DATA[$modul]['db']);
		    else
			$lr = @$mysql_connect($host, $MYSQL_DATA[$modul]['user'.$priv], $MYSQL_DATA[$modul]['pw'.$priv], $force);
		}
		if ($lr === false) {// Ha nem sikerült, vagy nem volt megadva privilégium, akkor próbáljunk anélkül csatlakozni
		    if (MYSQLI_ENABLED===true)
			$lr = @mysqli_connect($host, $MYSQL_DATA[$modul]['user'], $MYSQL_DATA[$modul]['pw'], $MYSQL_DATA[$modul]['db']);
		    else
			$lr = @$mysql_connect($host, $MYSQL_DATA[$modul]['user'], $MYSQL_DATA[$modul]['pw'], $force);
		}
	    } else {
		$_SESSION['alert'][] = "message:sql_failure/${SET['fv']}:db_connect:modul $modul has no config";
	    }
	}
	if ($lr) {
	    // mysql_set_charset('utf8', $lr);
	    if ($priv != 'root') $db = $MYSQL_DATA[$modul]['db'];
	    if ($db != '') {
		$result = db_selectDb($db , $lr);
		if ($result === true) {
		    if (MYSQLI_ENABLED===true) {
			mysqli_set_charset($lr, "utf8");
			mysqli_query($lr, "SET NAMES utf8");
			mysqli_query($lr, "SET collation_connection='utf8_hungarian_ci'");
		    } else {
			mysql_query("SET NAMES utf8", $lr);
			mysql_query("SET collation_connection='utf8_hungarian_ci'", $lr);
		    }
		} else {
		    $_SESSION['alert'][] = "message:sql_select_db_failure:db_connect/${SET['fv']}:$modul:".$MYSQL_DATA[$modul]['db'];
		    mysql_close($lr);
		    return false;
		}
	    } elseif (!isset($MYSQL_DATA[$modul]['db'])) {
		$_SESSION['alert'][] = "message:sql_warning:db_connect/${SET['fv']}:modul $modul has empty database config value";
	    }
	} else {
	    if (__DEBUG || (defined('__DETAILED') && __DETAILED)) $_SESSION['alert'][] = "message:sql_connect_failure:db_connect/${SET['fv']}:modul - $modul, priv - $priv, username - $username, db - $db";
	    else $_SESSION['alert'][] = "message:sql_connect_failure:db_connect/${SET['fv']}:$modul modul";
	}
	return $lr;

    }

    function db_close($lr) {
	if ($MYSQL_DATA['persistent']!==true) {
	    if (MYSQLI_ENABLED===true)
		return mysqli_close($lr);
	    else
		return mysql_close($lr);
	} else
	    return true; // not closing;
    }

//-----------------------------------------------------------------------//

    function db_query($q, $SET, $olr = null) {
	/*
	    $SET = array(
		'modul' => '...'
		'fv' => '...'
		'result' => 'indexed'|'assoc'|'multiassoc'|'idonly'|'value'|'record'|'keyvaluepair'|'insert'|'affected rows'
		'keyfield' => '...' (if result in (assoc,multiassoc))
		'detailed' => true | false (default) (echo the query in error messages)
		'debug' => true | false (default) (echo the query before quering it)
		'rollback' => true | false (default)
		'values' => array(...) (if $q is an sprintf format string)
	    );
	*/
	global $_JSON;

	if (!isset($q) || $q == '') {
            $_SESSION['alert'][] = 'message:sql_query_failure:db_query/'.$SET['fv'].':query is empty';
            return false;
        }

	if (!isset($SET['result'])) $SET['result'] = '';

	// Adatbázis csatlakozás (ha szükséges)
	if (isset($olr) && $olr != '') {
	    if (MYSQLI_ENABLED===true)
		$mysql_get_server_info = mysqli_get_server_info($olr);
	    else
		$mysql_get_server_info = @mysql_get_server_info($olr);
	}
        if (isset($olr) && $olr != '' && $mysql_get_server_info !== false) {
            $lr = $olr;
        } else {
	    unset($olr);
            if (in_array(substr(strtolower($q),0,4), array('sele','show','expl','set ','use '))) $lr = @db_connect($SET['modul'], array('priv' => 'Read', 'fv' => $SET['fv']));
            else $lr = @db_connect($SET['modul'], array('priv' => 'Write', 'fv' => $SET['fv']));
        }
        if ($lr === false) {
            if ($SET['detailed'] === true || __DETAILED) $_SESSION['alert'][] = 'message:sql_connect_failure:db_query/'.$SET['fv'].':'.$SET['modul'].':'.$q;
	    else $_SESSION['alert'][] = 'message:sql_connect_failure:db_query/'.$SET['fv'];
            return false;
        }

	// Ha behelyettesítendő paraméterek vannak
	if (isset($SET['values']) && is_array($SET['values']) && count($SET['values']) > 0) {
	    $SET['values'] = array_map('db_escape_string', $SET['values'], array_fill(0 , count($SET['values']), $lr));
	    array_unshift($SET['values'], $q);
	    $q_pattern = $q;
	    $q = @call_user_func_array('sprintf', $SET['values']);
	    if ($q === false) {
		$_SESSION['alert'][] = 'message:wrong_data:db_query:behelyettesítés:'.$SET['fv'];
		return false;
	    }
	}
	if ((isset($SET['debug']) && $SET['debug']===true) || (defined('__DEBUG') && __DEBUG === true)) {
	    $_q = str_replace("	",'',$q);
	    echo '<pre>info:debug:'.htmlspecialchars($SET['modul'].':'.$SET['fv'].':'.date('Y-m-d H:i:s').': '." \n".$_q)."<hr /></pre>";
	    if ($_GET['skin'] == 'ajax') $_JSON['sql log'][] = $SET['modul'].':'.$SET['fv'].':'.date('Y-m-d H:i:s').': '." \n".$_q;
	    if ($_GET['skin'] == 'rpc') {
		openlog("MaYoR", LOG_PID | LOG_PERROR, LOG_LOCAL0);
		syslog(LOG_WARNING, '[RPC]MySQL: '.(json_encode($SET)).", query: $_q {$_SERVER['REMOTE_ADDR']} ({$_SERVER['HTTP_USER_AGENT']})");
		closelog();
	    }
	}
	if (
	    ($SET['detailed'] === true || __DETAILED)
	    && strpos($q_pattern, '%s') !== false 
	    && (strpos($q_pattern, '`%s`') === false && strpos($q_pattern, "'%s'") === false)
	) $_SESSION['alert'][] = 'message:lehet hiba?:db_query/'.$SET['fv'].':'.$SET['modul'].':'.$q_pattern;

	if ($SET['log']===true) mayorLogger(10,'mysql',$q,_USERACCOUNT);
	if (MYSQLI_ENABLED===true)
	    $r = mysqli_query($lr,$q);
	else
    	    $r = @mysql_query($q, $lr);
	define(MYSQL_LOGGER,false);
	if (MYSQL_LOGGER === true) {
	    $filename = '/tmp/mysql.log';
    	    $fp = fopen($filename, "a+");
    	    fputs ($fp, $q."\n");
    	    fclose ($fp);
	}
	/* WARNING HANDLER */
//	if ((isset($SET['debug']) && $SET['debug']===true) || (defined('__DEBUG') &&__DEBUG === true) || (defined('__DETAILED') && __DETAILED===true)) {
	if ((isset($SET['debug']) && $SET['debug']===true) || (defined('__DEBUG') &&__DEBUG === true)) {
	    if (MYSQLI_ENABLED===true)
		$warningCountResult = mysqli_query($lr,"SELECT @@warning_count");
	    else
		$warningCountResult = mysql_query("SELECT @@warning_count",$lr);
	    if ($warningCountResult) {
		if (MYSQLI_ENABLED===true)
		    $warningCount = mysqli_fetch_row($lr,$warningCountResult);
		else
		    $warningCount = mysql_fetch_row($warningCountResult);
		if ($warningCount[0] > 0) {
    		//Have warnings
		    if (MYSQLI_ENABLED===true)
    			$warningDetailResult = mysqli_query($lr, "SHOW WARNINGS");
		    else
    			$warningDetailResult = mysql_query("SHOW WARNINGS",$lr);
    		    if ($warningDetailResult ) {
			if (MYSQLI_ENABLED===true)
        		    while ($warning = mysqli_fetch_assoc($lr, $warningDetailResult)) {dump($warning); mayorLogger(2,'mysql',$q.' '.$warning,_USERACCOUNT);}
			else
        		    while ($warning = mysql_fetch_assoc($warningDetailResult)) {dump($warning); mayorLogger(2,'mysql',$q.' '.$warning,_USERACCOUNT);}
    		    }
		}//Else no warnings
	    }
	}
	/* WARNING HANDLER */
	if (!$r) {
            // if ($SET['detailed'] === true || __DETAILED) $_SESSION['alert'][] = 'message:sql_query_failure:'.$SET['fv'].':'.':'.$q;
            if ($SET['detailed'] === true || __DETAILED) {
		if (MYSQLI_ENABLED===true) {
		    $_SESSION['alert'][] = 'message:sql_query_failure:mysqli:'.$SET['fv'].':'.mysqli_error($lr).':'.$q;
		} else  {
		    $_SESSION['alert'][] = 'message:sql_query_failure:mysql:'.$SET['fv'].':'.mysql_error($lr).':'.$q;
		}
            } else {
		$_SESSION['alert'][] = 'message:sql_query_failure:'.$SET['fv'];
	    }
	    if ($SET['rollback'] === true) db_rollback($lr, $SET['fv']);
	    if (!isset($olr)) db_close($lr);
	    return false;
	}

	if (MYSQLI_ENABLED === true) {

	    if (in_array(substr(strtolower($q),0,4), array('sele','show','expl','(sel'))) {
        	$RESULT = array();
        	switch($SET['result']) {
                case 'indexed':
                    while ($A = mysqli_fetch_assoc($r)) $RESULT[] = $A;
                    break;
                case 'assoc':
                    while ($A = mysqli_fetch_assoc($r)) $RESULT[$A[$SET['keyfield']]] = $A;
                    break;
                case 'multiassoc':
                    while  ($A = mysqli_fetch_assoc($r)) $RESULT[$A[$SET['keyfield']]][] = $A;
                    break;
                case 'idonly':
                    while ($A = mysqli_fetch_row($r)) $RESULT[] = $A[0];
		    break;
                case 'value':
                    if (mysqli_num_rows($r) > 0) {
			$A = mysqli_fetch_row($r); $RESULT = $A[0];
		    } else { $RESULT = null; }
		    break;
                case 'record':
                    if (mysqli_num_rows($r) > 0) {
                	$A = mysqli_fetch_assoc($r); $RESULT = $A;
		    } else { $RESULT = null; }
		    break;
                case 'keyvaluepair':
                    while ($A = mysqli_fetch_row($r)) $RESULT[$A[0]] = $A[1];
                    break;
                case 'keyvalues':
                    while ($A = mysqli_fetch_row($r)) $RESULT[$A[0]][] = $A[1];
                    break;
        	}
    	    } elseif ($SET['result'] == 'insert' && (substr(strtolower($q),0,6) == 'insert' || substr(strtolower($q),0,7) == 'replace')) {
                $RESULT = mysqli_insert_id($lr);
		mayorLogger(1,'mysql',$q,_USERACCOUNT);
	    } elseif ($SET['result'] == 'affected rows') {
		$RESULT = mysqli_affected_rows($lr);
		mayorLogger(1,'mysql',$q,_USERACCOUNT);
    	    } else { // create, insert, de nem olyan resulttal...
                $RESULT = $r;
		mayorLogger(1,'mysql',$q,_USERACCOUNT);
    	    }
	} else { // OLD DRIVER

	    if (in_array(substr(strtolower($q),0,4), array('sele','show','expl','(sel'))) {
        	$RESULT = array();
        	switch($SET['result']) {
                case 'indexed':
                    while ($A = mysql_fetch_assoc($r)) $RESULT[] = $A;
                    break;
                case 'assoc':
                    while ($A = mysql_fetch_assoc($r)) $RESULT[$A[$SET['keyfield']]] = $A;
                    break;
                case 'multiassoc':
                    while  ($A = mysql_fetch_assoc($r)) $RESULT[$A[$SET['keyfield']]][] = $A;
                    break;
                case 'idonly':
                    while ($A = mysql_fetch_row($r)) $RESULT[] = $A[0];
		    break;
                case 'value':
                    if (mysql_num_rows($r) > 0) {
			$A = mysql_fetch_row($r); $RESULT = $A[0];
		    } else { $RESULT = null; }
		    break;
                case 'record':
                    if (mysql_num_rows($r) > 0) {
                	$A = mysql_fetch_assoc($r); $RESULT = $A;
		    } else { $RESULT = null; }
		    break;
                case 'keyvaluepair':
                    while ($A = mysql_fetch_row($r)) $RESULT[$A[0]] = $A[1];
                    break;
                case 'keyvalues':
                    while ($A = mysql_fetch_row($r)) $RESULT[$A[0]][] = $A[1];
                    break;
        	}
    	    } elseif ($SET['result'] == 'insert' && (substr(strtolower($q),0,6) == 'insert' || substr(strtolower($q),0,7) == 'replace')) {
                $RESULT = mysql_insert_id($lr);
		mayorLogger(1,'mysql',$q,_USERACCOUNT);
	    } elseif ($SET['result'] == 'affected rows') {
		$RESULT = mysql_affected_rows($lr);
		mayorLogger(1,'mysql',$q,_USERACCOUNT);
    	    } else { // create, insert, de nem olyan resulttal...
                $RESULT = $r;
		mayorLogger(1,'mysql',$q,_USERACCOUNT);
    	    }
	} // DRIVER

	if (!isset($olr)) db_close($lr);
	return $RESULT;

    }

//-----------------------------------------------------------------------//

    function db_start_trans($lr) {
        if ($lr != '') 
	    if (MYSQLI_ENABLED === true)
		mysqli_query($lr, "START TRANSACTION");
	    else
		mysql_query("START TRANSACTION", $lr);
    }

    function db_commit($lr) {
        if ($lr != '') 
	    if (MYSQLI_ENABLED === true)
		mysqli_query($lr, "COMMIT");
	    else
		mysql_query("COMMIT", $lr);
    }

    function db_rollback($lr, $msg = '') {
        if ($lr != '') {
            $_SESSION['alert'][] = 'message:rollback:'.$msg;
	    if (MYSQLI_ENABLED === true)
    		mysqli_query($lr, "ROLLBACK");
	    else
    		mysql_query("ROLLBACK", $lr);
        }
    }

//---------------------------------------------------------------------//

    function db_escape_string($str, $olr = null) {

	if (isset($olr)) $lr = $olr;
	else $lr = db_connect('login');

	if (!$lr) return false;

        if(get_magic_quotes_gpc()) {
            $return = mysql_real_escape_string(stripslashes($str), $lr);
        } else {
	    if (MYSQLI_ENABLED===true)
        	$return = mysqli_real_escape_string($lr, $str);
	    else
        	$return = mysql_real_escape_string($str, $lr);
        }
        if (!isset($olr)) db_close($lr);

	return $return;
    }

?>