PageRenderTime 41ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/mods/bulk_pm/bulk_pm.php

https://bitbucket.org/webop/webop-forum
PHP | 200 lines | 141 code | 35 blank | 24 comment | 36 complexity | a6270675b8cc1eaa74d5aa9d246ce639 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /* phorum module info
  3. title: Bulk PM
  4. desc: Allows admins to send private messages to all users by typing ALL in the To: field or selecing "PM to all users" from the user drop down list.
  5. category: admin
  6. hook: common|bulk_pm_init
  7. hook: pm_read|bulk_pm_rcpt_list
  8. hook: pm_before_send|bulk_pm_alter_rcpt
  9. */
  10. function bulk_pm_init(){
  11. global $PHORUM;
  12. if (phorum_page != 'pm') {
  13. return;
  14. }
  15. if (!empty($_POST['start'])) {
  16. $_SESSION['bulk_mailer']['start'] = (int)$_POST['start'];
  17. }
  18. if (!empty($_POST['count'])) {
  19. $_SESSION['bulk_mailer']['count'] = (int)$_POST['count'];
  20. }
  21. $recipients = isset($_POST['recipients']) ? $_POST['recipients'] : array();
  22. # Check for ALL
  23. $start = isset($_POST['start']) ? (int)$_POST['start'] : 0;
  24. $count = isset($_POST['count']) ? (int)$_POST['count'] : 0;
  25. if(phorum_page=="pm" && $PHORUM["user"]["admin"] && !is_null($_POST['to_name']) && strpos($_POST['to_name'], '%ALL') !== false) {
  26. $rcpt = isset($_POST['to_user']) ? $_POST['to_user'] : $_POST['to_id'];
  27. switch ($rcpt) {
  28. case '%ALL:A': {
  29. $recipients = $recipients + bulk_pm_user_get_list($start, $count, 1);
  30. break;
  31. }
  32. case '%ALL:I': {
  33. $recipients = $recipients + bulk_pm_user_get_list($start, $count, 2);
  34. break;
  35. }
  36. case '%ALL':
  37. default: {
  38. $recipients = $recipients + bulk_pm_user_get_list($start, $count, 0);
  39. break;
  40. }
  41. }
  42. $_POST["to_name"] = "";
  43. $_POST["to_id"] = "";
  44. }
  45. # Check for GROUP
  46. if(phorum_page=="pm" && $PHORUM["user"]["admin"] && isset($_POST) && !is_null($_POST['to_name']) && strpos($_POST["to_name"], "%GROUP:") !== false) {
  47. $grope = explode(":", $_POST['to_name']);
  48. $groupname = $grope[1];
  49. $group_list = phorum_db_get_groups();
  50. $group_id = 0;
  51. foreach($group_list as $group) {
  52. if ($group['name'] == $groupname) {
  53. $group_id = $group['group_id'];
  54. break;
  55. }
  56. }
  57. $group_recipients = phorum_db_get_group_members($group_id);
  58. if (empty($recipients)) {
  59. $recipients = $group_recipients;
  60. }
  61. else {
  62. $recipients = $recipients + $group_recipients;
  63. }
  64. $_POST["to_name"] = "";
  65. $_POST["to_id"] = "";
  66. }
  67. /*if (!empty($_POST['start']) && !empty($_POST['count']) && !empty($recipients)) {
  68. $start = empty($_POST['start']) ? 0 : (int)$_POST['start'];
  69. $count = empty($_POST['count']) ? 0 : (int)$_POST['count'];
  70. foreach ($recipients as $key => $recipient) {
  71. if ($recipient['user_id'] > $count || $recipient['user_id'] < $start) {
  72. unset($recipients[$key]);
  73. }
  74. }
  75. }
  76. asort($recipients);*/
  77. if (!empty($recipients)) {
  78. $_POST['recipients'] = $recipients;
  79. }
  80. }
  81. function bulk_pm_user_get_list($first = 0, $count = 0, $type = 0) {
  82. global $PHORUM;
  83. $ids = array();
  84. if ($first > 0 && $count > 0) {
  85. $chegdb = mysql_connect('localhost', 'PoCUser', 'dummy');
  86. mysql_select_db('PoCDB', $chegdb);
  87. $response = mysql_query("SELECT id FROM users WHERE TIMESTAMPDIFF(DAY, `seen` , now()) <= 30 AND id >= $first AND id NOT IN (73396, 144216, 189010, 208819, 387584) ORDER BY id ASC LIMIT $count", $chegdb);
  88. while($resultset = mysql_fetch_array($response)) {
  89. $ids[] = $resultset['id'];
  90. }
  91. mysql_close($chegdb);
  92. }
  93. settype($type, 'int');
  94. $wheres = array();
  95. if ($type == 1) $wheres[] = 'active = 1';
  96. elseif ($type == 2) $wheres[] = 'active != 1';
  97. if (!empty($ids)) {
  98. $wheres[] = 'user_id IN (' . join(', ', $ids) . ')';
  99. }
  100. if (!empty($wheres)) {
  101. $where = 'WHERE ' . join(' AND ', $wheres);
  102. }
  103. else {
  104. return array();
  105. }
  106. $users = phorum_db_interact(
  107. DB_RETURN_ASSOCS,
  108. "SELECT user_id,
  109. username,
  110. display_name
  111. FROM {$PHORUM['user_table']}
  112. $where
  113. ORDER BY user_id ASC",
  114. 'user_id'
  115. );
  116. return $users;
  117. }
  118. function bulk_pm_alter_rcpt($msg) {
  119. global $PHORUM;
  120. if (count($msg['recipients']) == 1) {
  121. return $msg;
  122. }
  123. if (!isset($_SESSION['bulk_mailer']['count']) || !isset($_SESSION['bulk_mailer']['start'])) {
  124. return $msg;
  125. }
  126. $lastmsg = array_slice($msg['recipients'], count($msg['recipients']) - 1, 1);
  127. $msgcount = count($msg['recipients']);
  128. $lastuid = $lastmsg[0]['user_id'];
  129. $firstmsg = array_slice($msg['recipients'], 0, 1);
  130. unset($msg['recipients'][(int)$firstmsg[0]['user_id']]);
  131. foreach ($msg['recipients'] as $recipient => $recipients) {
  132. $pm_message = $msg;
  133. $pm_message['recipients'] = array((int)$recipients['user_id'] => $recipients);
  134. //$pm_message['keep'] = 1;
  135. $pm_message_id = phorum_db_pm_send($pm_message["subject"], $pm_message["message"], array_keys($pm_message['recipients']), NULL, $pm_message["keep"]);
  136. $pm_message['pm_message_id'] = $pm_message_id;
  137. $pm_message['from_username'] = $PHORUM['user']['display_name'];
  138. $pm_message['user_id'] = $user_id;
  139. // Show an error in case of problems.
  140. if (! $pm_message_id) {
  141. $error = $PHORUM["DATA"]["LANG"]["PMNotSent"];
  142. // Do e-mail notifications on successful sending.
  143. } elseif (!empty($PHORUM['allow_pm_email_notify'])) {
  144. include_once("./include/email_functions.php");
  145. // Sort all recipients that want a notify by language.
  146. $langrcpts = array();
  147. foreach ($pm_message['recipients'] as $rcpt_id => $rcpt) {
  148. if ($rcpt["pm_email_notify"]) {
  149. if (!isset($langrcpts[$rcpt["user_language"]])) {
  150. $langrcpts[$rcpt["user_language"]] = array($rcpt);
  151. } else {
  152. $langrcpts[$rcpt["user_language"]][] = $rcpt;
  153. }
  154. }
  155. }
  156. phorum_email_pm_notice($pm_message, $langrcpts);
  157. }
  158. if (isset($PHORUM["hooks"]["pm_sent"])) {
  159. phorum_hook("pm_sent", $pm_message, array_keys($pm_message['recipients']));
  160. }
  161. }
  162. $dummy = phorum_db_pm_send("Bulk Mailer", "$msgcount messages sent.\nLast user ID sent to: $lastuid", $PHORUM['user']['user_id'], NULL, 0);
  163. $msg['recipients'] = array((int)$firstmsg[0]['user_id'] => $firstmsg[0]);
  164. return $msg;
  165. }