Replace "mail.php" with a controller

This commit is contained in:
Dmitriy Simushev 2014-06-02 11:56:56 +00:00
parent f7dd6a8d72
commit b6ee459833
4 changed files with 117 additions and 86 deletions

View File

@ -0,0 +1,111 @@
<?php
/*
* Copyright 2005-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Mibew\Controller\Chat\User;
use Mibew\Controller\Chat\AbstractController;
use Mibew\Http\Exception\BadRequestException;
use Mibew\Settings;
use Mibew\Thread;
use Symfony\Component\HttpFoundation\Request;
/**
* Contains actions which are related with send chat history to user mail.
*/
class MailController extends AbstractController
{
/**
* Process sending chat history to an email.
*
* @param Request $request Incoming request.
* @return string|\Symfony\Component\HttpFoundation\RedirectResponse Rendered
* page content or a redirect response.
* @throws BadRequestException If the thread cannot be loaded by some
* reasons.
*/
public function indexAction(Request $request)
{
global $mibew_mailbox;
$page = array(
'errors' => array(),
);
// Get and validate thread id
$thread_id = $request->request->get('thread');
if (!preg_match("/^\d{1,10}$/", $thread_id)) {
throw new BadRequestException('Wrong value of "thread" argument.');
}
// Get token and verify it
$token = $request->request->get('token');
if (!preg_match("/^\d{1,10}$/", $token)) {
throw new BadRequestException('Wrong value of "token" argument.');
}
$thread = Thread::load($thread_id, $token);
if (!$thread) {
throw new BadRequestException('Wrong thread.');
}
$email = $request->request->get('email');
$page['email'] = $email;
$group = is_null($thread->groupId) ? null : group_by_id($thread->groupId);
if (!$email) {
$page['errors'][] = no_field('form.field.email');
} elseif (!is_valid_email($email)) {
$page['errors'][] = wrong_field('form.field.email');
}
if (count($page['errors']) > 0) {
$page['formemail'] = $email;
$page['chat.thread.id'] = $thread->id;
$page['chat.thread.token'] = $thread->lastToken;
$page['level'] = '';
$page = array_merge_recursive(
$page,
setup_logo($group)
);
return $this->render('mail', $page);
}
$history = '';
$last_id = -1;
$messages = $thread->getMessages(true, $last_id);
foreach ($messages as $msg) {
$history .= message_to_text($msg);
}
$subject = getstring('mail.user.history.subject', true);
$body = getstring2(
'mail.user.history.body',
array($thread->userName,
$history,
Settings::get('title'),
Settings::get('hosturl')
),
true
);
mibew_mail($email, $mibew_mailbox, $subject, $body);
$page = array_merge_recursive($page, setup_logo($group));
return $this->render('mailsent', $page);
}
}

View File

@ -19,6 +19,11 @@ chat_user:
defaults:
_controller: Mibew\Controller\Chat\User\ChatController::indexAction
chat_user_mail:
path: /chat/mail
defaults:
_controller: Mibew\Controller\Chat\User\MailController::indexAction
# Pages that are available for all users
button:
path: /b

View File

@ -1,85 +0,0 @@
<?php
/*
* Copyright 2005-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Import namespaces and classes of the core
use Mibew\Settings;
use Mibew\Thread;
use Mibew\Style\ChatStyle;
// Initialize libraries
require_once(dirname(__FILE__) . '/libs/init.php');
$page = array(
'errors' => array(),
);
$token = verify_param("token", "/^\d{1,8}$/");
$thread_id = verify_param("thread", "/^\d{1,8}$/");
$thread = Thread::load($thread_id, $token);
if (!$thread) {
die("wrong thread");
}
// Initialize chat style which is currently used in system
$chat_style = new ChatStyle(ChatStyle::getCurrentStyle());
$email = get_param('email');
$page['email'] = $email;
$group = is_null($thread->groupId) ? null : group_by_id($thread->groupId);
if (!$email) {
$page['errors'][] = no_field("form.field.email");
} elseif (!is_valid_email($email)) {
$page['errors'][] = wrong_field("form.field.email");
}
if (count($page['errors']) > 0) {
$page['formemail'] = $email;
$page['chat.thread.id'] = $thread->id;
$page['chat.thread.token'] = $thread->lastToken;
$page['level'] = "";
$page = array_merge_recursive(
$page,
setup_logo($group)
);
$chat_style->render('mail', $page);
exit;
}
$history = "";
$last_id = -1;
$messages = $thread->getMessages(true, $last_id);
foreach ($messages as $msg) {
$history .= message_to_text($msg);
}
$subject = getstring("mail.user.history.subject", true);
$body = getstring2(
"mail.user.history.body",
array($thread->userName,
$history,
Settings::get('title'),
Settings::get('hosturl')
),
true
);
mibew_mail($email, $mibew_mailbox, $subject, $body);
$page = array_merge_recursive($page, setup_logo($group));
$chat_style->render('mailsent', $page);

View File

@ -8,7 +8,7 @@
{{/override}}
{{#override "message"}}
<form name="mailThreadForm" method="post" action="{{mibewRoot}}/mail.php">
<form name="mailThreadForm" method="post" action="{{mibewRoot}}/chat/mail">
<input type="hidden" name="style" value="{{styleName}}"/>
<input type="hidden" name="thread" value="{{chat.thread.id}}"/>
<input type="hidden" name="token" value="{{chat.thread.token}}"/>