~ubuntu-dev/ubuntu/lucid/zabbix/lucid-201002110857

« back to all changes in this revision

Viewing changes to frontends/php/include/db.inc.php

  • Committer: Bazaar Package Importer
  • Author(s): Michael Ablassmeier
  • Date: 2007-07-02 09:06:51 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070702090651-8l6fl3fjw9rh6l2u
Tags: 1:1.4.1-2
Add patch from SVN in order to fix Incorrect processing of character '%'
in user parameters and remote commands.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
**/
20
20
?>
21
21
<?php
22
 
 
23
 
// DATABASE CONFIGURATION
24
 
 
25
 
//      $DB_TYPE        ="ORACLE";
26
 
//      $DB_TYPE        ="POSTGRESQL";
27
 
        $DB_TYPE        ="MYSQL";
28
 
        $DB_SERVER      ="localhost";
29
 
        $DB_DATABASE    ="zabbix";
30
 
        $DB_USER        ="zabbix";
31
 
        $DB_PASSWORD    ="";
32
 
// END OF DATABASE CONFIGURATION
33
 
 
34
 
        global $USER_DETAILS;
35
 
 
36
 
        if($DB_TYPE == "MYSQL")
37
 
        {
38
 
                $DB=mysql_pconnect($DB_SERVER,$DB_USER,$DB_PASSWORD);
39
 
                if(!mysql_select_db($DB_DATABASE))
40
 
                {
41
 
                        echo "Error connecting to database [".mysql_error()."]";
42
 
                        exit;
43
 
                }
44
 
                mysql_select_db($DB_DATABASE);
45
 
        }
46
 
        if($DB_TYPE == "POSTGRESQL")
47
 
        {
48
 
                $DB=pg_pconnect("host='$DB_SERVER' dbname='$DB_DATABASE' user='$DB_USER' password='$DB_PASSWORD'");
49
 
                if(!$DB)
50
 
                {
51
 
                        echo "Error connecting to database";
52
 
                        exit;
53
 
                }
54
 
        }
55
 
 
56
 
        if($DB_TYPE == "ORACLE")
