<?php
/*
Plugin Name: UserChat phpLoaded
Plugin URI: http://phploaded.com/project/userchat-wp.html
Description: Plugin that creates a modern one-on-one chat system among the registered users.
Version: 1.1
Author: Satish Kumar Sharma
Author Email: satish@phploaded.com
Author URI: https://github.com/phploaded/
License:     GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Text Domain: userchat-phploaded
Domain Path: /lang
*/


function userchat_install(){

/* deleting old config file */
$file = plugin_dir_path( __FILE__ ).'/dbconfig.php';
if(file_exists($file)){
unlink($file);
}

/* creating new config file */
global $wpdb;
$user = $wpdb->dbuser;
$pass = $wpdb->dbpassword;
$db = $wpdb->dbname;
$host = $wpdb->dbhost;
$domain = plugin_dir_url( __FILE__ );
$prefix = $wpdb->prefix;
$content = "<?php 
\$username=\"$user\";
\$password=\"$pass\";
\$database=\"$db\";
\$host=\"$host\";
\$domain=\"$domain\";
\$prefix=\"$prefix\";
?>";
$fp = fopen($file,"wb");
fwrite($fp,$content);
fclose($fp);

/* running sql queries */
$con = mysqli_connect($host, $user, $pass, $db);

if(mysqli_connect_errno()){
echo "Failed to connect to MySQLi in userchat-phploaded plugin: " . mysqli_connect_error();
}

mysqli_query($con, "SET SQL_MODE = \"NO_AUTO_VALUE_ON_ZERO\"");

mysqli_query($con, "CREATE TABLE IF NOT EXISTS `".$prefix."contacts` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `uid` varchar(100) CHARACTER SET utf8 NOT NULL,
  `fid` varchar(100) CHARACTER SET utf8 NOT NULL,
  `r` int(1) NOT NULL DEFAULT '0',
  `a` int(1) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=63");

mysqli_query($con, "CREATE TABLE IF NOT EXISTS `".$prefix."frequests` (
  `uid` varchar(100) CHARACTER SET utf8 NOT NULL,
  `from` varchar(100) NOT NULL,
  `time` int(11) NOT NULL DEFAULT '0',
  `msg` varchar(5000) CHARACTER SET utf8 NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1");

mysqli_query($con, "CREATE TABLE IF NOT EXISTS `".$prefix."userchat` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `from` varchar(100) NOT NULL,
  `to` varchar(100) NOT NULL,
  `msg` text NOT NULL,
  `sent` int(11) NOT NULL DEFAULT '0',
  `read` tinyint(1) NOT NULL DEFAULT '0',
  `fx` int(1) NOT NULL DEFAULT '0',
  `tx` int(1) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  KEY `to` (`to`),
  KEY `from` (`from`),
  KEY `read` (`read`),
  KEY `sent` (`sent`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=5");

mysqli_query($con, "ALTER TABLE `".$prefix."users` ADD `userchat` INT NOT NULL DEFAULT '0'");

}

register_activation_hook(__FILE__,'userchat_install'); 



function userchat_uninstall(){

global $wpdb;
$user = $wpdb->dbuser;
$pass = $wpdb->dbpassword;
$db = $wpdb->dbname;
$host = $wpdb->dbhost;
$domain = plugin_dir_url( __FILE__ );
$prefix = $wpdb->prefix;

/* running sql queries */
$con = mysqli_connect($host, $user, $pass, $db);

if(mysqli_connect_errno()){
echo "Failed to connect to MySQLi in userchat plugin: " . mysqli_connect_error();
}
mysqli_query($con, "SET SQL_MODE = \"NO_AUTO_VALUE_ON_ZERO\""); 
mysqli_query($con, "ALTER TABLE `".$prefix."users` DROP `userchat`");
mysqli_query($con, "DROP TABLE `".$prefix."contacts`, `".$prefix."frequests`, `".$prefix."userchat`");
}

register_uninstall_hook(__FILE__,'userchat_uninstall'); 



function userchat_init_scripts(){
wp_enqueue_script( 'jquery' );
wp_register_style( 'userchat-css', plugins_url('userchat.css', __FILE__) );
wp_enqueue_style( 'userchat-css' );
wp_register_style( 'perfect-scrollbar-css', plugins_url('perfect-scrollbar.min.css', __FILE__) );
wp_enqueue_style( 'perfect-scrollbar-css' );
wp_register_script( 'json2', plugins_url('json2.js', __FILE__) );
wp_enqueue_script( 'json2' );
wp_register_script( 'jstorage', plugins_url('jstorage.js', __FILE__) , array( 'jquery' ) );
wp_enqueue_script( 'jstorage' );
wp_register_script( 'perfect-scrollbar-js', plugins_url('perfect-scrollbar.jquery.min.js', __FILE__) , array( 'jquery' ) );
wp_enqueue_script( 'perfect-scrollbar-js' );
wp_register_script( 'userchat-scripts', plugins_url('userchat.js', __FILE__), array( 'jquery' ) );
wp_enqueue_script( 'userchat-scripts' );
}


function userchat_js_vars(){
$xhtml = '';
$xoptions = array('blueticon','simple','logo');
$options = get_option('iconsets', $xoptions);

if(is_array($options)){
foreach($options as $key => $value){
if($xhtml==''){
$xhtml = "'".$key."'";
} else {
$xhtml = $xhtml.", '".$key."'";
}
}
}

if(get_option('default_iconset')==''){
$default_iconset = 'simple';
} else {
$default_iconset = get_option('default_iconset');
}

if(get_option('update_timer')==''){
$update_timer = '10';
} else {
$update_timer = get_option('update_timer');
}

if(get_option('enable_debug')==''){
$enable_debug = 'false';
} else {
$enable_debug = get_option('enable_debug');
}

if(get_option('max_debug_lines')<1){
$max_debug_lines = '200';
} else {
$max_debug_lines = get_option('max_debug_lines');
}

if(get_option('cache_enable')==''){
$cache_enable = 'true';
} else {
$cache_enable = get_option('cache_enable');
}

if(get_option('cache_expire')==''){
$cache_expire = '3600';
} else {
$cache_expire = get_option('cache_expire');
}

if(get_option('cache_write')==''){
$cache_write = '5';
} else {
$cache_write = get_option('cache_write');
}

if(get_option('list_mode')==''){
$list_mode = 'all';
} else {
$list_mode = get_option('list_mode');
}

echo'<script>
userchat_domain = \''.plugin_dir_url( __FILE__ ).'\';
userchat_smiley_default_pack = \''.$default_iconset.'\';
userchat_smiley_packs = new Array('.$xhtml.');
userchat_enable_debug = '.$enable_debug.';
userchat_debug_max_lines = '.$max_debug_lines.';
userchat_enable_cache = '.$cache_enable.';
userchat_cache_expire = 1000*'.$cache_expire.';
userchat_cache_write_interval = 1000*'.$cache_write.';
userchat_update_timer = '.$update_timer.';
userchat_list_mode = \''.$list_mode.'\';
userchat_current_user = \''.get_current_user_id().'\';
userchat_admin_url = \''.get_admin_url().'\';
userchat_cid = \''.md5(get_bloginfo('url').'_'.get_current_user_id()).'\';
</script>';
}



function userchat_setting_page(){
include('userchat_settings.php');
}

function userchat_manage_chat(){
include('manage_userchat.php');
}

function userchat_docs(){
include('docs.php');
}

function userchat_admin_actions() {  
add_menu_page('UserChat Settings', 'UserChat Settings','manage_options','userchat_settings.php', 'userchat_setting_page', 'dashicons-format-chat' ,500); 

add_submenu_page( 'userchat_settings.php', 'Documentation', 'Documentation', 'manage_options', 'docs.php', 'userchat_docs' );
}

function userchat_login_head() {
if(!isset($logout)){$logout='';} 
echo '<script>
jQuery(document).ready(function(){
jQuery.jStorage.flush();
'.$logout.'
});
</script>';

}
add_action('login_head', 'userchat_login_head');


function userchat_login_js(){
wp_enqueue_script( 'jquery' );
wp_register_script( 'json2', plugins_url('json2.js', __FILE__) );
wp_enqueue_script( 'json2' );
wp_register_script( 'jstorage', plugins_url('jstorage.js', __FILE__) , array( 'jquery' ) );
wp_enqueue_script( 'jstorage' );
}

add_action( 'login_enqueue_scripts', 'userchat_login_js', 1 );


function register_userchat_settings() {
register_setting( 'userchat', 'default_iconset' );
register_setting( 'userchat', 'iconsets' );
register_setting( 'userchat', 'enable_debug' );
register_setting( 'userchat', 'max_debug_lines' );
register_setting( 'userchat', 'cache_enable' );
register_setting( 'userchat', 'cache_expire' );
register_setting( 'userchat', 'cache_write' );
register_setting( 'userchat', 'update_timer' );
register_setting( 'userchat', 'list_mode' );
}

function userchat_contacts_list( $atts ){
return '<div class="userchat-people-list"></div>';
}


if ( is_admin() ){
add_action('admin_enqueue_scripts', 'userchat_init_scripts');
add_action('admin_head', 'userchat_js_vars');
add_action('admin_menu', 'userchat_admin_actions');
add_action( 'admin_init', 'register_userchat_settings' );
} else {
add_action('wp_enqueue_scripts', 'userchat_init_scripts');
add_action('wp_head', 'userchat_js_vars');
add_shortcode( 'userchat_contacts', 'userchat_contacts_list' );
}
?>