summaryrefslogtreecommitdiff
path: root/www/wiki/skins/chameleon/src/PermissionsHelper.php
blob: 3d895c988e091fd508ec838cb29d895750b37405 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
/**
 * File containing the PermissionsHelper class
 *
 * This file is part of the MediaWiki skin Chameleon.
 *
 * @copyright 2013 - 2014, Stephan Gambke
 * @license   GNU General Public License, version 3 (or any later version)
 *
 * The Chameleon skin 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 3 of the License, or (at your option) any
 * later version.
 *
 * The Chameleon skin 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 this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * @file
 * @ingroup   Skins
 */

namespace Skins\Chameleon;

use DOMElement;

/**
 * PermissionsHelper class
 *
 * @author  Stephan Gambke
 * @since   1.1
 * @ingroup Skins
 */
class PermissionsHelper {

	private $domElement;
	private $skin;
	private $default;

	/**
	 * @param \SkinChameleon $skin
	 * @param DOMElement     $domElement
	 * @param bool           $default
	 */
	public function __construct( \SkinChameleon $skin, DOMElement $domElement = null, $default = false ) {
		$this->skin = $skin;
		$this->domElement = $domElement;
		$this->default = $default;
	}

	/**
	 * @since 1.1
	 *
	 * @param string $attributeNameInDomElement
	 *
	 * @return bool
	 */
	public function userHasGroup( $attributeNameInDomElement ) {

		return $this->userHas( 'group', $attributeNameInDomElement );
	}

	/**
	 * @param string $attributeOfUser
	 * @param string $attributeNameInDomElement
	 *
	 * @throws \MWException
	 * @return bool
	 */
	protected function userHas( $attributeOfUser, $attributeNameInDomElement ) {

		$user = $this->skin->getUser();
		$attributeAccessors = array(
			'group'      => array( $user, 'getEffectiveGroups' ),
			'permission' => array( $user, 'getRights' ),
		);

		if ( !array_key_exists( $attributeOfUser, $attributeAccessors ) ) {
			throw new \MWException( sprintf( 'Unknown permission: %s', $attributeOfUser ) );
		}

		if ( !$this->hasAttribute( $attributeNameInDomElement ) ) {
			return $this->default;
		}

		$expectedValues = $this->getValueListFromAttribute( $attributeNameInDomElement );
		$observedValues = call_user_func( $attributeAccessors[ $attributeOfUser ] );
		$effectiveValues = array_intersect( $expectedValues, $observedValues );

		return !empty( $effectiveValues );
	}

	/**
	 * @since 1.1
	 *
	 * @param string $attributeNameInDomElement
	 *
	 * @return bool
	 */
	public function hasAttribute( $attributeNameInDomElement ) {
		return $this->domElement !== null && $this->domElement->hasAttribute( $attributeNameInDomElement );
	}

	/**
	 * @param string $attributeName
	 *
	 * @return string[]
	 */
	protected function getValueListFromAttribute( $attributeName ) {
		return $this->domElement === null ? array() : array_map( 'trim', explode( ',', $this->domElement->getAttribute( $attributeName ) ) );

	}

	/**
	 * @since 1.1
	 *
	 * @param string $attributeNameInDomElement
	 *
	 * @return bool
	 */
	public function userHasPermission( $attributeNameInDomElement ) {

		return $this->userHas( 'permission', $attributeNameInDomElement );
	}

	/**
	 * @since 1.1
	 *
	 * @param string $attributeNameInDomElement
	 *
	 * @return bool
	 */
	public function pageIsInNamespace( $attributeNameInDomElement ) {

		if ( !$this->hasAttribute( $attributeNameInDomElement ) ) {
			return $this->default;
		}

		$expectedNamespaces = array_map( array( $this, 'getNamespaceNumberFromDefinedConstantName' ), $this->getValueListFromAttribute( $attributeNameInDomElement ) );
		$pageNamespace = $this->skin->getTitle()->getNamespace();

		return in_array( $pageNamespace, $expectedNamespaces );
	}

	/**
	 * @since 1.1
	 *
	 * AGREGADO PARA REEVO!
	 *
	 * @param string $attributeNameInDomElement
	 *
	 * @return bool
	 */
	public function pageHasCategory( $attributeNameInDomElement ) {

		if ( !$this->hasAttribute( $attributeNameInDomElement ) ) {
			return $this->default;
		}

		$expectedCategories = $this->getValueListFromAttribute( $attributeNameInDomElement );

		$pagename = $this->skin->getTitle();
		$cats = $this->skin->getSkin()->getCategoryLinks();
		preg_match_all('/<\s*a[^>]*>(.*?)<\s*\/\s*a>/', $cats, $output_array);
		$pageCategory = $output_array[1];
		$match = array_intersect($pageCategory,$expectedCategories);
		$final = reset($match);
		if ($match) {
			// error_log('Se encontro una categoria en la página '.$pagename.' que aplica una regla de Modification en Chamaleon');
			$pageCategory = $final;
		}

		return in_array( $pageCategory, $expectedCategories );
	}



	/**
	 * @param null|string $value
	 *
	 * @return int
	 */
	protected function getNamespaceNumberFromDefinedConstantName( $value ) {
		$constants = get_defined_constants();
		if ( !is_null( $value ) && array_key_exists( $value, $constants ) ) {
			$value = $constants[ $value ];
		}

		return is_int( $value ) ? $value : -1;
	}
}