<div class="wrap"><?php 
error_reporting(0);
if($_GET['delpack']){
foreach(glob(plugin_dir_path( __FILE__ ).'smile/'.$_GET['delpack'].'/*.*') as $v){
unlink($v);
}
rmdir(plugin_dir_path( __FILE__ ).'smile/'.$_GET['delpack'].'/');
echo'<script>document.location.href=\'admin.php?page=userchat_settings.php\';</script>';
}


if($_FILES["zip_file"]["name"]) {
	$filename = $_FILES["zip_file"]["name"];
	$source = $_FILES["zip_file"]["tmp_name"];
	$type = $_FILES["zip_file"]["type"];
	
	$name = explode(".", $filename);
	$accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed');
	foreach($accepted_types as $mime_type) {
		if($mime_type == $type) {
			$okay = true;
			break;
		} 
	}
	
	$continue = strtolower($name[1]) == 'zip' ? true : false;
	if(!$continue) {
		$message = "<div class=\"error\"> <p>Invalid file type detected. Only zip files are allowed.</p></div>";
	}

	$target_path = plugin_dir_path( __FILE__ )."".$filename;  // change this to the correct site path
	if(move_uploaded_file($source, $target_path)) {
		$zip = new ZipArchive();
		$x = $zip->open($target_path);
		if ($x === true) {
			    if ($zip->locateName('userchat-pack.js', ZipArchive::FL_NODIR) !== false){
					$zip->extractTo(plugin_dir_path( __FILE__ )."smile/");
					$message = "<div class=\"updated\"> <p>Icon pack installed successfully.</p></div>";
					} else {
					$message = "<div class=\"error\"> <p>File does not seems like userchat icon pack. Installation aborted.</p></div>";
					}
			
			$zip->close();
	
			unlink($target_path);
		}
		
	} else {	
		$message = "<div class=\"error\"> <p>There was a problem with the upload. Please try again.</p></div>";
	}
}


if($message) {echo $message;}
?>
<h2>userchat Settings</h2>
<br /><br />
<form enctype="multipart/form-data" name="myupload" method="post" action="">
<h3 class="title">Upload Icon Pack</h3>
<p><input type="file" name="zip_file" /> <input type="submit" name="dsd" class="userchat-button" Value="Upload Now"></p>
<p class="description">You can get more icon packs from <a href="https://phploaded.com/userchat-iconpack-generator.html" target="_blank">https://phploaded.com/userchat-iconpack-generator.html</a></p>
</form>

<form enctype="multipart/form-data" id="userchat-form" name="userchat-form" method="post" action="options.php">
<?php 
settings_fields( 'userchat' );
do_settings_sections( 'userchat' );
$shtml = '';
$xhtml = '';
$options = array();
$options = get_option('iconsets');
$icon_packs = array();
foreach(glob(plugin_dir_path( __FILE__ ).'smile/*', GLOB_ONLYDIR) as $dir) {
$dirname = basename($dir);
if(file_exists(plugin_dir_path( __FILE__ ).'smile/'.$dirname.'/userchat-pack.js')){
$icon_packs[] = $dirname;	

$shtml = $shtml.'<option>'.$dirname.'</option>';
$xhtml = $xhtml.'<input type="checkbox" name="iconsets['.$dirname.']" value="1" /> '.$dirname.' (<a href="#" onclick="userchat_delete_pack(\''.$dirname.'\')">delete</a>)<br />';
}
}

?>
<br /><br />
<h3 class="title">Default Icon Pack</h3>
<p><select name="default_iconset"><?php echo $shtml; ?></select></p>
<p class="description">This is the default icon pack, which shows up when users click on smiley icon.</p>


<br /><br />
<h3 class="title">Activate OR disable Icon Packs</h3>
<p><?php echo $xhtml; ?></p>
<p class="description">Select icon packs that you want to be used by userchat users.</p>


<br /><br />
<h3 class="title">User listing mode</h3>
<p><select name="list_mode">
<option value="all">ALL MODE : List everyone (either online or offline).</option>
<option value="online">ONLINE MODE : List only online people (untested, may give errors)</option>
<option value="contacts">PERSONAL CONTACTS : List only personal list contacts (read more in documentation)</option>
</select></p>
<p class="description">Defaults to use all contacts. This options tells the script, whom to load in right sidebar. This applies to all users on your website.</p>


<br /><br />
<h3 class="title">Enable Debugging?</h3>
<p><select name="enable_debug"><option>true</option><option>false</option></select></p>
<p class="description"><b>To be used by userchat developer only</b>. Keep it to false otherwise a div with lot of debugging info will appear on top left of screen.</p>

<br /><br />
<h3 class="title">Max Debug lines?</h3>
<p><input type="text" name="max_debug_lines" value="<?php echo esc_attr( get_option('max_debug_lines','200') ); ?>" /></p>
<p class="description"><b>To be used by userchat developer only</b>. Keep it to 200. These are the no of lines that debug window will show, after that it will be reset to blank.</p>

<br /><br />
<h3 class="title">Enable Cache?</h3>
<p><select name="cache_enable"><option>true</option><option>false</option></select></p>
<p class="description"><b>To be used by userchat developer only</b>. Keep it to true otherwise performance will be decreased. userchat color themes will also not work.</p>

<br /><br />
<h3 class="title">Cache Expire</h3>
<p><input type="text" name="cache_expire" value="<?php echo esc_attr( get_option('cache_expire','3600') ); ?>" /></p>
<p class="description"><b>To be used by userchat developer only</b>. Default is 3600 seconds. These are the no of seconds after which the browser cache will expire.</p>

<br /><br />
<h3 class="title">Browser Cache Write Interval</h3>
<p><input type="text" name="cache_write" value="<?php echo esc_attr( get_option('cache_write','5') ); ?>" /></p>
<p class="description"><b>To be used by userchat developer only</b>. Default is 5 seconds. These are the no of seconds after which the contents of userchat are saved constantly to browser so that users can get a smooth experiance when browsing the website. Going below 2 seconds may cause browsers to crash, going above 10 seconds may lead to inconsistency.</p>


<br /><br />
<h3 class="title">Content Update timer</h3>
<p><input type="text" name="update_timer" value="<?php echo esc_attr( get_option('update_timer','10') ); ?>" /></p>
<p class="description">Constantly after a time delay, content of userchat is synchronised with servers. This defaults to 10 seconds. However, if you own a fast server(like VPS) you may want to reduce it to 2 or 1 seconds. If you are using shared hosting, you should keep it to 5-10 seconds. This purely depends how many online users you have and how powerful your server is. So you might need to tweak this to get best.</p>

<br /><br />
<h3 class="title">Clear current cache</h3>
<p><div class="userchat-button" onclick="clear_browser_cache()">Clear Browser Cache Now</div></p>
<p class="description">To clear the current cache, click the button above.</p>

<?php submit_button(); ?>
<script>
jQuery(document).ready(function(){
jQuery('#userchat-form [name="default_iconset"]').val('<?php echo esc_attr( get_option('default_iconset','simple') ); ?>');
jQuery('#userchat-form [name="enable_debug"]').val('<?php echo esc_attr( get_option('enable_debug','false') ); ?>');
jQuery('#userchat-form [name="cache_enable"]').val('<?php echo esc_attr( get_option('cache_enable','true') ); ?>');
jQuery('#userchat-form [name="list_mode"]').val('<?php echo esc_attr( get_option('list_mode','all') ); ?>');
<?php
foreach($options as $option => $val){
echo"jQuery('#userchat-form [name=\"iconsets[$option]\"]').attr('checked', true);\n";
}
?>
});
</script>
</form>
</div>