<?php

function tep_redirect($url, $enanle_SSL=false) {
    if ( ($enanle_SSL== true) && (getenv('HTTPS') == 'on') ) 
	{ // We are loading an SSL page
      if (substr($url, 0, strlen(HTTP_SERVER)) == HTTP_SERVER) 
	  { // NONSSL url
        $url = HTTPS_SERVER . substr($url, strlen(HTTP_SERVER)); // Change it to SSL
      }
    }
    header("Location: " . $url);
  }   

function get_state_name($state_id) {
		$query_comp		=	"select * from state_master 
							where st_id='".$state_id."'";
		$recresult_comp	= 	tep_db_query($query_comp,init());
		$intcount_comp	= 	tep_db_num_rows($recresult_comp); 	
		if($intcount_comp==0) {
			return "";
		} else {
			$obj_row_comp 	= tep_db_fetch_array($recresult_comp);
			return	stripslashes($obj_row_comp['st_details']);
		}
  }
  
  function get_district_name($district_id) {
		$query_comp		=	"select * from district_master 
							where district_id='".$district_id."'";
		$recresult_comp	= 	tep_db_query($query_comp,init());
		$intcount_comp	= 	tep_db_num_rows($recresult_comp); 	
		if($intcount_comp==0) {
			return "";
		} else {
			$obj_row_comp 	= tep_db_fetch_array($recresult_comp);
			return	stripslashes($obj_row_comp['district_details']);
		}
  }

