$message = $l->g('access_denied');
if (!isset($activation_code) || !is_scalar($activation_code)) {
    $activation_code = '';
}
_pcpin_loadClass('user');
$user = new PCPIN_User($session);
_pcpin_loadClass('message');
$msg = new PCPIN_Message($session);
_pcpin_loadClass('pcpintpl');
$tpl = new PcpinTpl();
$tpl->setBasedir('./tpl');
$tpl->readTemplatesFromFile('./dummy.tpl');
if ($activation_code != '') {
    if (isset($activate_email)) {
        // New email address activation
        if ($user->_db_getList('email_new != ', 'email_new_activation_code = ' . md5($activation_code), 1)) {
            // Requested email address found and activation code is OK
            $user->_db_setObject($user->_db_list[0]);
            $user->email = $user->email_new;
            $user->email_new = '';
            $user->email_new_date = '';
            $user->email_new_activation_code = '';
            $user->_db_updateObj($user->id);
            $message = $l->g('new_email_activated');
            if ($session->_db_getList('_s_room_id', '_s_user_id = ' . $user->id, 1)) {
                // User is online
                $msg->addMessage(1010, 'n', 0, '', $session->_db_list[0]['_s_room_id'], 0, $user->id);
            }
        } else {
            // Invalid activation code
            $message = $l->g('invalid_activation_code');
 /**
  * Set user avatar from Avatar Gallery
  * @param   int   $user_id      User ID
  * @param   int   $avatar_id    Avatar ID
  * @return  boolean TRUE on success or FALSE on error
  */
 function setAvatarFromGallery($user_id = 0, $avatar_id = 0)
 {
     $result = false;
     if (!empty($user_id) && !empty($avatar_id) && $this->_db_getList('binaryfile_id', 'id = ' . $avatar_id, 'user_id = 0', 1)) {
         // Avatar exists
         $binaryfile_id = $this->_db_list[0]['binaryfile_id'];
         _pcpin_loadClass('user');
         $user = new PCPIN_User($this);
         if ($user->_db_getList('id', 'id = ' . $user_id, 1)) {
             // User exists
             $user->_db_freeList();
             // Does user has primary avatar
             if ($this->_db_getList('id', 'user_id = ' . $user_id, 'primary = y', 1)) {
                 // There is already one primary avatar
                 $this->primary = 'n';
                 $this->_db_freeList();
             } else {
                 // This avatar will be primary
                 $this->primary = 'y';
             }
             // Insert avatar
             $this->id = 0;
             $this->user_id = $user_id;
             $this->binaryfile_id = $binaryfile_id;
             $result = $this->_db_insertObj();
         }
     }
     return $result;
 }
 /**
  * Global mute/unmute user
  * @param   int       $user_id              User ID
  * @param   int       $action               Action: (1: mute, 0: unmute)
  * @param   int       $mute_time            If user will be muted: For how many minutes (0 means permanent mute)
  * @param   string    $mute_reason          If user will be muted: Mute reason (optional)
  * @param   int       $muted_by_user_id     If user will be muted: ID of user who muted him
  * @param   int       $muted_by_username    If user will be muted: Nickname of user who muted him
  * @return  boolean  TRUE on success or FALSE on error
  */
 function globalMuteUnmute($user_id, $action = 1, $mute_time = 0, $mute_reason = '', $muted_by_user_id = 0, $muted_by_username = '')
 {
     $result = false;
     _pcpin_loadClass('user');
     $user = new PCPIN_User($this);
     if (!empty($user_id) && $user->_db_getList('id,global_muted_until,global_muted_permanently', 'id = ' . $user_id, 1)) {
         if ($action == 0) {
             // Unmute user
             $result = true;
             if ($user->_db_list[0]['global_muted_permanently'] == 'y' || $user->_db_list[0]['global_muted_until'] > '0000-00-00 00:00:00') {
                 // User is muted
                 $result = $user->_db_updateRow($user_id, 'id', array('global_muted_until' => '', 'global_muted_permanently' => 'n', 'global_muted_reason' => ''));
             }
         } elseif ($action == 1) {
             // Mute user
             $mute_reason = trim($mute_reason);
             $result = $user->_db_updateRow($user_id, 'id', array('global_muted_until' => empty($mute_time) ? '' : date('Y-m-d H:i:s', time() + $mute_time * 60), 'global_muted_permanently' => empty($mute_time) ? 'y' : 'n', 'global_muted_reason' => trim($mute_reason), 'global_muted_by' => trim($muted_by_user_id), 'global_muted_by_username' => trim($muted_by_username)));
         }
     }
     return $result;
 }
 /**
  * "Kill" timed out sessions, set "Away" online status for sessions with ping older than (updater_interval+N) seconds
  */
 function _s_cleanUp()
 {
     // Store current state
     $this_vars = $this->_db_getFromObject();
     // Get sessions
     $sessions = array();
     $query = $this->_db_makeQuery(2100, date('Y-m-d H:i:s', time() - 1800), date('Y-m-d H:i:s', time() - $this->_conf_all['session_timeout']), date('Y-m-d H:i:s', time() - 5));
     $result = $this->_db_query($query);
     while ($data = $this->_db_fetch($result, MYSQL_ASSOC)) {
         $sessions[] = $data;
     }
     $this->_db_freeResult($result);
     $this->_db_freeList();
     if (!empty($sessions)) {
         _pcpin_loadClass('session');
         $session = new PCPIN_Session($this, '', true);
         foreach ($sessions as $sessiondata) {
             $session->_db_setObject($sessiondata);
             $session->_s_logOut();
         }
     }
     // Delete old messages
     if (!empty($this->_conf_all['message_lifetime'])) {
         _pcpin_loadClass('message');
         $message = new PCPIN_Message($this);
         $message->deleteMessages(null, time() - $this->_conf_all['message_lifetime']);
     }
     // Unmute users
     _pcpin_loadClass('user');
     $user = new PCPIN_User($this);
     if ($user->_db_getList('id', 'global_muted_until > 0000-00-00 00:00:00', 'global_muted_until < ' . date('Y-m-d H:i:s'))) {
         $user_ids = $user->_db_list;
         $user->_db_freeList();
         foreach ($user_ids as $data) {
             // Unmute user
             $user->globalMuteUnmute($data['id'], 0);
         }
         // Add system messages
         _pcpin_loadClass('message');
         $message = new PCPIN_Message($this);
         foreach ($user_ids as $data) {
             if ($this->_db_getList('_s_room_id', '_s_user_id = ' . $data['id'], 1)) {
                 $message->addMessage(10111, 'n', 0, '', 0, 0, $data['id'] . '/0', date('Y-m-d H:i:s'), 0, '');
             }
         }
     }
     // Clean unbanned users
     if ($result = $this->_db_query($this->_db_makeQuery(2080, date('Y-m-d H:i:s')))) {
         $this->_db_freeResult($result);
     }
     // Clean not activated email addresses
     if (!empty($this->_conf_all['activate_new_emails'])) {
         _pcpin_loadClass('user');
         $user = new PCPIN_User($this);
         if ($user->_db_getList('id', 'email_new != ', 'email_new_date < ' . date('Y-m-d H:i:s', time() - 3600 * $this->_conf_all['new_email_activation_timeout']))) {
             $user_ids = $user->_db_list;
             $user->_db_freeList();
             foreach ($user_ids as $data) {
                 $user->_db_updateRow($data['id'], 'id', array('email_new' => '', 'email_new_date' => '', 'email_new_activation_code' => ''));
             }
         }
     }
     // Delete idle and/or not activated user accounts
     if (!PCPIN_SLAVE_MODE && $this->_conf_all['activate_new_accounts'] == 1 || !empty($this->_conf_all['account_pruning'])) {
         _pcpin_loadClass('user');
         $user = new PCPIN_User($this);
         $query = $this->_db_makeQuery(2060, $this->_conf_all['activate_new_accounts'] == 1 ? date('Y-m-d H:i:s', time() - 3600 * $this->_conf_all['new_account_activation_timeout']) : '', !empty($this->_conf_all['account_pruning']) ? date('Y-m-d H:i:s', time() - $this->_conf_all['account_pruning'] * 86400) : '');
         $user_ids = array();
         if ($result = $this->_db_query($query)) {
             while ($data = $this->_db_fetch($result, MYSQL_ASSOC)) {
                 $user_ids[] = $data['id'];
             }
             $this->_db_freeResult($result);
         }
         if (!empty($user_ids)) {
             foreach ($user_ids as $id) {
                 $user->deleteUser($id);
             }
         }
     }
     // Delete empty and timed out user rooms
     _pcpin_loadClass('room');
     $room = new PCPIN_Room($this);
     if ($room->_db_getList('id', 'type = u', 'users_count <= 0', 'last_ping < ' . date('Y-m-d H:i:s', time() - $this->_conf_all['empty_userroom_lifetime']))) {
         $rooms = $room->_db_list;
         $room->_db_freeList();
         foreach ($rooms as $data) {
             $room->deleteRoom($data['id']);
         }
         // Add system message
         _pcpin_loadClass('message');
         $message = new PCPIN_Message($this);
         $message->addMessage(1100, 'n', 0, '', 0, 0, '-', date('Y-m-d H:i:s'), 0, '');
     }
     // Delete old logs
     if (!empty($this->_conf_all['logging_period'])) {
         _pcpin_loadClass('message_log');
         $message_log = new PCPIN_Message_Log($this);
         $message_log->cleanUp();
     }
     // Restore current state
     $this->_db_setObject($this_vars);
 }
 /**
  * Get default nickname. If user has no nicknames, his username will be returned.
  * @param   int   $user_id    User ID
  * @return  string
  */
 function getDefaultNickname($user_id)
 {
     $nickname = '';
     if (!empty($user_id)) {
         if (!$this->_db_getList('nickname', 'user_id = ' . $user_id, 'default = y', 1)) {
             $this->_db_getList('nickname', 'user_id = ' . $user_id, 1);
         }
         if (!empty($this->_db_list)) {
             $nickname = $this->_db_list[0]['nickname'];
             $this->_db_freeList();
         }
         if ($nickname == '') {
             // User has no nicknames, get username
             $usr = new PCPIN_User($this);
             if ($usr->_db_getList('login', 'id =# ' . $user_id, 1)) {
                 $nickname = $usr->_db_list[0]['login'];
                 $usr->_db_freeList();
             }
         }
     }
     return $nickname;
 }