<?php
////////////////////////////////////////////////////////////////////////
// Restrict Page Access //
// Breeze Website Builder
// Copyright Silver Dolphin Solutions, LLC, Edward Lemmers, P.E.
// For licensing, see license.txt.
// Created: 4/9/2010
// Revised: 2/10/2012	ELL
// Added table prefix handling.
// Revised: 5/1/2017	ELL
// Changed from defining pages as either being editable by restricted users or not to ability to assign which restricted users have ability to edit any particular page.
	
	include ("check_session.php");
	
	$AccessLevel = $_REQUEST['AccessLevel'];

	$query = "SELECT page_name FROM " . BWB_TABLE_PREFIX . "page_data";
	$result = $bwb_dbh->query($query);
	$page_users = array();
	while($row = $result->fetch(PDO::FETCH_ASSOC))
	{
		$page_name = $row["page_name"];
		if (!isset($_POST[$page_name]))
			$page_users_str == "";
		else
			$page_users_str = $_POST[$page_name];

		// Repack in case somebody left extra commas or something.
		$users = explode(",", $page_users_str);
		$comma = "";
		$page_users_str = "";
		foreach ($users as $user)
		{
			if ($user != "")
				$page_users_str .= $comma . $user;
			$comma = ",";
		}
		
		$page_users[$page_name] = $page_users_str;
	}

	$query = "SELECT UserID FROM " . BWB_TABLE_PREFIX . "users WHERE AccessLevel = '" . $AccessLevel . "'";
	$result = $bwb_dbh->query($query);
	while($row = $result->fetch(PDO::FETCH_ASSOC))
	{
		$UserID = $row['UserID'];
		$users_pages = "";
		$comma = "";
		foreach ($page_users as $page => $page_users_str)
		{
			$users = explode(",", $page_users_str);
			$users_pages_str = "";
			foreach ($users as $user)
			{
				if (($user == $UserID) && ($user != ""))	{
					$users_pages .= $comma . $page;
					$comma = ",";
				}
			}
		}
	
		if ($users_pages != "")
			$query = "UPDATE " . BWB_TABLE_PREFIX . "users SET pages = '" . $users_pages . "' WHERE UserID = '" . $UserID . "'";
		else
			$query = "UPDATE " . BWB_TABLE_PREFIX . "users SET pages = NULL WHERE UserID = '" . $UserID . "'";
		$cnt = $bwb_dbh->exec($query);
	}
	
	echo 1;
?>