Difference between revisions of "Extension:PublicCat.php"

From Organic Design wiki
(1.0.2 - fixed 1.11 error)
(use $wgOut->disable() instead of die)
Line 1: Line 1:
 
<?php
 
<?php
# Extension:PublicCat{{Category:Extensions|PublicCat}}{{php}}
+
# Extension:PublicCat
 
# - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
 
# - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
 
# - Author: [http://www.organicdesign.co.nz/nad User:Nad]
 
# - Author: [http://www.organicdesign.co.nz/nad User:Nad]
+
 
 
if (!defined('MEDIAWIKI')) die('Not an entry point.');
 
if (!defined('MEDIAWIKI')) die('Not an entry point.');
+
 
define('PUBLICCAT_VERSION','1.0.2, 2007-09-14');
+
define('PUBLICCAT_VERSION','1.0.3, 2007-09-22');
+
 
 
# Name of category in which to place articles to make them viewable from the public site
 
# Name of category in which to place articles to make them viewable from the public site
 
$wgPublicCat = 'Public';
 
$wgPublicCat = 'Public';
+
 
 
# Pattern to match with domain to determine if client accessing private wiki or public website
 
# Pattern to match with domain to determine if client accessing private wiki or public website
 
$wgPublicCatPrivatePattern = '/^wiki\\./';
 
$wgPublicCatPrivatePattern = '/^wiki\\./';
+
 
 
$wgExtensionCredits['other'][] = array(
 
$wgExtensionCredits['other'][] = array(
 
'name'        => 'PublicCat',
 
'name'        => 'PublicCat',
Line 21: Line 21:
 
'version'    => PUBLICCAT_VERSION
 
'version'    => PUBLICCAT_VERSION
 
);
 
);
+
 
 
# Hook in after article and title prepared
 
# Hook in after article and title prepared
 
# - should be able to hook in earlier than this,
 
# - should be able to hook in earlier than this,
 
# - but note that it must be a hook that always executes, even for specialpages
 
# - but note that it must be a hook that always executes, even for specialpages
 
$wgHooks['OutputPageBeforeHTML'][] = 'wfPublicCatValidate';
 
$wgHooks['OutputPageBeforeHTML'][] = 'wfPublicCatValidate';
+
 
 
# Determine if the request is private or public from the domain and pattern
 
# Determine if the request is private or public from the domain and pattern
 
$wgPublicCatRequestIsPrivate = preg_match($wgPublicCatPrivatePattern,$_SERVER['HTTP_HOST']);
 
$wgPublicCatRequestIsPrivate = preg_match($wgPublicCatPrivatePattern,$_SERVER['HTTP_HOST']);
 
$wgPublicCatRequestIsPublic  = !$wgPublicCatRequestIsPrivate;
 
$wgPublicCatRequestIsPublic  = !$wgPublicCatRequestIsPrivate;
+
 
 
# Disable all actions except view for anonymous users
 
# Disable all actions except view for anonymous users
 
$wgGroupPermissions['*']['createaccount'] = false;
 
$wgGroupPermissions['*']['createaccount'] = false;
Line 37: Line 37:
 
$wgGroupPermissions['*']['createpage']    = false;
 
$wgGroupPermissions['*']['createpage']    = false;
 
$wgGroupPermissions['*']['createtalk']    = false;
 
$wgGroupPermissions['*']['createtalk']    = false;
+
 
 
# Allow unrestricted access to login and css's
 
# Allow unrestricted access to login and css's
 
$wgWhitelistRead = array('Special:Userlogin','-');
 
$wgWhitelistRead = array('Special:Userlogin','-');
 
if (eregi('\\.css$',$_REQUEST['title'])) $wgWhitelistRead[] = $_REQUEST['title'];
 
if (eregi('\\.css$',$_REQUEST['title'])) $wgWhitelistRead[] = $_REQUEST['title'];
+
 
 
# If request is public, restrict action to view, raw or render
 
# If request is public, restrict action to view, raw or render
 
if ($wgPublicCatRequestIsPublic) {
 
if ($wgPublicCatRequestIsPublic) {
Line 47: Line 47:
 
$_REQUEST['action'] = 'view';
 
$_REQUEST['action'] = 'view';
 
}
 
}
+
 
 
# Function to die returing a 404 not found error
 
# Function to die returing a 404 not found error
 
function wfPublicCat404() {
 
function wfPublicCat404() {
 +
global $wgOut;
 +
$wgOut->disable();
 
header('HTTP/1.0 404 Not Found');
 
header('HTTP/1.0 404 Not Found');
 
$err = '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><html><head><title>404 Not Found</title></head>
 
$err = '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><html><head><title>404 Not Found</title></head>
<body><h1>Not Found</h1><p>The requested URL was not found on this server.</p></body></html>';
+
<body><h1>Not Found</h1><p>The requested URL was not found on this server!</p></body></html>';
 
if (in_array('Content-Encoding: gzip',headers_list())) $err = gzencode($err);
 
if (in_array('Content-Encoding: gzip',headers_list())) $err = gzencode($err);
 
echo $err;
 
echo $err;
die;
 
 
}
 
}
+
 
 
# Function to check if passed a title is in a category
 