function fill_combo($id_field, $name_field, $table_name, $selected_value, $condition="1=1") {
	$query	=	"select ".$id_field.", ".$name_field." from ".$table_name."
				Where ".$condition." 
				Order by ".$name_field;
		
	$recresult = tep_db_query($query,init());
	
	$str_option	=	"";
	while( $obj_row = tep_db_fetch_array($recresult) )
	{	
			$str_option	.=	"<option value='".$obj_row[$id_field]."' ";
							if($selected_value==$obj_row[$id_field]) {
								$str_option	.=	" selected";
							}
			$str_option	.=	">".stripslashes($obj_row[$name_field])."</option>";				
	}
	
	return $str_option;
}
function get_name($name_field, $table_name, $condition="1=1") {
	$query	=	"select ".$name_field." from ".$table_name."
				Where ".$condition;
					
	$recresult = tep_db_query($query,init());
	if(tep_db_num_rows($recresult)==0) {
		return false;
	} else {
		$obj_row = tep_db_fetch_array($recresult);
		return stripslashes($obj_row[$name_field]);				
	}
}
function dateDiff($dformat, $endDate, $beginDate)
 {
	$date_parts1=explode($dformat, $beginDate);
	$date_parts2=explode($dformat, $endDate);
	$start_date=gregoriantojd($date_parts1[1], $date_parts1[2], $date_parts1[0]);
	$end_date=gregoriantojd($date_parts2[1], $date_parts2[2], $date_parts2[0]);
	return $end_date - $start_date;
 }
   
 function get_image($image_path, $int_width=110, $int_height=110, $root_image="") {	
		if(is_file($image_path)){    
			if(strlen($root_image)==0) {
				$root_image	=	$image_path;
			}
			$src_image	=	$image_path;
			
			$size = getimagesize ($image_path);   
			
			$originalw    =    $size[0];
			$originalh    =    $size[1];
			$expected_max_height = $int_height;
			$expected_max_width	 = $int_width;	
	
			if ($originalh<$expected_max_height) {
				if ($originalw<$expected_max_width) {
					$imgwidth    =    $originalw;
					$imgheight    =    $originalh;
				} else {
					$imgheight    =    ($expected_max_width * $originalh)/ $originalw;
					$imgwidth    =    $expected_max_width;
				}
			} else {
				$new_height        =    $expected_max_height;
				$new_width        =    ($expected_max_height * $originalw)/ $originalh;
	
				if ($new_width>$expected_max_width) {
					$new_height    =    ($expected_max_width * $expected_max_height) / $new_width;
					$new_width    =    $expected_max_width;
				}
	
				$imgwidth    =    $new_width;
				$imgheight    =    $new_height;
			}
			
			$int_width	=	$imgwidth;
			$int_height	=	$imgheight;
			
			return "thumb.php?imgname=".$root_image."&w=".$int_width."&h=".$int_height."";
		} else {
			return false;
		}
	}
	
 function get_age($str_date) {
 	return round(dateDiff("-", date("Y-m-d", time()), $str_date)/365, 0);
 }
	/*
	
	1. It's a encryption decryption function requires only a single arguments 
	2. Example :
			$encode_variable_name = encode("your string");
			$decode_variable_name = decode($encode_variable_name);
	*/

	mt_srand((double)microtime()*1000000); 
	srand((double)microtime()*1000000); 

	function stringSplit($_text, $_chunksize = 1) 
    { 
        preg_match_all('/(' . str_repeat('.', $_chunksize) . ')/Uims', 
                       $_text, 
                       $_matches); 

        return $_matches[1]; 
    } 
	
	function get_banner($banner_path, $str_alt, $int_width, $int_height, $str_align = "middle", $int_hspace="0") {		
		if(is_file(STATIC_PATH."/".$banner_path)){    
			$src_image	=	$banner_path;
			
			$size = getimagesize (STATIC_PATH."/".$banner_path);   
			
			$expected_max_width		=	$int_width;
			$expected_max_height	=	$int_height;
			
			$originalw	=	$size[0];
			$originalh	=	$size[1];
			
			if ($originalw<$expected_max_width) {
				if ($originalh<$expected_max_height) {
					$imgwidth	=	$originalw;
					$imgheight	=	$originalh;
				} else {
					$imgwidth	=	($expected_max_height * $originalw)/ $originalh;
					$imgheight	=	$expected_max_height;
				}
			} else {	
				$new_height		=	($expected_max_width * $originalh)/ $originalw;
				$new_width		=	$expected_max_width;		
				
				$imgwidth	=	$new_width;
				$imgheight	=	$new_height;
			}	
			
			$int_width	=	$imgwidth;
			$int_height	=	$imgheight;
			
			return "<img src='/thumb.php?imgname=".$src_image."&w=".$int_width."&h=".$int_height."' border='0' alt='".$str_alt."' align='".$str_align."' hspace='".$int_hspace."'>";
		} else {
			return;
		}
	}
	
	// Encode function starts here
	
 	function encode($_text, $_IV = 3, $_ENCRYPT_KEY = "z1Mc6KRxA7Nw90dGjY5qLXhtrPgJOfeCaUmHvQT3yW8nDsI2VkEpiS4blFoBuZ") 
    { 

        if (is_numeric($_IV)) { 

            $_IV = intval($_IV); 

            if ($_IV < 1) 
                $_IV = 1; 
            else 
              if ($_IV > 500) 
                  $_IV = 42; 

        } else { 

            $_IV = 3; 
        } 

        $_text .= ' '; 

        $_arr1 = stringSplit($_ENCRYPT_KEY); 
        $_arr2 = $_arr1; 

        foreach ($_arr1 as $_i1 => $_v1) { 

            foreach ($_arr2 as $_i2 => $_v2) { 

                $_counter = ($_i2 + 1) + ($_i1 * strlen($_ENCRYPT_KEY)); 

                $_array[$_counter] = $_v1 . $_v2; 

                if ($_v1 == $_v2) 
                    $_array[$_counter] = $_v1 . '_'; 
            } 
        } 

        $_encoded = ''; 
        $_count = 0; 
        $_msgarr = stringSplit($_text); 

        foreach ($_msgarr as $_mindex => $_mvalue) { 

           If ($_mindex / 2 <> ceil ($_mindex / 2)) { 

                $_masc = ord($_mvalue) - 31; 
                $_masc = $_masc + (ceil($_count * $_IV / 3) + $_IV); 
                $_count++; 
                if ($_count > 12) 
                    $_count = 0; 
                $_encoded .= $_array[$_masc]; 

            } else { 

                // No need to get around str_rot13 bug here since $_mvalue is 
                // not being referenced after this point & will get overriden. 
                $_encoded .= str_rot13($_mvalue); 
            } 
        } 

        return $_encoded; 
    } 

	// Encode function ends here

	// Decode function starts here

	function decode($_text, $_IV = 3, $_ENCRYPT_KEY = "z1Mc6KRxA7Nw90dGjY5qLXhtrPgJOfeCaUmHvQT3yW8nDsI2VkEpiS4blFoBuZ") 
    { 
        $_count = 0; 

        if (is_numeric($_IV)) { 

            $_IV = intval($_IV); 

            if ($_IV < 1) 
                $_IV = 1; 
            else 
              if ($_IV > 500) 
                  $_IV = 42; 

        } else { 

            $_IV = 3; 
        } 

        $_arr1 = stringSplit($_ENCRYPT_KEY); 
        $_arr2 = $_arr1; 

        foreach ($_arr1 as $_i1 => $_v1) { 

            foreach ($_arr2 as $_i2 => $_v2) { 

                $_counter = ($_i2 + 1) + ($_i1 * strlen($_ENCRYPT_KEY)); 
                $_array[$_counter] = $_v1 . $_v2; 
                if ($_v1 == $_v2) 
                    $_array[$_counter] = $_v1 . '_'; 
            } 
        } 

        $_array = array_flip($_array); 
        $_msgarr = stringSplit($_text, 3); 

        $_decoded = ''; 

        foreach ($_msgarr as $_mvalue) { 

            // $_tmp_hold used to get around a possible PHP bug in versions 
            // earlier than 4.3.0. The variable passed in function might change. 
            $_tmp_hold  = $_mvalue; 
            $_decoded  .= str_rot13($_tmp_hold{0}); 

            $_ivalue = $_array[substr($_mvalue, 1, 2)]; 
            $_ivalue = $_ivalue - (ceil($_count * $_IV / 3) + $_IV); 
            $_count++; 
            if ($_count > 12) 
               $_count = 0; 

            $_masc = chr($_ivalue + 31); 
            $_decoded .= $_masc; 
        } 

        return trim($_decoded); 
    } 
		

