<?php
/**************************************************
  Coppermine Photo Gallery 1.4.1 CPGMark Plugin
  *************************************************
  1.2  CPGMark
  Copyright (C) 2005 Jeff Paffett <jpaffett@yahoo.co.uk>
  *************************************************                                       //
  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.
  *************************************************
  Coppermine version: 1.4.1
  $Source: /cvsroot/cpg-contrib/CPGMark/codebase.php,v $
  $Revision: 1.8 $
  $Author: jpaffett $
  $Date: 2006/02/10 13:17:05 $
***************************************************/

if (!defined('IN_COPPERMINE')) die('Not in Coppermine...');

// Add an install action
$thisplugin->add_action('plugin_install','cpgmark_install');
$thisplugin->add_action('plugin_configure','cpgmark_configure');

// Add a action
$thisplugin->add_action('page_start','cpgmark_page_start');

// Add a filter
$thisplugin->add_filter('add_file_data','cpgmark_add_file_data');

// Install function
function cpgmark_install()
{
    // Install
    if ($_REQUEST['submit']=='Go!') {

        return true;

    // Loop again
    } else {

        return 1;
    }
}

function cpgmark_add_admin_button($href,$title,$target,$link)
{
  global $template_sys_menu, $template_sys_menu_spacer;

  $new_template=$template_sys_menu;
  $button=template_extract_block($new_template,'upload_pic');
  
   $params = array(
      '{UPL_PIC_LNK}' => $target,
      '{UPL_PIC_TITLE}' => $title,
      '{UPL_PIC_TGT}' => $href,
      'upload_pic' => $link,
   );
   $new_button="<!-- BEGIN $link -->".template_eval($button,$params)."<!-- END $link -->\n";
   template_extract_block($template_sys_menu,'upload_pic',"<!-- BEGIN upload_pic -->" . $button . "<!-- END upload_pic -->\n" .$new_button);
}

