vivio-swift-cache-excludes.php 17.4 KB
Newer Older
Jordan Michaels's avatar
Jordan Michaels committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
<?php
class Vivio_Swift_Cache_Excludes
{
	
    function __construct()
    {
        global $vivio_swift_global;
    }

    function get_cookie_values()
    {
        global $vivio_swift_global;
        $arr_cookie_values = $vivio_swift_global->configs->get_value('vivio_swift_cache_exclude_cookie_values');
        if(!is_array($arr_cookie_values)){$arr_cookie_values = array();}
        return array_values($arr_cookie_values);
    }

    function add_cookie_value($val)
    {
        global $vivio_swift_global;
        $arr_cookie_values = $vivio_swift_global->configs->get_value('vivio_swift_cache_exclude_cookie_values');
        if(!is_array($arr_cookie_values)){$arr_cookie_values = array();}
        if(!in_array($val,$arr_cookie_values)){
            array_push($arr_cookie_values, $val);
            $vivio_swift_global->configs->set_value('vivio_swift_cache_exclude_cookie_values',array_values($arr_cookie_values));
            $vivio_swift_global->configs->save_config();
        }
        Vivio_Swift_Utility_Htaccess::write_to_htaccess();
    }

    function remove_cookie_value($val)
    {
        global $vivio_swift_global;
        $arr_cookie_values = $vivio_swift_global->configs->get_value('vivio_swift_cache_exclude_cookie_values');
        if(!is_array($arr_cookie_values)){$arr_cookie_values = array();}
        foreach ($arr_cookie_values as $i => $value) {
37
            if ($value==$val){unset($arr_cookie_values[$i]);}
Jordan Michaels's avatar
Jordan Michaels committed
38 39 40 41 42 43 44 45 46 47 48
        }
        $vivio_swift_global->configs->set_value('vivio_swift_cache_exclude_cookie_values',array_values($arr_cookie_values));
        $vivio_swift_global->configs->save_config();
        Vivio_Swift_Utility_Htaccess::write_to_htaccess();
    }

    //returns true if passed cookie value is listed in required cookie exclusions
    function cookie_check_required_value($val)
    {
        $required_cookie_exclusions = array("comment_author_", "wordpress_logged_in", "wp-postpass_");
        foreach ($required_cookie_exclusions as $i => $value) {
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
            if($value==$val){return true;}
        }
        return false;
    }

    function get_user_agent_values()
    {
        global $vivio_swift_global;
        $arr_user_agent_values = $vivio_swift_global->configs->get_value('vivio_swift_cache_exclude_user_agent_values');
        if(!is_array($arr_user_agent_values)){$arr_user_agent_values = array();}
        return array_values($arr_user_agent_values);
    }

    function add_user_agent_value($val)
    {
        global $vivio_swift_global;
        $arr_user_agent_values = $vivio_swift_global->configs->get_value('vivio_swift_cache_exclude_user_agent_values');
        if(!is_array($arr_user_agent_values)){$arr_user_agent_values = array();}
        if(!in_array($val,$arr_user_agent_values)){
            array_push($arr_user_agent_values, $val);
            $vivio_swift_global->configs->set_value('vivio_swift_cache_exclude_user_agent_values',array_values($arr_user_agent_values));
            $vivio_swift_global->configs->save_config();
        }
        Vivio_Swift_Utility_Htaccess::write_to_htaccess();
    }

    function remove_user_agent_value($val)
    {
        global $vivio_swift_global;
        $arr_user_agent_values = $vivio_swift_global->configs->get_value('vivio_swift_cache_exclude_user_agent_values');
        if(!is_array($arr_user_agent_values)){$arr_user_agent_values = array();}
        foreach ($arr_user_agent_values as $i => $value) {
            if ($value==$val){unset($arr_user_agent_values[$i]);}
        }
        $vivio_swift_global->configs->set_value('vivio_swift_cache_exclude_user_agent_values',array_values($arr_user_agent_values));
        $vivio_swift_global->configs->save_config();
        Vivio_Swift_Utility_Htaccess::write_to_htaccess();
    }

    //returns true if passed user_agent value is listed in required user_agent exclusions
    function user_agent_check_required_value($val)
    {
        $required_user_agent_exclusions = array(VIVIO_SWIFT_CACHE_BOT_NAME);
        foreach ($required_user_agent_exclusions as $i => $value) {
            if($value==$val){return true;}
Jordan Michaels's avatar
Jordan Michaels committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
        }
        return false;
    }

