function activeListen($runs = 1, $isPost = false)
{
    global $CONFIG, $GROUPS, $INTERNAL, $USER;
    $start = time();
    $USER->Browsers[0]->Typing = isset($_POST[POST_EXTERN_TYPING]);
    if (!(!empty($USER->Browsers[0]->InternalUser) && $USER->Browsers[0]->InternalUser->LastActive > time() - $CONFIG["timeout_clients"])) {
        $USER->Browsers[0]->CloseChat(4);
    }
    while ($runs == 1) {
        processForward();
        if ($USER->Browsers[0]->Declined) {
            displayDeclined();
            return $USER;
        } else {
            if ($USER->Browsers[0]->Closed) {
                displayQuit();
                return $USER;
            } else {
                if ($USER->Browsers[0]->Activated == CHAT_STATUS_WAITING && !(!empty($USER->Browsers[0]->Forward) && !$USER->Browsers[0]->Forward->Processed)) {
                    beginnConversation();
                }
            }
        }
        if ($USER->Browsers[0]->Activated >= CHAT_STATUS_WAITING && !(!empty($USER->Browsers[0]->Forward) && !$USER->Browsers[0]->Forward->Processed)) {
            refreshPicture();
            processTyping();
        }
        if ($runs == 1 && isset($_POST[POST_EXTERN_USER_FILE_UPLOAD_NAME]) && !isset($_POST[POST_EXTERN_USER_FILE_UPLOAD_ERROR]) && !(!empty($USER->Browsers[0]->Forward) && !$USER->Browsers[0]->Forward->Processed)) {
            $USER = $USER->Browsers[0]->RequestFileUpload($USER, AJAXDecode($_POST[POST_EXTERN_USER_FILE_UPLOAD_NAME]));
        } else {
            if ($runs == 1 && isset($_POST[POST_EXTERN_USER_FILE_UPLOAD_NAME]) && isset($_POST[POST_EXTERN_USER_FILE_UPLOAD_ERROR])) {
                $USER = $USER->Browsers[0]->AbortFileUpload($USER, namebase(AJAXDecode($_POST[POST_EXTERN_USER_FILE_UPLOAD_NAME])), AJAXDecode($_POST[POST_EXTERN_USER_FILE_UPLOAD_ERROR]));
            }
        }
        if ($runs++ == 1 && isset($_POST[POST_GLOBAL_SHOUT])) {
            processPosts();
        }
        if ($USER->Browsers[0]->Activated == CHAT_STATUS_ACTIVE) {
            $isPost = getNewPosts();
            $USER->Browsers[0]->SetStatus(CHAT_STATUS_ACTIVE);
        }
        if (isset($_POST[POST_GLOBAL_SHOUT]) || isset($_POST[POST_GLOBAL_NO_LONG_POLL]) || $isPost || !empty($USER->Browsers[0]->Forward) && !$USER->Browsers[0]->Forward->Processed) {
            break;
        } else {
            if (md5($USER->Response) != AJAXDecode($_POST[POST_GLOBAL_XMLCLIP_HASH_ALL])) {
                $_POST[POST_GLOBAL_XMLCLIP_HASH_ALL] = md5($USER->Response);
                $USER->AddFunctionCall("lz_chat_listen_hash('" . md5($USER->Response) . "','" . getId(5) . "');", false);
                break;
            } else {
                $USER->Response = "";
                break;
            }
        }
    }
}
 function AbortFileUpload($_user, $_filename, $_error)
 {
     $fileid = md5(namebase($_filename) . $this->UserId . $this->BrowserId);
     $request = new FileUploadRequest(EX_FILE_UPLOAD_REQUEST . "_" . $fileid, $this->DesiredChatPartner);
     $request->Load();
     if (!$request->Closed) {
         $request->Error = $_error;
         $request->Save();
     } else {
         $_user->AddFunctionCall("top.lz_chat_file_reset();", false);
     }
     return $_user;
 }
