<commit_before><?php
// (c) Copyright 2002-2016 by authors of the Tiki Wiki CMS Groupware Project
// 
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$

// For documentation how to use this file please see the comment at the end of this file

//this script may only be included - so its better to die if called  directly.
if (strpos($_SERVER["SCRIPT_NAME"], basename(__FILE__)) !== false) {
	header("location: index.php");
	exit;
}

$categlib = TikiLib::lib('categ');

class AreasLib extends CategLib
{
	private $areas;
	private $areasArray;

	function __construct()
	{
		$this->areas = $this->table('tiki_areas');
		$this->cacheAreas();

		parent::__construct();
	}

	function HandleObjectCategories($objectCategoryIds)
	{
		global $prefs;
		$perspectivelib = TikiLib::lib('perspective');
		$accesslib = TikiLib::lib('access');

		$current_object = current_object();

		if (!$current_object) {	// only used on tiki objects
			return;
		}

		$descendants = $this->get_category_descendants($prefs['areas_root']);

		$objectPerspective = 0;
		if (!empty($objectCategoryIds)) {
			if (!isset($_SESSION['current_perspective'])) unset($_SESSION['current_perspective']);
			foreach ($objectCategoryIds as $categId) {
				// If category is inside $prefs['areas_root']
				if (in_array($categId, $descendants)) {
					$area = $this->getAreaByCategId($categId);

					if ($area) {
						$objectPerspective = $area['perspectives'][0]; // use 1st persp
						break;
					}
				}
			}
			if ($objectPerspective && $objectPerspective != $_SESSION['current_perspective']) {

				$area = $this->getAreaByPerspId($_SESSION['current_perspective']);
				$objectArea = $this->getAreaByPerspId($objectPerspective);

				if (($area && !$area['share_common']) || ($objectArea && $objectArea['exclusive'])) {
					$perspectivelib->set_perspective($objectPerspective, true);
					$accesslib->redirect(ZendOpenId\OpenId::selfUrl(), '', 301	);
				}
			}
		}
		if ($objectPerspective < 1 && !empty($_SESSION['current_perspective'])) { // uncategorised objects

			$area = $this->getAreaByPerspId($_SESSION['current_perspective']);
			if ($area) {
				if ( !$area['share_common']) {
					$perspectivelib->set_perspective($objectPerspective, true);
					$accesslib->redirect(ZendOpenId\OpenId::selfUrl(), '', 301);
				}
			}
		}
	}

