1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Icybee\Modules\Comments;
13
14 use ICanBoogie\ActiveRecord\RecordNotFound;
15 use ICanBoogie\Exception;
16 use ICanBoogie\I18n\FormattedString;
17 use ICanBoogie\Mailer;
18 use ICanBoogie\Operation;
19
20 21 22
23 class SaveOperation extends \ICanBoogie\SaveOperation
24 {
25 protected function lazy_get_properties()
26 {
27 global $core;
28
29 $properties = parent::lazy_get_properties();
30 $user = $core->user;
31
32 if ($this->key)
33 {
34 unset($properties[Comment::NID]);
35
36 if (!$user->has_permission(Module::PERMISSION_ADMINISTER))
37 {
38 unset($properties[Comment::AUTHOR_IP]);
39 }
40 }
41 else
42 {
43 $properties[Comment::AUTHOR_IP] = $_SERVER['REMOTE_ADDR'];
44
45 if (!$user->is_guest)
46 {
47 $properties[Comment::UID] = $user->uid;
48 }
49 }
50
51 if (!$user->has_permission(Module::PERMISSION_MANAGE, $this->module))
52 {
53 $properties['status'] = null;
54 }
55
56 if (!$this->key && empty($properties['status']))
57 {
58 $node = $core->models['nodes'][$properties[Comment::NID]];
59 $properties['status'] = $node->site->metas->get($this->module->flat_id . '.default_status', 'pending');
60 }
61
62 return $properties;
63 }
64
65 protected function validate(\ICanboogie\Errors $errors)
66 {
67 global $core;
68
69 $request = $this->request;
70
71 $nid = $request[Comment::NID];
72
73 if ($nid)
74 {
75 try
76 {
77 $node = $core->models['nodes'][$nid];
78 }
79 catch (RecordNotFound $e)
80 {
81 $errors[Comment::NID] = new FormattedString('Invalid node identifier: %nid', array('nid' => $nid));
82
83 return false;
84 }
85 }
86
87
88
89
90
91 if (!$this->key)
92 {
93 if (!$nid)
94 {
95 $errors[Comment::NID] = new FormattedString('The node id is required to create a comment.');
96
97 return false;
98 }
99
100
101
102
103
104 if ($this->module->model->where('author_ip = ? AND status = "spam"', $request->ip)->rc)
105 {
106 $errors[] = new FormattedString('A previous message from your IP was marked as spam.');
107 }
108 }
109
110 $author_url = $request[Comment::AUTHOR_URL];
111
112 if ($author_url && !filter_var($author_url, FILTER_VALIDATE_URL))
113 {
114 $errors[] = new FormattedString('Invalide URL: %url', array('url' => $author_url));
115 }
116
117 if (!$core->user_id)
118 {
119
120
121
122
123 $interval = $core->site->metas[$this->module->flat_id . '.delay'] ?: 5;
124
125 $last = $this->module->model
126 ->select('created_at')
127 ->where
128 (
129 '(author = ? OR author_email = ? OR author_ip = ?) AND created_at + INTERVAL ? MINUTE > UTC_TIMESTAMP()',
130 $request['author'], $request['author_email'], $request->ip, $interval
131 )
132 ->order('created_at DESC')
133 ->rc;
134
135 if ($last)
136 {
137 $errors[] = new FormattedString("Les commentaires ne peuvent être faits à moins de $interval minutes d'intervale.");
138 }
139 }
140
141 return !count($errors);
142 }
143
144 protected function process()
145 {
146 $rc = parent::process();
147
148 if (!$this->key)
149 {
150 $this->notify($rc['key']);
151
152 if ($this->properties['status'] == 'approved')
153 {
154 $comment = $this->module->model[$rc['key']];
155
156 $this->response->location = $comment->url;
157 }
158 }
159
160 return $rc;
161 }
162
163 164 165 166 167
168 protected function notify($commentid)
169 {
170 global $core;
171
172 $form_id = $core->site->metas['comments.form_id'];
173
174 if (!$form_id)
175 {
176 return;
177 }
178
179 try
180 {
181 $form = $core->models['forms'][$form_id];
182 }
183 catch (\Exception $e) { return; }
184
185 $options = unserialize($form->metas['comments/reply']);
186
187 if (!$options)
188 {
189 return;
190 }
191
192 $model = $this->module->model;
193 $comment = $model[$commentid];
194
195
196
197
198
199 $records = $model->where
200 (
201 'nid = ? AND `{primary}` < ? AND (`notify` = "yes" OR `notify` = "author") AND author_email != ?',
202
203 $comment->nid, $commentid, $comment->author_email
204 )
205 ->all;
206
207 if (!$records)
208 {
209 return;
210 }
211
212
213
214
215
216 $patron = new \Patron\Engine();
217 $subject = $patron($options['subject'], $comment);
218 $message = $patron($options['template'], $comment);
219
220 $from = $options['from'];
221 $bcc = $options['bcc'];
222
223 foreach ($records as $entry)
224 {
225
226
227
228
229 if ($entry->notify == 'author' && $comment->uid != $comment->node->uid)
230 {
231 continue;
232 }
233
234 \ICanBoogie\log
235 (
236 'Send notify to %author (email: %email, message n°%commentid, mode: %notify)', array
237 (
238 '%author' => $entry->author,
239 '%email' => $entry->author_email,
240 '%commentid' => $entry->commentid,
241 '%notify' => $entry->notify
242 )
243 );
244
245 $rc = $core->mail([
246
247 'to' => $entry->author_email,
248 'from' => $from,
249 'bcc' => $bcc,
250 'body' => $message,
251 'subject' => $subject,
252 'type' => 'plain'
253
254 ]);
255
256 if (!$rc)
257 {
258 \ICanBoogie\log_error('Unable to send notify to %author', array('%author' => $entry->author));
259
260 continue;
261 }
262
263 $entry->notify = 'done';
264 $entry->save();
265 }
266 }
267 }