-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMessages.php
155 lines (132 loc) · 4.18 KB
/
Messages.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
namespace App\controllers;
use Minz\Request;
use Minz\Response;
use App\auth;
use App\models;
use App\services;
/**
* @author Marien Fressinaud <[email protected]>
* @license http://www.gnu.org/licenses/agpl-3.0.en.html AGPL
*/
class Messages
{
/**
* Edit a message.
*
* @request_param string id
* @request_param string from
*
* @response 302 /login?redirect_to=:from
* If the user is not connected.
* @response 404
* If the message doesn’t exist or user hasn't access to it.
* @response 200
* On success.
*/
public function edit(Request $request): Response
{
$user = auth\CurrentUser::get();
$message_id = $request->param('id', '');
$from = $request->param('from', '');
if (!$user) {
return Response::redirect('login', ['redirect_to' => $from]);
}
$message = models\Message::findBy([
'id' => $message_id,
'user_id' => $user->id,
]);
if (!$message) {
return Response::notFound('not_found.phtml');
}
return Response::ok('messages/edit.phtml', [
'message' => $message,
'comment' => $message->content,
'from' => $from,
]);
}
/**
* Update a message.
*
* @request_param string id
* @request_param string content
* @request_param string from
* @request_param string csrf
*
* @response 302 /login?redirect_to=:from
* If the user is not connected.
* @response 404
* If the message doesn’t exist or user hasn't access to it.
* @response 302
* @flash error
* If the CSRF or the content are invalid.
* @response 302 :from
* On success.
*/
public function update(Request $request): Response
{
$user = auth\CurrentUser::get();
$message_id = $request->param('id');
$content = $request->param('content', '');
$from = $request->param('from', '');
$csrf = $request->param('csrf', '');
if (!$user) {
return Response::redirect('login', ['redirect_to' => $from]);
}
$message = models\Message::findBy([
'id' => $message_id,
'user_id' => $user->id,
]);
if (!$message) {
return Response::notFound('not_found.phtml');
}
if (!\Minz\Csrf::validate($csrf)) {
\Minz\Flash::set('error', _('A security verification failed.'));
return Response::found($from);
}
$message->content = trim($content);
$errors = $message->validate();
if ($errors) {
\Minz\Flash::set('errors', $errors);
return Response::found($from);
}
$message->save();
services\LinkTags::refresh($message->link());
return Response::found($from);
}
/**
* Delete a message
*
* @request_param string id
* @request_param string redirect_to default is /
*
* @response 302 /login?redirect_to=:redirect_to if not connected
* @response 404 if the message doesn’t exist or user hasn't access
* @response 302 :redirect_to if csrf is invalid
* @response 302 :redirect_to on success
*/
public function delete(Request $request): Response
{
$user = auth\CurrentUser::get();
$message_id = $request->param('id', '');
$redirect_to = $request->param('redirect_to', \Minz\Url::for('home'));
$csrf = $request->param('csrf', '');
if (!$user) {
return Response::redirect('login', ['redirect_to' => $redirect_to]);
}
$message = models\Message::findBy([
'id' => $message_id,
'user_id' => $user->id,
]);
if (!$message) {
return Response::notFound('not_found.phtml');
}
if (!\Minz\Csrf::validate($csrf)) {
\Minz\Flash::set('error', _('A security verification failed.'));
return Response::found($redirect_to);
}
models\Message::delete($message->id);
services\LinkTags::refresh($message->link());
return Response::found($redirect_to);
}
}