<?php
/**
* @version 1.2 - RokBox - RocketWerx
* @thanks to Dennis Pleiter for Wildcards contribution
* @package RocketWerx
* @copyright Copyright (C) 2007 RocketTheme. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
*/

// no direct access
defined( '_VALID_MOS' ) or die( 'Restricted access' );

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

function botRokBox( $published, &$row, &$params, $page=0 ) {
	global $mainframe, $Itemid, $database, $_MAMBOTS;

	require_once(dirname(__FILE__) . '/rokbox/imagehandler.php');

	// simple performance check to determine whether bot should process further
	if ( strpos( $row->text, 'rokbox' ) === false ) {
		return true;
	}

	// define the regular expression for the bot
	$regex = "#{rokbox(.*?)}(.*?){/rokbox}#s";

		// check whether mambot has been unpublished
	if ( !$published ) {
		return true;
	}

	if ( !isset($_MAMBOTS->_content_mambot_params['rokbox']) ) {
		// load mambot params info
		$query = "SELECT params"
		. "\n FROM #__mambots"
		. "\n WHERE element = 'rokbox'"
		. "\n AND folder = 'content'"
		;
		$database->setQuery( $query );
		$database->loadObject($mambot);

		// save query to class variable
		$_MAMBOTS->_content_mambot_params['rokbox'] = $mambot;
	}

	// pull query data from class variable
	$mambot = $_MAMBOTS->_content_mambot_params['rokbox'];

 	$botParams = new mosParameters( $mambot->params );
 	$GLOBALS['_MAMBOT_rokbox_PARAMS'] =& $botParams;

	// perform the replacement
	$row->text = preg_replace_callback( $regex, 'rokbox_replacer', $row->text );

	return true;
}

