privatemsg_limits.module

  1. 7-1 privatemsg_limits/privatemsg_limits.module
  2. 7-2 privatemsg_limits/privatemsg_limits.module
  3. 6-2 privatemsg_limits/privatemsg_limits.module

Privatemsg Quota module

File

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

Functions

Namesort descending Description
privatemsg_limits_form_privatemsg_list_alter Implements hook_form_FORM_ID_alter().
privatemsg_limits_menu Implements hook_menu().
privatemsg_limits_permission Implements hook_permission()-
privatemsg_limits_privatemsg_block_message Implements hook_privatemsg_block_message().
privatemsg_limits_privatemsg_message_presave_alter Implements hook_privatemsg_message_presave_alter().
privatemsg_limits_privatemsg_message_validate Implements hook_privatemsg_message_validate().
privatemsg_limits_sql_received SQL Function for amount of messages/threads a users has.
privatemsg_limits_sql_sent SQL Function for amount of messages/threads a user has written in a timeframe.
_privatemsg_limits_get_amount Loads the maximum value of a threshold value, takes roles into account.
_privatemsg_limits_get_oldest Loads the oldest message a user has written in the specified timeframe.
_privatemsg_limits_get_received Returns the number of messages/threads a user has received.
_privatemsg_limits_get_sent Returns the number of messages/threads a user has written.