Merge pull request #132 from WoltLab/flood-control
[GitHub/WoltLab/com.woltlab.wcf.conversation.git] / files / lib / system / conversation / ConversationHandler.class.php
1 <?php
2 namespace wcf\system\conversation;
3 use wcf\system\database\util\PreparedStatementConditionBuilder;
4 use wcf\system\exception\NamedUserException;
5 use wcf\system\exception\PermissionDeniedException;
6 use wcf\system\user\storage\UserStorageHandler;
7 use wcf\system\SingletonFactory;
8 use wcf\system\WCF;
9
10 /**
11 * Handles the number of conversations and unread conversations of the active user.
12 *
13 * @author Marcel Werk
14 * @copyright 2001-2017 WoltLab GmbH
15 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
16 * @package WoltLabSuite\Core\System\Conversation
17 */
18 class ConversationHandler extends SingletonFactory {
19 /**
20 * number of unread conversations
21 * @var integer[]
22 */
23 protected $unreadConversationCount = [];
24
25 /**
26 * number of conversations
27 * @var integer[]
28 */
29 protected $conversationCount = [];
30
31 /**
32 * Returns the number of unread conversations for given user.
33 *
34 * @param integer $userID
35 * @param boolean $skipCache
36 * @return integer
37 */
38 public function getUnreadConversationCount($userID = null, $skipCache = false) {
39 if ($userID === null) $userID = WCF::getUser()->userID;
40
41 if (!isset($this->unreadConversationCount[$userID]) || $skipCache) {
42 $this->unreadConversationCount[$userID] = 0;
43
44 // load storage data
45 UserStorageHandler::getInstance()->loadStorage([$userID]);
46
47 // get ids
48 $data = UserStorageHandler::getInstance()->getStorage([$userID], 'unreadConversationCount');
49
50 // cache does not exist or is outdated
51 if ($data[$userID] === null || $skipCache) {
52 $conditionBuilder = new PreparedStatementConditionBuilder();
53 $conditionBuilder->add('conversation.conversationID = conversation_to_user.conversationID');
54 $conditionBuilder->add('conversation_to_user.participantID = ?', [$userID]);
55 $conditionBuilder->add('conversation_to_user.hideConversation = 0');
56 $conditionBuilder->add('conversation_to_user.lastVisitTime < conversation.lastPostTime');
57
58 $sql = "SELECT COUNT(*) AS count
59 FROM wcf".WCF_N."_conversation_to_user conversation_to_user,
60 wcf".WCF_N."_conversation conversation
61 ".$conditionBuilder->__toString();
62 $statement = WCF::getDB()->prepareStatement($sql);
63 $statement->execute($conditionBuilder->getParameters());
64 $row = $statement->fetchArray();
65 $this->unreadConversationCount[$userID] = $row['count'];
66
67 // update storage data
68 UserStorageHandler::getInstance()->update($userID, 'unreadConversationCount', serialize($this->unreadConversationCount[$userID]));
69 }
70 else {
71 $this->unreadConversationCount[$userID] = unserialize($data[$userID]);
72 }
73 }
74
75 return $this->unreadConversationCount[$userID];
76 }
77
78 /**
79 * Returns the number of conversations for given user.
80 *
81 * @param integer $userID
82 * @return integer
83 */
84 public function getConversationCount($userID = null) {
85 if ($userID === null) $userID = WCF::getUser()->userID;
86
87 if (!isset($this->conversationCount[$userID])) {
88 $this->conversationCount[$userID] = 0;
89
90 // load storage data
91 UserStorageHandler::getInstance()->loadStorage([$userID]);
92
93 // get ids
94 $data = UserStorageHandler::getInstance()->getStorage([$userID], 'conversationCount');
95
96 // cache does not exist or is outdated
97 if ($data[$userID] === null) {
98 $conditionBuilder1 = new PreparedStatementConditionBuilder();
99 $conditionBuilder1->add('conversation_to_user.participantID = ?', [$userID]);
100 $conditionBuilder1->add('conversation_to_user.hideConversation IN (0,1)');
101 $conditionBuilder2 = new PreparedStatementConditionBuilder();
102 $conditionBuilder2->add('conversation.userID = ?', [$userID]);
103 $conditionBuilder2->add('conversation.isDraft = 1');
104
105 $sql = "SELECT (SELECT COUNT(*)
106 FROM wcf".WCF_N."_conversation_to_user conversation_to_user
107 ".$conditionBuilder1->__toString().")
108 +
109 (SELECT COUNT(*)
110 FROM wcf".WCF_N."_conversation conversation
111 ".$conditionBuilder2->__toString().") AS count";
112 $statement = WCF::getDB()->prepareStatement($sql);
113 $statement->execute(array_merge($conditionBuilder1->getParameters(), $conditionBuilder2->getParameters()));
114 $row = $statement->fetchArray();
115 $this->conversationCount[$userID] = $row['count'];
116
117 // update storage data
118 UserStorageHandler::getInstance()->update($userID, 'conversationCount', serialize($this->conversationCount[$userID]));
119 }
120 else {
121 $this->conversationCount[$userID] = unserialize($data[$userID]);
122 }
123 }
124
125 return $this->conversationCount[$userID];
126 }
127
128 /**
129 * Enforces the flood control.
130 */
131 public function enforceFloodControl() {
132 $limit = WCF::getSession()->getPermission('user.conversation.maxStartedConversationsPer24Hours');
133 if ($limit == -1) {
134 return;
135 }
136 else if ($limit == 0) {
137 // `0` is not a valid value, but the interface logic does not permit and exclusion
138 // while also allowing the special value `-1`. Therefore, `0` behaves like the
139 // 'canStartConversation' permission added in WoltLab Suite 5.2.
140 throw new PermissionDeniedException();
141 }
142
143 $sql = "SELECT COUNT(*) AS count, MIN(time) AS oldestDate
144 FROM wcf" . WCF_N . "_conversation
145 WHERE userID = ?
146 AND time > ?";
147 $statement = WCF::getDB()->prepareStatement($sql);
148 $statement->execute([
149 WCF::getUser()->userID,
150 TIME_NOW - 86400,
151 ]);
152 $row = $statement->fetchSingleRow();
153
154 if ($row['count'] >= $limit) {
155 throw new NamedUserException(WCF::getLanguage()->getDynamicVariable('wcf.conversation.error.floodControl', [
156 'limit' => $limit,
157 'notBefore' => $row['oldestDate'] + 86400,
158 ]));
159 }
160 }
161 }