57
 
        {
58
 
                $DB = ocilogon($DB_USER, $DB_PASSWORD, "");
59
 
//              $DB = ocilogon($DB_USER, $DB_PASSWORD, "(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=$DB_SERVER)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=$DB_DATABASE)))");
60
 
                if(!$DB)
61
 
                {
62
 
                        echo "Error connecting to database";
63
 
                        exit;
64
 
                }
 
22
        global $DB, $DB_TYPE, $DB_SERVER, $DB_PORT, $DB_DATABASE, $DB_USER, $DB_PASSWORD;
 
23
 
 
24
        function        DBconnect(&$error)
 
25
        {
 
26
                $result = true;
 
27
                
 
28
                global $DB, $DB_TYPE, $DB_SERVER, $DB_PORT, $DB_DATABASE, $DB_USER, $DB_PASSWORD;
 
29
 
 
30
                $DB = null;
 
31
 
 
32
//SDI('type: '.$DB_TYPE.'; server: '.$DB_SERVER.'; port: '.$DB_PORT.'; db: '.$DB_DATABASE.'; usr: '.$DB_USER.'; pass: '.$DB_PASSWORD);
 
33
 
 
34
                if(!isset($DB_TYPE))
 
35
                {
 
36
                        $error = "Unknown database type.";
 
37
                        $result = false;
 
38
                }
 
39
                else
 
40
                {
 
41
                        switch($DB_TYPE)
 
42
                        {
 
43
                                case "MYSQL":
 
44
                                        $mysql_server = $DB_SERVER.( !empty($DB_PORT) ? ':'.$DB_PORT : '');
 
45
 
 
46
                                        if ( !($DB = mysql_pconnect($mysql_server,$DB_USER,$DB_PASSWORD)))
 
47
                                        {
 
48
                                                $error = "Error connecting to database [".mysql_error()."]";
 
49
                                                $result = false;
 
50
                                        }
 
51
                                        else
 
52
                                        {
 
53
                                                if ( !mysql_select_db($DB_DATABASE) )
 
54
                                                {
 
55
                                                        $error = 'Error database selection ['.mysql_error().']';
 
56
                                                        $result = false;
 
57
                                                }
 
58
                                        }
 
59
                                        break;
 
60
                                case "POSTGRESQL":
 
61
                                        $pg_connection_string = 
 
62
                                                ( !empty($DB_SERVER) ? 'host=\''.$DB_SERVER.'\' ' : '').
 
63
                                                'dbname=\''.$DB_DATABASE.'\' '.
 
64
                                                ( !empty($DB_USER) ? 'user=\''.$DB_USER.'\' ' : '').
 
65
                                                ( !empty($DB_PASSWORD) ? 'password=\''.$DB_PASSWORD.'\' ' : '').
 
66
                                                ( !empty($DB_PORT) ? 'port='.$DB_PORT : '');
 
67
SDI($pg_connection_string);
 
68
                                        $DB=pg_pconnect($pg_connection_string);
 
69
                                        if(!$DB)
 
70
                                        {
 
71
                                                $error = 'Error connecting to database';
 
72
                                                $result = false;
 
73
                                        }
 
74
                                        break;
 
75
                                case "ORACLE":
 
76
                                        $DB = ocilogon($DB_USER, $DB_PASSWORD, $DB_DATABASE);
 
77
                                        //$DB = ocilogon($DB_USER, $DB_PASSWORD, "(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=$DB_SERVER)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=$DB_DATABASE)))");
 
78
                                        if(!$DB)
 
79
                                        {
 
80
                                                $error = "Error connecting to database";
 
81
                                                $result = false;
 
82
                                        }
 
83
                                        break;
 
84
                                case "SQLITE3":
 
85
                                        if(!function_exists('init_db_access'))
 
86
                                        {
 
87
                                                function init_db_access()
 
88
                                                {
 
89
                                                        global $DB_DATABASE, $ZBX_SEM_ID;
 
90
 
 
91
                                                        $ZBX_SEM_ID = false;
 
92
                                                        if(function_exists('ftok') && function_exists('sem_get') &&
 
93
                                                                file_exists($DB_DATABASE))
 
94
                                                        {
 
95
                                                                $ZBX_SEM_ID = sem_get(ftok($DB_DATABASE, 'z'), 1);
 
96
                                                        }
 
97
                                                }
 
98
                                        }
 
99
 
 
100
                                        if(!function_exists('lock_db_access'))
 
101
                                        {
 
102
                                                function lock_db_access()
 
103
                                                {
 
104
                                                        global $ZBX_SEM_ID;
 
105
 
 
106
                                                        if($ZBX_SEM_ID && function_exists('sem_acquire'))
 
107
                                                        {
 
108
                                                                sem_acquire($ZBX_SEM_ID);
 
109
                                                        }
 
110
                                                }
 
111
                                        }
 
112
 
 
113
                                        if(!function_exists('unlock_db_access'))
 
114
                                        {
 
115
                                                function unlock_db_access()
 
116
                                                {
 
117
                                                        global $ZBX_SEM_ID;
 
118
 
 
119
                                                        if($ZBX_SEM_ID && function_exists('sem_release'))
 
120
                                                                sem_release($ZBX_SEM_ID);
 
121
                                                }
 
122
                                        }
 
123
 
 
124
                                        if(!function_exists('free_db_access'))
 
125
                                        {
 
126
                                                function free_db_access()
 
127
                                                {
 
128
                                                        global $ZBX_SEM_ID;
 
129
 
 
130
                                                        if($ZBX_SEM_ID && function_exists('sem_remove'))
 
131
                                                                sem_remove($ZBX_SEM_ID);
 
132
 
 
133
                                                        $ZBX_SEM_ID = false;
 
134
                                                }
 
135
                                        }
 
136
 
 
137
 
 
138
                                        if(file_exists($DB_DATABASE))
 
139
                                        {
 
140
                                                $DB = sqlite3_open($DB_DATABASE);
 
141
                                                if(!$DB)
 
142
                                                {
 
143
                                                        $error = "Error connecting to database";
 
144
                                                        $result = false;
 
145
                                                }
 
146
                                        }
 
147
                                        else
 
148
                                        {
 
149
                                                $error = "Missed database";
 
150
                                                $result = false;
 
151
                                        }
 
152
 
 
153
                                        init_db_access();
 
154
                                        break;
 
155
                                default:
 
156
                                        $error = "Unsupported database";
 
157
                                        $result = false;
 
158
                        }
 
159
                }
 
160
                if( false == $result )
 
161
                        $DB = null;
 
162
 
 
163
                return $result;
 
164
        }
 
165
 
 
166
        function        DBclose()
 
167
        {
 
168
                global $DB, $DB_TYPE, $DB_SERVER, $DB_PORT, $DB_DATABASE, $DB_USER, $DB_PASSWORD;
 
169
 
 
170
                $result = false;
 
171
 
 
172
                if( isset($DB) && !empty($DB) )
 
173
                {
 
174
                        switch($DB_TYPE)
 
175
                        {
 
176
                                case "MYSQL":           $result = mysql_close($DB);     break;
 
177
                                case "POSTGRESQL":      $result = pg_close($DB);        break;
 
178
                                case "ORACLE":          $result = ocilogoff($DB);       break;
 
179
                                case "SQLITE3":         
 
180
                                        $result = true; 
 
181
                                        sqlite3_close($DB);     
 
182
                                        free_db_access();
 
183
                                        break;
 
184
                                default:                break;
 
185
                        }
 
186
                }
 
187
 
 
188
                unset(
 
189
                        $GLOBALS['DB'],
 
190
                        $GLOBALS['DB_TYPE'],
 
191
                        $GLOBALS['DB_SERVER'],
 
192
                        $GLOBALS['DB_PORT'],
 
193
                        $GLOBALS['DB_DATABASE'],
 
194
                        $GLOBALS['DB_USER'],
 
195
                        $GLOBALS['DB_PASSWORD']
 
196
                        );
 
197
                
 
198
                return $result;
 
199
        }
 
200
 
 
201
        function        DBloadfile($file, &$error)
 
202
        {
 
203
                global $DB_TYPE;
 
204
 
 
205
                if(!file_exists($file))
 
206
                {
 
207
                        $error = 'DBloadfile. Missing file['.$file.']';
 
208
                        return false;
 
209
                }
 
210
                
 
211
                $fl = file($file);
 
212
                
 
213
                foreach($fl as $n => $l) if(substr($l,0,2)=='--') unset($fl[$n]);
 
214
                
 
215
                $fl = explode(";\n", implode("\n",$fl));
 
216
                unset($fl[count($fl)-1]);
 
217
                
 
218
                foreach($fl as $sql)
 
219
                {
 
220
                        if(empty($sql)) continue;
 
221
 
 
222
                        if(!DBexecute($sql,0))
 
223
                        {
 
224
                                $error = '';
 
225
                                return false;
 
226
                        }
 
227
                }
 
228
                return true;
 
229
        }
 
230
 
 
231
        function        DBstart()
 
232
        {
 
233
                /* TODO *//* start transaction */
 
234
                // lock_db_access(); /* check DBselect & DBexecute */
 
235
        }
 
236
        
 
237
        function        DBend($result)
 
238
        {
 
239
                /* end transaction *//* TODO */
 
240
 
 
241
                if($result)
 
242
                { // OK
 
243
                        /* commit TODO */
 
244
                }
 
245
                else
 
246
                { // FAIL
 
247
                        /* rollback  TODO */
 
248
                }
 
249
                // unlock_db_access(); /* check DBselect & DBexecute */
65
250
        }
66
251
 
67
252
        /* NOTE:
79
264
                        SELECT * FROM (SELECT ROWNUM as RN, * FROM tbl) WHERE RN BETWEEN 6 AND 15
80
265
        */
81
266
 
82
 
        function        DBselect($query, $limit='NO')
 
267
        function        &DBselect($query, $limit='NO')
83
268
        {
84
 
                global $DB,$DB_TYPE;
85
 
 
86
 
//SDI('DBselect: ['.$query.']');
 
269
                global $DB, $DB_TYPE;
87
270
 
88
271
COpt::savesqlrequest($query);
89
272
 
90
273
                $result = false;
91
 
 
92
 
                if($DB_TYPE == "MYSQL")
93
 
                {
94
 
                        if(is_numeric($limit))
95
 
                        {
96
 
                                $query .= ' limit '.intval($limit);
97
 
                        }
98
 
                        $result=mysql_query($query,$DB);
99
 
                        if(!$result)
100
 
                        {
101
 
                                echo "Error in query [$query] [".mysql_error()."]";
102
 
                        }
103
 
                }
104
 
                if($DB_TYPE == "POSTGRESQL")
105
 
                {
106
 
                        if(is_numeric($limit))
107
 
                        {
108
 
                                $query .= ' limit '.intval($limit);
109
 
                        }
110
 
                        if(!($result=pg_exec($DB,$query)))
111
 
                        {
112
 
                                echo "Error in query [$query] [".pg_errormessage()."]";
113
 
                        }
114
 
                }
115
 
                if($DB_TYPE == "ORACLE")
116
 
                {
117
 
                        if(is_numeric($limit))
118
 
                        {
119
 
                                $query = 'select * from ('.$query.') where rownum<'.intval($limit);
120
 
                        }
121
 
 
122
 
                        $stid=OCIParse($DB,$query);
123
 
                        if(!$stid)
124
 
                        {
125
 
                                $e=ocierror();
126
 
                                error("SQL error [".$e["message"]."] in [".$e["sqltext"]."]");
127
 
                        }
128
 
                        $result=OCIExecute($stid);
129
 
                        if(!$result)
130
 
                        {
131
 
                                $e=ocierror($stid);
132
 
                                error("SQL error [".$e["message"]."] in [".$e["sqltext"]."]");
133
 
                        }
134
 
                        $result = $stid;
135
 
                }
136
 
 
137
 
//SDI("DBselect: result = '".$result."'");
 
274
                if( isset($DB) && !empty($DB) )
 
275
                switch($DB_TYPE)
 
276
                {
 
277
                        case "MYSQL":
 
278
                                if(is_numeric($limit))
 
279
                                {
 
280
                                        $query .= ' limit '.intval($limit);
 
281
                                }
 
282
                                $result=mysql_query($query,$DB);
 
283
                                if(!$result)
 
284
                                {
 
285
                                        error("Error in query [$query] [".mysql_error()."]");
 
286
                                }
 
287
                                break;
 
288
                        case "POSTGRESQL":
 
289
                                if(is_numeric($limit))
 
290
                                {
 
291
                                        $query .= ' limit '.intval($limit);
 
292
                                }
 
293
                                if(!($result = pg_query($DB,$query)))
 
294
                                {
 
295
                                        error("Error in query [$query] [".pg_last_error()."]");
 
296
                                }
 
297
                                break;
 
298
                        case "ORACLE":
 
299
                                if(is_numeric($limit))
 
300
                                {
 
301
                                        $query = 'select * from ('.$query.') where rownum<'.intval($limit);
 
302
                                }
 
303
 
 
304
                                $stid=OCIParse($DB,$query);
 
305
                                if(!$stid)
 
306
                                {
 
307
                                        $e=@ocierror();
 
308
                                        error("SQL error [".$e["message"]."] in [".$e["sqltext"]."]");
 
309
                                }
 
310
                                $result=@OCIExecute($stid);
 
311
                                if(!$result)
 
312
                                {
 
313
                                        $e=ocierror($stid);
 
314
                                        error("SQL error [".$e["message"]."] in [".$e["sqltext"]."]");
 
315
                                }
 
316
                                else
 
317
                                {
 
318
                                        $result = $stid;
 
319
                                }
 
320
                                break;
 
321
                        case "SQLITE3":
 
322
                                lock_db_access();
 
323
                                if(!($result = sqlite3_query($DB,$query)))
 
324
                                {
 
325
                                        error("Error in query [$query] [".sqlite3_error($DB)."]");
 
326
                                }
 
327
                                else
 
328
                                {
 
329
                                        $data = array();
 
330
 
 
331
                                        while($row = sqlite3_fetch_array($result))
 
332
                                        {
 
333
                                                foreach($row as $id => $name)
 
334
                                                {
 
335
                                                        if(!strstr($id,'.')) continue;
 
336
                                                        $ids = explode('.',$id);
 
337
                                                        $row[array_pop($ids)] = $row[$id];
 
338
                                                        unset($row[$id]);
 
339
                                                }
 
340
                                                $data[] = $row;
 
341
                                        }
 
342
 
 
343
                                        sqlite3_query_close($result);
 
344
 
 
345
                                        $result = &$data;
 
346
                                }
 
347
                                unlock_db_access();
 
348
                                break;
 
349
                }
 
350
                
138
351
                return $result;
139
352
        }
140
353
 
142
355
        {
143
356
                global $DB,$DB_TYPE;
144
357
 
145
 
//SDI('DBexecute: ['.$query.']');
146
 
 
147
358
COpt::savesqlrequest($query);
148
359
 
149
 
                $result = FALSE;
150
 
 
151
 
                if($DB_TYPE == "MYSQL")
152
 
                {
153
 
                        $result=mysql_query($query,$DB);
154
 
 
155
 
                        if(!$result && $skip_error_messages==0)
156
 
                        {
157
 
                                error("SQL error: ".mysql_error());
158
 
                                error("Query: $query");
159
 
                        }
160
 
                }
161
 
                if($DB_TYPE == "POSTGRESQL")
162
 
                {
163
 
                        if(!($result=pg_exec($DB,$query)))
164
 
                        {
165
 
                                echo "Error in query [$query] [".pg_errormessage()."]";
166
 
                        }
167
 
                }
168
 
                if($DB_TYPE == "ORACLE")
169
 
                {
170
 
 
171
 
                        $result = DBselect($query);
172
 
                }
173
 
 
174
 
//SDI("DBexecute: result = '".$result."'");
175
 
 
176
 
                return $result;
177
 
        }
178
 
 
179
 
        function        DBfetch($cursor)
180
 
        {
181
 
                global $DB_TYPE;
182
 
 
183
 
                if($DB_TYPE == "MYSQL")
184
 
                {
185
 
                        $row=mysql_fetch_array($cursor);
186
 
                        return $row;
187
 
                }
188
 
                if($DB_TYPE == "POSTGRESQL")
189
 
                {
190
 
                        $row=pg_fetch_array($cursor);
191
 
                        return $row;
192
 
                }
193
 
                if($DB_TYPE == "ORACLE")
194
 
                {
195
 
//                      echo "DBfetch<br>";
196
 
                        if(!ocifetchinto($cursor, $row, OCI_ASSOC+OCI_NUM+OCI_RETURN_NULLS))
197
 
                        {
198
 
                                return FALSE;
199
 
                        }
200
 
                        else
201
 
                        {
202
 
                                $result=array();
203
 
                                $keys = (array_keys($row));
204
 
                                foreach ($keys as $k)
205
 
                                {
206
 
                                        $result[strtolower($k)]=$row[$k];
207
 
                                }
208
 
                        } 
209
 
                        return $result;
210
 
                }
211
 
                return FALSE;
212
 
        }
213
 
 
214
 
        function        get_field($result,$rownum,$fieldnum)
215
 
        {
216
 
                global $DB_TYPE;
217
 
 
218
 
                if($DB_TYPE == "MYSQL")
219
 
                {
220
 
                        mysql_data_seek($result,$rownum);
221
 
                        $row=mysql_fetch_row($result);
222
 
                        return $row[$fieldnum];
223
 
                }
224
 
                if($DB_TYPE == "POSTGRESQL")
225
 
                {
226
 
                        $row=pg_fetch_row($result,$rownum);
227
 
                        if(!$row)
228
 
                        {
229
 
                                echo "Error getting row";
230
 
                                exit;
231
 
                        }
232
 
                        return $row[$fieldnum];
233
 
                }
234
 
                if($DB_TYPE == "ORACLE")
235
 
                {
236
 
                        $result=FALSE;
237
 
                }
238
 
        }
239
 
 
240
 
        function        DBinsert_id($result,$table,$field)
241
 
        {
242
 
                global  $DB,$DB_TYPE;
243
 
 
244
 
                if($DB_TYPE == "MYSQL")
245
 
                {
246
 
                        return mysql_insert_id($DB);
247
 
                }
248
 
 
249
 
                if($DB_TYPE == "POSTGRESQL")
250
 
                {
251
 
//                      $oid=pg_getlastoid($result);
252
 
                        $oid=pg_last_oid($result);
253
 
//                      echo "OID:$oid<br>";
254
 
                        $sql="select $field from $table where oid=$oid";
255
 
                        $result=DBselect($sql);
256
 
                        return get_field($result,0,0);
257
 
                }
258
 
                if($DB_TYPE == "ORACLE")
259
 
                {
260
 
/*                      $sql="select max($field) from $table";
261
 
                        $parse=DBexecute($sql);
262
 
                        while(OCIFetch($parse))
263
 
                        {
264
 
                                $colvalue = OCIResult($parse, 1);
265
 
                                return $colvalue;
266
 
                        }
267
 
*/
268
 
                        $res = DBfetch(DBselect('select '.$table.'_'.$field.'.currval from dual'));
269
 
                        return $res[0];
270
 
                }
271
 
        }
 
360
                $result = false;
 
361
 
 
362
                if( isset($DB) && !empty($DB) )
 
363
                switch($DB_TYPE)
 
364
                {
 
365
                        case "MYSQL":
 
366
                                $result=mysql_query($query,$DB);
 
367
 
 
368
                                if(!$result)
 
369
                                {
 
370
                                        error("Error in query [$query] [".mysql_error()."]");
 
371
                                }
 
372
                                break;
 
373
                        case "POSTGRESQL":
 
374
                                if(!($result = pg_query($DB,$query)))
 
375
                                {
 
376
                                        error("Error in query [$query] [".pg_last_error()."]");
 
377
                                }
 
378
                                break;
 
379
                        case "ORACLE":
 
380
                                $result = DBselect($query);
 
381
                                if(!$result)
 
382
                                {
 
383
                                        $e = ocierror($stid);
 
384
                                        error("SQL error [".$e["message"]."] in [".$e["sqltext"]."]");
 
385
                                }
 
386
                                break;
 
387
                        case "SQLITE3":
 
388
                                lock_db_access();
 
389
                                $result = sqlite3_exec($DB, $query);
 
390
                                if(!$result)
 
391
                                {
 
392
                                        error("Error in query [$query] [".sqlite3_error($DB)."]");
 
393
                                }
 
394
                                unlock_db_access();
 
395
                                break;
 
396
                }
 
397
 
 
398
                return $result;
 
399
        }
 
