Difference between revisions of "Extension:PublicCat.php"

From Organic Design wiki
m (ready for debugging)
m ({{legacy}})
 
(20 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
{{legacy}}
 +
<source lang="php">
 
<?php
 
<?php
# Extension:PublicCat{{Category:Extensions}}{{php}}
 
 
# - 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]{{Category:Extensions created with Template:Extension}}
+
# - Author: [http://www.organicdesign.co.nz/aran Aran Dunkley]
  
 
if (!defined('MEDIAWIKI')) die('Not an entry point.');
 
if (!defined('MEDIAWIKI')) die('Not an entry point.');
  
define('PUBLICCAT_VERSION','0.0.0, 2007-06-16');
+
define('PUBLICCAT_VERSION','1.0.7, 2008-10-28');
  
 
# 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';
+
if (!isset($wgPublicCat)) $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\\./';
+
if (!isset($wgPublicCatPrivatePattern)) $wgPublicCatPrivatePattern = '/^wiki\\./';
 
 
# Disable all actions except view for anonymous users
 
$wgGroupPermissions['*']['createaccount'] = false;
 
$wgGroupPermissions['*']['read']          = true;
 
$wgGroupPermissions['*']['edit']          = false;
 
$wgGroupPermissions['*']['createpage']    = false;
 
$wgGroupPermissions['*']['createtalk']    = false;
 
 
 
# Allow unrestricted access to login, home and main css
 
$wgWhitelistRead = array('Special:Userlogin','Main Page','-');
 
 
 
$wgExtensionFunctions[] = 'wfSetupPublicCat';
 
  
 
$wgExtensionCredits['other'][] = array(
 
$wgExtensionCredits['other'][] = array(
Line 32: Line 21:
 
'url'        => 'http://www.mediawiki.org/wiki/Extension:PublicCat',
 
'url'        => 'http://www.mediawiki.org/wiki/Extension:PublicCat',
 
'version'    => PUBLICCAT_VERSION
 
'version'    => PUBLICCAT_VERSION
);
+
);
 +
 
 +
# Hook in after article and title prepared
 +
# - should be able to hook in earlier than this,
 +
# - but note that it must be a hook that always executes, even for specialpages
 +
$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
 +
$wgGroupPermissions['*']['createaccount'] = false;
 +
$wgGroupPermissions['*']['read']          = $wgPublicCatRequestIsPublic;
 +
$wgGroupPermissions['*']['edit']          = false;
 +
$wgGroupPermissions['*']['createpage']    = false;
 +
$wgGroupPermissions['*']['createtalk']    = false;
 +
 +
# Allow unrestricted access to login and css's
 +
$wgWhitelistRead = array('Special:Userlogin','-');
 +
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
Line 42: Line 47:
 
if ($_REQUEST['action'] != 'view' && $_REQUEST['action'] != 'raw' && $_REQUEST['action'] != 'render')
 
if ($_REQUEST['action'] != 'view' && $_REQUEST['action'] != 'raw' && $_REQUEST['action'] != 'render')
 
$_REQUEST['action'] = 'view';
 
$_REQUEST['action'] = 'view';
}
+
}
 
 
# Function to die returing a 404 not found error
 
function wfPublicCat404() {
 
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>
 
<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);
 
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 inCat($title, $cat) {
if (!is_object($title)) $title = Title::newFromText($title));
+
if (!is_object($title)) $title = Title::newFromText($title);
$id     = $title->getArticleID()
+
$id   = $title->getArticleID();
$db      = &wfGetDB(DB_SLAVE);
+
$dbr  = &wfGetDB(DB_SLAVE);
$cl     = $db->tableName('categorylinks');
+
$cat  = $dbr->addQuotes($cat);
$result  = $db->query("SELECT 0 FROM $cl WHERE cl_from = $id AND cl_to = '$cat'");
+
$cl   = $dbr->tableName('categorylinks');
return is_array(mysql_fetch_row($result));
+
return $dbr->selectRow($cl, '0', "cl_from = $id AND cl_to = $cat", __METHOD__);
}
+
}
  
 
# 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 wfSetupPublicCat() {
+
function wfPublicCatValidate() {
global $wgPublicCatPattern,$wgPublicCatRequestIsPublic;
+
global $wgPublicCat,$wgPublicCatRequestIsPublic,$wgTitle;
if ($wgPublicCatRequestIsPublic && !wfPublicCatInCat($_REQUEST['title'],$wgPublicCatPattern) wf404();
+
if ($wgPublicCatRequestIsPublic && !wfPublicCatInCat($wgTitle,$wgPublicCat))
}
+
wfHttpError(404,"404 Not Found","The requested URL was not found on this server!");
 
+
return true;
?>
+
}
 +
</source>
 +
[[Category:Extensions]]

Latest revision as of 12:46, 30 August 2015

Legacy.svg Legacy: This article describes a concept that has been superseded in the course of ongoing development on the Organic Design wiki. Please do not develop this any further or base work on this concept, this is only useful for a historic record of work done. You may find a link to the currently used concept or function in this article, if not you can contact the author to find out what has taken the place of this legacy item.
<?php
# - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
# - Author: [http://www.organicdesign.co.nz/aran Aran Dunkley]

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

define('PUBLICCAT_VERSION','1.0.7, 2008-10-28');

# Name of category in which to place articles to make them viewable from the public site
if (!isset($wgPublicCat)) $wgPublicCat = 'Public';

# Pattern to match with domain to determine if client accessing private wiki or public website
if (!isset($wgPublicCatPrivatePattern)) $wgPublicCatPrivatePattern = '/^wiki\\./';

$wgExtensionCredits['other'][] = array(
	'name'        => 'PublicCat',
	'author'      => '[http://www.organicdesign.co.nz/nad 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
);

# Hook in after article and title prepared
# - should be able to hook in earlier than this,
# - but note that it must be a hook that always executes, even for specialpages
$wgHooks['OutputPageBeforeHTML'][] = 'wfPublicCatValidate';

# Determine if the request is private or public from the domain and pattern
$wgPublicCatRequestIsPrivate = preg_match($wgPublicCatPrivatePattern,$_SERVER['HTTP_HOST']);
$wgPublicCatRequestIsPublic  = !$wgPublicCatRequestIsPrivate;

# Disable all actions except view for anonymous users
$wgGroupPermissions['*']['createaccount'] = false;
$wgGroupPermissions['*']['read']          = $wgPublicCatRequestIsPublic;
$wgGroupPermissions['*']['edit']          = false;
$wgGroupPermissions['*']['createpage']    = false;
$wgGroupPermissions['*']['createtalk']    = false;

# Allow unrestricted access to login and css's
$wgWhitelistRead = array('Special:Userlogin','-');
if (eregi('\\.css$',$_REQUEST['title'])) $wgWhitelistRead[] = $_REQUEST['title'];

# 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';
}

# Function to check if passed a title is in a category
function inCat($title, $cat) {
	if (!is_object($title)) $title = Title::newFromText($title);
	$id   = $title->getArticleID();
	$dbr  = &wfGetDB(DB_SLAVE);
	$cat  = $dbr->addQuotes($cat);
	$cl   = $dbr->tableName('categorylinks');
	return $dbr->selectRow($cl, '0', "cl_from = $id AND cl_to = $cat", __METHOD__);
}

# 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))
		wfHttpError(404,"404 Not Found","The requested URL was not found on this server!");
	return true;
}