/**
 *    This file is part of "PCPIN Chat 6".
 *
 *    "PCPIN Chat 6" is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    "PCPIN Chat 6" is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    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($avatar_id) || !pcpin_ctype_digit($avatar_id)) {
    $avatar_id = 0;
}
_pcpin_loadClass('avatar');
$avatar = new PCPIN_Avatar($session);
if (!empty($current_user->id) && $current_user->is_admin === 'y' && !empty($avatar_id) && $avatar->_db_getList('primary', 'id = ' . $avatar_id, 'user_id = 0', 1)) {
    // Avatar exists
    $xmlwriter->setHeaderMessage('OK');
    $xmlwriter->setHeaderStatus(0);
    if ($avatar->_db_list[0]['primary'] != 'y') {
        $avatar->setDefaultAvatarGallery($avatar_id);
    }
    $avatar->_db_freeList();
}
 *
 *    "PCPIN Chat 6" is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    "PCPIN Chat 6" is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
_pcpin_loadClass('avatar');
$avatar = new PCPIN_Avatar($session);
if (empty($profile_user_id) || $profile_user_id != $current_user->id && $current_user->is_admin !== 'y') {
    $profile_user_id = $current_user->id;
}
if (empty($avatar_id) || !is_scalar($avatar_id)) {
    $avatar_id = 0;
}
if (!empty($profile_user_id) && !empty($avatar_id) && $avatar->_db_getList('id,binaryfile_id', 'id = ' . $avatar_id, 'user_id = 0', 1)) {
    $binaryfile_id = $avatar->_db_list[0]['binaryfile_id'];
    // Check avatars limit
    $avatar->_db_getList('COUNT', 'user_id = ' . $profile_user_id);
    if ($avatar->_db_list_count >= $session->_conf_all['avatars_max_count']) {
        // Limit reached
        $xmlwriter->setHeaderMessage(str_replace('[NUMBER]', $session->_conf_all['avatars_max_count'], $l->g('avatars_limit_reached')));
        $xmlwriter->setHeaderStatus(1);
    } elseif ($avatar->_db_getList('id', 'user_id = ' . $profile_user_id, 'binaryfile_id = ' . $binaryfile_id, 1)) {
 *    (at your option) any later version.
 *
 *    "PCPIN Chat 6" is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
/**
 * Delete avatar
 * @param   int   $avatar_id      Avatar ID
 */
if (empty($avatar_id) || !pcpin_ctype_digit($avatar_id)) {
    $avatar_id = 0;
}
if (empty($profile_user_id) || $profile_user_id != $current_user->id && $current_user->is_admin !== 'y') {
    $profile_user_id = $current_user->id;
}
_pcpin_loadClass('avatar');
$avatar = new PCPIN_Avatar($session);
_pcpin_loadClass('message');
$msg = new PCPIN_Message($session);
if (!empty($profile_user_id) && !empty($avatar_id)) {
    // Delete avatar
    $avatar->deleteAvatar($profile_user_id, $avatar_id);
    $xmlwriter->setHeaderMessage($l->g('avatar_deleted'));
    $xmlwriter->setHeaderStatus(0);
    $msg->addMessage(1010, 'n', 0, '', $session->_s_room_id, 0, $profile_user_id);
}
 *
 *    "PCPIN Chat 6" is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    "PCPIN Chat 6" is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
