*
 *    "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/>.
 */
/**
 * Update moderator data
 */
_pcpin_loadClass('room');
$room = new PCPIN_Room($session);
_pcpin_loadClass('category');
$category = new PCPIN_Category($session);
if (!isset($moderator_user_id) || !pcpin_ctype_digit($moderator_user_id)) {
    $moderator_user_id = 0;
}
if (!isset($categories) || !is_scalar($categories)) {
    $categories = '';
}
if (!isset($rooms) || !is_scalar($rooms)) {
    $rooms = '';
}
// Get client session
if (is_object($session) && !empty($moderator_user_id) && !empty($current_user->id) && $current_user->is_admin === 'y') {
    if ($current_user->_db_getList('id = ' . $moderator_user_id, 1)) {
        $xmlwriter->setHeaderMessage('OK');
        $xmlwriter->setHeaderStatus(0);
        $current_user->_db_freeList();
 */
/**
 * Get current chat state
 * @param   int       $room_id              ID of the room where the request was sent from
 * @param   boolean   $first_request        If TRUE, then full room information and full userlist will be returned,
 *                                          returned messages count will be limited by 'init_display_messages_count'
 * @param   boolean   $full_request         If TRUE, then full room information and full userlist will be returned
 * @param   int       $get_last_msgs        If > 0, then last X messages will be returned (including already delivered messages)
 * @param   boolean   $pref_timestamp       Optional. Current state of client's "Display message timestamp" preference
 * @param   boolean   $pref_allow_sounds    Optional. Current state of client's "Allow sounds" preference
 * @param   boolean   $pref_message_color   Optional. Current message color
 */
_pcpin_loadClass('message');
$msg = new PCPIN_Message($session);
_pcpin_loadClass('category');
$category = new PCPIN_Category($session);
_pcpin_loadClass('nickname');
$nickname = new PCPIN_Nickname($session);
_pcpin_loadClass('room');
$room = new PCPIN_Room($session);
_pcpin_loadClass('invitation');
$invitation = new PCPIN_Invitation($session);
_pcpin_loadClass('avatar');
$avatar = new PCPIN_Avatar($session);
_pcpin_loadClass('badword');
$badword = new PCPIN_Badword($session);
_pcpin_loadClass('banner');
$banner = new PCPIN_Banner($session);
$default_nicknames = array();
// cached nicknames
$last_sent_message_time = $session->_s_last_sent_message_time <= '0000-00-00 00:00:00' ? 0 : PCPIN_Common::datetimeToTimestamp($session->_s_last_sent_message_time);
 *
 *    "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('category');
$category = new PCPIN_Category($session);
$parent_id = 0;
// todo
if (!isset($name) || !is_scalar($name)) {
    $name = '';
}
if (!isset($description) || !is_scalar($description)) {
    $description = '';
}
if (!isset($creatable_rooms) || !is_scalar($creatable_rooms)) {
    $creatable_rooms = 'n';
}
$errortext = array();
if (!empty($current_user->id) && $current_user->is_admin === 'y' && $session->_s_user_id == $current_user->id) {
    $errortext = array();
    $name = trim($name);
 *    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('room');
$room = new PCPIN_Room($session);
_pcpin_loadClass('category');
$category = new PCPIN_Category($session);
_pcpin_loadClass('tmpdata');
$tmpdata = new PCPIN_TmpData($session);
$type = 'p';
//todo
if (!isset($name) || !is_scalar($name)) {
    $name = '';
}
if (!isset($description) || !is_scalar($description)) {
    $description = '';
}
if (!isset($default_message_color) || !is_scalar($default_message_color)) {
    $default_message_color = $session->_conf_all['default_message_color'];
}
if (!isset($password_protect) || !pcpin_ctype_digit($password_protect)) {
    $password_protect = 0;
 *
 *    "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('category');
$category = new PCPIN_Category($session);
if (!isset($category_id) || !pcpin_ctype_digit($category_id)) {
    $category_id = 0;
}
if (!isset($action) || !is_scalar($action)) {
    $action = '';
}
if (!isset($dir) || !pcpin_ctype_digit($dir)) {
    $dir = 0;
}
$parent_id = 0;
//todo
if (!isset($name) || !is_scalar($name)) {
    $name = '';
}
if (!isset($description) || !is_scalar($description)) {
 *
 *    "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 member data
 */
_pcpin_loadClass('room');
$room = new PCPIN_Room($session);
_pcpin_loadClass('category');
$category = new PCPIN_Category($session);
if (!isset($profile_user_id)) {
    $profile_user_id = 0;
}
$member_xml = array();
$moderated_rooms = array();
$moderated_categories = array();
// Get client session
if (is_object($session) && !empty($profile_user_id) && !empty($current_user->id) && $current_user->is_admin === 'y') {
    if ($current_user->_db_getList('moderated_categories,moderated_rooms', 'id = ' . $profile_user_id, 1)) {
        $xmlwriter->setHeaderMessage('OK');
        $xmlwriter->setHeaderStatus(0);
        $member = $current_user->_db_list[0];
        $current_user->_db_freeList();
        $room_ids = array();
        // Get moderated categories
 *    "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/>.
 */
$room_id = 0;
_pcpin_loadClass('category');
$category = new PCPIN_Category($session);
_pcpin_loadClass('tmpdata');
$tmpdata = new PCPIN_TmpData($session);
_pcpin_loadClass('room');
$room = new PCPIN_Room($session);
_pcpin_loadClass('message');
$msg = new PCPIN_Message($session);
_pcpin_loadClass('binaryfile');
$binaryfile = new PCPIN_BinaryFile($session);
$errortext = array();
if (!isset($name)) {
    $name = '';
}
if (!isset($description)) {
    $description = '';
}
 *
 *    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 (!isset($recursion)) {
    $recursion = 1;
}
$invitations_arrived = 0;
$messages_arrived = 0;
$categories_tree = array();
if (!empty($current_user->id)) {
    $xmlwriter->setHeaderMessage('OK');
    $xmlwriter->setHeaderStatus(0);
    // Get room structure
    _pcpin_loadClass('category');
    $category = new PCPIN_Category($session);
    $categories_tree = $category->getTree($current_user->id, 0, !empty($recursion));
    // Are there any invitations or abuse reports?
    if (!empty($current_user->id)) {
        _pcpin_loadClass('invitation');
        $invitation = new PCPIN_Invitation($session);
        $invitations = $invitation->getNewInvitations($current_user->id, true);
        $invitations_arrived = !empty($invitations) ? 1 : 0;
        unset($invitations);
        _pcpin_loadClass('message');
        $msg = new PCPIN_Message($session);
        $messages = $msg->getNewMessages($session->_s_user_id);
        if (!empty($messages)) {
            $messages_arrived = 1;
        }
    }
 */
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];
            $current_user->_db_freeList();
            // Check wether user already logged in or not
            if ($session->_db_getList('_s_user_id = ' . $current_user_set['id'], '_s_online_status != 3', 1)) {
                // User already logged in
 *
 *    "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('category');
$category = new PCPIN_Category($session);
if (!isset($category_id) || !pcpin_ctype_digit($category_id)) {
    $category_id = 0;
}
if (!empty($current_user->id) && $current_user->is_admin === 'y' && $session->_s_user_id == $current_user->id) {
    $xmlwriter->setHeaderStatus(1);
    $xmlwriter->setHeaderMessage($l->g('error'));
    if (!empty($category_id) && $category->_db_getList('name', 'id = ' . $category_id)) {
        // Category exists
        $xmlwriter->setHeaderStatus(0);
        $category_name = $category->_db_list[0]['name'];
        $xmlwriter->setHeaderMessage(str_replace('[NAME]', $category_name, $l->g('category_deleted')));
        // Delete category
        $category->deleteCategory($category_id);
    }
}
 /**
  * Create new room
  * @param   int       $category_id            Parent category ID
  * @param   string    $type                   Room type
  * @param   string    $name                   Room name
  * @param   string    $description            Room description
  * @param   string    $default_message_color  Default message color
  * @param   string    $password               Password. If not empty, then room will be password-protected.
  * @param   int       $background_image       Binaryfile-ID of room background image
  * @return  boolean TRUE on success or FALSE on error
  */
 function createRoom($category_id = 0, $type = '', $name = '', $description = '', $default_message_color = '', $password = '', $background_image = 0)
 {
     $result = false;
     if (!empty($category_id) && $name != '') {
         $this->id = 0;
         $this->type = $type;
         $this->date_created = date('Y-m-d H:i:s');
         $this->name = $name;
         $this->category_id = $category_id;
         $this->description = $description;
         $this->users_count = 0;
         $this->default_message_color = $default_message_color;
         $this->password = $password != '' ? md5($password) : '';
         $this->background_image = $background_image;
         $this->last_ping = date('Y-m-d H:i:s');
         $this->listpos = 0;
         // Calculate listing position
         if ($this->_db_getList('listpos', 'category_id = ' . $category_id, 'listpos DESC', 1)) {
             $this->listpos = $this->_db_list[0]['listpos'] + 1;
         }
         if ($result = $this->_db_insertObj()) {
             $this->id = $this->_db_lastInsertID();
         }
         // Update "moderated_rooms" field by category moderators
         _pcpin_loadClass('category');
         $category = new PCPIN_Category($this);
         $moderators = $category->getModerators($category_id);
         if (!empty($moderators)) {
             _pcpin_loadClass('user');
             $user = new PCPIN_User($this);
             foreach ($moderators as $data) {
                 $rooms = array_unique(explode(',', trim($data['moderated_rooms'] . ',' . $this->id, ',')));
                 sort($rooms);
                 $user->_db_updateRow($data['id'], 'id', array('moderated_rooms' => implode(',', $rooms)));
             }
         }
     }
     return $result;
 }