implemented varnish configuration generator
implemented varnish configuration generator

file:a/readme.txt -> file:b/readme.txt
--- a/readme.txt
+++ b/readme.txt
@@ -27,6 +27,7 @@
 * Cache TTL (for every other page)
 * IPs/Hosts to clear cache to support every type of Varnish Cache implementation
 * Purge key based PURGE
+* Logged in cookie
 * Debugging option
 * console for precise manual purges
 
@@ -51,6 +52,7 @@
  * supports every type of Varnish Cache implementation, see screenshots for examples
  * unlimited number of Varnish Cache servers
  * use of custom headers when communicating with Varnish Cache does not interfere with other caching plugins, cloudflare, etc
+ * Varnish Cache configuration generator
  * purge key method so you don't need to setup ACLs
  * debugging
  * actively maintained
@@ -59,8 +61,11 @@
 INSTALLATION AND CONFIGURATION
 ------------------------------
 
-Use the provided config files for Varnish Cache located in vcaching/varnish-conf folder. Just edit the backend IP/port and ACLs.
-You can also use the purge key method. You must fill in lib/purge.vcl the purge key.
+ * You must install Varnish Cache on your server(s)
+ * Go to the configuration generator. Fill in the backends/ACLs then download the configuration files
+ * Use these configuration files to configure Varnish Cache server(s). Usualy the configuration files are in /etc/varnish. In most cases you must put the downloaded configuration files in /etc/varnish and restart Varnish Cache
+
+ * Or use the provided Varnish Cache configuration files located in /wp-content/plugins/vcaching/varnish-conf folder.
 
  * Configure Varnish Caching settings in Configure » System » Varnish Caching:
     - Enable/Disable caching
@@ -95,11 +100,19 @@
 
  * How do I configure my Varnish Cache VCL?
 
-    - Use the provided Varnish Cache configuration files located in vcaching/varnish-conf folder.
+    - Use the Varnish Cache configuration generator. Fill in the backends/ACLs then download your configuration files.
+    - Or use the provided Varnish Cache configuration files located in /wp-content/plugins/vcaching/varnish-conf folder.
 
  * Can I use this with a proxy service like CloudFlare?
 
     - Yes.
+
+    * What is logged in cookie?
+
+    - Logged in cookie is a special cookie this plugin sets upon user login. Varnish Cache uses this cookie to bypass caching for logged in users.
+
+    - This is a small step towards securing your site for denial of service attacks. Denial of service attacks can happen if the attacker bypasses Varnish Cache and hits the backend directly.
+    - With the current configuration and the way Wordpress works, this can still happen with POST/AJAX requests.
 
 
 CREDITS / CONTACT

--- a/varnish-conf/v4/conf/backend.vcl
+++ b/varnish-conf/v4/conf/backend.vcl
@@ -1,4 +1,16 @@
-backend default {
+import directors;
+
+backend backend1 {
     .host = "192.168.0.2";
     .port = "80";
 }
+
+sub vcl_init {
+    new backends = directors.round_robin();
+    backends.add_backend(backend1);
+}
+
+sub vcl_recv {
+    set req.backend_hint = backends.backend();
+}
+

--- a/vcaching.class.php
+++ b/vcaching.class.php
@@ -209,5 +209,75 @@
             return array('error' => false, 'message' => $ret);
         }
     }
