/**
  * Activates a user
  * user plugins must have been loaded
  *
  * @param  UserTable  $user
  * @param  int        $ui               1=frontend, 2=backend, 0=no UI: machine-machine UI
  * @param  string     $cause            (one of: 'UserRegistration', 'UserConfirmation', 'UserApproval', 'NewUser', 'UpdateUser')
  * @param  boolean    $mailToAdmins     true if the standard new-user email should be sent to admins if moderator emails are enabled
  * @param  boolean    $mailToUser       true if the welcome new user email (from CB config) should be sent to the new user
  * @param  boolean    $triggerBeforeActivate
  * @return array                        Texts to display
  */
 function activateUser(&$user, $ui, $cause, $mailToAdmins = true, $mailToUser = true, $triggerBeforeActivate = true)
 {
     global $ueConfig, $_PLUGINS;
     static $notificationsSent = array();
     $activate = $user->confirmed && $user->approved == 1;
     $showSysMessage = true;
     $savedLanguage = CBTxt::setLanguage($user->getUserLanguage());
     $messagesToUser = getActivationMessage($user, $cause);
     CBTxt::setLanguage($savedLanguage);
     if ($cause == 'UserConfirmation' && $user->approved == 0) {
         $activate = false;
         $msg = array('emailAdminSubject' => array('sys' => CBTxt::T('UE_REG_ADMIN_PA_SUB', 'ACTION REQUIRED! New user sign up request pending approval')), 'emailAdminMessage' => array('sys' => CBTxt::T('UE_REG_ADMIN_PA_MSG', "A new user has signed up at [SITEURL] and requires approval.\nThis email contains their details\n\nName - [NAME]\nE-mail - [EMAILADDRESS]\nUsername - [USERNAME]\n\n\nPlease do not respond to this message as it is automatically generated and is for informational purposes only.\n")), 'emailUserSubject' => array(), 'emailUserMessage' => array());
     } elseif ($user->confirmed == 0) {
         $msg = array('emailAdminSubject' => array(), 'emailAdminMessage' => array());
         $savedLanguage = CBTxt::setLanguage($user->getUserLanguage());
         $msg['emailUserSubject'] = array('sys' => CBTxt::T(stripslashes($ueConfig['reg_pend_appr_sub'])));
         $msg['emailUserMessage'] = array('sys' => CBTxt::T(stripslashes($ueConfig['reg_pend_appr_msg'])));
         CBTxt::setLanguage($savedLanguage);
     } elseif ($cause == 'SameUserRegistrationAgain') {
         $activate = false;
         $msg = array('emailAdminSubject' => array(), 'emailAdminMessage' => array(), 'emailUserSubject' => array(), 'emailUserMessage' => array());
     } elseif ($user->confirmed && !($user->approved == 1)) {
         $msg = array('emailAdminSubject' => array('sys' => CBTxt::T('UE_REG_ADMIN_PA_SUB', 'ACTION REQUIRED! New user sign up request pending approval')), 'emailAdminMessage' => array('sys' => CBTxt::T('UE_REG_ADMIN_PA_MSG', "A new user has signed up at [SITEURL] and requires approval.\nThis email contains their details\n\nName - [NAME]\nE-mail - [EMAILADDRESS]\nUsername - [USERNAME]\n\n\nPlease do not respond to this message as it is automatically generated and is for informational purposes only.\n")));
         $savedLanguage = CBTxt::setLanguage($user->getUserLanguage());
         $msg['emailUserSubject'] = array('sys' => CBTxt::T(stripslashes($ueConfig['reg_pend_appr_sub'])));
         $msg['emailUserMessage'] = array('sys' => CBTxt::T(stripslashes($ueConfig['reg_pend_appr_msg'])));
         CBTxt::setLanguage($savedLanguage);
     } elseif ($user->confirmed && $user->approved == 1) {
         $msg = array('emailAdminSubject' => array('sys' => CBTxt::T('UE_REG_ADMIN_SUB', 'New user sign up')), 'emailAdminMessage' => array('sys' => CBTxt::T('UE_REG_ADMIN_MSG', "A new user has signed up at [SITEURL].\nThis email contains their details\n\nName - [NAME]\nE-mail - [EMAILADDRESS]\nUsername - [USERNAME]\n\n\nPlease do not respond to this message as it is automatically generated and is for information purposes only.\n")));
         $savedLanguage = CBTxt::setLanguage($user->getUserLanguage());
         $msg['emailUserSubject'] = array('sys' => CBTxt::T(stripslashes($ueConfig['reg_welcome_sub'])));
         $msg['emailUserMessage'] = array('sys' => CBTxt::T(stripslashes($ueConfig['reg_welcome_msg'])));
         CBTxt::setLanguage($savedLanguage);
     }
     $msg['messagesToUser'] = $messagesToUser;
     if ($triggerBeforeActivate) {
         $results = $_PLUGINS->trigger('onBeforeUserActive', array(&$user, $ui, $cause, $mailToAdmins, $mailToUser));
         if ($_PLUGINS->is_errors() && $ui != 0) {
             echo $_PLUGINS->getErrorMSG('<br />');
         }
         foreach ($results as $res) {
             if (is_array($res)) {
                 $activate = $activate && $res['activate'];
                 $mailToAdmins = $mailToAdmins && $res['mailToAdmins'];
                 $mailToUser = $mailToUser && $res['mailToUser'];
                 $showSysMessage = $showSysMessage && $res['showSysMessage'];
                 foreach (array_keys($msg) as $key) {
                     if (isset($res[$key]) && $res[$key]) {
                         array_push($msg[$key], $res[$key]);
                     }
                 }
             }
         }
         if (!($mailToAdmins && $ueConfig['moderatorEmail'] == 1)) {
             unset($msg['emailAdminSubject']['sys']);
             unset($msg['emailAdminMessage']['sys']);
         }
         if (!$mailToUser) {
             unset($msg['emailUserSubject']['sys']);
             unset($msg['emailUserMessage']['sys']);
         }
         if (!$showSysMessage) {
             unset($msg['messagesToUser']['sys']);
         }
     }
     if ($activate) {
         $user->block = 0;
         $user->storeBlock(false);
         $user->removeActivationCode();
     }
     if ($activate) {
         $_PLUGINS->trigger('onUserActive', array(&$user, $ui, $cause, $mailToAdmins, $mailToUser));
         if ($_PLUGINS->is_errors() && $ui != 0) {
             $msg['messagesToUser'] = $_PLUGINS->getErrorMSG('<br />') . $msg['messagesToUser'];
         }
     }
     if (!isset($notificationsSent[$user->id][$user->confirmed][$user->approved][$user->block])) {
         // in case done several times (e.g. plugins), avoid resending messages.
         $cbNotification = new cbNotification();
         if ($ueConfig['moderatorEmail'] && count($msg['emailAdminMessage'])) {
             $pwd = $user->password;
             $user->password = null;
             $cbNotification->sendToModerators(implode(', ', $msg['emailAdminSubject']), $cbNotification->_replaceVariables(implode('\\n\\n', $msg['emailAdminMessage']), $user));
             $user->password = $pwd;
         }
         if (count($msg['emailUserMessage'])) {
             $cbNotification->sendFromSystem($user, implode(', ', $msg['emailUserSubject']), implode('\\n\\n', $msg['emailUserMessage']), true, isset($ueConfig['reg_email_html']) ? (int) $ueConfig['reg_email_html'] : 0);
         }
         $notificationsSent[$user->id][$user->confirmed][$user->approved][$user->block] = true;
     }
     return $msg['messagesToUser'];
 }
