<?php 
session_start();
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); 
include('asli/conf.php');
include('asli/func.php');


$key = str_replace('+',' ',$_GET['tag']);
$key = str_replace("'",'',$key);
$usersearch = $key;
$separated = explode('+', $usersearch);

foreach ($separated as $val) {
    
$quuu = "select * from badword where word='".$val."'";

$checkipp = $pdo->prepare($quuu);
$checkipp->execute();
$badword = $checkipp->rowCount();
if($badword>0)
{
    include("404.php");
    exit();
}
}

function clss($string) {
   $string = str_replace('"', ' ', $string); 
   $string = str_replace('#', ' ', $string); 

   return $string;
}



function getUserIpAddr(){
    if(!empty($_SERVER['HTTP_CLIENT_IP'])){
        //ip from share internet
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    }elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
        //ip pass from proxy
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    }else{
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}
$userip =  getUserIpAddr();
$ip = $userip;
    $split = explode(".", $ip);
    $part1 = $split[0]; 
    $part2 = $split[1];
$finalip =  $part1.'.'.$part2.'.';
$dataa = $pdo->query("SELECT ip FROM ip where ip LIKE '$finalip%'")->fetch();
 $ss = 0;
if ($dataa) {
    $ss = 1;
}

//$ss = 0;
		if($ss==0)	
		{$irani=0; }
		if($ss>0)	
		{$irani=1; }
	

$title=$key;
if($_GET['pg']!='1'){$title = $title . ' - '.'صفحه '.$_GET['pg'];}
$meta = '
<link rel="canonical" href="https://www.namavid.com/search/'.$_GET['tag'].'">
<meta name="language" content="fa_IR"/>
<meta name="geo.region" content="IR">
<meta property="og:url" content="https://www.namavid.com/search/'.$_GET['tag'].'"/>
<meta property="og:locale:alternate" content="fa_IR"/>
<meta property="og:type" content="website" />
<meta property="og:title" content="'.$title.'"/>
<meta property="og:description" content="'.$title.' '.$title.'"/>
<meta property="og:site_name" content="نماوید"/>
<meta property="og:image" content="https://www.namavid.com/asset/logo.png"/>

';

include('n_header.php');
function cleanssss($string) {
   $string = str_replace('"', ' ', $string); 
   $string = str_replace('#', ' ', $string); 
   $string = str_replace('}', ' ', $string); 
$string = str_replace('{', ' ', $string); 
$string = str_replace("'", " ", $string); 
$string = str_replace(',', ' ', $string); 
$string = str_replace('\\', '/', $string); 
   return $string;
}


	if($_GET['pg']=='')
{
	$pg=1;
	}
	if($_GET['pg']!=''){
		$pg=strip_tags($_GET['pg']);
		}
		$end=$pg * 20;
		$start=$end - 20;
		
		$search = explode(" ", $key);
$description = "";
$name = "";
foreach($search AS $s)
{
   
    $name .= "`title` LIKE '%".$s."%' AND ";
}

$name = substr($name, 0, -4);



//$sqll = "SELECT * FROM `vvids` WHERE enable='1' and ($name) ORDER BY id DESC LIMIT 25 OFFSET ".$start."";

if($_GET['order']=='')
{
$sqll = "SELECT id,title,guid,manba,thumb2,time, MATCH(title) AGAINST('".$key."') AS score
from vvids
WHERE enable ='1' and MATCH(title) AGAINST('".$key."') > 3
ORDER BY score DESC LIMIT 20 OFFSET ".$start."";


}

if($_GET['order']=='new')
{
$sqll = "SELECT id,title,guid,manba,thumb2,time, MATCH(title) AGAINST('".$key."') AS score
from vvids
WHERE enable ='1' and MATCH(title) AGAINST('".$key."') > 3
ORDER BY id DESC,score DESC LIMIT 40 OFFSET ".$start."";

}

