Guest User

Mediawiki AccessControl for MediaWiki 1.21

a guest
Sep 13th, 2013
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.19 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. function getContentPage( $namespace, $title ) {
  100.     /* Function get content the page identified by title object from database */
  101.     $Title = new Title();
  102.     $gt = $Title->makeTitle( $namespace, $title );
  103.     if ( method_exists( 'WikiPage', 'getContent' ) ) {
  104.         $contentPage = new WikiPage( $gt );
  105.         if ($contentPage->getContent() != NULL)
  106.         {
  107.             return $contentPage->getContent()->getNativeData();
  108.         }
  109.     } else {
  110.         // create Article and get the content
  111.         $contentPage = new Article( $gt, 0 );
  112.         return $contentPage->fetchContent( 0 );
  113.     }
  114. }
  115.  
  116. function getTemplatePage( $template ) {
  117.     /* Function get content the template page identified by title object from database */
  118.     $Title = new Title();
  119.     $gt = $Title->makeTitle( 10, $template );
  120.     //echo '<!--';
  121.     //print_r($gt);
  122.     //echo '-->';
  123.     if ( method_exists( 'WikiPage', 'getContent' ) ) {
  124.                $contentPage = new WikiPage( $gt );
  125.                //return $contentPage->getContent()->getNativeData();
  126.     } else {
  127.         // create Article and get the content
  128.         $contentPage = new Article( $gt, 0 );
  129.         return $contentPage->fetchContent( 0 );
  130.     }
  131. }
  132.  
  133. function getUsersFromPages( $skupina ) {
  134.     /* Extracts the allowed users from the userspace access list */
  135.     $allowedAccess = Array();
  136.     $allow = Array();
  137.     $Title = new Title();
  138.     $gt = $Title->makeTitle( 0, $skupina );
  139.     if ( method_exists( 'WikiPage', 'getContent' ) ) {
  140.                $groupPage = new WikiPage( $gt );
  141.                $allowedUsers = $groupPage->getContent()->getNativeData();
  142.     } else {
  143.         // create Article and get the content
  144.         $groupPage = new Article( $gt, 0 );
  145.         $allowedUsers = $groupPage->fetchContent( 0 );
  146.     }
  147.     $groupPage = NULL;
  148.     $usersAccess = explode( "\n", $allowedUsers );
  149.     foreach  ($usersAccess as $userEntry ) {
  150.         $userItem = trim( $userEntry );
  151.         if ( substr( $userItem, 0, 1 ) == "*" ) {
  152.             if ( strpos( $userItem, "(ro)" ) === false ) {
  153.                 $user = trim( str_replace( "*", "", $userItem ) );
  154.                 $allow[$user] = 'edit';
  155.             } else {
  156.                 $user = trim( str_replace( "*", "", $userItem ) );
  157.                 $user = trim( str_replace( "(ro)", "", $user ) );
  158.                 $allow[$user] = 'read';
  159.             }
  160.         }
  161.     }
  162.     if ( is_array( $allow ) ) {
  163.         $allowedAccess = $allow;
  164.         unset( $allow );
  165.     }
  166.     return $allowedAccess;
  167. }
  168.  
  169. function doRedirect( $info ) {
  170.     /* make redirection for non authorized users */
  171.     global $wgScript, $wgSitename, $wgOut;
  172.  
  173.     if ( ! $info ) {
  174.         $info = "No_access";
  175.         }
  176.     if ( $info == "Only_sysop" ) {
  177.         $target = wfMsg( 'accesscontrol-info-user' );
  178.     } elseif ( $info == "No_anonymous" ) {
  179.         $target = wfMsg( 'accesscontrol-info-anonymous' );
  180.     } elseif ( $info == "Deny_anonymous") {
  181.         $target = wfMsg( 'accesscontrol-edit-anonymous' );
  182.     } elseif ( $info == "Deny_edit_list" ) {
  183.         $target = wfMsg( 'accesscontrol-edit-users' );
  184.     } else {
  185.         $target = wfMsg( 'accesscontrol-info-deny' );
  186.     }
  187.     if ( isset( $_SESSION['redirect'] ) ) {
  188.         // removing info about redirect from session after move..
  189.         unset( $_SESSION['redirect'] );
  190.     }
  191.     header( "Location: " . $wgScript . "/" . $wgSitename . ":" . $target );
  192. }
  193.  
  194. function fromTemplates( $string ) {
  195.     global $wgUser, $wgAdminCanReadAll;
  196.     // Vytažení Å¡ablon
  197.     if ( strpos( $string, '{{' ) ) {
  198.         if ( substr( $string, strpos ( $string, '{{' ), 3 ) === '{{{' ) {
  199.             $start = strpos( $string, '{{{' );
  200.             $end = strlen( $string );
  201.             $skok = $start + 3;
  202.             fromTemplates( substr( $string, $skok, $end - $skok ) );
  203.         } else {
  204.             $start = strpos( $string, '{{' );
  205.             $end = strpos( $string, '}}' );
  206.             $skok = $start + 2;
  207.             $templatepage = substr( $string, $skok, $end - $skok );
  208.             if ( strpos( $templatepage, '|' ) > 0) {
  209.                 $templatename = substr( $templatepage, 0, strpos( $templatepage, '|' ) );
  210.             } else {
  211.                 $templatename = $templatepage ;
  212.             }
  213.             if ( substr( $templatename, 0, 1 ) === ':') {
  214.                 // vložena stránka
  215.                 $rights = allRightTags( getContentPage( 0, substr( $templatename, 1 ) ) );
  216.             } else {
  217.                 // vložena Å¡ablona
  218.                 $rights = allRightTags( getTemplatePage( $templatename ) );
  219.             }
  220.             if ( is_array( $rights ) ) {
  221.             if ( $wgUser->mId === 0 ) {
  222.                 /* Redirection unknown users */
  223.                 $wgActions['view'] = false;
  224.                 doRedirect('accesscontrol-info-anonymous');
  225.                 } else {
  226.                 if ( in_array( 'sysop', $wgUser->mGroups, true ) ) {
  227.                     if ( isset( $wgAdminCanReadAll ) ) {
  228.                         if ( $wgAdminCanReadAll ) {
  229.                             return true;
  230.                             }
  231.                         }
  232.                     }
  233.                 $users = accessControl( $rights['groups'] );
  234.                 if ( ! in_array( $wgUser->mName, $users[0], true ) ) {
  235.                     $wgActions['edit']           = false;
  236.                     $wgActions['history']        = false;
  237.                     $wgActions['submit']         = false;
  238.                     $wgActions['info']           = false;
  239.                     $wgActions['raw']            = false;
  240.                     $wgActions['delete']         = false;
  241.                     $wgActions['revert']         = false;
  242.                     $wgActions['revisiondelete'] = false;
  243.                     $wgActions['rollback']       = false;
  244.                     $wgActions['markpatrolled']  = false;
  245.                     if ( ! in_array( $wgUser->mName, $users[1], true ) ) {
  246.                         $wgActions['view']   = false;
  247.                         return doRedirect( 'accesscontrol-info-anonymous' );
  248.                         }
  249.                     }
  250.                 }
  251.             }
  252.             fromTemplates( substr( $string, $end + 2 ) );
  253.         }
  254.         }
  255.     }
  256.  
  257.  
  258. function allRightTags( $string ) {
  259.     /* Function for extraction content tag accesscontrol from raw source the page */
  260.     $contenttag  = Array();
  261.     $starttag    = "<accesscontrol>";
  262.     $endtag      = "</accesscontrol>";
  263.     $redirecttag = "redirect";
  264.  
  265.     if ( ( mb_substr( trim( $string ), 0, 1 ) == "#" )
  266.         && ( stripos( mb_substr( trim( $string ), 1, 9 ), $redirecttag ) == "0" )
  267.         ) {
  268.         /* Treatment redirects - content variable $string must be replaced over content the target page */
  269.         $sourceredirecttag = mb_substr( $string, 0, strpos( $string, ']]' ) );
  270.         $redirecttarget = trim( substr( $sourceredirecttag, strpos( $sourceredirecttag, '[[' ) + 2 ) );
  271.         if ( strpos( $redirecttarget, '|' ) ) {
  272.             $redirecttarget = trim( substr( $redirecttarget, 0, strpos( $redirecttarget, '|' ) ) );
  273.         }
  274.         $Title = new Title();
  275.         $gt = $Title->makeTitle( 0, $redirecttarget );
  276.         return allRightTags( getContentPage( $gt->getNamespace(), $gt ) );
  277.     }
  278.  
  279.     // Kontrola accesscontrol ve vložených Å¡ablonách a stránkách
  280.     fromTemplates($string);
  281.  
  282.     $start = strpos( $string, $starttag );
  283.     if ( $start !== false ) {
  284.         $start += strlen( $starttag );
  285.         $end = strpos( $string, $endtag );
  286.         if ( $end !== false ) {
  287.             $groupsString = substr( $string, $start, $end-$start );
  288.             if ( strlen( $groupsString ) == 0 ) {
  289.                 $contenttag['end'] = strlen( $starttag ) + strlen( $endtag );
  290.             } else {
  291.                 $contenttag['groups'] = $groupsString;
  292.                 $contenttag['end'] = $end + strlen( $endtag );
  293.             }
  294.  
  295.             if( isset( $_SESSION['redirect'] ) ) {
  296.                 $_SESSION['redirect'] = $contenttag;
  297.             } else {
  298.                 return $contenttag;
  299.             }
  300.         }
  301.     } else {
  302.         if( isset( $_SESSION['redirect'] ) ) {
  303.             return $_SESSION['redirect'];
  304.         } else {
  305.             return false;
  306.         }
  307.     }
  308. }
  309.  
  310. function hookUserCan( &$title, &$wgUser, $action, &$result ) {
  311.     /* Main function control access for all users */
  312.     global $wgActions, $wgAdminCanReadAll;
  313.     if ( $wgUser->mId === 0 ) {
  314.         /* Deny actions for all anonymous */
  315.         $wgActions['edit']           = false;
  316.         $wgActions['history']        = false;
  317.         $wgActions['submit']         = false;
  318.         $wgActions['info']           = false;
  319.         $wgActions['raw']            = false;
  320.         $wgActions['delete']         = false;
  321.         $wgActions['revert']         = false;
  322.         $wgActions['revisiondelete'] = false;
  323.         $wgActions['rollback']       = false;
  324.         $wgActions['markpatrolled']  = false;
  325.         }
  326.  
  327.     $rights = allRightTags( getContentPage( $title->getNamespace(), $title->mDbkeyform ) );
  328.     if ( is_array( $rights ) ) {
  329.         if ( $wgUser->mId === 0 ) {
  330.             /* Redirection unknown users */
  331.             $wgActions['view'] = false;
  332.             doRedirect( 'accesscontrol-info-anonymous' );
  333.         } else {
  334.             if ( in_array( 'sysop', $wgUser->getGroups(), true ) ) {
  335.                 if ( isset( $wgAdminCanReadAll ) ) {
  336.                     if ( $wgAdminCanReadAll ) {
  337.                         return true;
  338.                     }
  339.                 }
  340.             }              
  341.             $users = accessControl( $rights['groups'] );
  342.             if ( in_array( $wgUser->mName, $users[0], true ) ) {
  343.                 return true;
  344.             } else {
  345.                 $wgActions['edit']           = false;
  346.                 $wgActions['history']        = false;
  347.                 $wgActions['submit']         = false;
  348.                 $wgActions['info']           = false;
  349.                 $wgActions['raw']            = false;
  350.                 $wgActions['delete']         = false;
  351.                 $wgActions['revert']         = false;
  352.                 $wgActions['revisiondelete'] = false;
  353.                 $wgActions['rollback']       = false;
  354.                 $wgActions['markpatrolled']  = false;
  355.                 if ( in_array( $wgUser->mName, $users[1], true ) ) {
  356.                     return true;
  357.                 } else {
  358.                     $wgActions['view']   = false;
  359.                     return doRedirect( 'accesscontrol-info-anonymous' );
  360.                 }
  361.             }
  362.         }
  363.     } else {
  364.         return true;
  365.     }
  366. }
  367.  
  368. ?>
Advertisement
Add Comment
Please, Sign In to add comment