<?php
if (!defined('ROOT')) exit('Can\'t Access !'); 
class config{
   function get($key) {
	 $config=include(PATH.'include/config.php');
	 if(isset($config[$key])) {
	   return $config[$key];
	 }else{
	   return false;
	 }
   } 
  
   function input($name,$value=null,$values=null,$option=null){
       return  "<input type=\"text\" name=\"$name\" id=\"$name\" value=\"$value\"  $option/>";
   }
   
   function data($name,$value=null,$values=null,$option=null){
	   $format = $values ? ",dateFmt:'yyyy-MM-dd HH:mm:ss'" : "";
       return  "<input class=\"Wdate $option\" type=\"text\" name=\"$name\" id=\"$name\" value=\"$value\"  onFocus=\"WdatePicker({isShowClear:false,readOnly:true{$format}})\" />";
   }
   
   function datas($name,$value=null,$values=null,$option=null){
	   $value = explode(',',$value);
	   $return = "<input class=\"Wdate\" type=\"text\" name=\"$name\" id=\"$name\" value=\"{$value[0]}\"  ";
	   $return .= 'onclick="var '.$name.'t=$dp.$(\''.$name.'t\');WdatePicker({onpicked:function(){'.$name.'t.focus();}})"/>';
	   $return .= "&nbsp;—&nbsp;<input class=\"Wdate\" type=\"text\" name=\"{$name}t\" id=\"{$name}t\" value=\"{$value[1]}\" ";
	   $return .= ' onFocus="WdatePicker({minDate:\'#F{$dp.$D(\\\''.$name.'\\\')}\'})">';
       return  $return;
   }

   function password($name,$value=null,$values=null,$option=null) {
       return  "<input type=\"password\" name=\"$name\" id=\"$name\" value=\"$value\"  $option/>";
   }

   function textarea($name,$value=null,$values=null,$option=null) {
       return  "<textarea name=\"$name\" id=\"$name\" $option>$value</textarea>";
   }

   function image($name,$value=null,$values=null,$option=null){
	   $return = "&nbsp;&nbsp;<a href=\"#\" style=\"color:#F00\" onmousemove=\"showpup('<img src=".$value." />')\">".$value."</a>";
	   $return .= '&nbsp;&nbsp;&nbsp;&nbsp;';
	   $return .= "<input type=\"file\" name=\"imgFile\" id=\"imgFile\" />";
       return  $return;
   }
   
   static function editor($name,$value=null,$values=null,$option='') {
	   @preg_match_all("%\<(.+?)@(.+?)\>%s",$option, $uf, PREG_PATTERN_ORDER);
	   $option = preg_replace("%\<(.+?)@(.+?)\>%s", "" ,$option);
	   if($option=='') $option = "style='width:600px; height:300px;'";
	   $ImgPath = 'act';
	   if($_GET['mod']=='member') $ImgPath = 'type';
	   $upload = Purl("?mod=tools&act=upload&ImgPath=".geturlval($ImgPath));
	   $filemanager = Purl("tools_filemanager");
	   $allowManager = $uf[2][0] ? "true" : "false";
	   $UploadJson = $uf[1][0] ? ",allowFileManager : {$allowManager},imageUploadJson:'{$upload}',fileManagerJson:'{$filemanager}'" : ",allowUpload : false";
	   $item = ",items:['source','|', 'justifyleft', 'justifycenter', 'justifyright','justifyfull', 'insertorderedlist', 'insertunorderedlist', '|', 'fontname', 'fontsize', '|', 'textcolor', 'bgcolor', 'bold','italic', 'underline', 'removeformat', '|', 'image', 'advtable', 'hr','link', 'unlink']";
	   $item = $values ? "" : $item;
	   $kindeditor = "<script>KE.show({id:'{$name}'{$UploadJson}{$item}});</script>";
       return  "<div class='nop'>".$kindeditor."<textarea name=\"$name\" id=\"$name\" $option>$value</textarea></div>";
   }

   function select($name,$value=null,$values=null,$option=null) {
       $select = "<select id=\"$name\" name=\"$name\" $option>";
       foreach($values as $val){
		   $selected = $val[value]==$value ? 'selected ' : '';
           $select.="<option value=\"$val[value]\" $selected>$val[title]</option>"; 
	   }           
       $select.="</select>";
       return $select;
   }

   function radio($name,$value=null,$values=null,$option=null) {
	   $radio = '';
       foreach($values as $val){
		   $checked = $val[value]==$value ? 'checked="checked" ' : '';
           $radio .= "<input name=\"$name\" type=\"radio\" id=\"$name\" value=\"$val[value]\" $checked $option/>&nbsp;".$val[title]."&nbsp;&nbsp;";
	   }
       return $radio;
   }
   function checkbox($name,$value=null,$values=null,$option=null) {
	   $checkbox = '';
	   $value = explode(',',$value);
       foreach($values as $val){
		   $checked = in_array($val[value],$value) ? 'checked="checked" ' : '';
           $checkbox .= "<span><input type=\"checkbox\" name=\"{$name}[]\" id=\"$name\" value=\"$val[value]\" $checked $option/>".$val[title]."</span>";
	   }
       return $checkbox;
   }


   function hidden($name,$value=null,$values=null,$option=null){
       return "<input type=\"hidden\" name=\"$name\" id=\"$name\" value=\"$value\"/>";
   }


   function submit($name,$value=null,$values=null,$option=null) {	   
       return config::stopoutenable()."\n<input type=\"submit\" name=\"$name\" id=\"$name\" value=\"$value\" $option>";
   }
   
   function form($name,$value,$type,$values='',$option='') {
	   $values = config::values($values);
       return config::$type($name,$value,$values,$option);
   }
   
   function stopoutenable(){
	   $_SESSION['stopoutenable'] = md5(substr(uniqid(rand()), -6)); 
	   return "<input type=\"hidden\" name=\"stopoutenable\" id=\"stopoutenable\" value=\"{$_SESSION['stopoutenable']}\"/>";
   }
   
   function seccode($v=''){
	   $pre = config::get('rewrite') ? '?' : '&';
       return "<img src=\"".Purl("tools_seccode")."\" width=\"60\" height=\"20\" onclick=\"this.src=this.src + '".$pre."' + Math.floor(Math.random()*1000+1);\"  style=\"cursor:pointer;\"  />";
   }
   
   function values($values){
      if(preg_match_all('%\[(.*?)\]%', $values, $rs, PREG_PATTERN_ORDER)==0) return $values;
	  $i = 0;
      foreach($rs[1] as $value) {
         if(preg_match_all('%\<(.*?)\>%',$value,$r, PREG_PATTERN_ORDER)==0) return $values;
		 $val[$i]['title'] = $r[1][1];
		 $val[$i]['value'] = $r[1][0]; 
		 $i++;
      }
	  return $val;
   }
   static function configs(){
	 return @file_get_contents(PATH."./include/config.php");	   
   }
   
   static function update($arr){
	 $configs = config::configs();
	 foreach($arr as $key => $value) {
       if(is_array($value)) $value = implode(',',$value);
       $configs = preg_replace("%(\"$key\"=>)\".*?\"(,\s*//)%s", "$1\"$value\"$2" ,$configs);
     }
     @file_put_contents(PATH.'./include/config.php',$configs);
   }
   
   
}
?>