400
 
 
401
        function        DBfetch(&$cursor)
 
402
        {
 
403
                global $DB, $DB_TYPE;
 
404
        
 
405
                $result = false;
 
406
                
 
407
                if( isset($DB) && !empty($DB) )
 
408
                switch($DB_TYPE)
 
409
                {
 
410
                        case "MYSQL":
 
411
                                $result = mysql_fetch_array($cursor);
 
412
                                break;
 
413
                        case "POSTGRESQL":
 
414
                                $result = pg_fetch_array($cursor);
 
415
                                break;
 
416
                        case "ORACLE":
 
417
                                if(ocifetchinto($cursor, $row, OCI_ASSOC+OCI_NUM+OCI_RETURN_NULLS))
 
418
                                {
 
419
                                        $result = array();
 
420
                                        $keys = (array_keys($row));
 
421
                                        foreach ($keys as $k)           $result[strtolower($k)] = $row[$k];
 
422
                                } 
 
423
                                break;
 
424
                        case "SQLITE3":
 
425
                                if($cursor)
 
426
                                {
 
427
                                        $result = array_shift($cursor);
 
428
 
 
429
                                        if(is_null($result)) $result = false;
 
430
 
 
431
                                }
 
432
                                break;
 
433
                }
 
434
 
 
435
                return $result;
 
436
        }
 
