Extension:Prefix Security/Installation.php

From MediaWiki.org

Jump to: navigation, search
  • Copy the code into the file Installation.php
<?php
 
/**
 * An Instalation page
 *
 * @package MediaWiki version 1.6.5 or later
 * @subpackage Extensions
 *
 * @author Borut Tomažin (debijan@gmail.com)
 * @url http://www.mediawiki.org/wiki/Extension:Prefix_Security
 * @copyright Copyright © 2008, Borut Tomažin
 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public Licence 2.0 or later
*/
 
$dbr =& wfGetDB( DB_SLAVE );
$table_name1 = "page_prefixes";
$table_name2 = "page_prefixes_auto";
$res1 = $dbr->query( "SHOW TABLES LIKE '$table_name1'" );
$res2 = $dbr->query( "SHOW TABLES LIKE '$table_name2'" );
$font_ok = "<font color='#00aa00'><b>"; $font_end = "</b></font>";
$font_no = "<font color='#ff0000'><b>";
$step1 = false;
$step2 = false;
$step3 = false;
$step4 = false;
$step5 = false;
$step6 = false;
$finalstep = false;
$localSettings = "./LocalSettings.php";
$groups_file   = './extensions/GroupsAdministration.php';
$groups_name   = 'GroupsAdministration';
$prefixes_file = './extensions/PrefixAdministration.php';
$prefixes_name = 'PrefixAdministration';
$hooks_file    = './extensions/PageRestrictionHooks.php';
$hooks_name    = 'PageRestrictionHooks';
$sgroups       = substr( $groups_file, 2, strlen( $groups_file ) );
$sprefixes     = substr( $prefixes_file, 2, strlen( $prefixes_file ) );
$shooks        = substr( $hooks_file, 2, strlen( $hooks_file ) );
 
$put_ext1 = "extension into ./extensions directory! $font_ok'''(done)'''$font_end";
$put_ext2 = "extension into ./extensions directory! $font_ok'''(please verify)'''$font_end";
$add_ext1 = "line to the end of file $localSettings $font_ok'''(done)'''$font_end";
$add_ext2 = "line to the end of file $localSettings $font_no'''(please verify)'''$font_end";
 
