<commit_before><?php
// (c) Copyright 2002-2013 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$

/**
 * PerspectiveLib
 *
 */
class PerspectiveLib
{
	private $perspectives;
	private $perspectivePreferences;

    /**
     *
     */
    function __construct()
	{
		$this->perspectives = TikiDb::get()->table('tiki_perspectives');
		$this->perspectivePreferences = TikiDb::get()->table('tiki_perspective_preferences');
	}

    /**
     * @param $prefs
     * @return int
     */
    function get_current_perspective( $prefs )
	{
		if ( isset( $_REQUEST['perspectiveId'] ) ) {
			return (int) $_REQUEST['perspectiveId'];
		} elseif ( isset( $_SESSION['current_perspective'] ) ) {
			return (int) $_SESSION['current_perspective'];
		}

		$tikilib = TikiLib::lib("tiki");
		if (method_exists($tikilib, "get_ip_address")) {
			$ip = $tikilib->get_ip_address();
		}

		foreach ( $this->get_subnet_map($prefs) as $subnet => $perspective ) {
			if ( $this->is_in_subnet($ip, $subnet) ) {
				return $perspective;
			}
		}

		$currentDomain = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '';
		foreach ( $this->get_domain_map($prefs) as $domain => $perspective ) {
			if ( $domain == $currentDomain ) {
				$_SESSION['current_perspective'] = trim($perspective);
				return $perspective;
			}
		}
	}

    /**
     * @param $prefs
     * @param $active_pref
     * @param $config_pref
     * @return array
     */
    private function get_map( $prefs, $active_pref, $config_pref )
	{
		if ( ! $prefs ) {
			global $prefs;
		}

		$out = array();

		if (( !empty($prefs[$active_pref]) && $prefs[$active_pref] != 'n' ) && isset($prefs[$config_pref])) {
			foreach ( explode("\n", $prefs[$config_pref]) as $config ) {
				if (substr_count($config, ',') == 1) {
					// Ignore lines which don't have exactly one comma, such as empty lines.
					// TODO: make sure there are no such lines in the first place
					list($domain, $perspective) = explode(',', $config);
					$out[$domain] = trim($perspective);
				}
			}
		}

		return $out;
	}

    /**
     * @param null $prefs
     * @return array
     */
    function get_subnet_map( $prefs = null )
	{
		return $this->get_map($prefs, 'site_terminal_active', 'site_terminal_config');
	}

    /**
     * @param null $prefs
     * @return array
     */
    function get_domain_map( $prefs = null )
	{
		return $this->get_map($prefs, 'multidomain_active', 'multidomain_config');
	}

    /**
     * @param $ip
     * @param $subnet
     * @return bool
     */
    private function is_in_subnet( $ip, $subnet )
	{
		list($subnet, $size) = explode('/', $subnet);

		// Warning - bit shifting ahead.

		// Create the real mask from the /X suffix
		$mask = 0xFFFFFFFF ^ ((1 << (int) (32 - $size)) - 1);

		// Make sure the subnet-relevant part matches for the IP and the subnet being compared
		return (ip2long($subnet) & $mask) === (ip2long($ip) & $mask);
	}

	/**
	 * Returns a string-indexed array containing the preferences for the given perspective as "pref_name" => "pref_value".
	 *
	 */
	function get_preferences( $perspectiveId )
	{
		$result = TikiDb::get()->query("SELECT pref, value FROM tiki_perspective_preferences WHERE perspectiveId = ?", array( $perspectiveId ));

		$out = array();

		while ( $row = $result->fetchRow() ) {
			$out[ $row['pref'] ] = unserialize($row['value']);
		}

		return $out;
	}

    /**
     * @param $perspectiveId
     * @return mixed
     */
    function get_perspective( $perspectiveId )
	{
		$result = TikiDb::get()->query("SELECT perspectiveId, name FROM tiki_perspectives WHERE perspectiveId = ?", array( $perspectiveId ));

		if ( $info = $result->fetchRow() ) {
			$perms = Perms::get(array( 'type' => 'perspective', 'object' => $perspectiveId ));
			if ( $perms->view_perspective ) {
				$info['preferences'] = $this->get_preferences($perspectiveId);
				$this->write_permissions($info, $perms);

				return $info;
			}
		}
	}


