<?php

if(!(is_readable(realpath(dirname(__FILE__)).'/settings.inc'))) {
    die('Error - Could not open or read file include/settings.inc');
} else {
    include_once(realpath(dirname(__FILE__)).'/settings.inc');
}

function getVarSize($var)
{
 if (is_string($var)) {return strlen($var);}
 elseif (is_array($var))
 {
  $size = 0;
  foreach ($var as $k=>$v) {$size += strlen($k)+getVarSize($v);}
  return $size;
 }
 else {return 1;}
}


function logmessage($messalge){
	$file = realpath(dirname(__FILE__).'/../').'/log/portal.log';
	if (is_readable($file)) {
		$filear = file($file);
		array_push($filear, date(GetSetting('portal','dataFormat','m.d.y H:i:s')." : ").$messalge.PHP_EOL);
		$n = 0 ;
		$size = GetSetting('portal','log.size',1024) * 1024;
		if ($size>0)//9230
    {
      while(getVarSize($filear) > $size) {
			  unset($filear[$n]);
		  	  $n = $n + 1;
		  }
    }
		file_put_contents($file, $filear);
		unset($filear);
	} else {
	    $f = fopen($file, 'w');
	    fwrite($f, date(GetSetting('portal','dataFormat','m.d.y H:i:s')." : ").$messalge.PHP_EOL);
    	fclose($f);
	}

	if (isset($_REQUEST['DEBUG']) && ($_REQUEST['DEBUG'] == 'TRUE')){
	    echo $messalge."<br/>";
	}
}
?>