Difference between revisions of "Extension:SimpleSecurity2.1.php"

From Organic Design wiki
m (nother bug)
m
Line 1: Line 1:
 
<?
 
<?
 
# Security
 
# Security
# Handle moves and submits
 
 
$a = $action == 'submit' ? 'edit' : strtolower($action);
 
$a = $action == 'submit' ? 'edit' : strtolower($action);
 
if ($title=='Special:Movepage' && $action=='submit') {
 
if ($title=='Special:Movepage' && $action=='submit') {
Line 12: Line 11:
  
 
# Get security links from this article
 
# Get security links from this article
$pattern = '/\\[{2}\\s*security\\s*:\\s*([^\\]]+?)\\s*\\|\\s*([^\\]]*)\\s*\\]{2}/i';
 
 
$secinfo = '';
 
$secinfo = '';
 
$text = new Article(Title::newFromText($t));
 
$text = new Article(Title::newFromText($t));
 
$text = $text->fetchContent(0,false,false);
 
$text = $text->fetchContent(0,false,false);
if (preg_match_all($pattern,$text,$seclinks,PREG_SET_ORDER)) addSecurityInfo($seclinks,$secinfo);
+
if (preg_match_all('/\\[{2}\\s*:?security\\s*:\\s*([^\\]]+?)\\s*\\|\\s*([^\\]]*)\\s*\\]{2}/i',$text,$seclinks,PREG_SET_ORDER))
else $seclinks = array();
+
addSecurityInfo($seclinks,$secinfo); else $seclinks = array();
  
 
# Get security links from article's categories
 
# Get security links from article's categories
Line 23: Line 21:
 
foreach ($cats[1] as $cat) {
 
foreach ($cats[1] as $cat) {
 
if (is_object($text = new Article(Title::newFromText($cat)))) {
 
if (is_object($text = new Article(Title::newFromText($cat)))) {
if (preg_match_all($pattern,$text->fetchContent(0,false,false),$match,PREG_SET_ORDER)) {
+
if (preg_match_all('/\\[{2}\\s*security\\s*:\\s*([^\\]]+?)\\s*\\|\\s*([^\\]]*)\\s*\\]{2}/i',$text->fetchContent(0,false,false),$match,PREG_SET_ORDER)) {
 
$seclinks = array_merge($match,$seclinks);
 
$seclinks = array_merge($match,$seclinks);
 
addSecurityInfo($match,$secinfo,"this rule is inherited from <a href='/$cat'>$cat</a>");
 
addSecurityInfo($match,$secinfo,"this rule is inherited from <a href='/$cat'>$cat</a>");
Line 41: Line 39:
 
# Validate extracted security against this user/groups
 
# Validate extracted security against this user/groups
 
$deny = false;
 
$deny = false;
if ($security && !in_array('sysop',$groups) && !in_array('directors',$groups)) {
+
if ($security && !in_array('sysop',$groups) && !in_array('director',$groups)) {
 
$security = preg_split("/\\s*,\\s*/",$security);
 
$security = preg_split("/\\s*,\\s*/",$security);
 
if (!in_array('*',$security)) {
 
if (!in_array('*',$security)) {
Line 61: Line 59:
 
$text = $text->fetchContent(0,false,false);
 
$text = $text->fetchContent(0,false,false);
 
}
 
}
else $text = preg_replace("/\\[{2}\\s*security\\s*:[^\\]]+?\\]{2}[\r\n]?/i",'',$text);
+
else $text = preg_replace("/\\[{2}\\s*:?security\\s*:[^\\]]+?\\]{2}[\r\n]?/i",'',$text);
 
}
 
}
  

Revision as of 00:36, 12 January 2007

<?

  1. Security

$a = $action == 'submit' ? 'edit' : strtolower($action); if ($title=='Special:Movepage' && $action=='submit') { $a = 'move'; $t = $wgRequest->getText('wpOldTitle',$wgRequest->getVal('target')); } else $t = $title; $groups = $wgUser->getGroups(); foreach($groups as $k => $v) $groups[$k] = strtolower($v); if ($t) {

# Get security links from this article $secinfo = ; $text = new Article(Title::newFromText($t)); $text = $text->fetchContent(0,false,false); if (preg_match_all('/\\[{2}\\s*:?security\\s*:\\s*([^\\]]+?)\\s*\\|\\s*([^\\]]*)\\s*\\]{2}/i',$text,$seclinks,PREG_SET_ORDER)) addSecurityInfo($seclinks,$secinfo); else $seclinks = array();

# Get security links from article's categories preg_match_all('/\\[{2}(category:.+?)(\\|.+?)?\\]]/i',$text,$cats); foreach ($cats[1] as $cat) { if (is_object($text = new Article(Title::newFromText($cat)))) { if (preg_match_all('/\\[{2}\\s*security\\s*:\\s*([^\\]]+?)\\s*\\|\\s*([^\\]]*)\\s*\\]{2}/i',$text->fetchContent(0,false,false),$match,PREG_SET_ORDER)) { $seclinks = array_merge($match,$seclinks); addSecurityInfo($match,$secinfo,"this rule is inherited from <a href='/$cat'>$cat</a>"); } } }

# Resolve permission for this action from the extracted security links $security = ; foreach ($seclinks as $link) { if ($link[2]==) $link[2] = 'sysop'; $actions = preg_split("/\\s*,\\s*/",strtolower($link[1])); if (in_array($a,$actions)) $security = $link[2]; if (in_array('*',$actions) && ($security == )) $security = $link[2]; }

# Validate extracted security against this user/groups $deny = false; if ($security && !in_array('sysop',$groups) && !in_array('director',$groups)) { $security = preg_split("/\\s*,\\s*/",$security); if (!in_array('*',$security)) { $groups[] = ucwords($wgUser->mName); if (count(array_intersect($groups,$security))==0) { $action = 'view'; $deny = true; } } } }

  1. Remove the security links before wiki-parsing

$wgHooks['ParserBeforeStrip'][] = 'removeSecurityLinks'; if ($secinfo) $wgHooks['ParserAfterTidy'][] = 'renderSecurityInfo'; function removeSecurityLinks(&$parser,&$text,&$strip_state) { if ($GLOBALS['deny']) { $text = new Article(Title::newFromText('Action not permitted')); $text = $text->fetchContent(0,false,false); } else $text = preg_replace("/\\[{2}\\s*:?security\\s*:[^\\]]+?\\]{2}[\r\n]?/i",,$text); }

  1. Security information functions

function addSecurityInfo(&$links,&$info,$comment=) { if ($comment) $comment = "   ($comment)"; foreach ($links as $link) { $a = $link[1] == '*' ? 'Every action' : ucfirst($link[1]); $b = $link[2] == '*' ? 'anybody' : $link[2];

$info .= "

  • $a requires the user to be $b$comment
  • ";

    } } function renderSecurityInfo(&$parser,&$text) { global $secinfo,$action;

    if ($action == 'view') $text .= "\n

    There are security restrictions on this article

      $secinfo
    <img src='/wiki/images/c/c1/Padlock.png'/>

    ";

    } ?>