<?php
function smtp_mailer($to,$email,$name,$subject,$msg){
   
	$mail = new PHPMailer(); 
	$mail->SMTPDebug  = 0;
	$mail->IsSMTP(); 
	$mail->SMTPAuth = true; 
	$mail->SMTPSecure = 'tls'; 
	$mail->Host = "162.215.169.152";
	$mail->Port = 587; 
	$mail->IsHTML(true);
	$mail->CharSet = 'UTF-8';
	$mail->Username = "enquiry@webpulsesolution.net";
	$mail->Password = "aWj)lgpI5s5+";
	$mail->SetFrom("enquiry@webpulsesolution.net",'webpulsesolution.net');
	$mail->AddReplyTo($email,$name);
	$mail->Subject = $subject;
	$mail->Body =$msg;
	$mail->AddAddress($to);
    
    
	$mail->SMTPOptions=array('ssl'=>array(
		'verify_peer'=>false,
		'verify_peer_name'=>false,
		'allow_self_signed'=>false
	));
	if(!$mail->Send()){
      
		echo $mail->ErrorInfo;
	}else{
       
		return 'Sent';
	}
}


function getUserIP() {
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
        return $_SERVER['HTTP_CLIENT_IP'];
    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        return explode(',', $_SERVER['HTTP_X_FORWARDED_FOR'])[0];
    } else {
        return $_SERVER['REMOTE_ADDR'];
    }
}

function getValidFileName($fname) {
	$pattern="[?()\/&#\,\;\.$+%]";
	$valid_file=mb_ereg_replace($pattern,"-",$fname);
	$valid_file=strtolower($valid_file);
	$valid_file=str_replace("  ","-",$valid_file);
	$valid_file=str_replace(" ","-",$valid_file);
	$valid_file=str_replace("_","-",$valid_file);
	$valid_file=str_replace("%","",$valid_file);
	$valid_file=str_replace("__","-",$valid_file);
	$valid_file=str_replace("--","-",$valid_file);
	return $valid_file;
}


function mail_header() {
	$ContactPerson=getAdminname();
	$eMail=getAdminemail();
	$headers = "From: $ContactPerson<$eMail> \n";
	$headers .= "Reply-To: $eMail \r\n";
	$headers .= "X-Mailer: PHP/". phpversion();
	$headers .= "X-Priority: 3 \n";
	$headers .= "MIME-version: 1.0\n";
	$headers .= "Content-Type: text/html; charset=UTF-8\n";
	return $headers;
}

function getAdminemail() {
	$res=getResult("admin_details"," where 1");
	return $res['email'];
}


