POST OFFICE Stuff | > Module written by Matt Mecham | > Date started: 1st April 2004 (April Fools!) | | > Module Version Number: 1.0.0 | > DBA Checked: Tue 25th May 2004 +-------------------------------------------------------------------------- */ if ( ! defined( 'IN_ACP' ) ) { print "

Incorrect access

You cannot access this file directly. If you have recently upgraded, make sure you upgraded 'admin.php'."; exit(); } class ad_postoffice { var $base_url; var $colours = array(); var $root_path = './'; function auto_run() { global $ibforums, $DB, $std; $ibforums->admin->nav[] = array( 'act=postoffice', 'IPB Post Office' ); $ibforums->admin->page_detail = "You may manage previous bulk emails and create new bulk emails in this section."; $ibforums->admin->page_title = "IPB Post Office"; //----------------------------------------- // Kill globals - globals bad, Homer good. //----------------------------------------- $tmp_in = array_merge( $_GET, $_POST, $_COOKIE ); foreach ( $tmp_in as $k => $v ) { unset($$k); } switch($ibforums->input['code']) { case 'mail_new': $this->mail_form('add'); break; case 'mail_edit': $this->mail_form('edit'); break; case 'mail_save': $this->mail_save(); break; case 'mail_preview': $this->mail_preview_start(); break; case 'mail_preview_do': $this->mail_preview_complete(); break; case 'mail_send_start': $this->mail_send_start(); break; case 'mail_send_complete': $this->mail_send_complete(); break; case 'mail_send_cancel': $this->mail_send_cancel(); break; case 'mail_delete': $this->mail_delete(); break; //----------------------------------------- // Default //----------------------------------------- default: $this->mail_start(); break; } } //----------------------------------------- // DELETE MAIL - WOT IS SAYZ ON TEH TIN //----------------------------------------- function mail_delete() { global $ibforums, $DB, $std; $id = intval( $ibforums->input['id'] ); $DB->do_update( 'bulk_mail', array( 'mail_active' => 0, 'mail_updated' => time(), ), "mail_active=1" ); $DB->do_update( 'task_manager', array( 'task_enabled' => 0 ), "task_key='bulkmail'" ); $DB->simple_exec_query( array( 'delete' => 'bulk_mail', 'where' => 'mail_id='.$id ) ); $ibforums->main_msg = "Bulk email deleted"; $this->mail_start(); } //----------------------------------------- // SEND MAIL: Cancel sending in progress //----------------------------------------- function mail_send_cancel() { global $ibforums, $DB, $std; $DB->do_update( 'bulk_mail', array( 'mail_active' => 0, 'mail_updated' => time(), ), "mail_active=1" ); $DB->do_update( 'task_manager', array( 'task_enabled' => 0 ), "task_key='bulkmail'" ); $ibforums->main_msg = "Bulk email cancelled"; $this->mail_start(); } //----------------------------------------- // SEND MAIL: Send the mail mah-boy //----------------------------------------- function mail_send_process( $root_path="" ) { global $ibforums, $DB, $std; //----------------------------------------- // Set up //----------------------------------------- if ( $root_path ) { $this->root_path = $root_path; } else if ( ROOT_PATH ) { $this->root_path = ROOT_PATH; } require_once( $this->root_path."sources/classes/class_email.php"); $this->email = new emailer( $this->root_path ); $done = 0; $sent = 0; //----------------------------------------- // Get it from the db //----------------------------------------- $mail = $DB->simple_exec_query( array( 'select' => '*', 'from' => 'bulk_mail', 'where' => 'mail_active=1' ) ); if ( ! $mail['mail_subject'] and ! $mail['mail_content'] ) { $done = 1; } //----------------------------------------- // Per go... //----------------------------------------- $pergo = intval($mail['mail_pergo']); if ( ! $pergo or $pergo > 1000 ) { $pergo = 50; } //----------------------------------------- // So far... //----------------------------------------- $sofar = intval($mail['mail_sentto']); $mail['mail_content'] = $std->txt_stripslashes( $mail['mail_content'] ); $mail['mail_subject'] = $std->txt_stripslashes( $mail['mail_subject'] ); //----------------------------------------- // Unconvert options //----------------------------------------- $opts = unserialize(stripslashes( $mail['mail_opts'] ) ); foreach( $opts as $k => $v ) { $mail[ $k ] = $v; } //----------------------------------------- // Format the query //----------------------------------------- $query = $this->_build_members_query( $mail ); //----------------------------------------- // Get members.... //----------------------------------------- $DB->simple_construct( array( 'select' => '*', 'from' => 'members', 'where' => $query, 'order' => 'id', 'limit' => array( $sofar, $pergo ) ) ); $o = $DB->simple_exec(); if ( $mail['mail_html_on'] ) { $this->email->html_email = 1; } while ( $r = $DB->fetch_row( $o ) ) { $sent++; $contents = $this->_convert_quick_tags( $mail['mail_content'], $r ); $this->email->from = $ibforums->vars['email_in']; $this->email->to = $r['email']; $this->email->message = str_replace( "\r\n", "\n", $contents); $this->email->subject = $mail['mail_subject']; $this->email->send_mail(); } //----------------------------------------- // Did we send any? //----------------------------------------- if ( ! $sent ) { $done = 1; } //----------------------------------------- // Save out.. //----------------------------------------- if ( $done ) { $DB->do_update( 'bulk_mail', array( 'mail_active' => 0, 'mail_updated' => time(), 'mail_sentto' => $sofar + $sent ), 'mail_id='.$mail['mail_id'] ); $DB->do_update( 'task_manager', array( 'task_enabled' => 0 ), "task_key='bulkmail'" ); } else { $DB->do_update( 'bulk_mail', array( 'mail_updated' => time(), 'mail_sentto' => $sofar + $sent ), 'mail_id='.$mail['mail_id'] ); } } //----------------------------------------- // SEND MAIL: Complete //----------------------------------------- function mail_send_complete() { global $ibforums, $DB, $std; $pergo = intval($ibforums->input['pergo']); $id = intval($ibforums->input['id']); if ( ! $id ) { $ibforums->main_msg = "There was no ID passed to this function"; $this->mail_start(); } //----------------------------------------- // Get it from the db //----------------------------------------- $mail = $DB->simple_exec_query( array( 'select' => '*', 'from' => 'bulk_mail', 'where' => 'mail_id='.$id ) ); if ( ! $mail['mail_subject'] and ! $mail['mail_content'] ) { $ibforums->main_msg = "Cannot send - the email doesn't have a subject and content"; $this->mail_start(); } //----------------------------------------- // Update mail //----------------------------------------- if ( ! $pergo or $pergo > 1000 ) { $pergo = 50; } $DB->do_update( 'bulk_mail', array( 'mail_active' => 1, 'mail_pergo' => $pergo, 'mail_sentto' => 0, 'mail_start' => time() ), 'mail_id='.$id ); $DB->do_update( 'bulk_mail', array( 'mail_active' => 0 ) , 'mail_id != '.$id ); //----------------------------------------- // Wake up task manager //----------------------------------------- require_once( ROOT_PATH.'sources/lib/task_functions.php' ); $task = new task_functions(); $DB->do_update( 'task_manager', array( 'task_enabled' => 1 ), "task_key='bulkmail'" ); $this_task = $DB->simple_exec_query( array( 'select' => '*', 'from' => 'task_manager', 'where' => "task_key='bulkmail'" ) ); $newdate = $task->generate_next_run($this_task); $DB->do_update( 'task_manager', array( 'task_next_run' => $newdate ), "task_id=".$this_task['task_id'] ); $task->save_next_run_stamp(); //----------------------------------------- // Sit back and watch the show //----------------------------------------- $ibforums->main_msg = "Bulk Mail Initiated"; $this->mail_start(); } //----------------------------------------- // SEND MAIL: Start //----------------------------------------- function mail_send_start() { global $ibforums, $DB, $std; $id = intval($ibforums->input['id']); if ( ! $id ) { $ibforums->main_msg = "There was no ID passed to this function"; $this->mail_start(); } //----------------------------------------- // Get it from the db //----------------------------------------- $mail = $DB->simple_exec_query( array( 'select' => '*', 'from' => 'bulk_mail', 'where' => 'mail_id='.$id ) ); if ( ! $mail['mail_subject'] and ! $mail['mail_content'] ) { $ibforums->main_msg = "Cannot send - the email doesn't have a subject and content"; $this->mail_start(); } //----------------------------------------- // Unconvert options //----------------------------------------- $opts = unserialize(stripslashes( $mail['mail_opts'] ) ); foreach( $opts as $k => $v ) { $mail[ $k ] = $v; } //----------------------------------------- // Format the query //----------------------------------------- $query = $this->_build_members_query( $mail ); //----------------------------------------- // Count how many matches //----------------------------------------- $count = $DB->simple_exec_query( array( 'select' => 'count(*) as cnt', 'from' => 'members', 'where' => $query ) ); $the_count = intval( $count['cnt'] ); //----------------------------------------- // Print 'continue' screen //----------------------------------------- $ibforums->html .= $ibforums->adskin->start_form( array( 1 => array( 'code' , 'mail_send_complete' ), 2 => array( 'act' , 'postoffice' ), 4 => array( 'id' , $ibforums->input['id'] ), ) ); $ibforums->html .= "
Sending the bulk mail to {$the_count} members
"; $ibforums->html .= "
Email Details Subject: {$mail['mail_subject']}

{$mail['mail_content']}

Edit This Email

Sending to {$the_count}  members.

Sending the Email Clicking the 'Start Sending The Emails' button below will activate the task manager's send bulk email function which will send the email in batches until the all the mail has been sent. You can watch the progress from the main Post Office screen and cancel the email from being sent.
We strongly recommend that you opt to send 50 emails per cycle as a maximum. Any more per cycle could severly affect performance of your server while the mail is being sent out.

As the mail is sent out in cycles of n  per minute, it can take a while to process all the mail.

Emails sent per cycle  
"; $ibforums->admin->output(); } //----------------------------------------- // PREVIEW MAIL: Show it - yo! //----------------------------------------- function mail_preview_complete() { global $ibforums, $DB, $std; $content = nl2br($this->_convert_quick_tags( $std->txt_stripslashes($_POST['text']), $ibforums->member )); print "
{$content} "; exit(); } //----------------------------------------- // PREVIEW MAIL: JS BOUNCE //----------------------------------------- function mail_preview_start() { global $ibforums, $DB, $std; print "
"; } //----------------------------------------- // SAVE MAIL //----------------------------------------- function mail_save() { global $ibforums, $DB, $std; //----------------------------------------- // Set up //----------------------------------------- $ids = array(); $ibforums->input['id'] = intval($ibforums->input['id']); //----------------------------------------- // Start //----------------------------------------- $type = $ibforums->input['type']; if ( ! $ibforums->input['mail_subject'] or ! $ibforums->input['mail_content'] ) { $ibforums->main_msg = "You must enter an email subject and email content before submitting."; $this->mail_form( $type ); } //----------------------------------------- // Groups... //----------------------------------------- foreach ($ibforums->input as $key => $value) { if ( preg_match( "/^sg_(\d+)$/", $key, $match ) ) { if ($ibforums->input[ $match[0] ]) { $ids[] = $match[1]; } } } if ( ! count( $ids ) ) { $ibforums->main_msg = "You must select at least one group to send this email message to."; $this->mail_form( $type ); } $ibforums->input['mail_groups'] = implode( ",", $ids ); //----------------------------------------- // Format the query //----------------------------------------- $query = $this->_build_members_query( array( 'mail_post_ltmt' => $_POST['mail_post_ltmt'], 'mail_filter_post' => $_POST['mail_filter_post'], 'mail_visit_ltmt' => $_POST['mail_visit_ltmt'], 'mail_filter_visit' => $_POST['mail_filter_visit'], 'mail_joined_ltmt' => $_POST['mail_joined_ltmt'], 'mail_filter_joined' => $_POST['mail_filter_joined'], 'mail_honor' => $_POST['mail_honor'], 'group_id_string' => implode( ",", $ids ) ) ); //----------------------------------------- // Count how many matches //----------------------------------------- $count = $DB->simple_exec_query( array( 'select' => 'count(*) as cnt', 'from' => 'members', 'where' => $query ) ); if ( ! $count['cnt'] ) { $ibforums->main_msg = "Your criteria didn't match any members - please adjust your current criteria"; $this->mail_form( $type ); } //----------------------------------------- // Save //----------------------------------------- $save_array = array( 'mail_subject' => str_replace( "'", "'", $std->txt_stripslashes( $_POST['mail_subject'] ) ), 'mail_content' => $std->txt_stripslashes( $_POST['mail_content'] ), 'mail_groups' => implode( ",", $ids ), 'mail_honor' => $ibforums->input['mail_honor'], 'mail_start' => time(), 'mail_updated' => time(), 'mail_sentto' => 0, 'mail_opts' => serialize( array( 'mail_post_ltmt' => $_POST['mail_post_ltmt'], 'mail_filter_post' => $_POST['mail_filter_post'], 'mail_visit_ltmt' => $_POST['mail_visit_ltmt'], 'mail_filter_visit' => $_POST['mail_filter_visit'], 'mail_joined_ltmt' => $_POST['mail_joined_ltmt'], 'mail_filter_joined' => $_POST['mail_filter_joined'], 'mail_html_on' => $_POST['mail_html_on'], ) ) ); if ( $type == 'add' ) { //----------------------------------------- // Save to DB //----------------------------------------- $DB->do_insert( 'bulk_mail', $save_array ); $ibforums->input['id'] = $DB->get_insert_id(); $ibforums->admin->save_log("Bulk mail ({$ibforums->input['mail_subject']}) added"); $this->mail_send_start(); } else { if ( ! $ibforums->input['id'] ) { $ibforums->main_msg = "Cannot save, no ID returned"; $this->mail_form($type); } $DB->do_update( 'bulk_mail', $save_array, 'mail_id='.$ibforums->input['id'] ); $ibforums->admin->save_log("Bulk mail ({$ibforums->input['mail_subject']}) edited"); $ibforums->main_msg = "Bulk mail edited"; $this->mail_start(); } } //----------------------------------------- // SHOW MAIN MAIL SCREENIE-POOS //----------------------------------------- function mail_form($type='add') { global $ibforums, $DB, $std; $ibforums->input['id'] = intval($ibforums->input['id']); $dd_ltmt = array( 0 => array( 'lt' , "less than" ), 1 => array( 'mt' , "more than" ) ); if ( $type == 'add' ) { $mail = array(); $title = "Step 1: Start New Bulk Email"; $button = "Proceed..."; $honour_checked = 'checked="checked"'; $html_checked = ''; } else { $mail = $DB->simple_exec_query( array( 'select' => '*', 'from' => 'bulk_mail', 'where' => 'mail_id='.$ibforums->input['id'] ) ); $title = "Editing Stored Mail"; $button = "Edit Mail"; //----------------------------------------- // Unpack more.. //----------------------------------------- $tmp = unserialize( stripslashes( $mail['mail_opts'] ) ); if ( is_array( $tmp ) and count ( $tmp ) ) { foreach( $tmp as $k => $v ) { if ( ! $mail[ $k ] ) { $mail[ $k ] = $v; } } } $honour_checked = $mail['mail_honor'] == 1 ? 'checked="checked"' : ''; $html_checked = $mail['mail_html_on'] == 1 ? 'checked="checked"' : ''; } $ibforums->html .= $ibforums->adskin->start_form( array( 1 => array( 'code' , 'mail_save' ), 2 => array( 'act' , 'postoffice' ), 3 => array( 'type' , $type ), 4 => array( 'id' , $ibforums->input['id'] ), ) ); $mail_subject = $_POST['mail_subject'] ? $std->txt_stripslashes($_POST['mail_subject']) : $mail['mail_subject']; $mail_content = $_POST['mail_content'] ? $std->txt_stripslashes($_POST['mail_content']) : $mail['mail_content']; $mail_subject = str_replace( "'", "'", $mail_subject ); $mail_content = preg_replace( "[^\r]\n", "\r\n", $mail_content ); if ( ! $mail_content and $type == 'add' ) { $mail_content = $this->_get_default_mail_contents(); } if ( $ibforums->input['mail_groups'] ) { $mail['mail_groups'] = $ibforums->input['mail_groups']; } $ibforums->html .= " "; $ibforums->html .= "
{$title}
"; $ibforums->html .= "
Email Subject

Email Content
Mail Sending Options   Honor 'Allow admin to email me' member setting to only email those who have opted in?
  Send email in HTML format (full HTML formatting & tags allowed)
"; $ibforums->html .= "

"; $ibforums->html .= "
Filters
"; $ibforums->html .= "
Send to Groups:"; foreach( $ibforums->cache['group_cache'] as $bleh => $g ) { if ( $g['g_id'] == $ibforums->vars['guest_group'] ) { continue; } $checked = ""; if ( $mail['mail_groups'] ) { if ( strstr( ','.$mail['mail_groups'].',', ','.$g['g_id'].',' ) ) { $checked = 'checked="checked"'; } } $ibforums->html .= "  {$g['g_title']}
"; } $ibforums->html .= "
Additional Filters
Send to members with n posts
Leave blank to not filter
". $ibforums->adskin->form_dropdown('mail_post_ltmt', $dd_ltmt, $_POST['mail_post_ltml'] ? $_POST['mail_post_ltml'] : $mail['mail_post_ltmt'] ).' '. $ibforums->adskin->form_simple_input( "mail_filter_post", $_POST['mail_filter_post'] ? $_POST['mail_filter_post'] : $mail['mail_filter_post'], 7 )."
Send to members who's last visit was n days ago
Leave blank to not filter
". $ibforums->adskin->form_dropdown('mail_visit_ltmt', $dd_ltmt, $_POST['mail_visit_ltml'] ? $_POST['mail_visit_ltml'] : $mail['mail_visit_ltmt'] ).' '. $ibforums->adskin->form_simple_input( "mail_filter_visit", $_POST['mail_filter_visit'] ? $_POST['mail_filter_visit'] : $mail['mail_filter_visit'], 7 )."
Send to members who joined n days ago
Leave blank to not filter
". $ibforums->adskin->form_dropdown('mail_joined_ltmt', $dd_ltmt, $_POST['mail_joined_ltml'] ? $_POST['mail_joined_ltml'] : $mail['mail_joined_ltmt'] ).' '. $ibforums->adskin->form_simple_input( "mail_filter_joined", $_POST['mail_filter_joined'] ? $_POST['mail_filter_joined'] : $mail['mail_filter_joined'], 7 )."
   
"; $ibforums->html .= "
Quick Tags
You may use the following 'quick tags' in your email and they will be converted when the email is sent.
{board_name} The name of your board {board_url} The URL to your board
{reg_total} The total number of registered members {total_posts} The total number of posts
{busy_count} The most number of users online {busy_time} The time that the most number of members were online
{member_id} The member ID of the email recipient {member_name} The member name of the email recipient
{member_joined} The member join date of the email recipient {member_posts} The number of posts for the email recipient
"; $ibforums->admin->output(); } //----------------------------------------- // SHOW MAIN MAIL SCREENIE-POOS //----------------------------------------- function mail_start() { global $ibforums, $DB, $std; $ibforums->adskin->td_header[] = array( " " , "1%" ); $ibforums->adskin->td_header[] = array( "Subject" , "30%" ); $ibforums->adskin->td_header[] = array( "Sent On" , "15%" ); $ibforums->adskin->td_header[] = array( "Sent To" , "15%" ); $ibforums->adskin->td_header[] = array( "Time Taken" , "15%" ); $ibforums->adskin->td_header[] = array( "Options" , "25%" ); $ibforums->html .= $ibforums->adskin->start_table( "Your stored bulk email" ); //----------------------------------------- // Get mail from DB //----------------------------------------- $DB->simple_construct( array( 'select' => '*', 'from' => 'bulk_mail', 'order' => 'mail_start DESC' ) ); $DB->simple_exec(); while( $r = $DB->fetch_row() ) { $inprogress = ""; if ( $r['mail_updated'] == $r['mail_start'] ) { $time_taken = 'Not yet sent'; } else { $time_taken = intval($r['mail_updated'] - $r['mail_start']); if ( $time_taken < 0 ) { $time_taken = 0; } if ( $time_taken ) { $time_taken = ceil( $time_taken / 60 ); } $time_taken .= ' minutes'; } if ( $r['mail_active'] ) { $inprogress = " ( Sending in progress - base_url}&act=postoffice&code=mail_send_cancel', 'OK to cancel bulk mail?'); return false;\">Cancel )"; } $ibforums->html .= $ibforums->adskin->add_td_row( array( "", "{$r['mail_subject']} $inprogress", $std->get_date( $r['mail_start'], 'SHORT' ), $std->do_number_format( $r['mail_sentto'] ).' members', $time_taken, "
Re-send Edit base_url}&act=postoffice&code=mail_delete&id={$r['mail_id']}');return false;\">Delete " ) ); } $ibforums->html .= $ibforums->adskin->end_table(); $ibforums->admin->output(); } //----------------------------------------- // INTERNAL: build members query //----------------------------------------- function _build_members_query( $args = array() ) { global $ibforums, $DB, $std; $DB->load_cache_file( $this->root_path.'sources/sql/'.SQL_DRIVER.'_extra_queries.php', 'sql_extra_queries' ); $query = array(); if ( $args['mail_filter_post'] ) { $ltmt = $args['mail_post_ltmt'] == 'lt' ? '<' : '>'; $query[] = "posts ".$ltmt." ".$args['mail_filter_post']; } if ( $args['mail_filter_visit'] ) { $ltmt = $args['mail_visit_ltmt'] == 'lt' ? '<' : '>'; $time = time() - ( $args['mail_filter_visit'] * 86400 ); $query[] = "last_visit ".$ltmt." ". $time; } if ( $args['mail_filter_visit'] ) { $ltmt = $args['mail_joined_ltmt'] == 'lt' ? '<' : '>'; $time = time() - ( $args['mail_filter_joined'] * 86400 ); $query[] = "joined ".$ltmt." ". $time; } if ( $args['mail_honor'] ) { $query[] = "allow_admin_mails=1"; } if ( $args['mail_groups'] ) { $tmp_q = 'mgroup IN ('. $args['mail_groups'] .')'; $temp = explode( ',', $args['mail_groups'] ); if ( is_array( $temp ) and count( $temp ) ) { $tmp = array(); foreach( $temp as $id ) { $DB->cache_add_query( 'acp_postoffice_concat_bit', array( 'gid' => $id ), 'sql_extra_queries' ); $tmp[] = $DB->cur_query; $DB->cur_query = ""; } $tmp_q .= " OR ( ".implode( ' OR ', $tmp ). " )"; } $query[] = $tmp_q; } return implode( ' AND ', $query ); } //----------------------------------------- // INTERNAL: convert quick tags //----------------------------------------- function _convert_quick_tags( $contents="", $member=array() ) { global $ibforums, $DB, $std; $contents = str_replace( "{board_name}" , str_replace( "'", "'", $ibforums->vars['board_name'] ) , $contents ); $contents = str_replace( "{board_url}" , $ibforums->vars['board_url']."/index.".$ibforums->vars['php_ext'] , $contents ); $contents = str_replace( "{reg_total}" , $ibforums->cache['stats']['mem_count'] , $contents ); $contents = str_replace( "{total_posts}" , $ibforums->cache['stats']['total_topics'] + $ibforums->cache['stats']['total_replies'] , $contents ); $contents = str_replace( "{busy_count}" , $ibforums->cache['stats']['most_count'] , $contents ); $contents = str_replace( "{busy_time}" , $std->get_date( $ibforums->cache['stats']['most_date'], 'SHORT' ), $contents ); $contents = str_replace( "{member_id}" , $member['id'], $contents ); $contents = str_replace( "{member_name}" , $member['name'], $contents ); $contents = str_replace( "{member_joined}", date( 'j-F y', $member['joined'] ), $contents ); $contents = str_replace( "{member_posts}" , $member['posts'], $contents ); return $contents; } //----------------------------------------- // INTERNAL: get default mail //----------------------------------------- function _get_default_mail_contents() { global $ibforums, $DB, $std; $mail = "{member_name},\n\n\n\n-------------------------------------\n{board_name} Statistics:\n" ."-------------------------------------\nRegistered Users: {reg_total}\nTotal Posts: {total_posts}\n" ."Busiest Time: {busy_count} users were online on {busy_time}\n\n" ."-------------------------------------\nHandy Links\n" ."-------------------------------------\nBoard Address: {board_url}\nLog In: {board_url}?act=Login&CODE=00\n" ."Lost Password Recovery: {board_url}?act=Reg&CODE=10\n\n" ."-------------------------------------\nHow to unsubscribe\n" ."-------------------------------------\nVisit your email preferences ({board_url}?act=UserCP&CODE=02) and ensure " ."that the box for 'Send me any updates sent by the board administrator' is unchecked and submit the form"; return $mail; } } ?>