function activeListen($runs = 1, $isPost = false)
{
    global $CONFIG, $USER, $VOUCHER;
    $USER->Browsers[0]->Typing = isset($_POST[POST_EXTERN_TYPING]);
    if (isset($_POST["p_tc_declined"])) {
        $USER->Browsers[0]->UpdateArchive("");
    } else {
        if (isset($_POST["p_tc_email"])) {
            $USER->Browsers[0]->UpdateArchive(base64UrlDecode($_POST["p_tc_email"]));
        }
    }
    if ($USER->Browsers[0]->InternalUser->Status == USER_STATUS_OFFLINE) {
        $USER->Browsers[0]->CloseChat(4);
    } else {
        foreach ($USER->Browsers[0]->Members as $sid => $member) {
            if ($USER->Browsers[0]->InternalUser->Status == USER_STATUS_OFFLINE) {
                $USER->Browsers[0]->LeaveChat($sid);
            }
        }
        if ($USER->Browsers[0]->InternalUser->SystemId != $USER->Browsers[0]->DesiredChatPartner) {
            $USER->Browsers[0]->DesiredChatPartner = $USER->Browsers[0]->InternalUser->SystemId;
        }
    }
    processForward();
    if (!empty($USER->Browsers[0]->Declined)) {
        if ($USER->Browsers[0]->Declined < time() - $CONFIG["poll_frequency_clients"] * 2) {
            displayDeclined();
        }
        return $USER;
    } else {
        if ($USER->Browsers[0]->Closed || empty($USER->Browsers[0]->InternalUser)) {
            displayQuit();
            return $USER;
        } else {
            if ($USER->Browsers[0]->Activated == CHAT_STATUS_WAITING && !(!empty($USER->Browsers[0]->Forward) && !$USER->Browsers[0]->Forward->Processed)) {
                beginnConversation();
            }
        }
    }
    if ($USER->Browsers[0]->Activated >= CHAT_STATUS_WAITING && !(!empty($USER->Browsers[0]->Forward) && !$USER->Browsers[0]->Forward->Processed)) {
        refreshPicture();
        processTyping();
    }
    if ($runs == 1 && isset($_POST[POST_EXTERN_USER_FILE_UPLOAD_NAME]) && !isset($_POST[POST_EXTERN_USER_FILE_UPLOAD_ERROR]) && !(!empty($USER->Browsers[0]->Forward) && !$USER->Browsers[0]->Forward->Processed)) {
        $USER = $USER->Browsers[0]->RequestFileUpload($USER, base64UrlDecode($_POST[POST_EXTERN_USER_FILE_UPLOAD_NAME]));
    } else {
        if ($runs == 1 && isset($_POST[POST_EXTERN_USER_FILE_UPLOAD_NAME]) && isset($_POST[POST_EXTERN_USER_FILE_UPLOAD_ERROR])) {
            $USER = $USER->Browsers[0]->AbortFileUpload($USER, namebase(base64UrlDecode($_POST[POST_EXTERN_USER_FILE_UPLOAD_NAME])), base64UrlDecode($_POST[POST_EXTERN_USER_FILE_UPLOAD_ERROR]));
        }
    }
    if (isset($_POST[POST_GLOBAL_SHOUT])) {
        processPosts();
    }
    if ($USER->Browsers[0]->Activated == CHAT_STATUS_ACTIVE) {
        $isPost = getNewPosts();
        $USER->Browsers[0]->SetStatus(CHAT_STATUS_ACTIVE);
        if (!empty($VOUCHER)) {
            if (time() - $USER->Browsers[0]->LastActive > 0) {
                $VOUCHER->UpdateVoucherChatTime(time() - $USER->Browsers[0]->LastActive);
            }
            if (!(!empty($USER->Browsers[0]->Forward) && !$USER->Browsers[0]->Forward->Processed)) {
                $VOUCHER->UpdateVoucherChatSessions($USER->Browsers[0]->ChatId);
            }
            $vouchers = VisitorChat::GetRelatedChatVouchers(base64UrlDecode($_POST[POST_EXTERN_USER_GROUP]), $VOUCHER);
            $USER->AddFunctionCall("lz_chat_add_update_vouchers_init('" . base64_encode(getChangeVoucherHTML($vouchers)) . "');", false);
            foreach ($vouchers as $tonlist) {
                $USER->AddFunctionCall("lz_chat_add_available_voucher('" . $tonlist->Id . "'," . $tonlist->ChatTime . "," . $tonlist->ChatTimeMax . "," . $tonlist->ChatSessions . "," . $tonlist->ChatSessionsMax . "," . $tonlist->VoucherAutoExpire . "," . parseBool($tonlist->VoucherAutoExpire < time()) . ");", false);
            }
        } else {
            $USER->AddFunctionCall("lz_chat_add_update_vouchers_init('" . base64_encode("") . "');", false);
        }
    }
    if ($USER->Browsers[0]->TranslationSettings != null) {
        $USER->AddFunctionCall("lz_chat_set_translation(" . $USER->Browsers[0]->TranslationSettings[0] . ",'" . base64_encode($USER->Browsers[0]->TranslationSettings[1]) . "','" . base64_encode($USER->Browsers[0]->TranslationSettings[2]) . "');", false);
    }
    if (isset($_POST[POST_GLOBAL_SHOUT]) || isset($_POST[POST_GLOBAL_NO_LONG_POLL]) || $isPost || !empty($USER->Browsers[0]->Forward) && !$USER->Browsers[0]->Forward->Processed) {
        //break;
    } else {
        if (md5($USER->Response) != base64UrlDecode($_POST[POST_GLOBAL_XMLCLIP_HASH_ALL])) {
            $_POST[POST_GLOBAL_XMLCLIP_HASH_ALL] = md5($USER->Response);
            $USER->AddFunctionCall("lz_chat_listen_hash('" . md5($USER->Response) . "','" . getId(5) . "');", false);
            //break;
        } else {
            $USER->Response = "";
            //break;
        }
    }
}
function activeListen($runs = 1, $picUpdate = false, $isPost = false)
{
    global $CONFIG, $GROUPS, $INTERNAL, $USER, $ISFORWARD;
    $start = time();
    $USER->Browsers[0]->Typing = isset($_POST[POST_EXTERN_TYPING]);
    while ($runs == 1) {
        isForward();
        processForward();
        if ($runs > 1) {
            getDataSets();
            getData(false, false, false, true);
            $USER->Browsers[0]->LoadChat($CONFIG, null);
            if (isnull($USER->Browsers[0]->Chat)) {
                break;
            }
        }
        if ($USER->Browsers[0]->Chat->Closed) {
            displayQuit();
            return $USER;
        } else {
            if ($USER->Browsers[0]->Chat->Declined) {
                displayDeclined();
                return $USER;
            } else {
                if ($USER->Browsers[0]->Chat->Activated == CHAT_STATUS_WAITING && !$ISFORWARD) {
                    beginnConversation();
                }
            }
        }
        if ($USER->Browsers[0]->Chat->Activated >= CHAT_STATUS_WAITING && !$ISFORWARD) {
            $picUpdate = refreshPicture();
            processTyping();
        }
        if ($runs == 1 && isset($_POST[POST_EXTERN_USER_FILE_UPLOAD_NAME]) && !isset($_POST[POST_EXTERN_USER_FILE_UPLOAD_ERROR]) && !$ISFORWARD) {
            $USER = $USER->Browsers[0]->RequestFileUpload($USER, $_POST[POST_EXTERN_USER_FILE_UPLOAD_NAME]);
        } else {
            if ($runs == 1 && isset($_POST[POST_EXTERN_USER_FILE_UPLOAD_NAME]) && isset($_POST[POST_EXTERN_USER_FILE_UPLOAD_ERROR])) {
                $USER->Browsers[0]->AbortFileUpload($USER, namebase($_POST[POST_EXTERN_USER_FILE_UPLOAD_NAME]), $_POST[POST_EXTERN_USER_FILE_UPLOAD_ERROR]);
            }
        }
        if ($runs++ == 1 && isset($_POST[POST_GLOBAL_SHOUT])) {
            processPosts();
        }
        if ($USER->Browsers[0]->Chat->Activated == CHAT_STATUS_ACTIVE) {
            $isPost = getNewPosts();
            updateRoom($USER->Browsers[0]->Chat->Id, CHAT_STATUS_ACTIVE);
        } else {
            updateRoom($USER->Browsers[0]->Chat->Id, CHAT_STATUS_WAITING);
        }
        if (isset($_POST[POST_GLOBAL_SHOUT]) || isset($_POST[POST_GLOBAL_NO_LONG_POLL]) || $isPost || $ISFORWARD) {
            break;
        } else {
            if (md5($USER->Response) != $_POST[POST_GLOBAL_XMLCLIP_HASH_ALL] || $picUpdate) {
                $_POST[POST_GLOBAL_XMLCLIP_HASH_ALL] = md5($USER->Response);
                $USER->AddFunctionCall("lz_chat_listen_hash('" . md5($USER->Response) . "','" . getId(5) . "');", false);
                break;
            } else {
                $USER->Response = "";
                $wait = max($CONFIG["poll_frequency_clients"] - 1, 1);
                if (time() + $wait < $start + getLongPollRuntime()) {
                    sleep($wait);
                } else {
                    break;
                }
            }
        }
    }
}
 function AbortFileUpload($_user, $_filename, $_error)
 {
     $fileid = substr(md5(namebase($_filename)), 0, 5);
     $request = new FileUploadRequest($this->BrowserId . "_" . $fileid, $this->DesiredChatPartner);
     if (dataSetExists($request->TargetFile)) {
         $request->Load();
         $request->Error = $_error;
         $request->Save();
     }
 }
 function AppendPostFile($_postKey, $_userId)
 {
     if (!empty($_FILES[$_postKey]) && true) {
         $filename = namebase($_FILES[$_postKey]['name']);
         if (!isValidUploadFile($filename)) {
             return $filename;
         }
         $fileId = getId(32);
         $fileurid = $_userId . "_" . $fileId;
         if (move_uploaded_file($_FILES[$_postKey]["tmp_name"], PATH_UPLOADS . $fileurid)) {
             processResource("SYSTEM", $fileId, $fileurid, 3, $filename, 0, 100, $_FILES[$_postKey]["size"]);
             $this->ApplyAttachment($fileId);
             return $filename;
         }
     }
 }