+
+    public function _parse_conf_file($version, $file, $content)
+    {
+        if ($file == 'default.vcl') {
+            $logged_in_cookie = variable_get($this->prefix . 'cookie');
+            $content = str_replace('c005492c65', $logged_in_cookie, $content);
+        } else if ($file == 'conf/backend.vcl') {
+            if ($version == 3) {
+                $content = "";
+            } else if ($version == 4) {
+                $content = "import directors;\n\n";
+            }
+            $backend = array();
+            $ips = variable_get($this->prefix . 'conf_backends');
+            $ips = explode(',', $ips);
+            $id = 1;
+            foreach ($ips as $ip) {
+                if (strstr($ip, ":")) {
+                    $_ip = explode(':', $ip);
+                    $ip = $_ip[0];
+                    $port = $_ip[1];
+                } else {
+                    $port = 80;
+                }
+                $content .= "backend backend" . $id . " {\n";
+                $content .= "\t.host = \"" . $ip . "\";\n";
+                $content .= "\t.port = \"" . $port . "\";\n";
+                $content .= "}\n";
+                $backend[3] .= "\t{\n";
+                $backend[3] .= "\t\t.backend = backend" . $id . ";\n";
+                $backend[3] .= "\t}\n";
+                $backend[4] .= "\tbackends.add_backend(backend" . $id . ");\n";
+                $id++;
+            }
+            if ($version == 3) {
+                $content .= "\ndirector backends round-robin {\n";
+                $content .= $backend[3];
+                $content .= "}\n";
+                $content .= "\nsub vcl_recv {\n";
+                $content .= "\tset req.backend = backends;\n";
+                $content .= "}\n";
+            } elseif ($version == 4) {
+                $content .= "\nsub vcl_init {\n";
+                $content .= "\tnew backends = directors.round_robin();\n";
+                $content .= $backend[4];
+                $content .= "}\n";
+                $content .= "\nsub vcl_recv {\n";
+                $content .= "\tset req.backend_hint = backends.backend();\n";
+                $content .= "}\n";
+            }
+        } else if ($file == 'conf/acl.vcl') {
+            $acls = variable_get($this->prefix . 'conf_acls');
+            $acls = explode(',', $acls);
+            $content = "acl cloudflare {\n";
+            $content .= "\t# set this ip to your Railgun IP (if applicable)\n";
+            $content .= "\t# \"1.2.3.4\";\n";
+            $content .= "}\n";
+            $content .= "\nacl purge {\n";
+            $content .= "\t\"localhost\";\n";
+            $content .= "\t\"127.0.0.1\";\n";
+            foreach ($acls as $acl) {
+                $content .= "\t\"" . $acl . "\";\n";
+            }
+            $content .= "}\n";
+        } else if ($file == 'lib/purge.vcl') {
+            $purge_key = variable_get($this->prefix . 'purge_key');
+            $content = str_replace('ff93c3cb929cee86901c7eefc8088e9511c005492c6502a930360c02221cf8f4', $purge_key, $content);
+        }
+        return $content;
+    }
 }
 

--- a/vcaching.module
+++ b/vcaching.module
@@ -62,6 +62,18 @@
 }
 
 /**
+ * Purge a node from Varnish on delete.
+ */
+function varnish_all_node_delete($node)
+{
+    if (variable_get('vcaching_enable')) {
+        $vcaching = new VCaching('vcaching_');
+        $vcaching->purgeUrl(base_path() . $node->path['alias']);
+        drupal_set_message(t('Varnish cache cleared!') . $vcaching->getNoticeMessage());
+    }
+}
+
+/**
  * Implements hook_menu()
  */
 function vcaching_menu()
@@ -111,15 +123,25 @@
         'weight' => 30
     );
 
-      $items['vcaching_purge_all_cache'] = array(
+    $items['admin/config/system/vcaching/conf'] = array(
+        'title' => 'Varnish VCLs',
+        'description' => 'Varnish Cache configuration',
+        'page callback' => 'drupal_get_form',
+        'page arguments' => array('vcaching_conf_form'),
+        'access arguments' => array('varnish caching settings access'),
+        'type' => MENU_LOCAL_TASK,
+        'weight' => 40
+    );
+
+    $items['vcaching_purge_all_cache'] = array(
         'title' => 'Purge Varnish page cache',
         'description' => 'Clear all page cache',
         'page callback' => 'vcaching_purge_all_cache_callback',
         'access arguments' => array('varnish caching purge all'),
         'menu_name' => 'shortcut-set-1',
         'type' => MENU_NORMAL_ITEM,
-        'weight' => 10,
-      );
+        'weight' => 10
+    );
 
     return $items;
 }
@@ -279,17 +301,6 @@
 }
 
 /**
- * Stats form
- */
-function vcaching_stats_form()
-{
-    if (variable_get('vcaching_enable')) {
-        $vcaching = new VCaching('vcaching_');
-        return $vcaching->stats();
-    }
-}
-
-/**
  * Console form submit
  */
 function vcaching_console_form_submit($form, &$form_state)