if($_GET['order']=='rate')
{
$sqll = "SELECT id,title,guid,manba,thumb2,time, MATCH(title) AGAINST('".$key."') AS score
from vvids
WHERE enable ='1' and MATCH(title) AGAINST('".$key."') > 3
ORDER BY score DESC LIMIT 40 OFFSET ".$start."";

}
						
						$stmt = $pdo->query($sqll);
						$hast=$stmt->rowCount();

?> 
<div style="min-height: 70vh;">
<div style="max-width: 900px;margin-right: auto;margin-left: auto;margin-top: 10px;padding: 20px;">
<?php if($irani>0){?>
<style>
						    .gsc-results-wrapper-visible {
    opacity: 1 !important;
    visibility: visible !important;
    margin-top: 90px !important;
}
.gsc-input{
    direction:rtl;
    color: black !important;
    text-indent: 0px !important;
    font-family: shabnam !important;
    font-size : 92% !important;
}
}
						</style>
						<script async src="https://cse.google.com/cse.js?cx=013128831670617188969:utjuhw14mky"></script>
<div class="gcse-search"></div>
<script>window.onload = function(){
document.getElementById('gsc-i-id1').placeholder = 'جستجوی دقیق تر با گوگل ...';
};</script>
<?php } ?>
<br>
<select size="1" onchange="javascript:document.location=this.value" style="float: left;width: auto,tahoma;border: 0px;padding: 3px;background: #2481f7;color: white;font-size: 15px;min-width: 140px;"> 

<option value="<?php echo 'https://www.namavid.com/search/'.$_GET['tag'];?>" selected="selected">مرتب سازی</option> 

               <option <?php if($_GET['order']=='new') {echo 'selected';}?> value="<?php echo 'https://www.namavid.com/search/'.$_GET['tag'].'?pg='.$n.'&order=new';?>">جدیدترین</option>
               
               
       <option <?php if($_GET['order']=='rate') {echo 'selected';}?> value="<?php echo 'https://www.namavid.com/search/'.$_GET['tag'].'?pg='.$n.'&order=rate';?>">شبیه ترین</option>
             
              
        			
</select>
</div>

<div class="separator">
 <span class="separator-title"><?php echo $key;?></span>
</div>

<main class="main-content">




    <div class="videos-container">
        
        <?php 