// Dhaval Dave : 13 May 2005. A very simples random string generator function	
function rand_str($size)
{
	$feed = "0123456789abcdefghijklmnopqrstuvwxyz";
	for ($i=0; $i < $size; $i++)
	{
		$rand_str .= substr($feed, rand(0, strlen($feed)-1), 1);
	}
	return $rand_str;
}

function replace_specialchar($str_string, $replace_char)
{	
	$special_char	=	"@#$%&*!()+=[]\\\';,-_./{}|\":<>?^`~";	
	
	for ($i=0; $i < strlen($special_char); $i++) 
	{
		$str_string	=	str_replace($specialchar[$i],$replace_char,$str_string);
	}
	
	$str_string	=	str_replace(" ",$replace_char,$str_string);
	
	return $str_string;	

}

function get_profile_image($image_path, $int_width=110, $int_height=110) {	
	
	if(is_file(STATIC_PATH."/".$image_path)){   
		 
		$src_image	=	$image_path;
		
		$size = getimagesize (STATIC_PATH."/".$image_path);   
		
		$originalw    =    $size[0];
		$originalh    =    $size[1];
		$expected_max_height = $int_height;
		$expected_max_width	 = $int_width;	

		if ($originalh<$expected_max_height) {
			if ($originalw<$expected_max_width) {
				$imgwidth    =    $originalw;
				$imgheight    =    $originalh;
			} else {
				$imgheight    =    ($expected_max_width * $originalh)/ $originalw;
				$imgwidth    =    $expected_max_width;
			}
		} else {
			$new_height        =    $expected_max_height;
			$new_width        =    ($expected_max_height * $originalw)/ $originalh;
			

			if ($new_width>$expected_max_width) {
				$new_height    =    ($expected_max_width * $expected_max_height) / $new_width;
				$new_width    =    $expected_max_width;
			}

			$imgwidth    =    $new_width;
			$imgheight    =    $new_height;
		}
		
		$int_width	=	$imgwidth;
		$int_height	=	$imgheight;
		
		return "/thumb.php?imgname=".$src_image."&w=".$int_width."&h=".$int_height."";
	} else {
		return;
	}
}

function get_profile_image_width($image_path, $int_width=110, $int_height=110) {	
	if(is_file(STATIC_PATH."/".$image_path)){    
		$src_image	=	$image_path;
		
		$size = getimagesize (STATIC_PATH."/".$image_path);   
		
		$originalw    =    $size[0];
		$originalh    =    $size[1];
		
		$expected_max_height = $int_height;
		$expected_max_width	 = $int_width;	
		
		if($originalw<$expected_max_width) {
			$imgwidth    =    $expected_max_width;
			$imgheight   =    ($expected_max_width * $originalh)/ $originalw;
		} else {
			$new_width        =    $expected_max_width;
			//$new_height       =    ($expected_max_height * $originalw)/ $originalh;
			$new_height=($originalh)*$expected_max_width/$originalw;


			$imgwidth    =    $new_width;
			$imgheight    =    $new_height;
		}
		
		$int_width	=	$imgwidth;
		$int_height	=	$imgheight;
		
		return "/thumb.php?imgname=".$src_image."&w=".$int_width."&h=".$int_height."";
	} else {
		return;
	}
}

	function getYouTubeIdFromURL($url)
	{
	  $url_string = parse_url($url, PHP_URL_QUERY);
	  parse_str($url_string, $args);
	  return isset($args['v']) ? $args['v'] : false;
	}

	function remove_char_abstract($stringtoconvert){
		$stringtoconvert = preg_replace(array("/[^'+a-z0-9-]/i") , ' ', $stringtoconvert);
		return $stringtoconvert;
	}
	
	function remove_char_title($stringtoconvert){
		$stringtoconvert = ucwords(strtolower(stripslashes(preg_replace(array("/[^'+a-z0-9-]/i") , ' ', $stringtoconvert))));
		return $stringtoconvert;
	}



	function dateCount($start, $end) {
		$start_ts = strtotime($start);
		$end_ts = strtotime($end);
		$diff = $end_ts - $start_ts;
		return round($diff / 86400);
	}
	
	function limit_text($text, $limit) {
      if (str_word_count($text, 0) > $limit) {
          $words = str_word_count($text, 2);
          $pos = array_keys($words);
          $text = substr($text, 0, $pos[$limit]) . '...';
      }
      return $text;
    }
	
?>