function cpgmark_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 watermark_image($CURRENT_PIC_DATA, $pid = '')
	{
	
    global $CONFIG, $CPGMARK, $lang_cpgmark, $lang_cpgmark_config, $lang_cpgmark_images, $path_to_pic;

	require ('plugins/CPGMark/include/watermark.class.php');
	
	//A bit of a hack, but pid hasn't been set by here in image upload
	if ($pid == '')
		{
		$result = cpg_db_query("SELECT MAX(pid) FROM {$CONFIG['TABLE_PREFIX']}pictures");
		
		$row = mysql_fetch_row($result);
		
		$pid = $row[0] + 1;
		}
	
	$result = cpg_db_query("SELECT * FROM {$CONFIG['TABLE_PREFIX']}mark_users WHERE user_id = {$CURRENT_PIC_DATA['owner_id']}");
		
		$row = mysql_fetch_row($result);
		
		if ($row[1] == 1 || $row[4] == 1)
		{
		
		$CPGMARK_USER['use_watermark_img'] = $row[1];
		$CPGMARK_USER['img_vpos'] = $row[2];
		$CPGMARK_USER['img_hpos'] = $row[3];
		$CPGMARK_USER['use_watermark_txt'] = $row[4];
		$CPGMARK_USER['watermark_text'] = $row[5];
		$CPGMARK_USER['watermark_text_size'] = $row[6];
		$CPGMARK_USER['large_watermark_text_size'] = $row[7];
		$CPGMARK_USER['watermark_text_color'] = $row[8];
		$CPGMARK_USER['txt_vpos'] = $row[9];
		$CPGMARK_USER['txt_hpos'] = $row[10];
		
			
			
		$result = cpg_db_query("SELECT pid FROM {$CONFIG['TABLE_PREFIX']}mark_watermark WHERE pid = $pid");
		
		if (mysql_num_rows($result) == 0)
			{
			
			if (file_exists($CONFIG['fullpath'].$CURRENT_PIC_DATA['filepath'].$CONFIG['normal_pfx'].$CURRENT_PIC_DATA['filename']))
				{
				$watermark_target = $CONFIG['fullpath'].$CURRENT_PIC_DATA['filepath'].$CONFIG['normal_pfx'].$CURRENT_PIC_DATA['filename']; 
				$watermark=new watermark($CONFIG['fullpath'].$CURRENT_PIC_DATA['filepath'].$CONFIG['normal_pfx'].$CURRENT_PIC_DATA['filename']);
				$normal_exists = true;
				}
				else
				{
				$watermark_target = $CONFIG['fullpath'].$CURRENT_PIC_DATA['filepath'].$CURRENT_PIC_DATA['filename'];
				$normal_exists = false;
				}
				
			$watermark=new watermark($watermark_target);	
			if ($CPGMARK_USER['use_watermark_img'] == 1)
				{
				if ($CPGMARK['user_watermark'] == 1)
					{
					$watermark->img_watermark=$CONFIG['fullpath'].$CPGMARK['watermark_path'].$CPGMARK['small_watermark_image'];
					}
					else
					{
					$watermark->img_watermark=$CONFIG['fullpath'].$CPGMARK['watermark_path'].$CURRENT_PIC_DATA['owner_id'].'/'.$CPGMARK['small_watermark_image'];
					}
				$watermark->img_watermark_Valing=$CPGMARK_USER['img_vpos'];
				$watermark->img_watermark_Haling=$CPGMARK_USER['img_hpos'];
				}
			if ($CPGMARK_USER['use_watermark_txt'] == 1)
				{
				$watermark->txt_watermark=$CPGMARK_USER['watermark_text'];
				$watermark->txt_watermark_color=$CPGMARK_USER['watermark_text_color'];
				$watermark->txt_watermark_font=$CPGMARK_USER['watermark_text_size'];
				$watermark->txt_watermark_Valing=$CPGMARK_USER['txt_vpos'];
				$watermark->txt_watermark_Haling=$CPGMARK_USER['txt_hpos'];
				}
			$watermark->quality=$CONFIG['jpeg_qual'];
			$watermark->process();
			
			$watermark->save($watermark_target);
			
			
			if ($normal_exists == true)
				{
				$watermark_target = $CONFIG['fullpath'].$CURRENT_PIC_DATA['filepath'].$CURRENT_PIC_DATA['filename'];
				$watermark=new watermark($watermark_target);
				if ($CPGMARK_USER['use_watermark_img'] == 1)
					{
					if ($CPGMARK['user_watermark'] == 1)
						{
						$watermark->img_watermark=$CONFIG['fullpath'].$CPGMARK['watermark_path'].$CURRENT_PIC_DATA['owner_id'].'/'.$CPGMARK['large_watermark_image'];
						}
						else
						{
						$watermark->img_watermark=$CONFIG['fullpath'].$CPGMARK['watermark_path'].$CPGMARK['large_watermark_image'];
						}
					$watermark->img_watermark_Valing=$CPGMARK_USER['img_vpos'];
					$watermark->img_watermark_Haling=$CPGMARK_USER['img_hpos'];
					}
				if ($CPGMARK['use_watermark_txt'] ==1)
					{
					$watermark->txt_watermark=$CPGMARK_USER['watermark_text'];
					$watermark->txt_watermark_color=$CPGMARK_USER['watermark_text_color'];
					$watermark->txt_watermark_font=$CPGMARK_USER['large_watermark_text_size'];
					$watermark->txt_watermark_Valing=$CPGMARK_USER['txt_vpos'];
					$watermark->txt_watermark_Haling=$CPGMARK_USER['txt_hpos'];
					}
				$watermark->quality=$CONFIG['jpeg_qual'];
				$watermark->process();
				
				$watermark->save($watermark_target);
				}
			
			cpg_db_query("INSERT INTO {$CONFIG['TABLE_PREFIX']}mark_watermark SET pid = $pid, watermarked = 1");
				
			}
		}
	
	}

function cpgmark_add_file_data($picdata)
{

global $CPGMARK;

	if ($CPGMARK['upload_mark'] == 1)
		{
		watermark_image($picdata);
		}
	
	return $picdata;
}

