<?php
session_start();
include "../c/globalsettings.php"; 
$javascripts="searchresults";

if(defined('DEFAULT_STOCK') && DEFAULT_STOCK === "123RF") { 
	$modulePath = '123rf_module/';
	include $modulePath.'header.php';

	$useCache = false;
	$cacheKeyData = [];
	$CACHE_DIR = __DIR__ . '/searchcache/'.$stock->zoom_lang;
	$CACHE_TTL = 3600 * 24 * 14; // 14 days cache
	if (!is_dir($CACHE_DIR)) { @mkdir($CACHE_DIR, 0775, true); }

	if(isset($_GET['words']) && $_GET['words']<>'') { 	$stock->search_words=$stock->sanitizeData($_GET['words']); $_SESSION['word_searched']=$stock->search_words;}
	if(isset($_GET['offset']) && $_GET['offset']<>'') { 	$stock->search_offset=$stock->sanitizeData($_GET['offset']); }
	if(isset($_GET['creator_id']) && $_GET['creator_id']<>'') { $stock->search_creator_id=$stock->sanitizeData($_GET['creator_id']); }
	if(isset($_GET['category1']) && $_GET['category1']<>'') { $stock->search_category1=$stock->sanitizeData($_GET['category1']); }
	if(isset($_GET['category1']) && $_GET['category1']<>'' && !is_numeric($_GET['category1'])) 
	{ 
		$stock->search_category1_flink=$_GET['category1'];	
		if(isset($f_cat[$_GET['category1']]['id'])) $stock->search_category1=$f_cat[$_GET['category1']]['id'];	
		else $stock->search_category1=$stock->search_category1_flink;
	}

	$rzFilters=array();

	//SEARCHFILTERS - set selected search filters from session
	$rzFilters['orientation']='ALL';
	$rzFilters['show_photos']=true;
	$rzFilters['show_illustrations']=true;
	$rzFilters['show_vectors']=true;
	//$rzFilters['show_panoramic']=true;

	if(isset($_SESSION['set_filter_orientation']))
	{
		if($_SESSION['set_filter_orientation']=="1") $rzFilters['orientation']='HORIZONTAL';
		elseif($_SESSION['set_filter_orientation']=="2") $rzFilters['orientation']='VERTICAL';
		elseif($_SESSION['set_filter_orientation']=="3") $rzFilters['orientation']='SQUARE';
		else $rzFilters['orientation']='ALL';
	}
	else $rzFilters['orientation']='ALL';

	if(isset($_SESSION['set_filter_filetype']))
	{
		//reset all filters
		$rzFilters['show_photos']=false;
		$rzFilters['show_illustrations']=false;
		$rzFilters['show_vectors']=false;
		
		//set choosed filter
		if($_SESSION['set_filter_filetype']=="1") $rzFilters['show_photos']=true;
		elseif($_SESSION['set_filter_filetype']=="2") $rzFilters['show_illustrations']=true;
		elseif($_SESSION['set_filter_filetype']=="3") $rzFilters['show_vectors']=true;
		else 
			{
			//in case something goes wrong all filters are disabled
			$rzFilters['show_photos']=true;
			$rzFilters['show_illustrations']=true;
			$rzFilters['show_vectors']=true;
			}
	}
	else
	{
		$rzFilters['show_photos']=true;
		$rzFilters['show_illustrations']=true;
		$rzFilters['show_vectors']=true;
	}

	$cacheFilterLink = $rzFilters['orientation'];
	if ( $rzFilters['show_photos'] ) $cacheFilterLink.="_show_photos";
	if ( $rzFilters['show_illustrations'] ) $cacheFilterLink.="_show_illustrations";
	if ( $rzFilters['show_vectors'] ) $cacheFilterLink.="_show_vectors";

	$cacheKeyData['query'] = 'https://'.$_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . $_SERVER['QUERY_STRING'];
	if ($useCache) session_write_close();
	//END SEARCHFILTERS

	$cacheKey  = 'search_' . md5(json_encode($cacheKeyData, JSON_UNESCAPED_UNICODE));
	$cacheFile = $CACHE_DIR . '/' . $cacheKey . '.html';

	try {
		if ($useCache && is_file($cacheFile) && (time() - filemtime($cacheFile) < $CACHE_TTL)) { 
			//wyswietl wyniki wyszukiwania z cache
			readfile($cacheFile);

			//if(isset($_SESSION['rz_debug']) && $_SESSION['rz_debug']=="1") {	echo "<div style='position:absolute;bottom:10px;left:0px;background:red;color:white;z-index:99999'>RESULT FROM CACHE</div>";}
		} else {
			if($useCache) {
				ob_start(); //rozpocznij zapis do cache
				$stock->searchResults($txts,$rzFilters); //wydrukuj wyniki wyszukiwania
				$html = ob_get_clean(); //pobierz html z bufora

				//zapisz atomowo do pliku
				$tempFile = $cacheFile . '.tmp';
				file_put_contents($tempFile, $html);
				@chmod($tempFile, 0664);
				@rename($tempFile, $cacheFile);

				//wyświetl wynik
				echo $html;
			} else {
				$stock->searchResults($txts,$rzFilters); //wydrukuj wyniki wyszukiwania
			}
		}
	} catch(Throwable $e) {
		if ($useCache && is_file($cacheFile)) {
			//wyswietl wyniki wyszukiwania z cache bez względu na czas pliku, jeśli problem z api
			readfile($cacheFile);
		} else {
			switch($stock->zoom_lang) {
				case "pl": echo "<center><br><br><br><br> Przepraszamy, wystąpił nieoczekiwany błąd. <br><br><strong>Spróbuj odświeżyć stronę, jeżeli problem będzie się powtarzał prosimy o kontakt z obslugą serwisu.</strong></center>"; break;
				case "de": 
					echo "<center><br><br><br><br> Es tut uns leid, ein unerwarteter Fehler ist aufgetreten. <br><br><strong>Bitte versuchen Sie, die Seite zu aktualisieren. Wenn das Problem weiterhin besteht, kontaktieren Sie bitte den Kundenservice.</strong></center>"; 	
				break;
				case "en": echo "<center><br><br><br><br> We're sorry, an unexpected error has occurred. <br><br><strong>Please try refreshing the page. If the problem persists, contact our support team.</strong></center>"; break;
			}	
		}	
	}

	if($stock->debugMode) {
		echo "<div style='width:100%;float:left;'>";
			echo "<h2>GET</h2>";
			if (!empty($_GET)) {
				echo "<pre>";
				print_r($_GET);
				echo "</pre>";
			} else {
				echo "<p>Brak danych w \$_GET</p>";
			}

			echo "<h2>POST</h2>";
			if (!empty($_POST)) {
				echo "<pre>";
				print_r($_POST);
				echo "</pre>";
			} else {
				echo "<p>Brak danych w \$_POST</p>";
			}

			echo "<h2>SESSION</h2>";
			if (!empty($_SESSION)) {
				echo "<pre>";
				print_r($_SESSION);
				echo "</pre>";
			} else {
				echo "<p>Brak danych w \$_SESSION</p>";
			}
		echo "</div>";
	}

	include $modulePath.'footer.php';
	exit;
}