437
        
272
438
 
273
439
/* string value prepearing */
274
 
if($DB_TYPE == "ORACLE") {      
 
440
if(isset($DB_TYPE) && $DB_TYPE == "ORACLE") {   
275
441
        function        zbx_dbstr($var) {
276
442
                return "'".ereg_replace('\'','\'\'',$var)."'";  
277
443
        }
280
446
                return "'".addslashes($var)."'";
281
447
        }
282
448
}
 
449
 
 
450
        function        zbx_dbconcat($params)
 
451
        {
 
452
                global $DB_TYPE;
 
453
 
 
454
                switch($DB_TYPE)
 
455
                {
 
456
                        case "SQLITE3":
 
457
                                return implode(' || ',$params);
 
458
                        default:
 
459
                                return 'CONCAT('.implode(',',$params).')';
 
460
                }
 
461
        }
 
462
 
 
463
        function DBid2nodeid($id_name)
 
464
        {
 
465
                global $DB_TYPE;
 
466
 
 
467
                switch($DB_TYPE)
 
468
                {
 
469
                        case "MYSQL":
 
470
                                $result = '('.$id_name.' div 100000000000000)';
 
471
                                break;
 
472
                        case "ORACLE":
 
473
                                $result = 'round('.$id_name.'/100000000000000)';
 
474
                                break;
 
475
                        default:
 
476
                                $result = '('.$id_name.'/100000000000000)';
 
477
                }
 
478
                return $result;
 
479
        }
 