if($hast==0){echo '

<div class="alert alert-error" style="margin-right: auto;margin-left: auto;">
        <i class="alert-icon icon-attention"></i>
        <div class="alert-content">
          ویدیویی یافت نشد.
        </div>
       
    </div>
';}

while ($row1 = $stmt->fetch())
{
	
		
		?>
        
       <div class="video-item">
          <a href="/<?php if($row1['manba']=='post'){echo 'post';} else {echo 'video';}?>/<?php echo $row1['id'];?>/<?php echo $row1['guid'];?>" title="<?php echo cleanssss($row1['title']);?>">  <div class="lazyarea"><img id="lazy" loading="lazy" data-src="<?php echo $row1['thumb2'];?>" alt="<?php echo cleanssss($row1['title']);?>" class="video-thumbnail"></div>
            <span class="video-duration"><?php echo $row1['time'];?></span></a>
            <div class="video-title"><h3 class="vt"><a href="/<?php if($row1['manba']=='post'){echo 'post';} else {echo 'video';}?>/<?php echo $row1['id'];?>/<?php echo $row1['guid'];?>" title="<?php echo cleanssss($row1['title']);?>"><?php echo $row1['title'];?></a></h3></div>
			
   


        </div>
        
		
		<?php } ?>
		
        <br>
        
        
        <style>
            .pagination {
    display: flex;
    justify-content: center;
    align-items: center;
    margin: 20px auto;
    padding: 0;
    list-style: none;
    max-width: 100%;
    flex-wrap: wrap;
}

.pagination li {
    margin: 5px;
}

.pagination a {
    display: inline-block;
    padding: 10px 15px;
    background-color: #646464;
    color: #fff;
    text-decoration: none;
    border-radius: 50%;
    font-size: 16px;
    transition: background-color .3s, color .3s;
    min-width: 40px;
    text-align: center;
}

.pagination a:hover {
    background-color: #ff4081;
    color: #fff;
}

.pagination a[style*="background: #646464"] {
    background-color: #ff4081 !important;
    color: #fff !important;
}

.pagination a:first-child,
.pagination a:last-child {
    border-radius: 50%;
}

.pagination .activepg {
    background-color: #ff4081 !important;
    color: #fff !important;
    border-radius: 50%;
    font-weight: bold;
    box-shadow: 0 4px 8px rgba(0,0,0,0.2);
}

.pagination .activepg:hover {
    background-color: #e040fb;
}

@media (max-width: 600px) {
    .pagination {
        flex-wrap: wrap;
        justify-content: center;
        width: 100%;
        padding: 0 10px;
    }

    .pagination li {
        margin: 3px;
    }

    .pagination a {
        padding: 8px 12px;
        font-size: 14px;
        min-width: 30px;
    }
}
        </style>


					

    </div>
</main>
<?php 
		
		if($_GET['order']=='')

$quuu = "SELECT id,title,guid,manba,thumb2,time, MATCH(title) AGAINST('".$key."') AS score
from vvids
WHERE enable ='1' and MATCH(title) AGAINST('".$key."') > 2";


$checkipp = $pdo->prepare($quuu);
$checkipp->execute();
$total = $checkipp->rowCount();


				 $page=$total / 32;
				 $pages=ceil($page);
				 if($total>32)
				 {
				 
				 if($_GET['order']!='')
{
				 
				 ?> 
				 
				 <ul class="pagination">
    <?php 
   
    if ($pg != 1) { 
        $prev = $pg - 1;
        echo '<li>
                <a href="https://www.namavid.com/search/'.$_GET['tag'].'?pg='.$prev.'&order='.$_GET['order'].'">«</a>
              </li>';
    }

   
    for ($n = 1; $n <= $pages; $n++) {
        if ($n < $pg + 5 && $n > $pg - 5) {
           
            $act = ($n == $pg) ? 'class="activepg"' : '';
            echo '<li>
                    <a '.$act.' href="https://www.namavid.com/search/'.$_GET['tag'].'?pg='.$n.'&order='.$_GET['order'].'">'.$n.'</a>
                  </li>';
        }
    }

  
    if ($pg != $pages) {
        $next = $pg + 1;
        echo '<li>
                <a href="https://www.namavid.com/search/'.$_GET['tag'].'?pg='.$next.'&order='.$_GET['order'].'">»</a>
              </li>';
    }
    ?>
</ul>

					
					<?php 
					}
					
					
					 if($_GET['order']=='')
{
				 
				 ?> 
				 <ul class="pagination">
  <?php 
  
  if ($pg != 1) { 
    $prev = $pg - 1; 
    echo '<li>
            <a href="https://www.namavid.com/search/'.$_GET['tag'].'?pg='.$prev.'">«</a>
          </li>';
  }

  
  for ($n = 1; $n <= $pages; $n++) { 
    if ($n < $pg + 5 && $n > $pg - 5) { 
     
      $act = ($n == $pg) ? 'class="activepg"' : '';
      echo '<li>
              <a '.$act.' href="https://www.namavid.com/search/'.$_GET['tag'].'?pg='.$n.'">'.$n.'</a>
            </li>';
    }
  }

  
  if ($pg != $pages) { 
    $next = $pg + 1; 
    echo '<li>
            <a href="https://www.namavid.com/search/'.$_GET['tag'].'?pg='.$next.'">»</a>
          </li>';
  } 
  ?>
</ul>
				 <?php 
					}
					
					
					} ?> 
</div>

  <?php 
include('n_footer.php');
?> 