From 76624b97670f277afb8be32250d67d3dd32da5bb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fabian=20S=C3=B6rqvist?= <fabian.sorqvist@gmail.com>
Date: Wed, 2 Mar 2011 10:08:52 +0100
Subject: [PATCH] Issue #927860 by fabsor, Ian ward: Drupal 7 version of varnish.

---
 varnish.admin.inc |   23 ++++-----
 varnish.info      |    4 +-
 varnish.module    |  132 +++++++++++++++++++++++++++++++++-------------------
 3 files changed, 97 insertions(+), 62 deletions(-)

diff --git a/varnish.admin.inc b/varnish.admin.inc
index 0654532..2ed2188 100644
--- a/varnish.admin.inc
+++ b/varnish.admin.inc
@@ -1,18 +1,16 @@
 <?php
-// $Id:
+// $Id$
 
 /**
- * @file varnish.admin.inc
+ * @file
  * Administrative functions for Varnish integration.
  */
- 
- 
+
 /**
-* Menu callback for varnish admin settings.
-*/
+ * Menu callback for varnish admin settings.
+ */
 function varnish_admin_settings_form() {
   $form = array();
-  
   if (!extension_loaded('sockets')) {
     drupal_set_message(t('<a href="http://php.net/manual/en/sockets.installation.php">PHP Sockets extension</a> not enabled. Varnish terminal communication configuration skipped.'), 'error');
     return system_settings_form($form);
@@ -78,12 +76,12 @@ function varnish_admin_settings_form() {
     list($server, $port) = explode(':', $terminal);
     $stat = array_shift($status);
     if ($stat['code'] != 200) {
-      $items[] = t('The Varnish control terminal is not responding at %server on port %port.', array('%server' => $server, '%port' => $port));
+      $status = t('The Varnish control terminal is not responding at %server on port %port.', array('%server' => $server, '%port' => $port));
     }
     else {
-      $items[] = t('Varnish running. Observe more detailed statistics !link.', array('!link' => l(t('here'), 'admin/reports/varnish')));
+      $status = t('Varnish running. Observe more detailed statistics !link.', array('!link' => l(t('here'), 'admin/reports/varnish')));
     }
-    $form['varnish_stats']['#value'] = theme('item_list', $items);
+    $form['varnish_stats']['#markup'] = $status;
   }
   return system_settings_form($form);
 }
@@ -95,8 +93,9 @@ function varnish_admin_settings_form() {
 function varnish_admin_reports_page() {
   // connect to varnish and do a full status report
   $status = _varnish_terminal_run('stats');
-  foreach($status as $stat) {
-    $output .= '<pre>'. $stat['msg'] .'</pre>';
+  $output = '';
+  foreach ($status as $stat) {
+    $output .= '<pre>' . $stat['msg'] . '</pre>';
   }
   return $output;
 }
diff --git a/varnish.info b/varnish.info
index d746227..0f1a619 100644
--- a/varnish.info
+++ b/varnish.info
@@ -1,4 +1,4 @@
 name = Varnish 
 description = "Provides integration with the Varnish HTTP accelerator."
-core = 6.x
-package = Caching
\ No newline at end of file
+core = 7.x
+package = Caching
diff --git a/varnish.module b/varnish.module
index c3fd1b2..d8a3596 100644
--- a/varnish.module
+++ b/varnish.module
@@ -1,5 +1,5 @@
 <?php
-// $Id:
+// $Id$
 
 define ('VARNISH_NO_CLEAR', 0);
 define ('VARNISH_DEFAULT_CLEAR', 1);
@@ -12,14 +12,14 @@ define ('VARNISH_SELECTIVE_CLEAR', 2); // Requires Expire.module to be enabled.
  
 
 /**
- * Implementation of hook_menu()
+ * Implements hook_menu().
  *
  * Set up admin settings callbacks, etc.
  */
 function varnish_menu() {
   $items = array();
-  $items['admin/settings/varnish'] = array(
-    'title' => 'Varnish settings',
+  $items['admin/config/development/varnish'] = array(
+    'title' => 'Varnish',
     'description' => 'Configure your varnish integration.',
     'page callback' => 'drupal_get_form',
     'page arguments' => array('varnish_admin_settings_form'),
@@ -36,17 +36,22 @@ function varnish_menu() {
   return $items;
 }
 
-/** 
- * Implemetation of hook_perm()
+/**
+ * Implements hook_permission().
  *
- * Allows admins to control access to varnish settings.
+ * Add administer varnish permission.
  */
-function varnish_perm() {
-  return array('administer varnish');
+function varnish_permission() {
+  return array(
+    'administer varnish' =>  array(
+      'title' => t('Administer varnish'),
+      'description' => t('Perform administration tasks for varnish.'),
+    ),
+  );
 }
 
 /**
- * Implementation of hook_requirements()
+ * Implements hook_requirements().
  *
  * Insure that varnish's connection is good.
  */
@@ -75,47 +80,79 @@ function varnish_requirements($phase) {
 }
 
 /**
- * Implementation of hook_nodeapi()
+ * Implements hook_node_insert().
  *
- * Used to pick up cache_clearing events
- */
-function varnish_nodeapi(&$node, $op) {
-  if ($op == 'insert' || $op == 'update') {
-    // We've probably just run through node_save, and normally this is where
-    // Drupal calls a cache_clear_all().
-    switch (variable_get('varnish_cache_clear', VARNISH_DEFAULT_CLEAR)) {
-      case VARNISH_DEFAULT_CLEAR:
-        varnish_purge_all_pages();
-        break;
-    }
-  }
+ * Clear the cache when nodes have been inserted.
+ */
+function varnish_node_insert($node) {
+  variable_get('varnish_cache_clear', VARNISH_DEFAULT_CLEAR) ? varnish_purge_all_pages() : FALSE;
 }
 
 /**
- * Implementation of hook_comment()
+ * Implements hook_node_update().
  *
- * Used to pick up cache_clearing events
- */
-function varnish_comment($comment, $op) {
-  switch ($op) {
-    case 'insert':
-    case 'update':
-    case 'publish':
-    case 'unpublish':
-    case 'delete':
-      if (variable_get('varnish_cache_clear', VARNISH_DEFAULT_CLEAR) == VARNISH_DEFAULT_CLEAR) {
-        varnish_purge_all_pages();
-      }
-      break;
-  }
+ * Clear the cache when nodes have been updated.
+ */
+function varnish_node_update($node) {
+  variable_get('varnish_cache_clear', VARNISH_DEFAULT_CLEAR) ? varnish_purge_all_pages() : FALSE;
 }
 
 /**
- * Implementation of hook_expire_cache
+ * Implements hook_node_delete().
  *
- * Takes an array from expire.module and issue purges.
+ * Clear the cache when nodes have been deleted.
+ */
+function varnish_node_delete($node) {
+  variable_get('varnish_cache_clear', VARNISH_DEFAULT_CLEAR) ? varnish_purge_all_pages() : FALSE;
+}
+
+/**
+ * Implements hook_comment_insert().
+ *
+ * Clear the cache when comments have been inserted.
+ */
+function varnish_comment_insert($comment) {
+  variable_get('varnish_cache_clear', VARNISH_DEFAULT_CLEAR) ? varnish_purge_all_pages() : FALSE;
+}
+
+/**
+ * Implements hook_comment_update().
+ */
+function varnish_comment_update($comment) {
+  variable_get('varnish_cache_clear', VARNISH_DEFAULT_CLEAR) ? varnish_purge_all_pages() : FALSE;
+}
+
+/**
+ * Implements hook_comment_publish().
+ */
+function varnish_comment_publish($comment) {
+  variable_get('varnish_cache_clear', VARNISH_DEFAULT_CLEAR) ? varnish_purge_all_pages() : FALSE;
+}
+
+/**
+ * Implements hook_comment_unpublish().
  *
+ * Clear the cache when comments have been unpublished.
+ */
+function varnish_comment_unpublish($comment) {
+  variable_get('varnish_cache_clear', VARNISH_DEFAULT_CLEAR) ? varnish_purge_all_pages() : FALSE;
+}
+
+/**
+ * Implements hook_comment_delete().
+ *
+ * Clear the cache when nodes have been deleted.
+ */
+function varnish_comment_delete($comment) {
+  variable_get('varnish_cache_clear', VARNISH_DEFAULT_CLEAR) ? varnish_purge_all_pages() : FALSE;
+}
+
+/**
+ * Implements hook_expire_cache().
+ *
+ * Takes an array from expire.module and issue purges.
  * You may also safely call this function directly with an array of local urls to purge.
+ * @todo update expire.module to drupal 7?
  */
 function varnish_expire_cache($paths) {
   $host = _varnish_get_host();
@@ -125,7 +162,7 @@ function varnish_expire_cache($paths) {
 }
 
 /**
- * Implementation of hook_form_alter()
+ * Implements hook_form_alter().
  *
  * Add our submit callback to the "clear caches" button.
  */
@@ -136,10 +173,9 @@ function varnish_form_alter(&$form, $form_state, $form_id) {
 }
 
 /**
- * Implementation of hook_flush_caches()
+ * Implements hook_flush_caches().
  *
  * Flush caches on events like cron.
- *
  * This borrows logic from cache_clear_all() to respect cache_lifetime.
  */
 function varnish_flush_caches() {
@@ -170,7 +206,7 @@ function varnish_purge_all_pages() {
 }
 
 /**
- * Help[er function to parse the host from the global $base_url
+ * Helper function to parse the host from the global $base_url
  */
 function _varnish_get_host() {
   global $base_url;
@@ -180,8 +216,7 @@ function _varnish_get_host() {
 
 
 /**
- * Helper function that sends commands to Varnish
- *
+ * Helper function that sends commands to Varnish.
  * Utilizes sockets to talk to varnish terminal.
  */
 function _varnish_terminal_run($command) {
@@ -207,8 +242,9 @@ function _varnish_terminal_run($command) {
 
     // Do we need to authenticate?
     if ($status['code'] == 107) { // Require authentication
-      $challenge = substr($status['msg'], 0, 32);
-      $pack = $challenge ."\x0A". $secret ."\x0A". $challenge ."\x0A";
+      $secret = variable_get('varnish_control_key', '');
+      $challenge = drupal_substr($status['msg'], 0, 32);
+      $pack = $challenge . "\x0A" . $secret . "\x0A" . $challenge . "\x0A";
       $key = hash('sha256', $pack);
 
       socket_write($client, "auth $key\n");
-- 
1.7.1