    /**
     * @param $perspective
     */
    function set_perspective($perspective)
	{
		global $prefs, $url_scheme, $tikiroot;

		if ( $this->get_perspective($perspective) || ($prefs['feature_areas'] === 'y' && $perspective === 0)) {
			if ($prefs['multidomain_switchdomain'] == 'y') {
				foreach ( $this->get_domain_map() as $domain => $persp ) {
					if ( $persp == $perspective && isset($_SERVER['HTTP_HOST']) && $domain != $_SERVER['HTTP_HOST'] ) {
						$path = '';
						if ($prefs['feature_areas'] === 'y') {
							if (!empty($_SERVER['REQUEST_URI'])) {<commit_msg>[bp/r47436] [FIX] Wrong permission was checked <commit_after><?php
// (c) Copyright 2002-2013 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$

/**
 * PerspectiveLib
 *
 */
class PerspectiveLib
{
	private $perspectives;
	private $perspectivePreferences;

    /**
     *
     */
    function __construct()
	{
		$this->perspectives = TikiDb::get()->table('tiki_perspectives');
		$this->perspectivePreferences = TikiDb::get()->table('tiki_perspective_preferences');
	}

    /**
     * @param $prefs
     * @return int
     */
    function get_current_perspective( $prefs )
	{
		if ( isset( $_REQUEST['perspectiveId'] ) ) {
			return (int) $_REQUEST['perspectiveId'];
		} elseif ( isset( $_SESSION['current_perspective'] ) ) {
			return (int) $_SESSION['current_perspective'];
		}

		$tikilib = TikiLib::lib("tiki");
		if (method_exists($tikilib, "get_ip_address")) {
			$ip = $tikilib->get_ip_address();
		}

		foreach ( $this->get_subnet_map($prefs) as $subnet => $perspective ) {
			if ( $this->is_in_subnet($ip, $subnet) ) {
				return $perspective;
			}
		}

		$currentDomain = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '';
		foreach ( $this->get_domain_map($prefs) as $domain => $perspective ) {
			if ( $domain == $currentDomain ) {
				$_SESSION['current_perspective'] = trim($perspective);
				return $perspective;
			}
		}
	}

    /**
     * @param $prefs
     * @param $active_pref
     * @param $config_pref
     * @return array
     */
    private function get_map( $prefs, $active_pref, $config_pref )
	{
		if ( ! $prefs ) {
			global $prefs;
		}

		$out = array();

		if (( !empty($prefs[$active_pref]) && $prefs[$active_pref] != 'n' ) && isset($prefs[$config_pref])) {
			foreach ( explode("\n", $prefs[$config_pref]) as $config ) {
				if (substr_count($config, ',') == 1) {
					// Ignore lines which don't have exactly one comma, such as empty lines.
					// TODO: make sure there are no such lines in the first place
					list($domain, $perspective) = explode(',', $config);
					$out[$domain] = trim($perspective);
				}
			}
		}

		return $out;
	}

    /**
     * @param null $prefs
     * @return array
     */
    function get_subnet_map( $prefs = null )
	{
		return $this->get_map($prefs, 'site_terminal_active', 'site_terminal_config');
	}

    /**
     * @param null $prefs
     * @return array
     */
    function get_domain_map( $prefs = null )
	{
		return $this->get_map($prefs, 'multidomain_active', 'multidomain_config');
	}

    /**
     * @param $ip
     * @param $subnet
     * @return bool
     */
    private function is_in_subnet( $ip, $subnet )
	{
		list($subnet, $size) = explode('/', $subnet);

		// Warning - bit shifting ahead.

		// Create the real mask from the /X suffix
		$mask = 0xFFFFFFFF ^ ((1 << (int) (32 - $size)) - 1);

		// Make sure the subnet-relevant part matches for the IP and the subnet being compared
		return (ip2long($subnet) & $mask) === (ip2long($ip) & $mask);
	}

	/**
	 * Returns a string-indexed array containing the preferences for the given perspective as "pref_name" => "pref_value".
	 *
	 */
	function get_preferences( $perspectiveId )
	{
		$result = TikiDb::get()->query("SELECT pref, value FROM tiki_perspective_preferences WHERE perspectiveId = ?", array( $perspectiveId ));

		$out = array();

		while ( $row = $result->fetchRow() ) {
			$out[ $row['pref'] ] = unserialize($row['value']);
		}

		return $out;
	}

    /**
     * @param $perspectiveId
     * @return mixed
     */
    function get_perspective( $perspectiveId )
	{
		$result = TikiDb::get()->query("SELECT perspectiveId, name FROM tiki_perspectives WHERE perspectiveId = ?", array( $perspectiveId ));

		if ( $info = $result->fetchRow() ) {
			$perms = Perms::get(array( 'type' => 'perspective', 'object' => $perspectiveId ));
			if ( $perms->perspective_view ) {
				$info['preferences'] = $this->get_preferences($perspectiveId);
				$this->write_permissions($info, $perms);

				return $info;
			}
		}
	}


    /**
     * @param $perspective
     */
    function set_perspective($perspective)
	{
		global $prefs, $url_scheme, $tikiroot;

		if ( $this->get_perspective($perspective) || ($prefs['feature_areas'] === 'y' && $perspective === 0)) {
			if ($prefs['multidomain_switchdomain'] == 'y') {
				foreach ( $this->get_domain_map() as $domain => $persp ) {
					if ( $persp == $perspective && isset($_SERVER['HTTP_HOST']) && $domain != $_SERVER['HTTP_HOST'] ) {
						$path = '';
						if ($prefs['feature_areas'] === 'y') {
							if (!empty($_SERVER['REQUEST_URI'])) {
{'fraction_non_alphanumeric': 0.1721014492753623, 'fraction_numerical': 0.0041560102301790285, 'mean_word_length': 3.336876155268022, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '79194', 'n_tokens_mistral': 3510, 'n_tokens_neox': 3156, 'n_words': 710}