/mods/bulk_pm/bulk_pm.php
PHP | 200 lines | 141 code | 35 blank | 24 comment | 36 complexity | a6270675b8cc1eaa74d5aa9d246ce639 MD5 | raw file
Possible License(s): LGPL-2.1
- <?php
- /* phorum module info
- title: Bulk PM
- 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.
- category: admin
- hook: common|bulk_pm_init
- hook: pm_read|bulk_pm_rcpt_list
- hook: pm_before_send|bulk_pm_alter_rcpt
- */
- function bulk_pm_init(){
- global $PHORUM;
- if (phorum_page != 'pm') {
- return;
- }
-
- if (!empty($_POST['start'])) {
- $_SESSION['bulk_mailer']['start'] = (int)$_POST['start'];
- }
-
- if (!empty($_POST['count'])) {
- $_SESSION['bulk_mailer']['count'] = (int)$_POST['count'];
- }
-
- $recipients = isset($_POST['recipients']) ? $_POST['recipients'] : array();
- # Check for ALL
- $start = isset($_POST['start']) ? (int)$_POST['start'] : 0;
- $count = isset($_POST['count']) ? (int)$_POST['count'] : 0;
- if(phorum_page=="pm" && $PHORUM["user"]["admin"] && !is_null($_POST['to_name']) && strpos($_POST['to_name'], '%ALL') !== false) {
- $rcpt = isset($_POST['to_user']) ? $_POST['to_user'] : $_POST['to_id'];
- switch ($rcpt) {
- case '%ALL:A': {
- $recipients = $recipients + bulk_pm_user_get_list($start, $count, 1);
- break;
- }
- case '%ALL:I': {
- $recipients = $recipients + bulk_pm_user_get_list($start, $count, 2);
- break;
- }
- case '%ALL':
- default: {
- $recipients = $recipients + bulk_pm_user_get_list($start, $count, 0);
- break;
- }
- }
-
- $_POST["to_name"] = "";
- $_POST["to_id"] = "";
- }
- # Check for GROUP
- if(phorum_page=="pm" && $PHORUM["user"]["admin"] && isset($_POST) && !is_null($_POST['to_name']) && strpos($_POST["to_name"], "%GROUP:") !== false) {
- $grope = explode(":", $_POST['to_name']);
- $groupname = $grope[1];
- $group_list = phorum_db_get_groups();
- $group_id = 0;
-
- foreach($group_list as $group) {
- if ($group['name'] == $groupname) {
- $group_id = $group['group_id'];
- break;
- }
- }
-
- $group_recipients = phorum_db_get_group_members($group_id);
- if (empty($recipients)) {
- $recipients = $group_recipients;
- }
- else {
- $recipients = $recipients + $group_recipients;
- }
- $_POST["to_name"] = "";
- $_POST["to_id"] = "";
- }
-
- /*if (!empty($_POST['start']) && !empty($_POST['count']) && !empty($recipients)) {
- $start = empty($_POST['start']) ? 0 : (int)$_POST['start'];
- $count = empty($_POST['count']) ? 0 : (int)$_POST['count'];
- foreach ($recipients as $key => $recipient) {
- if ($recipient['user_id'] > $count || $recipient['user_id'] < $start) {
- unset($recipients[$key]);
- }
- }
- }
- asort($recipients);*/
- if (!empty($recipients)) {
- $_POST['recipients'] = $recipients;
- }
- }
- function bulk_pm_user_get_list($first = 0, $count = 0, $type = 0) {
- global $PHORUM;
-
- $ids = array();
-
- if ($first > 0 && $count > 0) {
- $chegdb = mysql_connect('localhost', 'PoCUser', 'dummy');
- mysql_select_db('PoCDB', $chegdb);
- $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);
- while($resultset = mysql_fetch_array($response)) {
- $ids[] = $resultset['id'];
- }
- mysql_close($chegdb);
- }
- settype($type, 'int');
- $wheres = array();
- if ($type == 1) $wheres[] = 'active = 1';
- elseif ($type == 2) $wheres[] = 'active != 1';
-
- if (!empty($ids)) {
- $wheres[] = 'user_id IN (' . join(', ', $ids) . ')';
- }
-
- if (!empty($wheres)) {
- $where = 'WHERE ' . join(' AND ', $wheres);
- }
- else {
- return array();
- }
- $users = phorum_db_interact(
- DB_RETURN_ASSOCS,
- "SELECT user_id,
- username,
- display_name
- FROM {$PHORUM['user_table']}
- $where
- ORDER BY user_id ASC",
- 'user_id'
- );
- return $users;
- }
- function bulk_pm_alter_rcpt($msg) {
- global $PHORUM;
- if (count($msg['recipients']) == 1) {
- return $msg;
- }
-
- if (!isset($_SESSION['bulk_mailer']['count']) || !isset($_SESSION['bulk_mailer']['start'])) {
- return $msg;
- }
-
- $lastmsg = array_slice($msg['recipients'], count($msg['recipients']) - 1, 1);
- $msgcount = count($msg['recipients']);
- $lastuid = $lastmsg[0]['user_id'];
- $firstmsg = array_slice($msg['recipients'], 0, 1);
-
- unset($msg['recipients'][(int)$firstmsg[0]['user_id']]);
- foreach ($msg['recipients'] as $recipient => $recipients) {
- $pm_message = $msg;
- $pm_message['recipients'] = array((int)$recipients['user_id'] => $recipients);
- //$pm_message['keep'] = 1;
- $pm_message_id = phorum_db_pm_send($pm_message["subject"], $pm_message["message"], array_keys($pm_message['recipients']), NULL, $pm_message["keep"]);
-
- $pm_message['pm_message_id'] = $pm_message_id;
- $pm_message['from_username'] = $PHORUM['user']['display_name'];
- $pm_message['user_id'] = $user_id;
-
- // Show an error in case of problems.
- if (! $pm_message_id) {
-
- $error = $PHORUM["DATA"]["LANG"]["PMNotSent"];
-
- // Do e-mail notifications on successful sending.
- } elseif (!empty($PHORUM['allow_pm_email_notify'])) {
-
- include_once("./include/email_functions.php");
-
- // Sort all recipients that want a notify by language.
- $langrcpts = array();
- foreach ($pm_message['recipients'] as $rcpt_id => $rcpt) {
-
- if ($rcpt["pm_email_notify"]) {
- if (!isset($langrcpts[$rcpt["user_language"]])) {
- $langrcpts[$rcpt["user_language"]] = array($rcpt);
- } else {
- $langrcpts[$rcpt["user_language"]][] = $rcpt;
- }
- }
- }
-
- phorum_email_pm_notice($pm_message, $langrcpts);
-
- }
-
- if (isset($PHORUM["hooks"]["pm_sent"])) {
- phorum_hook("pm_sent", $pm_message, array_keys($pm_message['recipients']));
- }
- }
-
- $dummy = phorum_db_pm_send("Bulk Mailer", "$msgcount messages sent.\nLast user ID sent to: $lastuid", $PHORUM['user']['user_id'], NULL, 0);
- $msg['recipients'] = array((int)$firstmsg[0]['user_id'] => $firstmsg[0]);
- return $msg;
- }