function getImage($file_name, $width, $height, $product_name,$source_folder1){

    
	$product_name = str_replace(' ','-', $product_name);
	$file_parts = explode('.',$file_name);
	$file_name_to_store = $product_name.'-thumbs-'.$width.'X'.$height.'.'.$file_parts[1];
	$source_folder= SITE_DIR_PATH.$source_folder1;
	$destination_folder=$source_folder."thumbs";
$file=SITE_DIR_PATH.$source_folder1."thumbs/".$file_name_to_store;
	
	if(file_exists($file)) {
		unlink($file);
	}
	saveResizedImage($file_name, $width, $height, $source_folder, $destination_folder, $file_name_to_store);
	return site_url.$source_folder1."thumbs/".$file_name_to_store;
	
	}
	
	
	
	function getYouTubeVideothumbnail($pageVideUrl,$size) {
    $link = $pageVideUrl;
     $video_id = explode("/", $link);
    $youtubeID = $video_id[4];
    $thumbURL = 'https://img.youtube.com/vi/' . $youtubeID . '/'.$size.'default.jpg';
    if ($thumbURL) {
        return $thumbURL;
    } else {
        return false;
    }
}

	function saveResizedImage($file_name, $width, $height, $source_folder, $destination_folder,$file_name_to_store='') {
		if($file_name_to_store == ''){$file_name_to_store = $file_name;}
		
		if (!file_exists($destination_folder)) {
			mkdir($destination_folder, 0777);
			chmod($destination_folder, 0777);
		}
		
		$source_path=$source_folder."/".$file_name;
		$destination_path=$destination_folder."/".$file_name_to_store;
		$ext=explode(".",$file_name);
		
		if($ext[1]=="gif" or $ext[1]=="GIF") {
			$mainimage=imagecreatefromgif($source_path);
		}
		elseif($ext[1]=="png" or $ext[1]=="PNG") {
			$mainimage=imagecreatefrompng($source_path);
		}
		elseif($ext[1]=="jpg" or $ext[1]=="JPG") {
			$mainimage=imagecreatefromjpeg($source_path);
		}
		elseif($ext[1]=="jpeg" or $ext[1]=="JPEG") {
			$mainimage=imagecreatefromjpeg($source_path);
		}
		$mainwidth=imagesx($mainimage);
		$mainheight=imagesy($mainimage);
		
		if($mainwidth<=$width and $mainheight<=$height) {
			$thumbleheight=$mainheight;
			$thumblewidth=$mainwidth;	
		}
		else {
			if($mainwidth>$width) {
				$thumblewidth=$width;
				$thumbleheight=(($width/$mainwidth)*$mainheight);	
				if ($thumbleheight > $height) {
					$thumbleheight=$height;
					$thumblewidth=(($height/$mainheight)*$mainwidth);	
				}
			}
			else if($mainheight>$height) {
				$thumbleheight=$height;
				$thumblewidth=(($height/$mainheight)*$mainwidth);	
				if ($thumblewidth > $width) {
					$thumblewidth=$width;
					$thumbleheight=(($width/$mainwidth)*$mainheight);	
				}
			}
		}
		
		$thumbleimage=imagecreate($thumblewidth,$thumbleheight);
		$thumbleimage=@imagecreatetruecolor($thumblewidth,$thumbleheight);
		$my_temp_file= ImageCopyResampled($thumbleimage,$mainimage,0,0,0,0,$thumblewidth,$thumbleheight,$mainwidth,$mainheight);
		imagejpeg($thumbleimage,$destination_path,70);
		imagedestroy($thumbleimage);
		imagedestroy($mainimage);
	}

	function getCount($tableName, $cond="", $fields="*") {
		$sql=db_query("select $fields from $tableName $cond");
		return mysqli_num_rows($sql);
	}
	

	function updateTable($tableName,$cond="") {
		db_query("update $tableName set $cond");
	}
	
	
	function getResult($tableName, $cond="", $fields="*") {
		$sql=db_query("select $fields from $tableName $cond");
		$res=mysqli_fetch_array($sql);
		return $res;
	}

	function getAdminname() {
		$res=getResult("admin_details"," where 1");
		return $res['name'];
	}
	

	function send_email($fromPerson, $fromEmail, $to, $subject, $message) {
		$eMail=getAdminemail();
		$headers = "From:$fromPerson<$fromEmail> \n";
		$headers .= "Reply-To: $eMail \r\n";
		$headers .= "X-Mailer:PHP v".phpversion()."\r\n";
		$headers .= "X-Priority:3\n";
		$headers .= "MIME-version:1.0\n";
		$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
		$msg=$message;
		mail($to, $subject, $msg, $headers);
	}

	function print_message() {
		echo "<center><font style=\"color:#ee0000;\"><b>".$_SESSION['session_message']."</b></font></center><br />";
		$_SESSION['session_message']="";
	}

	function upload_file($newname,$image,$folder) {
		$folder=SITE_DIR_PATH."/".$folder;
		if (!file_exists($folder)) {
			mkdir($folder, 0777);
			chmod($folder, 0777);
		}
		if($_FILES[$image]['name']!="") {
			$extension=getExtension($_FILES[$image]['name']);
			$image_name=$newname.".".$extension;		
			$uploadedfile=$_FILES[$image]['tmp_name'];
			$tmp_imagename=$_FILES[$image]['name'];
			if($tmp_imagename<>"" && $uploadedfile<>"" && $image_name<>"") {		
				move_uploaded_file($uploadedfile, $folder."/".$image_name);
			}
		}
		return $image_name;
	}

	function getExtension($str) {
		$i = strrpos($str,".");
		if (!$i) { return ""; }
		$l = strlen($str) - $i;
		$ext = substr($str,$i+1,$l);
		return $ext;
	}
	

	function getClass() {
		$msg=explode("~",$_SESSION['opmsg']);
		if(count($msg)<2) {
			$class="msgadmin";	
		}else {
			$class=$msg[1];
		}
		return $class;	
	}

	function page_nav($catid) {
		$res=getResult("category_tbl"," where catID='$catid'");
		$flag=0;
		$catparent=$catid;
		while($flag!=1) {
			$res1=db_query("select * from category_tbl where catID='$catparent'");
			$record=db_fetch_array($res1);
			if($record['parentID']!=0) {
				$catparent=$record['parentID'];
				$array=$record['catID']."~";
			}
			else {
				if($record['catID']!="") {
					$array=$record['catID']."~";
				}
				$flag=1;
			}
		}
		$arr=explode("~",$array);
		$result = array_reverse($arr);
		echo "<b><a href='manage_categories.php' class='menu2'>Manage Categories</a> ";
		for($i=1;$i<count($result);$i++) {
			$res=getResult("category_tbl"," where catID='$result[$i]'");
			$re=db_query("select * from category_tbl where catID='".$res['catID']."'");
			if(NumRows($re)==0) {
				if($res['catID']==$catid) {
					echo " <b>::</b> ".$res['catName'];
				}else {
					echo " <b>::</b> <a href='manage_categories.php?parentID=".$res['catID']."' class='menu2'>".$res['catName']."</a>";
				}
			}else {
				if($res['catID']==$catid) {
					echo " <b>::</b> ".$res['catName'];
				}else {
					echo " <b>::</b> <a href='manage_categories.php?parentID=".$res['catID']."' class='menu2'>".$res['catName']."</a>";
				}
			}
		}
		echo "</b>";
	}
	
	function getListingCategoriesDropDownAjax3($fldName, $value, $width="") {
		if ($width=="") {
			$width="220";
		}
		$link="'".$_SERVER['PHP_SELF']."'";
		$var='<select name="'.$fldName.'" style="width: '.$width.'px; " onChange="javascript:dynshowHint(this.value,'.$link.',\'?id=show_sub_categories\',\'showSubCategories\');">
		<option value="" selected="selected" >----------- Select Listing Categories -----------</option>';
		$catQry=db_query("select catID, catName from  category_tbl where parentID='0' and catgType='2' order by catOrder asc");
		while($catRes=mysql_fetch_array($catQry)) {
			
			if(trim($catRes[catID])==trim($value)) {
				$var.= "<option value='".$catRes[catID]."' selected='selected'>".$catRes[catName]."</option>";
			}
			else {
				$var.="<option value='".$catRes[catID]."'>".$catRes[catName]."</option>";
			}
		}
		$var.='</select>';
		return $var; 
	}

