<?php
class check {

	var $time;
	var $LE;
	var $Priority;
	var $Encoding;
	var $CharSet;
	var $uniq_id;
	var $boundary;
	
	function check()
	{
		$this->time			= date("Y-m-d");
		$this->LE			= "\n";
		$this->Priority		= '3';
		$this->Encoding		= "8bit";
		$this->CharSet		= "iso-8859-1";
		$this->uniq_id		= md5(uniqid(time()));
		$this->boundary[1]	= "b1_" . $this->uniq_id;
		$this->boundary[2]	= "b2_" . $this->uniq_id;
	}
	
	function set_mail_environment()
	{
		$this->time			= date("Y-m-d");
		$this->LE			= "\n";
		$this->Priority		= '3';
		$this->Encoding		= "8bit";
		$this->CharSet		= "iso-8859-1";
		$this->uniq_id		= md5(uniqid(time()));
		$this->boundary[1]	= "b1_" . $this->uniq_id;
		$this->boundary[2]	= "b2_" . $this->uniq_id;
	}

	function header_line($name, $value)
	{
		return $name . ": " . $value . $this->LE;
	}

	function text_line($value) 
	{
		return $value . $this->LE;
	}

	function rfc_date() 
	{
		$tz		= date("Z");
		$tzs	= ($tz < 0) ? "-" : "+";
		$tz		= abs($tz);
		$tz		= ($tz/3600)*100 + ($tz%3600)/60;
		$result = sprintf("%s %s%04d", date("D, j M Y H:i:s"), $tzs, $tz);
		return $result;
	}

	function create_header($from, $fromname, $to, $toname)
	{
		$result	 = "";
		$result .= $this->header_line("Date", $this->rfc_date());
		if($fromname == "")
			$result .= $this->header_line("Return-Path", trim($from));
		else
			$result .= $this->header_line("Return-Path", trim($fromname));
/*
		$tos		= array();
		$tos[0][0]	= trim($to);
		$tos[0][1]	= $toname;
		$result		.= $this->address_append("To", $tos);
*/
		$froms		 = array();
		$froms[0][0] = trim($from);
		$froms[0][1] = $fromname;
		$result		 .= $this->address_append("From", $froms); 


		$result .= sprintf("Message-ID: <%s@%s>%s", $this->uniq_id, $_SERVER["REMOTE_ADDR"], $this->LE);
		$result .= $this->header_line("X-Priority", $this->Priority);
		$result .= $this->header_line("X-Mailer", "ArmiaMailer");

		$result .= $this->header_line("MIME-Version", "1.0");
		$result .= $this->header_line("Content-Type", "multipart/alternative;");
		$result .= $this->text_line("\tboundary=\"" . $this->boundary[1] . '"');
		return $result;
	}

	function create_body($body, $altbody)
	{
		$result .= $this->get_boundary($this->boundary[1], $this->CharSet, "text/plain", $this->Encoding);
		$result .= $this->encode_string($altbody, $this->Encoding);
		$result .= $this->LE.$this->LE;
		if ($body <> "") {
			$result .= $this->get_boundary($this->boundary[1], $this->CharSet, "text/html", $this->Encoding);
			$result .= $this->encode_string($body, $this->Encoding);
			$result .= $this->LE.$this->LE;
		}
		$result .= $this->end_boundary($this->boundary[1]);
		return $result;
	}

	function address_append($type, $addr)
	{
		$addr_str = $type . ": ";
		$addr_str .= $this->address_format($addr[0]);
		if(count($addr) > 1)
		{
			for($i = 1; $i < count($addr); $i++)
				$addr_str .= ", " . $this->address_format($addr[$i]);
		}
		$addr_str .= $this->LE;
		return $addr_str;
	}

	function address_format($addr) 
	{ 
		if(empty($addr[1])) {
			$formatted = $addr[0];
		} else {
			$formatted = $this->encode_header($addr[1], 'phrase') . " <" . 
						 $addr[0] . ">";
		}
		return $formatted;
	}