480
 
 
481
        function id2nodeid($id_var)
 
482
        {
 
483
                return (int)bcdiv("$id_var","100000000000000");
 
484
        }
 
485
 
 
486
        function        get_dbid($table,$field)
 
487
        {
 
488
                global  $ZBX_CURNODEID;
 
489
 
 
490
                if(!isset($ZBX_CURNODEID))      init_nodes();
 
491
 
 
492
/*              $row=DBfetch(DBselect("select max($field) as id from $table where ".DBid2nodeid($field)." in (".$ZBX_CURNODEID.")"));
 
493
                if($row && !is_null($row["id"]))
 
494
                {
 
495
                        return  bcadd($row["id"],1);
 
496
                }
 
497
                else
 
498
                {
 
499
                        return bcadd(bcmul($ZBX_CURNODEID,"100000000000000"),1);
 
500
                }*/
 
501
 
 
502
 
 
503
                $found = false;
 
504
                do
 
505
                {
 
506
                        $row = DBfetch(DBselect("select nextid from ids where nodeid=$ZBX_CURNODEID and table_name='$table' and field_name='$field'"));
 
507
                        if(!$row || is_null($row["nextid"]))
 
508
                        {
 
509
                                $row=DBfetch(DBselect("select max($field) as id from $table where ".DBid2nodeid($field)." in (".$ZBX_CURNODEID.")"));
 
510
                                if(!$row || is_null($row["id"]))
 
511
                                {
 
512
                                        DBexecute("insert into ids (nodeid,table_name,field_name,nextid) values ($ZBX_CURNODEID,'$table','$field',".bcadd(bcmul($ZBX_CURNODEID,"100000000000000"),1).")");
 
513
                                }
 
514
                                else
 
515
                                {
 
516
                                        DBexecute("insert into ids (nodeid,table_name,field_name,nextid) values ($ZBX_CURNODEID,'$table','$field',".$row["id"].")");
 
517
                                }
 
518
                                continue;
 
519
                        }
 
520
                        else
 
521
                        {
 
522
                                $ret1 = $row["nextid"];
 
523
        
 
524
                                DBexecute("update ids set nextid=nextid+1 where nodeid=$ZBX_CURNODEID and table_name='$table' and field_name='$field'");
 
525
        
 
526
                                $row = DBfetch(DBselect("select nextid from ids where nodeid=$ZBX_CURNODEID and table_name='$table' and field_name='$field'"));
 
527
                                if(!$row || is_null($row["nextid"]))
 
528
                                {
 
529
                                        /* Should never be here */
 
530
                                        continue;
 
531
                                }
 
532
                                else
 
533
                                {
 
534
                                        $ret2 = $row["nextid"];
 
535
                                        if(bccomp(bcadd($ret1,1),$ret2) ==0)
 
536
                                        {
 
537
                                                $found = true;
 
538
                                        }
 
539
                                }
 
540
                        }
 
541
                }
 
542
                while(false == $found);
 
543
 
 
544
                return $ret2;
 
545
        }
283
546
?>