Esempio n. 1
0
function DBconnect(&$error)
{
    $result = true;
    global $DB;
    $DB['DB'] = null;
    $DB['TRANSACTIONS'] = 0;
    //Stats
    $DB['SELECT_COUNT'] = 0;
    $DB['EXECUTE_COUNT'] = 0;
    //SDI('type: '.$DB['TYPE'].'; server: '.$DB['SERVER'].'; port: '.$DB['PORT'].'; db: '.$DB['DATABASE'].'; usr: '******'USER'].'; pass: '******'PASSWORD']);
    if (!isset($DB['TYPE'])) {
        $error = "Unknown database type.";
        $result = false;
    } else {
        switch ($DB['TYPE']) {
            case 'MYSQL':
                $mysql_server = $DB['SERVER'] . (!empty($DB['PORT']) ? ':' . $DB['PORT'] : '');
                if (!($DB['DB'] = mysql_pconnect($mysql_server, $DB['USER'], $DB['PASSWORD']))) {
                    $error = 'Error connecting to database [' . mysql_error() . ']';
                    $result = false;
                } else {
                    if (!mysql_select_db($DB['DATABASE'])) {
                        $error = 'Error database selection [' . mysql_error() . ']';
                        $result = false;
                    }
                }
                break;
            case 'POSTGRESQL':
                $pg_connection_string = (!empty($DB['SERVER']) ? 'host=\'' . $DB['SERVER'] . '\' ' : '') . 'dbname=\'' . $DB['DATABASE'] . '\' ' . (!empty($DB['USER']) ? 'user=\'' . $DB['USER'] . '\' ' : '') . (!empty($DB['PASSWORD']) ? 'password=\'' . $DB['PASSWORD'] . '\' ' : '') . (!empty($DB['PORT']) ? 'port=' . $DB['PORT'] : '');
                $DB['DB'] = pg_connect($pg_connection_string);
                if (!$DB['DB']) {
                    $error = 'Error connecting to database';
                    $result = false;
                }
                break;
            case 'ORACLE':
                $DB['DB'] = ociplogon($DB['USER'], $DB['PASSWORD'], $DB['DATABASE']);
                //					$DB['DB']= ociplogon($DB['USER'], $DB['PASSWORD'], '(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST='.$DB['SERVER'].')(PORT=1521))(CONNECT_DATA=(SERVICE_NAME='.$DB['DATABASE'].')))');
                if (!$DB['DB']) {
                    $error = 'Error connecting to database';
                    $result = false;
                }
                break;
            case 'SQLITE3':
                $DB['TRANSACTIONS'] = 0;
                if (!function_exists('init_db_access')) {
                    function init_db_access()
                    {
                        global $DB, $ZBX_SEM_ID;
                        $ZBX_SEM_ID = false;
                        if (function_exists('ftok') && function_exists('sem_get') && file_exists($DB['DATABASE'])) {
                            $ZBX_SEM_ID = sem_get(ftok($DB['DATABASE'], 'z'), 1);
                        }
                    }
                }
                if (!function_exists('lock_db_access')) {
                    function lock_db_access()
                    {
                        global $ZBX_SEM_ID;
                        if ($ZBX_SEM_ID && function_exists('sem_acquire')) {
                            sem_acquire($ZBX_SEM_ID);
                        }
                    }
                }
                if (!function_exists('unlock_db_access')) {
                    function unlock_db_access()
                    {
                        global $ZBX_SEM_ID;
                        if ($ZBX_SEM_ID && function_exists('sem_release')) {
                            sem_release($ZBX_SEM_ID);
                        }
                    }
                }
                if (!function_exists('free_db_access')) {
                    function free_db_access()
                    {
                        global $ZBX_SEM_ID;
                        if ($ZBX_SEM_ID && function_exists('sem_remove')) {
                            sem_remove($ZBX_SEM_ID);
                        }
                        $ZBX_SEM_ID = false;
                    }
                }
                if (file_exists($DB['DATABASE'])) {
                    $DB['DB'] = sqlite3_open($DB['DATABASE']);
                    if (!$DB['DB']) {
                        $error = 'Error connecting to database';
                        $result = false;
                    }
                } else {
                    $error = 'Missed database';
                    $result = false;
                }
                init_db_access();
                break;
            default:
                $error = 'Unsupported database';
                $result = false;
        }
    }
    if (false == $result) {
        $DB['DB'] = null;
    }
    return $result;
}
Esempio n. 2
0
function DBconnect(&$error)
{
    $result = true;
    global $DB;
    $DB['DB'] = null;
    $DB['TRANSACTIONS'] = 0;
    //Stats
    $DB['SELECT_COUNT'] = 0;
    $DB['EXECUTE_COUNT'] = 0;
    //SDI('type: '.$DB['TYPE'].'; server: '.$DB['SERVER'].'; port: '.$DB['PORT'].'; db: '.$DB['DATABASE'].'; usr: '******'USER'].'; pass: '******'PASSWORD']);
    if (!isset($DB['TYPE'])) {
        $error = "Unknown database type.";
        $result = false;
    } else {
        $DB['TYPE'] = zbx_strtoupper($DB['TYPE']);
        switch ($DB['TYPE']) {
            case 'MYSQL':
                $mysql_server = $DB['SERVER'] . (!empty($DB['PORT']) ? ':' . $DB['PORT'] : '');
                if (!($DB['DB'] = mysql_connect($mysql_server, $DB['USER'], $DB['PASSWORD']))) {
                    $error = 'Error connecting to database [' . mysql_error() . ']';
                    $result = false;
                } else {
                    if (!mysql_select_db($DB['DATABASE'])) {
                        $error = 'Error database in selection [' . mysql_error() . ']';
                        $result = false;
                    } else {
                        DBexecute('SET NAMES utf8');
                        DBexecute('SET CHARACTER SET utf8');
                    }
                }
                break;
            case 'POSTGRESQL':
                $pg_connection_string = (!empty($DB['SERVER']) ? 'host=\'' . $DB['SERVER'] . '\' ' : '') . 'dbname=\'' . $DB['DATABASE'] . '\' ' . (!empty($DB['USER']) ? 'user=\'' . $DB['USER'] . '\' ' : '') . (!empty($DB['PASSWORD']) ? 'password=\'' . $DB['PASSWORD'] . '\' ' : '') . (!empty($DB['PORT']) ? 'port=' . $DB['PORT'] : '');
                $DB['DB'] = pg_connect($pg_connection_string);
                if (!$DB['DB']) {
                    $error = 'Error connecting to database';
                    $result = false;
                }
                break;
            case 'ORACLE':
                $connect = '';
                if (!empty($DB['SERVER'])) {
                    $connect = '//' . $DB['SERVER'];
                    if ($DB['PORT'] != '0') {
                        $connect .= ':' . $DB['PORT'];
                    }
                    if ($DB['DATABASE']) {
                        $connect .= '/' . $DB['DATABASE'];
                    }
                }
                $DB['DB'] = ociplogon($DB['USER'], $DB['PASSWORD'], $connect);
                //					$DB['DB']= ociplogon($DB['USER'], $DB['PASSWORD'], '(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST='.$DB['SERVER'].')(PORT=1521))(CONNECT_DATA=(SERVICE_NAME='.$DB['DATABASE'].')))');
                if (!$DB['DB']) {
                    $error = 'Error connecting to database';
                    $result = false;
                }
                break;
            case 'IBM_DB2':
                $connect = '';
                $connect .= 'DATABASE=' . $DB['DATABASE'] . ';';
                $connect .= 'HOSTNAME=' . $DB['SERVER'] . ';';
                $connect .= 'PORT=' . $DB['PORT'] . ';';
                $connect .= 'PROTOCOL=TCPIP;';
                $connect .= 'UID=' . $DB['USER'] . ';';
                $connect .= 'PWD=' . $DB['PASSWORD'] . ';';
                $DB['DB'] = db2_connect($connect, $DB['USER'], $DB['PASSWORD']);
                if (!$DB['DB']) {
                    $error = 'Error connecting to database';
                    $result = false;
                } else {
                    $options = array('db2_attr_case' => DB2_CASE_LOWER);
                    db2_set_option($DB['DB'], $options, 1);
                    if (isset($DB['SCHEMA']) && $DB['SCHEMA'] != '') {
                        DBexecute("SET CURRENT SCHEMA='" . $DB['SCHEMA'] . "'");
                    }
                }
                break;
            case 'SQLITE3':
                $DB['TRANSACTIONS'] = 0;
                if (!function_exists('init_db_access')) {
                    function init_db_access()
                    {
                        global $DB, $ZBX_SEM_ID;
                        $ZBX_SEM_ID = false;
                        if (function_exists('ftok') && function_exists('sem_get') && file_exists($DB['DATABASE'])) {
                            $ZBX_SEM_ID = sem_get(ftok($DB['DATABASE'], 'z'), 1);
                        }
                    }
                }
                if (!function_exists('lock_db_access')) {
                    function lock_db_access()
                    {
                        global $ZBX_SEM_ID;
                        if ($ZBX_SEM_ID && function_exists('sem_acquire')) {
                            sem_acquire($ZBX_SEM_ID);
                        }
                    }
                }
                if (!function_exists('unlock_db_access')) {
                    function unlock_db_access()
                    {
                        global $ZBX_SEM_ID;
                        if ($ZBX_SEM_ID && function_exists('sem_release')) {
                            sem_release($ZBX_SEM_ID);
                        }
                    }
                }
                if (!function_exists('free_db_access')) {
                    function free_db_access()
                    {
                        global $ZBX_SEM_ID;
                        if ($ZBX_SEM_ID && function_exists('sem_remove')) {
                            sem_remove($ZBX_SEM_ID);
                        }
                        $ZBX_SEM_ID = false;
                    }
                }
                if (file_exists($DB['DATABASE'])) {
                    $DB['DB'] = sqlite3_open($DB['DATABASE']);
                    if (!$DB['DB']) {
                        $error = 'Error connecting to database';
                        $result = false;
                    }
                } else {
                    $error = 'Missing database';
                    $result = false;
                }
                init_db_access();
                break;
            default:
                $error = 'Unsupported database';
                $result = false;
        }
    }
    if (false == $result) {
        $DB['DB'] = null;
    }
    return $result;
}