	function encode_header ($str, $position = 'text') 
	{
	  $x = 0;
	  switch (strtolower($position)) {
		case 'phrase':
		  if (!preg_match('/[\200-\377]/', $str)) {
			// Can't use addslashes as we don't know what value has magic_quotes_sybase.
			$encoded = addcslashes($str, "\0..\37\177\\\"");

			if (($str == $encoded) && !preg_match('/[^A-Za-z0-9!#$%&\'*+\/=?^_`{|}~ -]/', $str))
			  return ($encoded);
			else
			  return ("\"$encoded\"");
		  }
		  $x = preg_match_all('/[^\040\041\043-\133\135-\176]/', $str, $matches);
		  break;
		case 'comment':
		  $x = preg_match_all('/[()"]/', $str, $matches);
		  // Fall-through
		case 'text':
		default:
		  $x += preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $str, $matches);
		  break;
	  }

	  if ($x == 0)
		return ($str);

	  $maxlen = 75 - 7 - strlen($this->CharSet);
	  // Try to select the encoding which should produce the shortest output
	  if (strlen($str)/3 < $x) {
		$encoding = 'B';
		$encoded = base64_encode($str);
		$maxlen -= $maxlen % 4;
		$encoded = trim(chunk_split($encoded, $maxlen, "\n"));
	  } else {
		$encoding = 'Q';
		$encoded = $this->encode_Q($str, $position);
		$encoded = $this->wrap_text($encoded, $maxlen, true);
		$encoded = str_replace("=".$this->LE, "\n", trim($encoded));
	  }

	  $encoded = preg_replace('/^(.*)$/m', " =?".$this->CharSet."?$encoding?\\1?=", $encoded);
	  $encoded = trim(str_replace("\n", $this->LE, $encoded));
	  
