<?php
/*
 * This file is part of the easy-CMS project.
 * easy-CMS 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.
 *
 * easy-CMS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with easy-CMS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

// define the path context
chdir('..');
define('EASY_GUI_APP_DIR', '../');


include_once('config.inc.php');
include_once('classes/core/Site.class.php');

include_once('interface/egScript.class.php');
include_once('interface/egManager.class.php');
include_once('interface/egContainer.class.php');
include_once('interface/egMenuItem.class.php');
include_once('interface/egFrame.class.php');
include_once('interface/egToolTip.class.php');
include_once('interface/egRichText.class.php');
include_once('interface/egLink.class.php');
include_once('interface/egStatic.class.php');
include_once('interface/egEdit.class.php');
include_once('interface/egCheckBox.class.php');
include_once('interface/egRadioButton.class.php');
include_once('interface/egPressButton.class.php');
include_once('interface/egDropDownListBox.class.php');
include_once('interface/egPropertyList.class.php');
include_once('interface/egPropertySheet.class.php');
include_once('interface/egPropertyPage.class.php');
include_once('interface/egTreeNode.class.php');
include_once('interface/egTreeList.class.php');
include_once('interface/egSubWindow.class.php');


$site = &Site::getInstance();

//security check : user must be identified to browse repositories
$user = $site->getCurrentUser();
if(!$user || $user->getUserId() == USER_ID_ANONYMOUS) {
	die("access denied");
	exit();
}

if(isset($_GET['acl_entries']) && isset($_GET['parent_container_name'])){
// show available groups and users
	$aclEntries = explode(',', $_GET['acl_entries']);
	$containerId = count($aclEntries);
	$parentContainer = $_GET['parent_container_name'];

	$usersManager = &$site->getUsersManager();
	$groupsManager = &$site->getGroupsManager();

	$usersEntries = array();
	$groupsEntries = array();

	foreach($aclEntries as $entry) {
		if(($group=strstr($entry, 'g_'))) {
	    $gid = substr($group, 2);
			$groupsEntries[$gid] = true;
		}
		else if (($user=strstr($entry, 'u_'))) {
	    $uid = substr($user, 2);
			$usersEntries[$uid] = true;
		}
	}

// frame + cancel button

// type : 'acl_users' or 'acl_groups'

	$manager = &egManager::getInstance();
	$main = &new egContainer("main_ctn", 0, 0, 0, 0);

	$listContainer = &new egContainer("list_ctn", 10, 32, 355, 140, true);
	$frame = &new egFrame("fra_1", "", 10, 30, 356, 145);
	$main->addObject($frame);
	$main->addContainer($listContainer);


	$groupsArray = $groupsManager->getGroupsListForDisplay();
	$usersArray = $usersManager->getUsersArrayForDisplay();

	$txtTitle = &new egStatic("sta_1", LANG_ACL_CHOOSE_STATIC.":", 10, 10, 250, 0, true);
	$main->addObject($txtTitle);

	$i = 0;
	$vPos = 2;
	foreach ($groupsArray as $groupId => $groupLogin) {
		if (!isset($groupsEntries[$groupId])) {
        $groupLink = "javascript:window.opener.frames[\'control_frame\'].location.href=\'".$_SERVER['PHP_SELF']."?acl_type=acl_groups&entry_name=$groupLogin&entry_id=$groupId&container_id=$containerId&parent_container_name=$parentContainer\';";
        $groupLink .= "window.close();";
        $link = &new egLink("group_link_".$i, LANG_ACL_GROUP_STATIC." ".$groupLogin, 10, $vPos, 200, 18, $groupLink, 'interface/icons/icon_group.png');
        $listContainer->addObject($link);
				++$i;
				$vPos += 20;
		}
	}

	foreach ($usersArray as $userId => $userLogin) {
		if (!isset($usersEntries[$userId])) {
        $userLink = "javascript:window.opener.frames[\'control_frame\'].location.href=\'".$_SERVER['PHP_SELF']."?acl_type=acl_users&entry_name=$userLogin&entry_id=$userId&container_id=$containerId&parent_container_name=$parentContainer\';";
        $userLink .= "window.close();";
        $link = &new egLink("user_link_".$i, LANG_ACL_USER_STATIC." ".$userLogin, 10, $vPos, 200, 18, $userLink, 'interface/icons/icon_user.png');
        $listContainer->addObject($link);
				++$i;
				$vPos += 20;
		}
	}

	$manager->addContainer($main);

	echo $manager->getHTML();
}
else if(isset($_GET['acl_type']) && isset($_GET['entry_name']) && isset($_GET['entry_id']) && isset($_GET['container_id']) && isset($_GET['parent_container_name'])) {
// populate the acl entries property list with the new entry
	$manager = &egManager::getInstance();
	$main = &new egContainer("main_ctn", 0, 0, 0, 0);

	$aclType = $_GET['acl_type'];
	$entryName = $_GET['entry_name'];
	$entryId = $_GET['entry_id'];
	$containerId = $_GET['container_id'];
  $parentContainer = $_GET['parent_container_name'];

	$scripts = new egScript();

  if(	$aclType == 'acl_users') {
	 $line = "window.parent.acl_entries[window.parent.acl_entries.length] = 'u_'+$entryId";
  }
  else {
    $line = "window.parent.acl_entries[window.parent.acl_entries.length] = 'g_'+$entryId";
  }

	$line .= "
	window.parent.container_acl_ctn_$containerId = new window.parent.egContainer('acl_ctn_".$containerId."', '0', '230', '500', '260', '".$parentContainer."', '');
	window.parent.frame_fra_$containerId = new window.parent.egFrame('fra_".$containerId."', 'frame ".$containerId."', '10', '30', '480', '215', 'acl_ctn_".$containerId."');
	window.parent.static_st_obname_$containerId = new window.parent.egStatic('st_obname_".$containerId."', '".LANG_ACL_PERMISSION_STATIC." $entryName:', '10', '5', '200', '0', 'acl_ctn_".$containerId."', '1');
	window.parent.container_acl_sub_ctn_$containerId = new window.parent.egContainer('acl_sub_ctn_".$containerId."', '11', '31', '478', '213', 'acl_ctn_".$containerId."', '1');
	window.parent.static_st_view_$containerId = new window.parent.egStatic('st_view_".$containerId."', '".LANG_ACL_VIEW_STATIC."', '5', '5', '150', '0', 'acl_sub_ctn_".$containerId."', '');
	window.parent.static_st_edit_$containerId = new window.parent.egStatic('st_edit_".$containerId."', '".LANG_ACL_EDIT_STATIC."', '5', '30', '150', '0', 'acl_sub_ctn_".$containerId."', '');
	window.parent.static_st_pub_$containerId = new window.parent.egStatic('st_pub_".$containerId."', '".LANG_ACL_PUBLISH_STATIC."', '5', '55', '150', '0', 'acl_sub_ctn_".$containerId."', '');
	window.parent.static_st_rem_$containerId = new window.parent.egStatic('st_rem_".$containerId."', '".LANG_ACL_REMOVE_STATIC."', '5', '80', '150', '0', 'acl_sub_ctn_".$containerId."', '');
	window.parent.static_st_adm_$containerId = new window.parent.egStatic('st_adm_".$containerId."', '".LANG_ACL_ADMIN_STATIC."', '5', '105', '150', '0', 'acl_sub_ctn_".$containerId."', '');

	window.parent.container_view_ctn_$containerId = new window.parent.egContainer('view_ctn_".$containerId."', '375', '5', '100', '20', 'acl_sub_ctn_".$containerId."', '1');
	window.parent.checkBox_cb_view_a_$containerId = new window.parent.egCheckBox('cb_view_a_".$containerId."', '', '0', '0', '10', '', 'view_ctn_".$containerId."', '".$aclType."[$entryId][view]', '1', '');
	window.parent.checkBox_cb_view_d_$containerId = new window.parent.egCheckBox('cb_view_d_".$containerId."', '', '63', '0', '10', '', 'view_ctn_".$containerId."', '".$aclType."[$entryId][view]', '-1', '');
	window.parent.checkBox_cb_view_a_$containerId.addSibling(window.parent.checkBox_cb_view_d_$containerId);

	window.parent.container_edit_ctn_$containerId = new window.parent.egContainer('edit_ctn_".$containerId."', '375', '30', '100', '20', 'acl_sub_ctn_".$containerId."', '1');
	window.parent.checkBox_cb_edit_a_$containerId = new window.parent.egCheckBox('cb_edit_a_".$containerId."', '', '0', '0', '10', '', 'edit_ctn_".$containerId."', '".$aclType."[$entryId][edit]', '1', '');
	window.parent.checkBox_cb_edit_d_$containerId = new window.parent.egCheckBox('cb_edit_d_".$containerId."', '', '63', '0', '10', '', 'edit_ctn_".$containerId."', '".$aclType."[$entryId][edit]', '-1', '');
	window.parent.checkBox_cb_edit_a_$containerId.addSibling(window.parent.checkBox_cb_edit_d_$containerId);

	window.parent.container_publish_ctn_$containerId = new window.parent.egContainer('publish_ctn_".$containerId."', '375', '55', '100', '20', 'acl_sub_ctn_".$containerId."', '1');
	window.parent.checkBox_cb_pub_a_$containerId = new window.parent.egCheckBox('cb_pub_a_".$containerId."', '', '0', '0', '10', '', 'publish_ctn_".$containerId."', '".$aclType."[$entryId][publish]', '1', '');
	window.parent.checkBox_cb_pub_d_$containerId = new window.parent.egCheckBox('cb_pub_d_".$containerId."', '', '63', '0', '10', '', 'publish_ctn_".$containerId."', '".$aclType."[$entryId][publish]', '-1', '');
	window.parent.checkBox_cb_pub_a_$containerId.addSibling(window.parent.checkBox_cb_pub_d_$containerId);

	window.parent.container_remove_ctn_$containerId = new window.parent.egContainer('remove_ctn_".$containerId."', '375', '80', '100', '20', 'acl_sub_ctn_".$containerId."', '1');
	window.parent.checkBox_cb_rem_a_$containerId = new window.parent.egCheckBox('cb_rem_a_".$containerId."', '', '0', '0', '10', '', 'remove_ctn_".$containerId."', '".$aclType."[$entryId][remove]', '1', '');
	window.parent.checkBox_cb_rem_d_$containerId = new window.parent.egCheckBox('cb_rem_d_".$containerId."', '', '63', '0', '10', '', 'remove_ctn_".$containerId."', '".$aclType."[$entryId][remove]', '-1', '');
	window.parent.checkBox_cb_rem_a_$containerId.addSibling(window.parent.checkBox_cb_rem_d_$containerId);

	window.parent.container_admin_ctn_$containerId = new window.parent.egContainer('admin_ctn_".$containerId."', '375', '105', '100', '20', 'acl_sub_ctn_".$containerId."', '1');
	window.parent.checkBox_cb_adm_a_$containerId = new window.parent.egCheckBox('cb_adm_a_".$containerId."', '', '0', '0', '10', '', 'admin_ctn_".$containerId."', '".$aclType."[$entryId][admin]', '1', '');
	window.parent.checkBox_cb_adm_d_$containerId = new window.parent.egCheckBox('cb_adm_d_".$containerId."', '', '63', '0', '10', '', 'admin_ctn_".$containerId."', '".$aclType."[$entryId][admin]', '-1', '');
	window.parent.checkBox_cb_adm_a_$containerId.addSibling(window.parent.checkBox_cb_adm_d_$containerId);
";

  if(	$aclType == 'acl_users') {
	 $line .= "window.parent.propertyList_ppl_1.addListItem('".LANG_ACL_USER_STATIC." $entryName', 'acl_ctn_".$containerId."');";
  }
  else {
	 $line .= "window.parent.propertyList_ppl_1.addListItem('".LANG_ACL_GROUP_STATIC." $entryName', 'acl_ctn_".$containerId."');";
  }

  $line .= "window.parent.propertyList_ppl_1.selectLastItem();";

	$scripts->addLine($line);

	$manager->addScripts($scripts);
	$manager->addContainer($main);

	echo $manager->getHTML();

}
else {
	echo "nothing to display";
}
?>