<?php
/*	
	
	WIDGET FILTERS:

    widget_title			- widget title
	
*/

// WIDGET CLASS
class Bizz_Widget_Domain_Check extends WP_Widget {

	/**
	 * Set up the widget's unique name, ID, class, description, and other options.
	 * @since 0.6
	 */
	function Bizz_Widget_Domain_Check() {		
		$widget_ops = array( 'classname' => 'domain-check', 'description' => __( 'Check for free domain names.', 'bizzthemes' ) );
		$control_ops = array( 'id_base' => "widgets-reloaded-bizz-domain-check" );
		$this->WP_Widget( "widgets-reloaded-bizz-domain-check", __( 'Domain Lookup', 'bizzthemes' ), $widget_ops, $control_ops );
	}

	/**
	 * Outputs the widget based on the arguments input through the widget controls.
	 * @since 0.6
	 */
	function widget( $args, $instance ) {
		extract( $args );
		
		$search_text = ( !empty($instance['search_text']) ) ? $instance['search_text'] : '';
		$custom_link = ( !empty($instance['custom_link']) ) ? $instance['custom_link'] : '';
		
		echo $before_widget;
		
		if ( $instance['title'] )
			echo $before_title . apply_filters( 'widget_title',  $instance['title'], $instance, $this->id_base ) . $after_title;
			
		$search = '<div class="form_domain_wrap">';
		$search .= '<form method="post" class="form_domain" action="' . home_url() . '/"><div>';
		$search .= '<input type="text" class="search_domain" name="domain" value="' . esc_attr( $search_text ) . '" onfocus="if(this.value==this.defaultValue)this.value=\'\';" onblur="if(this.value==\'\')this.value=this.defaultValue;" />';
		$search .= '<button class="main search-sprite"><!----></button>';
		$search .= '<div class="clearx search-sprite"><!----></div>';
		$search .= '<div class="loadingx"><!----></div>';
		$search .= '<input type="hidden" class="submit" name="submit" />';
		$search .= '<input type="hidden" name="clink" value="'.$custom_link.'" />';
		$search .= '</div></form>';
		$search .= '</div>';
		
		$search .= '<div class="results_domain"><!----></div>';

		echo $search;
		
		echo $after_widget;
	}

	/**
	 * Updates the widget control options for the particular instance of the widget.
	 * @since 0.6
	 */
	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
		$instance = $new_instance;
		
		$instance['title'] = strip_tags( $new_instance['title'] );
		$instance['search_text'] = strip_tags( $new_instance['search_text'] );
		$instance['custom_link'] = strip_tags( $new_instance['custom_link'] );
		
		return $instance;
	}

	/**
	 * Displays the widget control options in the Widgets admin screen.
	 * @since 0.6
	 */
	function form( $instance ) {

		//Defaults
		$defaults = array(
			'title' => __( 'Domain Lookup', 'bizzthemes' ), 
			'search_text' => 'enter domain name',
			'custom_link' => 'http://yoursite.com/?name=%1$s&amp;ext=%2$s'
		);
		$instance = wp_parse_args( (array) $instance, $defaults );
?>
		<div class="bizz-widget-controls">
		<p>
			<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'bizzthemes' ); ?></label>
			<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" />
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'search_text' ); ?>"><?php _e( 'Search Text:', 'bizzthemes' ); ?></label>
			<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'search_text' ); ?>" name="<?php echo $this->get_field_name( 'search_text' ); ?>" value="<?php echo $instance['search_text']; ?>" />
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'custom_link' ); ?>"><?php _e( 'Custom link:', 'bizzthemes' ); ?></label>
			<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'custom_link' ); ?>" name="<?php echo $this->get_field_name( 'custom_link' ); ?>" value="<?php echo $instance['custom_link']; ?>" />
		</p>
		</div>
		<div style="clear:both;">&nbsp;</div>
	<?php
	}
}

// INITIATE WIDGET
register_widget( 'Bizz_Widget_Domain_Check' );

/* AJAX domain search */
/*------------------------------------------------------------------*/
function bizz_ajax_domain_search() {

    $data = $_POST['data'];
	parse_str($data,$output);
	
	$domain_name = $output['domain'];
	$custom_link = $output['clink'];
		
	set_time_limit(0);
	ob_start();

	########### Extensions to be checked
	$extensions = array(
		'.com' 		=> array('whois.crsnic.net','No match for'),
		'.info' 	=> array('whois.afilias.net','NOT FOUND'),	
		'.net' 		=> array('whois.crsnic.net','No match for'),
		'.co.uk' 	=> array('whois.nic.uk','No match'),		
		'.nl' 		=> array('whois.domain-registry.nl','not a registered domain'),
		'.ca' 		=> array('whois.cira.ca', 'AVAIL'),
		'.name' 	=> array('whois.nic.name','No match'),
		'.ws'		=> array('whois.website.ws','No Match'),
		'.be' 		=> array('whois.ripe.net','No entries'),
		'.org' 		=> array('whois.pir.org','NOT FOUND'),
		'.biz' 		=> array('whois.biz','Not found'),
		'.tv' 		=> array('whois.nic.tv', 'No match for'),
	);
	###########
	
	$extensions = apply_filters( 'whois_list_args',  $extensions );
	
	if(isset($domain_name)) {
		
		$domain = str_replace(array('www.', 'http://'), NULL, $domain_name);
		
		if(strlen($domain) > 0) {
			foreach($extensions as $extension => $who) {
			
				if ( $custom_link != '' ) {
					$link_start = '<a href="'.sprintf($custom_link, $domain, $extension).'">';
					$link_end = '</a>';
				}
				else {
					$link_start = '';
					$link_end = '';
				}
				
				$buffer = NULL;
				$sock = fsockopen($who[0], 43) or die('Error Connecting To Server:' . $server);
				fputs($sock, $domain.$extension . "\r\n");
					
					while( !feof($sock) )
						$buffer .= fgets($sock,128);
					
				fclose($sock);
								
				if(eregi($who[1], $buffer))
					echo '<div class="status available">' . $link_start . $domain. '<b>' . $extension .'</b><div class="avail search-sprite"></div>'.$link_end.'</div>';
				else
					echo '<div class="status taken">' . $domain . '<b>' .$extension .'</b></div>';
								
				ob_flush();
				flush();
				sleep(0.3);
				
			}
		}
		else
			echo 'Please enter the domain name';
	}
	
	die;
}

add_action('wp_ajax_bizz_ajax_domain_search', 'bizz_ajax_domain_search');
add_action('wp_ajax_nopriv_bizz_ajax_domain_search', 'bizz_ajax_domain_search');