function cpgmark_page_start()
{
  
  	global $template_sys_menu, $template_sys_menu_spacer, $template_sys_menu_button, $sys_menu_buttons;
    global $CONFIG, $CPGMARK, $lang_cpgmark, $lang_cpgmark_config, $lang_cpgmark_admin, $lang_cpgmark_images, $path_to_pic;

  	require ('plugins/CPGMark/include/init.inc.php');
  	  	
	if (USER_ID)
		{
  		cpgmark_add_admin_button('index.php?file=CPGMark/cpgmark_admin','CPGMark','CPGMark','CPGMark');
  		}
  		
  		cpgmark_add_config_button('index.php?file=CPGMark/cpgmark_config',$lang_cpgmark['config_title'],'',$lang_cpgmark['config_title']);
  	
  	if (defined('DISPLAYIMAGE_PHP') && ($CPGMARK['display_image_mark'] == 1))
		{
		
		$pos = isset($_GET['pos']) ? (int)$_GET['pos'] : 0;
	
		$pid = isset($_GET['pid']) ? (int)$_GET['pid'] : 0;
		
		$cat = isset($_GET['cat']) ? (int)$_GET['cat'] : 0;
		$album = isset($_GET['album']) ? $_GET['album'] : '';
		
		// Retrieve data for the current picture
		if ($pos < 0 || $pid > 0) {
			$pid = ($pos < 0) ? -$pos : $pid;
			$result = cpg_db_query("SELECT aid from {$CONFIG['TABLE_PICTURES']} WHERE pid='$pid' $ALBUM_SET LIMIT 1");
			if (mysql_num_rows($result) == 0) cpg_die(ERROR, $lang_errors['non_exist_ap'], __FILE__, __LINE__);
			$row = mysql_fetch_array($result);
			$album = $row['aid'];
			$pic_data = get_pic_data($album, $pic_count, $album_name, -1, -1, false);
			for($pos = 0; $pic_data[$pos]['pid'] != $pid && $pos < $pic_count; $pos++);
			$pic_data = get_pic_data($album, $pic_count, $album_name, $pos, 1, false);
			$CURRENT_PIC_DATA = $pic_data[0];
		
		} elseif (isset($_GET['pos'])) {
			$pic_data = get_pic_data($album, $pic_count, $album_name, $pos, 1, false);
			if ($pic_count == 0) {
				cpg_die(INFORMATION, $lang_errors['no_img_to_display'], __FILE__, __LINE__);
			} elseif (count($pic_data) == 0 && $pos >= $pic_count) {
				$pos = $pic_count - 1;
				$human_pos = $pos + 1;
				$pic_data = get_pic_data($album, $pic_count, $album_name, $pos, 1, false);
			}
		
		$CURRENT_PIC_DATA = $pic_data[0];
		
		$pid = $CURRENT_PIC_DATA['pid'];
		
		watermark_image($CURRENT_PIC_DATA, $pid);
		
		}
		
		
	}

 }
 
 // Configure function
// Displays the form
function cpgmark_configure($stop=true)
{
    global $errors, $CONFIG;
    require ('include/sql_parse.php');

    $db_update = 'plugins/CPGMark/sql/basic.sql';
    $sql_query = fread(fopen($db_update, 'r'), filesize($db_update));
    // Update table prefix
    $sql_query = preg_replace('/CPG_/', $CONFIG['TABLE_PREFIX'], $sql_query);

    $sql_query = remove_remarks($sql_query);
    $sql_query = split_sql_file($sql_query, ';');
    

    ?>
        <h2>Performing Database Updates<h2>
        <table class="maintable">

    <?php

    foreach($sql_query as $q) {
        echo "<tr><td class='tableb'>$q</td>";
        if (mysql_query($q)) {
            echo "<td class='updatesOK'>OK</td></tr>";
        } else {
            echo "<td class='updatesFail'>Already Done</td></tr>";
        }
    }

    echo "</table>";
    
    if ($stop) {
        echo <<< EOT

        <form action="{$_SERVER['REQUEST_URI']}" method="post">
            <input type="submit" value="Go!" name="submit" />
        </form>
EOT;
    }

}

?>