	public function getAreaByCategId($categId, $enabled = true)
	{
		foreach ($this->areasArray as $area) {
			if ($area['enabled'] == $enabled && $categId == $area['categId']) {<commit_msg>[FIX] When using ZendOpenId::selfUrl the query arguments are removed, so it dont work when we have tiki-index.php?page=XXXX<commit_after><?php
// (c) Copyright 2002-2016 by authors of the Tiki Wiki CMS Groupware Project
// 
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$

// For documentation how to use this file please see the comment at the end of this file

//this script may only be included - so its better to die if called  directly.
if (strpos($_SERVER["SCRIPT_NAME"], basename(__FILE__)) !== false) {
	header("location: index.php");
	exit;
}

$categlib = TikiLib::lib('categ');

class AreasLib extends CategLib
{
	private $areas;
	private $areasArray;

	function __construct()
	{
		$this->areas = $this->table('tiki_areas');
		$this->cacheAreas();

		parent::__construct();
	}

	function HandleObjectCategories($objectCategoryIds)
	{
		global $prefs;
		$perspectivelib = TikiLib::lib('perspective');
		$accesslib = TikiLib::lib('access');

		$current_object = current_object();

		if (!$current_object) {	// only used on tiki objects
			return;
		}

		$descendants = $this->get_category_descendants($prefs['areas_root']);

		$objectPerspective = 0;
		if (!empty($objectCategoryIds)) {
			if (!isset($_SESSION['current_perspective'])) unset($_SESSION['current_perspective']);
			foreach ($objectCategoryIds as $categId) {
				// If category is inside $prefs['areas_root']
				if (in_array($categId, $descendants)) {
					$area = $this->getAreaByCategId($categId);

					if ($area) {
						$objectPerspective = $area['perspectives'][0]; // use 1st persp
						break;
					}
				}
			}
			if ($objectPerspective && $objectPerspective != $_SESSION['current_perspective']) {

				$area = $this->getAreaByPerspId($_SESSION['current_perspective']);
				$objectArea = $this->getAreaByPerspId($objectPerspective);

				if (($area && !$area['share_common']) || ($objectArea && $objectArea['exclusive'])) {
					$perspectivelib->set_perspective($objectPerspective, true);
					$accesslib->redirect(self::selfUrl(), '', 301);
				}
			}
		}
		if ($objectPerspective < 1 && !empty($_SESSION['current_perspective'])) { // uncategorised objects

			$area = $this->getAreaByPerspId($_SESSION['current_perspective']);
			if ($area) {
				if ( !$area['share_common']) {
					$perspectivelib->set_perspective($objectPerspective, true);
					$accesslib->redirect(self::selfUrl(), '', 301);
				}
			}
		}
	}

	/**
	 * Returns a full URL that was requested on current HTTP request.
	 *
	 * Inspired on \ZendOpenId\OpenId::selfUrl
	 *
	 * @return string
	 */
	public static function selfUrl()
	{
		$url = '';
		$port = '';

		if (isset($_SERVER['HTTP_HOST'])) {
			if (($pos = strpos($_SERVER['HTTP_HOST'], ':')) === false) {
				if (isset($_SERVER['SERVER_PORT'])) {
					$port = ':' . $_SERVER['SERVER_PORT'];
				}
				$url = $_SERVER['HTTP_HOST'];
			} else {
				$url = substr($_SERVER['HTTP_HOST'], 0, $pos);
				$port = substr($_SERVER['HTTP_HOST'], $pos);
			}
		} elseif (isset($_SERVER['SERVER_NAME'])) {
			$url = $_SERVER['SERVER_NAME'];
			if (isset($_SERVER['SERVER_PORT'])) {
				$port = ':' . $_SERVER['SERVER_PORT'];
			}
		}

		if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
			$url = 'https://' . $url;
			if ($port == ':443') {
				$port = '';
			}
		} else {
			$url = 'http://' . $url;
			if ($port == ':80') {
				$port = '';
			}
		}

		$url .= $port;
		if (isset($_SERVER['HTTP_X_REWRITE_URL'])) {
			$url .= $_SERVER['HTTP_X_REWRITE_URL'];
		} elseif (isset($_SERVER['REQUEST_URI'])) {
			$url .= $_SERVER['REQUEST_URI'];
		} elseif (isset($_SERVER['SCRIPT_URL'])) {
			$url .= $_SERVER['SCRIPT_URL'];
		} elseif (isset($_SERVER['REDIRECT_URL'])) {
			$url .= $_SERVER['REDIRECT_URL'];
		} elseif (isset($_SERVER['PHP_SELF'])) {
			$url .= $_SERVER['PHP_SELF'];
		} elseif (isset($_SERVER['SCRIPT_NAME'])) {
			$url .= $_SERVER['SCRIPT_NAME'];
			if (isset($_SERVER['PATH_INFO'])) {
				$url .= $_SERVER['PATH_INFO'];
			}
		}
		return $url;
	}

	public function getAreaByCategId($categId, $enabled = true)
	{
		foreach ($this->areasArray as $area) {
			if ($area['enabled'] == $enabled && $categId == $area['categId']) {
{'fraction_non_alphanumeric': 0.1780127462340672, 'fraction_numerical': 0.00608342989571263, 'mean_word_length': 4.187828700225395, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 29, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6248523', 'n_tokens_mistral': 2638, 'n_tokens_neox': 2264, 'n_words': 495}