Jump to content

Extension:PagePolice

From mediawiki.org
MediaWiki extensions manual
PagePolice
Release status: unmaintained
Implementation User rights , Tag
Description Individual user access control to pages
Author(s) Ury Yakovlev (Ury.Yakovlevtalk)
Latest version 0.1 (2012-11-26)
MediaWiki
PHP 5.3+
Database changes No
License GPL
Download No link
Example <permit>Pr0;Root;H1;127.0.0.1</permit>
  • $wgPPError
  • $wgPPMessage
‎<permit>

The PagePolice extension allows individual user access control to pages.

Installation

[edit]

Configuration

[edit]

Example:

$wgPPError = "ACCESS DENIED";
$wgPPMessage = "SECURE ZONE (access only for %s)";

Code

[edit]
<?php
# Block Page content
# 
# Tag:
#   <permit>user_id</permit>
# Ex:
#   <permit>Pr0;Root;H1;127.0.0.1</permit>
# 
# Enjoy!

$wgExtensionCredits['parserhook'][] = array(
        'name' => 'PagePolice',
        'description' => 'Allows to block access to content',
        'author' => 'Ury Yakovlev',
        'url' => 'https://www.mediawiki.org/wiki/Extension:PagePolice'
);

$wgHooks['ArticlePageDataAfter'][] = 'check_permit';
$wgHooks['ParserFirstCallInit'][] = 'pp_setup';
 
function pp_setup( Parser $parser ) {
        $parser->setHook( 'permit', 'permit_render' );
       return true;
}

function check_permit( $article, $row ) {
  global $wgUser;
  global $wgPPError;
  
  if (!$wgPPError) {
    $wgPPError = "<h1>403 ACCESS DENIED</h1> <meta http-equiv='Refresh' content='5;url=/'>";
  }
  
  $dbw = wfGetDB( DB_PRIMARY );
  $text_data_row = $dbw->selectRow( 'text',
	  array( 'old_text', 'old_flags' ),
	  array( 'old_id' => $row->page_latest ),
	  __METHOD__ );
  $content = $text_data_row->old_text;
        preg_match('|<permit>(.*)</permit>|Uis', $content, $users_str);
        
        if ($users_str[1]) {
	      $input = $users_str[1];
        } else {
	      return true;
	}
 
         $users = explode(";", $input);
        
        $allow = false;
        $i=0;
        while ($users[$i]) {
              if ($wgUser->getName() == $users[$i]) {
		    $allow = true;
		    break;
              }
              $i++;
        }
 
	if ($allow) {
	      return true;
	} else {
	      echo $wgPPError;
	      exit;
	      return false;
	}
  
  return 0;
}

# The callback function for converting the input text to HTML output
function permit_render($input) {

	global $wgPPMessage;

	if (!$wgPPMessage) {
		$wgPPMessage = "<div style='background: #FFCC00;'><b>Защита</b></br>
			<b>Контент с защитой содержимого</b></br>
			Доступ разрешен только следующим пользователям: '%s'</div>";
	}
 
        $output = sprintf($wgPPMessage, $input);
        return $output;
}

See also

[edit]