 <?php
if(!defined('APP_PATH')||!defined('I_PATH')){exit('Access Denied');}

class ld_map extends controllerBase
{
	function __construct(){
		parent::__construct();
		$this->a=$this->sysParams('a',1);
	}
	function cache_write($file, $string, $dir = '') {
	if(is_array($string)) $string = "<?php defined('IN_DESTOON') or exit('Access Denied'); return ".strip_nr(var_export($string, true))."; ?>";
	$file = $dir ? DT_CACHE.'/'.$dir.'/'.$file : DT_CACHE.'/'.$file;
	$strlen = file_put($file, $string);
	return $strlen;
}


	function index(){
		if($this->sysParams('run')==1){
			require(APP_PATH."/config.php");
			$map=$iConfig['map'];
			$configfile='config.php';
			$fp_tp=@fopen($configfile,"r");
			$fp_txt=@fread($fp_tp,filesize($configfile));
			@fclose($fp_tp);
			syDB("sysconfig")->update(array('code'=>set_map),array('value'=>1));//1代表已经设置完公司地图
			foreach($map as $k=>$v)
			{
				$fp_txt=preg_replace("/'".$k."' => '.*?',/","'".$k."' => '".$this->sysParams($k,1)."',",$fp_txt);
			}
			$fpt_tpl=@fopen($configfile,"w");
			@fwrite($fpt_tpl,$fp_txt);
			@fclose($fpt_tpl);
			message_a("公司地图修改成功",'?c=ld_map');
		}
		$this->toptxt='公司地图';
		$this->d = func_get_iglobal(array('map'));
		$this->display("map.html");
	}
	
	private function checkdir($basedir){
		if (is_file($basedir)) {
			$this->checkBOM($basedir);
		}else{
			if ($dh = opendir($basedir)) {
				while (($file = readdir($dh)) !== false) {
					if ($file != '.' && $file != '..'){
						if (!is_dir($basedir."/".$file)) {
							$this->checkBOM("$basedir/$file");
						}else{
							$dirname = $basedir.'/'.$file;
							$this->checkdir($dirname);
						}
					}
				}
			closedir($dh);
			}
		}
	}
	private function checkBOM ($filename) {
		$contents = file_get_contents($filename);
		$charset[1] = substr($contents, 0, 1); 
		$charset[2] = substr($contents, 1, 1); 
		$charset[3] = substr($contents, 2, 1); 
		if (ord($charset[1]) == 239 && ord($charset[2]) == 187 && ord($charset[3]) == 191) {
			$rest = substr($contents, 3);
			$this->rewr ($filename, $rest);
		} 
	}
	private function rewr ($filename, $data) {
		$filenum = fopen($filename, "w");
		flock($filenum, LOCK_EX);
		fwrite($filenum, $data);
		fclose($filenum);
	}
}	