Difference between revisions of "Extension:SimpleSecurity2.1.php"
m |
(version 2.0 - redoing almost from scratch, much more efficient and stronger!) |
||
Line 1: | Line 1: | ||
<? | <? | ||
− | # Simple security extension | + | # Simple security extension version 2.0 |
# - [[Security:edit,delete,move,protect|sysop]] | # - [[Security:edit,delete,move,protect|sysop]] | ||
# - See [[[[MediaWiki Security]]]] article for installation and usage details | # - See [[[[MediaWiki Security]]]] article for installation and usage details | ||
Line 6: | Line 6: | ||
# - Needs apache's mod-rewrite for security on images, see code comments below | # - Needs apache's mod-rewrite for security on images, see code comments below | ||
− | # | + | # Asseses security after raw content fetched from database and clears if not readable |
− | $ | + | # - also fills the global $securityItems cache with info to append to the rendered article |
− | + | $securityAfterFetchIndex = count($wgHooks['ArticleAfterFetchContent']); | |
− | $ | + | $wgHooks['ArticleAfterFetchContent'][] = 'securityAfterFetch'; |
− | $ | + | function securityAfterFetch(&$this,&$text) { |
− | + | global $wgUser,$wgTitle,$securityItems,$securityAfterFetchIndex; | |
− | $ | + | $parser = new Parser; |
+ | $options = ParserOptions::newFromUser($wgUser); | ||
+ | $parser->setFunctionHook('Security','securityProcessItem'); | ||
+ | $parser->setFunctionHook(':Security','securityProcessItem'); | ||
+ | $securityItems = array(); | ||
+ | $output = $parser->parse($text,$wgTitle,$options,false,false); | ||
+ | $allItems = $securityItems; | ||
− | $ | + | # Before inheriting security from categories |
− | + | # - stop checking :Security because they shouldn't inherit | |
− | + | # - remove this AfterDatabaseFetch hook because we need to read each cats content | |
+ | unset($parser->mFunctionHooks[':Security']); | ||
+ | $hookBak = $wgHooks['ArticleAfterFetchContent']; | ||
+ | $wgHooks['ArticleAfterFetchContent'][$securityAfterFetchIndex] | ||
+ | = array_splice($wgHooks['ArticleAfterFetchContent'],$securityAfterFetchIndex,1); | ||
− | # | + | # Get the security items from the cats by running the parser over the content of each |
− | + | foreach ($output->getCategoryLinks() as $cat) { | |
− | + | $article = new Article($title = Title::newFromText($cat = "Category:$cat")); | |
− | + | $securityItems = array(); | |
− | + | $parser->parse($article->fetchContent(0,false,false),$title,$options,false,false); | |
− | + | foreach ($securityItems as $i) $allItems[] = array($i[0],$i[1],"this rule is inherited from [[:$cat]]"); | |
− | $ | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
} | } | ||
Line 54: | Line 41: | ||
if ($link[2]=='') $link[2] = 'sysop'; | if ($link[2]=='') $link[2] = 'sysop'; | ||
$actions = preg_split("/\\s*,\\s*/",strtolower($link[1])); | $actions = preg_split("/\\s*,\\s*/",strtolower($link[1])); | ||
− | if (in_array($ | + | if (in_array($securityAction,$actions)) $security = $link[2]; |
if (in_array('*',$actions) && ($security == '')) $security = $link[2]; | if (in_array('*',$actions) && ($security == '')) $security = $link[2]; | ||
} | } | ||
# Validate extracted security against this user/groups | # Validate extracted security against this user/groups | ||
+ | $groups = $wgUser->getGroups(); | ||
+ | foreach($groups as $k => $v) $groups[$k] = strtolower($v); | ||
$deny = false; | $deny = false; | ||
if ($security && !in_array('sysop',$groups) && !in_array('director',$groups)) { | if ($security && !in_array('sysop',$groups) && !in_array('director',$groups)) { | ||
Line 70: | Line 59: | ||
} | } | ||
} | } | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | # | + | # Put the AfterFetch hook back how it was and exit successfully |
− | $wgHooks[' | + | $wgHooks['ArticleAfterFetchContent'][$securityAfterFetchIndex] = $hookBak; |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
return true; | return true; | ||
} | } | ||
− | # | + | # Accumulates security information in a global cache whenever a security item is parsed |
− | function | + | function securityProcessItem(&$this,$a,$b) { |
− | + | $GLOBALS['securityItems'][] = array($a,$b); | |
− | + | return ''; | |
− | |||
− | |||
− | |||
− | |||
} | } | ||
?> | ?> |
Revision as of 09:30, 3 March 2007
<?
- Simple security extension version 2.0
- - sysop
- - See [[MediaWiki Security]] article for installation and usage details
- - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
- - Needs apache's mod-rewrite for security on images, see code comments below
- Asseses security after raw content fetched from database and clears if not readable
- - also fills the global $securityItems cache with info to append to the rendered article
$securityAfterFetchIndex = count($wgHooks['ArticleAfterFetchContent']); $wgHooks['ArticleAfterFetchContent'][] = 'securityAfterFetch'; function securityAfterFetch(&$this,&$text) { global $wgUser,$wgTitle,$securityItems,$securityAfterFetchIndex; $parser = new Parser; $options = ParserOptions::newFromUser($wgUser); $parser->setFunctionHook('Security','securityProcessItem'); $parser->setFunctionHook(':Security','securityProcessItem'); $securityItems = array(); $output = $parser->parse($text,$wgTitle,$options,false,false); $allItems = $securityItems;
# Before inheriting security from categories # - stop checking :Security because they shouldn't inherit # - remove this AfterDatabaseFetch hook because we need to read each cats content unset($parser->mFunctionHooks[':Security']); $hookBak = $wgHooks['ArticleAfterFetchContent']; $wgHooks['ArticleAfterFetchContent'][$securityAfterFetchIndex] = array_splice($wgHooks['ArticleAfterFetchContent'],$securityAfterFetchIndex,1);
# Get the security items from the cats by running the parser over the content of each foreach ($output->getCategoryLinks() as $cat) { $article = new Article($title = Title::newFromText($cat = "Category:$cat")); $securityItems = array(); $parser->parse($article->fetchContent(0,false,false),$title,$options,false,false); foreach ($securityItems as $i) $allItems[] = array($i[0],$i[1],"this rule is inherited from $cat"); }
# 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($securityAction,$actions)) $security = $link[2]; if (in_array('*',$actions) && ($security == )) $security = $link[2]; }
# Validate extracted security against this user/groups $groups = $wgUser->getGroups(); foreach($groups as $k => $v) $groups[$k] = strtolower($v); $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; } } }
# Put the AfterFetch hook back how it was and exit successfully $wgHooks['ArticleAfterFetchContent'][$securityAfterFetchIndex] = $hookBak; return true; }
- Accumulates security information in a global cache whenever a security item is parsed
function securityProcessItem(&$this,$a,$b) { $GLOBALS['securityItems'][] = array($a,$b); return ; } ?>