<?php
/**************************************************
  Coppermine 1.4.x Plugin - HighSlide
  *************************************************
  Copyright (c) 2006 Borzoo Mossavari
  *************************************************
  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.
  *************************************************
  Skip Intermediate Page and show full page on the page
  Based on Highslide JS @ http://vikjavev.no/highslide/ 
  ***************************************************/

if (!defined('IN_COPPERMINE')) die('Not in Coppermine...');

// Add an install & configure & uninstall actions
$thisplugin->add_action('plugin_install','hs_install');
$thisplugin->add_action('plugin_configure','hs_configure');
$thisplugin->add_action('plugin_uninstall','hs_uninstall');
$thisplugin->add_action('plugin_cleanup','hs_cleanup');


// Add a configure action
$thisplugin->add_action('page_start','hs_temp');

// Add a filter for the page HTML
$thisplugin->add_filter('page_html','hs_main');
// Installation Function
function hs_install() 
{
	global $CONFIG, $thisplugin, $lang_plugin_highslide_install;
	require ('plugins/highslide/include/init.inc.php');
	if ($_POST['submit']==$lang_plugin_highslide_install['button_install']) {
		if(!isset($CONFIG['highslide_enable'])) {
			require 'include/sql_parse.php';
			$query="INSERT INTO ".$CONFIG['TABLE_CONFIG']." VALUES ('highslide_enable', '1');";
			cpg_db_query($query);

			// create table	
			$db_schema = $thisplugin->fullpath . '/schema.sql';
			$sql_query = fread(fopen($db_schema, 'r'), filesize($db_schema));
			$sql_query = preg_replace('/CPG_/', $CONFIG['TABLE_PREFIX'], $sql_query);

			$sql_query = remove_remarks($sql_query);
			$sql_query = split_sql_file($sql_query, ';');
		
			foreach($sql_query as $q) { 
				cpg_db_query($q);
			}
			// Put default setting
			$db_schema = $thisplugin->fullpath . '/basic.sql';
			$sql_query = fread(fopen($db_schema, 'r'), filesize($db_schema));
			$sql_query = preg_replace('/CPG_/', $CONFIG['TABLE_PREFIX'], $sql_query);

			$sql_query = remove_remarks($sql_query);
			$sql_query = split_sql_file($sql_query, ';');
		
			foreach($sql_query as $q) { 
				cpg_db_query($q);
			}
		}
		return true;
	} else {
		return 1;
	}
}

// Configure Plugin
function hs_configure() 
{
	global $CONFIG, $lang_plugin_highslide_install;
	require ('plugins/highslide/include/init.inc.php');

	echo <<< EOT
		<h2>{$lang_plugin_highslide_install['install_click']}</h2>
		{$lang_plugin_highslide_install['install_note']}<br />
		<br />
		<form action="{$_SERVER['REQUEST_URI']}" method="post">
		<input type="submit" value="{$lang_plugin_highslide_install['button_install']}" name="submit" />
		</form>
EOT;
}

// Uninstall
function hs_uninstall()
{
	global $CONFIG, $thisplugin;
	
	if (!isset($_POST['drop'])) return 1;
	
	if ($_POST['drop']) {
		cpg_db_query("DROP TABLE IF EXISTS {$CONFIG['TABLE_HIGHSLIDE_CONFIG']}");
		cpg_db_query("DELETE FROM {$CONFIG['TABLE_CONFIG']} WHERE name='highslide_enable';");
   	}
	return true;
}

// Ask if admin wants to drop the table
function hs_cleanup($action) 
{
    global $lang_plugin_highslide_install;
    require ('plugins/highslide/include/init.inc.php');

    if ($action===1) {
        echo <<< EOT
    <form action="{$_SERVER['REQUEST_URI']}" method="post">
        <p>
            {$lang_plugin_highslide_install['cleanup_question']}
        </p>
        <div style="margin:25;">
        <table border="0" cellspacing="0" cellpadding="0">
            <tr>
                <td><input type="radio" name="drop" value="1" /></td>
                <td>Yes</td>
            </tr>
            <tr>
                <td><input type="radio" name="drop" checked="checked" value="0" /></td>
                <td>No</td>
            </tr>
        </table>
        </div>
        <span>
           <input type="submit" name="submit" value="{$lang_plugin_highslide_install['button_submit']}" /> &nbsp;&nbsp;&nbsp;
            <input type="button" name="cancel" onClick="window.location='pluginmgr.php';" value="{$lang_plugin_highslide_install['button_cancel']}" />
        </span>
    </form>
EOT;
    }
}

