<?php
//adds self to proper hooks & parser functions
$wgHooks['EditFilter'][] = 'fnCheckEncryptHook';
$wgHooks['ArticleSave'][] = 'fnEncryptHook';
$wgExtensionFunctions[] = 'efCryptoParserFunction_Setup';
$wgHooks['LanguageGetMagic'][] = 'efCryptoParserFunction_Magic';
//the tag name that will be searched for to find text to encrypt
$encryptTag = 'encrypt';
//setsup the crypto name as a parser function
function efCryptoParserFunction_Setup() {
global $wgParser;
$wgParser->setFunctionHook( 'crypto', 'decryptoParserFunction_Render' );
}
//adding to magic words
function efCryptoParserFunction_Magic( &$magicWords, $langCode ) {
$magicWords['crypto'] = array( 0, 'crypto' );
return true;
}
//parser function to decrypt the thext
function decryptoParserFunction_Render( &$parser, $group = '', $text = '', $alt = '' ) {
global $wgUser, $cryptoGroupMap, $encryptTag;
$parser->disableCache();
$groups = $wgUser->getEffectiveGroups();
if ($wgUser->isLoggedIn() && in_array($group , $groups )) {
if (stripos($text, $encryptTag)) {
$tags = array('<'.$encyrptTag.'>', '</'.$encyrptTag.'>');
return str_replace($tags, "", $text);
}
else {
return XORDecrypt($text, $cryptoGroupMap[$group]);
}
}
else {
return $alt;
}
}
//Check if user is able to use the encryption hook
function fnCheckEncryptHook($editor, &$text, $section, &$error) {
global $cryptoGroupMap, $wgUser;
$idx = 0;
while (($idx = strpos($text, '{{', $idx)) > -1) {
$idx = $idx + 2;
$repIdx = $idx;
while($text[$idx] == ' ') {
$idx++;
}
if (substr($text, $idx, strlen('#crypto:')) == '#crypto:') {
$idx = $idx + strlen('#crypto:');
while($text[$idx] == ' ') {
$idx++;
}
$group = '';
while($text[$idx] != '|') {
if ($text[$idx] != ' ') {
$group = $group . $text[$idx];
}
$idx++;
}
$groups = $wgUser->getEffectiveGroups();
$crypt = $cryptoGroupMap[$group];
if ($crypt) {
if (!$wgUser->isLoggedIn() || !in_array( $group, $groups )) {
$error = "<p class='error'>You do not have permission to use the '$group' tag. To use this you need to be a member of that group.</p>";
break;
}
}
else {
$error = "<p class='error'>The group '$group' does not have a crypto key associated with it.</p>" . $cryptoGroupMap[$arr[0]];
break;
}
}
}
return true;
}
//the hook that does the encryption. I may later try to clean up this code with the method above
function fnEncryptHook(&$article, &$user, &$text, &$summary, $minor, $watch, $sectionanchor, &$flags) {
global $cryptoGroupMap, $wgUser, $encryptTag;
$idx = 0;
while (($idx = strpos($text, '{{', $idx)) > -1) {
$idx = $idx + 2;
$repIdx = $idx;
while($text[$idx] == ' ') {
$idx++;
}
if (substr($text, $idx, strlen('#crypto:')) == '#crypto:') {
$idx = $idx + strlen('#crypto:');
while($text[$idx] == ' ') {
$idx++;
}
$group = '';
while($text[$idx] != '|') {
if ($text[$idx] != ' ') {
$group = $group . $text[$idx];
}
$idx++;
}
$idx++;
while($text[$idx] == ' ') {
$idx++;
}
$beginTag = '<' . $encryptTag . '>';
if ($beginTag == substr($text, $idx, strlen($beginTag))) {
$beginRepIdx = $idx;
$endTag = '</' . $encryptTag . '>';
$endTagLen = stripos($text, $endTag, $idx) - $idx - strlen($beginTag);
$crypt = $cryptoGroupMap[$group];
if ($crypt) {
$subText = substr($text, $beginRepIdx + strlen($beginTag), $endTagLen);
$encText = XOREncrypt($subText, $crypt);
$text = substr_replace($text, $encText, $beginRepIdx, strlen($beginTag) + $endTagLen + strlen($endTag));
}
}
}
}
return true;
}
// Pilfered from http://www.jonasjohn.de/snippets/php/xor-encryption.htm
function XOREncryption($InputString, $KeyPhrase){
$KeyPhraseLength = strlen($KeyPhrase);
// Loop trough input string
for ($i = 0; $i < strlen($InputString); $i++){
// Get key phrase character position
$rPos = $i % $KeyPhraseLength;
// Magic happens here:
$r = ord($InputString[$i]) ^ ord($KeyPhrase[$rPos]);
// Replace characters
$InputString[$i] = chr($r);
}
return $InputString;
}
function XOREncrypt($InputString, $KeyPhrase){
$InputString = XOREncryption($InputString, $KeyPhrase);
$InputString = base64_encode($InputString);
return $InputString;
}
function XORDecrypt($InputString, $KeyPhrase){
$InputString = base64_decode($InputString);
$InputString = XOREncryption($InputString, $KeyPhrase);
return $InputString;
}