# Function to check if passed a title is in a category
 
function wfPublicCatInCat(&$title,$cat) {
 
function wfPublicCatInCat(&$title,$cat) {
Line 69: Line 70:
 
return is_array(mysql_fetch_row($result));
 
return is_array(mysql_fetch_row($result));
 
}
 
}
+
 
 
# Return a 404 not found error if request is public but requested title is not in the public cat
 
# Return a 404 not found error if request is public but requested title is not in the public cat
 
function wfPublicCatValidate() {
 
function wfPublicCatValidate() {
Line 75: Line 76:
 
if ($wgPublicCatRequestIsPublic && !wfPublicCatInCat($wgTitle,$wgPublicCat)) wfPublicCat404();
 
if ($wgPublicCatRequestIsPublic && !wfPublicCatInCat($wgTitle,$wgPublicCat)) wfPublicCat404();
 
}
 
}
 
 
?>
 
?>

Revision as of 21:03, 21 September 2007

<?php

  1. Extension:PublicCat
  2. - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
  3. - Author: User:Nad

if (!defined('MEDIAWIKI')) die('Not an entry point.');

define('PUBLICCAT_VERSION','1.0.3, 2007-09-22');

  1. Name of category in which to place articles to make them viewable from the public site

$wgPublicCat = 'Public';

  1. Pattern to match with domain to determine if client accessing private wiki or public website

$wgPublicCatPrivatePattern = '/^wiki\\./';

$wgExtensionCredits['other'][] = array( 'name' => 'PublicCat', 'author' => 'User:Nad', 'description' => 'Divides wiki into private and public by domain name, but allows unrestricted access to articles in a public category.', 'url' => 'http://www.mediawiki.org/wiki/Extension:PublicCat', 'version' => PUBLICCAT_VERSION );

  1. Hook in after article and title prepared
  2. - should be able to hook in earlier than this,
  3. - but note that it must be a hook that always executes, even for specialpages

$wgHooks['OutputPageBeforeHTML'][] = 'wfPublicCatValidate';

  1. Determine if the request is private or public from the domain and pattern

$wgPublicCatRequestIsPrivate = preg_match($wgPublicCatPrivatePattern,$_SERVER['HTTP_HOST']); $wgPublicCatRequestIsPublic = !$wgPublicCatRequestIsPrivate;

  1. Disable all actions except view for anonymous users

$wgGroupPermissions['*']['createaccount'] = false; $wgGroupPermissions['*']['read'] = $wgPublicCatRequestIsPublic; $wgGroupPermissions['*']['edit'] = false; $wgGroupPermissions['*']['createpage'] = false; $wgGroupPermissions['*']['createtalk'] = false;

  1. Allow unrestricted access to login and css's

$wgWhitelistRead = array('Special:Userlogin','-'); if (eregi('\\.css$',$_REQUEST['title'])) $wgWhitelistRead[] = $_REQUEST['title'];

  1. If request is public, restrict action to view, raw or render

if ($wgPublicCatRequestIsPublic) { if ($_REQUEST['action'] != 'view' && $_REQUEST['action'] != 'raw' && $_REQUEST['action'] != 'render') $_REQUEST['action'] = 'view'; }

  1. Function to die returing a 404 not found error

function wfPublicCat404() { global $wgOut; $wgOut->disable(); header('HTTP/1.0 404 Not Found'); $err = '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">404 Not Found

Not Found

The requested URL was not found on this server!

'; if (in_array('Content-Encoding: gzip',headers_list())) $err = gzencode($err); echo $err; }

  1. Function to check if passed a title is in a category

function wfPublicCatInCat(&$title,$cat) { if (!is_object($title)) $title = Title::newFromText($title); if (!is_object($title)) return false; $id = $title->getArticleID(); $db = &wfGetDB(DB_SLAVE); $cl = $db->tableName('categorylinks'); $result = $db->query("SELECT 0 FROM $cl WHERE cl_from = $id AND cl_to = '$cat'"); if ($result instanceof ResultWrapper) $result = $result->result; return is_array(mysql_fetch_row($result)); }

  1. Return a 404 not found error if request is public but requested title is not in the public cat

function wfPublicCatValidate() { global $wgPublicCat,$wgPublicCatRequestIsPublic,$wgTitle; if ($wgPublicCatRequestIsPublic && !wfPublicCatInCat($wgTitle,$wgPublicCat)) wfPublicCat404(); } ?>