forked from SimpleMachines/SMF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPost.php
3203 lines (2789 loc) · 116 KB
/
Post.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* The job of this file is to handle everything related to posting replies,
* new topics, quotes, and modifications to existing posts. It also handles
* quoting posts by way of javascript.
* Simple Machines Forum (SMF)
*
* @package SMF
* @author Simple Machines https://www.simplemachines.org
* @copyright 2020 Simple Machines and individual contributors
* @license https://www.simplemachines.org/about/smf/license.php BSD
*
* @version 2.1 RC3
*/
if (!defined('SMF'))
die('No direct access...');
/**
* Handles showing the post screen, loading the post to be modified, and loading any post quoted.
*
* - additionally handles previews of posts.
* - Uses the Post template and language file, main sub template.
* - requires different permissions depending on the actions, but most notably post_new, post_reply_own, and post_reply_any.
* - shows options for the editing and posting of calendar events and attachments, as well as the posting of polls.
* - accessed from ?action=post.
*
* @param array $post_errors Holds any errors found while tyring to post
*/
function Post($post_errors = array())
{
global $txt, $scripturl, $topic, $modSettings, $board;
global $user_info, $context, $settings;
global $sourcedir, $smcFunc, $language;
loadLanguage('Post');
if (!empty($modSettings['drafts_post_enabled']))
loadLanguage('Drafts');
// You can't reply with a poll... hacker.
if (isset($_REQUEST['poll']) && !empty($topic) && !isset($_REQUEST['msg']))
unset($_REQUEST['poll']);
// Posting an event?
$context['make_event'] = isset($_REQUEST['calendar']);
$context['robot_no_index'] = true;
call_integration_hook('integrate_post_start');
// Get notification preferences for later
require_once($sourcedir . '/Subs-Notify.php');
// use $temp to get around "Only variables should be passed by reference"
$temp = getNotifyPrefs($user_info['id']);
$context['notify_prefs'] = (array) array_pop($temp);
$context['auto_notify'] = !empty($context['notify_prefs']['msg_auto_notify']);
// Not in a board? Fine, but we'll make them pick one eventually.
if (empty($board) || $context['make_event'])
{
// Get ids of all the boards they can post in.
$post_permissions = array('post_new');
if ($modSettings['postmod_active'])
$post_permissions[] = 'post_unapproved_topics';
$boards = boardsAllowedTo($post_permissions);
if (empty($boards))
fatal_lang_error('cannot_post_new', false);
// Get a list of boards for the select menu
require_once($sourcedir . '/Subs-MessageIndex.php');
$boardListOptions = array(
'included_boards' => in_array(0, $boards) ? null : $boards,
'not_redirection' => true,
'use_permissions' => true,
'selected_board' => !empty($board) ? $board : ($context['make_event'] && !empty($modSettings['cal_defaultboard']) ? $modSettings['cal_defaultboard'] : $boards[0]),
);
$board_list = getBoardList($boardListOptions);
}
// Let's keep things simple for ourselves below
else
$boards = array($board);
require_once($sourcedir . '/Subs-Post.php');
if (isset($_REQUEST['xml']))
{
$context['sub_template'] = 'post';
// Just in case of an earlier error...
$context['preview_message'] = '';
$context['preview_subject'] = '';
}
// No message is complete without a topic.
if (empty($topic) && !empty($_REQUEST['msg']))
{
$request = $smcFunc['db_query']('', '
SELECT id_topic
FROM {db_prefix}messages
WHERE id_msg = {int:msg}',
array(
'msg' => (int) $_REQUEST['msg'],
)
);
if ($smcFunc['db_num_rows']($request) != 1)
unset($_REQUEST['msg'], $_POST['msg'], $_GET['msg']);
else
list ($topic) = $smcFunc['db_fetch_row']($request);
$smcFunc['db_free_result']($request);
}
// Check if it's locked. It isn't locked if no topic is specified.
if (!empty($topic))
{
$request = $smcFunc['db_query']('', '
SELECT
t.locked, t.approved, COALESCE(ln.id_topic, 0) AS notify, t.is_sticky, t.id_poll, t.id_last_msg, mf.id_member,
t.id_first_msg, mf.subject, ml.modified_reason,
CASE WHEN ml.poster_time > ml.modified_time THEN ml.poster_time ELSE ml.modified_time END AS last_post_time
FROM {db_prefix}topics AS t
LEFT JOIN {db_prefix}log_notify AS ln ON (ln.id_topic = t.id_topic AND ln.id_member = {int:current_member})
LEFT JOIN {db_prefix}messages AS mf ON (mf.id_msg = t.id_first_msg)
LEFT JOIN {db_prefix}messages AS ml ON (ml.id_msg = t.id_last_msg)
WHERE t.id_topic = {int:current_topic}
LIMIT 1',
array(
'current_member' => $user_info['id'],
'current_topic' => $topic,
)
);
list ($locked, $topic_approved, $context['notify'], $sticky, $pollID, $context['topic_last_message'], $id_member_poster, $id_first_msg, $first_subject, $editReason, $lastPostTime) = $smcFunc['db_fetch_row']($request);
$smcFunc['db_free_result']($request);
// If this topic already has a poll, they sure can't add another.
if (isset($_REQUEST['poll']) && $pollID > 0)
unset($_REQUEST['poll']);
if (empty($_REQUEST['msg']))
{
if ($user_info['is_guest'] && !allowedTo('post_reply_any') && (!$modSettings['postmod_active'] || !allowedTo('post_unapproved_replies_any')))
is_not_guest();
// By default the reply will be approved...
$context['becomes_approved'] = true;
if ($id_member_poster != $user_info['id'] || $user_info['is_guest'])
{
if ($modSettings['postmod_active'] && allowedTo('post_unapproved_replies_any') && !allowedTo('post_reply_any'))
$context['becomes_approved'] = false;
else
isAllowedTo('post_reply_any');
}
elseif (!allowedTo('post_reply_any'))
{
if ($modSettings['postmod_active'] && ((allowedTo('post_unapproved_replies_own') && !allowedTo('post_reply_own')) || allowedTo('post_unapproved_replies_any')))
$context['becomes_approved'] = false;
else
isAllowedTo('post_reply_own');
}
}
else
$context['becomes_approved'] = true;
$context['can_lock'] = allowedTo('lock_any') || ($user_info['id'] == $id_member_poster && allowedTo('lock_own'));
$context['can_sticky'] = allowedTo('make_sticky');
$context['can_move'] = allowedTo('move_any');
// You can only announce topics that will get approved...
$context['can_announce'] = allowedTo('announce_topic') && $context['becomes_approved'];
$context['show_approval'] = !allowedTo('approve_posts') ? 0 : ($context['becomes_approved'] ? 2 : 1);
// We don't always want the request vars to override what's in the db...
$context['already_locked'] = $locked;
$context['already_sticky'] = $sticky;
$context['sticky'] = isset($_REQUEST['sticky']) ? !empty($_REQUEST['sticky']) : $sticky;
// Check whether this is a really old post being bumped...
if (!empty($modSettings['oldTopicDays']) && $lastPostTime + $modSettings['oldTopicDays'] * 86400 < time() && empty($sticky) && !isset($_REQUEST['subject']))
$post_errors[] = array('old_topic', array($modSettings['oldTopicDays']));
}
else
{
// @todo Should use JavaScript to hide and show the warning based on the selection in the board select menu
$context['becomes_approved'] = true;
if ($modSettings['postmod_active'] && !allowedTo('post_new', $boards, true) && allowedTo('post_unapproved_topics', $boards, true))
$context['becomes_approved'] = false;
else
isAllowedTo('post_new', $boards, true);
$locked = 0;
$context['already_locked'] = 0;
$context['already_sticky'] = 0;
$context['sticky'] = !empty($_REQUEST['sticky']);
// What options should we show?
$context['can_lock'] = allowedTo(array('lock_any', 'lock_own'), $boards, true);
$context['can_sticky'] = allowedTo('make_sticky', $boards, true);
$context['can_move'] = allowedTo('move_any', $boards, true);
$context['can_announce'] = allowedTo('announce_topic', $boards, true) && $context['becomes_approved'];
$context['show_approval'] = !allowedTo('approve_posts', $boards, true) ? 0 : ($context['becomes_approved'] ? 2 : 1);
}
$context['notify'] = !empty($context['notify']);
$context['can_notify'] = !$context['user']['is_guest'];
$context['move'] = !empty($_REQUEST['move']);
$context['announce'] = !empty($_REQUEST['announce']);
$context['locked'] = !empty($locked) || !empty($_REQUEST['lock']);
$context['can_quote'] = empty($modSettings['disabledBBC']) || !in_array('quote', explode(',', $modSettings['disabledBBC']));
// An array to hold all the attachments for this topic.
$context['current_attachments'] = array();
// Clear out prior attachment activity when starting afresh
if (empty($_REQUEST['message']) && empty($_REQUEST['preview']) && !empty($_SESSION['already_attached']))
{
require_once($sourcedir . '/ManageAttachments.php');
foreach ($_SESSION['already_attached'] as $attachID => $attachment)
removeAttachments(array('id_attach' => $attachID));
unset($_SESSION['already_attached']);
}
// Don't allow a post if it's locked and you aren't all powerful.
if ($locked && !allowedTo('moderate_board'))
fatal_lang_error('topic_locked', false);
// Check the users permissions - is the user allowed to add or post a poll?
if (isset($_REQUEST['poll']) && $modSettings['pollMode'] == '1')
{
// New topic, new poll.
if (empty($topic))
isAllowedTo('poll_post');
// This is an old topic - but it is yours! Can you add to it?
elseif ($user_info['id'] == $id_member_poster && !allowedTo('poll_add_any'))
isAllowedTo('poll_add_own');
// If you're not the owner, can you add to any poll?
else
isAllowedTo('poll_add_any');
if (!empty($board))
{
require_once($sourcedir . '/Subs-Members.php');
$allowedVoteGroups = groupsAllowedTo('poll_vote', $board);
$guest_vote_enabled = in_array(-1, $allowedVoteGroups['allowed']);
}
// No board, so we'll have to check this again in Post2
else
$guest_vote_enabled = true;
// Set up the poll options.
$context['poll_options'] = array(
'max_votes' => empty($_POST['poll_max_votes']) ? '1' : max(1, $_POST['poll_max_votes']),
'hide' => empty($_POST['poll_hide']) ? 0 : $_POST['poll_hide'],
'expire' => !isset($_POST['poll_expire']) ? '' : $_POST['poll_expire'],
'change_vote' => isset($_POST['poll_change_vote']),
'guest_vote' => isset($_POST['poll_guest_vote']),
'guest_vote_enabled' => $guest_vote_enabled,
);
// Make all five poll choices empty.
$context['choices'] = array(
array('id' => 0, 'number' => 1, 'label' => '', 'is_last' => false),
array('id' => 1, 'number' => 2, 'label' => '', 'is_last' => false),
array('id' => 2, 'number' => 3, 'label' => '', 'is_last' => false),
array('id' => 3, 'number' => 4, 'label' => '', 'is_last' => false),
array('id' => 4, 'number' => 5, 'label' => '', 'is_last' => true)
);
$context['last_choice_id'] = 4;
}
if ($context['make_event'])
{
// They might want to pick a board.
if (!isset($context['current_board']))
$context['current_board'] = 0;
// Start loading up the event info.
$context['event'] = array();
$context['event']['title'] = isset($_REQUEST['evtitle']) ? $smcFunc['htmlspecialchars'](stripslashes($_REQUEST['evtitle'])) : '';
$context['event']['location'] = isset($_REQUEST['event_location']) ? $smcFunc['htmlspecialchars'](stripslashes($_REQUEST['event_location'])) : '';
$context['event']['id'] = isset($_REQUEST['eventid']) ? (int) $_REQUEST['eventid'] : -1;
$context['event']['new'] = $context['event']['id'] == -1;
// Permissions check!
isAllowedTo('calendar_post');
require_once($sourcedir . '/Subs-Calendar.php');
// We want a fairly compact version of the time, but as close as possible to the user's settings.
$time_string = strtr(get_date_or_time_format('time'), array(
'%I' => '%l',
'%H' => '%k',
'%S' => '',
'%r' => '%l:%M %p',
'%R' => '%k:%M',
'%T' => '%l:%M',
));
// Editing an event? (but NOT previewing!?)
if (empty($context['event']['new']) && !isset($_REQUEST['subject']))
{
// If the user doesn't have permission to edit the post in this topic, redirect them.
if ((empty($id_member_poster) || $id_member_poster != $user_info['id'] || !allowedTo('modify_own')) && !allowedTo('modify_any'))
{
require_once($sourcedir . '/Calendar.php');
return CalendarPost();
}
// Get the current event information.
$eventProperties = getEventProperties($context['event']['id']);
$context['event'] = array_merge($context['event'], $eventProperties);
}
else
{
// Get the current event information.
$eventProperties = getNewEventDatetimes();
$context['event'] = array_merge($context['event'], $eventProperties);
// Make sure the year and month are in the valid range.
if ($context['event']['month'] < 1 || $context['event']['month'] > 12)
fatal_lang_error('invalid_month', false);
if ($context['event']['year'] < $modSettings['cal_minyear'] || $context['event']['year'] > $modSettings['cal_maxyear'])
fatal_lang_error('invalid_year', false);
$context['event']['categories'] = $board_list;
}
// Find the last day of the month.
$context['event']['last_day'] = (int) strftime('%d', mktime(0, 0, 0, $context['event']['month'] == 12 ? 1 : $context['event']['month'] + 1, 0, $context['event']['month'] == 12 ? $context['event']['year'] + 1 : $context['event']['year']));
// An all day event? Set up some nice defaults in case the user wants to change that
if ($context['event']['allday'] == true)
{
$context['event']['tz'] = getUserTimezone();
$context['event']['start_time'] = timeformat(time(), $time_string);
$context['event']['end_time'] = timeformat(time() + 3600, $time_string);
}
// Otherwise, just adjust these to look nice on the input form
else
{
$context['event']['start_time'] = $context['event']['start_time_orig'];
$context['event']['end_time'] = $context['event']['end_time_orig'];
}
// Need this so the user can select a timezone for the event.
$context['all_timezones'] = smf_list_timezones($context['event']['start_date']);
// If the event's timezone is not in SMF's standard list of time zones, try to fix it.
if (!isset($context['all_timezones'][$context['event']['tz']]))
{
$later = strtotime('@' . $context['event']['start_timestamp'] . ' + 1 year');
$tzinfo = timezone_transitions_get(timezone_open($context['event']['tz']), $context['event']['start_timestamp'], $later);
$found = false;
foreach ($context['all_timezones'] as $possible_tzid => $dummy)
{
$possible_tzinfo = timezone_transitions_get(timezone_open($possible_tzid), $context['event']['start_timestamp'], $later);
if ($tzinfo === $possible_tzinfo)
{
$context['event']['tz'] = $possible_tzid;
$found = true;
break;
}
}
// Hm. That's weird. Well, just prepend it to the list and let the user deal with it.
if (!$found)
{
$d = date_create($context['event']['start_datetime'] . ' ' . $context['event']['tz']);
$context['all_timezones'] = array($context['event']['tz'] => '[UTC' . date_format($d, 'P') . '] - ' . $context['event']['tz']) + $context['all_timezones'];
}
}
loadDatePicker('#event_time_input .date_input');
loadTimePicker('#event_time_input .time_input', $time_string);
loadDatePair('#event_time_input', 'date_input', 'time_input');
addInlineJavaScript('
$("#allday").click(function(){
$("#start_time").attr("disabled", this.checked);
$("#end_time").attr("disabled", this.checked);
$("#tz").attr("disabled", this.checked);
}); ', true);
$context['event']['board'] = !empty($board) ? $board : $modSettings['cal_defaultboard'];
$context['event']['topic'] = !empty($topic) ? $topic : 0;
}
// See if any new replies have come along.
// Huh, $_REQUEST['msg'] is set upon submit, so this doesn't get executed at submit
// only at preview
if (empty($_REQUEST['msg']) && !empty($topic))
{
if (isset($_REQUEST['last_msg']) && $context['topic_last_message'] > $_REQUEST['last_msg'])
{
$request = $smcFunc['db_query']('', '
SELECT COUNT(*)
FROM {db_prefix}messages
WHERE id_topic = {int:current_topic}
AND id_msg > {int:last_msg}' . (!$modSettings['postmod_active'] || allowedTo('approve_posts') ? '' : '
AND approved = {int:approved}') . '
LIMIT 1',
array(
'current_topic' => $topic,
'last_msg' => (int) $_REQUEST['last_msg'],
'approved' => 1,
)
);
list ($context['new_replies']) = $smcFunc['db_fetch_row']($request);
$smcFunc['db_free_result']($request);
if (!empty($context['new_replies']))
{
if ($context['new_replies'] == 1)
$txt['error_new_replies'] = isset($_GET['last_msg']) ? $txt['error_new_reply_reading'] : $txt['error_new_reply'];
else
$txt['error_new_replies'] = sprintf(isset($_GET['last_msg']) ? $txt['error_new_replies_reading'] : $txt['error_new_replies'], $context['new_replies']);
$post_errors[] = 'new_replies';
$modSettings['topicSummaryPosts'] = $context['new_replies'] > $modSettings['topicSummaryPosts'] ? max($modSettings['topicSummaryPosts'], 5) : $modSettings['topicSummaryPosts'];
}
}
}
// Get a response prefix (like 'Re:') in the default forum language.
if (!isset($context['response_prefix']) && !($context['response_prefix'] = cache_get_data('response_prefix')))
{
if ($language === $user_info['language'])
$context['response_prefix'] = $txt['response_prefix'];
else
{
loadLanguage('index', $language, false);
$context['response_prefix'] = $txt['response_prefix'];
loadLanguage('index');
}
cache_put_data('response_prefix', $context['response_prefix'], 600);
}
// Previewing, modifying, or posting?
// Do we have a body, but an error happened.
if (isset($_REQUEST['message']) || isset($_REQUEST['quickReply']) || !empty($context['post_error']))
{
if (isset($_REQUEST['quickReply']))
$_REQUEST['message'] = $_REQUEST['quickReply'];
// Validate inputs.
if (empty($context['post_error']))
{
// This means they didn't click Post and get an error.
$really_previewing = true;
}
else
{
if (!isset($_REQUEST['subject']))
$_REQUEST['subject'] = '';
if (!isset($_REQUEST['message']))
$_REQUEST['message'] = '';
if (!isset($_REQUEST['icon']))
$_REQUEST['icon'] = 'xx';
// They are previewing if they asked to preview (i.e. came from quick reply).
$really_previewing = !empty($_POST['preview']);
}
// In order to keep the approval status flowing through, we have to pass it through the form...
$context['becomes_approved'] = empty($_REQUEST['not_approved']);
$context['show_approval'] = isset($_REQUEST['approve']) ? ($_REQUEST['approve'] ? 2 : 1) : (allowedTo('approve_posts') ? 2 : 0);
$context['can_announce'] &= $context['becomes_approved'];
// Set up the inputs for the form.
$form_subject = strtr($smcFunc['htmlspecialchars']($_REQUEST['subject']), array("\r" => '', "\n" => '', "\t" => ''));
$form_message = $smcFunc['htmlspecialchars']($_REQUEST['message'], ENT_QUOTES);
// Make sure the subject isn't too long - taking into account special characters.
if ($smcFunc['strlen']($form_subject) > 100)
$form_subject = $smcFunc['substr']($form_subject, 0, 100);
if (isset($_REQUEST['poll']))
{
$context['question'] = isset($_REQUEST['question']) ? $smcFunc['htmlspecialchars'](trim($_REQUEST['question'])) : '';
$context['choices'] = array();
$choice_id = 0;
$_POST['options'] = empty($_POST['options']) ? array() : htmlspecialchars__recursive($_POST['options']);
foreach ($_POST['options'] as $option)
{
if (trim($option) == '')
continue;
$context['choices'][] = array(
'id' => $choice_id++,
'number' => $choice_id,
'label' => $option,
'is_last' => false
);
}
// One empty option for those with js disabled...I know are few... :P
$context['choices'][] = array(
'id' => $choice_id++,
'number' => $choice_id,
'label' => '',
'is_last' => false
);
if (count($context['choices']) < 2)
{
$context['choices'][] = array(
'id' => $choice_id++,
'number' => $choice_id,
'label' => '',
'is_last' => false
);
}
$context['last_choice_id'] = $choice_id;
$context['choices'][count($context['choices']) - 1]['is_last'] = true;
}
// Are you... a guest?
if ($user_info['is_guest'])
{
$_REQUEST['guestname'] = !isset($_REQUEST['guestname']) ? '' : trim($_REQUEST['guestname']);
$_REQUEST['email'] = !isset($_REQUEST['email']) ? '' : trim($_REQUEST['email']);
$_REQUEST['guestname'] = $smcFunc['htmlspecialchars']($_REQUEST['guestname']);
$context['name'] = $_REQUEST['guestname'];
$_REQUEST['email'] = $smcFunc['htmlspecialchars']($_REQUEST['email']);
$context['email'] = $_REQUEST['email'];
$user_info['name'] = $_REQUEST['guestname'];
}
// Only show the preview stuff if they hit Preview.
if (($really_previewing == true || isset($_REQUEST['xml'])) && !isset($_REQUEST['save_draft']))
{
// Set up the preview message and subject and censor them...
$context['preview_message'] = $form_message;
preparsecode($form_message, true);
preparsecode($context['preview_message']);
// Do all bulletin board code tags, with or without smileys.
$context['preview_message'] = parse_bbc($context['preview_message'], isset($_REQUEST['ns']) ? 0 : 1);
censorText($context['preview_message']);
if ($form_subject != '')
{
$context['preview_subject'] = $form_subject;
censorText($context['preview_subject']);
}
else
$context['preview_subject'] = '<em>' . $txt['no_subject'] . '</em>';
call_integration_hook('integrate_preview_post', array(&$form_message, &$form_subject));
// Protect any CDATA blocks.
if (isset($_REQUEST['xml']))
$context['preview_message'] = strtr($context['preview_message'], array(']]>' => ']]]]><![CDATA[>'));
}
// Set up the checkboxes.
$context['notify'] = !empty($_REQUEST['notify']);
$context['use_smileys'] = !isset($_REQUEST['ns']);
$context['icon'] = isset($_REQUEST['icon']) ? preg_replace('~[\./\\\\*\':"<>]~', '', $_REQUEST['icon']) : 'xx';
// Set the destination action for submission.
$context['destination'] = 'post2;start=' . $_REQUEST['start'] . (isset($_REQUEST['msg']) ? ';msg=' . $_REQUEST['msg'] . ';' . $context['session_var'] . '=' . $context['session_id'] : '') . (isset($_REQUEST['poll']) ? ';poll' : '');
$context['submit_label'] = isset($_REQUEST['msg']) ? $txt['save'] : $txt['post'];
// Previewing an edit?
if (isset($_REQUEST['msg']) && !empty($topic))
{
// Get the existing message. Previewing.
$request = $smcFunc['db_query']('', '
SELECT
m.id_member, m.modified_time, m.smileys_enabled, m.body,
m.poster_name, m.poster_email, m.subject, m.icon, m.approved,
COALESCE(a.size, -1) AS filesize, a.filename, a.id_attach,
a.approved AS attachment_approved, t.id_member_started AS id_member_poster,
m.poster_time, log.id_action
FROM {db_prefix}messages AS m
INNER JOIN {db_prefix}topics AS t ON (t.id_topic = {int:current_topic})
LEFT JOIN {db_prefix}attachments AS a ON (a.id_msg = m.id_msg AND a.attachment_type = {int:attachment_type})
LEFT JOIN {db_prefix}log_actions AS log ON (m.id_topic = log.id_topic AND log.action = {string:announce_action})
WHERE m.id_msg = {int:id_msg}
AND m.id_topic = {int:current_topic}',
array(
'current_topic' => $topic,
'attachment_type' => 0,
'id_msg' => $_REQUEST['msg'],
'announce_action' => 'announce_topic',
)
);
// The message they were trying to edit was most likely deleted.
// @todo Change this error message?
if ($smcFunc['db_num_rows']($request) == 0)
fatal_lang_error('no_board', false);
$row = $smcFunc['db_fetch_assoc']($request);
$attachment_stuff = array($row);
while ($row2 = $smcFunc['db_fetch_assoc']($request))
$attachment_stuff[] = $row2;
$smcFunc['db_free_result']($request);
if ($row['id_member'] == $user_info['id'] && !allowedTo('modify_any'))
{
// Give an extra five minutes over the disable time threshold, so they can type - assuming the post is public.
if ($row['approved'] && !empty($modSettings['edit_disable_time']) && $row['poster_time'] + ($modSettings['edit_disable_time'] + 5) * 60 < time())
fatal_lang_error('modify_post_time_passed', false);
elseif ($row['id_member_poster'] == $user_info['id'] && !allowedTo('modify_own'))
isAllowedTo('modify_replies');
else
isAllowedTo('modify_own');
}
elseif ($row['id_member_poster'] == $user_info['id'] && !allowedTo('modify_any'))
isAllowedTo('modify_replies');
else
isAllowedTo('modify_any');
if ($context['can_announce'] && !empty($row['id_action']))
{
loadLanguage('Errors');
$context['post_error']['messages'][] = $txt['error_topic_already_announced'];
}
if (!empty($modSettings['attachmentEnable']))
{
$request = $smcFunc['db_query']('', '
SELECT COALESCE(size, -1) AS filesize, filename, id_attach, approved, mime_type, id_thumb
FROM {db_prefix}attachments
WHERE id_msg = {int:id_msg}
AND attachment_type = {int:attachment_type}
ORDER BY id_attach',
array(
'id_msg' => (int) $_REQUEST['msg'],
'attachment_type' => 0,
)
);
while ($row = $smcFunc['db_fetch_assoc']($request))
{
if ($row['filesize'] <= 0)
continue;
$context['current_attachments'][$row['id_attach']] = array(
'name' => $smcFunc['htmlspecialchars']($row['filename']),
'size' => $row['filesize'],
'attachID' => $row['id_attach'],
'approved' => $row['approved'],
'mime_type' => $row['mime_type'],
'thumb' => $row['id_thumb'],
);
}
$smcFunc['db_free_result']($request);
}
// Allow moderators to change names....
if (allowedTo('moderate_forum') && !empty($topic))
{
$request = $smcFunc['db_query']('', '
SELECT id_member, poster_name, poster_email
FROM {db_prefix}messages
WHERE id_msg = {int:id_msg}
AND id_topic = {int:current_topic}
LIMIT 1',
array(
'current_topic' => $topic,
'id_msg' => (int) $_REQUEST['msg'],
)
);
$row = $smcFunc['db_fetch_assoc']($request);
$smcFunc['db_free_result']($request);
if (empty($row['id_member']))
{
$context['name'] = $smcFunc['htmlspecialchars']($row['poster_name']);
$context['email'] = $smcFunc['htmlspecialchars']($row['poster_email']);
}
}
}
// No check is needed, since nothing is really posted.
checkSubmitOnce('free');
}
// Editing a message...
elseif (isset($_REQUEST['msg']) && !empty($topic))
{
$context['editing'] = true;
$_REQUEST['msg'] = (int) $_REQUEST['msg'];
// Get the existing message. Editing.
$request = $smcFunc['db_query']('', '
SELECT
m.id_member, m.modified_time, m.modified_name, m.modified_reason, m.smileys_enabled, m.body,
m.poster_name, m.poster_email, m.subject, m.icon, m.approved,
COALESCE(a.size, -1) AS filesize, a.filename, a.id_attach, a.mime_type, a.id_thumb,
a.approved AS attachment_approved, t.id_member_started AS id_member_poster,
m.poster_time, log.id_action
FROM {db_prefix}messages AS m
INNER JOIN {db_prefix}topics AS t ON (t.id_topic = {int:current_topic})
LEFT JOIN {db_prefix}attachments AS a ON (a.id_msg = m.id_msg AND a.attachment_type = {int:attachment_type})
LEFT JOIN {db_prefix}log_actions AS log ON (m.id_topic = log.id_topic AND log.action = {string:announce_action})
WHERE m.id_msg = {int:id_msg}
AND m.id_topic = {int:current_topic}',
array(
'current_topic' => $topic,
'attachment_type' => 0,
'id_msg' => $_REQUEST['msg'],
'announce_action' => 'announce_topic',
)
);
// The message they were trying to edit was most likely deleted.
if ($smcFunc['db_num_rows']($request) == 0)
fatal_lang_error('no_message', false);
$row = $smcFunc['db_fetch_assoc']($request);
$attachment_stuff = array($row);
while ($row2 = $smcFunc['db_fetch_assoc']($request))
$attachment_stuff[] = $row2;
$smcFunc['db_free_result']($request);
if ($row['id_member'] == $user_info['id'] && !allowedTo('modify_any'))
{
// Give an extra five minutes over the disable time threshold, so they can type - assuming the post is public.
if ($row['approved'] && !empty($modSettings['edit_disable_time']) && $row['poster_time'] + ($modSettings['edit_disable_time'] + 5) * 60 < time())
fatal_lang_error('modify_post_time_passed', false);
elseif ($row['id_member_poster'] == $user_info['id'] && !allowedTo('modify_own'))
isAllowedTo('modify_replies');
else
isAllowedTo('modify_own');
}
elseif ($row['id_member_poster'] == $user_info['id'] && !allowedTo('modify_any'))
isAllowedTo('modify_replies');
else
isAllowedTo('modify_any');
if ($context['can_announce'] && !empty($row['id_action']))
{
loadLanguage('Errors');
$context['post_error']['messages'][] = $txt['error_topic_already_announced'];
}
// When was it last modified?
if (!empty($row['modified_time']))
{
$context['last_modified'] = timeformat($row['modified_time']);
$context['last_modified_reason'] = censorText($row['modified_reason']);
$context['last_modified_text'] = sprintf($txt['last_edit_by'], $context['last_modified'], $row['modified_name']) . empty($row['modified_reason']) ? '' : ' ' . $txt['last_edit_reason'] . ': ' . $row['modified_reason'];
}
// Get the stuff ready for the form.
$form_subject = $row['subject'];
$form_message = un_preparsecode($row['body']);
censorText($form_message);
censorText($form_subject);
// Check the boxes that should be checked.
$context['use_smileys'] = !empty($row['smileys_enabled']);
$context['icon'] = $row['icon'];
// Leave the approval checkbox unchecked by default for unapproved messages.
if (!$row['approved'] && !empty($context['show_approval']))
$context['show_approval'] = 1;
// Sort the attachments so they are in the order saved
$temp = array();
foreach ($attachment_stuff as $attachment)
{
if ($attachment['filesize'] >= 0 && !empty($modSettings['attachmentEnable']))
$temp[$attachment['id_attach']] = $attachment;
}
ksort($temp);
// Load up 'em attachments!
foreach ($temp as $attachment)
{
$context['current_attachments'][$attachment['id_attach']] = array(
'name' => $smcFunc['htmlspecialchars']($attachment['filename']),
'size' => $attachment['filesize'],
'attachID' => $attachment['id_attach'],
'approved' => $attachment['attachment_approved'],
'mime_type' => $attachment['mime_type'],
'thumb' => $attachment['id_thumb'],
);
}
// Allow moderators to change names....
if (allowedTo('moderate_forum') && empty($row['id_member']))
{
$context['name'] = $smcFunc['htmlspecialchars']($row['poster_name']);
$context['email'] = $smcFunc['htmlspecialchars']($row['poster_email']);
}
// Set the destination.
$context['destination'] = 'post2;start=' . $_REQUEST['start'] . ';msg=' . $_REQUEST['msg'] . ';' . $context['session_var'] . '=' . $context['session_id'] . (isset($_REQUEST['poll']) ? ';poll' : '');
$context['submit_label'] = $txt['save'];
}
// Posting...
else
{
// By default....
$context['use_smileys'] = true;
$context['icon'] = 'xx';
if ($user_info['is_guest'])
{
$context['name'] = isset($_SESSION['guest_name']) ? $_SESSION['guest_name'] : '';
$context['email'] = isset($_SESSION['guest_email']) ? $_SESSION['guest_email'] : '';
}
$context['destination'] = 'post2;start=' . $_REQUEST['start'] . (isset($_REQUEST['poll']) ? ';poll' : '');
$context['submit_label'] = $txt['post'];
// Posting a quoted reply?
if (!empty($topic) && !empty($_REQUEST['quote']))
{
// Make sure they _can_ quote this post, and if so get it.
$request = $smcFunc['db_query']('', '
SELECT m.subject, COALESCE(mem.real_name, m.poster_name) AS poster_name, m.poster_time, m.body
FROM {db_prefix}messages AS m
LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
WHERE {query_see_message_board}
AND m.id_msg = {int:id_msg}' . (!$modSettings['postmod_active'] || allowedTo('approve_posts') ? '' : '
AND m.approved = {int:is_approved}') . '
LIMIT 1',
array(
'id_msg' => (int) $_REQUEST['quote'],
'is_approved' => 1,
)
);
if ($smcFunc['db_num_rows']($request) == 0)
fatal_lang_error('quoted_post_deleted', false);
list ($form_subject, $mname, $mdate, $form_message) = $smcFunc['db_fetch_row']($request);
$smcFunc['db_free_result']($request);
// Add 'Re: ' to the front of the quoted subject.
if (trim($context['response_prefix']) != '' && $smcFunc['strpos']($form_subject, trim($context['response_prefix'])) !== 0)
$form_subject = $context['response_prefix'] . $form_subject;
// Censor the message and subject.
censorText($form_message);
censorText($form_subject);
// But if it's in HTML world, turn them into htmlspecialchar's so they can be edited!
if (strpos($form_message, '[html]') !== false)
{
$parts = preg_split('~(\[/code\]|\[code(?:=[^\]]+)?\])~i', $form_message, -1, PREG_SPLIT_DELIM_CAPTURE);
for ($i = 0, $n = count($parts); $i < $n; $i++)
{
// It goes 0 = outside, 1 = begin tag, 2 = inside, 3 = close tag, repeat.
if ($i % 4 == 0)
$parts[$i] = preg_replace_callback('~\[html\](.+?)\[/html\]~is', function($m)
{
return '[html]' . preg_replace('~<br\s?/?' . '>~i', '<br /><br>', "$m[1]") . '[/html]';
}, $parts[$i]);
}
$form_message = implode('', $parts);
}
$form_message = preg_replace('~<br ?/?' . '>~i', "\n", $form_message);
// Remove any nested quotes, if necessary.
if (!empty($modSettings['removeNestedQuotes']))
$form_message = preg_replace(array('~\n?\[quote.*?\].+?\[/quote\]\n?~is', '~^\n~', '~\[/quote\]~'), '', $form_message);
// Add a quote string on the front and end.
$form_message = '[quote author=' . $mname . ' link=msg=' . (int) $_REQUEST['quote'] . ' date=' . $mdate . ']' . "\n" . rtrim($form_message) . "\n" . '[/quote]';
}
// Posting a reply without a quote?
elseif (!empty($topic) && empty($_REQUEST['quote']))
{
// Get the first message's subject.
$form_subject = $first_subject;
// Add 'Re: ' to the front of the subject.
if (trim($context['response_prefix']) != '' && $form_subject != '' && $smcFunc['strpos']($form_subject, trim($context['response_prefix'])) !== 0)
$form_subject = $context['response_prefix'] . $form_subject;
// Censor the subject.
censorText($form_subject);
$form_message = '';
}
else
{
$form_subject = isset($_GET['subject']) ? $_GET['subject'] : '';
$form_message = '';
}
}
$context['can_post_attachment'] = !empty($modSettings['attachmentEnable']) && $modSettings['attachmentEnable'] == 1 && (allowedTo('post_attachment', $boards, true) || ($modSettings['postmod_active'] && allowedTo('post_unapproved_attachments', $boards, true)));
if ($context['can_post_attachment'])
{
// If there are attachments, calculate the total size and how many.
$context['attachments']['total_size'] = 0;
$context['attachments']['quantity'] = 0;
// If this isn't a new post, check the current attachments.
if (isset($_REQUEST['msg']))
{
$context['attachments']['quantity'] = count($context['current_attachments']);
foreach ($context['current_attachments'] as $attachment)
$context['attachments']['total_size'] += $attachment['size'];
}
// A bit of house keeping first.
if (!empty($_SESSION['temp_attachments']) && count($_SESSION['temp_attachments']) == 1)
unset($_SESSION['temp_attachments']);
if (!empty($_SESSION['temp_attachments']))
{
// Is this a request to delete them?
if (isset($_GET['delete_temp']))
{
foreach ($_SESSION['temp_attachments'] as $attachID => $attachment)
{
if (strpos($attachID, 'post_tmp_' . $user_info['id']) !== false)
if (file_exists($attachment['tmp_name']))
unlink($attachment['tmp_name']);
}
$post_errors[] = 'temp_attachments_gone';
$_SESSION['temp_attachments'] = array();
}
// Hmm, coming in fresh and there are files in session.
elseif ($context['current_action'] != 'post2' || !empty($_POST['from_qr']))
{
// Let's be nice and see if they belong here first.
if ((empty($_REQUEST['msg']) && empty($_SESSION['temp_attachments']['post']['msg']) && $_SESSION['temp_attachments']['post']['board'] == (!empty($board) ? $board : 0)) || (!empty($_REQUEST['msg']) && $_SESSION['temp_attachments']['post']['msg'] == $_REQUEST['msg']))
{
// See if any files still exist before showing the warning message and the files attached.
foreach ($_SESSION['temp_attachments'] as $attachID => $attachment)
{
if (strpos($attachID, 'post_tmp_' . $user_info['id']) === false)
continue;
if (file_exists($attachment['tmp_name']))
{
$post_errors[] = 'temp_attachments_new';
$context['files_in_session_warning'] = $txt['attached_files_in_session'];
unset($_SESSION['temp_attachments']['post']['files']);
break;
}
}
}
else
{
// Since, they don't belong here. Let's inform the user that they exist..
if (!empty($topic))
$delete_url = $scripturl . '?action=post' . (!empty($_REQUEST['msg']) ? (';msg=' . $_REQUEST['msg']) : '') . (!empty($_REQUEST['last_msg']) ? (';last_msg=' . $_REQUEST['last_msg']) : '') . ';topic=' . $topic . ';delete_temp';
else
$delete_url = $scripturl . '?action=post' . (!empty($board) ? ';board=' . $board : '') . ';delete_temp';
// Compile a list of the files to show the user.
$file_list = array();
foreach ($_SESSION['temp_attachments'] as $attachID => $attachment)
if (strpos($attachID, 'post_tmp_' . $user_info['id']) !== false)
$file_list[] = $attachment['name'];
$_SESSION['temp_attachments']['post']['files'] = $file_list;
$file_list = '<div class="attachments">' . implode('<br>', $file_list) . '</div>';
if (!empty($_SESSION['temp_attachments']['post']['msg']))
{
// We have a message id, so we can link back to the old topic they were trying to edit..
$goback_url = $scripturl . '?action=post' . (!empty($_SESSION['temp_attachments']['post']['msg']) ? (';msg=' . $_SESSION['temp_attachments']['post']['msg']) : '') . (!empty($_SESSION['temp_attachments']['post']['last_msg']) ? (';last_msg=' . $_SESSION['temp_attachments']['post']['last_msg']) : '') . ';topic=' . $_SESSION['temp_attachments']['post']['topic'] . ';additionalOptions';
$post_errors[] = array('temp_attachments_found', array($delete_url, $goback_url, $file_list));
$context['ignore_temp_attachments'] = true;
}
else
{
$post_errors[] = array('temp_attachments_lost', array($delete_url, $file_list));
$context['ignore_temp_attachments'] = true;
}
}
}
if (!empty($context['we_are_history']))
$post_errors[] = $context['we_are_history'];
foreach ($_SESSION['temp_attachments'] as $attachID => $attachment)
{
if (isset($context['ignore_temp_attachments']) || isset($_SESSION['temp_attachments']['post']['files']))
break;
if ($attachID != 'initial_error' && strpos($attachID, 'post_tmp_' . $user_info['id']) === false)
continue;
if ($attachID == 'initial_error')
{
$txt['error_attach_initial_error'] = $txt['attach_no_upload'] . '<div style="padding: 0 1em;">' . (is_array($attachment) ? vsprintf($txt[$attachment[0]], $attachment[1]) : $txt[$attachment]) . '</div>';
$post_errors[] = 'attach_initial_error';
unset($_SESSION['temp_attachments']);
break;
}