Webservice written in php.

This script connects to the management port on vanish and purges from given parameteres which can be url or/and host.

ex:

vpurge.php?url=/somepath.*&host=www.somehost.tld

(regex can be used)

If hash has been rewritten in vcl, it might not work, as it depends on that url comes first and host afterwards. Hashes usually looks like this: someurl#somehost#

The script: (vpurge.php)

<?php



  $ip = <ipadress on varnishserver>
  $port = <management port on varnish>

  $timeout = 1;
  $verbose = 0;

  # inits
  $sock = fsockopen ($ip,$port,$errno, $errstr,$timeout);
  if (!$sock) { echo "connections failed $errno $errstr"; exit; }

  # get param and strip invalid stuff
  $url = preg_replace ("/\n/","",$_GET['url'];);
  $hash = preg_replace ("/\n/","",$_GET['hash'];);
  $host = preg_replace ("/\n/","",$_GET['host'];);
  $hash = preg_replace ("/_/","#",$hash);

  if ( !($url || $hash || host) ) { echo "No params"; exit; }

  stream_set_timeout($sock,$timeout);

  $pcommand = "purge";
  # Send command
  if ($url && !$host) {
    $pcommand .= ".hash $url#";
  } elseif ($host) {
    $pcommand .= ".hash $url#$host#";
  }

  put ($pcommand);

  if ($hash) {
    put ("purge.hash $hash");
  }

  put ("quit");

  fclose ($sock);

  function readit() {
    global $sock,$verbose;
    if (!$verbose) { return; }
    while ($sockstr = fgets($sock,1024)) {
      $str .= "rcv: " . $sockstr . "<br/>";
    }
    if ($verbose) { echo "$str\n"; }
  }

  function put($str) {
    global $sock,$verbose;
    fwrite ($sock, $str . "\r\n");
    if ($verbose) { echo "send: $str <br/>\n"; }
    readit();
  }
?>