_pcpin_loadClass('avatar');
$avatar = new PCPIN_Avatar($session);
if (empty($profile_user_id) || $profile_user_id != $current_user->id && $current_user->is_admin !== 'y') {
    $profile_user_id = $current_user->id;
}
if (empty($avatar_id) || !is_scalar($avatar_id)) {
    $avatar_id = 0;
}
if (!empty($avatar_id) && $avatar->_db_getList('id,primary', 'id = ' . $avatar_id, 'user_id = ' . $profile_user_id, 1)) {
    // Avatar exists and belongs to user
    $xmlwriter->setHeaderMessage('OK');
    $xmlwriter->setHeaderStatus(0);
    if ($avatar->_db_list[0]['primary'] != 'y') {
        $avatar->setPrimaryAvatar($profile_user_id, $avatar_id);
        _pcpin_loadClass('message');
        $msg = new PCPIN_Message($session);
        $msg->addMessage(1010, 'n', 0, '', $session->_s_room_id, 0, $profile_user_id);
if (!empty($f_data) && is_array($f_data) && isset($f_data['error']) && isset($f_data['tmp_name']) && isset($f_data['size'])) {
    $filename = $f_data['name'];
    $upload_status = array('code' => 10, 'message' => $l->g('error'));
    if ($f_data['error'] == UPLOAD_ERR_NO_FILE || $f_data['error'] == UPLOAD_ERR_OK && empty($f_data['size'])) {
        // No file was uploaded or file is empty
        $upload_status = array('code' => -1, 'message' => $l->g('file_upload_error'));
    } elseif ($f_data['error'] != UPLOAD_ERR_OK) {
        // File upload error
        $upload_status = array('code' => 100, 'message' => $l->g('file_upload_error'));
    } else {
        switch ($f_target) {
            case 'avatar':
                // New Avatar
                // Check avatars number limit
                _pcpin_loadClass('avatar');
                $avatar = new PCPIN_Avatar($session);
                $avatar->_db_getList('COUNT', 'user_id = ' . $profile_user_id);
                if ($avatar->_db_list_count >= $session->_conf_all['avatars_max_count']) {
                    // Limit reached
                    $upload_status = array('code' => 10, 'message' => str_replace('[NUMBER]', $session->_conf_all['avatars_max_count'], $l->g('avatars_limit_reached')));
                } else {
                    // Check image data
                    $img_data = null;
                    switch (PCPIN_Image::checkImage($img_data, $f_data['tmp_name'], $session->_conf_all['avatar_image_types'], $session->_conf_all['avatar_max_width'], $session->_conf_all['avatar_max_height'], $session->_conf_all['avatar_max_filesize'], false)) {
                        case PCPIN_IMAGE_CHECK_OK:
                            // Image OK
                            $upload_status = array('code' => 0, 'message' => $l->g('avatar_uploaded'));
                            break;
                        case PCPIN_IMAGE_CHECK_ERROR_FILE:
                            // File does not exists / not readable
                            $upload_status = array('code' => 100, 'message' => $l->g('file_upload_error'));
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    "PCPIN Chat 6" is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
/**
 * Get avatars owned by specified user
 * @param   int   $user_id    Optional. User ID. If empty, then $current_user->id will be assumed.
 */
_pcpin_loadClass('avatar');
$avatar = new PCPIN_Avatar($session);
if (empty($profile_user_id)) {
    $profile_user_id = $current_user->id;
}
$avatars_xml = array();
if (!empty($profile_user_id)) {
    $xmlwriter->setHeaderMessage('OK');
    $xmlwriter->setHeaderStatus(0);
    $avatars = $avatar->getAvatars($profile_user_id);
    foreach ($avatars as $avatar_data) {
        $avatars_xml[] = array('id' => $avatar_data['id'], 'primary' => $avatar_data['primary'], 'binaryfile_id' => $avatar_data['binaryfile_id'], 'width' => $avatar_data['width'], 'height' => $avatar_data['height']);
    }
}
$xmlwriter->setData(array('avatar' => $avatars_xml));
 /**
  * Delete user
  * @param   int   $user_id    User ID
  * @return  boolean   TRUE on success or FALSE on error
  */
 function deleteUser($user_id)
 {
     $result = false;
     if (!empty($user_id) && $this->_db_getList('id', 'id = ' . $user_id, 1)) {
         // Delete user
         if ($result = $this->_db_deleteRow($user_id)) {
             // Delete all avatars owned by user
             _pcpin_loadClass('avatar');
             $avatar = new PCPIN_Avatar($this);
             $avatar->deleteAvatar($user_id);
             // Delete all nicknames owned by user
             _pcpin_loadClass('nickname');
             $nickname = new PCPIN_Nickname($this);
             $nickname->deleteAllNickname($user_id);
             // Delete all messages sent TO this user
             _pcpin_loadClass('message');
             $message = new PCPIN_Message($this);
             if ($message->_db_getList('id', 'target_user_id = ' . $user_id)) {
                 $message_ids = array();
                 foreach ($message->_db_list as $data) {
                     $message_ids[] = $data['id'];
                 }
                 $message->_db_freeList();
                 $message->deleteMessages($message_ids);
             }
             // Delete userdata
             _pcpin_loadClass('userdata');
             $userdata = new PCPIN_UserData($this);
             $userdata->deleteUserData($user_id);
             // Update all users who ignored deleted user
             if ($res = $this->_db_query($this->_db_makeQuery(2050, $user_id))) {
                 $this->_db_freeResult($res);
             }
         }
     }
     return $result;
 }
    header('Location: ' . PCPIN_FORMLINK . '?' . md5(microtime()));
    die;
}
// Delete temporary message attachments
_pcpin_loadClass('tmpdata');
$tmpdata = new PCPIN_TmpData($session);
$tmpdata->deleteUserRecords($current_user->id, 3);
// Get room background image
_pcpin_loadClass('room');
$room = new PCPIN_Room($session);
$room->_db_getList('background_image', 'id = ' . $session->_s_room_id, 1);
$background_image = $room->_db_list[0]['background_image'];
$room->_db_freeList();
// Get default avatar
_pcpin_loadClass('avatar');
$avatar = new PCPIN_Avatar($session);
$avatars = $avatar->getAvatars($current_user->id, 1);
if (!empty($avatars)) {
    $avatar_bid = $avatars[0]['binaryfile_id'];
} else {
    $avatar_bid = 0;
}
unset($avatars);
// JS files to load
$_js_files[] = './js/user.js';
$_js_files[] = './js/message_queue.js';
$_js_files[] = './js/chat_room.js';
$_js_files[] = './js/commands.js';
$_js_lng[] = 'user_entered_this_room';
$_js_lng[] = 'user_left_this_room';
$_js_lng[] = 'nickname_matches_multiple';
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
if (!is_object($session) || empty($session->_s_user_id)) {
    header('Location: ' . PCPIN_FORMLINK . '?' . md5(microtime()));
    die;
}
if (empty($session->_conf_all['avatar_gallery'])) {
    // Avatar gallery is disabled
    die('access denied');
}
if (empty($profile_user_id) || $current_user->is_admin !== 'y') {
    $profile_user_id = $current_user->id;
}
// Get avatars
_pcpin_loadClass('avatar');
$avatar = new PCPIN_Avatar($session);
$avatars = $avatar->getGalleryAvatars();
// JS files
$_js_files[] = './js/avatar_gallery.js';
$_js_lng[] = 'avatar';
$_body_onload[] = 'initAvatarGallery(' . addslashes(htmlspecialchars($profile_user_id)) . ')';
$_window_title .= ' ' . PCPIN_WINDOW_TITLE_SEPARATOR . ' ' . $l->g('avatar_gallery');
// Init template
_pcpin_loadClass('pcpintpl');
$tpl = new PcpinTpl();
$tpl->setBasedir('./tpl');
$tpl->readTemplatesFromFile('./avatar_gallery.tpl');
// Add language expressions to template
foreach ($tpl->tpl_vars_plain as $var) {
    if (0 === strpos($var, 'LNG_')) {
        $var = strtolower($var);
 *    GNU General Public License for more details.
 *
 *    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 (function_exists('debug_backtrace')) {
    $_pcpin_dbt = debug_backtrace();
    if (is_array($_pcpin_dbt) && empty($_pcpin_dbt[0])) {
        die('Access denied');
    }
    unset($_pcpin_dbt);
}
_pcpin_loadClass('binaryfile');
$binaryfile = new PCPIN_BinaryFile($session);
_pcpin_loadClass('avatar');
$avatar = new PCPIN_Avatar($session);
_pcpin_loadClass('room');
$room = new PCPIN_Room($session);
_pcpin_loadClass('category');
$category = new PCPIN_Category($session);
if (PCPIN_SLAVE_MODE && !empty($_pcpin_slave_userdata) && !empty($session) && is_object($session)) {
    $language_id = $_pcpin_slave_userdata['language'];
    if ($_pcpin_slave_userdata['is_guest'] !== 'n') {
        // User is guest
        $guest_login = 1;
    } else {
        // Registered user
        // Check user
        if ($current_user->_db_getList('login = '******'login'], 1)) {
            // User exists
            $current_user_set = $current_user->_db_list[0];
// Get available languages
$languages = array();
if (!empty($session->_conf_all['allow_language_selection'])) {
    $languages = $l->getLanguages(false);
}
if (empty($profile_user_id)) {
    $profile_user_id = $current_user->id;
}
if ($profile_user_id != $current_user->id && ($current_user->is_admin !== 'y' || !$current_user->_db_getList('login', 'id =# ' . $profile_user_id, 1))) {
    $profile_user_id = $current_user->id;
}
// Display "Avatar gallery" link?
$show_avatar_gallery_link = false;
if (!empty($session->_conf_all['avatar_gallery'])) {
    _pcpin_loadClass('avatar');
    $avatar = new PCPIN_Avatar($session);
    if ($avatar->_db_getList('COUNT', 'user_id = 0')) {
        if ($avatar->_db_list_count > 1) {
            // There are more that one default avatar
            $show_avatar_gallery_link = true;
        }
        $avatar->_db_freeList();
    }
}
$_body_onload[1000000] = 'initProfile(' . $session->_conf_all['nickname_length_min'] . ',' . $session->_conf_all['nickname_length_max'] . ',' . '\'' . $session->_conf_all['default_nickname_color'] . '\',' . $session->_conf_all['avatars_max_count'] . ',' . $session->_conf_all['nicknames_max_count'] . ',' . $profile_user_id . ',' . ($show_avatar_gallery_link ? 'true' : 'false') . ',' . ($session->_conf_all['allow_language_selection'] ? 'true' : 'false') . ',' . ($session->_conf_all['allow_account_unsubscribe'] && $profile_user_id == $current_user->id ? 'true' : 'false') . ')';
// Init template
_pcpin_loadClass('pcpintpl');
$tpl = new PcpinTpl();
$tpl->setBasedir('./tpl');
$tpl->readTemplatesFromFile('./profile_main.tpl');
// JS files
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
/**
 * Delete avatar
 * @param   int   $avatar_id      Avatar ID
 */
if (empty($avatar_id) || !pcpin_ctype_digit($avatar_id)) {
    $avatar_id = 0;
}
_pcpin_loadClass('avatar');
$avatar = new PCPIN_Avatar($session);
if (!empty($current_user->id) && $current_user->is_admin === 'y' && !empty($avatar_id)) {
    // Check for other gallery avatars
    if ($avatar->_db_getList('id', 'user_id = 0', 'id != ' . $avatar_id, 1)) {
        $avatar->_db_freeList();
        // Delete avatar
        if ($avatar->deleteGalleryAvatar($avatar_id)) {
            $xmlwriter->setHeaderMessage($l->g('avatar_deleted'));
            $xmlwriter->setHeaderStatus(0);
        } else {
            $xmlwriter->setHeaderMessage($l->g('error'));
            $xmlwriter->setHeaderStatus(1);
        }
    } else {
        $xmlwriter->setHeaderMessage($l->g('delete_last_avatar_error'));
        $xmlwriter->setHeaderStatus(1);
 *
 *    "PCPIN Chat 6" is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    "PCPIN Chat 6" is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
_pcpin_loadClass('avatar');
$avatar = new PCPIN_Avatar($session);
_pcpin_loadClass('tmpdata');
$tmpdata = new PCPIN_TmpData($session);
_pcpin_loadClass('binaryfile');
$binaryfile = new PCPIN_BinaryFile($session);
$errortext = array();
if (!empty($current_user->id) && $current_user->is_admin === 'y') {
    $tmpdata->_db_getList('id, binaryfile_id', 'user_id = ' . $current_user->id, 'type = 4', 1);
    if (empty($tmpdata->_db_list)) {
        $errortext[] = $l->g('smilie_image_empty_error');
    } else {
        $tmpdata_id = $tmpdata->_db_list[0]['id'];
        $binaryfile_id = $tmpdata->_db_list[0]['binaryfile_id'];
        $tmpdata->_db_freeList();
    }
    if (empty($errortext)) {