    function get_path_is_values()
    {
        global $vivio_swift_global;
        $arr_path_is_values = $vivio_swift_global->configs->get_value('vivio_swift_cache_exclude_path_is_values');
        if(!is_array($arr_path_is_values)){$arr_path_is_values = array();}
        return array_values($arr_path_is_values);
    }

    function add_path_is_value($val)
    {
        global $vivio_swift_global;
        $arr_path_is_values = $vivio_swift_global->configs->get_value('vivio_swift_cache_exclude_path_is_values');
        if(!is_array($arr_path_is_values)){$arr_path_is_values = array();}
        if(!in_array($val,$arr_path_is_values)){
            array_push($arr_path_is_values, $val);
            $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_Excludes::add_path_is_value() - saving $val to array: ".$val, 1);
            $vivio_swift_global->configs->set_value('vivio_swift_cache_exclude_path_is_values',array_values($arr_path_is_values));
            $vivio_swift_global->configs->save_config();
        }
        $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_Excludes::add_path_is_value() - writing .htaccess file...", 1);
        Vivio_Swift_Utility_Htaccess::write_to_htaccess();
    }

    function remove_path_is_value($val)
    {
        global $vivio_swift_global;
        $arr_path_is_values = $vivio_swift_global->configs->get_value('vivio_swift_cache_exclude_path_is_values');
        if(!is_array($arr_path_is_values)){$arr_path_is_values = array();}
        foreach ($arr_path_is_values as $i => $value) {
            $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_Excludes::remove_path_is_value() - value=val: ".$value."=".$val, 0);
            if ($value==$val){
                $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_Excludes::remove_path_is_value() - unset i: ".$i, 0);
                unset($arr_path_is_values[$i]);
            }
        }
        $vivio_swift_global->configs->set_value('vivio_swift_cache_exclude_path_is_values',array_values($arr_path_is_values));
        $vivio_swift_global->configs->save_config();
        Vivio_Swift_Utility_Htaccess::write_to_htaccess();
    }

    function get_path_ends_with_values()
    {
        global $vivio_swift_global;
        $arr_path_ends_with_values = $vivio_swift_global->configs->get_value('vivio_swift_cache_exclude_path_ends_with_values');
        if(!is_array($arr_path_ends_with_values)){$arr_path_ends_with_values = array();}
        return array_values($arr_path_ends_with_values);
    }

    function add_path_ends_with_value($val)
    {
        global $vivio_swift_global;
        $arr_path_ends_with_values = $vivio_swift_global->configs->get_value('vivio_swift_cache_exclude_path_ends_with_values');
        if(!is_array($arr_path_ends_with_values)){$arr_path_ends_with_values = array();}
        if(!in_array($val,$arr_path_ends_with_values)){
            array_push($arr_path_ends_with_values, $val);
            $vivio_swift_global->configs->set_value('vivio_swift_cache_exclude_path_ends_with_values',array_values($arr_path_ends_with_values));
            $vivio_swift_global->configs->save_config();
        }
        Vivio_Swift_Utility_Htaccess::write_to_htaccess();
    }

    function remove_path_ends_with_value($val)
    {
        global $vivio_swift_global;
        $arr_path_ends_with_values = $vivio_swift_global->configs->get_value('vivio_swift_cache_exclude_path_ends_with_values');
        if(!is_array($arr_path_ends_with_values)){$arr_path_ends_with_values = array();}
        foreach ($arr_path_ends_with_values as $i => $value) {
            $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_Excludes::remove_path_ends_with_value() - value=val: ".$value."=".$val, 0);
            if ($value==$val){
                $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_Excludes::remove_path_ends_with_value() - unset i: ".$i, 0);
                unset($arr_path_ends_with_values[$i]);
            }
        }
        $vivio_swift_global->configs->set_value('vivio_swift_cache_exclude_path_ends_with_values',array_values($arr_path_ends_with_values));
        $vivio_swift_global->configs->save_config();
        Vivio_Swift_Utility_Htaccess::write_to_htaccess();
    }

    function get_path_contains_values()
    {
        global $vivio_swift_global;
        $arr_path_contains_values = $vivio_swift_global->configs->get_value('vivio_swift_cache_exclude_path_contains_values');
        if(!is_array($arr_path_contains_values)){$arr_path_contains_values = array();}
        return array_values($arr_path_contains_values);
    }