if( $dbr->numRows( $res1 ) == 0 && $dbr->numRows( $res2 ) == 0 ) {
	$wgOut->addHTML( "
		<form name='installation' action='$action' method='post'>
		<fieldset><legend><b>Restriction extensions installation</b></legend> 
	" );
	$out= " &#8226;package MediaWiki version 1.6.3 or later";
	$out.= "<br>&#8226;subpackage Extensions";
	$out.= "<br><br>&#8226;author Borut Toma&#382;in (debijan@gmail.com)";
	$out.= "<br>&#8226;copyright Copyright &#0169; 2006, Borut Toma&#382;in";
	$out.= "<br>&#8226;licence http://www.gnu.org/copyleft/gpl.html GNU General Public Licence 2.0 or later";
	$wgOut->addWikiText( $out );
 
	$wgOut->addWikiText( " {$font_no}For istallation details read README document first !{$font_end}" );
 
	$wgOut->addWikiText( "=====To install extensions successfully, please follow next few steps:=====" );
	$lines = file( $localSettings );
	$groups   = false;
	$pages    = false;
	$restrict = false;
	foreach( $lines as $line ) {
		if( stripos( $line, $sgroups ) !== false )
			$groups = true;
		if( stripos( $line, $sprefixes ) !== false )
			$pages = true;
		if( stripos( $line, $shooks ) !== false )
			$restrict = true;
	}
 
	if( file_exists( $groups_file ) ) {
		$wgOut->addWikiText( "*Put \"$groups_name\" ".$put_ext1 );
		$step1 = true;
	}
	else {
		$wgOut->addWikiText( "*Put \"$groups_name\" ".$put_ext2 );
		$step1 = false;
	}
	if( file_exists( $prefixes_file ) ) {
		$wgOut->addWikiText( "*Put \"$prefixes_name\" ".$put_ext1 );
		$step2 = true;
	}
	else {
		$wgOut->addWikiText( "*Put \"$prefixes_name\" ".$put_ext2 );
		$step2 = false;
	}
	if( file_exists( $hooks_file ) ) {
		$wgOut->addWikiText( "*Put \"$hooks_name\" ".$put_ext1 );
		$step3 = true;
	}
	else {
		$wgOut->addWikiText( "*Put \"$hooks_name\" ".$put_ext2 );
		$step3 = false;
	}
	if( $groups ) {
		$wgOut->addWikiText( "*Add \"require_once( 'extensions/$groups_name.php' )\" ".$add_ext1 );
		$step4 = true;
	}
	else {
		$wgOut->addWikiText( "*Add \"require_once( 'extensions/$groups_name.php' )\" ".$add_ext2 );
		$step4 = false;
	}
	if( $pages ) {
		$wgOut->addWikiText( "*Add \"require_once( 'extensions/$prefixes_name.php' )\" ".$add_ext1 );
		$step5 = true;
	}
	else {
		$wgOut->addWikiText( "*Add \"require_once( 'extensions/$prefixes_name.php' )\" ".$add_ext2 );
		$step5 = false;
	}
 
	if( $step1 & $step2 & $step3 & $step4 & $step5 ) {
		$wgOut->addHTML( "<br><input type='submit' name='install' value='Install extensions'>" );
	}
	else {
		$wgOut->addHTML( "<br><input type='submit' name='install' value='Install extensions' disabled>" );
	}
	$wgOut->addHTML( "</fieldset></form>" );
 
#=====================================================#
# POST REQUESTS
#=====================================================#
	if ( $wgRequest->wasPosted() ) {
		# POST REQUEST: installation
		#============================================#
		if( $wgRequest->getText( 'install' ) != "" ) {
			if( $wgRequest->getText( 'recheck' ) != "" ) {
				$wgOut->addHTML( "
					<fieldset><legend><b>Installation Details</b></legend>
					Creating database table 'page_prefixes'...
					$font_ok<b>(created)</b>$font_end
					<br>Creating database table 'page_prefixes_auto'...
					$font_ok<b>(created)</b>$font_end
					<br><br>$font_ok<b>Database tables were successfuly installed. Please proceed final step!</b>$font_end
					</fieldset>
				" );
			}
			else {
				$wgOut->addHTML( "<fieldset><legend><b>Installation Details</b></legend> ");
				$wgOut->addHTML( "Creating database table 'page_prefixes'... " );
				$dbw =& wfGetDB( DB_MASTER );
				$sql1 = "
					CREATE TABLE `$table_name1` (
						`id` int(8) NOT NULL auto_increment,
						`page_prefix` varchar(255) NOT NULL,
						`user_id` text NOT NULL,
						`group_name` text NOT NULL,
						`read_permission` varchar(1) NOT NULL,
						`edit_permission` varchar(1) NOT NULL,
						PRIMARY KEY (`id`)
					) ENGINE=MyISAM AUTO_INCREMENT=1
				";
				$sql2 = "INSERT INTO `$table_name1` VALUES (-1, '', '-1', '', 'y', 'y')";
				$sql3 = "INSERT INTO `$table_name1` VALUES (-2, '', '-2', '', 'y', 'n')";
 
				$dbw->query( $sql1 );
				$dbw->query( $sql2 );
				$dbw->query( $sql3 );
				$wgOut->addHTML( "$font_ok<b>(created)</b>$font_end" );
 
				$wgOut->addHTML( "<br>Creating database table 'page_prefixes_auto'... " );
				$sql4 = "
					CREATE TABLE `$table_name2` (
						`id` int(8) NOT NULL auto_increment,
						`page_title` varchar(255) NOT NULL,
						`user_id` varchar(255) NOT NULL,
						`group_name` varchar(255) NOT NULL,
						`tag_type` varchar(20) NOT NULL,
						PRIMARY KEY  (`id`)
					) ENGINE=MyISAM AUTO_INCREMENT=1;
				";
 
				$dbw->query( $sql4 );
				$wgOut->addHTML( "
					$font_ok<b>(created)</b>$font_end
					<br><br>$font_ok<b>Database tables were successfuly installed. Please proceed final step!</b>$font_end
					</fieldset>
				" );
			}
 
			$wgOut->addHTML( "<br><u>Final step:</u>" );
			if( file_exists( $localSettings ) ) {
				$lines = file( $localSettings );
				$restrict = false;
				foreach( $lines as $line ) {
					if( stripos( $line, $shooks ) !== false )
						$restrict = true;
				}
				if( $restrict ) {
					$wgOut->addWikiText( "*Add \"require_once( 'extensions/$hooks_name.php' )\" ".$add_ext1 );
					$wgOut->addWikiText( "*If your database has table prefix, please insert line bellow into the 'LocalSettings.php' file:" );
					$wgOut->addWikiText( "*<i>\$wgGroupPermissions['logged']['databasePrefix'] = \"database table prefix\";</i>" );
					$step6 = true;
				}
				else {
					$wgOut->addWikiText( "*Add \"require_once( 'extensions/$hooks_name.php' )\" ".$add_ext2 );
					$wgOut->addWikiText( "*If your database has table prefix, please insert line bellow into the 'LocalSettings.php' file:" );
					$wgOut->addWikiText( "*<i>\$wgGroupPermissions['logged']['databasePrefix'] = \"database table prefix\";</i>" );
					$step6 = false;
				}
			}
			else {
				$wgOut->addWikiText( "*File <u>$localSettings</u> does not exist in root directory! $font_no'''(please verify)'''$font_end" );
			}
			$wgOut->addHTML( "<br>" );
			if( $step6 ) {
				$wgOut->addWikiText( " <b>Extensions were successfully installed.</b><br>Enjoy!" );
			}
			else {
				$out = " {$font_no}To finish installation proceed final step!{$font_end}";
				$out.= "<br>If you are not shure that you put it correctly push button bellow to re-check.";
				$wgOut->addWikiText( $out );
				$wgOut->addHTML( "
					<form name='form_recheck' method='post' action='$action'>
					<input type='submit' name='recheck' value='Recheck'>
					</form>
				" );
			}
		}
	}
} 
else {
	if( $wgRequest->getText( 'recheck' ) != "" ) {
		$lines = file( $localSettings );
		$restrict = false;
		foreach( $lines as $line ) {
			if( stripos( $line, $shooks ) !== false )
				$restrict = true;
		}
		if( $restrict ) {
			$wgOut->addHTML( "<fieldset><legend>The last step</legend>Before you can start using extensions please remove \"Installation.php\" file from ./extensions directory!</fieldset>" ); 
			$step6 = true;
		}
		else {
			$wgOut->addWikiText( "<u>Final step</u><br>*Add \"require_once( 'extensions/$hooks_name.php' )\" ".$add_ext2 );
			$step6 = false;
			$out = " {$font_no}To finish installation proceed final step!{$font_end}";
			$out.= "<br>If you are not shure that you put it correctly push button bellow to re-check.";
			$wgOut->addWikiText( $out );
			$wgOut->addHTML( "
				<form name='form_recheck' method='post' action='$action'>
				<input type='submit' name='recheck' value='Recheck'>
				</form>
			" );
		}
	} 
	else {
		$wgOut->addHTML( "<fieldset><legend>The last step</legend>Before you can start using extensions please remove \"Installation.php\" file from ./extensions directory!</fieldset>" ); 
	}
}
Personal tools