1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* Privatemsg Quota module
|
6 |
|
|
*/
|
7 |
|
|
|
8 |
|
|
/**
|
9 |
|
|
* Implements hook_permission()-
|
10 |
|
|
*/
|
11 |
|
|
function privatemsg_limits_permission() {
|
12 |
|
|
return array(
|
13 |
|
|
'bypass recipient message limit' => array(
|
14 |
|
|
'title' => t('Bypass recipient message limit'),
|
15 |
|
|
'description' => t("Enables a user to send a message to a recipient even when the recipient's message/conversation maximum limit has already been reached. Without this permission, the message would ordinarily be blocked.")
|
16 |
|
|
)
|
17 |
|
|
);
|
18 |
|
|
}
|
19 |
|
|
|
20 |
|
|
/**
|
21 |
|
|
* Implements hook_menu().
|
22 |
|
|
*/
|
23 |
|
|
function privatemsg_limits_menu() {
|
24 |
|
|
$items['admin/config/messaging/privatemsg/limits'] = array(
|
25 |
|
|
'title' => 'Limits',
|
26 |
|
|
'description' => 'Configure limits',
|
27 |
|
|
'page callback' => 'drupal_get_form',
|
28 |
|
|
'page arguments' => array('privatemsg_limits_admin'),
|
29 |
|
|
'file' => 'privatemsg_limits.admin.inc',
|
30 |
|
|
'access arguments' => array('administer privatemsg settings'),
|
31 |
|
|
'type' => MENU_LOCAL_TASK,
|
32 |
|
|
);
|
33 |
|
|
return $items;
|
34 |
|
|
}
|
35 |
|
|
|
36 |
|
|
/**
|
37 |
|
|
* Implements hook_privatemsg_message_validate().
|
38 |
|
|
*/
|
39 |
|
|
function privatemsg_limits_privatemsg_message_validate($message, $form = FALSE) {
|
40 |
|
|
$errors = array();
|
41 |
|
|
|
42 |
|
|
if (variable_get('privatemsg_limits_recipients_enabled', FALSE)) {
|
43 |
|
|
$amount = _privatemsg_limits_get_amount('recipients_amount', $message->author);
|
44 |
|
|
if (!isset($message->thread_id) && $amount > 0 && count($message->recipients) > $amount) {
|
45 |
|
|
$errors[] = t("You are not allowed to send a message to more than @number recipients.", array('@number' => $amount));
|
46 |
|
|
}
|
47 |
|
|
}
|
48 |
|
|
|
49 |
|
|
// Only check sending limit if enabled and if this is either not a reply or
|
50 |
|
|
// messages should be checked and not threads. When the limit object are
|
51 |
|
|
// threads, users can send an unlimited amount of replies.
|
52 |
|
|
if (variable_get('privatemsg_limits_send_enabled', FALSE) && (empty($message->thread_id) || variable_get('privatemsg_limits_send_object', 'message') == 'message')) {
|
53 |
|
|
$amount = _privatemsg_limits_get_amount('send_amount', $message->author);
|
54 |
|
|
$used = _privatemsg_limits_get_sent($message->author, variable_get('privatemsg_limits_send_timeframe', 3600));
|
55 |
|
|
if ($amount > 0 && $used >= $amount) {
|
56 |
|
|
$wait_time = _privatemsg_limits_get_oldest($message->author, variable_get('privatemsg_limits_send_timeframe', 3600));
|
57 |
|
|
$period = format_interval(variable_get('privatemsg_limits_send_timeframe', 3600));
|
58 |
|
|
if (variable_get('privatemsg_limits_receive_object', 'message') == 'message') {
|
59 |
|
|
$errors[] = t("Your message was not sent because you have exceeded your sending limit. You are allowed to send %limit messages every @period. You can send your next message in in @wait_time.", array('@wait_time' => $wait_time, '%limit' => $amount, '@period' => $period));
|
60 |
|
|
}
|
61 |
|
|
else {
|
62 |
|
|
$errors[] = t("Your message was not sent because you have exceeded your sending limit. You are allowed to start %limit conversations every @period. You can start your next conversation in in @wait_time.", array('@wait_time' => $wait_time, '%limit' => $amount, '@period' => $period));
|
63 |
|
|
}
|
64 |
|
|
}
|
65 |
|
|
}
|
66 |
|
|
|
67 |
|
|
if (variable_get('privatemsg_limits_receive_enabled', FALSE) && (empty($message->thread_id) || variable_get('privatemsg_limits_receive_object', 'message') == 'message')) {
|
68 |
|
|
$amount = _privatemsg_limits_get_amount('receive_amount', $message->author);
|
69 |
|
|
$used = _privatemsg_limits_get_received($message->author);
|
70 |
|
|
|
71 |
|
|
if ($amount > 0 && $used >= $amount) {
|
72 |
|
|
if (variable_get('privatemsg_limits_receive_object', 'message') == 'message') {
|
73 |
|
|
$errors[] = t("Your message mailbox is currently full. You are allowed a maximum of %limit messages in your mailbox at one time. You won't be able to send or receive new messages until you delete some existing ones.", array('%limit' => $amount));
|
74 |
|
|
}
|
75 |
|
|
else {
|
76 |
|
|
$errors[] = t("Your message mailbox is currently full. You are allowed a maximum of %limit conversations in your mailbox at one time. You won't be able to start or receive new conversations until you delete some existing ones.", array('%limit' => $amount));
|
77 |
|
|
}
|
78 |
|
|
}
|
79 |
|
|
}
|
80 |
|
|
|
81 |
|
|
// Blocks message sending if over number of messages per-thread.
|
82 |
|
|
if (isset($message->thread_id) && variable_get('privatemsg_limits_messages_per_thread', 0) > 0) {
|
83 |
|
|
// If we're not blocking the message.
|
84 |
|
|
if (variable_get('privatemsg_limits_messages_per_thread_action', 'create-new') == 'block-message') {
|
85 |
|
|
$query = "SELECT COUNT(*) FROM {pm_index} WHERE thread_id = :thread_id AND recipient = :recipient AND type IN ('hidden', 'user')";
|
86 |
|
|
$messages = db_query($query, array(':thread_id' => $message->thread_id, ':recipient' => $message->author->uid))->fetchField();
|
87 |
|
|
|
88 |
|
|
if ($messages >= variable_get('privatemsg_limits_messages_per_thread', 0)) {
|
89 |
|
|
// If the number of messages per-thread has been exceeded, block message
|
90 |
|
|
// from being sent.
|
91 |
|
|
$errors[] = t("This message cannot be sent because the thread already contains %limit messages (the maximum number of messages permitted per thread). To send this message, please create a new message thread.", array('%limit' => variable_get('privatemsg_limits_messages_per_thread', 0)));
|
92 |
|
|
}
|
93 |
|
|
}
|
94 |
|
|
}
|
95 |
|
|
if (!empty($errors)) {
|
96 |
|
|
if ($form) {
|
97 |
|
|
foreach ($errors as $error) {
|
98 |
|
|
form_set_error('recipient', $error);
|
99 |
|
|
}
|
100 |
|
|
}
|
101 |
|
|
else {
|
102 |
|
|
return array('error' => $errors);
|
103 |
|
|
}
|
104 |
|
|
}
|
105 |
|
|
}
|
106 |
|
|
|
107 |
|
|
/**
|
108 |
|
|
* Implements hook_privatemsg_block_message().
|
109 |
|
|
*/
|
110 |
|
|
function privatemsg_limits_privatemsg_block_message($author, $recipients, $context = array()) {
|
111 |
|
|
if (variable_get('privatemsg_limits_receive_enabled', FALSE) && (empty($context['thread_id']) || variable_get('privatemsg_limits_receive_object', 'message') == 'message') ) {
|
112 |
|
|
$blocked = array();
|
113 |
|
|
|
114 |
|
|
// Users that have the by-pass permission can send messages even if the
|
115 |
|
|
// mailbox of the recipient is full.
|
116 |
|
|
if (user_access('bypass recipient message limit', $author)) {
|
117 |
|
|
return $blocked;
|
118 |
|
|
}
|
119 |
|
|
|
120 |
|
|
foreach ($recipients as $recipient) {
|
121 |
|
|
// Only user recipients are supported.
|
122 |
|
|
if (!isset($recipient->type) || $recipient->type == 'user' || $recipient->type == 'hidden') {
|
123 |
|
|
$amount = _privatemsg_limits_get_amount('receive_amount', $recipient);
|
124 |
|
|
$used = _privatemsg_limits_get_received($recipient);
|
125 |
|
|
if ($amount > 0 && $used >= $amount) {
|
126 |
|
|
$blocked[] = array(
|
127 |
|
|
'recipient' => privatemsg_recipient_key($recipient),
|
128 |
|
|
'message' => t("This message cannot be sent to !name because !name's mailbox is full.", array('!name' => theme('username', array('account' => $recipient)))),
|
129 |
|
|
);
|
130 |
|
|
}
|
131 |
|
|
}
|
132 |
|
|
}
|
133 |
|
|
return $blocked;
|
134 |
|
|
}
|
135 |
|
|
}
|
136 |
|
|
|
137 |
|
|
/**
|
138 |
|
|
* Implements hook_privatemsg_message_presave_alter().
|
139 |
|
|
*/
|
140 |
|
|
function privatemsg_limits_privatemsg_message_presave_alter($message) {
|
141 |
|
|
// Put message into new thread if over number of messages per-thread.
|
142 |
|
|
if (isset($message->thread_id) && variable_get('privatemsg_limits_messages_per_thread', 0) > 0) {
|
143 |
|
|
// If we're not creating a new thread.
|
144 |
|
|
if (variable_get('privatemsg_limits_messages_per_thread_action', 'create-new') != 'create-new') {
|
145 |
|
|
return;
|
146 |
|
|
}
|
147 |
|
|
|
148 |
|
|
$query = "SELECT COUNT(*) FROM {pm_index} WHERE thread_id = :thread_id AND recipient = :recipient AND type IN ('hidden', 'user')";
|
149 |
|
|
$result = db_query($query, array(':thread_id' => $message->thread_id, ':recipient' => $message->author->uid))->fetchField();
|
150 |
|
|
|
151 |
|
|
if ($result >= variable_get('privatemsg_limits_messages_per_thread', 0)) {
|
152 |
|
|
// If the number of messages per-thread has been exceeded, force message into new thread.
|
153 |
|
|
unset($message->thread_id);
|
154 |
|
|
drupal_set_message(t("Your message would have exceeded our %limit messages per conversation limit. As a result, we've created a new conversation for your message.", array('%limit' => variable_get('privatemsg_limits_messages_per_thread', 0))));
|
155 |
|
|
}
|
156 |
|
|
}
|
157 |
|
|
}
|
158 |
|
|
|
159 |
|
|
/**
|
160 |
|
|
* Implements hook_form_FORM_ID_alter().
|
161 |
|
|
*
|
162 |
|
|
* Displays a limit info in the message listing.
|
163 |
|
|
*/
|
164 |
|
|
function privatemsg_limits_form_privatemsg_list_alter(&$form, &$form_state) {
|
165 |
|
|
global $user;
|
166 |
|
|
$limit = _privatemsg_limits_get_amount('receive_amount', $form['account']['#value']);
|
167 |
|
|
if ($limit > 0) {
|
168 |
|
|
$used = _privatemsg_limits_get_received($form['account']['#value']);
|
169 |
|
|
if ($used < $limit) {
|
170 |
|
|
$percent = round(($used / $limit) * 100);
|
171 |
|
|
}
|
172 |
|
|
else {
|
173 |
|
|
$percent = 100;
|
174 |
|
|
if ($user->uid == $form['account']['#value']->uid) {
|
175 |
|
|
if (variable_get('privatemsg_limits_receive_object', 'message') == 'message') {
|
176 |
|
|
$error = t("Your message mailbox is currently full. You are allowed a maximum of %limit messages in your mailbox at one time. You won't be able to send or receive new messages until you delete some existing ones.", array('%limit' => $limit));
|
177 |
|
|
}
|
178 |
|
|
else {
|
179 |
|
|
$error = t("Your message mailbox is currently full. You are allowed a maximum of %limit conversations in your mailbox at one time. You won't be able to start or receive new conversations until you delete some existing ones.", array('%limit' => $limit));
|
180 |
|
|
}
|
181 |
|
|
}
|
182 |
|
|
else {
|
183 |
|
|
if (variable_get('privatemsg_limits_receive_object', 'message') == 'message') {
|
184 |
|
|
$error = t("This message mailbox is currently full. !user is allowed a maximum of %limit messages in his mailbox at one time. !user won't be able to send or receive new messages until you delete some existing ones.", array('%limit' => $limit, '!user' => theme('username', array('account' => $form['account']['#value']))));
|
185 |
|
|
}
|
186 |
|
|
else {
|
187 |
|
|
$error = t("This message mailbox is currently full. !user is allowed a maximum of %limit conversations in his mailbox at one time. !user won't be able to start or receive new conversations until you delete some existing ones.", array('%limit' => $limit, '!user' => theme('username', array('account' => $form['account']['#value']))));
|
188 |
|
|
}
|
189 |
|
|
}
|
190 |
|
|
drupal_set_message($error, 'error');
|
191 |
|
|
}
|
192 |
|
|
if (variable_get('privatemsg_limits_receive_object', 'message') == 'message') {
|
193 |
|
|
$message = format_plural($used, 'You are currently using %percent% (@count message) of your %limit messages limit.', 'You are currently using %percent% (@count messages) of your %limit messages limit.', array('%percent' => $percent, '%used' => $used, '%limit' => $limit));
|
194 |
|
|
}
|
195 |
|
|
else {
|
196 |
|
|
$message = format_plural($used, 'You are currently using %percent% (@count conversation) of your %limit conversations limit.', 'You are currently using %percent% (@count conversations) of your %limit conversations limit.', array('%percent' => $percent, '%used' => $used, '%limit' => $limit));
|
197 |
|
|
}
|
198 |
|
|
$form['limit'] = array(
|
199 |
|
|
'#markup' => $message,
|
200 |
|
|
'#weight' => 15,
|
201 |
|
|
);
|
202 |
|
|
}
|
203 |
|
|
}
|
204 |
|
|
|
205 |
|
|
/**
|
206 |
|
|
* Loads the oldest message a user has written in the specified timeframe.
|
207 |
|
|
*
|
208 |
|
|
* @param $account
|
209 |
|
|
* User object
|
210 |
|
|
* @param $timeframe
|
211 |
|
|
* Defines how many seconds back should be considered.
|
212 |
|
|
*/
|
213 |
|
|
function _privatemsg_limits_get_oldest($account, $timeframe) {
|
214 |
|
|
$query = _privatemsg_assemble_query(array('sent', 'privatemsg_limits'),
|
215 |
|
|
$account, $timeframe);
|
216 |
|
|
$timestamp = $query
|
217 |
|
|
->execute()
|
218 |
|
|
->fetchField();
|
219 |
|
|
|
220 |
|
|
$wait_time = ($timestamp - (REQUEST_TIME - $timeframe));
|
221 |
|
|
return format_interval($wait_time, 6);
|
222 |
|
|
}
|
223 |
|
|
|
224 |
|
|
/**
|
225 |
|
|
* Loads the maximum value of a threshold value, takes roles into account.
|
226 |
|
|
*
|
227 |
|
|
* @param $name
|
228 |
|
|
* Unique part of the name.
|
229 |
|
|
* @param $account
|
230 |
|
|
* User object to get the limit for.
|
231 |
|
|
*
|
232 |
|
|
* @return
|
233 |
|
|
* A specific number or 0 for unlimited.
|
234 |
|
|
*/
|
235 |
|
|
function _privatemsg_limits_get_amount($name, $account) {
|
236 |
|
|
// Don't limit uid 1.
|
237 |
|
|
if ($account->uid == 1) {
|
238 |
|
|
return 0;
|
239 |
|
|
}
|
240 |
|
|
|
241 |
|
|
// $account might not be a fully loaded user account, fetch the roles in that
|
242 |
|
|
// case.
|
243 |
|
|
// @todo: Remove once privatemsg_user_load_multiple() is implemented.
|
244 |
|
|
if (!isset($account->roles)) {
|
245 |
|
|
$account->roles = array(DRUPAL_AUTHENTICATED_RID => 'authenticated user');
|
246 |
|
|
$account->roles += db_query('SELECT r.rid, r.name FROM {role} r INNER JOIN {users_roles} ur ON ur.rid = r.rid WHERE ur.uid = :uid', array(':uid' => $account->uid))->fetchAllKeyed();
|
247 |
|
|
}
|
248 |
|
|
|
249 |
|
|
$role_max = 0;
|
250 |
|
|
foreach ($account->roles as $id => $role) {
|
251 |
|
|
$new_max = variable_get("privatemsg_limits_{$name}_role_" . $id, NULL);
|
252 |
|
|
if ($new_max == 'unlimited') {
|
253 |
|
|
return 0;
|
254 |
|
|
}
|
255 |
|
|
if ($new_max > $role_max) {
|
256 |
|
|
$role_max = $new_max;
|
257 |
|
|
}
|
258 |
|
|
}
|
259 |
|
|
if ($role_max == 0) {
|
260 |
|
|
return variable_get('privatemsg_limits_' . $name, 0);
|
261 |
|
|
}
|
262 |
|
|
return $role_max;
|
263 |
|
|
}
|
264 |
|
|
|
265 |
|
|
/**
|
266 |
|
|
* Returns the number of messages/threads a user has written.
|
267 |
|
|
*
|
268 |
|
|
* @param user $account User object
|
269 |
|
|
* @param int $timeframe How many seconds back should be considered
|
270 |
|
|
*
|
271 |
|
|
* @return int Number of messages/threads
|
272 |
|
|
*/
|
273 |
|
|
function _privatemsg_limits_get_sent($account, $timeframe) {
|
274 |
|
|
$query = _privatemsg_assemble_query(array('sent', 'privatemsg_limits'),
|
275 |
|
|
$account, $timeframe);
|
276 |
|
|
return $query
|
277 |
|
|
->countQuery()
|
278 |
|
|
->execute()
|
279 |
|
|
->fetchField();
|
280 |
|
|
}
|
281 |
|
|
|
282 |
|
|
/**
|
283 |
|
|
* SQL Function for amount of messages/threads a user has written in a timeframe.
|
284 |
|
|
*
|
285 |
|
|
* @param array $fragments Query array
|
286 |
|
|
* @param user $account User object
|
287 |
|
|
* @param int $timeframe how many seconds to consider.
|
288 |
|
|
*/
|
289 |
|
|
function privatemsg_limits_sql_sent($account, $timeframe) {
|
290 |
|
|
$query = db_select('pm_message', 'pm');
|
291 |
|
|
|
292 |
|
|
if (variable_get('privatemsg_limits_send_object', 'message') == 'thread') {
|
293 |
|
|
$query->addExpression('MAX(pm.timestamp)', 'timestamp');
|
294 |
|
|
$query->join('pm_index', 'pmi', 'pmi.mid = pm.mid');
|
295 |
|
|
$query->groupBy('pmi.thread_id');
|
296 |
|
|
}
|
297 |
|
|
else {
|
298 |
|
|
$query->addField('pm', 'timestamp');
|
299 |
|
|
}
|
300 |
|
|
|
301 |
|
|
return $query
|
302 |
|
|
->condition('pm.author', $account->uid)
|
303 |
|
|
->condition('pm.timestamp', REQUEST_TIME - $timeframe, '>')
|
304 |
|
|
->orderBy('timestamp', 'ASC');
|
305 |
|
|
}
|
306 |
|
|
|
307 |
|
|
/**
|
308 |
|
|
* Returns the number of messages/threads a user has received.
|
309 |
|
|
*
|
310 |
|
|
* @param user $account User object
|
311 |
|
|
* @param int $timeframe How many seconds back should be considered
|
312 |
|
|
*
|
313 |
|
|
* @return int Number of messages/threads
|
314 |
|
|
*/
|
315 |
|
|
function _privatemsg_limits_get_received($account) {
|
316 |
|
|
$query = _privatemsg_assemble_query(array('received', 'privatemsg_limits'),
|
317 |
|
|
$account);
|
318 |
|
|
return (int)$query
|
319 |
|
|
->countQuery()
|
320 |
|
|
->execute()
|
321 |
|
|
->fetchField();
|
322 |
|
|
}
|
323 |
|
|
|
324 |
|
|
/**
|
325 |
|
|
* SQL Function for amount of messages/threads a users has.
|
326 |
|
|
*
|
327 |
|
|
* @param array $fragments Query array
|
328 |
|
|
* @param user $account User object
|
329 |
|
|
* @param int $timeframe how many seconds to consider.
|
330 |
|
|
*/
|
331 |
|
|
function privatemsg_limits_sql_received($account) {
|
332 |
|
|
$query = db_select('pm_index', 'pmi');
|
333 |
|
|
$query->join('pm_message', 'pm', 'pm.mid = pmi.mid');
|
334 |
|
|
$query->addExpression('MAX(pm.timestamp)', 'timestamp');
|
335 |
|
|
|
336 |
|
|
$query
|
337 |
|
|
->condition('pmi.recipient', $account->uid)
|
338 |
|
|
->condition('pmi.deleted', 0)
|
339 |
|
|
->condition('pmi.type', array('hidden', 'user'))
|
340 |
|
|
->orderBy('timestamp', 'ASC');
|
341 |
|
|
|
342 |
|
|
if (variable_get('privatemsg_limits_receive_object', 'message') == 'thread') {
|
343 |
|
|
$query->groupBy('pmi.thread_id');
|
344 |
|
|
}
|
345 |
|
|
else {
|
346 |
|
|
$query->groupBy('pmi.mid');
|
347 |
|
|
}
|
348 |
|
|
return $query;
|
349 |
|
|
} |