<?
class libraries {
	function libraries() {}

	function load_lib($parent_lib) {
		global $CONF;

		$LIB_CONTENT_TAG	= '//DigiHex Systems\\';
		$CACHE_ENABLED		= ($CONF->DEV_MODE==0 || ($CONF->DEV_MODE==1 && $CONF->DEV_CACHED==1) ? 1:0);
		$CACHE_FILE			= CACHE.'/'.(ADMIN===true ? 'backend':'frontend').'_X.php';


		if($CACHE_ENABLED==false || !file_exists($CACHE_FILE)) {
			$LIST=$this->get_files(LIBS_PATH);
			if(ADMIN===true) {
				$LIST_ADMIN=$this->get_files(LIBS_PATH_ADMIN);
				$LIST=@array_merge($LIST,$LIST_ADMIN);
			}


			if(count($LIST)>0) foreach($LIST as $lib_dir => $lib_files) {
			/////////////////
				$lib_name=strtoupper(substr($lib_dir,1));

				if(count($lib_files)>0) {
					foreach($lib_files as $lib_file) {
						$start=$end=false;
						$lib_path=(ADMIN===true && substr($lib_file,0,2)=='__' ? LIBS_PATH_ADMIN : LIBS_PATH);
						$lib_content=fopen($lib_path.'/'.$lib_dir.'/'.$lib_file, "r");

						while(!feof($lib_content)) {
							$linia=fgets($lib_content, 2048);
							if(stristr($linia,$LIB_CONTENT_TAG) && $start==true) $end=true;
							if($start==true && $end==false) $lib_code.=$linia;
							if(stristr($linia,$LIB_CONTENT_TAG)) $start=true;
						}
						fclose($lib_content);
					}

					$lib_code='class '.$parent_lib.'_'.$lib_name.' {
'.$lib_code.'
}
$this->'.$lib_name.' = new '.$parent_lib.'_'.$lib_name.'();
$this->'.$lib_name.'->DIR = LIBS.\'/'.$lib_dir.'\';
$this->'.$lib_name.'->PATH = LIBS_PATH.\'/'.$lib_dir.'\';

';
$INITS.='$this->'.$lib_name.'->init();
';
					$LIB_CACHE.=$lib_code;
					$lib_code='';
				}
			/////////////////
			}

			file_put_contents($CACHE_FILE, '<?
if(!$ALLOW_INCLUDE) exit;

'.$LIB_CACHE.'

'.$INITS.'
?>');
			unset($LIB_CACHE);
			unset($INITS);
		}


		$ALLOW_INCLUDE=true;
		require_once($CACHE_FILE);
	}


	function get_files($libs_path) {
		if($lib_dir=opendir($libs_path)) {
			while(($dir=readdir($lib_dir))!=false) {
				if($dir!='.' && $dir!='..' && is_dir($libs_path.'/'.$dir) && substr($dir,0,1)=='_') {
					if($lib_files=opendir($libs_path.'/'.$dir)) {
						while(($file=readdir($lib_files))!=false) {
							if(stristr($file,'.php') && substr($file,0,1)=='_') {
								if(substr($file,0,2)=='__' && ADMIN!==true) continue;
								$list[$dir][] = $file;
							}
						}
						closedir($lib_files);
					}
				}
			}
			closedir($lib_dir);
		}
		return $list;
	}


}
$X=new libraries();
$X->CONF=$CONF;
$X->load_lib('X');


$sql=$X->DB->q(' SELECT klucz,wartosc FROM _front_conf ');
while($dane=$X->DB->fa($sql)) $X->SETT[$dane['klucz']] = stripslashes($dane['wartosc']);
$X->SETTINGS=$X->SETT;



//////////////////////////////////////////////////////////////////////
class log
{
	function add($text, $before_nl=0) {
		global $X;

		$log_file=ROOT_PATH.'/log.log';
		$before_text=str_repeat("\n",$before_nl);

		$f=fopen($log_file,'a');
		fwrite($f, $before_text."[".date('Y-m-d H:i:s')."]  ".$text."\n");
		fclose($f);

		$X->MAIL->send('error@digihex.pl','System Log',"[".date('Y-m-d H:i:s')."]  ".$text);
	}
}
$LOG=new log();
$X->LOG=$LOG;
?>