// add config button
function hs_add_config_button($href,$title,$target,$link)
{
  global $template_gallery_admin_menu;

  $new_template = $template_gallery_admin_menu;
  $button = template_extract_block($new_template,'documentation');
  $params = array(
      '{DOCUMENTATION_HREF}' => $href,
      '{DOCUMENTATION_TITLE}' => $title,
      'target="cpg_documentation"' => $target,
      '{DOCUMENTATION_LNK}' => $link,
   );
   $new_button="<!-- BEGIN $link -->".template_eval($button,$params)."<!-- END $link -->\n";
   template_extract_block($template_gallery_admin_menu,'documentation',"<!-- BEGIN documentation -->" . $button . "<!-- END documentation -->\n" . $new_button);
}

//function to cahnge thumbnail template and load some setting at page start
function hs_temp() {
	global $CONFIG, $template_thumbnail_view, $lang_plugin_highslide, $lang_plugin_highslide_config, $lang_plugin_highslide_install, $HIGHSLIDESET, $lang_plugin_highslide_js;
	
	require ('plugins/highslide/include/init.inc.php');
	require ('plugins/highslide/include/init2.inc.php');

	$CONFIG['TABLE_HIGHSLIDE_CONFIG'] = $CONFIG['TABLE_PREFIX'].'highslide_config';

	if (GALLERY_ADMIN_MODE) {
		hs_add_config_button('index.php?file=highslide/plugin_config',$lang_plugin_highslide_config['config_title'],'',$lang_plugin_highslide_config['config_button']);
	}
if (($HIGHSLIDESET['admin_show'] == 1 && !GALLERY_ADMIN_MODE ) || ($HIGHSLIDESET['admin_show'] == 0)) {
	$template_thumbnail_view = <<<EOT

<!-- BEGIN header -->
        <tr>
<!-- END header -->
<!-- BEGIN thumb_cell -->
        <td valign="top" class="thumbnails" width ="{CELL_WIDTH}" align="center">
                <table width="100%" cellpadding="0" cellspacing="0">
                        <tr>
                                <td align="center">
                                        <!-- BEGIN hs_thumb_cell_init --><a href="{LINK_TGT}">{THUMB}<br /></a><!-- END hs_thumb_cell_init -->
                                        {CAPTION}
                                        {ADMIN_MENU}
                                </td>
                        </tr>
                </table>
        </td>
<!-- END thumb_cell -->
<!-- BEGIN empty_cell -->
                <td valign="top" class="thumbnails" align="center">&nbsp;</td>
<!-- END empty_cell -->
<!-- BEGIN row_separator -->
        </tr>
        <tr>
<!-- END row_separator -->
<!-- BEGIN footer -->
        </tr>
<!-- END footer -->
<!-- BEGIN tabs -->
        <tr>
                <td colspan="{THUMB_COLS}" style="padding: 0px;">
                        <table width="100%" cellspacing="0" cellpadding="0">
                                <tr>
                                       {TABS}
                                </tr>
                        </table>
                </td>
        </tr>
<!-- END tabs -->
<!-- BEGIN spacer -->
        <img src="images/spacer.gif" width="1" height="7" border="" alt="" /><br />
<!-- END spacer -->

EOT;
}
}

