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

From Organic Design wiki
(allow no users/groups (for sysop only))
(update with new category-inheritence)
Line 1: Line 1:
 
<?
 
<?
 +
# Read groups from groups
 +
$groups = new Article(Title::newFromText('Groups'));
 +
$groups = preg_match_all('/^\\*\\s*(.+?)\\s*$/m',$groups->fetchContent(0,false,false),$match);
 +
foreach($match[1] as $group) $wgGroupPermissions[$group] = array();
 +
 
# Security
 
# Security
$a = $action == 'submit' ? 'edit' : $action;
+
# Handle moves and submits
 +
$a = $action == 'submit' ? 'edit' : strtolower($action);
 
if ($title=='Special:Movepage' && $action=='submit') {
 
if ($title=='Special:Movepage' && $action=='submit') {
# Handle moves and submits
 
 
$a = 'move';
 
$a = 'move';
 
$t = $wgRequest->getText('wpOldTitle',$wgRequest->getVal('target'));
 
$t = $wgRequest->getText('wpOldTitle',$wgRequest->getVal('target'));
Line 9: Line 14:
 
$groups = $wgUser->getGroups();
 
$groups = $wgUser->getGroups();
 
foreach($groups as $k => $v) $groups[$k] = strtolower($v);
 
foreach($groups as $k => $v) $groups[$k] = strtolower($v);
if ($t&&!in_array('sysop',$groups)&&!in_array('directors',$groups)) {
+
if ($t) {
 +
 
 +
# Get security links from this article
 +
$pattern = '/\\[{2}\\s*security\\s*:\\s*([^\\]]+?)\\s*\\|\\s*([^\\]]*)\\s*\\]{2}/i';
 +
$secinfo = '';
 +
$text = new Article(Title::newFromText($t));
 +
$text = $text->fetchContent(0,false,false);
 +
if (preg_match_all($pattern,$text,$seclinks,PREG_SET_ORDER)) addSecurityInfo($seclinks,$secinfo);
 +
else $seclinks = array();
  
# Extract security information for this action from this article
+
# Get security links from article's categories
 +
preg_match_all('/\\[{2}(category:.+?)\\]]/i',$text,$cats);
 +
foreach ($cats[1] as $cat) {
 +
$text = new Article(Title::newFromText($cat));
 +
$text = $text->fetchContent(0,false,false);
 +
if (preg_match_all($pattern,$text,$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 = '';
 
$security = '';
$text = new Article(Title::newFromText($t));
+
foreach ($seclinks as $link) {
$text = $text->fetchContent(0, false, false);
+
if ($link[2]=='') $link[2] = 'sysop';
preg_match_all("/\\[{2}\\s*security\\s*:\\s*([^\\]]+?)\\s*\\|\\s*([^\\]]*?)\\s*\\]{2}/i",$text,$matches,PREG_SET_ORDER);
+
$actions = preg_split("/\\s*,\\s*/",strtolower($link[1]));
foreach ($matches as $m) {
+
if (in_array($a,$actions)) $security = $link[2];
$actions = preg_split("/\\s*,\\s*/",$m[1]);
+
if (in_array('*',$actions) && ($security == '')) $security = $link[2];
if (in_array($a,$actions)) $security = $m[2];
 
if (in_array('*',$actions) && ($security == '')) $security = $m[2];
 
 
}
 
}
  
 
# Validate extracted security against this user/groups
 
# Validate extracted security against this user/groups
 
$deny = false;
 
$deny = false;
if ($security) {
+
if ($security && !in_array('sysop',$groups) && !in_array('directors',$groups)) {
 
$security = preg_split("/\\s*,\\s*/",$security);
 
$security = preg_split("/\\s*,\\s*/",$security);
 
if (!in_array('*',$security)) {
 
if (!in_array('*',$security)) {
Line 37: Line 59:
  
 
# Remove the security links before wiki-parsing
 
# Remove the security links before wiki-parsing
$wgHooks['ParserBeforeStrip'][] = 'securityRemoveLinks';
+
$wgHooks['ParserBeforeStrip'][] = 'removeSecurityLinks';
function securityRemoveLinks(&$parser, &$text, &$strip_state) {
+
if ($secinfo) $wgHooks['ParserAfterTidy'][] = 'renderSecurityInfo';
 +
function removeSecurityLinks(&$parser,&$text,&$strip_state) {
 
if ($GLOBALS['deny']) {
 
if ($GLOBALS['deny']) {
 
$text = new Article(Title::newFromText('Action not permitted'));
 
$text = new Article(Title::newFromText('Action not permitted'));
$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);
 +
}
 +
 +
# Security information functions
 +
function addSecurityInfo(&$links,&$info,$comment='') {
 +
if ($comment) $comment = " &nbsp; <i>($comment)</i>";
 +
foreach ($links as $link) {
 +
$a = $link[1] == '*' ? 'Every action' : ucfirst($link[1]);
 +
$b = $link[2] == '*' ? 'anybody' : $link[2];
 +
$info .= "<li><b>$a</b> requires the user to be <b>$b</b>$comment</li>";
 +
}
 +
}
 +
function renderSecurityInfo(&$parser,&$text) {
 +
global $secinfo,$action;
 +
if ($action == 'view') $text .= "\n<br><table width=100% style='border:1px solid #aaa'><tr><td valign=top><h4>There are security restrictions on this article</h4><ul>$secinfo</ul><td align=right valign=middle><img src='/wiki/images/c/c1/Padlock.png'/></table>";
 
}
 
}
 
?>
 
?>

Revision as of 10:16, 10 January 2007

<?

  1. Read groups from groups

$groups = new Article(Title::newFromText('Groups')); $groups = preg_match_all('/^\\*\\s*(.+?)\\s*$/m',$groups->fetchContent(0,false,false),$match); foreach($match[1] as $group) $wgGroupPermissions[$group] = array();

  1. Security
  2. Handle moves and submits

$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 $pattern = '/\\[{2}\\s*security\\s*:\\s*([^\\]]+?)\\s*\\|\\s*([^\\]]*)\\s*\\]{2}/i'; $secinfo = ; $text = new Article(Title::newFromText($t)); $text = $text->fetchContent(0,false,false); if (preg_match_all($pattern,$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) { $text = new Article(Title::newFromText($cat)); $text = $text->fetchContent(0,false,false); if (preg_match_all($pattern,$text,$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('directors',$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'/>

    ";

    } ?>