//-------------------------------------------------------------------------------------------------------------------------------------------------- ADOBE CODE
include 'adobeHeader.php';

$cacheKeyData = [];
$CACHE_DIR = __DIR__ . '/searchcache/'.$astock->zoom_lang;
$CACHE_TTL = 3600 * 24 * 14; // 14 days cache
if (!is_dir($CACHE_DIR)) { @mkdir($CACHE_DIR, 0775, true); }


//if(isset($_GET['type']) && $_GET['type']<>'') { $astock->on_page=$_GET['type']; } else $on_page="canvas";	
if(isset($_GET['words']) && $_GET['words']<>'') { 	$astock->search_words=$astock->sanitizeData($_GET['words']); $_SESSION['word_searched']=$astock->search_words;}
if(isset($_GET['offset']) && $_GET['offset']<>'') { 	$astock->search_offset=$astock->sanitizeData($_GET['offset']); }
if(isset($_GET['creator_id']) && $_GET['creator_id']<>'') { $astock->search_creator_id=$astock->sanitizeData($_GET['creator_id']); }
if(isset($_GET['category1']) && $_GET['category1']<>'') { $astock->search_category1=$astock->sanitizeData($_GET['category1']); }
if(isset($_GET['category1']) && $_GET['category1']<>'' && !is_numeric($_GET['category1'])) 
{ 
	$astock->search_category1_flink=$_GET['category1'];	
	$astock->search_category1=$f_cat[$_GET['category1']]['id'];	
}
/*
echo "FLINK: ".$_GET['category1'];
echo "<br/>CAT ID: ".$f_cat[$_GET['category1']]['id'];	
*/

//setCategory
//if (count($params)<=0) { header("Location: canvas-beach-0"); }
//if(isset($_GET['words']) && !is_numeric(substr($_GET['words'],1))) $params['filters']['license_XL:on'] = 1;

$rzFilters=array();

//SEARCHFILTERS - set selected search filters from session
$rzFilters['orientation']='ALL';
$rzFilters['show_photos']=true;
$rzFilters['show_illustrations']=true;
$rzFilters['show_vectors']=true;
//$rzFilters['show_panoramic']=true;

