. * * @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; } }