function watermark_image($target, $wtrmrk_file, $newcopy) {
  $watermark = imagecreatefrompng($wtrmrk_file);
  imagealphablending($watermark, false);
  imagesavealpha($watermark, true);
  $img = imagecreatefromjpeg($target);
  $img_w = imagesx($img);
  $img_h = imagesy($img);
  $wtrmrk_w = imagesx($watermark);
  $wtrmrk_h = imagesy($watermark);
  imagecopy($img, $watermark, -5, -5, 0, 0, $wtrmrk_w, $wtrmrk_h);
  imagejpeg($img, $newcopy, 100);
  imagedestroy($img);
  imagedestroy($watermark);
}
	
	
	function getListingSubCategoriesDropDown($fldName, $catID, $sel, $width="") {
		if ($width=="") {
			$width="220";
		}
		
		$var="<div style='width:".$width."px; height:115px; overflow-x:hidden; overflow-y:scroll; border:1px solid gray'>";
		
		$low_level = "";
		if($catID!='') {
			$qry=db_query("select catID from category_tbl where parentID='$catID' and status='Y' order by catName");
			$num=NumRows($qry);
			if($num>0) {
				$i=0;
				while($row=mysql_fetch_array($qry)) {
					$low_level.=	$row[0].",".getAllChilds($row[0]);
					$i++;
				}
			}
		}
		
		$catarr=explode(",",$low_level);
		if (count($catarr) > 1) {
			for($i=0;$i<count($catarr);$i++) {
				$catid2=$catarr[$i];
				$res2=getResult("category_tbl"," where catID='$catid2'");
				$space=parentCount($catid2);
				if(haveSubCategory($catid2)=='Y' && $i < count($catarr)-1) {
					if ($space!=0) {
						for ($p=1;$p<$space;$p++) {
							$var.='&nbsp;&nbsp;';
						}
					}
					$var.="<strong>".ucwords($res2[catName])."</strong><br/>";
				}
				
				if(haveSubCategory($catid2)=='N') {
					if ($space!=0) {
						for ($p=2;$p<=$space;$p++) {
							$var.='&nbsp;&nbsp;';					
						}
					}
					if (is_array($sel)) {
						if (in_array($catid2,$sel)) {
							$var.= "<input type='checkbox' name='".$fldName."[]' id='".$fldName."' value='".$catid2."' checked>".getCategoryName($catid2)."<br/>";
						}
						else {
							$var.= "<input type='checkbox' name='".$fldName."[]' id='".$fldName."' value='".$catid2."'>".getCategoryName($catid2)."<br/>";
						}
					}
					else {
						$catg="^".$catid2."^";
						if(strstr($sel,$catg)) {
							$var.= "<input type='checkbox' name='".$fldName."[]' id='".$fldName."' value='".$catid2."' checked>".getCategoryName($catid2)."<br/>";
						}
						else {
							$var.= "<input type='checkbox' name='".$fldName."[]' id='".$fldName."' value='".$catid2."'>".getCategoryName($catid2)."<br/>";
						}
					}								
				}
			}			
		}
		else {
			$res3=getResult("category_tbl"," where catID='$catID'");
			$var.= "<input type='checkbox' name='".$fldName."' id='".$fldName."' value='".$catid."' checked>".ucwords($res3[catName])."<br/>";
		}
		
		$var.='</div>';
		return $var; 
	}


	function pageNavigation($reccnt,$pagesize,$start,$link="") {
		if($reccnt>$pagesize) {
			$num_pages=$reccnt/$pagesize;
			$PHP_SELF=$_SERVER['PHP_SELF'];
			$qry_str=$_SERVER['argv'][0];
			$m=$_GET;
			unset($m['start']);
			$qry_str=qry_str($m);
			$j=$start/$pagesize-5;
			if($j<=0) {
				$j=0;
			}
			$k=$j+10;
			if($k>$num_pages)	{
				$k=$num_pages;
			}
			$j=intval($j);
			?>
			
			<div class="main_index" align="center" style="margin:auto; ">
			 <?php
			 if($start!=0) {
				 $s=$start-$pagesize;
				 $newUrl=$PHP_SELF."".$qry_str."&start=".$s."&pagesize=".$pagesize.$link;
				 $newUrl=str_replace("?&","?",$newUrl);
				 ?>
				 <a href="<?=$newUrl?>" ><img src="<?=SITE_ADMIN_URL?>/images/previous.gif" alt="Previous Page" width="69" height="17" border="0" class="left"/></a>
				 <?php
			 }
			 else {
				 ?>
				 <img src="<?=SITE_ADMIN_URL?>/images/previous.gif" alt="Previous Page" width="69" height="17" border="0" class="left" />
				 <?php
			 }
			 if($start+$pagesize < $reccnt) {
				 $s=$start+$pagesize;
				 $newUrl=$PHP_SELF."".$qry_str."&start=".$s."&pagesize=".$pagesize.$link;
				 $newUrl=str_replace("?&","?",$newUrl);
				 ?>
				 <a href="<?=$newUrl?>"><img src="<?=SITE_ADMIN_URL?>/images/next.gif" alt="Next Page" width="44" height="17" border="0"  class="right" /></a>
				 <?php
			 }
			 else {
				 ?>
				 <img src="<?=SITE_ADMIN_URL?>/images/next.gif" alt="Next Page" width="44" height="17" border="0"  class="right" />
				 <?php
			 }
			 $startfrom=$start+1;
			 $end=$start+$pagesize;
			 if($reccnt<$end) {
				 $end=$reccnt;
			 }
			 #echo "Showing	$startfrom - $end of Total $reccnt Records";
			 ?>
			 <div class="index">
			  <?php 
			  for($i=$j;$i<$k;$i++) {
				  if(($pagesize*$i)!=$start) {
					  $s=$pagesize*$i;
					  $newUrl=$PHP_SELF."".$qry_str."&start=".$s."&pagesize=".$pagesize.$link;
					  $newUrl=str_replace("?&","?",$newUrl);
					  ?>
					  <a href="<?=$newUrl?>"  class="navBarTxt"><?=$i+1?></a>
					  <?php
				  }
				  else {
					  ?>
					  <span class="navBarTxt"><?=$i+1?></span>
					  <?php
				  }
			  }
			  ?>
			 </div>
			</div>
			<?php
		}
	}
	function strReplace($strs){
		if($strs!=''){
		return str_ireplace("'", "&apos;", $strs);
		}
	}
	function getAllPageLinks($count,$perpage)
	{
		$output = '';
		if(!isset($_GET["page"])) $_GET["page"] = 1;
		if($perpage!= 0)
			$pages  = ceil($count/$perpage); 
		if($pages>1) {
			if($_GET["page"] == 1) 
				$output = $output . '<li class="page-item disabled"><a href="javascript:void(0);" class="link first disabled">&#8810;</a></li><li class="page-item disabled"><a href="javascript:void(0);" class="link disabled">&#60;</span></li>';
			else	
				$output = $output . '<li class="page-item"><a href="javascript:void(0);" class="link first page" data-page="'.(1).'" >&#8810;</a></li><li class="page-item"><a href="javascript:void(0);" class="page-link page" data-page="'.($_GET["page"]-1).'" >&#60;</a></li>';
			
			if ($_GET["page"]<4)
			{
				$start 	= 1;
				$end 	= 5;
			}
			else
			{
				$start 	= ($_GET["page"]-2);
				$end 	= ($_GET["page"]+2);
			}
			
			$endpage = ($pages-3);
			if ($_GET["page"]>$endpage)
			{
				$start 	= ($pages-4);
			}

			for($i=$start; $i<=$end; $i++)	
			{ 
				if($i<1) continue;
				if($i>$pages) break;
				if($_GET["page"] == $i)
				{
					$output = $output . '<li class="page-item active"><a href="javascript:void(0);" id='.$i.' class="page-link page">'.$i.'</a></li>';
				}
				else
				{				
					$output = $output . '<li class="page-item"><a href="javascript:void(0);" class="page-link page" data-page="'.$i.'" >'.$i.'</a></li>';
				}
			}
			
			if($_GET["page"] < $pages)
				$output = $output . '<li class="page-item"><a href="javascript:void(0);" class="page-link page" data-page="'.($_GET["page"]+1).'" >></a></li><li href="javascript:void(0);" class="page-item"><a href="javascript:void(0);" class="page-link page" data-page="'.($pages).'" >&#8811;</a>';
			else				
				$output = $output . '<li class="page-item disabled"><a href="javascript:void(0);" class="link disabled">></a></li><li class="page-item disabled"><a href="javascript:void(0);" class="link disabled">&#8811;</a></li>';
			
		}
		return $output;
	}
?>