/**
* Replaces the matched tags an image
* @param array An array of matches (see preg_match_all)
* @return string
*/
function rokbox_replacer( &$matches ) {
	global $mosConfig_cachepath;

	$botParams =& $GLOBALS['_MAMBOT_rokbox_PARAMS'];

	$thumb_ext	= $botParams->def( 'thumb_ext', '_thumb');
	$thumb_class	= $botParams->def( 'thumb_class', 'album');
	$thumb_width = $botParams->def( 'thumb_width', '100');
	$thumb_height = $botParams->def( 'thumb_height', '100');
	$thumb_quality = $botParams->def( 'thumb_quality', '90');
	$thumb_custom = $botParams->def( 'thumb_custom', 0);
	$thumb_dir = $botParams->def( 'thumb_dir');
	$compatibility = $botParams->def( 'compatibility', 'rokbox');
	
	/* thumbnail settings */
	$improve_thumbnails = false; // Auto Contrast, Unsharp Mask, Desaturate,  White Balance
	$temp_path = $mosConfig_cachepath; //required for improved gd_verion() function
	$thumb_quality = $thumb_quality;
	$width = $thumb_width;
	$height = $thumb_height;

	/* slimbox = lightbox mode */
	if ($compatibility == "slimbox") $compatibility = "lightbox";

	for ($i=0; $i < count($matches[0]); $i++) { 
		$thealbum = '';
		$thetitle = '';
		$thethumb = '';
		$thetype = '';
		$thesize = '';
		$thetext = '';
		$themodule = '';
		$thethumbcount = 999;
		if (@$matches[1]) {
			$inline_params = $matches[1];

			// get album
			$album_matches = array();
			preg_match( "#album=\|(.*?)\|#s", $inline_params, $album_matches );
			if (isset($album_matches[1])) $thealbum = "(" . trim($album_matches[1]) . ")";

			// get size
			$size_matches = array();
			preg_match( "#size=\|(.*?)\|#s", $inline_params, $size_matches );
			if (isset($size_matches[1])) $thesize = "[" . trim($size_matches[1]) . "]";
	
			// get title
			$title_matches = array();
			preg_match( "#title=\|(.*?)\|#s", $inline_params, $title_matches );
			if (isset($title_matches[1])) $thetitle =  $title_matches[1];
	
			// get text
			$text_matches = array();
			preg_match( "#text=\|(.*?)\|#s", $inline_params, $text_matches );
			if (isset($text_matches[1])) $thetext =  $text_matches[1];
	
			// force image
			$type_matches = array();
			preg_match( "#type=\|(.*?)\|#s", $inline_params, $type_matches );
			if (isset($type_matches[1])) $thetype = $type_matches[1];

        		// get module
        		$module_matches = array();
        		preg_match( "#module=\|(.*?)\|#s", $inline_params, $module_matches );
        		if (isset($module_matches[1])) $themodule =  "[module=".$module_matches[1]."]";

			// get thumb
			$thumb_matches = array();
			preg_match( "#thumb=\|(.*?)\|#s", $inline_params, $thumb_matches );
			if (isset($thumb_matches[1])) $thethumb =  $thumb_matches[1];
			
			// get thumb count
    		$thumbcount_matches = array();
    		preg_match( "#thumbcount=\|(.*?)\|#s", $inline_params, $thumbcount_matches );
    		if (isset($thumbcount_matches[1])) $thethumbcount =  $thumbcount_matches[1];
		}
		
		$onsite=1;
		$text = '';
		$displaythumb = '';
		$tmp = glob(trim($matches[2]));
		if (count($tmp) < 1) {
			$tmp = array(trim($matches[2]));
			$onsite=0;
		}
		
	
		foreach ($tmp as $link) {
			if ( strpos( $link, $thumb_ext ) === false ) {

				if (strlen($thethumb)) $image_url = trim($thethumb);
				else $image_url = $link;

				$extension = substr($image_url,strrpos($image_url,"."));
				$image_name = substr($image_url,0,strrpos($image_url, "."));

				$tmp_thumb =  $thumb_dir . "/" . substr($image_url,strrpos($image_url,DIRECTORY_SEPARATOR));
				$thumb_url = $image_name . $thumb_ext . $extension;

				$isimage = ($extension == '.jpg' || $extension == '.jpeg' || $extension == '.bmp' || $extension == '.png' || $extension == '.gif');

				if ($onsite){
					if (!isset($size_matches[1]) && $isimage) {
						list($image_width,$image_height)=getimagesize($link);
						$thesize = "[" . $image_width . " " . $image_height . "]";
					}
					$thethumbcount--;
					if ($thethumbcount<0) $displaythumb = '" style="display: none;';
					else $displaythumb = "";
				}

				if (!strlen($thethumb) && !strlen($thetype) && strlen($thetext) > 0) {
					if (strlen($themodule)) $text .= '<a href="' . $link . '" rel="' . $compatibility . $thesize . $thealbum . $displaythumb . $themodule . '" title="' . $thetitle . '">'.$thetext.'</a>';
					else $text .= '<a href="' . $link . '" rel="' . $compatibility . $thesize . $thealbum . $displaythumb . '" title="' . $thetitle . '">'.$thetext.'</a>';
				} else if (!strlen($thethumb) && !strlen($thetype) && !$isimage) {
					if (strlen($themodule)) $text .= '<a href="' . $link . '" rel="' . $compatibility . $thesize . $thealbum . $displaythumb . $themodule . '" title="' . $thetitle . '">'.$thetitle.'</a>';
					else $text .= '<a href="' . $link . '" rel="' . $compatibility . $thesize . $thealbum . $displaythumb . '" title="' . $thetitle . '">'.$thetitle.'</a>';
				} else {
					if (strlen($thethumb) > 0) {
						if (strlen($themodule)) $text .= '<a href="' . $link . '" rel="' . $compatibility . $thesize . $thealbum . $displaythumb . $themodule . '" title="' . $thetitle . '"><img class="'. $thumb_class . '" src="' . $thethumb . '" alt="' . $thetitle . '" /></a>';
						else $text .= '<a href="' . $link . '" rel="' . $compatibility . $thesize . $thealbum . $displaythumb . '" title="' . $thetitle . '"><img class="'. $thumb_class . '" src="' . $thethumb . '" alt="' . $thetitle . '" /></a>';
					} elseif (!$thumb_custom && file_exists($thumb_url) && strlen($thethumb) > 0) {
						if (strlen($themodule)) $text .= '<a href="' . $link . '" rel="' . $compatibility . $thesize . $thealbum . $displaythumb . $themodule . '" title="' . $thetitle . '"><img class="'. $thumb_class . '" src="' . $thethumb . '" alt="' . $thetitle . '" /></a>';
						else $text .= '<a href="' . $link . '" rel="' . $compatibility . $thesize . $thealbum . $displaythumb . '" title="' . $thetitle . '"><img class="'. $thumb_class . '" src="' . $thethumb . '" alt="' . $thetitle . '" /></a>';
					} elseif (file_exists($thumb_url)) { 	
						// thumbnail exists so can do lightbox with thumbnail
						if (strlen($themodule)) $text .= '<a href="' . $link . '" rel="' . $compatibility . $thesize . $thealbum . $displaythumb . $themodule . '" title="' . $thetitle . '"><img class="'. $thumb_class . '" src="' . $thumb_url . '" alt="' . $thetitle . '" /></a>';
						else $text .= '<a href="' . $link . '" rel="' . $compatibility . $thesize . $thealbum . $displaythumb . '" title="' . $thetitle . '"><img class="'. $thumb_class . '" src="' . $thumb_url . '" alt="' . $thetitle . '" /></a>';
					} elseif (file_exists($tmp_thumb)) {
						if (strlen($themodule)) $text .= '<a href="' . $image_url . '" rel="' . $compatibility . $thesize . $thealbum . $displaythumb . $themodule . '" title="' . $thetitle . '"><img class="'. $thumb_class . '" src="' . $tmp_thumb . '" alt="' . $thetitle . '" /></a>';
						else $text .= '<a href="' . $image_url . '" rel="' . $compatibility . $thesize . $thealbum . $displaythumb . '" title="' . $thetitle . '"><img class="'. $thumb_class . '" src="' . $tmp_thumb . '" alt="' . $thetitle . '" /></a>';

					} elseif ($isimage || $thetype == 'image') {
						//try to generate thumbs
						if ($thumb_custom) $thumb_url = $tmp_thumb;
						$rd = new imgRedim(false, $improve_thumbnails, $temp_path);
						$image_filename = $image_url; // define source image here
						$output_filename = $thumb_url; // define destination image here

						$rd->loadImage($image_filename);
						$rd->redimToSize($width, $height, true);
						$rd->saveImage($output_filename, $thumb_quality);
						$text .= '<a href="' . $image_url . '" rel="' . $compatibility . $thesize . $thealbum . $displaythumb . '" title="' . $thetitle . '"><img class="'. $thumb_class . '" src="' . $thumb_url . '" alt="' . $thetitle . '" /></a>';
					}
				}

					$text .= ' ';
			}
		}
		
		return $text;
	}
}
?>
