/** * Log in chat * * @access public * @return array */ public static function login($name, $email, $is_admin = false) { global $wpdb; // Logout if logged already Live_Chat::logout(); // Start session again sc_chat_session_start(); // Get options $opts = sc_chat_get_options(); // Check if any operator is online if (!Live_Chat::check_if_any_op_online() and !current_user_can('chat_with_users')) { throw new Exception(__('No operator is online. Please refresh the page and use contact form', 'sc_chat')); } // Check if fields are not empty if ($opts['ask_name_field'] == 1 and empty($name) or empty($email)) { throw new Exception(__('Please fill out all required fields', 'sc_chat')); } // Check if email is correct if (!is_email($email)) { throw new Exception(__('Email is invalid', 'sc_chat')); } // Sanitize name $name = sanitize_key(trim($name)); // Use localpart of email if no chars supported in given name // or name is not active already if (empty($name)) { list($email_name, $email_domain) = explode('@', $email); $name = sanitize_key($email_name); } // Preparing the gravatar hash: $gravatar = md5(strtolower(trim($email))); // Find user type if ($is_admin and current_user_can('chat_with_users')) { $user_type = 1; } else { $user_type = 2; } // Visitor // Get user IP Address (ip2long can't return negative now) $ip_address = sprintf('%u', ip2long(sc_chat_get_IP())); // Create new user $user = new Chat_user(array('name' => $name, 'email' => $email, 'gravatar' => $gravatar, 'type' => $user_type, 'ip_address' => $ip_address, 'user_agent' => $_SERVER['HTTP_USER_AGENT'])); // Save user into DB $user = $user->save(); // Display error if (!$user) { return false; } // Remove current conversations of current OPERATOR if ($is_admin and current_user_can('chat_with_users')) { $wpdb->query($wpdb->prepare(' DELETE FROM ' . $wpdb->prefix . 'chat_lines WHERE `author` = %s OR `receiver_ID` = %s', $name, $name)); } // Save user into session $_SESSION['sc_chat']['chat_user_ID'] = $user['user_id']; $_SESSION['sc_chat']['chat_user_name'] = $user['name']; $_SESSION['sc_chat']['chat_user_gravatar'] = $user['gravatar']; $_SESSION['sc_chat']['chat_user_email'] = $user['email']; // Prepare data for notification $_data = array('username' => $user['name'], 'email' => $user['email']); // Send NEW VISITOR LOGIN notification by email if (!current_user_can('chat_with_users')) { sc_chat_send_notification_email('user_login', $_data); } return array('user_id' => $user['user_id'], 'status' => 1, 'name' => $user['name'], 'gravatar' => Live_Chat::gravatar_from_hash($user['gravatar'])); }
/** * Init Screets Chat when WordPress Initialises * * @access public * @return void */ function init() { global $sc_chat_default_opts; // Start session sc_chat_session_start(); // Get default options $this->default_opts = $sc_chat_default_opts; // Get options $this->opts = sc_chat_get_options(); // WPML support if (function_exists('icl_register_string')) { $this->WPML(); } // Variables $this->skin_url = apply_filters('sc_chat_skin_url', 'skins/basic'); // Classes/actions loaded for the Frontend and for Ajax requests if (!is_admin() || defined('DOING_AJAX')) { add_action('wp_enqueue_scripts', array(&$this, 'init_frontend_scripts')); } if (is_admin() || defined('DOING_AJAX')) { // Register admin styles and scripts add_action('admin_print_scripts', array(&$this, 'init_backend_scripts')); } // Session control on user data add_action('wp_login', array(&$this, 'destroy_session')); add_action('wp_logout', array(&$this, 'destroy_session')); // Add operator name to user fields if (current_user_can('chat_with_users')) { add_action('show_user_profile', array(&$this, 'xtra_profile_fields'), 10); add_action('edit_user_profile', array(&$this, 'xtra_profile_fields'), 10); add_action('personal_options_update', array(&$this, 'save_xtra_profile_fields')); add_action('edit_user_profile_update', array(&$this, 'save_xtra_profile_fields')); } // Show Conversation Box add_action('wp_footer', array(&$this, 'show_chatbox')); // Shortcodes add_shortcode('chat_online', 'sc_chat_shortcode_online'); add_shortcode('chat_offline', 'sc_chat_shortcode_offline'); // Init action do_action('sc_chat_init'); }