if(isset($_SESSION['set_filter_orientation']))
 {
	if($_SESSION['set_filter_orientation']=="1") $rzFilters['orientation']='HORIZONTAL';
	elseif($_SESSION['set_filter_orientation']=="2") $rzFilters['orientation']='VERTICAL';
	elseif($_SESSION['set_filter_orientation']=="3") $rzFilters['orientation']='SQUARE';
	else $rzFilters['orientation']='ALL';
 }
else $rzFilters['orientation']='ALL';

if(isset($_SESSION['set_filter_filetype']))
 {
	 //reset all filters
	 $rzFilters['show_photos']=false;
	 $rzFilters['show_illustrations']=false;
	 $rzFilters['show_vectors']=false;
	 
	 //set choosed filter
	 if($_SESSION['set_filter_filetype']=="1") $rzFilters['show_photos']=true;
	 elseif($_SESSION['set_filter_filetype']=="2") $rzFilters['show_illustrations']=true;
	 elseif($_SESSION['set_filter_filetype']=="3") $rzFilters['show_vectors']=true;
	 else 
		{
		//in case something goes wrong all filters are disabled
		$rzFilters['show_photos']=true;
		$rzFilters['show_illustrations']=true;
		$rzFilters['show_vectors']=true;
		}
 }
else
 {
	$rzFilters['show_photos']=true;
	$rzFilters['show_illustrations']=true;
	$rzFilters['show_vectors']=true;
 }

$cacheFilterLink = $rzFilters['orientation'];
if ( $rzFilters['show_photos'] ) $cacheFilterLink.="_show_photos";
if ( $rzFilters['show_illustrations'] ) $cacheFilterLink.="_show_illustrations";
if ( $rzFilters['show_vectors'] ) $cacheFilterLink.="_show_vectors";

$cacheKeyData['query'] = 'https://'.$_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . $_SERVER['QUERY_STRING'];
$cacheKeyData['filters'] = $cacheFilterLink;

session_write_close();
//END SEARCHFILTERS

$cacheKey  = 'search_' . md5(json_encode($cacheKeyData, JSON_UNESCAPED_UNICODE));
$cacheFile = $CACHE_DIR . '/' . $cacheKey . '.html';

// if(isset($_SESSION['rz_debug']) && $_SESSION['rz_debug']=="1") {	
// 	print_r($cacheKeyData);
// 	echo "<div style='position:absolute;top:10px;left:0px;background:red;color:white;z-index:99999'>
// 	".$cacheKey."<br>".$cacheFile."
// 	</div>";
// }

try {
	if (is_file($cacheFile) && (time() - filemtime($cacheFile) < $CACHE_TTL)) { 
		//wyswietl wyniki wyszukiwania z cache
		readfile($cacheFile);

		//if(isset($_SESSION['rz_debug']) && $_SESSION['rz_debug']=="1") {	echo "<div style='position:absolute;bottom:10px;left:0px;background:red;color:white;z-index:99999'>RESULT FROM CACHE</div>";}
	} else {
		
		ob_start(); //rozpocznij zapis do cache
		$astock->searchResults($txts,$rzFilters); //wydrukuj wyniki wyszukiwania
		$html = ob_get_clean(); //pobierz html z bufora

		//zapisz atomowo do pliku
		$tempFile = $cacheFile . '.tmp';
		file_put_contents($tempFile, $html);
		@chmod($tempFile, 0664);
		@rename($tempFile, $cacheFile);

		//wyświetl wynik
		echo $html;
	}
} catch(Throwable $e) {
	if (is_file($cacheFile)) {
		//wyswietl wyniki wyszukiwania z cache bez względu na czas pliku, jeśli problem z api
		readfile($cacheFile);
	} else {
		switch($astock->zoom_lang) {
			case "pl": echo "<center><br><br><br><br> Przepraszamy, wystąpił nieoczekiwany błąd. <br><br><strong>Spróbuj odświeżyć stronę, jeżeli problem będzie się powtarzał prosimy o kontakt z obslugą serwisu.</strong></center>"; break;
			case "de": echo "<center><br><br><br><br> Es tut uns leid, ein unerwarteter Fehler ist aufgetreten. <br><br><strong>Bitte versuchen Sie, die Seite zu aktualisieren. Wenn das Problem weiterhin besteht, kontaktieren Sie bitte den Kundenservice.</strong></center>"; break;
			case "en": echo "<center><br><br><br><br> We're sorry, an unexpected error has occurred. <br><br><strong>Please try refreshing the page. If the problem persists, contact our support team.</strong></center>"; break;
		}	
	}	
}

include 'adobeFooter.php';
?>