Conditions | 29 |
Paths | > 20000 |
Total Lines | 155 |
Code Lines | 67 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
139 | public function action_reporttm2() |
||
140 | { |
||
141 | global $txt, $topic, $board, $modSettings, $language, $context; |
||
142 | |||
143 | // You must have the proper permissions! |
||
144 | isAllowedTo('report_any'); |
||
145 | |||
146 | // Make sure they aren't spamming. |
||
147 | spamProtection('reporttm'); |
||
148 | |||
149 | require_once(SUBSDIR . '/Mail.subs.php'); |
||
150 | |||
151 | // No errors, yet. |
||
152 | $report_errors = ErrorContext::context('report', 1); |
||
153 | |||
154 | // Check their session. |
||
155 | if (checkSession('post', '', false) != '') |
||
156 | { |
||
157 | $report_errors->addError('session_timeout'); |
||
158 | } |
||
159 | |||
160 | // Make sure we have a comment and it's clean. |
||
161 | if ($this->_req->getPost('comment', '\\ElkArte\\Helper\\Util::htmltrim', '') === '') |
||
162 | { |
||
163 | $report_errors->addError('no_comment'); |
||
164 | } |
||
165 | |||
166 | $poster_comment = strtr(Util::htmlspecialchars($this->_req->post->comment), ["\r" => '', "\t" => '']); |
||
167 | |||
168 | if (Util::strlen($poster_comment) > 254) |
||
169 | { |
||
170 | $report_errors->addError('post_too_long'); |
||
171 | } |
||
172 | |||
173 | // Guests need to provide their address! |
||
174 | if ($this->user->is_guest) |
||
175 | { |
||
176 | if (!DataValidator::is_valid($this->_req->post, ['email' => 'valid_email'], ['email' => 'trim'])) |
||
177 | { |
||
178 | empty($this->_req->post->email) ? $report_errors->addError('no_email') : $report_errors->addError('bad_email'); |
||
179 | } |
||
180 | |||
181 | isBannedEmail($this->_req->post->email, 'cannot_post', sprintf($txt['you_are_post_banned'], $txt['guest_title'])); |
||
182 | |||
183 | $this->user->email = htmlspecialchars($this->_req->post->email, ENT_COMPAT, 'UTF-8'); |
||
184 | } |
||
185 | |||
186 | // Could they get the right verification code? |
||
187 | if ($this->user->is_guest && !empty($modSettings['guests_report_require_captcha'])) |
||
188 | { |
||
189 | $verificationOptions = [ |
||
190 | 'id' => 'report', |
||
191 | ]; |
||
192 | $context['require_verification'] = VerificationControlsIntegrate::create($verificationOptions, true); |
||
193 | |||
194 | if (is_array($context['require_verification'])) |
||
195 | { |
||
196 | foreach ($context['require_verification'] as $error) |
||
197 | { |
||
198 | $report_errors->addError($error, 0); |
||
199 | } |
||
200 | } |
||
201 | } |
||
202 | |||
203 | // Any errors? |
||
204 | if ($report_errors->hasErrors()) |
||
205 | { |
||
206 | $this->action_reporttm(); |
||
207 | |||
208 | return true; |
||
209 | } |
||
210 | |||
211 | // Get the basic topic information, and make sure they can see it. |
||
212 | $msg_id = (int) $this->_req->post->msg; |
||
213 | $message = posterDetails($msg_id, $topic); |
||
214 | |||
215 | if (empty($message)) |
||
216 | { |
||
217 | throw new Exception('no_board', false); |
||
218 | } |
||
219 | |||
220 | $poster_name = un_htmlspecialchars($message['real_name']) . ($message['real_name'] !== $message['poster_name'] ? ' (' . $message['poster_name'] . ')' : ''); |
||
221 | $reporterName = un_htmlspecialchars($this->user->name) . ($this->user->name !== $this->user->username && $this->user->username != '' ? ' (' . $this->user->username . ')' : ''); |
||
222 | $subject = un_htmlspecialchars($message['subject']); |
||
223 | |||
224 | // Get a list of members with the moderate_board permission. |
||
225 | require_once(SUBSDIR . '/Members.subs.php'); |
||
226 | $moderators = membersAllowedTo('moderate_board', $board); |
||
227 | $result = getBasicMemberData($moderators, ['preferences' => true, 'sort' => 'lngfile']); |
||
228 | $mod_to_notify = []; |
||
229 | foreach ($result as $row) |
||
230 | { |
||
231 | if ($row['notify_types'] !== 4) |
||
232 | { |
||
233 | $mod_to_notify[] = $row; |
||
234 | } |
||
235 | } |
||
236 | |||
237 | // Check that moderators do exist! |
||
238 | if (empty($mod_to_notify)) |
||
239 | { |
||
240 | throw new Exception('no_mods', false); |
||
241 | } |
||
242 | |||
243 | // If we get here, I believe we should make a record of this, for historical significance, yabber. |
||
244 | if (empty($modSettings['disable_log_report'])) |
||
245 | { |
||
246 | require_once(SUBSDIR . '/Messages.subs.php'); |
||
247 | $message['type'] = 'msg'; |
||
248 | $id_report = recordReport($message, $poster_comment); |
||
249 | |||
250 | // If we're just going to ignore these, then who gives a monkeys... |
||
251 | if ($id_report === false) |
||
252 | { |
||
253 | redirectexit('topic=' . $topic . '.msg' . $msg_id . '#msg' . $msg_id); |
||
254 | } |
||
255 | } |
||
256 | |||
257 | // Find out who the real moderators are - for mod preferences. |
||
258 | require_once(SUBSDIR . '/Boards.subs.php'); |
||
259 | $real_mods = getBoardModerators($board, true); |
||
260 | |||
261 | // Send every moderator an email. |
||
262 | foreach ($mod_to_notify as $row) |
||
263 | { |
||
264 | // Maybe they don't want to know?! |
||
265 | if (!empty($row['mod_prefs'])) |
||
266 | { |
||
267 | [, , $pref_binary] = explode('|', $row['mod_prefs']); |
||
268 | if (!($pref_binary & 1) && (!($pref_binary & 2) || !in_array($row['id_member'], $real_mods))) |
||
269 | { |
||
270 | continue; |
||
271 | } |
||
272 | } |
||
273 | |||
274 | $replacements = [ |
||
275 | 'TOPICSUBJECT' => $subject, |
||
276 | 'POSTERNAME' => $poster_name, |
||
277 | 'REPORTERNAME' => $reporterName, |
||
278 | 'TOPICLINK' => getUrl('topic', ['topic' => $topic, 'start' => 'msg' . $msg_id, 'subject' => $subject]) . '#msg' . $msg_id, |
||
279 | 'REPORTLINK' => empty($id_report) ? '' : getUrl('action', ['action' => 'moderate', 'area' => 'reports', 'report' => $id_report]), |
||
280 | 'COMMENT' => $this->_req->post->comment, |
||
281 | ]; |
||
282 | |||
283 | $emaildata = loadEmailTemplate('report_to_moderator', $replacements, empty($row['lngfile']) || empty($modSettings['userLanguage']) ? $language : $row['lngfile']); |
||
284 | |||
285 | // Send it to the moderator. |
||
286 | sendmail($row['email_address'], $emaildata['subject'], $emaildata['body'], $this->user->email, null, false, 2); |
||
287 | } |
||
288 | |||
289 | // Keep track of when the mod reports get updated, that way we know when we need to look again. |
||
290 | updateSettings(['last_mod_report_action' => time()]); |
||
291 | |||
292 | // Back to the post we reported! |
||
293 | redirectexit('reportsent;topic=' . $topic . '.msg' . $msg_id . '#msg' . $msg_id); |
||
294 | } |
||
296 |