    function add_path_contains_value($val)
    {
        global $vivio_swift_global;
        $arr_path_contains_values = $vivio_swift_global->configs->get_value('vivio_swift_cache_exclude_path_contains_values');
        if(!is_array($arr_path_contains_values)){$arr_path_contains_values = array();}
        if(!in_array($val,$arr_path_contains_values)){
            array_push($arr_path_contains_values, $val);
            $vivio_swift_global->configs->set_value('vivio_swift_cache_exclude_path_contains_values',array_values($arr_path_contains_values));
            $vivio_swift_global->configs->save_config();
        }
        Vivio_Swift_Utility_Htaccess::write_to_htaccess();
    }

    function remove_path_contains_value($val)
    {
        global $vivio_swift_global;
        $arr_path_contains_values = $vivio_swift_global->configs->get_value('vivio_swift_cache_exclude_path_contains_values');
        if(!is_array($arr_path_contains_values)){$arr_path_contains_values = array();}
        foreach ($arr_path_contains_values as $i => $value) {
            $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_Excludes::remove_path_contains_value() - value=val: ".$value."=".$val, 0);
            if ($value==$val){
                $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_Excludes::remove_path_contains_value() - unset i: ".$i, 0);
                unset($arr_path_contains_values[$i]);
            }
        }
        $vivio_swift_global->configs->set_value('vivio_swift_cache_exclude_path_contains_values',array_values($arr_path_contains_values));
        $vivio_swift_global->configs->save_config();
        Vivio_Swift_Utility_Htaccess::write_to_htaccess();
    }

214
    // returns error message is condition is hit. otherwise simply returns false.
Jordan Michaels's avatar
Jordan Michaels committed
215 216 217 218 219 220 221 222
    function process_cache_excludes($url='',$force=0)
    {
        global $vivio_swift_global;

        if(!$url){$url=$_SERVER[REQUEST_URI];}
        $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_Excludes::process_cache_excludes() - processing excludes for: ".$url, 1);

        // user agent excludes
223
        /*
Jordan Michaels's avatar
Jordan Michaels committed
224 225 226 227
        if(strpos($_SERVER['HTTP_USER_AGENT'],VIVIO_SWIFT_CACHE_BOT_NAME)!==false){
            $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_Excludes::process_cache_excludes() - bot rule hit [SKIPPING]", 2);
            return false;
        }
228
        */
Jordan Michaels's avatar
Jordan Michaels committed
229 230 231 232
        
        // POST requests exclude
        if(!is_admin() && $_SERVER['REQUEST_METHOD']=="POST"){
            $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_Excludes::process_cache_excludes() - HTTP POST rule hit [SKIPPING]", 2);
233
            return "HTTP POST rule hit [SKIPPING]";
Jordan Michaels's avatar
Jordan Michaels committed
234 235 236 237 238 239 240
        }

        // query string excludes
        if ($vivio_swift_global->configs->get_value('vivio_swift_cache_exclude_query_strings')=='1')
        {
            if(parse_url($url, PHP_URL_QUERY)){
                $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_Excludes::process_cache_excludes() - URL query stings rule hit [SKIPPING]", 2);
241
                return "URL query stings rule hit [SKIPPING]";
Jordan Michaels's avatar
Jordan Michaels committed
242 243 244 245 246 247 248 249 250 251 252 253 254 255
            }
        }
        
        // cookie excludes
        // forced refreshes do not need to process cookies
        if(!$force){
            $arr_cookie_values=Vivio_Swift_Cache_Excludes::get_cookie_values();
            foreach ($arr_cookie_values as $i => $value) {
                foreach ((array)$_COOKIE as $cookie_key => $cookie_value){
                    $searchval='/'.$value.'/i';
                    $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_Excludes::process_cache_excludes() - Testing Cookie: ".$cookie_key, 0);
                    $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_Excludes::process_cache_excludes() - Against Cookie Exclude: ".$value, 0);
                    if (preg_match($searchval,$cookie_key)){
                        $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_Excludes::process_cache_excludes() - Exclude Cookie rule hit for cookie contains value: '".$value."' [SKIPPING]", 2);
256
                        return "Exclude Cookie rule hit for cookie contains value: '".$value."' [SKIPPING]";
Jordan Michaels's avatar
Jordan Michaels committed
257 258 259 260 261 262 263 264 265
                    } else {
                        $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_Excludes::process_cache_excludes() - NO MATCH", 0);
                    }
                }
            }
        } else {
            $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_Excludes::process_cache_excludes() - Forced refresh, skipping cookie checks.", 1);
        }

266 267 268 269 270 271 272 273 274 275 276 277
        // user agent excludes
        if ($vivio_swift_global->configs->get_value('vivio_swift_enable_cache_comment')=='1')
        {
            $arr_user_agent_values=Vivio_Swift_Cache_Excludes::get_user_agent_values();
            foreach ($arr_user_agent_values as $i => $value) {
                if(strpos($_SERVER['HTTP_USER_AGENT'],$value)!==false){
                    $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_Excludes::process_cache_excludes() - Exclude 'user agent' rule hit for user agent value: '".$value."' [SKIPPING]", 2);
                    return "Exclude 'user agent' rule hit for user agent value: '".$value."' [SKIPPING]";
                }
            }
        }

Jordan Michaels's avatar
Jordan Michaels committed
278 279 280 281 282 283 284 285 286
        if ($vivio_swift_global->configs->get_value('vivio_swift_cache_exclude_path_is')=='1')
        {
            $arr_path_is_values=Vivio_Swift_Cache_Excludes::get_path_is_values();
            foreach ($arr_path_is_values as $i => $value) {
                $value = '/'.$value.'/';
                $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_Excludes::process_cache_excludes() - Testing URL: ".$url, 0);
                $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_Excludes::process_cache_excludes() - Against Path-Is Exclude: ".$value, 0);
                if ($url==$value){
                    $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_Excludes::process_cache_excludes() - Exclude 'Path Is' rule hit for path value: '".$value."' [SKIPPING]", 2);
287
                    return "Exclude 'Path Is' rule hit for path value: '".$value."' [SKIPPING]";
Jordan Michaels's avatar
Jordan Michaels committed
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
                } else {
                    $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_Excludes::process_cache_excludes() - NO MATCH", 0);
                }
            }
        }

        if ($vivio_swift_global->configs->get_value('vivio_swift_cache_exclude_path_ends_with')=='1')
        {
            $arr_path_ends_with_values=Vivio_Swift_Cache_Excludes::get_path_ends_with_values();
            foreach ($arr_path_ends_with_values as $i => $value) {
                $searchval='/\.('.$value.')$/';
                $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_Excludes::process_cache_excludes() - Testing URL: ".$url, 0);
                $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_Excludes::process_cache_excludes() - Against Ends-With Exclude: ".$searchval, 0);
                if (preg_match($searchval,$url)){
                    $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_Excludes::process_cache_excludes() - Exclude 'Path Ends With' rule hit for value: '".$value."' [SKIPPING]", 2);
303
                    return "Exclude 'Path Ends With' rule hit for value: '".$value."' [SKIPPING]";
Jordan Michaels's avatar
Jordan Michaels committed
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
                } else {
                    $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_Excludes::process_cache_excludes() - NO MATCH", 0);
                }
            }
        }

        if ($vivio_swift_global->configs->get_value('vivio_swift_cache_exclude_path_contains')=='1')
        {
            $arr_path_contains_values=Vivio_Swift_Cache_Excludes::get_path_contains_values();
            foreach ($arr_path_contains_values as $i => $value) {
                $searchval='/^.*('.$value.').*$/';
                $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_Excludes::process_cache_excludes() - Testing URL: ".$url, 0);
                $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_Excludes::process_cache_excludes() - Against Path-Contains Exclude: ".$searchval, 0);
                if (preg_match($searchval,$url)){
                    $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_Excludes::process_cache_excludes() - Exclude 'Path Contains' rule hit for value: '".$value."' [SKIPPING]", 2);
319
                    return "Exclude 'Path Contains' rule hit for value: '".$value."' [SKIPPING]";
Jordan Michaels's avatar
Jordan Michaels committed
320 321 322 323 324 325
                } else {
                    $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_Excludes::process_cache_excludes() - NO MATCH", 0);
                }
            }
        }

326
        // tests passed.
Jordan Michaels's avatar
Jordan Michaels committed
327
        $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_Excludes::process_cache_excludes() - Exclude tests passed.", 1);
328
        return false;
Jordan Michaels's avatar
Jordan Michaels committed
329 330 331 332

    }

}