<?php
if (!function_exists('_debugecho'))
{
function _debugecho($text)
{
    static $loghandle;
    
    if (!isset($loghandle))
    {
        $logname = (isset($GLOBALS['base_debug_log_name']))?$GLOBALS['base_debug_log_name']:$GLOBALS['QNS_BASEDIR'].DIRECTORY_SEPARATOR.'debug'.DIRECTORY_SEPARATOR.'debug.txt';

        $loghandle = fopen($logname, 'a');
	if ($loghandle === false)
	{
            unset($loghandle);
	}
    }
    if (!isset($loghandle))
    {
	echo '<!-- ['.date('m/d/y H:i:s').substr($usec, 1, 5).'] '.$text.' -->'."\n";
    }
    else
    {
	list($usec, $sec) = explode(' ', microtime());
	fwrite($loghandle, '['.date('m/d/y H:i:s').substr($usec, 1, 5).'] '.$text."\n");
    }
}
}
function get_mysql_password($user)
{
    static $cache = array();
	
    $result = false;
	
    if (isset($cache[$user]))
    {
        $result = $cache[$user];
    }
    else
    {
        $fhndl = fopen(__DIR__.DIRECTORY_SEPARATOR.'passwd', 'r');
        if ($fhndl)
        {
//          _debugecho('opened password file '.__DIR__.DIRECTORY_SEPARATOR.'passwd');
//          _debugecho('searching for '.$user);
            while (!feof($fhndl))
            {
                $line = trim(fgets($fhndl));
//              _debugecho('line: '.$line);
                list($username, $password) = explode('=', $line, 2);
//              _debugecho('name: '.$username.' password: '.$password.' - ', '');
                if ($username == $user)
                {
//                  _debugecho('found');
                    $result=$password;
                }
                else
                {
//                  _debugecho('not found');
                }
                $cache[$username] = $password;
            }
            fclose($fhndl);
        }
    }
    return $result;	
}
 ?>
