Skip to content

Commit

Permalink
Added new functionality to reply to mails via mod forum and send repl…
Browse files Browse the repository at this point in the history
…y as mail
  • Loading branch information
diptobi1 committed Oct 21, 2022
1 parent cfd23aa commit e110ffe
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 27 deletions.
8 changes: 8 additions & 0 deletions config.sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,14 @@ class Config
public static $modEMailServerHTTP='https://webmail.your-server.de'; // URL-Link to the server
public static $modEMailLogin='';
public static $modEMailPassword='';
public static $modEMailName='vDiplomacy Moderator Team';

/**
* Site name to be shown at various places on the application and inside mails.
*
* @var string
*/
public static $siteName='vDiplomacy';

/**
* If you use the piwik-webanalyser define his path here. If not comment this out.
Expand Down
4 changes: 2 additions & 2 deletions lib/html.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ static function alert()
* The points icon
* @return string
*/
static function points()
static function points($path = '')
{
return ' <img src="'.l_s('images/icons/points.png').'" alt="D" title="'.l_t('webDiplomacy points').'" />';
return ' <img src="'.$path.l_s('images/icons/points.png').'" alt="D" title="'.l_t('webDiplomacy points').'" />';
}

static function forumMessage($threadID, $messageID)
Expand Down
18 changes: 13 additions & 5 deletions modforum.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,9 @@
{
// To a thread
$threadDetails = $DB->sql_hash(
"SELECT f.id, f.latestReplySent, f.assigned, u.type as userType
"SELECT f.id, f.latestReplySent, f.assigned, u.type as userType, f.subject, f.fromMail
FROM wD_ModForumMessages f
INNER JOIN wD_Users u ON ( f.fromUserID = u.id )
LEFT JOIN wD_Users u ON ( f.fromUserID = u.id )
WHERE f.id=".$new['sendtothread']."
AND f.type='ThreadStart'");

Expand All @@ -234,9 +234,10 @@
$new['id'] = ModForumMessage::send( $new['sendtothread'],
$fromUserID,
$new['message'],
'',
'RE: '.$threadDetails['subject'],
'ThreadReply',
(isset($_REQUEST['ReplyAdmin']) ? 'Yes' : 'No')
(isset($_REQUEST['ReplyAdmin']) ? 'Yes' : 'No'),
$threadDetails['fromMail']
);

$_SESSION['lastPostText']=$new['message'];
Expand Down Expand Up @@ -943,7 +944,14 @@ function readModThread(threadID, lastMessageID) {
{
print '<input type="submit" ';
if (strpos($message['userType'],'Moderator')===false && $User->type['Moderator'])
print 'onclick="return confirm(\'Are you sure you want post this reply visible for the thread-starter too?\');"';
{
if( isset($message['fromMail']) )
{
print 'onclick="return confirm(\'Are you sure you want send this reply visible as email to \\\''.$message['fromMail'].'\\\'?\');"';
} else {
print 'onclick="return confirm(\'Are you sure you want post this reply visible for the thread-starter too?\');"';
}
}
print 'class="form-submit" value="Post reply" name="Reply">';

if (strpos($message['userType'],'Moderator')===false && $User->type['Moderator'])
Expand Down
49 changes: 44 additions & 5 deletions modforum/libMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,37 +41,76 @@ static public function linkify($message)
return preg_replace($patterns, $replacements, $message);
}

static public function linkifyWithAbsPaths($message)
{
$message=self::splitWords($message);

$baseUrl = 'https://'.$_SERVER['SERVER_NAME'].'/';

$patterns = array(
'/gameID[:= _]?([0-9]+)/i',
'/userID[:= _]?([0-9]+)/i',
'#(modforum.php.*viewthread[:= _]?)([0-9]+)#i',
'#/forum.php.*threadID[:= _]?([0-9]+)#i',
'/((?:[^a-z0-9])|(?:^))([0-9]+) ?(?:(?:D)|(?:points))((?:[^a-z])|(?:$))/i',
);
$replacements = array(
'<a href="'.$baseUrl.'board.php?gameID=\1" class="light">gameID=\1</a>',
'<a href="'.$baseUrl.'profile.php?userID=\1" class="light">userID=\1</a>',
'<a href="'.$baseUrl.'modforum.php?viewthread=\2#\2" class="light">\1\2</a>',
'<a href="'.$baseUrl.'forum.php?threadID=\1#\1" class="light">'.$baseUrl.'/forum.php?threadID=\1</a>',
'\1\2'.libHTML::points($baseUrl).'\3'
);

return preg_replace($patterns, $replacements, $message);
}

/**
* Send a message to the public forum. The variables passed are assumed to be already sanitized
*
* @param int $toID User/Thread ID to send to
* @param int $fromUserID UserID sent from
* @param string $message The message to be sent
* @param string $plainMessage The message to be sent
* @param string[optional] $subject The subject
* @param string[optional] $type 'Bulletin'(GameMaster->Player) 'ThreadStart'(User->All) 'ThreadReply'(User->Thread)
* @param string[optional] $adminReply 'No'(show to recipient) 'Yes'(show only internally)
* @param string[optional] $sendMailTo If set will be used to send an email to the provided address.
*
* @return int The message ID
*/
static public function send($toID, $fromUserID, $message, $subject="", $type='Bulletin', $adminReply='No')
static public function send($toID, $fromUserID, $message, $subject="", $type='Bulletin', $adminReply='No', $sendMailTo=NULL)
{
global $DB, $User;

if( defined('AdminUserSwitch') && AdminUserSwitch != $User->id) $fromUserID = AdminUserSwitch;

$message = self::linkify($message);
$linkifiedMessage = self::linkify($message);

$sentTime=time();

if( 65000 < strlen($message) )
if( 65000 < strlen($linkifiedMessage) )
{
throw new Exception("Message too long");
}

$sendMail = ( $sendMailTo !== NULL && $adminReply === 'No' );
if( $sendMail )
{
require_once(l_r('objects/mailer.php'));
$Mailer = new Mailer();
$Mailer->Send(array($sendMailTo=>$sendMailTo), $subject, self::linkifyWithAbsPaths($message), 'mod');
}

if( $type === 'ThreadReply' )
{
$subject="";
}

libCache::wipeDir(libCache::dirName('mod_forum'));

$DB->sql_put("INSERT INTO wD_ModForumMessages
SET toID = ".$toID.", fromUserID = ".$fromUserID.", timeSent = ".$sentTime.",
message = '".$message."', subject = '".$subject."', replies = 0,
message = '".$linkifiedMessage."', subject = '".$subject."', replies = 0,
type = '".$type."', latestReplySent = 0, adminReply = '".$adminReply."'");

$id = $DB->last_inserted();
Expand Down
52 changes: 37 additions & 15 deletions objects/mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ public function __construct()

$this->PHPMailer = new PHPMailer();

$this->PHPMailer->setFrom(Config::$mailerConfig['From'], Config::$mailerConfig['FromName']);
$this->PHPMailer->addReplyTo(Config::$mailerConfig['From'], Config::$mailerConfig['FromName']);
$this->PHPMailer->wordWrap = 50;
$this->PHPMailer->isHTML(true);

Expand Down Expand Up @@ -80,31 +78,55 @@ private function Clear()
$this->PHPMailer->clearReplyTos();
}

public function Send(array $to, $title, $message)
public function Send(array $to, $title, $message, $fromMail = 'generated')
{
if( $fromMail == 'generated' )
{
$from = Config::$mailerConfig['From'];
$fromName = Config::$mailerConfig['FromName'];
$generated = true;
}
else if( $fromMail == 'mod' )
{
$from = Config::$modEMail;
$fromName = Config::$modEMailName;
$generated = false;
}
else
trigger_error(l_t("Invalid mailer sender config chosen."));

if( $this->useDebug )
{
print '<p>'.l_t('Mailed "%s": <b>%s</b> - %s</p>','<em>&lt;'.implode('&gt;, &lt;',$to).'&gt;</em>',$title,$message);
return;
}

$title = l_t('webDiplomacy: %s (no reply)',$title);
$this->PHPMailer->setFrom($from, $fromName);
$this->PHPMailer->addReplyTo($from, $fromName);

if( $generated )
$title = l_t('%s: %s (no reply)', Config::$siteName, $title);
$this->PHPMailer->Subject = $title;


foreach( $to as $email=>$name )
{
$wrappedmessage = '
<p>'.l_t('Hello %s,',$name).'</p>
<p style="color:#222">
'.$message."
</p>
<p style='color:#555'>".l_t('Kind regards,')."<br />
".Config::$mailerConfig['FromName']."</p>
<p style='font-size:90%; font-style:italic; color:#555'>
".l_t("This message was generated by a webDiplomacy server, sent from %s to %s (%s) at %s (GMT+0), on behalf of %s.",
Config::$mailerConfig['From'],
$wrappedmessage = '';

if( $generated )
$wrappedmessage .= '<p>'.l_t('Hello %s,',$name).'</p>';

$wrappedmessage .= '<p style="color:#222">
'.$message.'
</p>';
if( $generated )
$wrappedmessage .= "<p style='color:#555'>".l_t('Kind regards,')."<br />
".$fromName."</p>";

$wrappedmessage .= "<p style='font-size:90%; font-style:italic; color:#555'>
".l_t("This message was generated by a %s server, sent from %s to %s (%s) at %s (GMT+0), on behalf of %s.",
Config::$siteName,
$from,
$email,
$name,
gmdate("Y-m-d\TH:i:s\Z"),
Expand Down

0 comments on commit e110ffe

Please sign in to comment.