function LivelyChatSupport_admin()
{
    global $wpdb;
    wp_register_style("LivelyChatSupport-admin-css", plugins_url("lively-chat-support/admin/css/style.css"));
    wp_register_style("LivelyChatSupport-chatbox-reset", plugins_url("lively-chat-support/chatbox/css/reset.css"));
    wp_register_style("LivelyChatSupport-chatbox-style", plugins_url("lively-chat-support/chatbox/css/style.css"));
    wp_register_style("LivelyChatSupport-jQuery-UI", plugins_url("lively-chat-support/admin/css/livelychatsupport.jquery-ui.min.css"));
    wp_enqueue_style(array("LivelyChatSupport-chatbox-reset", "LivelyChatSupport-chatbox-style", "LivelyChatSupport-chatbox-colours", "LivelyChatSupport-admin-css", "wp-color-picker", "LivelyChatSupport-jQuery-UI"));
    wp_register_script("LivelyChatSupport-admin-js", plugins_url("lively-chat-support/admin/js/admin.js"));
    wp_enqueue_script(array("jquery", "jquery-ui", "jquery-ui-datepicker", "wp-color-picker", "LivelyChatSupport-admin-js"));
    wp_enqueue_media();
    if (isset($_POST)) {
        if (function_exists("flush_pgcache")) {
            flush_pgcache();
        }
        if (function_exists("reset_oc_version")) {
            reset_oc_version();
        }
        $posted_data = array();
        $post_fields = array("subscriber_email", "subscriber_name", "default_responder_id", "visible_pages", "online", "offline_thanks", "colour", "position", "cta_online_text", "cta_offline_text", "cta_online_image_offset_y", "cta_online_image_offset_x", "cta_offline_image_offset_y", "cta_offline_image_offset_x", "cta_online_image", "cta_offline_image", "start", "finish", "twilio_sid", "twilio_auth", "sms_responder_id", "show_powered_by", "track_pages");
        $allow_blanks = array();
        foreach ($post_fields as $field) {
            if (isset($_POST[$field])) {
                $posted_data[$field] = trim(stripslashes($_POST[$field]));
            }
        }
        LivelyChatSupport_settings($posted_data);
    }
    if (isset($_POST["activation_code"])) {
        LivelyChatSupport_activate();
    }
    if (isset($_GET["delete_convo"])) {
        LivelyChatSupport_delete_convo($_GET["convo_token"]);
    }
    if (isset($_POST["twilio_phone"])) {
        $agent = LivelyChatSupport_agent(get_current_user_id());
        LivelyChatSupport_settings(array("twilio_phone" => "+" . preg_replace("/[^0-9]/", "", trim($_POST["twilio_phone"]))));
        LivelyChatSupport_send_sms("Site", "Your Lively Chat Support is installed!", $agent);
    }
    if (isset($_POST["agents"])) {
        foreach ($_POST["agents"] as $agent) {
            $active = $agent["active"] == "true" ? true : false;
            if (isset($agent["mobile"]) && $agent["mobile"] != "") {
                update_user_meta($agent["id"], "livelychatsupport-mobile", "+" . preg_replace("/[^0-9]/", "", trim($agent["mobile"])));
            }
            if (isset($agent["avatar"])) {
                update_user_meta($agent["id"], "livelychatsupport-avatar", $agent["avatar"]);
            }
            if (isset($agent["name"])) {
                update_user_meta($agent["id"], "livelychatsupport-name", $agent["name"]);
            }
            if (isset($agent["active"])) {
                update_user_meta($agent["id"], "livelychatsupport-active", $active);
            }
            if ($active) {
                $user = new WP_User($agent["id"]);
                $user->add_cap("can_livelychatsupport");
            } else {
                $user = new WP_User($agent["id"]);
                $user->remove_cap("can_livelychatsupport");
            }
        }
    }
    if (isset($_POST["triggers"])) {
        foreach ($_POST["triggers"] as $trigger) {
            $now = date("Y-m-d H:i:s", current_time("timestamp"));
            if ($trigger["id"] != "template") {
                if ($trigger["delete"] == "1") {
                    $wpdb->delete($wpdb->prefix . "livelychatsupport_triggers", array("id" => $trigger["id"]));
                } else {
                    if ($trigger["body"] != "") {
                        if ($trigger["id"] == "new") {
                            $wpdb->insert($wpdb->prefix . "livelychatsupport_triggers", array("urls" => $trigger["urls"], "delay" => $trigger["delay"], "body" => filter_var($trigger["body"], FILTER_SANITIZE_STRING), "created_at" => $now, "updated_at" => $now));
                        } else {
                            $wpdb->update($wpdb->prefix . "livelychatsupport_triggers", array("urls" => $trigger["urls"], "delay" => $trigger["delay"], "body" => filter_var($trigger["body"], FILTER_SANITIZE_STRING), "updated_at" => $now), array("id" => $trigger["id"]));
                        }
                    }
                }
            }
        }
    }
    if (isset($_POST["surveys"])) {
        foreach ($_POST["surveys"] as $survey) {
            $now = date("Y-m-d H:i:s", current_time("timestamp"));
            $questions = stripslashes($survey["questions"]);
            $title = stripslashes($survey["title"]);
            $urls = stripslashes($survey["urls"]);
            $delay = stripslashes($survey["delay"]);
            $thanks = stripslashes($survey["thanks"]);
            if ($survey["id"] != "template") {
                if ($survey["delete"] == "1") {
                    $wpdb->delete($wpdb->prefix . "livelychatsupport_surveys", array("id" => $survey["id"]));
                } else {
                    if ($title != "") {
                        if ($survey["id"] == "new") {
                            $wpdb->insert($wpdb->prefix . "livelychatsupport_surveys", array("title" => $title, "urls" => $urls, "delay" => $delay, "questions" => $questions, "thanks" => $thanks, "created_at" => $now, "updated_at" => $now));
                        } else {
                            $wpdb->update($wpdb->prefix . "livelychatsupport_surveys", array("title" => $title, "urls" => $urls, "delay" => $delay, "questions" => $questions, "thanks" => $thanks, "updated_at" => $now), array("id" => $survey["id"]));
                        }
                    }
                }
            }
        }
    }
    if (isset($_POST["hours"])) {
        foreach ($_POST["hours"] as $hour) {
            $now = date("Y-m-d H:i:s", current_time("timestamp"));
            if (isset($hour["id"])) {
                if ($hour["id"] != "template") {
                    if ($hour["delete"] == "1") {
                        $wpdb->delete($wpdb->prefix . "livelychatsupport_hours", array("id" => $hour["id"]));
                    } else {
                        if ($hour["id"] == "new") {
                            $wpdb->insert($wpdb->prefix . "livelychatsupport_hours", array("day" => $hour["day"], "open_at" => date("Hi", strtotime($hour["open_at"])), "close_at" => date("Hi", strtotime($hour["close_at"])), "responder_id" => $hour["responder_id"], "via" => $hour["via"], "created_at" => $now, "updated_at" => $now));
                        } else {
                            $wpdb->update($wpdb->prefix . "livelychatsupport_hours", array("day" => $hour["day"], "open_at" => date("Hi", strtotime($hour["open_at"])), "close_at" => date("Hi", strtotime($hour["close_at"])), "responder_id" => $hour["responder_id"], "via" => $hour["via"], "updated_at" => $now), array("id" => $hour["id"]));
                        }
                    }
                }
            }
        }
    }
    $livelychatsupport = LivelyChatSupport_details();
    echo '<div id="livelychatsupport" class="wrap">';
    include_once LIVELYCHATSUPPORT_ROOT . "/admin/includes/header.php";
    if ($livelychatsupport["subscriber_email"] == "" || $livelychatsupport["subscriber_name"] == "") {
        include_once LIVELYCHATSUPPORT_ROOT . "/admin/tabs/email_required.php";
    } else {
        $_GET["tab"] = isset($_GET["tab"]) ? $_GET["tab"] : "visitors";
        include_once LIVELYCHATSUPPORT_ROOT . "/admin/tabs/" . $_GET["tab"] . ".php";
    }
    echo '</div>';
}
function LivelyChatSupport_create_message($convo_token, $body, $from_agent, $not_initiated = false)
{
    if ($body != "") {
        global $wpdb;
        $livelychatsupport = LivelyChatSupport_details();
        $convo = LivelyChatSupport_convo($convo_token);
        $agent = LivelyChatSupport_agent($convo->agent_id);
        $now = date("Y-m-d H:i:s.u");
        if ($convo) {
            $message = $wpdb->insert($wpdb->prefix . "livelychatsupport_messages", array("convo_token" => $convo->token, "body" => $body, "from_agent" => $from_agent, "created_at" => $now));
            if ($from_agent == 1) {
                if ($not_initiated == true) {
                    $details = array("messages_count" => $convo->messages_count + 1, "updated_at" => $now, "agent_id" => get_current_user_id());
                } else {
                    $details = array("messages_count" => $convo->messages_count + 1, "updated_at" => $now, "initiated" => 1, "agent_id" => get_current_user_id());
                }
            } else {
                $hour = LivelyChatSupport_hour();
                $sms_activated = strpos($livelychatsupport["addons"], "sms") !== false;
                if ($hour && $hour->via == "sms" || $sms_activated && get_user_meta($agent->id, "livelychatsupport-mobile") != "") {
                    LivelyChatSupport_send_sms($convo->mini_token, $body, $agent);
                    $details = array("messages_count" => $convo->messages_count + 1, "updated_at" => $now, "last_seen" => $now, "initiated" => 1);
                } else {
                    $details = array("pending" => true, "messages_count" => $convo->messages_count + 1, "updated_at" => $now, "last_seen" => $now, "initiated" => 1);
                }
            }
            $wpdb->update($wpdb->prefix . "livelychatsupport_convos", $details, array("token" => $convo->token));
        }
    }
    die(json_encode(array("success" => true)));
}