/**
 * Activates a user
 * user plugins must have been loaded
 *
 * @param  moscomprofilerUser  $user
 * @param  int      $ui               1=frontend, 2=backend, 0=no UI: machine-machine UI
 * @param  string   $cause            (one of: 'UserRegistration', 'UserConfirmation', 'UserApproval', 'NewUser', 'UpdateUser')
 * @param  boolean  $mailToAdmins     true if the standard new-user email should be sent to admins if moderator emails are enabled
 * @param  boolean  $mailToUser       true if the welcome new user email (from CB config) should be sent to the new user
 * @param  boolean  $triggerBeforeActivate
 * @return array of string          texts to display
 */
function activateUser(&$user, $ui, $cause, $mailToAdmins = true, $mailToUser = true, $triggerBeforeActivate = true)
{
    global $_CB_database, $ueConfig, $_PLUGINS;
    static $notificationsSent = array();
    $activate = $user->confirmed && $user->approved == 1;
    $showSysMessage = true;
    $messagesToUser = getActivationMessage($user, $cause);
    if ($cause == 'UserConfirmation' && $user->approved == 0) {
        $activate = false;
        $msg = array('emailAdminSubject' => array('sys' => _UE_REG_ADMIN_PA_SUB), 'emailAdminMessage' => array('sys' => _UE_REG_ADMIN_PA_MSG), 'emailUserSubject' => array(), 'emailUserMessage' => array());
    } elseif ($user->confirmed == 0) {
        $msg = array('emailAdminSubject' => array(), 'emailAdminMessage' => array(), 'emailUserSubject' => array('sys' => getLangDefinition(stripslashes($ueConfig['reg_pend_appr_sub']))), 'emailUserMessage' => array('sys' => getLangDefinition(stripslashes($ueConfig['reg_pend_appr_msg']))));
    } elseif ($cause == 'SameUserRegistrationAgain') {
        $activate = false;
        $msg = array('emailAdminSubject' => array(), 'emailAdminMessage' => array(), 'emailUserSubject' => array(), 'emailUserMessage' => array());
    } elseif ($user->confirmed && !($user->approved == 1)) {
        $msg = array('emailAdminSubject' => array('sys' => _UE_REG_ADMIN_PA_SUB), 'emailAdminMessage' => array('sys' => _UE_REG_ADMIN_PA_MSG), 'emailUserSubject' => array('sys' => getLangDefinition(stripslashes($ueConfig['reg_pend_appr_sub']))), 'emailUserMessage' => array('sys' => getLangDefinition(stripslashes($ueConfig['reg_pend_appr_msg']))));
    } elseif ($user->confirmed && $user->approved == 1) {
        $msg = array('emailAdminSubject' => array('sys' => _UE_REG_ADMIN_SUB), 'emailAdminMessage' => array('sys' => _UE_REG_ADMIN_MSG), 'emailUserSubject' => array('sys' => getLangDefinition(stripslashes($ueConfig['reg_welcome_sub']))), 'emailUserMessage' => array('sys' => getLangDefinition(stripslashes($ueConfig['reg_welcome_msg']))));
    }
    $msg['messagesToUser'] = $messagesToUser;
    if ($triggerBeforeActivate) {
        $results = $_PLUGINS->trigger('onBeforeUserActive', array(&$user, $ui, $cause, $mailToAdmins, $mailToUser));
        if ($_PLUGINS->is_errors() && $ui != 0) {
            echo $_PLUGINS->getErrorMSG('<br />');
        }
        foreach ($results as $res) {
            if (is_array($res)) {
                $activate = $activate && $res['activate'];
                $mailToAdmins = $mailToAdmins && $res['mailToAdmins'];
                $mailToUser = $mailToUser && $res['mailToUser'];
                $showSysMessage = $showSysMessage && $res['showSysMessage'];
                foreach (array_keys($msg) as $key) {
                    if (isset($res[$key]) && $res[$key]) {
                        array_push($msg[$key], $res[$key]);
                    }
                }
            }
        }
        if (!($mailToAdmins && $ueConfig['moderatorEmail'] == 1)) {
            unset($msg['emailAdminSubject']['sys']);
            unset($msg['emailAdminMessage']['sys']);
        }
        if (!$mailToUser) {
            unset($msg['emailUserSubject']['sys']);
            unset($msg['emailUserMessage']['sys']);
        }
        if (!$showSysMessage) {
            unset($msg['messagesToUser']['sys']);
        }
    }
    if ($activate) {
        $user->block = 0;
        $user->storeBlock(false);
        $user->removeActivationCode();
    }
    if ($activate) {
        $_PLUGINS->trigger('onUserActive', array(&$user, $ui, $cause, $mailToAdmins, $mailToUser));
        if ($_PLUGINS->is_errors() && $ui != 0) {
            $msg['messagesToUser'] = $_PLUGINS->getErrorMSG('<br />') . $msg['messagesToUser'];
        }
    }
    if (!isset($notificationsSent[$user->id][$user->confirmed][$user->approved][$user->block])) {
        // in case done several times (e.g. plugins), avoid resending messages.
        $cbNotification = new cbNotification();
        if ($ueConfig['moderatorEmail'] && count($msg['emailAdminMessage'])) {
            $pwd = $user->password;
            $user->password = null;
            $cbNotification->sendToModerators(implode(', ', $msg['emailAdminSubject']), $cbNotification->_replaceVariables(implode('\\n\\n', $msg['emailAdminMessage']), $user));
            $user->password = $pwd;
        }
        if (count($msg['emailUserMessage'])) {
            $cbNotification->sendFromSystem($user, implode(', ', $msg['emailUserSubject']), implode('\\n\\n', $msg['emailUserMessage']));
        }
        $notificationsSent[$user->id][$user->confirmed][$user->approved][$user->block] = true;
    }
    return $msg['messagesToUser'];
}