Platon Technologies
not logged in Login Registration
EnglishSlovak
open source software development celebrating 10 years of open source development! Saturday, April 18, 2026

File: [Platon] / Metafox / lib / db-core / db-core.inc.php (download)

Revision 1.16, Tue Nov 7 06:00:29 2017 UTC (8 years, 5 months ago) by nepto


Changes since 1.15: +4 -1 lines

Fixed mysqli_escape_string() call: added resource link as a first
parameter

<?php

/*
 * Metafox - flexible content management system
 *
 * db-core.inc.php - DB handle and DB error manipulation interface
 * ____________________________________________________________
 *
 * Developed by Ondrej Jombik <nepto@platon.sk>
 * Copyright (c) 2001-2006 Platon Group, http://platon.sk/
 * All rights reserved.
 *
 * See README file for more information about this software.
 * See COPYING file for license information.
 *
 * Download the latest version from
 * http://platon.sk/projects/Metafox/
 */

/* $Platon: Metafox/lib/db-core/db-core.inc.php,v 1.15 2017/06/09 15:05:11 igor Exp $ */

/* MAINTAINANCE: This file is maintained in Metafox project and distributed
   across the particular projects accordingly. */

/*
 * Metafox global variabiles
 */

global $ezin_db_handle;
$ezin_db_handle = null;

/*
 * ezin_db_error()
 *
 * Database error handling function
 */

function ezin_db_error($fnc_name = '', $message = '') /* {{{ */
{
    if (strlen($message) <= 0) {
        $message = 'error during work with database server';
    }

    if (strlen($fnc_name) > 0) {
        $message = "$fnc_name: $message";
    }

    if (0) {
        die ('<b>Metafox fatal failure</b>: ' . $message 
                . '<br>Please contact webmaster or system administrator'
                . ' of this page with this problem.<br>');
    } else {
        global $ezin_db_handle;
        if (is_resource($ezin_db_handle) && mysqli_error($ezin_db_handle)) {
            $message .= "\n";
            $message .= 'ERROR #'.mysqli_errno($ezin_db_handle).': '.mysqli_error($ezin_db_handle);
        }
        trigger_error($message, E_USER_ERROR);
    }

    return false;
} /* }}} */

/*
 *
 * ezin_get_db_connect_function()
 *
 * Return available DB connect function
 */

function ezin_get_db_connect_function() /* {{{ */
{
    static $connect_fnc = null;
    if (is_null($connect_fnc)) {
        static $connect_functions = array(
                'mysqli_connect',
                );
        foreach ($connect_functions as $fnc) {
            if (function_exists($fnc)) {
                $connect_fnc = $fnc;
                break;
            }
        }
    }
    return $connect_fnc;
} /* }}} */

/*
 * ezin_open_db_connection()
 *
 * Opens database connection.
 */

function ezin_open_db_connection() /* {{{ */
{
    global $ezin_sys;
    global $ezin_db_handle;

    if (is_resource($ezin_db_handle)) {
        return $ezin_db_handle;
    }

    $connect_fnc = ezin_get_db_connect_function();
    if (empty($connect_fnc)) {
        ezin_db_error('ezin_open_db_connection()', 'no database connection function available');
    }

    $ezin_db_handle = $connect_fnc(
            $ezin_sys['db']['host'], $ezin_sys['db']['user'],
            $ezin_sys['db']['pass'], $ezin_sys['db']['name'],
            $ezin_sys['db']['port']);

    if ($ezin_db_handle == false)
        return false;

    if (mysqli_select_db($ezin_db_handle, $ezin_sys['db']['name']) == false)
        return false;

    return $ezin_db_handle;
} /* }}} */


/*
 * ezin_close_db_connection()
 *
 * Closes database connection.
 */

function ezin_close_db_connection() /* {{{ */
{
    global $ezin_db_handle;
    $ret = mysqli_close($ezin_db_handle);
    $ezin_db_handle = null;
    return $ret; // true if OK, false if failed
} /* }}} */


/*
 * ezin_check_db_handle()
 *
 * Check if databse connection is still alive.
 */

function ezin_check_db_handle() /* {{{ */
{
    global $ezin_db_handle;
    if (is_null($ezin_db_handle)) {
        ezin_open_db_connection() 
            or ezin_db_error('ezin_check_db_handle()', 'database server connection error');
    }
} /* }}} */

/*
 * ezin_set_db_encoding()
 *
 * Set-up data retrieval encoding
 */

function ezin_set_db_encoding($encoding = '') /* {{{ */
{
    global $ezin_db_handle;
    ezin_check_db_handle();
    $client_encoding = mysqli_character_set_name($ezin_db_handle);
    preg_match('/[0-9]*\.[0-9]*\.[0-9]/', mysqli_get_client_info($ezin_db_handle), $versions_matches);
    $version = (is_array($versions_matches) && isset($versions_matches[0])) ? $versions_matches[0] : null;
    if (! strcasecmp($client_encoding, 'latin1')
        && (is_null($version)
            || version_compare($version, '5.0.0', '<=')))
    {
        return; // do rather nothing when LATIN1 (old MySQL 3.23 tables)
    }
    static $enc_map = array(
            'UTF8'   => array('utf', 'utf8'),
            'CP1250' => array('windows', 'windows1250', 'cp1250'),
            'LATIN2' => array('latin', 'latin2', 'iso88592'),
            ''       => array()
            );
    if (strlen($encoding) <= 0) {
        global $ezin_cfg;
        $encoding = @$ezin_cfg['encoding'];
    }
    $encoding = trim($encoding);
    $encoding = strtolower($encoding);
    $encoding = preg_replace('|[_-]|', '', $encoding);
    foreach ($enc_map as $mysqli_encoding => $ar) {
        if (in_array($encoding, $ar)) {
            break;
        }
    }
    if (strlen($mysqli_encoding) > 0) {
        mysqli_query($ezin_db_handle, 'SET NAMES '.$mysqli_encoding);
        mysqli_query($ezin_db_handle, 'SET COLLATION_CONNECTION='.$mysqli_encoding.'_GENERAL_CI');
    }
    return;
} /* }}} */

/*
 * ezin_db_data_escape()
 *
 * Escape database data
 */

function ezin_db_escape($data) /* {{{ */
{
    static $escape_function = null;
    if (is_null($escape_function)) {
        // XXX: TODO: mysqli_escape_string needs resource link as a first param,
        // without this param it will return empty value
        //   -- Nepto [2017-11-07]
        foreach (array('addslashes', 'mysqli_escape_string') as $fnc_name) {
            if (function_exists($fnc_name)) {
                $escape_function = $fnc_name;
                break;
            }
        }
        if (is_null($escape_function)) {
            die('ezin_db_escape(): unable to set DB escape function');
        }
    }
    if (is_array($data)) {
        foreach (array_keys($data) as $key) {
            $data[$key] = $escape_function($data[$key]);
        }
    } else {
        $data = $escape_function($data);
    }
    return $data;
} /* }}} */


/* Modeline for ViM {{{
 * vim: set ts=4:
 * vim600: fdm=marker fdl=0 fdc=0:
 * }}} */

?>

Platon Group <platon@platon.org> http://platon.org/
Copyright © 2002-2006 Platon Group
Site powered by Metafox CMS
Go to Top