autosave_transcripts()) {
                if ($this->result = $GLOBALS['db']->query('SELECT * FROM `sessions` WHERE `chatid`="'.$chatid.'"')) {
                    if ($this->result2 = $GLOBALS['db']->query('SELECT * FROM `traffic` WHERE `id`="'.$chatid.'"')) {
                        $GLOBALS['db']->query('INSERT INTO `transcripts` (`timestamp`, `chatid`, `operatorid`, `departmentid`, `guest`, `hostname`) VALUES (UNIX_TIMESTAMP(), "'.$chatid.'", "'.$GLOBALS['operator']->id().'", "'.$this->result[0]['departmentid'].'", "'.addslashes($GLOBALS['chat']->guestname($chatid)).'", "'.$this->result2[0]['hostname'].'")');
                    }
                }
            }
        }
        function insert($chatid, $message, $admin)
        {
            if ($GLOBALS['operator']->autosave_transcripts($_SESSION['hcl_'.$chatid]['operatorid'])) {
                if ($this->result = $GLOBALS['db']->query('SELECT * FROM `transcripts` WHERE `chatid`="'.$chatid.'" ORDER BY `id` DESC LIMIT 1')) {
                    $this->timestamp = gmdate('G:i:s', time() + (3600 * $GLOBALS['conf']['offset']));
                    if ($admin == 'true') {
                        $this->text = html_to_char(rawurldecode($message)).' ';
                        $this->text = preg_replace("/%%user%%/i", $_SESSION['hcl_'.$chatid]['guest'], $this->text);
                        $this->text = preg_replace("/url:(.*?) /i", "\\1 ", $this->text);
                        $this->text = preg_replace("/image:(.*?) /i", " ", $this->text);
                        $this->text = preg_replace("/email:(.*?) /i", "\\1 ", $this->text);
                        $this->text = preg_replace("/push:(.*?) /i", "Pushed Page: \\1 ", $this->text);
                        $this->text = substr($this->text, 0, -1);
                        $this->text = '('.$this->timestamp.') '.$_SESSION['hcl_'.$chatid]['operator'].': '.rawurldecode($this->text).'
 ", $this->text);
                        $this->text = preg_replace("/email:(.*?) /i", "\\1 ", $this->text);
                        $this->text = preg_replace("/push:(.*?) /i", "Pushed Page: \\1 ", $this->text);
                        $this->text = substr($this->text, 0, -1);
                        $this->text = '('.$this->timestamp.') '.$_SESSION['hcl_'.$chatid]['operator'].': '.rawurldecode($this->text).'
';
                        //$this->text = '('.$this->timestamp.') '.$_SESSION['hcl_'.$chatid]['operator'].': '.char_to_html($this->text).'
';
                    } else {
                        $this->text = '('.$this->timestamp.') '.$_SESSION['hcl_'.$chatid]['guest'].': '.rawurldecode($message).'
';
                    }
                    $this->chat = rawurldecode($this->result[0]['transcript']).$this->text;
                    $this->chat = rawurlencode($this->chat);
                    $GLOBALS['db']->query('UPDATE `transcripts` SET `transcript`="'.$this->chat.'", `timestamp`=UNIX_TIMESTAMP() WHERE `id`="'.$this->result[0]['id'].'"');
                }
            }
        }
        function build($chatid)
        {
            $this->chat = '';
            $this->result = $GLOBALS['db']->query('SELECT * FROM `traffic` WHERE `id`="'.$chatid.'"');
            $this->result2 = $GLOBALS['db']->query('SELECT * FROM `chat` WHERE `chatid`="'.$chatid.'" ORDER BY `timestamp` ASC');
            foreach ($this->result2 as $key => $val) {
                    $this->timestamp = gmdate('G:i:s', $this->result2[$key]['timestamp'] + (3600 * $GLOBALS['conf']['offset']));
                    if ($this->result2[$key]['x'] == 'o') {
                        $this->text = html_to_char(rawurldecode($this->result2[$key]['message'])).' ';
                        $this->text = preg_replace("/%%user%%/i", $_SESSION['hcl_'.$chatid]['guest'], $this->text);
                        $this->text = preg_replace("/url:(.*?) /i", "\\1 ", $this->text);
                        $this->text = preg_replace("/image:(.*?) /i", " ", $this->text);
                        $this->text = preg_replace("/email:(.*?) /i", "\\1 ", $this->text);
                        $this->text = preg_replace("/push:(.*?) /i", "Pushed Page: \\1 ", $this->text);
                        $this->text = substr($this->text, 0, -1);
                        $this->text = '('.$this->timestamp.') '.$_SESSION['hcl_'.$chatid]['operator'].': '.char_to_html($this->text).'
 ", $this->text);
                        $this->text = preg_replace("/email:(.*?) /i", "\\1 ", $this->text);
                        $this->text = preg_replace("/push:(.*?) /i", "Pushed Page: \\1 ", $this->text);
                        $this->text = substr($this->text, 0, -1);
                        $this->text = '('.$this->timestamp.') '.$_SESSION['hcl_'.$chatid]['operator'].': '.char_to_html($this->text).'
';
                    } else {
                        $this->text = '('.$this->timestamp.') '.$_SESSION['hcl_'.$chatid]['guest'].': '.rawurldecode($this->result2[$key]['message']).'
';
                    }
                $this->chat = $this->chat.$this->text;
            }
            $this->chat = rawurlencode($this->chat);
            $GLOBALS['db']->query('INSERT INTO `transcripts` (`timestamp`, `chatid`, `operatorid`, `departmentid`, `guest`, `hostname`, `transcript`) VALUES (UNIX_TIMESTAMP(), "'.$chatid.'", "'.$GLOBALS['operator']->id().'", "'.$_SESSION['hcl_'.$chatid]['departmentid'].'", "'.$_SESSION['hcl_'.$chatid]['guest'].'", "'.$this->result[0]['hostname'].'", "'.$this->chat.'")');
        }
        function get($chatid = '', $id = '', $admin = '')
        {
            if ($chatid == '' && $id == '') {
                $this->transcripts = array();
                if ($GLOBALS['auth']->admin() && $admin == 'admin') {
                    if ($this->result = $GLOBALS['db']->query('SELECT * FROM `departments` WHERE 1')) {
                        foreach ($this->result as $key => $val) {
                            if ($this->result2 = $GLOBALS['db']->query('SELECT * FROM `transcripts` WHERE `departmentid`="'.$this->result[$key]['id'].'" ORDER BY `timestamp` DESC')) {
                                foreach ($this->result2 as $key2 => $val2) {
                                    $this->result2[$key2]['time'] = gmdate('G:i:s D jS F Y', $this->result2[$key2]['timestamp'] + (3600 * $GLOBALS['conf']['offset']));
                                }
                                $this->transcripts = array_merge($this->transcripts, array(array('name' => $GLOBALS['department']->name($this->result[$key]['id']), 'transcript' => $this->result2)));
                            }
                        }
                    }
                } else {
                    if ($this->result = $GLOBALS['db']->query('SELECT * FROM `assigns` WHERE `operatorid`="'.$GLOBALS['operator']->id().'"')) {
                        foreach ($this->result as $key => $val) {
                            if ($this->result2 = $GLOBALS['db']->query('SELECT * FROM `transcripts` WHERE `departmentid`="'.$this->result[$key]['departmentid'].'" AND `operatorid`="'.$GLOBALS['operator']->id().'" ORDER BY `timestamp` DESC')) {
                                foreach ($this->result2 as $key2 => $val2) {
                                    $this->result2[$key2]['time'] = gmdate('G:i:s D jS F Y', $this->result2[$key2]['timestamp'] + (3600 * $GLOBALS['conf']['offset']));
                                }
                                $this->transcripts = array_merge($this->transcripts, array(array('name' => $GLOBALS['department']->name($this->result[$key]['departmentid']), 'transcript' => $this->result2)));
                            }
                        }
                    }
                }
                return $this->transcripts;
            } elseif ($id !== '') {
                $this->transcripts = array();
                if ($this->result = $GLOBALS['db']->query('SELECT * FROM `transcripts` WHERE `id`="'.$id.'"')) {
                    if ($GLOBALS['auth']->admin() && $admin == 'admin') {
                        $this->transcripts['id'] = $this->result[0]['id'];
                        $this->transcripts['nick'] = $this->result[0]['guest'];
                        $this->transcripts['operator'] = $GLOBALS['operator']->name($this->result[0]['operatorid']);
                        $this->transcripts['department'] = $GLOBALS['department']->name($this->result[0]['departmentid']);
                        $this->transcripts['email'] = $GLOBALS['department']->email($this->result[0]['departmentid']);
                        $this->transcripts['time'] = gmdate('G:i:s D jS F Y', $this->result[0]['timestamp'] + (3600 * $GLOBALS['conf']['offset']));
                        $this->transcripts['chat'] = rawurldecode($this->result[0]['transcript']);
                        return $this->transcripts;
                    } elseif ($this->result2 = $GLOBALS['db']->query('SELECT * FROM `assigns` WHERE `operatorid`="'.$GLOBALS['operator']->id().'"')) {
                        foreach ($this->result2 as $key => $val) {
                            if ($this->result2[$key]['departmentid'] == $this->result[0]['departmentid']) {
                                $this->transcripts['id'] = $this->result[0]['id'];
                                $this->transcripts['nick'] = $this->result[0]['guest'];
                                $this->transcripts['operator'] = $GLOBALS['operator']->name($this->result[0]['operatorid']);
                                $this->transcripts['department'] = $GLOBALS['department']->name($this->result[0]['departmentid']);
                                $this->transcripts['email'] = $GLOBALS['department']->email($this->result[0]['departmentid']);
                                $this->transcripts['time'] = gmdate('G:i:s D jS F Y', $this->result[0]['timestamp'] + (3600 * $GLOBALS['conf']['offset']));
                                $this->transcripts['chat'] = rawurldecode($this->result[0]['transcript']);
                                return $this->transcripts;
                            }
                        }
                    }
                } else {
                    return false;
                }
            } else {
                $this->chat = '';
                $this->result = $GLOBALS['db']->query('SELECT * FROM `traffic` WHERE `id`="'.$chatid.'"');
                if ($this->result2 = $GLOBALS['db']->query('SELECT * FROM `chat` WHERE `chatid`="'.$chatid.'" ORDER BY `timestamp` ASC')) {
                    foreach ($this->result2 as $key => $val) {
                        $this->timestamp = gmdate('G:i:s', $this->result2[$key]['timestamp'] + (3600 * $GLOBALS['conf']['offset']));
                        if ($this->result2[$key]['x'] == 'o') {
                            $this->text = html_to_char(rawurldecode($this->result2[$key]['message'])).' ';
                            $this->text = preg_replace("/%%user%%/i", $_SESSION['hcl_'.$chatid]['guest'], $this->text);
                            $this->text = preg_replace("/url:(.*?) /i", "\\1 ", $this->text);
                            $this->text = preg_replace("/image:(.*?) /i", " ", $this->text);
                            $this->text = preg_replace("/email:(.*?) /i", "\\1 ", $this->text);
                            $this->text = preg_replace("/push:(.*?) /i", "Pushed Page: \\1 ", $this->text);
                            $this->text = substr($this->text, 0, -1);
                            $this->text = '('.$this->timestamp.') '.$_SESSION['hcl_'.$chatid]['operator'].': '.char_to_html($this->text).'
 ", $this->text);
                            $this->text = preg_replace("/email:(.*?) /i", "\\1 ", $this->text);
                            $this->text = preg_replace("/push:(.*?) /i", "Pushed Page: \\1 ", $this->text);
                            $this->text = substr($this->text, 0, -1);
                            $this->text = '('.$this->timestamp.') '.$_SESSION['hcl_'.$chatid]['operator'].': '.char_to_html($this->text).'
';
                        } else {
                            $this->text = '('.$this->timestamp.') '.$_SESSION['hcl_'.$chatid]['guest'].': '.rawurldecode($this->result2[$key]['message']).'
';
                        }
                        $this->chat = $this->chat.$this->text;
                    }
                    return $this->chat;  
                } else {
                    return 'Error';
                }
            }          
        }
    }
?>