	  return $encoded;
	}

    function encode_Q ($str, $position = "text") 
	{
        // There should not be any EOL in the string
        $encoded = preg_replace("[\r\n]", "", $str);

        switch (strtolower($position)) {
          case "phrase":
            $encoded = preg_replace("/([^A-Za-z0-9!*+\/ -])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
            break;
          case "comment":
            $encoded = preg_replace("/([\(\)\"])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
          case "text":
          default:
            // Replace every high ascii, control =, ? and _ characters
            $encoded = preg_replace('/([\000-\011\013\014\016-\037\075\077\137\177-\377])/e',
                  "'='.sprintf('%02X', ord('\\1'))", $encoded);
            break;
        }
        
        // Replace every spaces to _ (more readable than =20)
        $encoded = str_replace(" ", "_", $encoded);
        return $encoded;
    }

	function end_boundary($boundary) 
	{
		return $this->LE . "--" . $boundary . "--" . $this->LE; 
	}

	function get_boundary($boundary, $charSet="iso-8859-1", $contentType="text/plain", $encoding="8bit") 
	{
		$result = "";

		$result .= $this->text_line("--" . $boundary);
		$result .= sprintf("Content-Type: %s; charset = \"%s\"", 
							$contentType, $charSet);
		$result .= $this->LE;
		$result .= $this->header_line("Content-Transfer-Encoding", $encoding);
		$result .= $this->LE;
	   
		return $result;
	}

	function encode_string ($str, $encoding = "base64") 
	{
		$encoded = "";
		switch(strtolower($encoding)) {
		  case "base64":
			  // chunk_split is found in PHP >= 3.0.6
			  $encoded = chunk_split(base64_encode($str), 76, $this->LE);
			  break;
		  case "7bit":
		  case "8bit":
			  $encoded = $this->fix_eol($str);
			  if (substr($encoded, -(strlen($this->LE))) != $this->LE)
				$encoded .= $this->LE;
			  break;
		  case "binary":
			  $encoded = $str;
			  break;
		  case "quoted-printable":
			  $encoded = $this->encode_QP($str);
			  break;
		  default:
			  $encoded = $str;
			  break;
		}
		return $encoded;
	}

	function fix_eol($str) 
	{
		$str = str_replace("\r\n", "\n", $str);
		$str = str_replace("\r", "\n", $str);
		$str = str_replace("\n", $this->LE, $str);
		return $str;
	}

	function encode_QP ($str) 
	{
		$encoded = $this->fix_eol($str);
		if (substr($encoded, -(strlen($this->LE))) != $this->LE)
			$encoded .= $this->LE;

		// Replace every high ascii, control and = characters
		$encoded = preg_replace('/([\000-\010\013\014\016-\037\075\177-\377])/e',
				  "'='.sprintf('%02X', ord('\\1'))", $encoded);
		// Replace every spaces and tabs when it's the last character on a line
		$encoded = preg_replace("/([\011\040])".$this->LE."/e",
				  "'='.sprintf('%02X', ord('\\1')).'".$this->LE."'", $encoded);

		// Maximum line length of 76 characters before CRLF (74 + space + '=')
		$encoded = $this->wrap_text($encoded, 74, true);

		return $encoded;
	}

	function wrap_text($message, $length, $qp_mode = false) 
	{
		$soft_break = ($qp_mode) ? sprintf(" =%s", $this->LE) : $this->LE;

		$message = $this->fix_eol($message);
		if (substr($message, -1) == $this->LE)
			$message = substr($message, 0, -1);

		$line = explode($this->LE, $message);
		$message = "";
		for ($i=0 ;$i < count($line); $i++)
		{
		  $line_part = explode(" ", $line[$i]);
		  $buf = "";
		  for ($e = 0; $e<count($line_part); $e++)
		  {
			  $word = $line_part[$e];
			  if ($qp_mode and (strlen($word) > $length))
			  {
				$space_left = $length - strlen($buf) - 1;
				if ($e != 0)
				{
					if ($space_left > 20)
					{
						$len = $space_left;
						if (substr($word, $len - 1, 1) == "=")
						  $len--;
						elseif (substr($word, $len - 2, 1) == "=")
						  $len -= 2;
						$part = substr($word, 0, $len);
						$word = substr($word, $len);
						$buf .= " " . $part;
						$message .= $buf . sprintf("=%s", $this->LE);
					}
					else
					{
						$message .= $buf . $soft_break;
					}
					$buf = "";
				}
				while (strlen($word) > 0)
				{
					$len = $length;
					if (substr($word, $len - 1, 1) == "=")
						$len--;
					elseif (substr($word, $len - 2, 1) == "=")
						$len -= 2;
					$part = substr($word, 0, $len);
					$word = substr($word, $len);

					if (strlen($word) > 0)
						$message .= $part . sprintf("=%s", $this->LE);
					else
						$buf = $part;
				}
			  }
			  else
			  {
				$buf_o = $buf;
				$buf .= ($e == 0) ? $word : (" " . $word); 

				if (strlen($buf) > $length and $buf_o != "")
				{
					$message .= $buf_o . $soft_break;
					$buf = $word;
				}
			  }
		  }
		  $message .= $buf . $this->LE;
		}

		return $message;
	}

	function check_amount($amount="")
	{
		$flag	= true;
		$normal = "~^[0-9]+(\.[0-9]+)*$~i";
		if (!preg_match($normal, $amount))
			$flag	= false;
		return $flag;			
	}
	
	function check_numerals($text="")
	{
		$flag	= true;
		$normal = "~^[0-9]+$~i";
		if (!preg_match($normal, $text))
			$flag	= false;
		return $flag;			
	}

	function check_username($name="")
	{
		$flag	= true;
		$normal = "~^[a-zA-Z0-9\.-\_]+$~i";
		if (!preg_match($normal, $name))
			$flag	= false;
		return $flag;			
	}

//	function check_date($date="")
//	{
//            echo"dd",$date;
//		$flag	= true;
//		$normal="~^[0-9]{1,2}-[0-9]{1,2}-[0-9]{4}$~i";
//                //$normal="/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/";
//		//$normal = "^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}$"; 
//		if (!preg_match($normal, $date))
//			$flag	= false;
//		return $flag;			
//	}
        function check_date($date=""){
           $flag	=   true;
           $arrDate     =   explode("/",$date);
           list($month,$date,$year) =   $arrDate;
           return checkdate($month,$date,$year);
            //echo $var;exit;
        }

	function check_date_time($date="")
	{
		$flag	= true;
		$normal = "~^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2} [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}$~i";
		if (!preg_match($normal, $date))
			$flag	= false;
		return $flag;			
	}

	function check_email($email="")
	{
		$flag	= true;
		$normal = "~^[a-z0-9_\+-]+(\.[a-z0-9_\+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,4})$~i";
		if (!preg_match($normal, $email))
			$flag	= false;
		return $flag;			
	}

      function check_email_available($email="",$id="")

	{

		$flag	= false;

	        $sql	= "SELECT * FROM ".TABLEPREFIX."user

				   WHERE vEmail LIKE '".addslashes($email)."' AND nUser_Id NOT LIKE '".$id."' AND vStatus NOT LIKE 'D%'";
               
               //echo $sql;
		$res	= @mysql_query($sql, $this->dblink);


		if (@mysql_num_rows($res) > 0) {

			$flag	= true;

		}

		return $flag;

	}
	function generate_checkbox_list($name='checkbox', $selectvalue='val', $arrEntries=array(), $count=3, $typeFlag='value')
	{
		$string		= "";
		$arrValues	= array();
		$arrValues	= explode(",", $selectvalue);
		$index		= 1;
		$string		.= '<table width="98%"  border="0" cellspacing="0" cellpadding="4">';

		foreach ($arrEntries as $key => $value) {
			if ($index == 1) {
				$string	.= "<tr>";
			}
			$string	.= "<td align='left'>";
			if ($typeFlag == 'key') {
				$valueval	= $key;
				$check		= in_array($key, $arrValues) ? "checked" : "";
			} else {
				$check		= in_array($value, $arrValues) ? "checked" : "";
				$valueval	= $value;
			}
			$string	.= "<input type='checkbox' name='".$name."' value='".$valueval."' ".$check." class='inputbox'>".$value;
			$string	.= "</td>";
			if ($index	== $count) {
				$string	.= "</tr>";
				$index	= 1;
			} else {
				$index++;
			}
		}
		$string	.= "</table>";
		return $string;
	}

	function generate_checkbox_list_js($name='checkbox', $selectvalue='val', $arrEntries=array(), $count=3, $typeFlag='value')
	{
		$string		= "";
		$arrValues	= array();
		$arrValues	= explode(",", $selectvalue);
		$index		= 1;
		$string		.= '<table width="98%"  border="0" cellspacing="0" cellpadding="4">';

		foreach ($arrEntries as $key => $value) {
			if ($index == 1) {
				$string	.= "<tr>";
			}
			$string	.= "<td align='left'>";
			if ($typeFlag == 'key') {
				$valueval	= $key;
				$check		= in_array($key, $arrValues) ? "checked" : "";
			} else {
				$check		= in_array($value, $arrValues) ? "checked" : "";
				$valueval	= $value;
			}
			if ($valueval == "") 
				$string	.= "<input type='checkbox' name='".$name."' value='".$valueval."' ".$check." onClick='check_option_dosent_matter(this)'; class='inputbox'>".$value;
			else
				$string	.= "<input type='checkbox' name='".$name."' value='".$valueval."' ".$check." onClick='check_option(this)'; class='inputbox'>".$value;
			$string	.= "</td>";
			if ($index	== $count) {
				$string	.= "</tr>";
				$index	= 1;
			} else {
				$index++;
			}
		}
		$string	.= "</table>";
		return $string;
	}

	function generate_password($length=8)
	{
		$password =  chr(mt_rand(65,90));
		for($k=0; $k < $length - 1; $k++) {
			$probability = mt_rand(1,10);
			if($probability <= 8)
				$password .= chr(mt_rand(97,122));
			else
				$password .= chr(mt_rand(48, 57));
		}
		return $password;
	}

	function remove_script($text)
	{
		$spos	= 0;
		$spos	= strpos($text, "<script");
		if ($spos===false) {}
		else $text	= substr($text, 0, $spos);
		return $text;
	}

	function get_script($text)
	{
		$spos	= 0;
		$spos	= strpos($text, "<script");
		if ($spos === false) {$text="";}
		else $text	= substr($text, -(strlen($text)-$spos));
		return $text;
	}

	function get_sub_string($from="", $to="", $string="")
	{
		$pos	= strpos($string, $from);
		if ($pos === false) {
			$return	= "";
		} else {
			$pos1	= strpos($string, $to);
			if ($pos1 === false) {
				$return	= "";
			} else {
				$return	= substr($string, $pos+strlen($from), $pos1-($pos+strlen($to)-1));
			}
		}
		return $return;
	}

	function get_age_from_date ($YYYYMMDD_In)
	{
		$yIn	= substr($YYYYMMDD_In, 0, 4);
		$mIn	= substr($YYYYMMDD_In, 4, 2);
		$dIn	= substr($YYYYMMDD_In, 6, 2);

		$ddiff = date("d") - $dIn;
		$mdiff = date("m") - $mIn;
		$ydiff = date("Y") - $yIn;

		// Check If Birthday Month Has Been Reached
		if ($mdiff < 0)
		{
			// Birthday Month Not Reached
			// Subtract 1 Year From Age
			$ydiff--;
		} elseif ($mdiff==0) {
			// Birthday Month Currently
			// Check If BirthdayDay Passed
			if ($ddiff < 0)
			{
			  //Birthday Not Reached
			  // Subtract 1 Year From Age
			  $ydiff--;
			}
		}
		return $ydiff;
	} 

	function get_height($nHeight="100")
	{
			$vFeet	= floor($nHeight/12);
			$vInch	= $nHeight%12;
			return $vFeet." Ft ".$vInch." in.";
	}

	function get_html_mail($basepath="./",$mode = null)
	{
                $html	= "";
                if($mode == null)
		$filename	= $basepath."/emailtemp/mail.html";
                else
                $filename	= $basepath."/emailtemp/plainmail.html";
		$handle		= fopen($filename, "rb");
		$html		= fread($handle, filesize($filename));
		fclose($handle);
                
		return $html;
	}

        function get_html_mail_tell_friends($basepath="./",$mode = null)
	{
                $html	= "";
		$filename	= $basepath."/emailtemp/mail_tell_friend.html";
		$handle		= fopen($filename, "rb");
		$html		= fread($handle, filesize($filename));
		fclose($handle);

		return $html;
	}

	function get_html_mail2($basepath="./")
	{
		$html	= "";
		$filename	= $basepath."/emailtemp/mail_admin.html";
		$handle		= fopen($filename, "rb");
		$html		= fread($handle, filesize($filename));
		fclose($handle);
		return $html;
	}


	function parse_html_mail($arrSearch=array(), $arrReplace=array(), $html="")
	{
		return str_replace($arrSearch, $arrReplace, $html);
	}

	function html_mail($basepath="./", $arrSearch=array(), $arrReplace=array(),$mode = null)
	{
                if($mode=='Tell A Friend'){
                    $html	= $this->get_html_mail_tell_friends($basepath,$mode);
                    $html	= $this->parse_html_mail($arrSearch, $arrReplace, $html);
                }else{
                    $html	= $this->get_html_mail($basepath,$mode);
                    $html	= $this->parse_html_mail($arrSearch, $arrReplace, $html);
                }
		return $html;
	}

	function html_mail2($basepath="./", $arrSearch=array(), $arrReplace=array())
	{
		$html	= $this->get_html_mail2($basepath);
		$html	= $this->parse_html_mail($arrSearch, $arrReplace, $html);
		return $html;
	}


}
?>