<?php
/**
 * Filename:  includes/misc.inc.php
 * Function:  Miscellaneous functions file for phpDivingLog.
 * Last Modified: 2006-08-21
 * /***************************************************************************
 * 
 * phpDivingLog
 * Copyright (C) 2006 Lloyd Borrett - http://www.borrett.id.au
 * 
 * Adapted from code by Olaf van Zandwijk - http://enschede.vanzandwijk.net
 * 
 * For use with Diving Log by Sven Knoch - http://www.divinglog.de
 * 
 * This file and all dependant and otherwise related files are part of phpDivingLog.
 * 
 * phpDivingLog is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * phpDivingLog is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * /**************************************************************************
 */

// HTML on screen function
function htmlentities_array($arr = array())
{
    $rs = array();
    while (list($key, $val) = each($arr)) {
        if (is_array($val)) {
            $rs[$key] = htmlentities_array($val);
        } else {
            $rs[$key] = htmlentities($val, ENT_QUOTES);
        } 
    } 
    return $rs;
} 

// include_once config file
$config_filename = "includes/config.inc.php";
if (!file_exists($config_filename)) {
    print "<p>Configuration file '" . $config_filename . "' not found.</p>";
    exit;
} 
$_config = array();
include_once ($config_filename);

// Get the language values

// use english if it is not set in the configuration file
if (!isset($_config['language'])) {
    $_config['language'] = "english"; // if not set, get the english language file.
} 

// first get the default english values
// so that we'll have values for anything not in the specified language file
if ($_config['language'] != "english") {
    if (!file_exists('includes/language/english.inc.php')) {
        print "<p>Language file includes/language/english.inc.php not found.</p>";
        exit;
    } 
    include ('includes/language/english.inc.php');
} 

// include the specified language file
$language_filename = "includes/language/" . $_config['language'] . ".inc.php";
if (!file_exists($language_filename)) {
    print "<p>Language file '" . $language_filename . "' not found.</p>";
    exit;
} 
$_lang = array();
include ($language_filename);

// Convert applicable characters in the language file to entities
$_lang = htmlentities_array($_lang);

function sql_file($filename)
{
    /* Returns the contents of an SQL statement file as a string. $filename is  */
    /* the name of the SQL-file to be parsed. Variables defined under 'global'  */
    /* may be used in the sql-scripts and will be replaced by their values!     */
    /* The globals term should include all the variables which are used in the  */
    /* sql-script, so the variables are parsed correctly			    */

    global $_config;
    $sqlpath = $_config['sqlpath'];

    global $globals;
    $debug = false;

    $location = $sqlpath . $filename;
    if (!file_exists($location)) {
        die ("SQL file '" . $location . "' not found !");
    } 

    if ($location) {
        $contents = implode('', file($location));
        eval ("\$contents = \"$contents\";"); 
        // Support the "'" character from a HTTP GET variabele
        $contents = str_replace ("\'", "'", $contents);

        /* Handles debug-information: if debugging is enabled by setting	*/
        /* $debug =1 in the script, thiswrites debug output.			*/

        if ($debug) {
            echo "<hr>\n<b>" . $location . ":</b><br>\n<br>\n";
            echo nl2br(htmlentities($contents)) . "<br>\n<hr>\n";
        } 

        return $contents;
    } else {
        return false;
    } 
} 

function parse_mysql_query($filename)
{
    global $_config;
    $username = $_config['database_username'];
    $password = $_config['database_password'];
    $server = $_config['database_server'];
    $db = $_config['database_db'];

    $query = sql_file($filename);
    if ($query) {
        $connection = mysql_connect($server, $username, $password);
        mysql_select_db($db, $connection);

        $server_query = mysql_query($query, $connection);
        if (mysql_errno() && $debug) {
            echo "<hr>\n<b>MySQL error " . mysql_errno() . ": " . mysql_error() . "\n:</b><br>\n<hr>\n";
        } 

        for($i = 0; $query_output = mysql_fetch_assoc($server_query); $i++) {
            while (list($key, $val) = each($query_output)) {
                if (is_string($val)) {
                    $val = utf8_encode($val);
                    $query_output[$key] = $val;
                } 
            } 
            $result[$i] = $query_output;
        } 
    } 
    return $result;
} 

function check_number($number)
{
    // Only allow the characters 0 to 9.
    // Return nothing otherwise.
    if (!$number) {
        $get = "";
    } else {
        if (ereg('[^0-9]', $number)) {
            $get = "";
        } else {
            if ($number == 0) {
                $get = "";
            } else {
                $get = $number;
            } 
        } 
    } 
    return $get;
} 

function MetreToFeet($value, $precision)
{
    return round(($value * 3.2808399), $precision);
} 

function BarToPsi($value, $precision)
{
    return round(($value * 14.503774), $precision);
} 

function KgToLbs($value, $precision)
{
    return round(($value * 2.2046226), $precision);
} 

function CelsiusToFahrenh($value, $precision)
{
    return round((($value * (9 / 5)) + 32), $precision);
} 

function LitreToCuft($value, $precision)
{
    return round(($value * 7), $precision);
} 

?>
