<?php

class cacheAction extends BackendAction
{
    public function _initialize() {
        parent::_initialize();
		
    }	
    public function index() {
        $this->display();
    }
	public function html() {
        $config_file = CONF_PATH . 'index/config.php';
        $config = include $config_file;
        if (IS_POST) {            
            $html_time = $this->_post('html_time', 'trim');
			$status = $this->_post('status', 'trim');
			if($status==1){
			$status = true;	
			}else{
			$status = false;	
			}
            $html_suffix = $this->_post('html_suffix', 'trim');
			$html_rules = $this->_post('html_rules', 'trim');
			$rules = explode("\n", $html_rules);
			foreach ($rules as $k => $v){
				$v=str_replace("\n","",$v); 
				$v=str_replace("\r\n","",$v); 
				$v=preg_replace("/\s/","",$v); 
				$v = str_replace(PHP_EOL, '', $v);
				$vs = str_replace(':', '/', $v);
				$cc[$v][0] = $vs.'/{$_SERVER.REQUEST_URI|md5}';
			}
            $new_config = array(                
                'HTML_CACHE_TIME' => $html_time,
                'HTML_FILE_SUFFIX' => $html_suffix,
				'HTML_CACHE_ON' => $status,
				'HTML_CACHE_RULES' => $cc
            );
            if ($this->update_config($new_config, $config_file)) {
                $this->success(L('operation_success'));
            } else {
                $this->error(L('file_no_authority'));
            }
        } else {			
			$list = $config['HTML_CACHE_RULES'];
			foreach ($list as $key => $vol){				
				$ss[] = $key;
			}
			foreach ($ss as $k => $v){
				$cc[$k] = $v;
			}
			$config['rules'] = implode("\n", $cc);
            $this->assign('config', $config);
            $this->display();
        }        
    }
    public function clear() {
        $type = $this->_get('type','trim');
        $obj_dir = new Dir;		
		
        switch ($type) {
            case 'field':
                is_dir(DATA_PATH . '_fields/') && $obj_dir->del(DATA_PATH . '_fields/');
                break;
            case 'tpl':
                is_dir(CACHE_PATH) && $obj_dir->delDir(CACHE_PATH);
                break;
            case 'data':
                is_dir(DATA_PATH) && $obj_dir->del(DATA_PATH);
                is_dir(TEMP_PATH) && $obj_dir->delDir(TEMP_PATH);
                break;
            case 'runtime':
                @unlink(RUNTIME_FILE);
                break;
            case 'logs':
                is_dir(LOG_PATH) && $obj_dir->delDir(LOG_PATH);
                break; 			
			case 'html':
                is_dir(HTML_PATH) && $obj_dir->del(HTML_PATH);
                break;		
			case 'memcache':
			    $mem = new Memcache;
                $mem->connect('127.0.0.1', 11211) or die ('Could not connect');
                $mem->flush();
				$mem->close();
                break;	
        }
        $this->ajaxReturn(1);
    }

    public function qclear() {
        $obj_dir = new Dir;
        is_dir(DATA_PATH . '_fields/') && $obj_dir->del(DATA_PATH . '_fields/');
        is_dir(CACHE_PATH) && $obj_dir->delDir(CACHE_PATH);
        is_dir(DATA_PATH) && $obj_dir->del(DATA_PATH);
        is_dir(TEMP_PATH) && $obj_dir->delDir(TEMP_PATH);			
        is_dir(LOG_PATH) && $obj_dir->delDir(LOG_PATH); 
		//is_dir(HTML_PATH) && $obj_dir->del(HTML_PATH);
        @unlink(RUNTIME_FILE);
        $this->ajaxReturn(1, L('clear_success'));
    }
}