@@ -302,6 +313,108 @@
 }
 
 /**
+ * Stats form
+ */
+function vcaching_stats_form()
+{
+    if (variable_get('vcaching_enable')) {
+        $vcaching = new VCaching('vcaching_');
+        return $vcaching->stats();
+    }
+}
+
+/**
+ * Conf form
+ */
+function vcaching_conf_form()
+{
+    $form = array();
+    $form['general'] = array(
+        '#type' => 'fieldset',
+        '#title' => t('Varnish configuration'),
+        '#weight' => 5,
+        '#collapsible' => false,
+        '#collapsed' => false
+    );
+    $form['general']['vcaching_conf_backends'] = array(
+        '#type' => 'textfield',
+        '#title' => t('Backends'),
+        '#description' => t('Comma separated ip/ip:port. Example : 192.168.0.2,192.168.0.3:8080'),
+        '#default_value' => variable_get('vcaching_conf_backends'),
+        '#size' => 60,
+        '#required' => true
+    );
+    $form['general']['vcaching_conf_acls'] = array(
+        '#type' => 'textfield',
+        '#title' => t('ACLs'),
+        '#description' => t('Comma separated ip/ip range. Example : 192.168.0.2,192.168.1.1/24'),
+        '#default_value' => variable_get('vcaching_conf_acls'),
+        '#size' => 60,
+        '#required' => true
+    );
+    $form['action'] = array(
+        '#type' => 'fieldset',
+        '#title' => t('Get configuration files'),
+        '#weight' => 5,
+        '#collapsible' => false,
+        '#collapsed' => false
+    );
+    $form['action']['vcaching_conf_version'] = array(
+        '#type' => 'select',
+        '#title' => t('Version'),
+        '#description' => t('Varnish Cache version'),
+        '#options' => array(3 => 3,4 => 4),
+        '#default_value' => 3,
+        '#required' => true
+    );
+    $form['action']['submit'] = array('#type' => 'submit', '#value' => t('Download'), '#submit' => array('vcaching_conf_form_submit'));
+    return system_settings_form($form);
+}
+
+/**
+ * Conf form submit
+ */
+function vcaching_conf_form_submit($form, &$form_state)
+{
+    $version = in_array($_POST['vcaching_conf_version'], array(3,4)) ? $_POST['vcaching_conf_version'] : 3;
+    $tmpfile = tempnam("tmp", "zip");
+    $zip = new ZipArchive();
+    $zip->open($tmpfile, ZipArchive::OVERWRITE);
+    $files = array(
+        'default.vcl' => true,
+        'LICENSE' => false,
+        'README.rst' => false,
+        'conf/acl.vcl' => true,
+        'conf/backend.vcl' => true,
+        'lib/bigfiles.vcl' => false,
+        'lib/bigfiles_pipe.vcl' => false,
+        'lib/cloudflare.vcl' => false,
+        'lib/mobile_cache.vcl' => false,
+        'lib/mobile_pass.vcl' => false,
+        'lib/purge.vcl' => true,
+        'lib/static.vcl' => false,
+        'lib/xforward.vcl' => false,
+    );
+    $vcaching = new VCaching('vcaching_');
+    foreach ($files as $file => $parse) {
+        $filepath = __DIR__ . '/varnish-conf/v' . $version . '/' . $file;
+        if ($parse) {
+            $content = $vcaching->_parse_conf_file($version, $file, file_get_contents($filepath));
+        } else {
+            $content = file_get_contents($filepath);
+        }
+        $zip->addFromString($file, $content);
+    }
+    $zip->close();
+    header('Content-Type: application/zip');
+    header('Content-Length: ' . filesize($tmpfile));
+    header('Content-Disposition: attachment; filename="varnish_v' . $version . '_conf.zip"');
+    readfile($tmpfile);
+    unlink($tmpfile);
+    exit();
+}
+
+/**
  * Shortcut callback
  */
 function vcaching_purge_all_cache_callback()

comments