<?php

/*
Google Analytics Plugin for MyBB
Copyright (C) 2010 Sebastian Wunderlich

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

if(!defined('IN_MYBB'))
{
	die();
}

$plugins->add_hook('pre_output_page','ganalytics');

function ganalytics_info()
{
	return array
	(
		'name'=>'Google Analytics',
		'description'=>'Adds Google Analytics tracking code to every page.',
		'website'=>'http://mods.mybboard.net/view/google-analytics',
		'author'=>'Sebastian Wunderlich',
		'version'=>'1.5',
		'guid'=>'b928e2bd9d442c830c672ec3d94397b6',
		'compatibility'=>'14*,16*',
		'codename'=>'ganalytics'
	);
}

function ganalytics_activate()
{
	global $db,$mybb;
	$info=ganalytics_info();
	$setting_group_array=array
	(
		'name'=>$info['codename'],
		'title'=>$info['name'],
		'description'=>'Here you can edit '.$info['name'].' settings.',
		'disporder'=>1,
		'isdefault'=>0
	);
	$db->insert_query('settinggroups',$setting_group_array);
	$group=$db->insert_id();
	$settings=array
	(
		'ganalytics_webpropertyid'=>array
		(
			'Google Analytics web property ID',
			'Enter here your Google Analytics web property ID (for example "UA-7207177-6").',
			'text',
			''
		)
	);
	$i=1;
	foreach($settings as $name=>$sinfo)
	{
		$insert_array=array
		(
			'name'=>$name,
			'title'=>$db->escape_string($sinfo[0]),
			'description'=>$db->escape_string($sinfo[1]),
			'optionscode'=>$db->escape_string($sinfo[2]),
			'value'=>$db->escape_string($sinfo[3]),
			'gid'=>$group,
			'disporder'=>$i,
			'isdefault'=>0
		);
		$db->insert_query('settings',$insert_array);
		$i++;
	}
	rebuild_settings();
}

function ganalytics_deactivate()
{
	global $db;
	$info=ganalytics_info();
	$result=$db->simple_select('settinggroups','gid','name="'.$info['codename'].'"',array('limit'=>1));
	$group=$db->fetch_array($result);
	if(!empty($group['gid']))
	{
		$db->delete_query('settinggroups','gid="'.$group['gid'].'"');
		$db->delete_query('settings','gid="'.$group['gid'].'"');
		rebuild_settings();
	}
}

function ganalytics($page)
{
	global $mybb;
	if($mybb->settings['ganalytics_webpropertyid'])
	{
		$page=str_replace('</body>','<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src=\'" + gaJsHost + "google-analytics.com/ga.js\' type=\'text/javascript\'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("'.$mybb->settings['ganalytics_webpropertyid'].'");
pageTracker._trackPageview();
} catch(err) {}</script>
</body>',$page);
	}
	return $page;
}

?>