<?php
/*
+--------------------------------------------------------------------------
|   TanyaCMS - Question and Answer Sofware
|   -----------------------------------------------------------------------
|   by fbschool dev team
|   (c) 2011 - 2012 FBSCHOOL
|   http://www.fbschool.com/tanyacms
|   -----------------------------------------------------------------------
|   This is NOT a freeware, use is subject to license terms
|   Support: 13455n@gmail.com
+--------------------------------------------------------------------------
*/

class H
{

	/**
	 * Get the file extension
	 * @param string $filename File name
	 */
	
	public static function get_file_ext($filename)
	{
		$filename = explode('.', $filename);
		
		$ext_count = sizeof($filename) - 1;
		
		if ($filename[$ext_count] == 'jpeg' or $filename[$ext_count] == 'jpe')
		{
			$filename[$ext_count] = 'jpg';
		}
		
		if ($filename[$ext_count] == 'htm')
		{
			$filename[$ext_count] = 'html';
		}
		
		return strtolower($filename[$ext_count]);
	}

	/**
	 * Return JSON array
	 * 
	 * @param  $array 
	 */
	public static function ajax_json_output($array)
	{
		//HTTP::no_cache_header('text/javascript');
		
		echo str_replace(array("\r", "\n", "\t"), '', json_encode(H::sensitive_words($array)));
		exit;
	}

	/**
	 * Check your phone number is legitimate
	 * @param $moblie
	 * @return unknown_type
	 */
	public static function check_mobile_char($mobile)
	{	
		$mobile = trim($mobile);
		
		if (strlen($mobile) != 11)
		{
			return false;
		}
		
		$exp = '/^(((13[0-9]{1})|(15[0-9]{1}))+\d{8})/isU';
		
		return preg_match($exp, $mobile);
	
	}

