<?php

class settingModel extends Model
{

    /**
     * 获取配置信息写入缓存
     */
    public function setting_cache() {
        $setting = array();
        $res = $this->getField('name,data');
        foreach ($res as $key=>$val) {			
			if($this->is_serialized($val)){
			$val = $this->mb_unserialize($val);	
			$setting['ftx_'.$key] = unserialize($val) ? unserialize($val) : $val;
			}else{
			$setting['ftx_'.$key] = $val;	
			}
            
        }
        S('setting', $setting);
        return $setting;
    }

    /**
     * 后台有更新则删除缓存
     */
    protected function _before_write(&$data) {
        S('setting', NULL);
    }
	protected function mb_unserialize($str) {
        return preg_replace_callback('#s:(\d+):"(.*?)";#s',function($match){return 's:'.strlen($match[2]).':"'.$match[2].'";';},$str);
    }
	protected function is_serialized( $data ) {
        $data = trim( $data );
        if ( 'N;' == $data )
         return true;
        if ( !preg_match( '/^([adObis]):/', $data, $badions ) )
         return false;
        switch ( $badions[1] ) {
         case 'a' :
         case 'O' :
         case 's' :
             if ( preg_match( "/^{$badions[1]}:[0-9]+:.*[;}]\$/s", $data ) )
                 return true;
             break;
         case 'b' :
         case 'i' :
         case 'd' :
             if ( preg_match( "/^{$badions[1]}:[0-9.E-]+;\$/", $data ) )
                 return true;
             break;
     }
     return false;
    }
}