// Are there new messages in request?
 if (!empty($new_messages) && is_array($new_messages)) {
     // There are some new messages from user
     // Need to change online status?
     //if ($session->_s_online_status!=1) {
     //  $session->_s_setOnlineStatus(1, $l->g('online_status_1'));
     //}
     if ($session->_s_online_status == 2 || $session->_s_online_status == 3) {
         $session->_s_setOnlineStatus(1, $l->g('online_status_1'));
     }
     foreach ($new_messages as $data) {
         if (isset($data['body'])) {
             // Attachments?
             _pcpin_loadClass('tmpdata');
             $tmpdata = new PCPIN_TmpData($session);
             $tmpdata->_db_getList('user_id = ' . $current_user->id, 'type = 3');
             $tmpdata_list = $tmpdata->_db_list;
             $tmpdata->_db_freeList();
             if (!empty($tmpdata_list)) {
                 $tmpdata->deleteUserRecords($current_user->id, 3, 0, true);
             }
             $data['body'] = trim($data['body']);
             if ($data['body'] != '' || !empty($tmpdata_list)) {
                 $type = isset($data['type']) ? $data['type'] : 0;
                 $offline = isset($data['offline']) ? $data['offline'] : 'n';
                 $target_room_id = isset($data['target_room_id']) ? $data['target_room_id'] : $session->_s_room_id;
                 $target_user_id = isset($data['target_user_id']) ? $data['target_user_id'] : 0;
                 $body = _pcpin_substr($data['body'], 0, $session->_conf_all['message_length_max']);
                 $privacy = isset($data['privacy']) ? $data['privacy'] : 0;
                 $css_properties = isset($data['css_properties']) ? $data['css_properties'] : '';
                 // Initial state: User must be not global muted
$tmpdata = new PCPIN_TmpData($session);
_pcpin_loadClass('binaryfile');
$binaryfile = new PCPIN_BinaryFile($session);
$errortext = array();
if (!isset($code)) {
    $code = '';
}
if (!isset($description)) {
    $description = '';
}
if (!empty($current_user->id) && $current_user->is_admin === 'y') {
    $xmlwriter->setHeaderStatus(1);
    if ($code == '') {
        $errortext[] = $l->g('smilie_code_empty_error');
    }
    $tmpdata->_db_getList('id, binaryfile_id', 'user_id = ' . $current_user->id, 'type = 2', 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 ($smilie->_db_getList('code = ' . $code, 1)) {
        $errortext[] = $l->g('smilie_code_already_exists_error');
        $smilie->_db_freeList();
    }
    if (empty($errortext)) {
        // Save smilie
        if ($smilie->addSmilie($binaryfile_id, $code, $description)) {
            // Delete temporary data
 $description = trim($description);
 if (empty($category_id) || !$category->_db_getList('id', 'id = ' . $category_id, 1)) {
     $errortext[] = $l->g('select_category');
 } elseif ($name == '') {
     $errortext[] = $l->g('room_name_empty');
 } elseif ($room->_db_getList('category_id = ' . $category_id, 'name LIKE ' . $name, 1)) {
     $errortext[] = str_replace('[NAME]', $name, $l->g('room_already_exists_in_category'));
 } elseif (!empty($password_protect) && !empty($change_password) && _pcpin_strlen($password) < 3) {
     $errortext[] = $l->g('password_too_short');
 }
 if (!empty($errortext)) {
     $xmlwriter->setHeaderStatus(1);
     $xmlwriter->setHeaderMessage(implode("\n", $errortext));
 } else {
     // Check image
     if (!empty($image) && $tmpdata->_db_getList('binaryfile_id', 'user_id = ' . $session->_s_user_id, 'type = 1', 1)) {
         $binaryfile_id = $tmpdata->_db_list[0]['binaryfile_id'];
         $tmpdata->_db_freeList();
     } else {
         $binaryfile_id = 0;
     }
     $tmpdata->deleteUserRecords($session->_s_user_id, 1, 0, true);
     if (!empty($password_protect)) {
         $room_password = md5(base64_decode($password));
     } else {
         $room_password = '';
     }
     $room->createRoom($category_id, $type, $name, $description, $default_message_color, $password, $binaryfile_id);
     $xmlwriter->setHeaderStatus(0);
     $xmlwriter->setHeaderMessage(str_replace('[NAME]', $name, $l->g('room_created')));
 }
         // Duplicate room name
         $xmlwriter->setHeaderStatus(1);
         $errortext[] = str_replace('[NAME]', $name, $l->g('room_already_exists_in_category'));
     }
     if (!empty($password_protect)) {
         $password = base64_decode($password);
         if (_pcpin_strlen($password) < 3) {
             $xmlwriter->setHeaderStatus(1);
             $errortext[] = $l->g('password_too_short');
         }
     }
 }
 if (empty($errortext)) {
     // Check image
     $background_image = 0;
     if (!empty($image) && $tmpdata->_db_getList('id, binaryfile_id', 'user_id = ' . $current_user->id, 'type = 1', 1)) {
         // There is an image
         $background_image = $tmpdata->_db_list[0]['binaryfile_id'];
         // Delete temporary data
         $tmpdata->_db_freeList();
         $tmpdata->deleteUserRecords($session->_s_user_id, 1, 0, true);
     }
     if ($room->createRoom($category_id, 'u', $name, $description, $session->_conf_all['default_message_color'], !empty($password_protect) ? $password : '', $background_image)) {
         $xmlwriter->setHeaderStatus(0);
         $xmlwriter->setHeaderMessage('OK');
         // Room created
         $room_id = $room->id;
         // Add system message
         $msg->addMessage(1100, 'n', 0, '', 0, 0, '-', date('Y-m-d H:i:s'), 0, '');
     } else {
         $xmlwriter->setHeaderStatus(1);
 // Message attachment
 $msg_attachments_limit = $session->_conf_all['msg_attachments_limit'];
 if (empty($session->_s_room_id)) {
     // User is not in room
     $upload_status = array('code' => 100, 'message' => $l->g('file_upload_error'));
 } elseif (!file_exists($f_data['tmp_name']) || !is_file($f_data['tmp_name']) || !is_readable($f_data['tmp_name'])) {
     // File upload error
     $upload_status = array('code' => 100, 'message' => $l->g('file_upload_error'));
 } elseif (filesize($f_data['tmp_name']) > $session->_conf_all['msg_attachments_maxsize'] * 1024) {
     // File too large
     $upload_status = array('code' => 400, 'message' => str_replace('[SIZE]', $session->_conf_all['msg_attachments_maxsize'] * 1024, $l->g('file_too_large')));
 } else {
     // Check attachments limit
     _pcpin_loadClass('tmpdata');
     $tmpdata = new PCPIN_TmpData($session);
     $tmpdata->_db_getList('COUNT', 'type = 3', 'user_id = ' . $session->_s_user_id);
     if ($tmpdata->_db_list_count >= $msg_attachments_limit) {
         // Max attachments limit reached
         $upload_status = array('code' => 100, 'message' => $l->g('file_upload_error'));
     } else {
         $upload_status = array('code' => 0, 'message' => 'OK');
     }
 }
 if ($upload_status['code'] === 0) {
     // Get MIME type
     $mime_type = $f_data['type'];
     // TODO: detect real MIME type
     _pcpin_loadClass('binaryfile');
     $binaryfile = new PCPIN_BinaryFile($session);
     if ($binaryfile->newBinaryFile(file_get_contents($f_data['tmp_name']), $mime_type, 0, 0, 'room|' . $session->_s_room_id)) {
         $binaryfile_id = $binaryfile->id;
 if (empty($category_id) || !$category->_db_getList('id', 'id = ' . $category_id, 1)) {
     $errortext[] = $l->g('select_category');
 } elseif ($name == '') {
     $errortext[] = $l->g('room_name_empty');
 } elseif ($room->_db_getList('id != ' . $room_id, 'category_id = ' . $category_id, 'name LIKE ' . $name, 1)) {
     $errortext[] = str_replace('[NAME]', $name, $l->g('room_already_exists_in_category'));
 } elseif (!empty($password_protect) && !empty($change_password) && _pcpin_strlen($password) < 3) {
     $errortext[] = $l->g('password_too_short');
 }
 if (!empty($errortext)) {
     $xmlwriter->setHeaderStatus(1);
     $xmlwriter->setHeaderMessage(implode("\n", $errortext));
 } else {
     // Check image
     if (!empty($image)) {
         if ($tmpdata->_db_getList('binaryfile_id', 'user_id = ' . $session->_s_user_id, 'type = 1', 1)) {
             // New image uploaded
             $binaryfile_id = $tmpdata->_db_list[0]['binaryfile_id'];
             $tmpdata->_db_freeList();
         } elseif ($room_data['background_image'] == $image) {
             $binaryfile_id = $image;
         } else {
             $binaryfile_id = 0;
         }
     } else {
         $binaryfile_id = 0;
     }
     $tmpdata->deleteUserRecords($session->_s_user_id, 1, 0, true);
     $xmlwriter->setHeaderStatus(0);
     $xmlwriter->setHeaderMessage($l->g('changes_saved'));
     $room_password = null;