	/**
	 * Whether the e-mail format
	 * @param $email
	 * @return bool
	 */
	public static function valid_email($email)
	{
		if (!$email)
		{
			return false;
		}
		
		//return strlen($email) > 6 && preg_match("/^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/", $email);
		
		return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $email)) ? FALSE : TRUE;
	}
	
	public static function redirect_msg($message, $url = NULL, $interval = 5)
	{		
		TPL::assign('message', $message);
		TPL::assign('url_bit', HTTP::parse_redirect_url($url));
		TPL::assign('interval', $interval);

		TPL::output('global/show_message');
		die;
	}

	/**
	 * Cryptographic hash, the hash generated string sent to the user
	 *
	 * @param array $hash_arr
	 * @return string
	 */
	public static function encode_hash($hash_arr, $hash_key = false)
	{
		if (empty($hash_arr))
		{
			return false;
		}
		
		$hash_str = "";
		
		foreach ($hash_arr as $key => $value)
		{
			$hash_str .= $key . "^]+" . $value . "!;-";
		}
		
		$hash_str = substr($hash_str, 0, - 3);
		
		// Interference code encryption, encryption and decryption when needed KEY
		if (! $hash_key)
		{
			$hash_key = GET_COOKIE_HASH_KEY;
		}
		
		// Encryption
		$tmp_str = '';
		
		for ($i = 1; $i <= strlen($hash_str); $i ++)
		{
			$char = substr($hash_str, $i - 1, 1);
			$keychar = substr($hash_key, ($i % strlen($hash_key)) - 2, 1);
			$char = chr(ord($char) + ord($keychar));
			$tmp_str .= $char;
		}
		
		$hash_str = base64_encode($tmp_str);
		$hash_str = str_replace(array(
			'+', 
			'/', 
			'='
		), array(
			'-', 
			'_', 
			'.'
		), $hash_str);
		//$hash_str	=	urlencode($hash_str);
		

		return $hash_str;
	}

	/**
	 * Decrypt the hash, the hash chain from the user back to decrypt the string inside the Content
	 *
	 * @param string $hash_str
	 * @param boolean $b_urldecode	When the $hash_str not pass through the browser when you need urldecode, otherwise, the decryption fails, and vice versa
	 * @return array
	 */
	public static function decode_hash($hash_str, $b_urldecode = false, $hash_key = false)
	{
		if (empty($hash_str))
		{
			return array();
		}
			
			// Interference code encryption, encryption and decryption when needed KEY
		if (! $hash_key)
		{
			$hash_key = GET_COOKIE_HASH_KEY;
		}
		
		//Decryption process
		$tmp_str = '';
		
		if (strpos($hash_str, "-") || strpos($hash_str, "_") || strpos($hash_str, "."))
		{
			$hash_str = str_replace(array(
				'-', 
				'_', 
				'.'
			), array(
				'+', 
				'/', 
				'='
			), $hash_str);
		}
		
		$hash_str = base64_decode($hash_str);
		
		for ($i = 1; $i <= strlen($hash_str); $i ++)
		{
			$char = substr($hash_str, $i - 1, 1);
			$keychar = substr($hash_key, ($i % strlen($hash_key)) - 2, 1);
			$char = chr(ord($char) - ord($keychar));
			$tmp_str .= $char;
		}
		
		$hash_arr = array();
		$arr = explode("!;-", $tmp_str);
		
		foreach ($arr as $value)
		{
			list($k, $v) = explode("^]+", $value);
			if ($k)
			{
				$hash_arr[$k] = $v;
			}
		}
		
		return $hash_arr;
	}

	/** Build Options **/
	public static function display_options($param, $default = '_DEFAULT_', $default_key='key')
	{
		$output = '';
		
		if (is_array($param))
		{
			$keyindex = 0;
			
			foreach ($param as $key => $value)
			{	
				if ($default_key=='value')
				{
					$output .= '<option value="' . $key . '"' . (($value == $default) ? '  selected' : '') . '>' . $value . '</option>';
				}
				else
				{
					$output .= '<option value="' . $key . '"' . (($key == $default) ? '  selected' : '') . '>' . $value . '</option>';
				}
			}
			
		}
		
		return $output;
	}
	
	public static function get_common_email($email)
	{
		if (!self::valid_email($email))
		{
			return false;
		}
		
		$email_domain = substr(stristr($email, '@'), 1);
		
		$common_email = (array)ASK_APP::config()->get('common_email');
		
		if ($common_email[$email_domain])
		{
			return $common_email[$email_domain];
		}
		else
		{
			return array(
					'name' => $email_domain,
					'url' => 'http://www.' . $email_domain,
			);
		}
	}

	/**
	 * Sensitive word substitution
	 * @param unknown_type $content
	 * @param unknown_type $replace
	 * @return mixed
	 */
	public static function sensitive_words($content, $replace = '*')
	{
		if (!$content or !get_setting('sensitive_words'))
		{
			return $content;
		}
		
		if (is_array($content) && !empty($content))
		{
			foreach($content as $key => $val)
			{
				$content[$key] = self::sensitive_words($val, $replace);
			}
			
			return $content;
		}
		
		$sensitive_words = explode("\n", get_setting('sensitive_words'));
	
		foreach($sensitive_words as $word)
		{
			$word = trim($word);
			
			if (empty($word))
			{
				continue;
			}
			
			$replace_str = '';
			
			$word_length = utf8_strlen($word);
	
			for($i = 0; $i < $word_length; $i++)
			{
				$replace_str .=  $replace;
			}
	
			$content = str_replace($word, $replace_str, $content);
		}

		return $content;
	}

	/**
	 * Contains sensitive words
	 * @param unknown_type $content
	 * @param unknown_type $replace
	 * @return mixed
	 */
	public static function sensitive_word_exists($content)
	{
		if (!$content or !get_setting('sensitive_words'))
		{
			return false;
		}
		
		if (is_array($content))
		{
			foreach($content as $key => $val)
			{
				$content[$key] = self::sensitive_word_exists($val);
			}
			
			return $content;
		}
		
		$sensitive_words = explode("\n", get_setting('sensitive_words'));
	
		foreach($sensitive_words as $word)
		{
			$word = trim($word);
			
			if (empty($word))
			{
				continue;
			}
			
			if (strstr($content, $word))
			{
				return true;
			}
		}

		return false;
	}
}