125 lines
3.2 KiB
PHP
125 lines
3.2 KiB
PHP
<?php
|
|
/*
|
|
************************************************************************
|
|
**
|
|
** Copyright (c) 2012..2013 by
|
|
** Core|Vision B.V.
|
|
** Cereslaan 10b
|
|
** 5384 VT Heesch
|
|
** The Netherlands
|
|
**
|
|
** All Rights Reserved
|
|
**
|
|
************************************************************************
|
|
*/
|
|
/*
|
|
************************************************************************
|
|
**
|
|
** Project name: Dual Inventive: MTinfo Support Scripts
|
|
** Filename: support.inc.php
|
|
** Author: Jack Weeland
|
|
** Date: October 24, 2012
|
|
** File version: $Revision: 1.3 $
|
|
** $Date: 2013/12/19 14:40:31 $
|
|
**
|
|
************************************************************************
|
|
*/
|
|
/*
|
|
************************************************************************
|
|
**
|
|
** Include file for the support functions
|
|
**
|
|
************************************************************************
|
|
*/
|
|
|
|
define('CONFIG_DIR', '/etc/di');
|
|
define('DBCONFIG_DIR', CONFIG_DIR . '/database');
|
|
|
|
function read_database($dir, $file)
|
|
{
|
|
$path = $dir . "/" . $file;
|
|
if( is_dir($path) ) return false;
|
|
if( ($fp = fopen($path, "r")) === false ) return false;
|
|
|
|
// read the file
|
|
$database = array();
|
|
$database['file'] = $file;
|
|
while( ($line = fgets($fp, 256)) !== false ) {
|
|
// delete comments
|
|
$line = strtok($line, "#");
|
|
if( ($n = strpos($line, "=")) !== false ) {
|
|
$key = trim(substr($line, 0, $n));
|
|
$value = trim(substr($line, $n + 1));
|
|
|
|
if( $key == "type" && ($n = strpos($value, ",")) !== false ) {
|
|
switch( ($database[$key] = substr($value, 0, $n)) ) {
|
|
case 'archive':
|
|
$database['main'] = substr($value, $n + 1);
|
|
break;
|
|
}
|
|
}
|
|
else $database[$key] = $value;
|
|
}
|
|
}
|
|
|
|
// done
|
|
fclose($fp);
|
|
if( !$database['name'] ) {
|
|
$database['name'] = "";
|
|
if( $database['host'] ) {
|
|
$database['name'] .= $database['host'];
|
|
if( $database['port'] ) $database['name'] .= ":" . $database['port'];
|
|
$database['name'] .= ":";
|
|
}
|
|
$database['name'] .= $database['database'];
|
|
}
|
|
return $database;
|
|
}
|
|
|
|
function read_databases($dir)
|
|
{
|
|
function sort_by_name($a, $b)
|
|
{
|
|
return strcmp($a['name'], $b['name']);
|
|
}
|
|
|
|
$databases = array();
|
|
// gather all files from /etc/di/database
|
|
$dirlist = opendir($dir);
|
|
while( ($file = readdir($dirlist)) !== false )
|
|
if( ($database = read_database($dir, $file)) !== false )
|
|
array_push($databases, $database);
|
|
closedir($dirlist);
|
|
|
|
usort($databases, "sort_by_name");
|
|
return $databases;
|
|
}
|
|
|
|
function mysql_run($query, $db_handle) {
|
|
$result = mysql_query($query, $db_handle);
|
|
if( !$result ) {
|
|
error_log($_PHP_SELF . "db-error: " . mysql_error() . ", query " . $query);
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
// Print error message to the log and in the image
|
|
// To be called as "die im_print_error('Something is wrong');"
|
|
function im_print_error($im, $error_str)
|
|
{
|
|
// log error
|
|
error_log($_PHP_SELF . ": " . $error_str);
|
|
|
|
// print message
|
|
$dred = imagecolorallocate($im, 160, 0, 0);
|
|
imagestringalign($im, 5, imagesx($im) / 2, imagesy($im) / 2, $error_str, $dred, AL_CENTERED);
|
|
|
|
// finalize image
|
|
imagepng($im);
|
|
imagedestroy($im);
|
|
|
|
return $error_str;
|
|
}
|
|
|
|
?>
|