r59449 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r59448‎ | r59449 | r59450 >
Date:14:08, 26 November 2009
Author:freakolowsky
Status:deferred
Tags:
Comment:
Simple sendmail 2 wiki redirection extension
Modified paths:
  • /trunk/extensions/SendmailToWiki (added) (history)
  • /trunk/extensions/SendmailToWiki/SendmailToWiki.i18n.php (added) (history)
  • /trunk/extensions/SendmailToWiki/SendmailToWiki.php (added) (history)
  • /trunk/extensions/SendmailToWiki/SendmailToWiki_body.php (added) (history)
  • /trunk/extensions/SendmailToWiki/scripts (added) (history)
  • /trunk/extensions/SendmailToWiki/scripts/post_to_wiki.sh (added) (history)

Diff [purge]

Index: trunk/extensions/SendmailToWiki/SendmailToWiki_body.php
@@ -0,0 +1,116 @@
 2+<?php
 3+class SendmailToWiki extends SpecialPage {
 4+
 5+ function __construct() {
 6+ parent::__construct( 'SendmailToWiki' );
 7+ wfLoadExtensionMessages('SendmailToWiki');
 8+ }
 9+
 10+ function execute( $SPparams ) {
 11+ global $wgRequest, $wgOut, $wgParser, $sendmailtowikiPrefix;
 12+
 13+ $wgOut->disable();
 14+ $wgParser->firstCallInit();
 15+ $wgParser->mOptions = new ParserOptions();
 16+
 17+ header( "Content-type: text/html; charset=utf-8" );
 18+
 19+ $postTitle = $wgRequest->getText('posttitle');
 20+ preg_match_all('/\s*\[([^\]]*)\]\s*/', $postTitle, $postOpts);
 21+ if (isset($postOpts[0])) {
 22+ $postTitle = str_replace($postOpts[0], '', $postTitle);
 23+ $postOpts = $postOpts[1];
 24+ foreach ($postOpts as &$postOpt)
 25+ $postOpt = strtolower($postOpt);
 26+ }
 27+
 28+ if ($wgRequest->getText('postcontenttype') != 'text/plain' ) {
 29+ $this->sendError(400, 'sendmailtowiki-err-onlyplain', !in_array('quiet', $postOpts));
 30+ return;
 31+ }
 32+
 33+
 34+ $postSender = $wgRequest->getText('postsender');
 35+ $postAccount = preg_split('/\./', preg_replace('/\+/', '.', $wgRequest->getText('postaccount')));
 36+
 37+ if ($postAccount[0] != $sendmailtowikiPrefix) {
 38+ $this->sendError(400, 'sendmailtowiki-err-wrongprefix', !in_array('quiet', $postOpts));
 39+ return;
 40+ } elseif (!is_numeric($postAccount[1]) || !is_numeric($postAccount[2])) {
 41+ $this->sendError(400, 'sendmailtowiki-err-invalidaccount', !in_array('quiet', $postOpts));
 42+ return;
 43+ }
 44+
 45+ $postUser = User::newFromId($postAccount[1]);
 46+ if ($postUser->loadFromId() === FALSE) {
 47+ $this->sendError(400, 'sendmailtowiki-err-invalidaccount', !in_array('quiet', $postOpts));
 48+ return;
 49+ } elseif ($postUser->mEmail !== $postSender && !preg_match('/<'.$postUser->mEmail.'>/', $postSender)) {
 50+ $this->sendError(403, 'sendmailtowiki-err-invalidsender', !in_array('quiet', $postOpts));
 51+ return;
 52+ } elseif ($postUser->getOption('sendmailtowiki_inpin') !== $postAccount[2]) {
 53+ $this->sendError(403, 'sendmailtowiki-err-invalidpin', !in_array('quiet', $postOpts));
 54+ return;
 55+ }
 56+
 57+ $postTitleObj = Title::newFromText($postTitle);
 58+ global $wgTitle;
 59+ $wgTitle = $postTitleObj;
 60+ if(in_array('createonly', $postOpts) && $postTitleObj->exists()) {
 61+ $this->sendError(400, 'articleexists', !in_array('quiet', $postOpts));
 62+ return;
 63+ } elseif((in_array('source', $postOpts) || in_array('view', $postOpts) || in_array('nocreate', $postOpts)) && !$postTitleObj->exists()) {
 64+ $this->sendError(404, 'nocreatetitle', !in_array('quiet', $postOpts));
 65+ return;
 66+ }
 67+
 68+ $postArticleObj = new Article($postTitleObj);
 69+ if (in_array('source', $postOpts)) {
 70+ echo $postArticleObj->getContent();
 71+ return;
 72+ }elseif (in_array('view', $postOpts)) {
 73+ $this->sendOK($postArticleObj->getContent());
 74+ return;
 75+ }
 76+
 77+ $errors = $postTitleObj->getUserPermissionsErrors('edit', $postUser);
 78+ if(!$postTitleObj->exists())
 79+ $errors = array_merge($errors, $postTitleObj->getUserPermissionsErrors('create', $postUser));
 80+ if(count($errors)) {
 81+ $this->sendError(403, $errors[0], !in_array('quiet', $postOpts));
 82+ return;
 83+ }
 84+
 85+ $postContent = $wgRequest->getText('postcontent');
 86+ $postArticleObj->doEdit($postContent, '', 0, false, $postUser);
 87+
 88+ if (in_array('reply', $postOpts)) $this->sendOK($postContent);
 89+ }
 90+
 91+ function sendOK($content) {
 92+ global $wgParser;
 93+ $out = $wgParser->internalParse($content);
 94+ $wgParser->replaceLinkHolders($out, 0);
 95+ echo $wgParser->doBlockLevels($out);
 96+ }
 97+
 98+ function sendError($code, $message, $sendback = false) {
 99+ if (!$sendback)
 100+ return;
 101+
 102+ global $wgMessageCache, $wgParser;
 103+
 104+ switch ($code) {
 105+ case 400: header('HTTP/1.1 400 Bad Request'); break;
 106+ case 403: header('HTTP/1.1 403 Forbidden'); break;
 107+ case 404: header('HTTP/1.1 404 Not Found'); break;
 108+ }
 109+
 110+ $out = '';
 111+ $out .= "<h1>".$wgMessageCache->get('errorpagetitle').": $code</h1>\n";
 112+ $out .= "<h2>".$wgMessageCache->get($message)."</h2>\n";
 113+
 114+ echo $wgParser->replaceVariables($out);
 115+ }
 116+}
 117+
