Extension:CrudeProtection/source

From MediaWiki.org
Jump to: navigation, search
<?php
if (!defined('MEDIAWIKI')) die("MediaWiki extensions cannot be run directly.");
/**
 * An extension to crudely protect pages from viewing by unathorised users.
 *
 * @package MediaWiki
 * @subpackage Extensions
 *
 * @author Mark Clements <mclements at kennel17 dot co dot uk>
 * @copyright Copyright © 2006-2010, Mark Clements
 * @license http://creativecommons.org/licenses/by-sa/2.5/ cc-by-sa 2.5 or later
 * @version $Rev: 272 $
 */
 
// Setup version number
        $pMCExt_Version = '$Rev: 272 $';
        $pMCExt_Version = substr($pMCExt_Version, 6, -2);
// Setup extension credits
        $wgExtensionCredits['other'][] = array(
                'name' => "CrudeProtection",
                'version' => "r" . $pMCExt_Version,
                'author' => "Mark Clements",
                'description' => "an extension to crudely protect pages from viewing by unathorised users",
                'url' => "http://www.mediawiki.org/wiki/Extension:CrudeProtection",
        );
// Tidy up
        unset($pMCExt_Version);
 
        $pCrudeProtection_Messages = array(
                'crudeprotection_title' => "Access denied",
                'crudeprotection_text'  => "You do not have permission to view this page.",
        );
 
        $wgExtensionFunctions[] = "wfCrudeProtection";
 
        function wfCrudeProtection() {
                global $wgMessageCache, $wgParser, $wgHooks;
                global $pCrudeProtection_Messages;
 
                $wgMessageCache->addMessages($pCrudeProtection_Messages);
 
        // Parser hooks 
                $wgHooks['SkinTemplateContentActions'][] = "wfCrudeProtection_RemoveEditTab";
                $wgHooks['OutputPageBeforeHTML'][] = "wfCrudeProtection_DoBlock";
 
        // Specific tags to be parsed   
                $wgParser->setHook( "protect", "wfCrudeProtection_Protect" );
        }
 
        function wfCrudeProtection_Protect($Input, $Args) {
                global $wgOut, $wgUser, $wgParser;
                global $pBlocked;
 
        // We need to disable caching for any page that contains a <protect> tag.
                $wgParser->disableCache();
 
        // Only respond to the first <protect> block.  Ignore all others.
                if (!isset($pBlocked)) {
                        if (isset($Args['type'])) {
                                $Type = strtolower($Args['type']);
                                if ($Type != "allow" && $Type != "deny")
                                        $Type = "allow";
                        }
                        else
                                $Type = "allow";
 
                // Allow "\n" to be specified as a line-break character.
                        if (isset($Args['separator'])) {
                                $Separator = $Args['separator'];
                                if ($Separator == "\\n")
                                        $Separator = "\n";
                        }
                        else
                                $Separator = ",";
 
                // Set appropriate default value, for if the user is not in the list of users.
                        if ($Type == "allow")
                                $pBlocked = true;
                        elseif ($Type == "deny")
                                $pBlocked = false;
 
                // Check if the user is in the list - if so, then swap their status.
                        $Users = explode($Separator, $Input);
                        foreach ($Users as $Key => $Value) {
                                $Value = trim($Value);
                                if ($wgUser->getName() == $Value && $Value != "") {
                                        $pBlocked = !$pBlocked;
                                        break;
                                }
                        }
 
                // If they are blocked, they will be redirected to error page, via
                // the OutputPageBeforeHTML hook.
                }
                return "";
        }
 
        function wfCrudeProtection_DoBlock(&$out, &$text) {
                global $pBlocked;
 
                if ($pBlocked) {
                        $out->errorpage("crudeprotection_title", "crudeprotection_text");
                        return false;
                }
 
                return true;
        }
 
        function wfCrudeProtection_RemoveEditTab(&$ContentActions) {
                global $pBlocked;
 
                if ($pBlocked) {
                        unset($ContentActions['edit']);
                        unset($ContentActions['viewsource']);
                }
 
                return true;
        }
Personal tools
Namespaces

Variants
Actions
Navigation
Support
Download
Development
Communication
Print/export
Toolbox