/**
  * Create new session
  * @param   int       $user_id            Optional ID of session owner user
  * @param   int       $last_message_id    ID of last message received by session owner
  * @param   int       $language_id        Optional. Selected language. If empty, then default language will be used.
  * @param   string    $backend_login      Optional. 'y', if user is Administrator and logged directly into Admin Backend.
  */
 function _s_newSession($user_id = 0, $last_message_id = 0, $language_id = 0, $backend_login = '******')
 {
     $ok = false;
     if ($backend_login !== 'y' && $backend_login !== 'n') {
         $backend_login = '******';
     }
     $max_attempts = 100;
     do {
         // Generate new session ID
         $this->_s_id = PCPIN_Common::randomString(PCPIN_SID_LENGTH, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789');
         // Check new session ID
         if (!$this->_db_getList('_s_id', '_s_id = ' . $this->_s_id, 1)) {
             // New session ID is unique
             // Check language
             _pcpin_loadClass('language');
             $language = new PCPIN_Language($this);
             if (empty($this->_conf_all['allow_language_selection']) || 0 == ($language_id = $language->checkLanguage($language_id))) {
                 $language_id = $this->_conf_all['default_language'];
             }
             // Set all object properties up
             $this->_s_ip = PCPIN_CLIENT_IP;
             $this->_s_client_agent_name = PCPIN_CLIENT_AGENT_NAME;
             $this->_s_client_agent_version = PCPIN_CLIENT_AGENT_VERSION;
             $this->_s_client_os = PCPIN_CLIENT_OS;
             $this->_s_created = date('Y-m-d H:i:s');
             $this->_s_last_ping = date('Y-m-d H:i:s');
             $this->_s_language_id = $language_id;
             $this->_s_user_id = $user_id;
             $this->_s_security_code = md5(PCPIN_Common::randomString(mt_rand(100, 255)));
             $this->_s_security_code_img = '';
             $this->_s_room_id = 0;
             $this->_s_room_date = '';
             $this->_s_last_message_id = $last_message_id;
             $this->_s_last_sent_message_time = '0000-00-00 00:00:00';
             $this->_s_last_sent_message_hash = '';
             $this->_s_last_sent_message_repeats_count = 0;
             $this->_s_online_status = 1;
             $this->_s_online_status_message = '';
             $this->_s_kicked = 'n';
             $this->_s_stealth_mode = 'n';
             $this->_s_backend = $backend_login;
             $this->_s_page_unloaded = 'n';
             // Save session into database
             $ok = $this->_db_insertObj();
         }
         $max_attempts--;
     } while ($ok !== true && $max_attempts > 0);
     $this->_db_freeList();
     if (!$ok) {
         PCPIN_Common::dieWithError(-1, '<b>Fatal error</b>: Failed to create new session');
     }
 }
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
if (empty($set_language_id) || !is_scalar($set_language_id)) {
    $set_language_id = $session->_s_language_id;
}
if (empty($profile_user_id) || $profile_user_id != $current_user->id && $current_user->is_admin !== 'y') {
    $profile_user_id = $current_user->id;
}
if ($profile_user_id != $current_user->id) {
    $profile_user = new PCPIN_User($session);
    $profile_user->_db_loadObj($profile_user_id);
} else {
    $profile_user =& $current_user;
}
if (is_object($session) && !empty($profile_user->id)) {
    $xmlwriter->setHeaderMessage('OK');
    $xmlwriter->setHeaderStatus(0);
    if (!empty($session->_conf_all['allow_language_selection'])) {
        _pcpin_loadClass('language');
        $profile_language = new PCPIN_Language($session);
        if ($set_language_id == $profile_language->checkLanguage($set_language_id)) {
            $profile_user->language_id = $set_language_id;
            $profile_user->_db_updateObj($profile_user->id, 'id');
            if ($profile_user_id == $session->_s_user_id) {
                $session->_s_updateSession($session->_s_id, true, true, $set_language_id);
            }
        }
    }
}