Index: trunk/extensions/SendmailToWiki/SendmailToWiki.i18n.php
@@ -0,0 +1,35 @@
 2+<?php
 3+$messages = array();
 4+
 5+/* *** English *** */
 6+$messages['en'] = array(
 7+ 'sendmailtowiki' => 'SendmailToWiki',
 8+ 'sendmailtowiki-desc' => "Post wiki content trough dedicated dynamic email address.",
 9+ 'prefs-sendmailtowiki' => "Posting content with email",
 10+ 'sendmailtowiki-inemail' => "Dedicated email:",
 11+ 'sendmailtowiki-inpin' => "PIN:",
 12+ 'prefs-help-sendmailtowiki_pin' => "Blank field for PIN number disables posting content to wiki with your account.",
 13+ 'sendmailtowiki-err-pinlength' => "PIN must contain exactly 5 numbers!",
 14+ 'sendmailtowiki-err-wrongprefix' => "Wrong wiki account prefix. Contact your administrator!",
 15+ 'sendmailtowiki-err-invalidaccount' => "Invalid account. Check the email address you're sending to.",
 16+ 'sendmailtowiki-err-invalidsender' => "Invalid sender. Check the email address you're sending from.",
 17+ 'sendmailtowiki-err-invalidpin' => "Invalid PIN. Access Denied.",
 18+ 'sendmailtowiki-err-onlyplain' => "Because of potential misinterpretatons only text/plain messages are accepted.",
 19+);
 20+
 21+/* *** German (Deutsch) *** */
 22+$messages['sl'] = array(
 23+ 'sendmailtowiki' => 'SendmailToWiki',
 24+ 'sendmailtowiki-desc' => 'Pošiljanje wiki vsebine preko namenskega dinamičnega email naslova.',
 25+ 'prefs-sendmailtowiki' => "Pošiljanje vsebine prek emaila",
 26+ 'sendmailtowiki-inemail' => "Namenski email:",
 27+ 'sendmailtowiki-inpin' => "PIN:",
 28+ 'prefs-help-sendmailtowiki_pin' => "Prazno polje za PIN številko onemogoči pošiljanje vsebine na wiki prek vašega računa.",
 29+ 'sendmailtowiki-err-pinlength' => "PIN mora vsebovati točno 5 številk!",
 30+ 'sendmailtowiki-err-wrongprefix' => "Napačna wiki predpona. Kontaktirajte vašega administratorja!",
 31+ 'sendmailtowiki-err-invalidaccount' => "Nepravilen račun. Preverite email naslov na katerega pošiljate.",
 32+ 'sendmailtowiki-err-invalidsender' => "Nepravilen pošiljatelj. Preverite email naslov iz katerega pošiljate.",
 33+ 'sendmailtowiki-err-invalidpin' => "Nepravilen PIN. Dostop zavrnjen.",
 34+ 'sendmailtowiki-err-onlyplain' => "Zaradi potencialnega nepravilnega interpretiranja so podprta samo text/plain sporočila.",
 35+);
 36+?>
Index: trunk/extensions/SendmailToWiki/scripts/post_to_wiki.sh
@@ -0,0 +1,37 @@
 2+#!/usr/bin/php
 3+<?php
 4+$stdin = fopen('php://stdin', 'r');
 5+$email = '';
 6+while (!feof($stdin)) {
 7+ $email .= fread($stdin, 1024);
 8+}
 9+fclose($stdin);
 10+
 11+$server = rtrim($argv[1], '/').'/index.php?title=Special:SendmailToWiki';
 12+$mailRes = mailparse_msg_create();
 13+$mail = mailparse_msg_parse($mailRes, $email);
 14+$mailData = mailparse_msg_get_part_data($mailRes);
 15+
 16+$mailBody = substr($email, $mailData['starting-pos-body'], $mailData['ending-pos-body']-$mailData['starting-pos-body']);
 17+
 18+$postData = 'postsender='.urlencode($mailData['headers']['from']);
 19+$postData .= '&postaccount='.urlencode(preg_replace('/@.*/', '', $mailData['headers']['to']));
 20+$postData .= '&posttitle='.urlencode($mailData['headers']['subject']);
 21+$postData .= '&postcontent='.urlencode($mailBody);
 22+$postData .= '&postcontenttype='.urlencode($mailData['content-type']);
 23+
 24+$csession = curl_init($server);
 25+curl_setopt($csession, CURLOPT_POST, 1);
 26+curl_setopt($csession, CURLOPT_POSTFIELDS, $postData);
 27+curl_setopt($csession, CURLOPT_FOLLOWLOCATION, 1);
 28+curl_setopt($csession, CURLOPT_RETURNTRANSFER, 1);
 29+$csessionReturn = curl_exec($csession);
 30+
 31+if ($csessionReturn != '') {
 32+ $headers = 'From: '.$mailData['headers']['to']."\r\n" .
 33+ 'Content-type: text/html; charset=UTF-8'."\r\n".
 34+ 'X-Mailer: PHP/'.phpversion();
 35+
 36+ mail($mailData['headers']['from'], 'RE: '.$mailData['headers']['subject'], $csessionReturn, $headers);
 37+}
 38+?>
Property changes on: trunk/extensions/SendmailToWiki/scripts/post_to_wiki.sh
___________________________________________________________________
Name: svn:executable
139 + *
Index: trunk/extensions/SendmailToWiki/SendmailToWiki.php
@@ -0,0 +1,70 @@
 2+<?php
 3+# Alert the user that this is not a valid entry point to MediaWiki if they try to access the special pages file directly.
 4+if (!defined('MEDIAWIKI')) {
 5+ echo <<<EOT
 6+To install my extension install sendmail redirection and put the following line in LocalSettings.php:
 7+require_once( "\$IP/extensions/SendmailToWiki/SendmailToWiki.php" );
 8+EOT;
 9+ exit( 1 );
 10+}
 11+
 12+$wgExtensionCredits['specialpage'][] = array(
 13+ 'name' => 'SendmailToWiki',
 14+ 'author' => 'Jure Kajzer - freakolowsky <jure.kajzer@abakus.si>',
 15+ 'url' => 'http://www.mediawiki.org/wiki/Extension:SendmailToWiki',
 16+ 'description' => 'Posting to wiki with sendmail redirection into a php script',
 17+ 'descriptionmsg' => 'sendmailtowiki-desc',
 18+ 'version' => '0.1.0',
 19+);
 20+
 21+$dir = dirname(__FILE__) . '/';
 22+
 23+$wgAutoloadClasses['SendmailToWiki'] = $dir . 'SendmailToWiki_body.php';
 24+$wgExtensionMessagesFiles['SendmailToWiki'] = $dir . 'SendmailToWiki.i18n.php';
 25+//$wgExtensionAliasesFiles['SendmailToWiki'] = $dir . 'SendmailToWiki.alias.php';
 26+$wgSpecialPages['SendmailToWiki'] = 'SendmailToWiki';
 27+
 28+$wgHooks['GetPreferences'][] = 'sendmailtowikiPrefHook';
 29+$wgHooks['userCan'][] = 'sendmailtowikiUserCanHook';
 30+
 31+//replace with custom prefix in LocalSettings.php
 32+$sendmailtowikiPrefix = 'wikipost';
 33+
 34+function sendmailtowikiPinValidate( $value, $alldata ) {
 35+ global $wgMessageCache;
 36+ if ($value != '' && strlen($value) != 5)
 37+ return $wgMessageCache->get('sendmailtowiki-err-pinlength');
 38+ return true;
 39+}
 40+
 41+function sendmailtowikiPrefHook( $user, &$preferences ) {
 42+ global $wgUser, $sendmailtowikiPrefix;
 43+ $preferences['sendmailtowiki_inemail'] = array(
 44+ 'type' => 'info',
 45+ 'label-message' => 'sendmailtowiki-inemail',
 46+ 'section' => 'personal/sendmailtowiki',
 47+ 'default' => $sendmailtowikiPrefix.'+'.$wgUser->getId().'.<PIN>@'.preg_replace("/^(.*\.)?([^.]*\..*)$/", "$2", $_SERVER['HTTP_HOST']),
 48+ );
 49+
 50+ $preferences['sendmailtowiki_inpin'] = array(
 51+ 'type' => 'int',
 52+ 'label-message' => 'sendmailtowiki-inpin',
 53+ 'section' => 'personal/sendmailtowiki',
 54+ 'validation-callback' => 'sendmailtowikiPinValidate',
 55+ 'help-message' => 'prefs-help-sendmailtowiki_pin',
 56+ );
 57+
 58+ return true;
 59+}
 60+
 61+function sendmailtowikiUserCanHook(&$title, &$user, $action, &$result) {
 62+ if ($user->isLoggedIn()) return true;
 63+ if ($title->getNamespace() == NS_SPECIAL && $title->mTextform == 'SendmailToWiki') {
 64+ $result = true;
 65+ return false;
 66+ }
 67+
 68+ return true;
 69+}
 70+
 71+?>

Status & tagging log