// END CrackerTracker v5.x
 // CrackerTracker v5.x
 $sql = "INSERT INTO " . GROUPS_TABLE . " (group_name, group_description, group_single_user, group_moderator)\n\t\t\t\tVALUES ('', 'Personal User', 1, 0)";
 $result = $db->sql_query($sql);
 $group_id = $db->sql_nextid();
 $sql = "INSERT INTO " . USER_GROUP_TABLE . " (user_id, group_id, user_pending)\n\t\t\t\tVALUES ({$user_id}, {$group_id}, 0)";
 $result = $db->sql_query($sql);
 $db->sql_transaction('commit');
 // PM ON REGISTER - BEGIN
 $founder_id = defined('FOUNDER_ID') ? FOUNDER_ID : get_founder_id();
 include_once IP_ROOT_PATH . 'includes/class_pm.' . PHP_EXT;
 $privmsg_sender = $founder_id;
 $privmsg_recipient = $user_id;
 $privmsg_subject = sprintf($lang['register_pm_subject'], $config['sitename']);
 $privmsg_message = sprintf($lang['register_pm'], $config['sitename'], $config['sitename']);
 $privmsg = new class_pm();
 $privmsg->send($privmsg_sender, $privmsg_recipient, $privmsg_subject, $privmsg_message);
 unset($privmsg);
 // PM ON REGISTER - END
 board_stats();
 if ($coppa) {
     $message = $lang['COPPA'];
     $email_template = 'coppa_welcome_inactive';
 } elseif ($config['require_activation'] == USER_ACTIVATION_SELF) {
     $message = $lang['Account_inactive'];
     $email_template = 'user_welcome_inactive';
 } elseif ($config['require_activation'] == USER_ACTIVATION_ADMIN) {
     $message = $lang['Account_inactive_admin'];
     $email_template = 'admin_welcome_inactive';
 } else {
     $message = $lang['Account_added'];
 $sql = "UPDATE " . MEGAMAIL_TABLE . "\n\t\t\tSET mailsession_id = '" . $db->sql_escape($mail_session_id) . "', batch_start= " . ($mail_data['batch_start'] + $mail_data['batch_size']) . $is_done . "\n\t\t\tWHERE mail_id = '" . $mail_id . "'";
 $result = $db->sql_query($sql);
 // OK, now let's start sending
 $error = false;
 $error_msg = '';
 if ($process_groups) {
     $sql = "SELECT u.user_id, u.user_email\n\t\t\t\t\t\tFROM " . USERS_TABLE . " u, " . USER_GROUP_TABLE . " ug\n\t\t\t\t\t\tWHERE ug.group_id = '" . $group_id . "'\n\t\t\t\t\t\t\tAND ug.user_pending <> " . TRUE . "\n\t\t\t\t\t\t\tAND u.user_id = ug.user_id\n\t\t\t\t\t\t\tAND u.user_active = 1\n\t\t\t\t\t\t\tAND u.user_allow_mass_email = 1";
 } else {
     $sql = "SELECT user_id, user_email\n\t\t\t\t\t\tFROM " . USERS_TABLE . " u\n\t\t\t\t\t\tWHERE u.user_active = 1\n\t\t\t\t\t\t\tAND u.user_allow_mass_email = 1\n\t\t\t\t\t\t\t" . $sql_non_recent_login;
 }
 $sql .= " LIMIT " . $mail_data['batch_start'] . ", " . $mail_data['batch_size'];
 $result = $db->sql_query($sql);
 if ($row = $db->sql_fetchrow($result)) {
     if ($mass_pm) {
         include_once IP_ROOT_PATH . 'includes/class_pm.' . PHP_EXT;
         $privmsg = new class_pm();
     }
     $bcc_list_array = array();
     $bcc_list = '';
     do {
         if ($mass_pm) {
             $privmsg->send($user->data['user_id'], $row['user_id'], $subject, $pm_message);
         }
         $bcc_list .= ($bcc_list != '' ? ', ' : '') . $row['user_email'];
         $bcc_list_array[] = $row['user_email'];
     } while ($row = $db->sql_fetchrow($result));
     $db->sql_freeresult($result);
     if ($mass_pm) {
         unset($privmsg);
     }
 } else {
function cash_pm(&$targetdata, $privmsg_subject, &$message)
{
    global $db, $cache, $config, $user, $lang, $bbcode, $html_entities_match, $html_entities_replace;
    //
    // It looks like we're sending a PM!
    // NOTE: most of the following code is shamelessly "reproduced" from privmsg.php
    //
    include_once IP_ROOT_PATH . 'includes/bbcode.' . PHP_EXT;
    include_once IP_ROOT_PATH . 'includes/functions_post.' . PHP_EXT;
    $attach_sig = $user->data['user_attachsig'];
    $bbcode->allow_html = $user->data['user_allowhtml'] && $config['allow_html'] ? true : false;
    $bbcode->allow_bbcode = true;
    $bbcode->allow_smilies = $user->data['user_allowsmile'] && $config['allow_smilies'] ? true : false;
    $html_status = $bbcode->allow_html;
    $bbcode_status = $bbcode->allow_bbcode;
    $smilies_status = $bbcode->allow_smilies;
    $acro_auto_status = false;
    include_once IP_ROOT_PATH . 'includes/class_pm.' . PHP_EXT;
    $privmsg_message = prepare_message($message, $html_status, $bbcode_status, $smilies_status);
    $privmsg_sender = $user->data['user_id'];
    $privmsg_recipient = $targetdata['user_id'];
    $privmsg = new class_pm();
    if ($user->data['user_level'] != ADMIN && $privmsg->is_flood()) {
        message_die(GENERAL_MESSAGE, $lang['Flood_Error']);
    }
    $privmsg->delete_older_message('PM_INBOX', $privmsg_recipient);
    $privmsg->send($privmsg_sender, $privmsg_recipient, $privmsg_subject, $privmsg_message, $attach_sig, $html_status, $bbcode_status, $smilies_status, $acro_auto_status);
    if ($targetdata['user_notify_pm'] && !empty($targetdata['user_email']) && $targetdata['user_active']) {
        //HTML Message
        $clean_tags = $config['html_email'] ? false : true;
        $bbcode->allow_bbcode = $config['allow_bbcode'] ? $config['allow_bbcode'] : false;
        $bbcode->allow_html = $config['allow_html'] ? $config['allow_html'] : false;
        $bbcode->allow_smilies = $config['allow_smilies'] ? $config['allow_smilies'] : false;
        $message = $bbcode->parse($privmsg_message, '', false, $clean_tags);
        $message = stripslashes($message);
        //HTML Message
        $privmsg->notification($privmsg_sender, $privmsg_recipient, $targetdata['user_email'], $lang['Notification_subject'], $message, false, $privmsg_subject, $targetdata['username'], $targetdata['user_lang'], false);
    }
    unset($privmsg);
}
/**
* Sends a birthday PM
*/
function birthday_pm_send()
{
    global $db, $cache, $config, $user, $lang;
    // Birthday - BEGIN
    // Check if the user has or have had birthday, also see if greetings are enabled
    if ($user->data['user_birthday'] != 999999 && !empty($config['birthday_greeting']) && create_date('Ymd', time(), $config['board_timezone']) >= $user->data['user_next_birthday_greeting'] . realdate('md', $user->data['user_birthday'])) {
        // If a user had a birthday more than one week before we will not send the PM...
        if (time() - gmmktime(0, 0, 0, $user->data['user_birthday_m'], $user->data['user_birthday_d'], $user->data['user_next_birthday_greeting']) <= 86400 * 8) {
            // Birthday PM - BEGIN
            $pm_subject = $lang['Greeting_Messaging'];
            $pm_date = gmdate('U');
            $year = create_date('Y', time(), $config['board_timezone']);
            $date_today = create_date('Ymd', time(), $config['board_timezone']);
            $user_birthday = realdate('md', $user->data['user_birthday']);
            $user_birthday2 = ($year . $user_birthday < $date_today ? $year + 1 : $year) . $user_birthday;
            $user_age = create_date('Y', time(), $config['board_timezone']) - realdate('Y', $user->data['user_birthday']);
            if (create_date('md', time(), $config['board_timezone']) < realdate('md', $user->data['user_birthday'])) {
                $user_age--;
            }
            $pm_text = $user_birthday2 == $date_today ? sprintf($lang['Birthday_greeting_today'], $user_age) : sprintf($lang['Birthday_greeting_prev'], $user_age, realdate(str_replace('Y', '', $lang['DATE_FORMAT_BIRTHDAY']), $user->data['user_birthday']) . (!empty($user->data['user_next_birthday_greeting']) ? $user->data['user_next_birthday_greeting'] : ''));
            $founder_id = defined('FOUNDER_ID') ? FOUNDER_ID : get_founder_id();
            include_once IP_ROOT_PATH . 'includes/class_pm.' . PHP_EXT;
            $privmsg_subject = sprintf($pm_subject, $config['sitename']);
            $privmsg_message = sprintf($pm_text, $config['sitename'], $config['sitename']);
            $privmsg_sender = $founder_id;
            $privmsg_recipient = $user->data['user_id'];
            $privmsg = new class_pm();
            $privmsg->delete_older_message('PM_INBOX', $privmsg_recipient);
            $privmsg->send($privmsg_sender, $privmsg_recipient, $privmsg_subject, $privmsg_message);
            unset($privmsg);
            // Birthday PM - END
        }
        // Update next greetings year
        $sql = "UPDATE " . USERS_TABLE . "\n\t\t\tSET user_next_birthday_greeting = " . (create_date('Y', time(), $config['board_timezone']) + 1) . "\n\t\t\tWHERE user_id = " . $user->data['user_id'];
        $status = $db->sql_query($sql);
    }
    //Sorry user shall not have a greeting this year
    // Birthday - END
}