diff options
author | Yaco <franco@reevo.org> | 2020-06-04 11:01:00 -0300 |
---|---|---|
committer | Yaco <franco@reevo.org> | 2020-06-04 11:01:00 -0300 |
commit | fc7369835258467bf97eb64f184b93691f9a9fd5 (patch) | |
tree | daabd60089d2dd76d9f5fb416b005fbe159c799d /www/wiki/skins/chameleon/src/PermissionsHelper.php |
first commit
Diffstat (limited to 'www/wiki/skins/chameleon/src/PermissionsHelper.php')
-rw-r--r-- | www/wiki/skins/chameleon/src/PermissionsHelper.php | 195 |
1 files changed, 195 insertions, 0 deletions
diff --git a/www/wiki/skins/chameleon/src/PermissionsHelper.php b/www/wiki/skins/chameleon/src/PermissionsHelper.php new file mode 100644 index 00000000..3d895c98 --- /dev/null +++ b/www/wiki/skins/chameleon/src/PermissionsHelper.php @@ -0,0 +1,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; + } +} |