<?php
/**************************************************
  Coppermine 1.4 Plugin - Restrict Access
  *************************************************
  Copyright (c) 2006 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_restrictaccess, $lang_plugin_restrictaccess_config;
require('include/init.inc.php');

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

require('plugins/restrict_access/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_restrictaccess_allowedgroups'";
		cpg_db_query($sql1);
		if ($CONFIG['log_mode'] == CPG_LOG_ALL) {
			log_write('CONFIG UPDATE SQL: '.$sql1.";\n".
				'TIME: '.date("F j, Y, g:i a")."\n".
				'USER: '.$USER_DATA['user_name'],
				CPG_DATABASE_LOG
				);
		}
	}
        pageheader($lang_plugin_restrictaccess['display_name']);
        msg_box($lang_plugin_restrictaccess['display_name'], $lang_plugin_restrictaccess['page_success'], $lang_continue, 'index.php');
	pagefooter();
        exit;
}

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

// default parameters
$allowed_group_list = '';

// pull in current values
$result = cpg_db_query("SELECT name, value FROM {$CONFIG['TABLE_CONFIG']} where name REGEXP '^plugin_restrictaccess_'");
$params = cpg_db_fetch_rowset($result);
mysql_free_result($result);
foreach($params as $param) {
	if($param['name'] == 'plugin_restrictaccess_allowedgroups') { $allowed_group_list = $param['value']; }
}
echo <<< EOT
	<h3>{$lang_plugin_restrictaccess_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 (!in_array($group['group_id'],array(1,2,3,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";
	}
}
echo <<< EOT
	$group_cb<br />
	<br />
	<input type="submit" value="{$lang_plugin_restrictaccess_config['button_done']}" name="update_config"/>
	</form>
EOT;

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