<?php
/**************************************************
  Coppermine 1.4.2 Plugin - Full-Size Photos Access
  *************************************************
  Copyright (c) 2005 Paul Van Rompay
  *************************************************
  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.
  *************************************************/

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

// Add an install & configure & uninstall actions
$thisplugin->add_action('plugin_install','controlfullsize_install');
$thisplugin->add_action('plugin_configure','controlfullsize_configure');
$thisplugin->add_action('plugin_uninstall','controlfullsize_uninstall');

// Add the main filter - validate access to full-size photos
$thisplugin->add_filter('file_data','controlfullsize_validate');

// Add actions
$thisplugin->add_action('page_start','controlfullsize_page_start');

// Install Plugin
function controlfullsize_install() {
	global $CONFIG, $lang_plugin_controlfullsize, $lang_plugin_controlfullsize_config;
	require ('plugins/control_fullsize/include/init.inc.php');
	
	if ($_POST['submit']==$lang_plugin_controlfullsize_config['button_done']) {
		return true;
	} else {
		return 1;
	}
}

// Configure Plugin
function controlfullsize_configure() {
	global $CONFIG, $lang_plugin_controlfullsize, $lang_plugin_controlfullsize_config;
	require ('plugins/control_fullsize/include/init.inc.php');

	// define('PLUGIN_CONTROLFULLSIZE_CONFIGURE','1');  // not used yet

	// delete previous plugin config options if necessary (just in case)
	cpg_db_query("DELETE FROM {$CONFIG['TABLE_CONFIG']} WHERE name REGEXP '^plugin_controlfullsize_'");

	// insert default parameters into configuration table
	$sql = "INSERT INTO {$CONFIG['TABLE_CONFIG']} (name, value)"
		." VALUES"
		." ('plugin_controlfullsize_allowedgroups','')"
		.",('plugin_controlfullsize_requsercomment','0')"
		.",('plugin_controlfullsize_conditionoperator','AND')"
		.",('plugin_controlfullsize_displaynotice','0')"
		.",('plugin_controlfullsize_noticemessage','{$lang_plugin_controlfullsize_config['noticemessage_default']}')"
		.",('plugin_controlfullsize_noticemessanon','{$lang_plugin_controlfullsize_config['noticemessage_default_anon']}')";
	cpg_db_query($sql);

	echo <<< EOT
		<h2>{$lang_plugin_controlfullsize['install_done']}</h2>
		{$lang_plugin_controlfullsize['install_note']}<br />
		<br />
		<form action="{$_SERVER['REQUEST_URI']}" method="post">
		<input type="submit" value="{$lang_plugin_controlfullsize_config['button_done']}" name="submit"/>
		</form>
EOT;
}

// Uninstall Plugin
function controlfullsize_uninstall() {
	global $CONFIG;
	cpg_db_query("DELETE FROM {$CONFIG['TABLE_CONFIG']} WHERE name REGEXP '^plugin_controlfullsize_'");
	return true;
}

// add config button
function controlfullsize_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);
}

// add admin button to start of each page
function controlfullsize_page_start()
{
	global $CONFIG, $lang_plugin_controlfullsize, $lang_plugin_controlfullsize_config;
	require ('plugins/control_fullsize/include/init.inc.php');

	if (GALLERY_ADMIN_MODE) {
		controlfullsize_add_config_button('index.php?file=control_fullsize/plugin_config',$lang_plugin_controlfullsize['config_title'],'',$lang_plugin_controlfullsize['config_button']);
	}
}

// Validate access to full-size photos and remove link to full-size photos if not allowed 
function controlfullsize_validate($cpicdata) {
	global $CONFIG, $USER;
	$usergroups = explode(',',substr(USER_GROUP_SET,1,-1));
	$allowedgroups = explode(',',$CONFIG['plugin_controlfullsize_allowedgroups']);
	$validuser = array_intersect($allowedgroups,$usergroups);
	$usercomment = false;
	if ($CONFIG['plugin_controlfullsize_requsercomment']) {
		$result = cpg_db_query("SELECT msg_id FROM {$CONFIG['TABLE_COMMENTS']} where (pid='{$cpicdata['pid']}' && author_id='".USER_ID."')");
		if (mysql_num_rows($result) > 0) { 
			$usercomment = true;
		}
		if ($CONFIG['plugin_controlfullsize_conditionoperator'] == 'OR') {
			$validuser = $validuser || $usercomment;
		} else {
			$validuser = $validuser && $usercomment;
		}
	}
	if (!$validuser && !GALLERY_ADMIN_MODE) {
		if(strpos($cpicdata['html'],'fullsize=1') !== false) {
			$cpicdata['html'] = strip_tags($cpicdata['html'],'<img>');
			if ($CONFIG['plugin_controlfullsize_displaynotice']) { 
				if (USER_ID) {
					$alt_text = $CONFIG['plugin_controlfullsize_noticemessage']; 
				} else {
					$alt_text = $CONFIG['plugin_controlfullsize_noticemessanon'];
				}
			} else { 
				$alt_text = ''; 
			}
			$cpicdata['html'] = preg_replace('/alt=\".*\"/i','alt="'.$alt_text.'" title="'.$alt_text.'"',$cpicdata['html']);
		}
	}
	return $cpicdata;
}

?>
