Ejemplo n.º 1
0
if (!current_user_can('manage_options')) {
    wp_die(__('You do not have sufficient permissions to access this page.'));
}
// Dodge Wordpress's rediculous magic quotes nonsense
$postdata = wpv_postdata(file_get_contents('php://input'));
// save posted settings
if (isset($postdata['wpv_save'])) {
    update_option('wpv_clients', $postdata['wpv_clients']);
    update_option('wpv_secrets', $postdata['wpv_secrets']);
    update_option('wpv_host_pattern', $postdata['wpv_host_pattern']);
    update_option('wpv_enabled', !empty($postdata['wpv_enabled']));
} else {
    if (isset($postdata['wpv_purge'])) {
        $purge = $postdata['wpv_purge_pattern'];
        $timeout = $postdata['wpv_timeout'] or $timeout = 3;
        $secrets = wpv_get_secrets();
        foreach (wpv_get_clients() as $i => $client) {
            list($host, $port, $vers) = $client;
            try {
                $Sock = wpv_admin_socket($host, $port, $vers);
                if (!empty($secrets[$i])) {
                    $Sock->set_auth($secrets[$i]);
                }
                $Sock->connect($timeout);
                $Sock->purge($purge);
                $purges[$host . ':' . $port] = 'Purged';
            } catch (Exception $Ex) {
                $purges[$host . ':' . $port] = 'Error: ' . $Ex->getMessage();
            }
        }
    } else {
Ejemplo n.º 2
0
/**
 * Send multiple purge commands to all varnishadm sockets
 */
function wpv_purge_urls(array $urls)
{
    $hostpattern = get_option('wpv_host_pattern', '');
    $secrets = wpv_get_secrets();
    $clients = wpv_get_clients();
    if (!$clients) {
        return;
    }
    // iterate over all available sockets
    foreach ($clients as $i => $client) {
        try {
            list($host, $port, $vers) = $client;
            $Sock = wpv_admin_socket($host, $port, $vers);
            if (!empty($secrets[$i])) {
                $Sock->set_auth($secrets[$i]);
            }
            $Sock->connect();
            if (!$Sock->status()) {
                throw new Exception(sprintf('Varnish server stopped on host %s:%d', $host, $port));
            }
            // iterate over all URLs and purge from this socket
            foreach ($urls as $url => $bool) {
                try {
                    $expr = sprintf('req.url ~ "%s"', $url);
                    if ($hostpattern) {
                        $expr .= sprintf(' && req.http.host ~ "%s"', $hostpattern);
                    }
                    $Sock->purge($expr);
                } catch (Exception $Ex) {
                    trigger_error($Ex->getMessage(), E_USER_WARNING);
                    continue;
                }
            }
        } catch (Exception $Ex) {
            trigger_error($Ex->getMessage(), E_USER_WARNING);
        }
        // next socket
        $Sock->quit();
    }
    return true;
}