function group_by_id($id) { global $mysqlprefix; $link = connect(); $group = select_one_row("select * from {$mysqlprefix}chatgroup where groupid = {$id}", $link); mysql_close($link); return $group; }
function load_message($key) { global $mysqlprefix; $link = connect(); $result = select_one_row("select vcvalue from {$mysqlprefix}chatresponses where id = " . intval($key), $link); mysql_close($link); return $result ? $result['vcvalue'] : null; }
function thread_info($id) { global $mysqlprefix; $link = connect(); $thread = select_one_row("select userName,agentName,remote,userAgent," . "unix_timestamp(dtmmodified) as modified, unix_timestamp(dtmcreated) as created," . "vclocalname as groupName " . "from {$mysqlprefix}chatthread left join {$mysqlprefix}chatgroup on {$mysqlprefix}chatthread.groupid = {$mysqlprefix}chatgroup.groupid " . "where threadid = " . intval($id), $link); mysql_close($link); return $thread; }
function notification_info($id) { global $mysqlprefix; $link = connect(); $notification = select_one_row(db_build_select("id, locale, vckind, vcto, unix_timestamp(dtmcreated) as created, vcsubject, tmessage, refoperator", "{$mysqlprefix}chatnotification", array("id = {$id}"), ""), $link); mysql_close($link); return $notification; }
function create_group($name, $descr, $commonname, $commondescr, $email) { global $mysqlprefix; $link = connect(); $query = sprintf("insert into {$mysqlprefix}chatgroup (vclocalname,vclocaldescription,vccommonname,vccommondescription,vcemail) values ('%s','%s','%s','%s','%s')", mysql_real_escape_string($name), mysql_real_escape_string($descr), mysql_real_escape_string($commonname), mysql_real_escape_string($commondescr), mysql_real_escape_string($email)); perform_query($query, $link); $id = mysql_insert_id($link); $newdep = select_one_row("select * from {$mysqlprefix}chatgroup where groupid = {$id}", $link); mysql_close($link); return $newdep; }
function thread_to_xml($thread, $link) { global $state_chatting, $threadstate_to_string, $threadstate_key, $mibew_encoding, $operator, $settings, $can_viewthreads, $can_takeover, $mysqlprefix; $state = $threadstate_to_string[$thread['istate']]; $result = "<thread id=\"" . safe_htmlspecialchars(safe_htmlspecialchars($thread['threadid'])) . "\" stateid=\"{$state}\""; if ($state == "closed") { return $result . "/>"; } $state = getstring($threadstate_key[$thread['istate']]); $nextagent = $thread['nextagent'] != 0 ? operator_by_id_($thread['nextagent'], $link) : null; $threadoperator = $nextagent ? get_operator_name($nextagent) : ($thread['agentName'] ? $thread['agentName'] : "-"); if ($threadoperator == "-" && $thread['groupname']) { $threadoperator = "- " . $thread['groupname'] . " -"; } if (!($thread['istate'] == $state_chatting && $thread['agentId'] != $operator['operatorid'] && !is_capable($can_takeover, $operator))) { $result .= " canopen=\"true\""; } if ($thread['agentId'] != $operator['operatorid'] && $thread['nextagent'] != $operator['operatorid'] && is_capable($can_viewthreads, $operator)) { $result .= " canview=\"true\""; } if ($settings['enableban'] == "1") { $result .= " canban=\"true\""; } $banForThread = $settings['enableban'] == "1" ? ban_for_addr_($thread['remote'], $link) : false; if ($banForThread) { $result .= " ban=\"blocked\" banid=\"" . safe_htmlspecialchars(safe_htmlspecialchars($banForThread['banid'])) . "\""; } $result .= " state=\"{$state}\" typing=\"" . safe_htmlspecialchars(safe_htmlspecialchars($thread['userTyping'])) . "\">"; $result .= "<name>"; if ($banForThread) { $result .= safe_htmlspecialchars(getstring('chat.client.spam.prefix')); } $result .= safe_htmlspecialchars(safe_htmlspecialchars(get_user_name($thread['userName'], $thread['remote'], $thread['userid']))) . "</name>"; $result .= "<addr>" . safe_htmlspecialchars(get_user_addr($thread['remote'])) . "</addr>"; $result .= "<agent>" . safe_htmlspecialchars(safe_htmlspecialchars($threadoperator)) . "</agent>"; $result .= "<time>" . safe_htmlspecialchars(safe_htmlspecialchars($thread['unix_timestamp(dtmcreated)'])) . "000</time>"; $result .= "<modified>" . safe_htmlspecialchars(safe_htmlspecialchars($thread['unix_timestamp(dtmmodified)'])) . "000</modified>"; if ($banForThread) { $result .= "<reason>" . safe_htmlspecialchars(safe_htmlspecialchars($banForThread['comment'])) . "</reason>"; } $userAgent = get_useragent_version($thread['userAgent']); $result .= "<useragent>" . safe_htmlspecialchars($userAgent) . "</useragent>"; if ($thread["shownmessageid"] != 0) { $query = "select tmessage from {$mysqlprefix}chatmessage where messageid = " . intval($thread["shownmessageid"]); $line = select_one_row($query, $link); if ($line) { $message = preg_replace("/[\r\n\t]+/", " ", $line["tmessage"]); $result .= "<message>" . safe_htmlspecialchars(safe_htmlspecialchars($message)) . "</message>"; } } $result .= "</thread>"; return $result; }
function check_connections_from_remote($remote, $link) { global $settings, $state_closed, $state_left, $mysqlprefix; if ($settings['max_connections_from_one_host'] == 0) { return true; } $result = select_one_row("select count(*) as opened from {$mysqlprefix}chatthread " . "where remote = '" . mysql_real_escape_string($remote, $link) . "' AND istate <> {$state_closed} AND istate <> {$state_left}", $link); if ($result && isset($result['opened'])) { return $result['opened'] < $settings['max_connections_from_one_host']; } return true; }
function is_operator_online($operatorid, $link) { global $settings, $mysqlprefix; loadsettings_($link); $query = "select count(*) as total, min(unix_timestamp(CURRENT_TIMESTAMP)-unix_timestamp(dtmlastvisited)) as time " . "from {$mysqlprefix}chatoperator where operatorid = " . intval($operatorid); $row = select_one_row($query, $link); return $row['time'] < $settings['online_timeout'] && $row['total'] == 1; }
} else { $errors[] = getlocal("chat.redirect.cannot"); } } else { $errors[] = getlocal("chat.redirect.unknown_group"); } } else { $nextid = verifyparam("nextAgent", "/^\\d{1,10}\$/"); $nextOperator = operator_by_id($nextid); if ($nextOperator) { $page['message'] = getlocal2("chat.redirected.content", array(safe_htmlspecialchars(topage(get_operator_name($nextOperator))))); if ($thread['istate'] == $state_chatting) { $link = connect(); $threadupdate = array("istate" => intval($state_waiting), "nextagent" => intval($nextid), "agentId" => 0); if ($thread['groupid'] != 0) { if (FALSE === select_one_row("select groupid from {$mysqlprefix}chatgroupoperator where operatorid = " . intval($nextid) . " and groupid = " . intval($thread['groupid']), $link)) { $threadupdate['groupid'] = 0; } } commit_thread($threadid, $threadupdate, $link); post_message_($thread['threadid'], $kind_events, getstring2_("chat.status.operator.redirect", array(get_operator_name($operator)), $thread['locale'], true), $link); mysql_close($link); } else { $errors[] = getlocal("chat.redirect.cannot"); } } else { $errors[] = getlocal("chat.redirect.unknown_operator"); } } setup_logo(); if (count($errors) > 0) {
$curr = getdate(time()); if ($curr['mday'] < 7) { // previous month if ($curr['mon'] == 1) { $month = 12; $year = $curr['year'] - 1; } else { $month = $curr['mon'] - 1; $year = $curr['year']; } $start = mktime(0, 0, 0, $month, 1, $year); $end = mktime(0, 0, 0, $month, date("t", $start), $year) + 24 * 60 * 60; } else { $start = mktime(0, 0, 0, $curr['mon'], 1, $curr['year']); $end = time() + 24 * 60 * 60; } } set_form_date($start, "start"); set_form_date($end - 24 * 60 * 60, "end"); if ($start > $end) { $errors[] = getlocal("statistics.wrong.dates"); } $link = connect(); $page['reportByDate'] = select_multi_assoc("select DATE(dtmcreated) as date, COUNT(distinct threadid) as threads, SUM({$mysqlprefix}chatmessage.ikind = {$kind_agent}) as agents, SUM({$mysqlprefix}chatmessage.ikind = {$kind_user}) as users " . "from {$mysqlprefix}chatmessage where unix_timestamp(dtmcreated) >= {$start} AND unix_timestamp(dtmcreated) < {$end} group by DATE(dtmcreated) order by dtmcreated desc", $link); $page['reportByDateTotal'] = select_one_row("select COUNT(distinct threadid) as threads, SUM({$mysqlprefix}chatmessage.ikind = {$kind_agent}) as agents, SUM({$mysqlprefix}chatmessage.ikind = {$kind_user}) as users " . "from {$mysqlprefix}chatmessage where unix_timestamp(dtmcreated) >= {$start} AND unix_timestamp(dtmcreated) < {$end}", $link); $page['reportByAgent'] = select_multi_assoc("select vclocalename as name, COUNT(distinct threadid) as threads, SUM(ikind = {$kind_agent}) as msgs, AVG(CHAR_LENGTH(tmessage)) as avglen " . "from {$mysqlprefix}chatmessage, {$mysqlprefix}chatoperator " . "where agentId = operatorid AND unix_timestamp(dtmcreated) >= {$start} AND unix_timestamp(dtmcreated) < {$end} group by operatorid", $link); $page['showresults'] = count($errors) == 0; mysql_close($link); prepare_menu($operator); start_html_output(); require '../view/statistics.php';
} else { $page['saved'] = true; $page['address'] = $address; } } else { $page['banId'] = topage($banId); $page['formaddress'] = topage($address); $page['formdays'] = topage($days); $page['formcomment'] = topage($comment); $page['threadid'] = $threadid; } } else { if (isset($_GET['id'])) { $banId = verifyparam('id', "/^\\d{1,10}\$/"); $link = connect(); $ban = select_one_row("select banid,(unix_timestamp(dtmtill)-unix_timestamp(CURRENT_TIMESTAMP)) as days,address,comment from {$mysqlprefix}chatban where banid = " . intval($banId), $link); mysql_close($link); if ($ban) { $page['banId'] = topage($ban['banid']); $page['formaddress'] = topage($ban['address']); $page['formdays'] = topage(round($ban['days'] / 86400)); $page['formcomment'] = topage($ban['comment']); } else { $errors[] = "Wrong id"; } } else { if (isset($_GET['thread'])) { $threadid = verifyparam('thread', "/^\\d{1,10}\$/"); $thread = thread_by_id($threadid); if ($thread) { $page['thread'] = topage($thread['userName']);
$curr = getdate(time()); if ($curr['mday'] < 7) { // previous month if ($curr['mon'] == 1) { $month = 12; $year = $curr['year'] - 1; } else { $month = $curr['mon'] - 1; $year = $curr['year']; } $start = mktime(0, 0, 0, $month, 1, $year); $end = mktime(0, 0, 0, $month, date("t", $start), $year) + 24 * 60 * 60; } else { $start = mktime(0, 0, 0, $curr['mon'], 1, $curr['year']); $end = time() + 24 * 60 * 60; } } set_form_date($start, "start"); set_form_date($end - 24 * 60 * 60, "end"); if ($start > $end) { $errors[] = getlocal("statistics.wrong.dates"); } $link = connect(); $page['reportByDate'] = select_multi_assoc("select DATE(dtmcreated) as date, COUNT(distinct threadid) as threads, SUM({$mysqlprefix}chatmessage.ikind = " . intval($kind_agent) . ") as agents, SUM({$mysqlprefix}chatmessage.ikind = " . intval($kind_user) . ") as users " . "from {$mysqlprefix}chatmessage where unix_timestamp(dtmcreated) >= " . intval($start) . " AND unix_timestamp(dtmcreated) < " . intval($end) . " group by DATE(dtmcreated) order by dtmcreated desc", $link); $page['reportByDateTotal'] = select_one_row("select COUNT(distinct threadid) as threads, SUM({$mysqlprefix}chatmessage.ikind = " . intval($kind_agent) . ") as agents, SUM({$mysqlprefix}chatmessage.ikind = " . intval($kind_user) . ") as users " . "from {$mysqlprefix}chatmessage where unix_timestamp(dtmcreated) >= " . intval($start) . " AND unix_timestamp(dtmcreated) < " . intval($end), $link); $page['reportByAgent'] = select_multi_assoc("select vclocalename as name, COUNT(distinct threadid) as threads, SUM(ikind = " . intval($kind_agent) . ") as msgs, AVG(CHAR_LENGTH(tmessage)) as avglen " . "from {$mysqlprefix}chatmessage, {$mysqlprefix}chatoperator " . "where agentId = operatorid AND unix_timestamp(dtmcreated) >= " . intval($start) . " AND unix_timestamp(dtmcreated) < " . intval($end) . " group by operatorid", $link); $page['showresults'] = count($errors) == 0; mysql_close($link); prepare_menu($operator); start_html_output(); require '../view/statistics.php';
} else { $errors[] = getlocal("chat.redirect.cannot"); } } else { $errors[] = "Unknown group"; } } else { $nextid = verifyparam("nextAgent", "/^\\d{1,8}\$/"); $nextOperator = operator_by_id($nextid); if ($nextOperator) { $page['message'] = getlocal2("chat.redirected.content", array(topage(get_operator_name($nextOperator)))); if ($thread['istate'] == $state_chatting) { $link = connect(); $threadupdate = array("istate" => $state_waiting, "nextagent" => $nextid, "agentId" => 0); if ($thread['groupid'] != 0) { if (FALSE === select_one_row("select groupid from {$mysqlprefix}chatgroupoperator where operatorid = {$nextid} and groupid = " . $thread['groupid'], $link)) { $threadupdate['groupid'] = 0; } } commit_thread($threadid, $threadupdate, $link); post_message_($thread['threadid'], $kind_events, getstring2_("chat.status.operator.redirect", array(get_operator_name($operator)), $thread['locale']), $link); mysql_close($link); } else { $errors[] = getlocal("chat.redirect.cannot"); } } else { $errors[] = "Unknown operator"; } } setup_logo(); if (count($errors) > 0) {