From 9940739e8961221323ee3e21e7e5e4f93682be6b Mon Sep 17 00:00:00 2001
From: Andy Zhang <mjin.zhang@gmail.com>
Date: Wed, 20 Jul 2011 15:47:26 +0800
Subject: [PATCH] Issue #1147900: Made it work on Drupal 7; Updated webtrends.js to 9.4.0.

---
 js/webtrends.js   |  106 +++++++++++++++++++++++++++++++++++++----------------
 webtrends.info    |   10 +++++
 webtrends.install |   17 ++++++++
 webtrends.module  |   77 ++++++++++++++++++++++++++++++--------
 4 files changed, 162 insertions(+), 48 deletions(-)
 create mode 100644 webtrends.install

diff --git a/js/webtrends.js b/js/webtrends.js
index 64ffb6d..171a830 100644
--- a/js/webtrends.js
+++ b/js/webtrends.js
@@ -1,14 +1,21 @@
+// WebTrends SmartSource Data Collector Tag
+// Version: 9.4.0     
+// Tag Builder Version: 3.2
+// Created: 7/7/2011 7:25:15 AM
+
 function WebTrends(){
 	var that=this;
 	// begin: user modifiable
 	this.dcsid="";
 	this.domain="statse.webtrendslive.com";
-	this.timezone=-11;
+	this.timezone=-8;
 	this.fpcdom="";
 	this.enabled=true;
-	this.i18n=false;
+	this.i18n=true;
 	this.fpc="WT_FPC";
 	this.paidsearchparams="gclid";
+	this.splitvalue="";
+	this.preserve=false;
 	// end: user modifiable
 	this.DCS={};
 	this.WT={};
@@ -16,7 +23,7 @@ function WebTrends(){
 	this.images=[];
 	this.index=0;
 	this.exre=(function(){return(window.RegExp?new RegExp("dcs(uri)|(ref)|(aut)|(met)|(sta)|(sip)|(pro)|(byt)|(dat)|(p3p)|(cfg)|(redirect)|(cip)","i"):"");})();
-	this.re=(function(){return(window.RegExp?(that.i18n?{"%25":/\%/g}:{"%09":/\t/g,"%20":/ /g,"%23":/\#/g,"%26":/\&/g,"%2B":/\+/g,"%3F":/\?/g,"%5C":/\\/g,"%22":/\"/g,"%7F":/\x7F/g,"%A0":/\xA0/g}):"");})();
+	this.re=(function(){return(window.RegExp?(that.i18n?{"%25":/\%/g,"%26":/\&/g}:{"%09":/\t/g,"%20":/ /g,"%23":/\#/g,"%26":/\&/g,"%2B":/\+/g,"%3F":/\?/g,"%5C":/\\/g,"%22":/\"/g,"%7F":/\x7F/g,"%A0":/\xA0/g}):"");})();
 }
 WebTrends.prototype.dcsGetId=function(){
 	if (this.enabled&&(document.cookie.indexOf(this.fpc+"=")==-1)&&(document.cookie.indexOf("WTLOPTOUT=")==-1)){
@@ -158,13 +165,16 @@ WebTrends.prototype.dcsFPC=function(){
 WebTrends.prototype.dcsMultiTrack=function(){
 	var args=dcsMultiTrack.arguments?dcsMultiTrack.arguments:arguments;
 	if (args.length%2==0){
+	    this.dcsSaveProps(args);
 		this.dcsSetProps(args);
 		var dCurrent=new Date();
 		this.DCS.dcsdat=dCurrent.getTime();
 		this.dcsFPC();
 		this.dcsTag();
+		this.dcsRestoreProps();
 	}
 }
+
 WebTrends.prototype.dcsCleanUp=function(){
 	this.DCS={};
 	this.WT={};
@@ -186,6 +196,39 @@ WebTrends.prototype.dcsSetProps=function(args){
 		}
 	}
 }
+WebTrends.prototype.dcsSaveProps=function(args){
+	var i,x,key,param;
+	if (this.preserve){
+		this.args=[];
+		for (i=0,x=0;i<args.length;i+=2){
+			param=args[i];
+			if (param.indexOf('WT.')==0){
+				key=param.substring(3);
+				this.args[x]=param;
+				this.args[x+1]=this.WT[key]||"";
+				x+=2;
+			}
+			else if (param.indexOf('DCS.')==0){
+				key=param.substring(4);
+				this.args[x]=param;
+				this.args[x+1]=this.DCS[key]||"";
+				x+=2;
+			}
+			else if (param.indexOf('DCSext.')==0){
+				key=param.substring(7);
+				this.args[x]=param;
+				this.args[x+1]=this.DCSext[key]||"";
+				x+=2;
+			}
+		}
+	}
+}
+WebTrends.prototype.dcsRestoreProps=function(){
+	if (this.preserve){
+		this.dcsSetProps(this.args);
+		this.args=[];
+	}
+}
 WebTrends.prototype.dcsAdv=function(){
 	this.dcsFPC();
 }
@@ -277,7 +320,7 @@ WebTrends.prototype.dcsVar=function(){
 	WT.fv=(function(){
 		var i,flash;
 		if (window.ActiveXObject){
-			for(i=10;i>0;i--){
+			for(i=15;i>0;i--){
 				try{
 					flash=new ActiveXObject("ShockwaveFlash.ShockwaveFlash."+i);
 					return i+".0";
@@ -297,7 +340,7 @@ WebTrends.prototype.dcsVar=function(){
 	})();
 	WT.slv=(function(){
 		var slv="Not enabled";
-		try{
+		try{     
 			if (navigator.userAgent.indexOf('MSIE')!=-1){
 				var sli = new ActiveXObject('AgControl.AgControl');
 				if (sli){
@@ -311,17 +354,24 @@ WebTrends.prototype.dcsVar=function(){
 		catch(e){
 		}
 		if (slv!="Not enabled"){
-			var i,j,v;
+			var i,m,M,F;
 			if ((typeof(Silverlight)=="object")&&(typeof(Silverlight.isInstalled)=="function")){
-				for(i=3;i>0;i--){
-					for (j=9;j>=0;j--){
-						v=i+"."+j;
-						if (Silverlight.isInstalled(v)){
-							slv=v;
+				for(i=9;i>0;i--){
+					M=i;
+					if (Silverlight.isInstalled(M+".0")){
 							break;
-						}
 					}
-					if (slv==v){
+					if (slv==M){
+						break;
+					}
+				}
+				for (m=9;m>=0;m--){
+					F=M+"."+m;
+					if (Silverlight.isInstalled(F)){
+						slv=F;
+						break;
+					}
+					if (slv==F){
 						break;
 					}
 				}
@@ -332,7 +382,7 @@ WebTrends.prototype.dcsVar=function(){
 	if (this.i18n){
 		if (typeof(document.defaultCharset)=="string"){
 			WT.le=document.defaultCharset;
-		}
+		} 
 		else if (typeof(document.characterSet)=="string"){
 			WT.le=document.characterSet;
 		}
@@ -340,8 +390,8 @@ WebTrends.prototype.dcsVar=function(){
 			WT.le="unknown";
 		}
 	}
-	WT.tv="8.6.2";
-//	WT.sp="@@SPLITVALUE@@";
+	WT.tv="9.4.0";
+	WT.sp=this.splitvalue;
 	WT.dl="0";
 	WT.ssl=(window.location.protocol.indexOf('https:')==0)?"1":"0";
 	DCS.dcsdat=dCurrent.getTime();
@@ -415,18 +465,15 @@ WebTrends.prototype.dcsCreateImage=function(dcsSrc){
 		this.images[this.index].src=dcsSrc;
 		this.index++;
 	}
-	else{
-		document.write('<IMG ALT="" BORDER="0" NAME="DCSIMG" WIDTH="1" HEIGHT="1" SRC="'+dcsSrc+'">');
-	}
 }
 WebTrends.prototype.dcsMeta=function(){
 	var elems;
-	if (document.all){
-		elems=document.all.tags("meta");
-	}
-	else if (document.documentElement){
+	if (document.documentElement){
 		elems=document.getElementsByTagName("meta");
 	}
+	else if (document.all){
+		elems=document.all.tags("meta");
+	}
 	if (typeof(elems)!="undefined"){
 		var length=elems.length;
 		for (var i=0;i<length;i++){
@@ -464,14 +511,6 @@ WebTrends.prototype.dcsTag=function(){
 			P+=this.dcsA(N,DCS[N]);
 		}
 	}
-	var keys=["co_f","vtid","vtvs","vt_f_tlv"];
-	for (var i=0;i<keys.length;i++){
-		var key=keys[i];
-		if (WT[key]){
-			P+=this.dcsA("WT."+key,WT[key]);
-			delete WT[key];
-		}
-	}
 	for (N in WT){
 		if (WT[N]&&(typeof WT[N]!="function")){
 			P+=this.dcsA("WT."+N,WT[N]);
@@ -516,6 +555,9 @@ WebTrends.prototype.dcsCollect=function(){
         this.dcsVar();
         this.dcsMeta();
         this.dcsAdv();
+        if (typeof(this.dcsCustom)=="function"){
+			this.dcsCustom();
+        }
         this.dcsTag();
     }
 }
@@ -538,4 +580,4 @@ Function.prototype.wtbind = function(obj){
 		return method.apply(obj,arguments);
 	};
 	return temp;
-}
\ No newline at end of file
+}
diff --git a/webtrends.info b/webtrends.info
index 7e8d705..d882cd2 100644
--- a/webtrends.info
+++ b/webtrends.info
@@ -3,3 +3,13 @@ description = Provides Webtrends tracking integration
 package = Webtrends
 core = 7.x
 version = 7.x-1.0-beta1
+files[] = webtrends.module
+files[] = webtrends.install
+files[] = webtrends_admin.inc
+
+; Information added by drupal.org packaging script on 2011-06-03
+version = "7.x-1.x-dev"
+core = "7.x"
+project = "webtrends"
+datestamp = "1307062484"
+
diff --git a/webtrends.install b/webtrends.install
new file mode 100644
index 0000000..e78d838
--- /dev/null
+++ b/webtrends.install
@@ -0,0 +1,17 @@
+<?php
+
+/**
+ * @file
+ * Implementing hook_uninstall to remove variables at uninstallation
+ */
+
+/**
+ * Implementation of hook_uninstall
+ */
+function  webtrends_uninstall() {
+  // Remove variables
+    variable_del('webtrends_source_id');
+    variable_del('webtrends_domain');
+    variable_del('webtrends_fpcdom');
+    variable_del('webtrends_timezone');
+}
diff --git a/webtrends.module b/webtrends.module
index dbedb3a..b482b4c 100644
--- a/webtrends.module
+++ b/webtrends.module
@@ -48,14 +48,12 @@ function webtrends_init() {
 }
 
 /**
- * Implementation of hook_footer() to insert Webtrends initialization script at the end of the page
- *
- * @return string $script
+ * Implementation of hook_page_alter(&$page)
  */
-function webtrends_footer() {
+function webtrends_page_alter(&$page) {
   $script = '';
   $webtrends_values = array();
-  
+
   foreach (module_list() as $module) {
     $function = $module .'_wt_values';
     if (function_exists($function)) {
@@ -66,16 +64,63 @@ function webtrends_footer() {
   global $webtrends_init;
 
   if ($webtrends_init) {
-    $script = '<script type="text/javascript">function init_webtrends() {';
-    $script .= 'var _tag = new WebTrends();'."\n";
-    $script .= '_tag.timezone = '. variable_get('webtrends_timezone', 0).";\n";
-    $script .= '_tag.dcsid = "'. ($webtrends_values['webtrends_source_id'] ? $webtrends_values['webtrends_source_id'] : variable_get('webtrends_source_id', '')).'";'."\n";
-    $script .= '_tag.domain = "'. ($webtrends_values['webtrends_domain'] ? $webtrends_values['webtrends_domain'] : variable_get('webtrends_domain', '')).'";'."\n";
-    $script .= '_tag.fpcdom = "'. ($webtrends_values['webtrends_fpcdom'] ? $webtrends_values['webtrends_fpcdom'] : variable_get('webtrends_fpcdom', '')).'";'."\n";
-    $script .= '_tag.dcsGetId();'."\n";
-    $script .= '_tag.dcsCollect();'."\n";
-    $script .= '} window.onload=init_webtrends;</script>';    
+
+    if (!isset($webtrends_values['webtrends_source_id'])) {
+      $webtrends_values['webtrends_source_id'] = variable_get('webtrends_source_id', '');
+    }
+    if (!isset($webtrends_values['webtrends_domain'])) {
+      $webtrends_values['webtrends_domain'] = variable_get('webtrends_domain', '');
+    }
+    if (!isset($webtrends_values['webtrends_fpcdom'])) {
+      $webtrends_values['webtrends_fpcdom'] = variable_get('webtrends_fpcdom', '');
+    }
+    if (!isset($webtrends_values['webtrends_timezone'])) {
+      $webtrends_values['webtrends_timezone'] = variable_get('webtrends_timezone', 0);
+    }
+
+    $src_url = '//' . $webtrends_values['webtrends_domain'];
+    $src_url .= '/' . $webtrends_values['webtrends_source_id'];
+    $src_url .= '/njs.gif?dcsuri=/nojavascript&amp;WT.js=No&amp;WT.tv=9.4.0';
+    $src_url .= '&amp;dcssip=' . $webtrends_values['webtrends_fpcdom'];
+
+    $script = <<< SCRIPT_STRING_EOD
+       <script type = 'text/javascript'>
+         //<![CDATA[
+         function init_webtrends() {
+           var _tag = new WebTrends();
+           _tag.timezone = {$webtrends_values['webtrends_timezone']};
+           _tag.dcsid = "{$webtrends_values['webtrends_source_id']}";
+           _tag.domain = "{$webtrends_values['webtrends_domain']}";
+           _tag.fpcdom = "{$webtrends_values['webtrends_fpcdom']}";
+           _tag.dcsGetId();
+           _tag.dcsCollect();
+         }
+         window.onload = init_webtrends;
+         //]]>
+       </script>
+       <noscript>
+          <div>
+             <img alt='DCSIMG' id='DCSIMG' width='1' height='1' src="{$src_url}" />
+          </div>
+       </noscript>
+SCRIPT_STRING_EOD;
+
+    $page['page_bottom']['webtrends_script'] = array(
+      '#markup' => $script,
+      '#weight' => 1000,
+    );
+  }
+}
+
+/**
+ * Implementation of hook_module_implements_alter()
+ */
+function webtrends_module_implements_alter(&$implementations, $hook) {
+  if ($hook == 'page_alter') {
+    // Move webtrends_page_alter to the end of the list. This way we will make
+    // sure that the webtrends script will be added just before the </body> tag
+    $group = $implementations['webtrends'];
+    unset($implementations['webtrends']);
+    $implementations['webtrends'] = $group;
   }
-  
-  return $script;
 }
-- 
1.7.4.1

