<?php
/**
Compatibility functions for the strictlyautotags.class.php plugin 
*/

//error_reporting(E_ERROR);

// set up constants used by the AutoTag method
if(!defined('AUTOTAG_BOTH')){
	define('AUTOTAG_BOTH',0);
}
if(!defined('AUTOTAG_SHORT')){
	define('AUTOTAG_SHORT',1);
}
if(!defined('AUTOTAG_LONG')){
	define('AUTOTAG_LONG',2);
}

if(!function_exists('me')){

	// turn debug on for one IP only
	function me(){	
		
		$ip = "";           
		if (getenv("HTTP_CLIENT_IP")){ 
			$ip = getenv("HTTP_CLIENT_IP"); 
		}elseif(getenv("HTTP_X_FORWARDED_FOR")){
			$ip = getenv("HTTP_X_FORWARDED_FOR"); 			
		}elseif(getenv("REMOTE_ADDR")){
			$ip = getenv("REMOTE_ADDR");
		}else {
			$ip = "NA";
		}
		
		// put your IP here
		if($ip == "223.4.12.1" || $ip == "61.3.121.27"){				
			return true;
		}else{
			return false;
		}

	}
}


if(!function_exists('ShowDebugAutoTag')){

	// if the DEBUG constant hasn't been set then create it and turn it off
	if(!defined('DEBUGAUTOTAG')){
		if(me()){
			define('DEBUGAUTOTAG',false);
		}else{
			define('DEBUGAUTOTAG',false);
		}
	}

	/**
	 * function to output debug to page
	 *
	 * @param string $msg
	 */
	function ShowDebugAutoTag($msg){
		if(DEBUGAUTOTAG){			
			if(is_array($msg)){
				print_r($msg);
				echo "<br />";
			}else if(is_object($msg)){
				var_dump($msg);
				echo "<br />";
			}else if(is_string($msg)||is_numeric($msg)){
				echo htmlspecialchars($msg) . "<br>";
			}		
		}
	}
}



if(!function_exists('IsNothing')){

	/**
	 * Checks whether a value is set and not empty
	 *
	 * @param variant $val
	 * @return boolean
	 */
	function IsNothing($val){
		if(isset($val)){
			if(!empty($val)){
				return false;
			}
		}
		return true;		
	}
}

if(!function_exists('StringLenSort')){


	/**
	 * used when sorting array by string
	 *
	 * @param array $a
	 * @param array $b
	 * @return integer
	 */
	
	function StringLenSort($a, $b) {
		return strlen($b)-strlen($a);
	}
}

if(!function_exists('Strictly_GetDomain')){
	/**
	 * function to output the domain from a URL, used within a preg_replace replacement statement
	 *
	 * @param string $link
	 * @param string $url
	 * @return string
	 */
	function Strictly_GetDomain($link,$url)
	{
		ShowDebugAutoTag("get domain from $link and $url");
		
		$domain = preg_replace("@^https?://@","",$url);

		ShowDebugAutoTag("domain now $domain");

		$domain = preg_replace("@(^.+?)(\/.+?$)@","$1",$domain);

		ShowDebugAutoTag("domain now $domain");
		ShowDebugAutoTag("replace middle of link = $link with $domain");

		$domain = preg_replace("@('>)(.+?)(<\/a>)@","'>".$domain."</a>",$link);

		return $domain;
	}
	
}


if(!function_exists('Strictly_GetWeakDomain')){
	/**
	 * function to output the domain from a URL, used within a preg_replace replacement statement
	 *
	 * @param string $link
	 * @param string $url
	 * @return string
	 */
	function Strictly_GetWeakDomain($link,$url)
	{
		ShowDebugAutoTag("get domain from $link and $url");
		
		$link = preg_replace("@href='@i","href='//",$link);
		$domain = preg_replace("@^https?://@","",$url);
		$domain = preg_replace("@^www\.@","",$domain);

		ShowDebugAutoTag("domain now $domain");

		$domain = preg_replace("@(^.+?)(\/.+?$)@","$1",$domain);

		ShowDebugAutoTag("domain now $domain");
		ShowDebugAutoTag("replace middle of link = $link with $domain");

		$domain = preg_replace("@('>)(.+?)(<\/a>)@","'>".$domain."</a>",$link);
		
		ShowDebugAutoTag("RETURN DOMAIN = $domain");

		return $domain;
	}
}

// handle any future wordpress update which may or may not remove add_filters and add_actions

if ( !function_exists('add_filters') ) {
	function add_filters($tags, $function_to_add, $priority = 10, $accepted_args = 1) {
		if ( is_array($tags) ) {
			foreach ( (array) $tags as $tag ) {
				add_filter($tag, $function_to_add, $priority, $accepted_args);
			}
			return true;
		} else {
			return add_filter($tags, $function_to_add, $priority, $accepted_args);
		}
	}
}

if ( !function_exists('add_actions') ) {
	function add_actions($tags, $function_to_add, $priority = 10, $accepted_args = 1) {
		return add_filters($tags, $function_to_add, $priority, $accepted_args);
	}
}
?>