// main function to modify page html
function hs_main($html,$HIGHSLIDESET) {
    global $thisplugin, $thumb_list, $HIGHSLIDESET, $lang_plugin_highslide, $lang_plugin_highslide_js;
if (($HIGHSLIDESET[admin_show] == 1 && !GALLERY_ADMIN_MODE ) || ($HIGHSLIDESET[admin_show] == 0)) {
	$hsinit = <<< EOT
	<link rel="stylesheet" href="plugins/highslide/include/highslide.css" type="text/css" />
	<script type="text/javascript" src="scripts.js"></script>
	<script type="text/javascript" src="plugins/highslide/highslide.js"></script>
	<style type="text/css">
	.highslide {
		cursor: url('plugins/highslide/graphics/zoomin.cur'), pointer;
    	outline: none;
	}
	.highslide-image {
  	  cursor: pointer; /* opera */
		cursor: url('plugins/highslide/graphics/zoomout.cur'), pointer;
   	 border: 2px solid white;
	}
	.highslide-loading {
 	   display: block;
		color: white;
		font-style: 'MS Sans Serif';
		font-size: 9px;
		font-weight: bold;
		text-transform: uppercase;
		text-decoration: none;
		padding: 3px;
		opacity: 0.60; /* w3c */
		filter: alpha(opacity=60); /* ie */
		border-top: 1px solid white;
		border-bottom: 1px solid white;
		background-color: black;
		
		padding-left: 22px;
		background-image: url('plugins/highslide/graphics/loader.gif');
		background-repeat: no-repeat;
		background-position: 3px 1px;
		
	}
	</style>
EOT;
	$hsinit .='<script type="text/javascript">    
	hs.cpghs_graphicsDir = \'plugins/highslide/graphics/\';
	hs.cpghs_loadingText = \''.$lang_plugin_highslide_js['loading_text'].'\';
	hs.cpghs_loadingTitle = \''.$lang_plugin_highslide_js['loading_title'].'\';
	';
	switch($HIGHSLIDESET[style_mod]){
		case 1:
		$hsinit_b = 'hs.cpghs_outlineType = \'rounded-white\';'; 
		break;
		case 2:
		$hsinit_b = ''; 
		break;
		case 3:
		$hsinit_b = 'hs.cpghs_outlineType = \'outer-glow\';'; 
		break;
		case 4:
		$hsinit_b = 'hs.cpghs_outlineType = null;';
		break; 
		case 5:
		$hsinit_b = '';
		break; 
		default:
		$hsinit_b = 'hs.cpghs_outlineType = \'rounded-white\';'; 
	}
 
	$hsinit .= $hsinit_b.'
	window.onload = function() {
		 hs.cpghs_preloadImages();
	  }
	</script>';
	$exper = '#(<\s*script\s*type\s*=\s*"text/javascript"\s*src\s*=\s*"scripts.js"\s*>\s*</script>)#i';
	$html = preg_replace($exper,$hsinit,$html);
	$exper = '#<body(.*)>#';
	preg_match($exper,$html,$bodies);
	$html = str_replace($bodies[0],'<body'.$bodies[1].'><div id="highslide-container"></div>',$html);
	if($HIGHSLIDESET[index_only] == 0 && $HIGHSLIDESET[sef] == 0){// check if plugin apply on index only or on all pages
		$exper = '#<!-- BEGIN hs_thumb_cell_init -->\s*<a\s*href=\"(displayimage\.php\?.*)\">\s*<img\s*src=\"(.*?)thumb_(.*?)\"\s*.*\s*alt=\"(.*?)\"\s*title=\"(.*\n.*\n.*\n.*?)\"\s*/><br /></a><!-- END hs_thumb_cell_init -->#i'; 
	}else if ($HIGHSLIDESET[index_only] == 1 && $HIGHSLIDESET[sef] == 0){
		$exper = '#<!-- BEGIN hs_thumb_cell_init -->\s*<a\s*href=\"(displayimage\.php\?album=.+cat=.+pos=.+)\">\s*<img\s*src=\"(.*?)thumb_(.*?)\"\s*.*\s*alt=\"(.*?)\"\s*title=\"(.*\n.*\n.*\n.*?)\"\s*/><br /></a><!-- END hs_thumb_cell_init -->#i'; 
	
	}else if ($HIGHSLIDESET[index_only] == 0 && $HIGHSLIDESET[sef] == 1){
		$exper = '#<!-- BEGIN hs_thumb_cell_init -->\s*<a\s*href=\"(displayimage-.*-.*-.*)\">\s*<img\s*src=\"(.*?)thumb_(.*?)\"\s*.*\s*alt=\"(.*?)\"\s*title=\"(.*\n.*\n.*\n.*?)\"\s*/><br /></a><!-- END hs_thumb_cell_init -->#i'; 

	} else {
		$exper = '#<!-- BEGIN hs_thumb_cell_init -->\s*<a\s*href=\"(displayimage-.*-.*)\">\s*<img\s*src=\"(.*?)thumb_(.*?)\"\s*.*\s*alt=\"(.*?)\"\s*title=\"(.*\n.*\n.*\n.*?)\"\s*/><br /></a><!-- END hs_thumb_cell_init -->#i'; 

	}
	preg_match_all($exper, $html,$matches,PREG_SET_ORDER);
	foreach($matches as $match) {
		//File Type dettection
		$hs_filetype = explode(".",$match[4]);
		if( $hs_filetype[1] == 'jpg' || $hs_filetype[1] == 'jpeg' || $hs_filetype[1] == 'jpe'|| $hs_filetype[1] == 'png'|| $hs_filetype[1] == 'gif'|| $hs_filetype[1] == 'bmp'|| $hs_filetype[1] == 'jpc' || $hs_filetype[1] == 'jp2' || $hs_filetype[1] == 'jpx' || $hs_filetype[1] == 'jb2'|| $hs_filetype[1] == 'swc' || $hs_filetype[1] == 'JPG' || $hs_filetype[1] == 'JPEG' || $hs_filetype[1] == 'JPE' || $hs_filetype[1] == 'PNG' || $hs_filetype[1] == 'GIF' || $hs_filetype[1] == 'BMP' || $hs_filetype[1] == 'JPC' || $hs_filetype[1] == 'JP2' || $hs_filetype[1] == 'JPX' || $hs_filetype[1] == 'SWC' ){
			$test_image=$match[2]."normal_".$match[3];
			if($HIGHSLIDESET[full_image]==1){
				$rep_str = '<!-- BEGIN hs_thumb_cell --><a href="'.$match[2].$match[3].'" class="highslide" onclick="return hs.cpghs_expand(this)"><img src="'.$match[2].'thumb_'.$match[3].'" class="image" id="'.$match[3].'" border="0" alt="'.$match[4].'" title="'.$match[5].'"/><br /></a>';
			}else if(my_is_file($test_image)){
				$rep_str = '<!-- BEGIN hs_thumb_cell --><a href="'.$match[2]."normal_".$match[3].'" class="highslide" onclick="return hs.cpghs_expand(this)"><img src="'.$match[2].'thumb_'.$match[3].'" class="image" id="'.$match[3].'" border="0" alt="'.$match[4].'" title="'.$match[5].'"/><br /></a>';
			}else {
				$rep_str = '<!-- BEGIN hs_thumb_cell --><a href="'.$match[2].$match[3].'" class="highslide" onclick="return hs.cpghs_expand(this)"><img src="'.$match[2].'thumb_'.$match[3].'" class="image" id="'.$match[3].'" border="0" alt="'.$match[4].'" title="'.$match[5].'"/><br /></a>';
			}
			if($HIGHSLIDESET[detail]==1 || $HIGHSLIDESET[close]==1 || $HIGHSLIDESET[title]==1){
				if($HIGHSLIDESET[title]==1)
					$rep_str.='<div class="highslide-caption" id="caption-for-'.$match[3].'" >';
				else
					$rep_str.='<div class="highslide-caption" style="height:15px;" id="caption-for-'.$match[3].'" >';
			}
			if($HIGHSLIDESET[detail]==1){ // add link to intermadiate
				$rep_str.='<div onclick="location.href=\''.$match[1].'\'" class="highslide_dtails"  onmouseover="this.className=\'highslide_dtails_over\'"  onmouseout="this.className=\'highslide_dtails\'" >'.$lang_plugin_highslide['detail'].'</div>';
			}
			if($HIGHSLIDESET[title]==1){ // add title to caption
			$rep_str .= '<div align="center" class="thumb_caption" >'.$match[5].'</div>';
			}
			if($HIGHSLIDESET[close]==1){ // add link to close
				if($HIGHSLIDESET[title]==1)
					$rep_str.='<div onclick="hs.cpghs_closeId(\''.$match[3].'\')" align="right" class="highslide_dtails"  onmouseover="this.className=\'highslide_dtails_over\'" onmouseout="this.className=\'highslide_dtails\'" >'.$lang_plugin_highslide['close'].'</div>';
				else
					$rep_str.='<div onclick="hs.cpghs_closeId(\''.$match[3].'\')" align="right" class="highslide_dtails" style="float:right;"  onmouseover="this.className=\'highslide_dtails_over\'" onmouseout="this.className=\'highslide_dtails\'" >'.$lang_plugin_highslide['close'].'</div>';
			}
			if($HIGHSLIDESET[detail]==1 || $HIGHSLIDESET[close]==1 || $HIGHSLIDESET[title]==1){
				$rep_str.='</div>';
			}
			$rep_str.='<!-- END hs_thumb_cell -->';
			$html = str_replace($match[0],$rep_str,$html);
	 	}
 	 }
	}
	return $html;
}
// cheking the file (Intermadiate) to be available
function my_is_file($file)
{
    if(is_file($file))
		return true;
	else
		return false;
}
?>
