function wplc_user_initiate_chat($name, $email, $cid = null, $session)
{
    global $wpdb;
    global $wplc_tblname_chats;
    do_action("wplc_hook_initiate_chat", array("cid" => $cid, "name" => $name, "email" => $email));
    $wplc_settings = get_option('WPLC_SETTINGS');
    if (isset($wplc_settings['wplc_record_ip_address']) && $wplc_settings['wplc_record_ip_address'] == 1) {
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR'] != '') {
            $ip_address = sanitize_text_field($_SERVER['HTTP_X_FORWARDED_FOR']);
        } else {
            $ip_address = sanitize_text_field($_SERVER['REMOTE_ADDR']);
        }
        $user_data = array('ip' => $ip_address, 'user_agent' => sanitize_text_field($_SERVER['HTTP_USER_AGENT']));
        $wplc_ce_ip = $ip_address;
    } else {
        $user_data = array('ip' => "", 'user_agent' => sanitize_text_field($_SERVER['HTTP_USER_AGENT']));
        $wplc_ce_ip = null;
    }
    if (function_exists('wplc_ce_activate')) {
        /* Log the chat for statistical purposes as well */
        if (function_exists('wplc_ce_record_initial_chat')) {
            wplc_ce_record_initial_chat($name, $email, $cid, $wplc_ce_ip, sanitize_text_field($_SERVER['HTTP_REFERER']));
        }
    }
    if ($cid != null) {
        /* change from a visitor to a chat */
        /**
         * This helps us identify if this user needs to be answered. The user can start typing so long but an agent still needs to answer the chat
         * @var serialized array
         */
        $chat_data = wplc_get_chat_data($cid);
        if (isset($chat_data->other)) {
            $other_data = maybe_unserialize($chat_data->other);
            $other_data['unanswered'] = true;
        }
        $wpdb->update($wplc_tblname_chats, array('status' => 2, 'timestamp' => current_time('mysql'), 'name' => $name, 'email' => $email, 'session' => $session, 'ip' => maybe_serialize($user_data), 'url' => sanitize_text_field($_SERVER['HTTP_REFERER']), 'last_active_timestamp' => current_time('mysql'), 'other' => maybe_serialize($other_data)), array('id' => $cid), array('%d', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s'), array('%d'));
        return $cid;
    } else {
        $other_data = array();
        $other_data['unanswered'] = true;
        $wpdb->insert($wplc_tblname_chats, array('status' => 2, 'timestamp' => current_time('mysql'), 'name' => $name, 'email' => $email, 'session' => $session, 'ip' => maybe_serialize($user_data), 'url' => sanitize_text_field($_SERVER['HTTP_REFERER']), 'last_active_timestamp' => current_time('mysql'), 'other' => maybe_serialize($other_data)), array('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s'));
        $lastid = $wpdb->insert_id;
        /* Nick: moved from top of function to bottom of function to try speed up the process of accepting the chart - version 7 */
        if (function_exists("wplc_list_chats_pro")) {
            /* check if functions-pro is around */
            wplc_pro_notify_via_email();
        }
        return $lastid;
    }
}
function wplc_api_return_messages($cid, $limit, $offset)
{
    global $wpdb;
    global $wplc_tblname_msgs;
    $cid = intval($cid);
    $limit = intval($limit);
    $offset = intval($offset);
    $cdata = wplc_get_chat_data($cid);
    $wplc_settings = get_option("WPLC_SETTINGS");
    if ($limit > 50) {
        $limit = 50;
    }
    $results = $wpdb->get_results("\n        SELECT * \n        FROM {$wplc_tblname_msgs} \n        WHERE `chat_sess_id` = '{$cid}' \n        ORDER BY `timestamp` ASC \n        LIMIT {$limit} OFFSET {$offset} \n        ");
    $messages = array();
    //The message array
    if ($results) {
        foreach ($results as $result) {
            $messages[$result->id] = array();
            //Message object
            $messages[$result->id]['from'] = $result->msgfrom;
            $messages[$result->id]['message'] = stripslashes($result->msg);
            $messages[$result->id]['timestamp'] = strtotime($result->timestamp);
            $messages[$result->id]['is_admin'] = $result->originates == 1 ? true : false;
            $gravatar = "";
            if ($messages[$result->id]['is_admin']) {
                if (isset($cdata->other)) {
                    $other = maybe_unserialize($cdata->other);
                    if (isset($other['aid'])) {
                        $user_info = get_userdata(intval($other['aid']));
                        $gravatar = "//www.gravatar.com/avatar/" . md5($user_info->user_email) . "?s=30";
                    }
                }
            } else {
                if (isset($cdata->email)) {
                    $gravatar = "//www.gravatar.com/avatar/" . md5($cdata->email) . "?s=30";
                }
            }
            if (function_exists('wplc_decrypt_msg')) {
                $messages[$result->id]['message'] = wplc_decrypt_msg($messages[$result->id]['message']);
            }
            $messages[$result->id]['message'] = stripslashes($messages[$result->id]['message']);
        }
    }
    return $messages;
}
Esempio n. 3
0
function wplc_return_chat_messages($cid, $transcript = false, $html = true)
{
    global $wpdb;
    global $wplc_tblname_msgs;
    $wplc_settings = get_option("WPLC_SETTINGS");
    if (isset($wplc_settings['wplc_display_name']) && $wplc_settings['wplc_display_name'] == 1) {
        $display_name = 1;
    } else {
        $display_name = 0;
    }
    $results = wplc_get_chat_messages($cid);
    if (!$results) {
        return;
    }
    $cdata = wplc_get_chat_data($cid);
    $msg_hist = "";
    $previous_time = "";
    $previous_timestamp = 0;
    foreach ($results as $result) {
        $from = $result->msgfrom;
        $msg = stripslashes($result->msg);
        $timestamp = strtotime($result->timestamp);
        $time_diff = $timestamp - $previous_timestamp;
        if ($time_diff > 60) {
            $show_time = true;
        } else {
            $show_time = false;
        }
        //        $date = new DateTime($timestamp);
        $timeshow = date('l, F d Y h:i A', $timestamp);
        if (!$transcript) {
            if ($previous_time == $timeshow || !$show_time) {
                $timeshow = "";
            }
        }
        $previous_time = $timeshow;
        $previous_timestamp = $timestamp;
        $image = "";
        if ($result->originates == 1) {
            $class = "wplc-admin-message wplc-color-bg-4 wplc-color-2 wplc-color-border-4";
            if (function_exists("wplc_pro_get_admin_picture")) {
                $src = wplc_pro_get_admin_picture();
                if ($src) {
                    $image = "<img src=" . $src . " width='20px' id='wp-live-chat-2-img'/>";
                }
            } else {
                $other = maybe_unserialize($cdata->other);
                if (isset($other['aid'])) {
                    $user_info = get_userdata(intval($other['aid']));
                    /* get agent id */
                    $image = "<img src='//www.gravatar.com/avatar/" . md5($user_info->user_email) . "?s=30'  class='wplc-admin-message-avatar' />";
                } else {
                    /* get default setting in the notifications tab */
                    $image = "";
                    if (1 == 1) {
                    } else {
                        /* there's nothing Jim.. */
                        $image = "";
                    }
                }
            }
        } else {
            $class = "wplc-user-message wplc-color-bg-1 wplc-color-2 wplc-color-border-1";
            if (isset($_COOKIE['wplc_email']) && $_COOKIE['wplc_email'] != "") {
                $wplc_user_gravatar = md5(strtolower(trim(sanitize_text_field($_COOKIE['wplc_email']))));
            } else {
                $wplc_user_gravatar = "";
            }
            if ($wplc_user_gravatar != "") {
                $image = "<img src='//www.gravatar.com/avatar/{$wplc_user_gravatar}?s=30'  class='wplc-user-message-avatar' />";
            } else {
                $image = "";
            }
        }
        if (function_exists('wplc_decrypt_msg')) {
            $msg = wplc_decrypt_msg($msg);
        }
        $msg = apply_filters("wplc_filter_message_control_out", $msg);
        if ($display_name) {
            if ($html) {
                $msg_hist .= "<span class='chat_time wplc-color-4'>{$timeshow}</span> <span class='{$class}'>{$image} <strong>{$from}</strong>: {$msg}</span><br /><div class='wplc-clear-float-message'></div>";
            } else {
                $msg_hist .= "({$timeshow}) {$from}: {$msg}\r\n";
            }
        } else {
            if ($html) {
                $msg_hist .= "<span class='chat_time wplc-color-4'>{$timeshow}</span> <span class='{$class}'>{$msg}</span><br /><div class='wplc-clear-float-message'></div>";
            } else {
                $msg_hist .= "({$timeshow}) {$from}: {$msg}\r\n";
            }
        }
    }
    return $msg_hist;
}