Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 471 |
|
0.00% |
0 / 6 |
CRAP | n/a |
0 / 0 |
|
message_options | |
0.00% |
0 / 310 |
|
0.00% |
0 / 1 |
7832 | |||
define_check_option | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
42 | |||
define_action_option | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
90 | |||
define_rule_option | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
72 | |||
define_cond_option | |
0.00% |
0 / 88 |
|
0.00% |
0 / 1 |
420 | |||
show_defined_rules | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | /** |
3 | * |
4 | * This file is part of the phpBB Forum Software package. |
5 | * |
6 | * @copyright (c) phpBB Limited <https://www.phpbb.com> |
7 | * @license GNU General Public License, version 2 (GPL-2.0) |
8 | * |
9 | * For full copyright and license information, please see |
10 | * the docs/CREDITS.txt file. |
11 | * |
12 | */ |
13 | |
14 | /** |
15 | * @ignore |
16 | */ |
17 | if (!defined('IN_PHPBB')) |
18 | { |
19 | exit; |
20 | } |
21 | |
22 | /** |
23 | * Execute message options |
24 | */ |
25 | function message_options($id, $mode, $global_privmsgs_rules, $global_rule_conditions) |
26 | { |
27 | global $phpbb_root_path, $phpEx, $user, $template, $config, $db, $request; |
28 | |
29 | $redirect_url = append_sid("{$phpbb_root_path}ucp.$phpEx", "i=pm&mode=options"); |
30 | |
31 | add_form_key('ucp_pm_options'); |
32 | // Change "full folder" setting - what to do if folder is full |
33 | if (isset($_POST['fullfolder'])) |
34 | { |
35 | if (!check_form_key('ucp_pm_options')) |
36 | { |
37 | trigger_error('FORM_INVALID'); |
38 | } |
39 | |
40 | $full_action = $request->variable('full_action', 0); |
41 | |
42 | $set_folder_id = 0; |
43 | switch ($full_action) |
44 | { |
45 | case 1: |
46 | $set_folder_id = FULL_FOLDER_DELETE; |
47 | break; |
48 | |
49 | case 2: |
50 | $set_folder_id = $request->variable('full_move_to', PRIVMSGS_INBOX); |
51 | break; |
52 | |
53 | case 3: |
54 | $set_folder_id = FULL_FOLDER_HOLD; |
55 | break; |
56 | |
57 | default: |
58 | $full_action = 0; |
59 | break; |
60 | } |
61 | |
62 | if ($full_action) |
63 | { |
64 | $sql = 'UPDATE ' . USERS_TABLE . ' |
65 | SET user_full_folder = ' . $set_folder_id . ' |
66 | WHERE user_id = ' . $user->data['user_id']; |
67 | $db->sql_query($sql); |
68 | |
69 | $user->data['user_full_folder'] = $set_folder_id; |
70 | |
71 | $message = $user->lang['FULL_FOLDER_OPTION_CHANGED'] . '<br /><br />' . sprintf($user->lang['RETURN_UCP'], '<a href="' . $redirect_url . '">', '</a>'); |
72 | meta_refresh(3, $redirect_url); |
73 | trigger_error($message); |
74 | } |
75 | } |
76 | |
77 | // Add Folder |
78 | if (isset($_POST['addfolder'])) |
79 | { |
80 | if (check_form_key('ucp_pm_options')) |
81 | { |
82 | $folder_name = $request->variable('foldername', '', true); |
83 | |
84 | if ($folder_name) |
85 | { |
86 | $sql = 'SELECT folder_name |
87 | FROM ' . PRIVMSGS_FOLDER_TABLE . " |
88 | WHERE folder_name = '" . $db->sql_escape($folder_name) . "' |
89 | AND user_id = " . $user->data['user_id']; |
90 | $result = $db->sql_query_limit($sql, 1); |
91 | $row = $db->sql_fetchrow($result); |
92 | $db->sql_freeresult($result); |
93 | |
94 | if ($row) |
95 | { |
96 | trigger_error(sprintf($user->lang['FOLDER_NAME_EXIST'], $folder_name)); |
97 | } |
98 | |
99 | $sql = 'SELECT COUNT(folder_id) as num_folder |
100 | FROM ' . PRIVMSGS_FOLDER_TABLE . ' |
101 | WHERE user_id = ' . $user->data['user_id']; |
102 | $result = $db->sql_query($sql); |
103 | $num_folder = (int) $db->sql_fetchfield('num_folder'); |
104 | $db->sql_freeresult($result); |
105 | |
106 | if ($num_folder >= $config['pm_max_boxes']) |
107 | { |
108 | trigger_error('MAX_FOLDER_REACHED'); |
109 | } |
110 | |
111 | $sql = 'INSERT INTO ' . PRIVMSGS_FOLDER_TABLE . ' ' . $db->sql_build_array('INSERT', array( |
112 | 'user_id' => (int) $user->data['user_id'], |
113 | 'folder_name' => $folder_name) |
114 | ); |
115 | $db->sql_query($sql); |
116 | $msg = $user->lang['FOLDER_ADDED']; |
117 | } |
118 | else |
119 | { |
120 | $msg = $user->lang['FOLDER_NAME_EMPTY']; |
121 | } |
122 | } |
123 | else |
124 | { |
125 | $msg = $user->lang['FORM_INVALID']; |
126 | } |
127 | $message = $msg . '<br /><br />' . sprintf($user->lang['RETURN_UCP'], '<a href="' . $redirect_url . '">', '</a>'); |
128 | meta_refresh(3, $redirect_url); |
129 | trigger_error($message); |
130 | } |
131 | |
132 | // Rename folder |
133 | if (isset($_POST['rename_folder'])) |
134 | { |
135 | if (check_form_key('ucp_pm_options')) |
136 | { |
137 | $new_folder_name = $request->variable('new_folder_name', '', true); |
138 | $rename_folder_id= $request->variable('rename_folder_id', 0); |
139 | |
140 | if (!$new_folder_name) |
141 | { |
142 | trigger_error('NO_NEW_FOLDER_NAME'); |
143 | } |
144 | |
145 | // Select custom folder |
146 | $sql = 'SELECT folder_name, pm_count |
147 | FROM ' . PRIVMSGS_FOLDER_TABLE . " |
148 | WHERE user_id = {$user->data['user_id']} |
149 | AND folder_id = $rename_folder_id"; |
150 | $result = $db->sql_query_limit($sql, 1); |
151 | $folder_row = $db->sql_fetchrow($result); |
152 | $db->sql_freeresult($result); |
153 | |
154 | if (!$folder_row) |
155 | { |
156 | trigger_error('CANNOT_RENAME_FOLDER'); |
157 | } |
158 | |
159 | $sql = 'UPDATE ' . PRIVMSGS_FOLDER_TABLE . " |
160 | SET folder_name = '" . $db->sql_escape($new_folder_name) . "' |
161 | WHERE folder_id = $rename_folder_id |
162 | AND user_id = {$user->data['user_id']}"; |
163 | $db->sql_query($sql); |
164 | $msg = $user->lang['FOLDER_RENAMED']; |
165 | } |
166 | else |
167 | { |
168 | $msg = $user->lang['FORM_INVALID']; |
169 | } |
170 | |
171 | $message = $msg . '<br /><br />' . sprintf($user->lang['RETURN_UCP'], '<a href="' . $redirect_url . '">', '</a>'); |
172 | |
173 | meta_refresh(3, $redirect_url); |
174 | trigger_error($message); |
175 | } |
176 | |
177 | // Remove Folder |
178 | if (isset($_POST['remove_folder'])) |
179 | { |
180 | $remove_folder_id = $request->variable('remove_folder_id', 0); |
181 | |
182 | // Default to "move all messages to inbox" |
183 | $remove_action = $request->variable('remove_action', 1); |
184 | $move_to = $request->variable('move_to', PRIVMSGS_INBOX); |
185 | |
186 | // Move to same folder? |
187 | if ($remove_action == 1 && $remove_folder_id == $move_to) |
188 | { |
189 | trigger_error('CANNOT_MOVE_TO_SAME_FOLDER'); |
190 | } |
191 | |
192 | // Select custom folder |
193 | $sql = 'SELECT folder_name, pm_count |
194 | FROM ' . PRIVMSGS_FOLDER_TABLE . " |
195 | WHERE user_id = {$user->data['user_id']} |
196 | AND folder_id = $remove_folder_id"; |
197 | $result = $db->sql_query_limit($sql, 1); |
198 | $folder_row = $db->sql_fetchrow($result); |
199 | $db->sql_freeresult($result); |
200 | |
201 | if (!$folder_row) |
202 | { |
203 | trigger_error('CANNOT_REMOVE_FOLDER'); |
204 | } |
205 | |
206 | $s_hidden_fields = array( |
207 | 'remove_folder_id' => $remove_folder_id, |
208 | 'remove_action' => $remove_action, |
209 | 'move_to' => $move_to, |
210 | 'remove_folder' => 1 |
211 | ); |
212 | |
213 | // Do we need to confirm? |
214 | if (confirm_box(true)) |
215 | { |
216 | // Gather message ids |
217 | $sql = 'SELECT msg_id |
218 | FROM ' . PRIVMSGS_TO_TABLE . ' |
219 | WHERE user_id = ' . $user->data['user_id'] . " |
220 | AND folder_id = $remove_folder_id"; |
221 | $result = $db->sql_query($sql); |
222 | |
223 | $msg_ids = array(); |
224 | while ($row = $db->sql_fetchrow($result)) |
225 | { |
226 | $msg_ids[] = (int) $row['msg_id']; |
227 | } |
228 | $db->sql_freeresult($result); |
229 | |
230 | // First of all, copy all messages to another folder... or delete all messages |
231 | switch ($remove_action) |
232 | { |
233 | // Move Messages |
234 | case 1: |
235 | $num_moved = move_pm($user->data['user_id'], $user->data['message_limit'], $msg_ids, $move_to, $remove_folder_id); |
236 | |
237 | // Something went wrong, only partially moved? |
238 | if ($num_moved != $folder_row['pm_count']) |
239 | { |
240 | trigger_error($user->lang('MOVE_PM_ERROR', $user->lang('MESSAGES_COUNT', (int) $folder_row['pm_count']), $num_moved)); |
241 | } |
242 | break; |
243 | |
244 | // Remove Messages |
245 | case 2: |
246 | delete_pm($user->data['user_id'], $msg_ids, $remove_folder_id); |
247 | break; |
248 | } |
249 | |
250 | // Remove folder |
251 | $sql = 'DELETE FROM ' . PRIVMSGS_FOLDER_TABLE . " |
252 | WHERE user_id = {$user->data['user_id']} |
253 | AND folder_id = $remove_folder_id"; |
254 | $db->sql_query($sql); |
255 | |
256 | // Check full folder option. If the removed folder has been specified as destination switch back to inbox |
257 | if ($user->data['user_full_folder'] == $remove_folder_id) |
258 | { |
259 | $sql = 'UPDATE ' . USERS_TABLE . ' |
260 | SET user_full_folder = ' . PRIVMSGS_INBOX . ' |
261 | WHERE user_id = ' . $user->data['user_id']; |
262 | $db->sql_query($sql); |
263 | |
264 | $user->data['user_full_folder'] = PRIVMSGS_INBOX; |
265 | } |
266 | |
267 | // Now make sure the folder is not used for rules |
268 | // We assign another folder id (the one the messages got moved to) or assign the INBOX (to not have to remove any rule) |
269 | $sql = 'UPDATE ' . PRIVMSGS_RULES_TABLE . ' SET rule_folder_id = '; |
270 | $sql .= ($remove_action == 1) ? $move_to : PRIVMSGS_INBOX; |
271 | $sql .= ' WHERE rule_folder_id = ' . $remove_folder_id; |
272 | |
273 | $db->sql_query($sql); |
274 | |
275 | $meta_info = append_sid("{$phpbb_root_path}ucp.$phpEx", "i=pm&mode=$mode"); |
276 | $message = $user->lang['FOLDER_REMOVED']; |
277 | |
278 | meta_refresh(3, $meta_info); |
279 | $message .= '<br /><br />' . sprintf($user->lang['RETURN_UCP'], '<a href="' . $meta_info . '">', '</a>'); |
280 | trigger_error($message); |
281 | } |
282 | else |
283 | { |
284 | confirm_box(false, 'REMOVE_FOLDER', build_hidden_fields($s_hidden_fields)); |
285 | } |
286 | } |
287 | |
288 | // Add Rule |
289 | if (isset($_POST['add_rule'])) |
290 | { |
291 | if (check_form_key('ucp_pm_options')) |
292 | { |
293 | $check_option = $request->variable('check_option', 0); |
294 | $rule_option = $request->variable('rule_option', 0); |
295 | $cond_option = $request->variable('cond_option', ''); |
296 | $action_option = explode('|', $request->variable('action_option', '')); |
297 | $rule_string = ($cond_option != 'none') ? $request->variable('rule_string', '', true) : ''; |
298 | $rule_user_id = ($cond_option != 'none') ? $request->variable('rule_user_id', 0) : 0; |
299 | $rule_group_id = ($cond_option != 'none') ? $request->variable('rule_group_id', 0) : 0; |
300 | |
301 | $action = (int) $action_option[0]; |
302 | $folder_id = (int) $action_option[1]; |
303 | |
304 | if (!$action || !$check_option || !$rule_option || !$cond_option || ($cond_option != 'none' && !$rule_string)) |
305 | { |
306 | trigger_error('RULE_NOT_DEFINED'); |
307 | } |
308 | |
309 | if (($cond_option == 'user' && !$rule_user_id) || ($cond_option == 'group' && !$rule_group_id)) |
310 | { |
311 | trigger_error('RULE_NOT_DEFINED'); |
312 | } |
313 | |
314 | $rule_ary = array( |
315 | 'user_id' => $user->data['user_id'], |
316 | 'rule_check' => $check_option, |
317 | 'rule_connection' => $rule_option, |
318 | 'rule_string' => $rule_string, |
319 | 'rule_user_id' => $rule_user_id, |
320 | 'rule_group_id' => $rule_group_id, |
321 | 'rule_action' => $action, |
322 | 'rule_folder_id' => $folder_id |
323 | ); |
324 | |
325 | $sql = 'SELECT rule_id |
326 | FROM ' . PRIVMSGS_RULES_TABLE . ' |
327 | WHERE ' . $db->sql_build_array('SELECT', $rule_ary); |
328 | $result = $db->sql_query($sql); |
329 | $row = $db->sql_fetchrow($result); |
330 | $db->sql_freeresult($result); |
331 | |
332 | if ($row) |
333 | { |
334 | trigger_error('RULE_ALREADY_DEFINED'); |
335 | } |
336 | |
337 | // Prevent users from flooding the rules table |
338 | $sql = 'SELECT COUNT(rule_id) AS num_rules |
339 | FROM ' . PRIVMSGS_RULES_TABLE . ' |
340 | WHERE user_id = ' . (int) $user->data['user_id']; |
341 | $result = $db->sql_query($sql); |
342 | $num_rules = (int) $db->sql_fetchfield('num_rules'); |
343 | $db->sql_freeresult($result); |
344 | |
345 | if ($num_rules >= 5000) |
346 | { |
347 | trigger_error('RULE_LIMIT_REACHED'); |
348 | } |
349 | |
350 | $sql = 'INSERT INTO ' . PRIVMSGS_RULES_TABLE . ' ' . $db->sql_build_array('INSERT', $rule_ary); |
351 | $db->sql_query($sql); |
352 | |
353 | // Set the user_message_rules bit |
354 | $sql = 'UPDATE ' . USERS_TABLE . ' |
355 | SET user_message_rules = 1 |
356 | WHERE user_id = ' . $user->data['user_id']; |
357 | $db->sql_query($sql); |
358 | |
359 | $msg = $user->lang['RULE_ADDED']; |
360 | } |
361 | else |
362 | { |
363 | $msg = $user->lang['FORM_INVALID']; |
364 | } |
365 | $message = $msg . '<br /><br />' . sprintf($user->lang['RETURN_UCP'], '<a href="' . $redirect_url . '">', '</a>'); |
366 | meta_refresh(3, $redirect_url); |
367 | trigger_error($message); |
368 | } |
369 | |
370 | // Remove Rule |
371 | if (isset($_POST['delete_rule']) && !isset($_POST['cancel'])) |
372 | { |
373 | $delete_id = array_keys($request->variable('delete_rule', array(0 => 0))); |
374 | $delete_id = (!empty($delete_id[0])) ? $delete_id[0] : 0; |
375 | |
376 | if (!$delete_id) |
377 | { |
378 | redirect(append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&mode=' . $mode)); |
379 | } |
380 | |
381 | // Do we need to confirm? |
382 | if (confirm_box(true)) |
383 | { |
384 | $sql = 'DELETE FROM ' . PRIVMSGS_RULES_TABLE . ' |
385 | WHERE user_id = ' . $user->data['user_id'] . " |
386 | AND rule_id = $delete_id"; |
387 | $db->sql_query($sql); |
388 | |
389 | $meta_info = append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&mode=' . $mode); |
390 | $message = $user->lang['RULE_DELETED']; |
391 | |
392 | // Reset user_message_rules if no more assigned |
393 | $sql = 'SELECT rule_id |
394 | FROM ' . PRIVMSGS_RULES_TABLE . ' |
395 | WHERE user_id = ' . $user->data['user_id']; |
396 | $result = $db->sql_query_limit($sql, 1); |
397 | $row = $db->sql_fetchrow($result); |
398 | $db->sql_freeresult($result); |
399 | |
400 | // Unset the user_message_rules bit |
401 | if (!$row) |
402 | { |
403 | $sql = 'UPDATE ' . USERS_TABLE . ' |
404 | SET user_message_rules = 0 |
405 | WHERE user_id = ' . $user->data['user_id']; |
406 | $db->sql_query($sql); |
407 | } |
408 | |
409 | meta_refresh(3, $meta_info); |
410 | $message .= '<br /><br />' . sprintf($user->lang['RETURN_UCP'], '<a href="' . $meta_info . '">', '</a>'); |
411 | trigger_error($message); |
412 | } |
413 | else |
414 | { |
415 | confirm_box(false, 'DELETE_RULE', build_hidden_fields(array('delete_rule' => array($delete_id => 1)))); |
416 | } |
417 | } |
418 | |
419 | $folder = array(); |
420 | |
421 | $sql = 'SELECT COUNT(msg_id) as num_messages |
422 | FROM ' . PRIVMSGS_TO_TABLE . ' |
423 | WHERE user_id = ' . $user->data['user_id'] . ' |
424 | AND folder_id = ' . PRIVMSGS_INBOX; |
425 | $result = $db->sql_query($sql); |
426 | $num_messages = (int) $db->sql_fetchfield('num_messages'); |
427 | $db->sql_freeresult($result); |
428 | |
429 | $folder[PRIVMSGS_INBOX] = array( |
430 | 'folder_name' => $user->lang['PM_INBOX'], |
431 | 'message_status' => $user->lang('FOLDER_MESSAGE_STATUS', $user->lang('MESSAGES_COUNT', (int) $user->data['message_limit']), $num_messages), |
432 | ); |
433 | |
434 | $sql = 'SELECT folder_id, folder_name, pm_count |
435 | FROM ' . PRIVMSGS_FOLDER_TABLE . ' |
436 | WHERE user_id = ' . $user->data['user_id']; |
437 | $result = $db->sql_query($sql); |
438 | |
439 | $num_user_folder = 0; |
440 | while ($row = $db->sql_fetchrow($result)) |
441 | { |
442 | $num_user_folder++; |
443 | $folder[$row['folder_id']] = array( |
444 | 'folder_name' => $row['folder_name'], |
445 | 'message_status' => $user->lang('FOLDER_MESSAGE_STATUS', $user->lang('MESSAGES_COUNT', (int) $user->data['message_limit']), (int) $row['pm_count']), |
446 | ); |
447 | } |
448 | $db->sql_freeresult($result); |
449 | |
450 | $s_full_folder_options = $s_to_folder_options = $s_folder_options = ''; |
451 | |
452 | if ($user->data['user_full_folder'] == FULL_FOLDER_NONE) |
453 | { |
454 | // -3 here to let the correct folder id be selected |
455 | $to_folder_id = $config['full_folder_action'] - 3; |
456 | } |
457 | else |
458 | { |
459 | $to_folder_id = $user->data['user_full_folder']; |
460 | } |
461 | |
462 | foreach ($folder as $folder_id => $folder_ary) |
463 | { |
464 | $s_full_folder_options .= '<option value="' . $folder_id . '"' . (($user->data['user_full_folder'] == $folder_id) ? ' selected="selected"' : '') . '>' . $folder_ary['folder_name'] . ' (' . $folder_ary['message_status'] . ')</option>'; |
465 | $s_to_folder_options .= '<option value="' . $folder_id . '"' . (($to_folder_id == $folder_id) ? ' selected="selected"' : '') . '>' . $folder_ary['folder_name'] . ' (' . $folder_ary['message_status'] . ')</option>'; |
466 | |
467 | if ($folder_id != PRIVMSGS_INBOX) |
468 | { |
469 | $s_folder_options .= '<option value="' . $folder_id . '">' . $folder_ary['folder_name'] . ' (' . $folder_ary['message_status'] . ')</option>'; |
470 | } |
471 | } |
472 | |
473 | $s_delete_checked = ($user->data['user_full_folder'] == FULL_FOLDER_DELETE) ? ' checked="checked"' : ''; |
474 | $s_hold_checked = ($user->data['user_full_folder'] == FULL_FOLDER_HOLD) ? ' checked="checked"' : ''; |
475 | $s_move_checked = ($user->data['user_full_folder'] >= 0) ? ' checked="checked"' : ''; |
476 | |
477 | if ($user->data['user_full_folder'] == FULL_FOLDER_NONE) |
478 | { |
479 | switch ($config['full_folder_action']) |
480 | { |
481 | case 1: |
482 | $s_delete_checked = ' checked="checked"'; |
483 | break; |
484 | |
485 | case 2: |
486 | $s_hold_checked = ' checked="checked"'; |
487 | break; |
488 | } |
489 | } |
490 | |
491 | $template->assign_vars(array( |
492 | 'S_FULL_FOLDER_OPTIONS' => $s_full_folder_options, |
493 | 'S_TO_FOLDER_OPTIONS' => $s_to_folder_options, |
494 | 'S_FOLDER_OPTIONS' => $s_folder_options, |
495 | 'S_DELETE_CHECKED' => $s_delete_checked, |
496 | 'S_HOLD_CHECKED' => $s_hold_checked, |
497 | 'S_MOVE_CHECKED' => $s_move_checked, |
498 | 'S_MAX_FOLDER_REACHED' => ($num_user_folder >= $config['pm_max_boxes']) ? true : false, |
499 | 'S_MAX_FOLDER_ZERO' => ($config['pm_max_boxes'] == 0) ? true : false, |
500 | |
501 | 'DEFAULT_ACTION' => ($config['full_folder_action'] == 1) ? $user->lang['DELETE_OLDEST_MESSAGES'] : $user->lang['HOLD_NEW_MESSAGES'], |
502 | |
503 | 'U_FIND_USERNAME' => append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=searchuser&form=ucp&field=rule_string&select_single=true'), |
504 | )); |
505 | |
506 | $rule_lang = $action_lang = $check_lang = array(); |
507 | |
508 | // Build all three language arrays |
509 | preg_replace_callback('#^((RULE|ACTION|CHECK)_([A-Z0-9_]+))$#', function ($match) use(&$rule_lang, &$action_lang, &$check_lang, $user) { |
510 | ${strtolower($match[2]) . '_lang'}[constant($match[1])] = $user->lang['PM_' . $match[2]][$match[3]]; |
511 | }, array_keys(get_defined_constants())); |
512 | |
513 | /* |
514 | Rule Ordering: |
515 | -> CHECK_* -> RULE_* [IN $global_privmsgs_rules:CHECK_*] -> [IF $rule_conditions[RULE_*] [|text|bool|user|group|own_group]] -> ACTION_* |
516 | */ |
517 | |
518 | $check_option = $request->variable('check_option', 0); |
519 | $rule_option = $request->variable('rule_option', 0); |
520 | $cond_option = $request->variable('cond_option', ''); |
521 | $action_option = $request->variable('action_option', ''); |
522 | $back = (isset($_REQUEST['back'])) ? $request->variable('back', array('' => 0)) : array(); |
523 | |
524 | if (count($back)) |
525 | { |
526 | if ($action_option) |
527 | { |
528 | $action_option = ''; |
529 | } |
530 | else if ($cond_option) |
531 | { |
532 | $cond_option = ''; |
533 | } |
534 | else if ($rule_option) |
535 | { |
536 | $rule_option = 0; |
537 | } |
538 | else if ($check_option) |
539 | { |
540 | $check_option = 0; |
541 | } |
542 | } |
543 | |
544 | if (isset($back['action']) && $cond_option == 'none') |
545 | { |
546 | $back['cond'] = true; |
547 | } |
548 | |
549 | // Check |
550 | if (!isset($global_privmsgs_rules[$check_option])) |
551 | { |
552 | $check_option = 0; |
553 | } |
554 | |
555 | define_check_option(($check_option && !isset($back['rule'])) ? true : false, $check_option, $check_lang); |
556 | |
557 | if ($check_option && !isset($back['rule'])) |
558 | { |
559 | define_rule_option(($rule_option && !isset($back['cond'])) ? true : false, $rule_option, $rule_lang, $global_privmsgs_rules[$check_option]); |
560 | } |
561 | |
562 | if ($rule_option && !isset($back['cond'])) |
563 | { |
564 | if (!isset($global_rule_conditions[$rule_option])) |
565 | { |
566 | $cond_option = 'none'; |
567 | $template->assign_var('NONE_CONDITION', true); |
568 | } |
569 | else |
570 | { |
571 | define_cond_option(($cond_option && !isset($back['action'])) ? true : false, $cond_option, $rule_option, $global_rule_conditions); |
572 | } |
573 | } |
574 | |
575 | if ($cond_option && !isset($back['action'])) |
576 | { |
577 | define_action_option(false, $action_option, $action_lang, $folder); |
578 | } |
579 | |
580 | show_defined_rules($user->data['user_id'], $check_lang, $rule_lang, $action_lang, $folder); |
581 | } |
582 | |
583 | /** |
584 | * Defining check option for message rules |
585 | */ |
586 | function define_check_option($hardcoded, $check_option, $check_lang) |
587 | { |
588 | global $template; |
589 | |
590 | $s_check_options = ''; |
591 | if (!$hardcoded) |
592 | { |
593 | foreach ($check_lang as $value => $lang) |
594 | { |
595 | $s_check_options .= '<option value="' . $value . '"' . (($value == $check_option) ? ' selected="selected"' : '') . '>' . $lang . '</option>'; |
596 | } |
597 | } |
598 | |
599 | $template->assign_vars(array( |
600 | 'S_CHECK_DEFINED' => true, |
601 | 'S_CHECK_SELECT' => ($hardcoded) ? false : true, |
602 | 'CHECK_CURRENT' => isset($check_lang[$check_option]) ? $check_lang[$check_option] : '', |
603 | 'S_CHECK_OPTIONS' => $s_check_options, |
604 | 'CHECK_OPTION' => $check_option) |
605 | ); |
606 | } |
607 | |
608 | /** |
609 | * Defining action option for message rules |
610 | */ |
611 | function define_action_option($hardcoded, $action_option, $action_lang, $folder) |
612 | { |
613 | global $template; |
614 | |
615 | $l_action = $s_action_options = ''; |
616 | if ($hardcoded) |
617 | { |
618 | $option = explode('|', $action_option); |
619 | $action = (int) $option[0]; |
620 | $folder_id = (int) $option[1]; |
621 | |
622 | $l_action = $action_lang[$action]; |
623 | if ($action == ACTION_PLACE_INTO_FOLDER) |
624 | { |
625 | $l_action .= ' -> ' . $folder[$folder_id]['folder_name']; |
626 | } |
627 | } |
628 | else |
629 | { |
630 | foreach ($action_lang as $action => $lang) |
631 | { |
632 | if ($action == ACTION_PLACE_INTO_FOLDER) |
633 | { |
634 | foreach ($folder as $folder_id => $folder_ary) |
635 | { |
636 | $s_action_options .= '<option value="' . $action . '|' . $folder_id . '"' . (($action_option == $action . '|' . $folder_id) ? ' selected="selected"' : '') . '>' . $lang . ' -> ' . $folder_ary['folder_name'] . '</option>'; |
637 | } |
638 | } |
639 | else |
640 | { |
641 | $s_action_options .= '<option value="' . $action . '|0"' . (($action_option == $action . '|0') ? ' selected="selected"' : '') . '>' . $lang . '</option>'; |
642 | } |
643 | } |
644 | } |
645 | |
646 | $template->assign_vars(array( |
647 | 'S_ACTION_DEFINED' => true, |
648 | 'S_ACTION_SELECT' => ($hardcoded) ? false : true, |
649 | 'ACTION_CURRENT' => $l_action, |
650 | 'S_ACTION_OPTIONS' => $s_action_options, |
651 | 'ACTION_OPTION' => $action_option) |
652 | ); |
653 | } |
654 | |
655 | /** |
656 | * Defining rule option for message rules |
657 | */ |
658 | function define_rule_option($hardcoded, $rule_option, $rule_lang, $check_ary) |
659 | { |
660 | global $template; |
661 | global $module; |
662 | |
663 | $exclude = array(); |
664 | |
665 | if (!$module->loaded('zebra', 'friends')) |
666 | { |
667 | $exclude[RULE_IS_FRIEND] = true; |
668 | } |
669 | |
670 | if (!$module->loaded('zebra', 'foes')) |
671 | { |
672 | $exclude[RULE_IS_FOE] = true; |
673 | } |
674 | |
675 | $s_rule_options = ''; |
676 | if (!$hardcoded) |
677 | { |
678 | foreach ($check_ary as $value => $_check) |
679 | { |
680 | if (isset($exclude[$value])) |
681 | { |
682 | continue; |
683 | } |
684 | $s_rule_options .= '<option value="' . $value . '"' . (($value == $rule_option) ? ' selected="selected"' : '') . '>' . $rule_lang[$value] . '</option>'; |
685 | } |
686 | } |
687 | |
688 | $template->assign_vars(array( |
689 | 'S_RULE_DEFINED' => true, |
690 | 'S_RULE_SELECT' => !$hardcoded, |
691 | 'RULE_CURRENT' => isset($rule_lang[$rule_option]) ? $rule_lang[$rule_option] : '', |
692 | 'S_RULE_OPTIONS' => $s_rule_options, |
693 | 'RULE_OPTION' => $rule_option) |
694 | ); |
695 | } |
696 | |
697 | /** |
698 | * Defining condition option for message rules |
699 | */ |
700 | function define_cond_option($hardcoded, $cond_option, $rule_option, $global_rule_conditions) |
701 | { |
702 | global $db, $template, $auth, $user, $request, $phpbb_container; |
703 | |
704 | /** @var \phpbb\group\helper $group_helper */ |
705 | $group_helper = $phpbb_container->get('group_helper'); |
706 | |
707 | $template->assign_vars(array( |
708 | 'S_COND_DEFINED' => true, |
709 | 'S_COND_SELECT' => (!$hardcoded && isset($global_rule_conditions[$rule_option])) ? true : false) |
710 | ); |
711 | |
712 | // Define COND_OPTION |
713 | if (!isset($global_rule_conditions[$rule_option])) |
714 | { |
715 | $template->assign_vars(array( |
716 | 'COND_OPTION' => 'none', |
717 | 'COND_CURRENT' => false) |
718 | ); |
719 | return; |
720 | } |
721 | |
722 | // Define Condition |
723 | $condition = $global_rule_conditions[$rule_option]; |
724 | |
725 | switch ($condition) |
726 | { |
727 | case 'text': |
728 | $rule_string = $request->variable('rule_string', '', true); |
729 | |
730 | $template->assign_vars(array( |
731 | 'S_TEXT_CONDITION' => true, |
732 | 'CURRENT_STRING' => $rule_string, |
733 | 'CURRENT_USER_ID' => 0, |
734 | 'CURRENT_GROUP_ID' => 0) |
735 | ); |
736 | |
737 | $current_value = $rule_string; |
738 | break; |
739 | |
740 | case 'user': |
741 | $rule_user_id = $request->variable('rule_user_id', 0); |
742 | $rule_string = $request->variable('rule_string', '', true); |
743 | |
744 | if ($rule_string && !$rule_user_id) |
745 | { |
746 | $sql = 'SELECT user_id |
747 | FROM ' . USERS_TABLE . " |
748 | WHERE username_clean = '" . $db->sql_escape(utf8_clean_string($rule_string)) . "'"; |
749 | $result = $db->sql_query($sql); |
750 | $rule_user_id = (int) $db->sql_fetchfield('user_id'); |
751 | $db->sql_freeresult($result); |
752 | |
753 | if (!$rule_user_id) |
754 | { |
755 | $rule_string = ''; |
756 | } |
757 | } |
758 | else if (!$rule_string && $rule_user_id) |
759 | { |
760 | $sql = 'SELECT username |
761 | FROM ' . USERS_TABLE . " |
762 | WHERE user_id = $rule_user_id"; |
763 | $result = $db->sql_query($sql); |
764 | $rule_string = $db->sql_fetchfield('username'); |
765 | $db->sql_freeresult($result); |
766 | |
767 | if (!$rule_string) |
768 | { |
769 | $rule_user_id = 0; |
770 | } |
771 | } |
772 | |
773 | $template->assign_vars(array( |
774 | 'S_USER_CONDITION' => true, |
775 | 'CURRENT_STRING' => $rule_string, |
776 | 'CURRENT_USER_ID' => $rule_user_id, |
777 | 'CURRENT_GROUP_ID' => 0) |
778 | ); |
779 | |
780 | $current_value = $rule_string; |
781 | break; |
782 | |
783 | case 'group': |
784 | $rule_group_id = $request->variable('rule_group_id', 0); |
785 | $rule_string = $request->variable('rule_string', '', true); |
786 | |
787 | $sql = 'SELECT g.group_id, g.group_name, g.group_type |
788 | FROM ' . GROUPS_TABLE . ' g '; |
789 | |
790 | if (!$auth->acl_gets('a_group', 'a_groupadd', 'a_groupdel')) |
791 | { |
792 | $sql .= 'LEFT JOIN ' . USER_GROUP_TABLE . ' ug |
793 | ON ( |
794 | g.group_id = ug.group_id |
795 | AND ug.user_id = ' . $user->data['user_id'] . ' |
796 | AND ug.user_pending = 0 |
797 | ) |
798 | WHERE (ug.user_id = ' . $user->data['user_id'] . ' OR g.group_type <> ' . GROUP_HIDDEN . ') |
799 | AND'; |
800 | } |
801 | else |
802 | { |
803 | $sql .= 'WHERE'; |
804 | } |
805 | |
806 | $sql .= " (g.group_name NOT IN ('GUESTS', 'BOTS') OR g.group_type <> " . GROUP_SPECIAL . ') |
807 | ORDER BY g.group_type DESC, g.group_name ASC'; |
808 | |
809 | $result = $db->sql_query($sql); |
810 | |
811 | $s_group_options = ''; |
812 | while ($row = $db->sql_fetchrow($result)) |
813 | { |
814 | if ($rule_group_id && ($row['group_id'] == $rule_group_id)) |
815 | { |
816 | $rule_string = $group_helper->get_name($row['group_name']); |
817 | } |
818 | |
819 | $s_class = ($row['group_type'] == GROUP_SPECIAL) ? ' class="sep"' : ''; |
820 | $s_selected = ($row['group_id'] == $rule_group_id) ? ' selected="selected"' : ''; |
821 | |
822 | $s_group_options .= '<option value="' . $row['group_id'] . '"' . $s_class . $s_selected . '>' . $group_helper->get_name($row['group_name']) . '</option>'; |
823 | } |
824 | $db->sql_freeresult($result); |
825 | |
826 | $template->assign_vars(array( |
827 | 'S_GROUP_CONDITION' => true, |
828 | 'S_GROUP_OPTIONS' => $s_group_options, |
829 | 'CURRENT_STRING' => $rule_string, |
830 | 'CURRENT_USER_ID' => 0, |
831 | 'CURRENT_GROUP_ID' => $rule_group_id) |
832 | ); |
833 | |
834 | $current_value = $rule_string; |
835 | break; |
836 | |
837 | default: |
838 | return; |
839 | } |
840 | |
841 | $template->assign_vars(array( |
842 | 'COND_OPTION' => $condition, |
843 | 'COND_CURRENT' => $current_value) |
844 | ); |
845 | } |
846 | |
847 | /** |
848 | * Display defined message rules |
849 | */ |
850 | function show_defined_rules($user_id, $check_lang, $rule_lang, $action_lang, $folder) |
851 | { |
852 | global $db, $template; |
853 | |
854 | $sql = 'SELECT * |
855 | FROM ' . PRIVMSGS_RULES_TABLE . ' |
856 | WHERE user_id = ' . $user_id . ' |
857 | ORDER BY rule_id ASC'; |
858 | $result = $db->sql_query($sql); |
859 | |
860 | $count = 0; |
861 | while ($row = $db->sql_fetchrow($result)) |
862 | { |
863 | $template->assign_block_vars('rule', array( |
864 | 'COUNT' => ++$count, |
865 | 'RULE_ID' => $row['rule_id'], |
866 | 'CHECK' => $check_lang[$row['rule_check']], |
867 | 'RULE' => $rule_lang[$row['rule_connection']], |
868 | 'STRING' => $row['rule_string'], |
869 | 'ACTION' => $action_lang[$row['rule_action']], |
870 | 'FOLDER' => ($row['rule_action'] == ACTION_PLACE_INTO_FOLDER) ? $folder[$row['rule_folder_id']]['folder_name'] : '') |
871 | ); |
872 | } |
873 | $db->sql_freeresult($result); |
874 | } |