/
Driver.php
2277 lines (1996 loc) · 87 KB
/
Driver.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
/**
* Agora_Driver:: provides the functions to access both threads and
* individual messages.
*
* Copyright 2003-2017 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (GPL). If you
* did not receive this file, see http://www.horde.org/licenses/gpl.
*
* @author Marko Djukic <marko@oblo.com>
* @author Jan Schneider <jan@horde.org>
* @author Duck <duck@obala.net>
* @package Agora
*/
class Agora_Driver {
/**
* Charset
*
* @var string
*/
protected $_charset;
/**
* The database connection object.
*
* @var Horde_Db_Adapter
*/
protected $_db;
/**
* The forums scope.
*
* @var string
*/
protected $_scope;
/**
* Current forum data
*
* @var array
*/
public $_forum;
/**
* Current forum ID
*
* @var string
*/
public $_forum_id;
/**
* Scope theads table name
*
* @var string
*/
protected $_threads_table = 'agora_messages';
/**
* Scope theads table name
*
* @var string
*/
protected $_forums_table = 'agora_forums';
/**
* Cache object
*
* @var Horde_Cache
*/
protected $_cache;
/**
* Constructor
*/
public function __construct($scope, $params)
{
if (empty($params['db'])) {
throw new InvalidArgumentException('Missing required connection parameter(s).');
}
/* Set parameters. */
$this->_scope = $scope;
$this->_db = $params['db'];
$this->_charset = $params['charset'];
/* Initialize the Cache object. */
$this->_cache = $GLOBALS['injector']->getInstance('Horde_Cache');
}
/**
* Checks if attachments are allowed in messages for the current forum.
*
* @return boolean Whether attachments allowed or not.
*/
public function allowAttachments()
{
return ($GLOBALS['conf']['forums']['enable_attachments'] == '1' ||
($GLOBALS['conf']['forums']['enable_attachments'] == '0' &&
$this->_forum['forum_attachments']));
}
/**
* Saves the message.
*
* @param array $info Array containing all the message data to save.
*
* @return mixed Message ID on success or PEAR_Error on failure.
* @throws Agora_Exception
*/
public function saveMessage($info)
{
/* Check if the thread is locked before changing anything. */
if ($info['message_parent_id'] &&
$this->isThreadLocked($info['message_parent_id'])) {
return PEAR::raiseError(_("This thread has been locked."));
}
/* Check post permissions. */
if (!$this->hasPermission(Horde_Perms::EDIT)) {
return PEAR::raiseError(sprintf(_("You don't have permission to post messages in forum %s."), $this->_forum_id));
}
if (empty($info['message_id'])) {
/* Get thread parents */
// TODO message_thread is always parent root, probably can use it here.
if ($info['message_parent_id'] > 0) {
$parents = $this->_db->selectValue('SELECT parents FROM ' . $this->_threads_table . ' WHERE message_id = ?',
array($info['message_parent_id']));
$info['parents'] = $parents . ':' . $info['message_parent_id'];
$info['message_thread'] = $this->getThreadRoot($info['message_parent_id']);
} else {
$info['parents'] = '';
$info['message_thread'] = 0;
}
/* Create new message */
$sql = 'INSERT INTO ' . $this->_threads_table
. ' (forum_id, message_thread, parents, '
. 'message_author, message_subject, body, attachments, '
. 'message_timestamp, message_modifystamp, ip) '
. ' VALUES (?, ?, ?, ?, ?, ?, 0, ?, ?, ?)';
$author = $GLOBALS['registry']->getAuth() ? $GLOBALS['registry']->getAuth() : $info['posted_by'];
$values = array($this->_forum_id,
$info['message_thread'],
$info['parents'],
$author,
$this->convertToDriver($info['message_subject']),
$this->convertToDriver($info['message_body']),
$_SERVER['REQUEST_TIME'],
$_SERVER['REQUEST_TIME'],
$_SERVER['REMOTE_ADDR']);
try {
$info['message_id'] = $this->_db->insert($sql, $values);
} catch (Horde_Db_Exception $e) {
throw new Agora_Exception($e->getMessage());
}
/* Update last message in forum, but only if it is not moderated */
if (!$this->_forum['forum_moderated']) {
// Send the new post to the distribution address
if ($this->_forum['forum_distribution_address']) {
Agora::distribute($info['message_id']);
}
/* Update cached message/thread counts and last poster */
$this->_lastInForum($this->_forum_id, $info['message_id'], $author, $_SERVER['REQUEST_TIME']);
$this->_forumSequence($this->_forum_id, 'message', '+');
if ($info['message_thread']) {
$this->_sequence($info['message_thread'], '+');
$this->_lastInThread($info['message_thread'], $info['message_id'], $author, $_SERVER['REQUEST_TIME']);
} else {
$this->_forumSequence($this->_forum_id, 'thread', '+');
}
}
} else {
// TODO clearing cache for editing doesn't work
/* Update message data */
$sql = 'UPDATE ' . $this->_threads_table . ' SET ' .
'message_subject = ?, body = ?, message_modifystamp = ? WHERE message_id = ?';
$values = array($this->convertToDriver($info['message_subject']),
$this->convertToDriver($info['message_body']),
$_SERVER['REQUEST_TIME'],
$info['message_id']);
try {
$this->_db->execute($sql, $values);
} catch (Horde_Db_Exception $e) {
throw new Agora_Exception($e->getMessage());
}
/* Get message thread for cache expiration */
$info['message_thread'] = $this->getThreadRoot($info['message_id']);
}
/* Handle attachment saves or deletions. */
if (!empty($info['message_attachment']) ||
!empty($info['attachment_delete'])) {
$vfs = Agora::getVFS();
if ($vfs instanceof PEAR_Error) {
return $vfs;
}
$vfs_dir = Agora::VFS_PATH . $this->_forum_id . '/' . $info['message_id'];
/* Check if delete requested or new attachment loaded, and delete
* any existing one. */
if (!empty($info['attachment_delete'])) {
$sql = 'SELECT file_id FROM agore_files WHERE message_id = ?';
foreach ($this->_db->selectValues($sql, array($info['message_id'])) as $file_id) {
if ($vfs->exists($vfs_dir, $file_id)) {
$delete = $vfs->deleteFile($vfs_dir, $file_id);
if ($delete instanceof PEAR_Error) {
return $delete;
}
}
}
try {
$this->_db->execute('DELETE FROM agore_files WHERE message_id = ?', array($info['message_id']));
} catch (Horde_Db_Exception $e) {
throw new Agora_Exception($e->getMessage());
}
$attachments = 0;
}
/* Save new attachment information. */
if (!empty($info['message_attachment'])) {
$file_sql = 'INSERT INTO agora_files (file_name, file_type, file_size, message_id) VALUES (?, ?, ?, ?)';
$file_data = array($info['message_attachment']['name'],
$info['message_attachment']['type'],
$info['message_attachment']['size'],
$info['message_id']);
try {
$file_id = $this->_db->insert($file_sql, $file_data);
} catch (Horde_Db_Exception $e) {
throw new Agora_Exception($e->getMessage());
}
$result = $vfs->write($vfs_dir, $file_id, $info['message_attachment']['file'], true);
if ($result instanceof PEAR_Error) {
return $result;
}
$attachments = 1;
}
$sql = 'UPDATE ' . $this->_threads_table . ' SET attachments = ? WHERE message_id = ?';
try {
$this->_db->execute($sql, array($attachments, $info['message_id']));
} catch (Horde_Db_Exception $e) {
throw new Agora_Exception($e->getMessage());
}
}
/* Update cache */
$this->_updateCacheState($info['message_thread']);
return $info['message_id'];
}
/**
* Moves a thread to another forum.
*
* @todo Update the number of messages in the old/new forum
*
* @param integer $thread_id The ID of the thread to move.
* @param integer $forum_id The ID of the destination forum.
*
* @throws Agora_Exception
*/
public function moveThread($thread_id, $forum_id)
{
$sql = 'SELECT forum_id FROM ' . $this->_threads_table . ' WHERE message_id = ?';
try {
$old_forum = $this->_db->selectValue($sql, array($thread_id));
} catch (Horde_Db_Exception $e) {
throw new Agora_Exception($e->getMessage());
}
$sql = 'UPDATE ' . $this->_threads_table . ' SET forum_id = ? WHERE message_thread = ? OR message_id = ?';
try {
$this->_db->execute($sql, array($forum_id, $thread_id, $thread_id));
} catch (Horde_Db_Exception $e) {
throw new Agora_Exception($e->getMessage());
}
$this->_forumSequence($old_forum, 'thread', '-');
$this->_forumSequence($forum_id, 'thread', '+');
/* Update last message */
$this->_lastInForum($old_forum);
$this->_lastInForum($forum_id);
/* Update cache */
$this->_updateCacheState($thread_id);
return true;
}
/**
* Splits a thread on message id.
*
* @param integer $message_id The ID of the message to split at.
*
* @throws Agora_Exception
*/
public function splitThread($message_id)
{
$sql = 'SELECT message_thread FROM ' . $this->_threads_table . ' WHERE message_id = ?';
try {
$thread_id = $this->_db->selectValue($sql, array($message_id));
} catch (Horde_Db_Exception $e) {
throw new Agora_Exception($e->getMessage());
}
$sql = 'UPDATE ' . $this->_threads_table . ' SET message_thread = ?, parents = ? WHERE message_id = ?';
try {
$this->_db->execute($sql, array(0, '', $message_id));
} catch (Horde_Db_Exception $e) {
throw new Agora_Exception($e->getMessage());
}
$sql = 'SELECT message_thread, parents, message_id FROM ' . $this->_threads_table . ' WHERE parents LIKE ?';
try {
$children = $this->_db->select($sql, array(":$thread_id:%$message_id%"));
} catch (Horde_Db_Exception $e) {
throw new Agora_Exception($e->getMessage());
}
if (!empty($children)) {
$pos = strpos($children[0]['parents'], ':' . $message_id);
foreach ($children as $i => $message) {
$children[$i]['message_thread'] = (int)$message_id;
$children[$i]['parents'] = substr($message['parents'], $pos);
}
$sql = 'UPDATE ' . $this->_threads_table . ' SET message_thread = ?, parents = ? WHERE message_id = ?';
try {
$this->_db->execute($sql, $children);
} catch (Horde_Db_Exception $e) {
throw new Agora_Exception($e->getMessage());
}
}
/* Update count on old thread */
$count = $this->countThreads($thread_id);
$sql = 'UPDATE ' . $this->_threads_table . ' SET message_seq = ? WHERE message_id = ?';
try {
$this->_db->execute($sql, array($count, $thread_id));
} catch (Horde_Db_Exception $e) {
throw new Agora_Exception($e->getMessage());
}
/* Update count on new thread */
$count = $this->countThreads($message_id);
$sql = 'UPDATE ' . $this->_threads_table . ' SET message_seq = ? WHERE message_id = ?';
try {
$this->_db->execute($sql, array($count, $message_id));
} catch (Horde_Db_Exception $e) {
throw new Agora_Exception($e->getMessage());
}
/* Update last message */
$this->_lastInForum($this->_forum_id);
$this->_lastInThread($thread_id);
$this->_lastInThread($message_id);
$this->_forumSequence($this->_forum_id, 'thread', '+');
/* Update cache */
$this->_updateCacheState($thread_id);
}
/**
* Merges two threads.
*
* @param integer $thread_id The ID of the thread to merge.
* @param integer $message_id The ID of the message to merge to.
*
* @throws Agora_Exception
*/
public function mergeThread($thread_from, $message_id)
{
$sql = 'SELECT message_thread, parents FROM ' . $this->_threads_table . ' WHERE message_id = ?';
try {
$destination = $this->_db->selectOne($sql, array($message_id));
} catch (Horde_Db_Exception $e) {
throw new Agora_Exception($e->getMessage());
}
/* Merge to the top level */
if ($destination['message_thread'] == 0) {
$destination['message_thread'] = $message_id;
}
$sql = 'SELECT message_thread, parents, message_id FROM ' . $this->_threads_table . ' WHERE message_id = ? OR message_thread = ?';
try {
$children = $this->_db->select($sql, array($thread_from, $thread_from));
} catch (Horde_Db_Exception $e) {
throw new Agora_Exception($e->getMessage());
}
/* TODO: merging more than one message breaks parent/child relations,
* also merging to deeper level than thread root doesn't work. */
if (!empty($children)) {
$sql = 'UPDATE ' . $this->_threads_table . ' SET message_thread = ?, parents = ? WHERE message_id = ?';
foreach ($children as $i => $message) {
$children[$i]['message_thread'] = $destination['message_thread'];
if (!empty($destination['parents'])) {
$children[$i]['parents'] = $destination['parents'] . $message['parents'];
} else {
$children[$i]['parents'] = ':' . $message_id;
}
try {
$this->_db->execute($sql, $children[$i]);
} catch (Horde_Db_Exception $e) {
throw new Agora_Exception($e->getMessage());
}
}
}
$count = $this->countThreads($destination['message_thread']);
$sql = 'UPDATE ' . $this->_threads_table . ' SET message_seq = ? WHERE message_id = ?';
try {
$this->_db->execute($sql, array($count, $destination['message_thread']));
} catch (Horde_Db_Exception $e) {
throw new Agora_Exception($e->getMessage());
}
/* Update last message */
$this->_lastInForum($this->_forum_id);
$this->_lastInThread($destination['message_thread']);
$this->_forumSequence($this->_forum_id, 'thread', '-');
/* Update cache */
$this->_updateCacheState($destination['message_thread']);
}
/**
* Fetches a message.
*
* @param integer $message_id The ID of the message to fetch.
*
* @throws Horde_Exception_NotFound
* @throws Agora_Exception
*/
public function getMessage($message_id)
{
$message = $this->_cache->get('agora_msg' . $message_id, $GLOBALS['conf']['cache']['default_lifetime']);
if ($message) {
return unserialize($message);
}
$sql = 'SELECT message_id, forum_id, message_thread, parents, '
. 'message_author, message_subject, body, message_seq, '
. 'message_timestamp, view_count, locked, attachments FROM '
. $this->_threads_table . ' WHERE message_id = ?';
try {
$message = $this->_db->selectOne($sql, array($message_id));
} catch (Horde_Db_Exception $e) {
throw new Agora_Exception($e->getMessage());
}
if (empty($message)) {
throw new Horde_Exception_NotFound(sprintf(_("Message ID \"%d\" not found"), $message_id));
}
$message['message_subject'] = $this->convertFromDriver($message['message_subject']);
$message['body'] = $this->convertFromDriver($message['body']);
if ($message['message_thread'] == 0) {
$message['message_thread'] = $message_id;
}
/* Is author a moderator? */
if (isset($this->_forum['moderators']) &&
in_array($message['message_author'], $this->_forum['moderators'])) {
$message['message_author_moderator'] = 1;
}
$this->_cache->set('agora_msg' . $message_id, serialize($message));
return $message;
}
/**
* Returns a hash with all information necessary to reply to a message.
*
* @param mixed $message The ID of the parent message to reply to, or arry of its data.
*
* @return array A hash with all relevant information.
* @throws Horde_Exception_NotFound
* @throws Agora_Exception
*/
public function replyMessage($message)
{
if (!is_array($message)) {
$message = $this->getMessage($message);
}
/* Set up the form subject with the parent subject. */
if (Horde_String::lower(Horde_String::substr($message['message_subject'], 0, 3)) != 're:') {
$message['message_subject'] = 'Re: ' . $message['message_subject'];
}
/* Prepare the message quite body . */
$message['body'] = sprintf(_("Posted by %s on %s"),
htmlspecialchars($message['message_author']),
strftime($GLOBALS['prefs']->getValue('date_format'), $message['message_timestamp']))
. "\n-------------------------------------------------------\n"
. $message['body'];
$message['body'] = "\n> " . Horde_String::wrap($message['body'], 60, "\n> ");
return $message;
}
/**
* Deletes a message and all replies.
*
* @todo Detele all related attachments from VFS.
*
* @param integer $message_id The ID of the message to delete.
*
* @return string Thread ID on success.
* @throws Agora_Exception
*/
public function deleteMessage($message_id)
{
/* Check delete permissions. */
if (!$this->hasPermission(Horde_Perms::DELETE)) {
return PEAR::raiseError(sprintf(_("You don't have permission to delete messages in forum %s."), $this->_forum_id));
}
$sql = 'SELECT message_thread FROM ' . $this->_threads_table . ' WHERE message_id = ?';
try {
$thread_id = $this->_db->selectValue($sql, array($message_id));
} catch (Horde_Db_Exception $e) {
throw new Agora_Exception($e->getMessage());
}
$sql = 'DELETE FROM ' . $this->_threads_table . ' WHERE message_id = ' . (int)$message_id;
if ($thread_id == 0) {
$sql .= ' OR message_thread = ' . (int)$message_id;
}
try {
$this->_db->execute($sql);
} catch (Horde_Db_Exception $e) {
throw new Agora_Exception($e->getMessage());
}
/* Update counts */
// TODO message count is not correctly decreased after deleting more than one message.
$this->_forumSequence($this->_forum_id, 'message', '-');
if ($thread_id) {
$this->_sequence($thread_id, '-');
} else {
$this->_forumSequence($this->_forum_id, 'thread', '-');
}
$this->_lastInForum($this->_forum_id);
$this->_lastInThread($thread_id);
/* Update cache */
$this->_updateCacheState($thread_id);
return $thread_id;
}
/**
* Update lastMessage in a Forum
*
* @param integer $forum_id Forum to update
* @param integer $message_id Last message id
* @param string $message_author Last message author
* @param integer $message_timestamp Last message timestamp
*
* @throws Agora_Exception
*/
private function _lastInForum($forum_id, $message_id = 0, $message_author = '', $message_timestamp = 0)
{
/* Get the last message in form or thread - when managing threads */
if ($message_id == 0) {
$sql = $this->_db->addLimitOffset('SELECT message_id, message_author, message_timestamp FROM ' . $this->_threads_table
. ' WHERE forum_id = ' . (int)$forum_id . ' ORDER BY message_id DESC', array('limit' => 1));
try {
$last = $this->_db->selectOne($sql);
} catch (Horde_Db_Execution $e) {
throw new Agora_Exception($e->getMessage());
}
if (!empty($last)) {
extract($last);
}
}
$sql = 'UPDATE ' . $this->_forums_table
. ' SET last_message_id = ?, last_message_author = ?, last_message_timestamp = ? WHERE forum_id = ?';
$values = array($message_id, $message_author, $message_timestamp, $forum_id);
try {
$this->_db->execute($sql, $values);
} catch (Horde_Db_Execution $e) {
throw new Agora_Exception($e->getMessage());
}
$this->_cache->expire('agora_forum_' . $forum_id, $GLOBALS['conf']['cache']['default_lifetime']);
}
/**
* Update lastMessage in Thread
*
* @param integer $thread_id Thread to update
* @param integer $message_id Last message id
* @param string $message_author Last message author
* @param integer $message_timestamp Last message timestamp
*
* @throws Agora_Exception
*/
private function _lastInThread($thread_id, $message_id = 0, $message_author = '', $message_timestamp = 0)
{
/* Get the last message in form or thread - when managing threads */
if ($message_id == 0) {
$sql = $this->_db->addLimitOffset('SELECT message_id, message_author, message_timestamp FROM ' . $this->_threads_table
. ' WHERE message_thread = ' . (int)$thread_id . ' ORDER BY message_id DESC', array('limit' => 1));
try {
$last = $this->_db->selectOne($sql);
} catch (Horde_Db_Execution $e) {
throw new Agora_Exception($e->getMessage());
}
if (!empty($last)) {
extract($last);
}
}
$sql = 'UPDATE ' . $this->_threads_table
. ' SET last_message_id = ?, last_message_author = ?, message_modifystamp = ? WHERE message_id = ?';
$values = array($message_id, $message_author, $message_timestamp, $thread_id);
try {
$this->_db->execute($sql, $values);
} catch (Horde_Db_Execution $e) {
throw new Agora_Exception($e->getMessage());
}
}
/**
* Increments or decrements a forum's message count.
*
* @param integer $forum_id Forum to update
* @param string $type What to increment message, thread or view.
* @param integer|string $diff Incremental or decremental step, either a
* positive or negative integer, or a plus or
* minus sign.
*/
public function _forumSequence($forum_id, $type = 'message', $diff = '+')
{
$t = $type . '_count';
$sql = 'UPDATE ' . $this->_forums_table . ' SET ' . $t . ' = ';
switch ($diff) {
case '+':
case '-':
$sql .= $t . ' ' . $diff . ' 1';
break;
default:
$sql .= (int)$diff;
break;
}
$sql .= ' WHERE forum_id = ' . (int)$forum_id;
// TODO do we really need this return?
return $this->_db->execute($sql);
}
/**
* Increments or decrements a thread's message count.
*
* @param integer $thread_id Thread to update.
* @param integer|string $diff Incremental or decremental step, either a
* positive or negative integer, or a plus or
* minus sign.
*/
private function _sequence($thread_id, $diff = '+')
{
$sql = 'UPDATE ' . $this->_threads_table . ' SET message_seq = ';
switch ($diff) {
case '+':
case '-':
$sql .= 'message_seq ' . $diff . ' 1';
break;
default:
$sql .= (int)$diff;
break;
}
$sql .= ', message_modifystamp = ' . $_SERVER['REQUEST_TIME'] . ' WHERE message_id = ' . (int)$thread_id;
// TODO do we really need this return?
return $this->_db->execute($sql);
}
/**
* Deletes an entire message thread.
*
* @param integer $thread_id The ID of the thread to delete. If not
* specified will delete all the threads for the
* current forum.
*
* @throws Agora_Exception
*/
public function deleteThread($thread_id = 0)
{
/* Check delete permissions. */
if (!$this->hasPermission(Horde_Perms::DELETE)) {
return PEAR::raiseError(sprintf(_("You don't have permission to delete messages in forum %s."), $this->_forum_id));
}
if ($thread_id > 0) {
$sql = 'DELETE FROM ' . $this->_threads_table . ' WHERE message_thread = ' . (int)$thread_id;
try {
$this->_db->execute($sql);
} catch (Horde_Db_Exception $e) {
throw new Agora_Exception($e->getMessage());
}
$sql = 'SELECT COUNT(*) FROM ' . $this->_threads_table . ' WHERE forum_id = ' . (int)$this->_forum_id;
$messages = $this->_db->selectValue($sql);
$this->_forumSequence($this->_forum_id, 'thread', '-');
$this->_forumSequence($this->_forum_id, 'message', $messages);
/* Update cache */
$this->_updateCacheState($thread_id);
} else {
$sql = 'DELETE FROM ' . $this->_threads_table . ' WHERE forum_id = ' . (int)$this->_forum_id;
try {
$this->_db->execute($sql);
} catch (Horde_Db_Exception $e) {
throw new Agora_Exception($e->getMessage());
}
$this->_forumSequence($this->_forum_id, 'thread', '0');
$this->_forumSequence($this->_forum_id, 'message', '0');
}
/* Update last message */
$this->_lastInForum($this->_forum_id);
return true;
}
/**
* Returns a list of threads.
*
* @param integer $thread_root Message at which to start the thread.
* If null get all forum threads
* @param boolean $all_levels Show all child levels or just one level.
* @param string $sort_by The column by which to sort.
* @param integer $sort_dir The direction by which to sort:
* 0 - ascending
* 1 - descending
* @param boolean $message_view
* @param string $link_back A url to pass to the reply script which
* will be returned to after an insertion of
* a post. Useful in cases when this thread
* view is used in blocks to return to the
* original page rather than to Agora.
* @param string $base_url An alternative URL where edit/delete links
* point to. Mainly for api usage. Takes "%p"
* as a placeholder for the parent message ID.
* @param string $from The thread to start listing at.
* @param string $count The number of threads to return.
* @param boolean $nofollow Whether to set the 'rel="nofollow"'
* attribute on linked URLs in the messages.
*/
public function getThreads($thread_root = 0,
$all_levels = false,
$sort_by = 'message_timestamp',
$sort_dir = 0,
$message_view = false,
$link_back = '',
$base_url = null,
$from = null,
$count = null,
$nofollow = false)
{
/* Check read permissions */
if (!$this->hasPermission(Horde_Perms::SHOW)) {
return PEAR::raiseError(sprintf(_("You don't have permission to read messages in forum %s."), $this->_forum_id));
}
/* Get messages data */
$messages = $this->_getThreads($thread_root, $all_levels, $sort_by, $sort_dir, $message_view, $from, $count);
if ($messages instanceof PEAR_Error || empty($messages)) {
return $messages;
}
/* Moderators */
if (isset($this->_forum['moderators'])) {
$moderators = array_flip($this->_forum['moderators']);
}
/* Set up the base urls for actions. */
$view_url = Horde::url('messages/index.php');
if ($base_url) {
$edit_url = $base_url;
$del_url = $base_url->copy()->add('delete', 'true');
} else {
$edit_url = Horde::url('messages/edit.php');
$del_url = Horde::url('messages/delete.php');
}
// Get needed prefs
$per_page = $GLOBALS['prefs']->getValue('thread_per_page');
$view_bodies = $GLOBALS['prefs']->getValue('thread_view_bodies');
$abuse_url = Horde::url('messages/abuse.php');
$hot_img = Horde::img('hot.png', _("Hot thread"), array('title' => _("Hot thread")));
$new_img = Horde::img('required.png', _("New posts"), array('title' => _("New posts")));
$is_moderator = $this->hasPermission(Horde_Perms::DELETE);
/* Loop through the threads and set up the array. */
foreach ($messages as &$message) {
/* Add attachment link */
if ($message['attachments']) {
$message['message_attachment'] = $this->getAttachmentLink($message['message_id']);
}
/* Get last message link */
if ($thread_root == 0 && $message['last_message_id'] > 0) {
$url = Agora::setAgoraId($message['forum_id'], $message['last_message_id'], $view_url, $this->_scope);
$message['message_url'] = Horde::link($url);
$last_timestamp = $message['last_message_timestamp'];
} else {
$last_timestamp = $message['message_timestamp'];
}
/* Check if thread is hot */
if ($this->isHot($message['view_count'], $last_timestamp)) {
$message['hot'] = $hot_img;
}
/* Check if has new posts since user last visit */
if ($thread_root == 0 && $this->isNew($message['message_id'], $last_timestamp)) {
$message['new'] = $new_img;
}
/* Mark moderators */
if (isset($this->_forum['moderators']) && array_key_exists($message['message_author'], $moderators)) {
$message['message_author_moderator'] = 1;
}
/* Link to view the message. */
$url = Agora::setAgoraId($message['forum_id'], $message['message_id'], $view_url, $this->_scope);
$message['link'] = Horde::link($url, $message['message_subject'], '', '', '', $message['message_subject']);
/* Set up indenting for threads. */
if ($sort_by != 'message_thread') {
unset($message['indent'], $message['parent']);
/* Links to pages */
if ($thread_root == 0 && $message['message_seq'] > $per_page && $view_bodies == 2) {
$sub_pages = $message['message_seq'] / $per_page;
for ($i = 0; $i < $sub_pages; $i++) {
$page_title = sprintf(_("Page %d"), $i+1);
$message['pages'][] = Horde::link($url->add('thread_page', $i), $page_title, '', '', '', $page_title) . ($i+1) . '</a>';
}
}
}
/* Button to post a reply to the message. */
if (!$message['locked']) {
if ($base_url) {
$url = $base_url;
if (strpos($url, '%p') !== false) {
$url = str_replace('%p', $message['message_id'], $url);
} else {
$url->add('message_parent_id', $message['message_id']);
}
if (!empty($link_back)) {
$url->add('url', $link_back);
}
} else {
$url = Agora::setAgoraId($message['forum_id'], $message['message_id'], $view_url, $this->_scope);
}
$url->add('reply_focus', 1) . '#messageform';
$message['reply'] = Horde::link($url, _("Reply to message"), '', '', '', _("Reply to message")) . _("Reply") . '</a>';
}
/* Link to edit the message. */
if ($thread_root > 0 && isset($this->_forum['moderators'])) {
$url = Agora::setAgoraId($message['forum_id'], $message['message_id'], $abuse_url);
$message['actions'][] = Horde::link($url, _("Report as abuse")) . _("Report as abuse") . '</a>';
}
if ($is_moderator) {
/* Link to edit the message. */
$url = Agora::setAgoraId($message['forum_id'], $message['message_id'], $edit_url, $this->_scope);
$message['actions'][] = Horde::link($url, _("Edit"), '', '', '', _("Edit message")) . _("Edit") . '</a>';
/* Link to delete the message. */
$url = Agora::setAgoraId($message['forum_id'], $message['message_id'], $del_url, $this->_scope);
$message['actions'][] = Horde::link($url, _("Delete"), '', '', '', _("Delete message")) . _("Delete") . '</a>';
/* Link to lock/unlock the message. */
$url = Agora::setAgoraId($this->_forum_id, $message['message_id'], Horde::url('messages/lock.php'), $this->_scope);
$label = ($message['locked']) ? _("Unlock") : _("Lock");
$message['actions'][] = Horde::link($url, $label, '', '', '', $label) . $label . '</a>';
/* Link to move thread to another forum. */
if ($this->_scope == 'agora') {
if ($message['message_thread'] == $message['message_id']) {
$url = Agora::setAgoraId($this->_forum_id, $message['message_id'], Horde::url('messages/move.php'), $this->_scope);
$message['actions'][] = Horde::link($url, _("Move"), '', '', '', _("Move")) . _("Move") . '</a>';
/* Link to merge a message thred with anoter thread. */
$url = Agora::setAgoraId($this->_forum_id, $message['message_id'], Horde::url('messages/merge.php'), $this->_scope);
$message['actions'][] = Horde::link($url, _("Merge"), '', '', '', _("Merge")) . _("Merge") . '</a>';
} elseif ($message['message_thread'] != 0) {
/* Link to split thread to two threads, from this message after. */
$url = Agora::setAgoraId($this->_forum_id, $message['message_id'], Horde::url('messages/split.php'), $this->_scope);
$message['actions'][] = Horde::link($url, _("Split"), '', '', '', _("Split")) . _("Split") . '</a>';
}
}
}
}
return $messages;
}
/**
* Formats a message body.
*
* @param array|Traversable $messages Messages to format
* @param string $sort_by List format order
* @param boolean $format Format messages body
* @param integer $thread_root Thread root
*/
protected function _formatThreads($messages, $sort_by = 'message_modifystamp',
$format = false, $thread_root = 0)
{
/* Loop through the threads and set up the array. */
foreach ($messages as &$message) {
$message['message_author'] = htmlspecialchars($message['message_author']);
$message['message_subject'] = htmlspecialchars($this->convertFromDriver($message['message_subject']));
$message['message_date'] = $this->dateFormat($message['message_timestamp']);
if ($format) {
$message['body'] = $this->formatBody($this->convertFromDriver($message['body']));
}
/* If we are on the top, thread id is message itself. */
if ($message['message_thread'] == 0) {
$message['message_thread'] = $message['message_id'];
}
/* Get last message. */
if ($thread_root == 0 && $message['last_message_id'] > 0) {
$message['last_message_date'] = $this->dateFormat($message['last_message_timestamp']);
}
/* Set up indenting for threads. */
if ($sort_by == 'message_thread') {
$indent = explode(':', $message['parents']);
$message['indent'] = count($indent) - 1;
$last = array_pop($indent);
/* TODO: this won't work because array_search doesn't search in
* multi-dimensional arrays.
*
* From what I see this is only needed because there is a bug in message
* deletion anyway. Parents should always be in array, because when
* deleting we should delete all sub-messages. We even state this in GUI,
* but we actually don't do it so.
*
/*if (array_search($last, $messages) != 'message_id') {
$message['indent'] = 1;
$last = null;
}*/
$message['parent'] = $last ? $last : null;
}
}
return $messages;
}
/**
* Formats a message body.
*
* @param string $body Text to format.
*/
public function formatBody($body)