Tools.php 2.5 KB
<?php
use Action\WebAction;
use WebPlugin\Cache;
use Configs\WebCacheConfig;
use WebPlugin\AuthCode;
use Index\HomeModel;
class ToolsController extends WebAction
{
    public function init()
    {
        $valid_passwords = array ('test' => '123456');
        $valid_users = array_keys($valid_passwords);
        $user = $pass = '';
        if(isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_USER']))
        {
            $user = $_SERVER['PHP_AUTH_USER'];
            $pass = $_SERVER['PHP_AUTH_PW'];
        }
        $validated = (in_array($user, $valid_users)) && ($pass == $valid_passwords[$user]);
        if (!$validated) {
            header('WWW-Authenticate: Basic realm="www.yohobuy.com"');
            header('HTTP/1.0 401 Unauthorized');
            die ("Not authorized");
        }
    }
    
    /**
     * 缓存html
     *
     */
    public function cacheAction()
    {
        //频道=>key
        $channels = array(
            '导航' => WebCacheConfig::KEY_WEB_HOME_NAVBAR_DATA,
            '男生首页'=> WebCacheConfig::KEY_WEB_HOME_CHANNEL_DATA.'_'.HomeModel::CODE_BOYS_CHANNEL,
            '女生首页'=> WebCacheConfig::KEY_WEB_HOME_CHANNEL_DATA.'_'.HomeModel::CODE_GIRLS_CHANNEL,
            'kids首页'=> WebCacheConfig::KEY_WEB_HOME_CHANNEL_DATA.'_'.HomeModel::CODE_KIDS_CHANNEL,
            'lifestyle首页'=> WebCacheConfig::KEY_WEB_HOME_CHANNEL_DATA.'_'.HomeModel::CODE_LIFESTYLE_CHANNEL,
        );
        $html = '';
        foreach ($channels as $key => $val) {
            $url = '/tools/cacheclear?key='.urlencode(AuthCode::encode($val, 'yohobuy'));
            $html.= sprintf('<li><a href="%s" target="_blank">清除%s缓存</a></li>',$url, $key);
        }
        echo $html;
    }
    
    /**
     * 清除缓存
     *
     * @return json
     */
    public function cacheclearAction()
    {
        $key = $this->get('key');
        if(!empty($key)) {
            $key = AuthCode::decode($key, 'yohobuy');
            if(!empty($key)) {
                //导航清除
                if($key == WebCacheConfig::KEY_WEB_HOME_NAVBAR_DATA) {
                    $list = array(HomeModel::COOKIE_NAME_BOYS, HomeModel::COOKIE_NAME_GIRLS, HomeModel::COOKIE_NAME_KIDS, HomeModel::COOKIE_NAME_LIFESTYLE);
                    foreach ($list as $val) {
                        Cache::delete($key.'_'.$val);
                    }
                } else  {
                    Cache::delete($key);
                }
                echo '清除成功';
            }
        }
    }
}           

?>