<?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.
  *************************************************/

global $CONFIG, $lang_plugin_controlfullsize, $lang_plugin_controlfullsize_config;
require('include/init.inc.php');

if (!GALLERY_ADMIN_MODE) cpg_die(ERROR, $lang_errors['access_denied'], __FILE__, __LINE__);

require('plugins/control_fullsize/include/init.inc.php');

if (count($_POST) > 0) {
	if (isset($_POST['update_config'])) {

		$group_list = isset($_POST['group_list']) ? $_POST['group_list'] : '';
		if (is_array($group_list)) {
			$allowed_group_list = implode(',',$group_list);
		} else {
			$allowed_group_list = '';
		}
		if ($_POST['displaynotice']) { $displaynotice = '1'; }
		else { $displaynotice = '0'; }

		$sql1 = "UPDATE {$CONFIG['TABLE_CONFIG']} SET value = '$allowed_group_list' WHERE name = 'plugin_controlfullsize_allowedgroups'";
		$sql2 = "UPDATE {$CONFIG['TABLE_CONFIG']} SET value = '{$_POST['requsercomment']}' WHERE name = 'plugin_controlfullsize_requsercomment'";
		$sql3 = "UPDATE {$CONFIG['TABLE_CONFIG']} SET value = '{$_POST['conditionoperator']}' WHERE name = 'plugin_controlfullsize_conditionoperator'";
		$sql4 = "UPDATE {$CONFIG['TABLE_CONFIG']} SET value = '$displaynotice' WHERE name = 'plugin_controlfullsize_displaynotice'";
		$sql5 = "UPDATE {$CONFIG['TABLE_CONFIG']} SET value = '{$_POST['noticemessage']}' WHERE name = 'plugin_controlfullsize_noticemessage'";
		$sql6 = "UPDATE {$CONFIG['TABLE_CONFIG']} SET value = '{$_POST['noticemessanon']}' WHERE name = 'plugin_controlfullsize_noticemessanon'";
		cpg_db_query($sql1);
		cpg_db_query($sql2);
		cpg_db_query($sql3);
		cpg_db_query($sql4);
		cpg_db_query($sql5);
		cpg_db_query($sql6);
		if ($CONFIG['log_mode'] == CPG_LOG_ALL) {
			log_write('CONFIG UPDATE SQL: '.$sql1.";\n".$sql2.";\n".$sql3.";\n".$sql4.";\n".$sql5.";\n".$sql6.
				'TIME: '.date("F j, Y, g:i a")."\n".
				'USER: '.$USER_DATA['user_name'],
				CPG_DATABASE_LOG
				);
		}
	}
        pageheader($lang_plugin_controlfullsize['display_name']);
        msg_box($lang_plugin_controlfullsize['display_name'], $lang_plugin_controlfullsize['page_success'], $lang_continue, 'index.php');
	pagefooter();
        exit;
}

pageheader($lang_plugin_controlfullsize['display_name']);
starttable('100%', $lang_plugin_controlfullsize['display_name'].' - <a href="pluginmgr.php" class="admin_menu">Plugin Manager</a>', 3);
echo '<tr><td>'."\n";

// default parameters
$allowed_group_list = '';
$requsercomment = 0;
$conditionoperator = "AND";
$displaynotice = 0;
$noticemessage = $lang_plugin_controlfullsize_config['noticemessage_default'];
$noticemessanon = $lang_plugin_controlfullsize_config['noticemessage_default_anon'];

// pull in current values
$result = cpg_db_query("SELECT name, value FROM {$CONFIG['TABLE_CONFIG']} where name REGEXP '^plugin_controlfullsize_'");
$params = cpg_db_fetch_rowset($result);
mysql_free_result($result);
foreach($params as $param) {
	if($param['name'] == 'plugin_controlfullsize_allowedgroups') { $allowed_group_list = $param['value']; }
	elseif($param['name'] == 'plugin_controlfullsize_requsercomment') { $requsercomment = $param['value']; }
	elseif($param['name'] == 'plugin_controlfullsize_conditionoperator') { $conditionoperator = $param['value']; }
	elseif($param['name'] == 'plugin_controlfullsize_displaynotice') { $displaynotice = $param['value']; }
	elseif($param['name'] == 'plugin_controlfullsize_noticemessage') { $noticemessage = $param['value']; }
	elseif($param['name'] == 'plugin_controlfullsize_noticemessanon') { $noticemessanon = $param['value']; }
}
echo <<< EOT
	<h3>{$lang_plugin_controlfullsize_config['select_groups']}:</h3>
	<form action="{$_SERVER['REQUEST_URI']}" method="post">
EOT;
$sql = "SELECT group_id, group_name FROM {$CONFIG['TABLE_USERGROUPS']} ORDER BY group_name";
$result = cpg_db_query($sql);
$group_list = cpg_db_fetch_rowset($result);
mysql_free_result($result);
$group_cb = '';
foreach($group_list as $group) {
	if ($group['group_id'] != 1 && $group['group_id'] != 4) {
		$checked = strpos(',,'.$allowed_group_list.',' , ','.$group['group_id'].',') ? 'checked' : '';
		$group_cb .= '<input name="group_list[]" type="checkbox" value="' . $group['group_id'] . '" ' . $checked . ' />' . $group['group_name'] . "<br />\n";
	}
}
$op_and_selected = '';
$op_or_selected = '';
if ($conditionoperator == 'AND') { $op_and_selected = "selected"; }
if ($conditionoperator == 'OR') { $op_or_selected = "selected"; }
$requser_yes_selected = '';
$requser_no_selected = '';
if ($requsercomment == '0') { $requser_no_selected = "selected"; }
if ($requsercomment == '1') { $requser_yes_selected = "selected"; }
$displaynotice_selected = ($displaynotice == '1') ? 'checked' : '';
echo <<< EOT
	$group_cb<br />
	{$lang_plugin_controlfullsize_config['note_admin']}<br />
	<br />
	<select name="conditionoperator"><option value="AND" $op_and_selected>{$lang_plugin_controlfullsize_config['op_and']}</option>
		<option value="OR" $op_or_selected>{$lang_plugin_controlfullsize_config['op_or']}</option></select>
	{$lang_plugin_controlfullsize_config['require_user']} -->
	<select name="requsercomment"><option value="1" $requser_yes_selected>{$lang_plugin_controlfullsize_config['choice_yes']}</option>
		<option value="0" $requser_no_selected>{$lang_plugin_controlfullsize_config['choice_no']}</option</select><br />
	<br />
	<input type="checkbox" value="1" name="displaynotice" $displaynotice_selected> {$lang_plugin_controlfullsize_config['display_notice']} : <br />
	&nbsp; &nbsp; &nbsp; {$lang_plugin_controlfullsize_config['notice_loggedin']}: <input type="text" name="noticemessage" value="$noticemessage" size="80"><br />
	&nbsp; &nbsp; &nbsp; {$lang_plugin_controlfullsize_config['notice_anon']}: <input type="text" name="noticemessanon" value="$noticemessanon" size="80"><br />
	<br />
	<input type="submit" value="{$lang_plugin_controlfullsize_config['button_done']}" name="update_config"/>
	</form>
EOT;

echo '</td></tr>'."\n";
endtable();
pagefooter();
ob_end_flush();
?>
