<?php
// Google AdSense Mambot for Mambo 4.5.1
//
// version 1.0 beta
//
// Author : Chris Meichtry
//
// URL: http://www.drcorbeille.ch
// MAIL: chris@drcorbeille.ch
// 
// 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 2
// 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, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
// The "GNU General Public License" (GPL) is available at
// http://www.gnu.org/copyleft/gpl.html.
//
// Usage : {mosgoogle} in content displays a google ad as specified in paramenters. 
// You can also use {mosgoogle left}, {mosgoogle right} and {mosgoogle center} if you want the ad align differntly from the set parameters.
//

defined('_VALID_MOS') or die('Direct Access to this location is not allowed.') ;

$_MAMBOTS->registerFunction('onPrepareContent', 'botGoogleAds') ;

function botGoogleAds($published, &$row, &$params, $page=0) 
	{
	global $database ;
	
	$query = "SELECT id FROM #__mambots WHERE element = 'bot_adsense' AND folder = 'content'";

	$database->setQuery( $query );
	$id = $database->loadResult();
	$mambot = new mosMambot( $database );
	$mambot->load( $id );
	$params =& new mosParameters( $mambot->params );

	switch ($params->get('google_ad_format'))
		{
		case "728x90_as" :
			$ad_width = 728 ;
			$ad_height = 90 ;
			break ;
		case "468x60_as" :
			$ad_width = 468 ;
			$ad_height = 60 ;
			break ;
		case "234x60_as" :
			$ad_width = 243 ;
			$ad_height = 60 ;
			break ;
		case "125x125_as" :
			$ad_width = 125 ;
			$ad_height = 125 ;
			break ;
		case "120x600_as" :
			$ad_width = 120 ;
			$ad_height = 600 ;
			break ;
		case "160x600_as" :
			$ad_width = 160 ;
			$ad_height = 600 ;
			break ;
		case "120x240_as" :
			$ad_width = 120 ;
			$ad_height = 240 ;
			break ;
		case "300x250_as" :
			$ad_width = 300 ;
			$ad_height = 250 ;
			break ;
		case "250x250_as" :
			$ad_width = 250 ;
			$ad_height = 250 ;
			break ;
		case "336x280_as" :
			$ad_width = 336 ;
			$ad_height = 280 ;
			break ;
		case "180x150_as" :
			$ad_width = 180 ;
			$ad_height = 150 ;
			break ;
		}
		
	$googleAd 	= "<script language=\"javascript\">\r\n"
				. "<!--\r\n"
				. "google_ad_client = \"" . $params->get('google_ad_client') . "\";\r\n"
				. "google_ad_width = " .  $ad_width . "; \r\n"
				. "google_ad_height = " . $ad_height . "; \r\n"
				. "google_ad_format = \"" . $params->get('google_ad_format') . "\"; \r\n"
				. "google_ad_channel = \"" . $params->get('google_ad_channel') . "\"; \r\n"
				. ( $params->get('google_ad_in_frame') ? "google_page_url = document.location; \r\n" : "")
				. "google_ad_type = \"" . $params->get('google_ad_type') . "\"; \r\n"
				. "google_color_border = \"" . $params->get('google_color_border') . "\"; \r\n"
				. "google_color_bg = \"" . $params->get('google_color_bg') . "\"; \r\n"
				. "google_color_link = \"" . $params->get('google_color_link') . "\"; \r\n"
				. "google_color_url = \"" . $params->get('google_color_url') . "\"; \r\n"
				. "google_color_text = \"" . $params->get('google_color_text') . "\"; \r\n"
				. "//--> \r\n"
				. "</script>\r\n"
				. "<script language=\"javascript\"  src=\"http://pagead2.googlesyndication.com/pagead/show_ads.js\"></script>\r\n"
				. "<!-- end of google ads -->\r\n" ;
	
	$GLOBALS['botMosGoogle'] = $googleAd ;
	$GLOBALS['botMosGoogleAlign'] = $params->get('google_default_align') ;
	$GLOBALS['botMosGoogleMargin'] = $params->get('google_margin') ;
	
	$regex = "#{mosgoogle.*?\}#s" ;
	
	$row->text = preg_replace_callback($regex, 'botMosGoogle_replacer', $row->text) ;

	unset($GLOBALS['botMosGoogle']) ;		
	unset($GLOBALS['botMosGoogleAlign']) ;
	unset($GLOBALS['botMosGoogleMargin']) ;
	
	return true ;
	}

function botMosGoogle_replacer( &$matches )
	{
	$googleAd = $GLOBALS['botMosGoogle'] ;
	$align = $GLOBALS['botMosGoogleAlign'] ;
	$margin = $GLOBALS['botMosGoogleMargin'] ;
	
	if (stristr ($matches[0], "left") )
		$align = "left" ;
		
	if (stristr ($matches[0], "right") )
		$align = "right" ;
				
	if (stristr ($matches[0], "center") )
		$align = "center" ;

	if ($align != "center")
		return "<!--MosGoogleAd (c) http://www.drcorbeille.ch -->\r\n<div style='float:".$align."; margin-top:".$margin."px ; margin-left:".$margin."px ; margin-right:".$margin."px ; margin-bottom:".$margin."px ;'>".$googleAd."</div>" ;
	
	return "<!--MosGoogleAd (c) http://www.drcorbeille.ch -->\r\n<div align='center'>".$googleAd."</div>" ;
	}
?>