Advertisement
Guest User

MediaWiki AccessControl Extension Mod

a guest
Jul 31st, 2012
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.93 KB | None | 0 0
  1. <?php
  2.  
  3. /* MediaWiki extension that enables group access restriction on a page-by-page
  4.  * basis contributed by Martin Mueller (http://blog.pagansoft.de) based into
  5.  * version 1.3 on accesscontrol.php by Josh Greenberg.
  6.  * Version 2.0 for MediaWiki >= 1.18 rewrited completly by Aleš Kapica.
  7.  * @package MediaWiki
  8.  * @subpackage Extensions
  9.  * @author Aleš Kapica
  10.  * @copyright 2008-2012 Aleš Kapica
  11.  * @licence GNU General Public Licence
  12.  */
  13.  
  14. if( !defined( 'MEDIAWIKI' ) ) {
  15.     echo ( "This file is an extension to the MediaWiki software and cannot be used standalone.\n" );
  16.     die();
  17. }
  18.  
  19. // sysop users can read all restricted pages
  20. $wgAdminCanReadAll = true;
  21.  
  22. $wgExtensionCredits['specialpage']['AccessControl'] = array(
  23.     'name'                  => 'AccessControlExtension',
  24.     'author'                => array( 'Aleš Kapica' ),
  25.     'url'                   => 'http://www.mediawiki.org/wiki/Extension:AccessControl',
  26.     'version'               => '2.1',
  27.     'description'           => 'Access control based on users lists. Administrator rights need not be for it.',
  28.     'descriptionmsg'        => 'accesscontrol-desc',
  29. );
  30.  
  31. $wgHooks['ParserFirstCallInit'][] = 'wfAccessControlExtension' ;
  32.  
  33. $dir = dirname( __FILE__ ) . '/';
  34. $wgExtensionMessagesFiles['AccessControl'] = $dir . 'AccessControl.i18n.php';
  35.  
  36.  
  37. //Hook the userCan function for bypassing the cache
  38. $wgHooks['userCan'][] = 'hookUserCan';
  39.  
  40. function wfAccessControlExtension( Parser $parser ) {
  41.     /* This the hook function adds the tag <accesscontrol> to the wiki parser */
  42.     $parser->setHook( "accesscontrol", "doControlUserAccess" );
  43.     return true;
  44. }
  45.  
  46. function doControlUserAccess( $input, array $args, Parser $parser, PPFrame $frame ) {
  47.     /* Funcion called by wfAccessControlExtension */
  48.     return displayGroups();
  49. }
  50.  
  51. function accessControl( $obsahtagu ){
  52.     $accessgroup = Array( Array(), Array() );
  53.     $listaccesslist = explode( ",", $obsahtagu );
  54.     foreach ( $listaccesslist as $accesslist ) {
  55.         if ( strpos( $accesslist, "(ro)" ) !== false ) {
  56.             $accesslist = trim( str_replace( "(ro)", "", $accesslist ) );
  57.             $group = makeGroupArray( $accesslist );
  58.             $accessgroup[1] = array_merge( $accessgroup[1], $group[0] );
  59.             $accessgroup[1] = array_merge( $accessgroup[1], $group[1] );
  60.         } else {
  61.             $accesslist = trim( $accesslist );
  62.             $group = makeGroupArray ($accesslist );
  63.             $accessgroup[0] = array_merge( $accessgroup[0], $group[0] );
  64.             $accessgroup[1] = array_merge( $accessgroup[1], $group[1] );
  65.         }
  66.     }
  67.     return $accessgroup;
  68. }
  69.  
  70. function makeGroupArray( $accesslist ) {
  71.     /* Function returns array with two lists.
  72.         First is list full access users.
  73.         Second is list readonly users. */
  74.     $userswrite = Array();
  75.     $usersreadonly = Array();
  76.     $users = getUsersFromPages( $accesslist );
  77.     foreach ( array_keys( $users ) as $user ) {
  78.         switch ( $users[$user] ) {
  79.             case 'read':
  80.                 $usersreadonly[] = $user;
  81.                 break;
  82.             case 'edit':
  83.                 $userswrite[] = $user;
  84.                 break;
  85.         }
  86.     }
  87.     return array( $userswrite , $usersreadonly );
  88. }
  89.  
  90. function displayGroups() {
  91.     /* Function replace the tag <accesscontrol> and his content, behind info about a protection this the page */
  92.     $style = "<p id=\"accesscontrol\" style=\"text-align:center;color:#BA0000;font-size:8pt\">";
  93.     $text = wfMsg( 'accesscontrol-info' );
  94.     $style_end = "</p>";
  95.     $wgAllowInfo = $style . $text . $style_end;
  96.     return $wgAllowInfo;
  97. }
  98.  
  99. // MOD by Paul Wieland to add $mNamespace so that this extension works with multiple namespaces
  100. function getContentPage( $title , $mNamespace=0) {
  101.     /* Function get content the page identified by title object from database */
  102.     $Title = new Title();
  103.     $gt = $Title->makeTitle( $mNamespace, $title );
  104.     // create Article and get the content
  105.     $contentPage = new Article( $gt, 0 );
  106.     return $contentPage->fetchContent( 0 );
  107.     }
  108.  
  109. function getTemplatePage( $template ) {
  110.     /* Function get content the template page identified by title object from database */
  111.     $Title = new Title();
  112.     $gt = $Title->makeTitle( 10, $template );
  113.     //echo '<!--';
  114.     //print_r($gt);
  115.     //echo '-->';
  116.     // create Article and get the content
  117.     $contentPage = new Article( $gt, 0 );
  118.     return $contentPage->fetchContent( 0 );
  119.     }
  120.  
  121. function getUsersFromPages( $skupina ) {
  122.     // Edits by Paul Wieland to make this thing work with namespaces (before it would only use ns 0)
  123.     $namespace_id = MWNamespace::getCanonicalIndex(strtolower(strstr($skupina, ':', true)));
  124.     $skupina = ltrim(strstr($skupina, ':'),':');
  125.  
  126.     /* Extracts the allowed users from the userspace access list */
  127.     $allowedAccess = Array();
  128.     $allow = Array();
  129.     $Title = new Title();
  130.     $gt = $Title->makeTitle( $namespace_id, $skupina );
  131.     // create Article and get the content
  132.     $groupPage = new Article( $gt, 0 );
  133.     $allowedUsers = $groupPage->fetchContent( 0 );
  134.     $groupPage = NULL;
  135.     $usersAccess = explode( "\n", $allowedUsers );
  136.     foreach  ($usersAccess as $userEntry ) {
  137.         $userItem = strtolower(trim( $userEntry ));
  138.         if ( substr( $userItem, 0, 1 ) == "*" ) {
  139.             if ( strpos( $userItem, "(ro)" ) === false ) {
  140.                 $user = trim( str_replace( "*", "", $userItem ) );
  141.                 $allow[$user] = 'edit';
  142.             } else {
  143.                 $user = trim( str_replace( "*", "", $userItem ) );
  144.                 $user = trim( str_replace( "(ro)", "", $user ) );
  145.                 $allow[$user] = 'read';
  146.             }
  147.         }
  148.     }
  149.     if ( is_array( $allow ) ) {
  150.         $allowedAccess = $allow;
  151.         unset( $allow );
  152.     }
  153.     return $allowedAccess;
  154. }
  155.  
  156. function doRedirect( $info ) {
  157.     /* make redirection for non authorized users */
  158.     global $wgScript, $wgSitename, $wgOut;
  159.  
  160.     if ( ! $info ) {
  161.         $info = "No_access";
  162.         }
  163.     if ( $info == "Only_sysop" ) {
  164.         $target = wfMsg( 'accesscontrol-info-user' );
  165.     } elseif ( $info == "No_anonymous" ) {
  166.         $target = wfMsg( 'accesscontrol-info-anonymous' );
  167.     } elseif ( $info == "Deny_anonymous") {
  168.         $target = wfMsg( 'accesscontrol-edit-anonymous' );
  169.     } elseif ( $info == "Deny_edit_list" ) {
  170.         $target = wfMsg( 'accesscontrol-edit-users' );
  171.     } else {
  172.         $target = wfMsg( 'accesscontrol-info-deny' );
  173.     }
  174.     if ( isset( $_SESSION['redirect'] ) ) {
  175.         // removing info about redirect from session after move..
  176.         unset( $_SESSION['redirect'] );
  177.     }
  178.  
  179.     header( "Location: " . $wgScript . "/" . $wgSitename . ":" . $target );
  180. }
  181.  
  182. function fromTemplates( $string ) {
  183.     global $wgUser, $wgAdminCanReadAll;
  184.     // Vytažení šablon
  185.     if ( strpos( $string, '{{' ) ) {
  186.         if ( substr( $string, strpos ( $string, '{{' ), 3 ) === '{{{' ) {
  187.             $start = strpos( $string, '{{{' );
  188.             $end = strlen( $string );
  189.             $skok = $start + 3;
  190.             fromTemplates( substr( $string, $skok, $end - $skok ) );
  191.         } else {
  192.             $start = strpos( $string, '{{' );
  193.             $end = strpos( $string, '}}' );
  194.             $skok = $start + 2;
  195.             $templatepage = substr( $string, $skok, $end - $skok );
  196.             if ( strpos( $templatepage, '|' ) > 0) {
  197.                 $templatename = substr( $templatepage, 0, strpos( $templatepage, '|' ) );
  198.             } else {
  199.                 $templatename = $templatepage ;
  200.             }
  201.             if ( substr( $templatename, 0, 1 ) === ':') {
  202.                 // vložena stránka
  203.                 $rights = allRightTags( getContentPage( substr( $templatename, 1 ) ) );
  204.             } else {
  205.                 // vložena šablona
  206.                 $rights = allRightTags( getTemplatePage( $templatename ) );
  207.             }
  208.             if ( is_array( $rights ) ) {
  209.             if ( $wgUser->mId === 0 ) {
  210.                 /* Redirection unknown users */
  211.                 $wgActions['view'] = false;
  212.                 doRedirect('accesscontrol-info-anonymous');
  213.                 } else {
  214.                 if ( in_array( 'sysop', $wgUser->mGroups, true ) ) {
  215.                     if ( isset( $wgAdminCanReadAll ) ) {
  216.                         if ( $wgAdminCanReadAll ) {
  217.                             return true;
  218.                             }
  219.                         }
  220.                     }
  221.                 $users = accessControl( $rights['groups'] );
  222.                 if ( ! in_array( strtolower($wgUser->mName), $users[0], true ) ) {
  223.                     $wgActions['edit']           = false;
  224.                     $wgActions['history']        = false;
  225.                     $wgActions['submit']         = false;
  226.                     $wgActions['info']           = false;
  227.                     $wgActions['raw']            = false;
  228.                     $wgActions['delete']         = false;
  229.                     $wgActions['revert']         = false;
  230.                     $wgActions['revisiondelete'] = false;
  231.                     $wgActions['rollback']       = false;
  232.                     $wgActions['markpatrolled']  = false;
  233.                     if ( ! in_array( strtolower($wgUser->mName), $users[1], true ) ) {
  234.                         $wgActions['view']   = false;
  235.                         return doRedirect( 'accesscontrol-info-anonymous' );
  236.                         }
  237.                     }
  238.                 }
  239.             }
  240.             fromTemplates( substr( $string, $end + 2 ) );
  241.         }
  242.         }
  243.     }
  244.  
  245.  
  246. function allRightTags( $string ) {
  247.     /* Function for extraction content tag accesscontrol from raw source the page */
  248.     $contenttag  = Array();
  249.     $starttag    = "<accesscontrol>";
  250.     $endtag      = "</accesscontrol>";
  251.     $redirecttag = "redirect";
  252.  
  253.     if ( ( mb_substr( trim( $string ), 0, 1 ) == "#" )
  254.         && ( stripos( mb_substr( trim( $string ), 1, 9 ), $redirecttag ) == "0" )
  255.         ) {
  256.         /* Treatment redirects - content variable $string must be replaced over content the target page */
  257.         $sourceredirecttag = mb_substr( $string, 0, strpos( $string, ']]' ) );
  258.         $redirecttarget = trim( substr( $sourceredirecttag, strpos( $sourceredirecttag, '[[' ) + 2 ) );
  259.         if ( strpos( $redirecttarget, '|' ) ) {
  260.             $redirecttarget = trim( substr( $redirecttarget, 0, strpos( $redirecttarget, '|' ) ) );
  261.         }
  262.         $Title = new Title();
  263.         $gt = $Title->makeTitle( 0, $redirecttarget );
  264.         return allRightTags( getContentPage( $gt ) );
  265.     }
  266.  
  267.     // Kontrola accesscontrol ve vložených šablonách a stránkách
  268.     fromTemplates($string);
  269.  
  270.     $start = strpos( $string, $starttag );
  271.     if ( $start !== false ) {
  272.         $start += strlen( $starttag );
  273.         $end = strpos( $string, $endtag );
  274.         if ( $end !== false ) {
  275.             $groupsString = substr( $string, $start, $end-$start );
  276.             if ( strlen( $groupsString ) == 0 ) {
  277.                 $contenttag['end'] = strlen( $starttag ) + strlen( $endtag );
  278.             } else {
  279.                 $contenttag['groups'] = $groupsString;
  280.                 $contenttag['end'] = $end + strlen( $endtag );
  281.             }
  282.  
  283.             if( isset( $_SESSION['redirect'] ) ) {
  284.                 $_SESSION['redirect'] = $contenttag;
  285.             } else {
  286.                 return $contenttag;
  287.             }
  288.         }
  289.     } else {
  290.         if( isset( $_SESSION['redirect'] ) ) {
  291.             return $_SESSION['redirect'];
  292.         } else {
  293.             return false;
  294.         }
  295.     }
  296. }
  297.  
  298. function hookUserCan( &$title, &$wgUser, $action, &$result ) {
  299.     /* Main function control access for all users */
  300.     global $wgActions, $wgAdminCanReadAll;
  301.     if ( $wgUser->mId === 0 ) {
  302.         /* Deny actions for all anonymous */
  303.         $wgActions['edit']           = false;
  304.         $wgActions['history']        = false;
  305.         $wgActions['submit']         = false;
  306.         $wgActions['info']           = false;
  307.         $wgActions['raw']            = false;
  308.         $wgActions['delete']         = false;
  309.         $wgActions['revert']         = false;
  310.         $wgActions['revisiondelete'] = false;
  311.         $wgActions['rollback']       = false;
  312.         $wgActions['markpatrolled']  = false;
  313.         }
  314.  
  315.     $rights = allRightTags( getContentPage( $title->mDbkeyform , $title->mNamespace) );
  316.    
  317.     if ( is_array( $rights ) ) {
  318.         if ( $wgUser->mId === 0 ) {
  319.             /* Redirection unknown users */
  320.             $wgActions['view'] = false;
  321.             doRedirect( 'accesscontrol-info-anonymous' );
  322.         } else {
  323.             if ( in_array( 'sysop', $wgUser->mGroups, true ) ) {
  324.                 if ( isset( $wgAdminCanReadAll ) ) {
  325.                     if ( $wgAdminCanReadAll ) {
  326.                         return true;
  327.                     }
  328.                 }
  329.             }
  330.             $users = accessControl( $rights['groups'] );
  331.             if ( in_array( strtolower($wgUser->mName), $users[0], true ) ) {
  332.                 return true;
  333.             } else {
  334.                 $wgActions['edit']           = false;
  335.                 $wgActions['history']        = false;
  336.                 $wgActions['submit']         = false;
  337.                 $wgActions['info']           = false;
  338.                 $wgActions['raw']            = false;
  339.                 $wgActions['delete']         = false;
  340.                 $wgActions['revert']         = false;
  341.                 $wgActions['revisiondelete'] = false;
  342.                 $wgActions['rollback']       = false;
  343.                 $wgActions['markpatrolled']  = false;
  344.                 if ( in_array( strtolower($wgUser->mName), $users[1], true ) ) {
  345.                     return true;
  346.                 } else {
  347.                     $wgActions['view']   = false;
  348.                     return doRedirect( 'accesscontrol-info-anonymous' );
  349.                 }
  350.             }
  351.         }
  352.     } else {
  353.         return true;
  354.     }
  355. }
  356.  
  357. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement