r19899 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r19898‎ | r19899 | r19900 >
Date:21:50, 12 February 2007
Author:daniel
Status:old
Tags:
Comment:
mail contact page for anon visitors
Modified paths:
  • /trunk/extensions/ContactPage (added) (history)
  • /trunk/extensions/ContactPage/ContactPage.i18n.de.php (added) (history)
  • /trunk/extensions/ContactPage/ContactPage.i18n.php (added) (history)
  • /trunk/extensions/ContactPage/ContactPage.php (added) (history)
  • /trunk/extensions/ContactPage/README (added) (history)
  • /trunk/extensions/ContactPage/SpecialContact.php (added) (history)
  • /trunk/extensions/ContactPage/install.settings (added) (history)

Diff [purge]

Index: trunk/extensions/ContactPage/ContactPage.i18n.php
@@ -0,0 +1,25 @@
 2+<?php
 3+
 4+/**
 5+ * Internationalisation file for the ContactPage extension
 6+ *
 7+ * @package MediaWiki
 8+ * @subpackage Extensions
 9+ * @author Daniel Kinzler, brightbyte.de
 10+ * @copyright © 2007 Daniel Kinzler
 11+ * @licence GNU General Public Licence 2.0 or later
 12+ */
 13+
 14+$messages['contactpage']= 'ContactPage';
 15+$messages['contactpage-title']= 'Contact';
 16+$messages['contactpage-pagetext']= 'Please use the form below to contact us.';
 17+$messages['contactpage-defsubject']= 'Contact Message';
 18+
 19+$messages['contactpage-fromname']= 'your name *';
 20+$messages['contactpage-fromaddress']= 'your email **';
 21+
 22+$messages['contactpage-formfootnotes']= '
 23+* optional<br/>
 24+** optional but needed if you want an answer
 25+';
 26+?>
Index: trunk/extensions/ContactPage/README
@@ -0,0 +1,51 @@
 2+--------------------------------------------------------------------------
 3+README for the CategoryTree extension
 4+Copyright © 2006 Daniel Kinzler
 5+Licenses: GNU General Public Licence (GPL)
 6+ GNU Free Documentation License (GFDL)
 7+--------------------------------------------------------------------------
 8+
 9+The ContactPage extension implements a contact form for visitors. It
 10+creates a special page Special:Contact, which is similar to
 11+Special:Emailuser, but it has a fixed recipient, and can be used
 12+anonymously.
 13+
 14+<http://meta.wikimedia.org/wiki/Contact_form_extension>
 15+
 16+The ContactPage extension was originally written by Daniel Kinzler in 2007
 17+and is released under the GNU General Public Licence (GPL). It is based on
 18+the code in SpecialEmailuser.php in the MediaWiki core.
 19+The internationalization files contain contributions by several people;
 20+they are mentioned in each file individually.
 21+
 22+
 23+INSTALLING
 24+--------------------------------------------------------------------------
 25+
 26+Copy the ContactPage directory into the extensions folder of your
 27+MediaWiki installation. Then add the following lines to your
 28+LocalSettings.php file (near the end):
 29+
 30+ require_once( "$IP/extensions/ContactPage/ContactPage.php" );
 31+
 32+ $wgContactUser = 'WikiAdmin';
 33+ $wgContactSender = 'apache@' . $wgServerName;
 34+ $wgContactSenderName = 'Contact Form on ' . $wgSitename;
 35+
 36+CONFIGURATION
 37+--------------------------------------------------------------------------
 38+
 39+$wgContactUser must be the name of a registered wiki user, who has
 40+supplied an email address, has user-to-user email enabled, and has
 41+confirmed his/her email address if that is required on this wiki
 42+(see $wgEmailAuthentication).
 43+
 44+$wgContactSender must be the email address used as the sender of the
 45+contact email. Depending on the setup of your web host, this may have to
 46+be an existing mail account.
 47+
 48+$wgContactSenderName is the name associated with the contact sender.
 49+This will be shown in the recipient's email program.
 50+
 51+--------------------------------------------------------------------------
 52+EOF
\ No newline at end of file
Index: trunk/extensions/ContactPage/ContactPage.php
@@ -0,0 +1,66 @@
 2+<?php
 3+/**
 4+ * Setup for ContactPage extension, a special page that implements a contact form
 5+ * for use by anonymous visitors.
 6+ *
 7+ * @package MediaWiki
 8+ * @subpackage Extensions
 9+ * @author Daniel Kinzler, brightbyte.de
 10+ * @copyright © 2007 Daniel Kinzler
 11+ * @licence GNU General Public Licence 2.0 or later
 12+ */
 13+
 14+$wgExtensionCredits['specialpage'][] = array(
 15+ 'name' => 'Contact',
 16+ 'author' => 'Daniel Kinzler',
 17+ 'url' => 'http://meta.wikimedia.org/wiki/Contact_form_extension',
 18+ 'description' => 'contact form for visitors',
 19+);
 20+
 21+$wgAutoloadClasses['SpecialContact'] = dirname( __FILE__ ) . '/SpecialContact.php';
 22+$wgSpecialPages['Contact'] = 'SpecialContact';
 23+
 24+$wgContactUser = NULL;
 25+$wgContactSender = 'apache@' . $wgServerName;
 26+$wgContactSenderName = 'Contact Form on ' . $wgSitename;
 27+
 28+/**
 29+* load the CategoryTree internationalization file
 30+*/
 31+function cpLoadMessages() {
 32+ global $wgLang;
 33+
 34+ $messages= array();
 35+
 36+ $f= dirname( __FILE__ ) . '/ContactPage.i18n.php';
 37+ include( $f );
 38+
 39+ $f= dirname( __FILE__ ) . '/ContactPage.i18n.' . $wgLang->getCode() . '.php';
 40+ if ( file_exists( $f ) ) include( $f );
 41+
 42+ return $messages;
 43+}
 44+
 45+/**
 46+* Get a ContactPage message, "contactpage-" prefix added automatically
 47+*/
 48+function cpMsg( $msg /*, ...*/ ) {
 49+ static $initialized = false;
 50+ global $wgMessageCache;
 51+ if ( !$initialized ) {
 52+ $wgMessageCache->addMessages( cpLoadMessages() );
 53+ $initialized = true;
 54+ }
 55+ if ( $msg === false ) {
 56+ return null;
 57+ }
 58+ $args = func_get_args();
 59+ $msg = array_shift( $args );
 60+ if ( $msg == '' ) {
 61+ return wfMsgReal( $msg, $args );
 62+ } else {
 63+ return wfMsgReal( "contactpage-$msg", $args );
 64+ }
 65+}
 66+
 67+?>
\ No newline at end of file
Index: trunk/extensions/ContactPage/SpecialContact.php
@@ -0,0 +1,221 @@
 2+<?php
 3+/**
 4+ * Speclial:Contact, a contact form for visitors.
 5+ * Based on SpecialEmailUser.php
 6+ *
 7+ * @addtogroup SpecialPage
 8+ * @author Daniel Kinzler, brightbyte.de
 9+ * @copyright © 2007 Daniel Kinzler
 10+ * @licence GNU General Public Licence 2.0 or later
 11+ */
 12+
 13+/**
 14+ *
 15+ */
 16+require_once('UserMailer.php');
 17+
 18+class SpecialContact extends SpecialPage {
 19+
 20+ /**
 21+ * Constructor
 22+ */
 23+ function __construct() {
 24+ global $wgOut;
 25+ SpecialPage::SpecialPage( 'Contact', '', true );
 26+
 27+ #inject messages
 28+ cpMsg(false);
 29+ }
 30+
 31+ /**
 32+ * Main execution function
 33+ * @param $par Parameters passed to the page
 34+ */
 35+ function execute( $par ) {
 36+ global $wgUser, $wgOut, $wgRequest, $wgEnableEmail, $wgContactUser, $wgContactSender;
 37+
 38+ if( !$wgEnableEmail || !$wgContactUser || !$wgContactSender) {
 39+ $wgOut->showErrorPage( "nosuchspecialpage", "nospecialpagetext" );
 40+ return;
 41+ }
 42+
 43+ $action = $wgRequest->getVal( 'action' );
 44+
 45+ $nu = User::newFromName( $wgContactUser );
 46+ if( is_null( $nu ) || !$nu->canReceiveEmail() ) {
 47+ wfDebug( "Target is invalid user or can't receive.\n" );
 48+ $wgOut->showErrorPage( "noemailtitle", "noemailtext" );
 49+ return;
 50+ }
 51+
 52+ $f = new EmailContactForm( $nu );
 53+
 54+ if ( "success" == $action ) {
 55+ $f->showSuccess( );
 56+ } else if ( "submit" == $action && $wgRequest->wasPosted() &&
 57+ $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
 58+ $f->doSubmit();
 59+ } else {
 60+ $f->showForm();
 61+ }
 62+ }
 63+}
 64+
 65+/**
 66+ * @todo document
 67+ * @addtogroup SpecialPage
 68+ */
 69+class EmailContactForm {
 70+
 71+ var $target;
 72+ var $text, $subject;
 73+ var $cc_me; // Whether user requested to be sent a separate copy of their email.
 74+
 75+ /**
 76+ * @param User $target
 77+ */
 78+ function EmailContactForm( $target ) {
 79+ global $wgRequest, $wgUser;
 80+ $this->target = $target;
 81+ $this->text = $wgRequest->getText( 'wpText' );
 82+ $this->subject = $wgRequest->getText( 'wpSubject' );
 83+ $this->cc_me = $wgRequest->getBool( 'wpCCMe' );
 84+
 85+ $this->fromname = $wgRequest->getText( 'wpFromName' );
 86+ $this->fromaddress = $wgRequest->getText( 'wpFromAddress' );
 87+
 88+ if ($wgUser->isLoggedIn()) {
 89+ if (!$this->fromname) $this->fromname = $wgUser->getName();
 90+ if (!$this->fromaddress) $this->fromaddress = $wgUser->getEmail();
 91+ }
 92+ }
 93+
 94+ function showForm() {
 95+ global $wgOut, $wgUser, $wgContactSender;
 96+
 97+ #TODO: show captcha
 98+
 99+ $wgOut->setPagetitle( cpMsg( "title" ) );
 100+ $wgOut->addWikiText( cpMsg( "pagetext" ) );
 101+
 102+ if ( $this->subject === "" ) {
 103+ $this->subject = cpMsg( "defsubject" );
 104+ }
 105+
 106+ #$emf = wfMsg( "emailfrom" );
 107+ #$sender = $wgContactSender;
 108+ $emt = wfMsg( "emailto" );
 109+ $rcpt = $this->target->getName();
 110+ $emr = wfMsg( "emailsubject" );
 111+ $emm = wfMsg( "emailmessage" );
 112+ $ems = wfMsg( "emailsend" );
 113+ $emc = wfMsg( "emailccme" );
 114+ $emfn = cpMsg( "fromname" );
 115+ $emfa = cpMsg( "fromaddress" );
 116+ $encSubject = htmlspecialchars( $this->subject );
 117+ $encFromName = htmlspecialchars( $this->fromname );
 118+ $encFromAddress = htmlspecialchars( $this->fromaddress );
 119+
 120+ $titleObj = SpecialPage::getTitleFor( "Contact" );
 121+ $action = $titleObj->escapeLocalURL( "action=submit" );
 122+ $token = $wgUser->editToken();
 123+
 124+ $wgOut->addHTML( "
 125+<form id=\"emailuser\" method=\"post\" action=\"{$action}\">
 126+<table border='0' id='mailheader'>
 127+<tr>
 128+<td align='right'>{$emr}:</td>
 129+<td align='left'>
 130+<input type='text' size='60' maxlength='200' name=\"wpSubject\" value=\"{$encSubject}\" />
 131+</td>
 132+</tr><tr>
 133+<td align='right'>{$emfn}:</td>
 134+<td align='left'>
 135+<input type='text' size='60' maxlength='200' name=\"wpFromName\" value=\"{$encFromName}\" />
 136+</td>
 137+<tr>
 138+<td align='right'>{$emfa}:</td>
 139+<td align='left'>
 140+<input type='text' size='60' maxlength='200' name=\"wpFromAddress\" value=\"{$encFromAddress}\" />
 141+</td>
 142+</tr>
 143+<tr>
 144+<td></td>
 145+<td align='left'>
 146+<small>".cpMsg( "formfootnotes" )."</small>
 147+</td>
 148+</tr>
 149+</table>
 150+<span id='wpTextLabel'><label for=\"wpText\">{$emm}:</label><br /></span>
 151+<textarea name=\"wpText\" rows='20' cols='80' wrap='virtual' style=\"width: 100%;\">" . htmlspecialchars( $this->text ) .
 152+"</textarea>
 153+" . wfCheckLabel( $emc, 'wpCCMe', 'wpCCMe', $wgUser->getBoolOption( 'ccmeonemails' ) ) . "<br />
 154+<input type='submit' name=\"wpSend\" value=\"{$ems}\" />
 155+<input type='hidden' name='wpEditToken' value=\"$token\" />
 156+</form>\n" );
 157+
 158+ }
 159+
 160+ function doSubmit( ) {
 161+ global $wgOut, $wgContactSender, $wgContactSenderName;
 162+
 163+ #TODO: check captcha
 164+
 165+ $fname = 'EmailContactForm::doSubmit';
 166+
 167+ wfDebug( "$fname: start\n" );
 168+
 169+ $to = new MailAddress( $this->target );
 170+ $from = new MailAddress( $wgContactSender, $wgContactSenderName );
 171+ $replyto = $this->fromaddress ? new MailAddress( $this->fromaddress, $this->fromname ) : NULL;
 172+ $subject = $this->subject;
 173+
 174+ if( wfRunHooks( 'ContactForm', array( &$to, &$replyto, &$subject, &$this->text ) ) ) {
 175+
 176+ wfDebug( "$fname: sending mail from ".$from->toString()." to ".$to->toString()." replyto ".($replyto==null?'-/-':$replyto->toString())."\n" );
 177+ $mailResult = userMailer( $to, $from, $subject, $this->text, $replyto ? $replyto->toString() : NULL ); #TODO: staring with MW 1.10, $replyto should be passed as an object
 178+
 179+ if( WikiError::isError( $mailResult ) ) {
 180+ $wgOut->addHTML( wfMsg( "usermailererror" ) . $mailResult);
 181+ } else {
 182+
 183+ // if the user requested a copy of this mail, do this now,
 184+ // unless they are emailing themselves, in which case one copy of the message is sufficient.
 185+ if ($this->cc_me && $replyto) {
 186+ $cc_subject = wfMsg('emailccsubject', $this->target->getName(), $subject);
 187+ if( wfRunHooks( 'ContactForm', array( &$from, &$replyto, &$cc_subject, &$this->text ) ) ) {
 188+ wfDebug( "$fname: sending cc mail from ".$from->toString()." to ".$replyto->toString()."\n" );
 189+ $ccResult = userMailer( $from, $replyto, $cc_subject, $this->text );
 190+ if( WikiError::isError( $ccResult ) ) {
 191+ // At this stage, the user's CC mail has failed, but their
 192+ // original mail has succeeded. It's unlikely, but still, what to do?
 193+ // We can either show them an error, or we can say everything was fine,
 194+ // or we can say we sort of failed AND sort of succeeded. Of these options,
 195+ // simply saying there was an error is probably best.
 196+ $wgOut->addHTML( wfMsg( "usermailererror" ) . $ccResult);
 197+ return;
 198+ }
 199+ }
 200+ }
 201+
 202+ wfDebug( "$fname: success\n" );
 203+
 204+ $titleObj = SpecialPage::getTitleFor( "Contact" );
 205+ $wgOut->redirect( $titleObj->getFullURL( "action=success" ) );
 206+ wfRunHooks( 'ContactFromComplete', array( $to, $replyto, $subject, $this->text ) );
 207+ }
 208+ }
 209+
 210+ wfDebug( "$fname: end\n" );
 211+ }
 212+
 213+ function showSuccess( ) {
 214+ global $wgOut;
 215+
 216+ $wgOut->setPagetitle( wfMsg( "emailsent" ) );
 217+ $wgOut->addHTML( wfMsg( "emailsenttext" ) );
 218+
 219+ $wgOut->returnToMain( false );
 220+ }
 221+}
 222+?>
Index: trunk/extensions/ContactPage/ContactPage.i18n.de.php
@@ -0,0 +1,25 @@
 2+<?php
 3+
 4+/**
 5+ * Internationalisation file for the ContactPage extension
 6+ *
 7+ * @package MediaWiki
 8+ * @subpackage Extensions
 9+ * @author Daniel Kinzler, brightbyte.de
 10+ * @copyright © 2007 Daniel Kinzler
 11+ * @licence GNU General Public Licence 2.0 or later
 12+ */
 13+
 14+$messages['contactpage']= 'Kontaktseite';
 15+$messages['contactpage-title']= 'Kontakt';
 16+$messages['contactpage-pagetext']= 'Mit diesem Formular können Sie uns Nachrichten zukommen lassen.';
 17+$messages['contactpage-defsubject']= 'Kontaktnachricht';
 18+
 19+$messages['contactpage-fromname']= 'Ihr Name *';
 20+$messages['contactpage-fromaddress']= 'Ihre E-Mail Adresse **';
 21+
 22+$messages['contactpage-formfootnotes']= '
 23+* optional<br/>
 24+** optional, wird aber benötigt um Ihnen antworten zu können
 25+';
 26+?>
Index: trunk/extensions/ContactPage/install.settings
@@ -0,0 +1,5 @@
 2+require_once( "{{path}}/ContactPage.php" );
 3+
 4+$wgContactUser = 'WikiAdmin';
 5+$wgContactSender = 'apache@' . $wgServerName;
 6+$wgContactSenderName = 'Contact Form on ' . $wgSitename;