<?php
// ------------------ Variable zum Bearbeiten -------------------
$vollAutomat = 0;		// zum Bearbeiten auf !=0 ändern
$dateienKopieren = 0;	// neue fertige .inc Dateien nach Bestseller kopieren
$doppelteSuchen = 0;		// doppelte Nummer bei Ketegorien
$imagesCheck = 0;		// images check
$imagesCheckDot = 0;		// 
$fotolia_fotolia500 = 0;	// fotolia und fotolia500px vergleichen mit Erweiterungen
$doppelteTXTSuchen = 0;	// doppelte Nummer bei C:\\Fotolia\dir /B /OE > 1.txt mit Erweiterungen
$vectorTXTDownload = 0;	// vector Nummer bei C:\\Fotolia\dir /B /OE > vectors.txt mit Erweiterungen
// --------------------------------------------------------------
$fileID_path = $galerie_list_path.'neue-ids/';
if ($vollAutomat) 	
{
	$ids_neuedateien_path = $fileID_path.'_ids_neuedateien/';
	$fileIDs = $fileID_path.'ids_neu.inc';							// neu - nur mit einer Datei arbeiten
	$fileAlleunsortiert = $fileID_path.'alleunsortiert.inc';
	$fileDB = $galerie_list_path.'_DB.inc';							// DB-Export
	copy($galerie_list_path.'alleunsortiert.inc', $fileAlleunsortiert);
	$menge_neuedateien = 0;

	if (@is_file($ids_neuedateien_path.'küche_ids_neu.inc')) rename($ids_neuedateien_path.'küche_ids_neu.inc', $ids_neuedateien_path.'kche_ids_neu.inc');
	if (@is_file($ids_neuedateien_path.'löwe_ids_neu.inc')) rename($ids_neuedateien_path.'löwe_ids_neu.inc', $ids_neuedateien_path.'lwe_ids_neu.inc');
	if (@is_file($ids_neuedateien_path.'new_ids_neu.inc')) rename($ids_neuedateien_path.'new_ids_neu.inc', $ids_neuedateien_path.'newyork_ids_neu.inc');
	if (@is_file($ids_neuedateien_path.'vögel_ids_neu.inc')) rename($ids_neuedateien_path.'vögel_ids_neu.inc', $ids_neuedateien_path.'vgel_ids_neu.inc');
	$fileList = scandir($ids_neuedateien_path);
	$notInFotolia = array();

	for ($q=2; $q<count($fileList);$q++)
	{
		copy($ids_neuedateien_path.$fileList[$q], $fileIDs);
		$DateiIncName = str_replace("_ids_neu.inc", ".inc", $fileList[$q]);
		$DateiInc = $fileID_path.$DateiIncName;

		// ---------------- load the data and delete the line from the array ----------------
		$lines = file($galerie_list_path.$DateiIncName); 
		$last = sizeof($lines) - 1 ; 
		unset($lines[$last]); 
		$handle_inc = fopen($DateiInc, 'w'); 
		fwrite($handle_inc, implode('', $lines)); 
		// ----------------------------------------------------------------------------------

		// --------------------------- Check for BESTSELLER Kategorie --------------------------- 
		include ($fileAlleunsortiert);
		// ---------------- load the data and delete the line from the array ----------------
		$lines = file($fileAlleunsortiert); 
		$last = sizeof($lines) - 1 ; 
		unset($lines[$last]); 
		$handle_alleunsortiert = fopen($fileAlleunsortiert, 'w'); 
		fwrite($handle_alleunsortiert, implode('', $lines)); 
		// ----------------------------------------------------------------------------------
		$yesInfo = 0;
		$lines = array();
		$handle = @fopen($fileIDs, "r");
		if ($handle) {
   			while (!feof($handle)) {
       			$lines[] = fgets($handle, 4096);
   			}
		}
   		fclose($handle);
		$imgMenge = 0;

// 		Only for 1.Making, not for adding new files
		$DateiInfo = '$value_id = array();		$value_original_width = array();	$value_original_height = array();	$value_title = array();';
//		@fputs($handle_inc,"<?php".chr(13).chr(10));
//		@fputs($handle_inc,"$DateiInfo".chr(13).chr(10));

		for ($i=0; $i<count($lines);$i++)
		{
		   if ($lines[$i] && intval($lines[$i])) {
			$imgMenge += 1;
			$key = array_search($lines[$i],$value_id);
			if ($key !== false) {
				$yesInfo += 1;
				$DateiInfo = '$value_id[]='.$value_id[$key].';	$value_original_width[]='.$value_original_width[$key].';	$value_original_height[]='.$value_original_height[$key].';	$value_title[]="'.$value_title[$key].'";';
				@fputs($handle_inc,"$DateiInfo".chr(13).chr(10));
			}
			else {
				$seachString = 'https://stock.adobe.io/Rest/Media/1/Search/Files?locale=de_DE&search_parameters[filters][premium]=false&search_parameters[words]='.intval($lines[$i]);


$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => $seachString,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_POSTFIELDS => "",
  CURLOPT_HTTPHEADER => array(
    "X-Product: CFS_Test_1.0",
    "cache-control: no-cache",
    "x-api-key: bc83e43ed44c496da2313fddd8f2484b"
  ),
));
$response = curl_exec($curl);
curl_close($curl);
$results = json_decode($response, true);

/*
echo $response.'<br><br>';
echo "-------------------------------<br><br>";
echo $results.'<br><br>';
*/

				if (is_numeric($results['nb_results'])) $nb_results = $results['nb_results'];
				if ($nb_results >= 1)
				{
					foreach($results['files'] as $key => $value) 
					{
						$fileW = 0;
						$fileH = 0;
						if (!$value[vector_type])
						{
							$fileW = $value[width];
							$fileH = $value[height];
						}
						$DateiInfo = '$value_id[]='.$value[id].';	$value_original_width[]='.$fileW.';	$value_original_height[]='.$fileH.';	$value_title[]="'.preg_replace("/[^A-Za-z0-9\-\_\.]/",' ',$value[title]).'";';
						@fputs($handle_inc,"$DateiInfo".chr(13).chr(10));
						@fputs($handle_alleunsortiert,"$DateiInfo".chr(13).chr(10));
					}
				}
				else {
					$notInFotolia[] = $lines[$i].' : '.$DateiIncName;
				}
			}
		   }
		}
		@fputs($handle_inc,"?>".chr(13).chr(10));
		@fputs($handle_alleunsortiert,"?>".chr(13).chr(10));
		@fclose($handle_inc);
		@fclose($handle_alleunsortiert);
		@unlink($fileIDs);
		// --------------------------- Sort --------------------------- 
		$fotoliaNr_liste = array(); $fotoliaNr_menge = array();
		$value_menge = array();	
		include ($fileDB);
		include ($DateiInc);
		for ($i=0; $i<count($value_id);$i++)
		{
			$key = array_search($value_id[$i],$fotoliaNr_liste);
			if ($key !== false) $value_menge[$i] = $fotoliaNr_menge[$key];
			else $value_menge[$i] = 1;
		}
		arsort($value_menge);
		$handle = @fopen ($DateiInc, "w");
		@fputs($handle,"<?php".chr(13).chr(10));
		$DateiInfo = '// -------------- '.$DateiIncName.' ---------------------';
		@fputs($handle,"$DateiInfo".chr(13).chr(10));
		$DateiInfo = '$value_id = array();		$value_original_width = array();	$value_original_height = array();	$value_title = array();';
		@fputs($handle,"$DateiInfo".chr(13).chr(10));
		foreach ($value_menge as $key => $val) {
			$DateiInfo = '$value_id[]='.$value_id[$key].';	$value_original_width[]='.$value_original_width[$key].';	$value_original_height[]='.$value_original_height[$key].';	$value_title[]="'.$value_title[$key].'";';
			@fputs($handle,"$DateiInfo".chr(13).chr(10));
		}
		@fputs($handle,"?>".chr(13).chr(10));
		@fclose($handle);
		echo '<div class="col-md-12 bottom14">'.$DateiIncName.' : ' .$imgMenge. ' Motive, davon NEU : ' .($imgMenge-$yesInfo). '</div>';
		$menge_neuedateien += $imgMenge-$yesInfo;
	}
	// --------------------------- Sort alleunsortiert.inc --------------------------- 
	$fotoliaNr_liste = array(); $fotoliaNr_menge = array();
	$value_menge = array();	
	include ($fileDB);
	include ($fileAlleunsortiert);
	for ($i=0; $i<count($value_id);$i++)
	{
		$key = array_search($value_id[$i],$fotoliaNr_liste);
		if ($key !== false) $value_menge[$i] = $fotoliaNr_menge[$key];
		else $value_menge[$i] = 1;
	}
	arsort($value_menge);
	$handle = @fopen ($fileAlleunsortiert, "w");
	@fputs($handle,"<?php".chr(13).chr(10));
	$DateiInfo = '// -------------- alleunsortiert.inc ---------------------';
	@fputs($handle,"$DateiInfo".chr(13).chr(10));
	$DateiInfo = '$value_id = array();		$value_original_width = array();	$value_original_height = array();	$value_title = array();';
	@fputs($handle,"$DateiInfo".chr(13).chr(10));
	foreach ($value_menge as $key => $val) {
		$DateiInfo = '$value_id[]='.$value_id[$key].';	$value_original_width[]='.$value_original_width[$key].';	$value_original_height[]='.$value_original_height[$key].';	$value_title[]="'.$value_title[$key].'";';
		@fputs($handle,"$DateiInfo".chr(13).chr(10));
	}
	@fputs($handle,"?>".chr(13).chr(10));
	@fclose($handle);
	@unlink($ids_neuedateien_path.$fileList[$q]);
	echo '<div class="col-md-12 bottom14"><hr>alleunsortiert.inc : '.$menge_neuedateien.' NEUE Bilder<hr></div>';

	if (count($notInFotolia)) {
		echo '<p>NOT in Adobe : </p>';
		for ($i=0; $i<count($notInFotolia);$i++) echo $notInFotolia[$i].'<br>';
	}
}
if ($dateienKopieren) 	
{
	$fileList = scandir($fileID_path);
	for ($q=0; $q<count($fileList);$q++)
	{
		if (stristr($fileList[$q], ".inc")) 
		{
			if (copy($fileID_path.$fileList[$q], $galerie_list_path.$fileList[$q])) @unlink($fileID_path.$fileList[$q]);
		}
	}
}
if ($doppelteSuchen) 	
{
	if (isset($_GET['wahl']) && $_GET['wahl']<>'') { $wahl = urldecode($_GET['wahl']); $wahl = str_replace("_"," ",$wahl);}
	$wahl = str_replace("%20"," ",$wahl);
	if ($wahl) {

		$file = $galerie_list_path.strtolower(preg_replace("/[^A-Za-z0-9\-\_\.]/",'',$wahl)).'.inc';
		include ($file);
		$vc = array_count_values($value_id);
		$result = array();
		foreach($vc as $key => $value) {
		if($value > 1)
			array_push($result, $key);
		} 
  		if ($result) {
			echo '<hr><b>Doppelte Nummer in '.$wahl.'</b><br>';
			print_r($result);
		}
		else echo '<hr><b>Keine</b> Doppelte Nummer in '.$wahl.'<br>';
	}
	else echo "<p>keine Kategorie ausgew&auml;lt</p>";
}
if ($imagesCheck) 	
{
	$imgCopien_path = $galerie_list_path.'neue-ids/imgBestseller/';
	$images_path = '/var/www/GALERIE-SSL/images/bestseller/';
	include ($galerie_list_path.'alleunsortiert.inc');
	$a = '';
	for ($i=0; $i<count($value_id);$i++)
	{
		if (!@is_file($images_path.$value_id[$i].'.jpg')) $a .='<br>'.$value_id[$i];
	}
  	if ($a) {
		echo '<hr><b>Fehlende jpgs-Dateien</b>';
		echo $a;
	}

	$a = '';
	$fileList = scandir($images_path);
	$imgCopienNr = 0;
	for ($q=2; $q<count($fileList);$q++)
	{
		$jpgName = str_replace(".jpg", "", $fileList[$q]);
		if ((false === array_search($jpgName,$value_id)) && (!stristr($jpgName, "alleunsortiert"))) 
		{
			if (!@is_file($imgCopien_path))
			{
				@mkdir($imgCopien_path,0777);
				@chmod($imgCopien_path,0777);
			}
			copy($images_path.$fileList[$q], $imgCopien_path.$fileList[$q]);
			$a .='<br>'.$jpgName;
			$imgCopienNr += 1;
		}
	}
  	if ($a) {
		echo '<hr><b>'.$imgCopienNr.' jpgs-Dateien, die nicht in alleunsortiert.inc sind</b>';
		echo $a;
	}
}
if ($imagesCheckDot) 	
{
	$images_path = '/var/www/GALERIE-SSL/images/bestseller/';

	$imgCopienNr = 0;
	$fileList = scandir($images_path);

	for ($q=2; $q<count($fileList);$q++)
	{
		$jpgName = str_replace("..", ".", $fileList[$q]);
		if ($jpgName != $fileList[$q]) 
		{
			$imgCopienNr += 1;
			echo '<br>'.$fileList[$q];
			rename($images_path.$fileList[$q], $images_path.$jpgName);
		}
	}
	echo '<hr><b>'.count($fileList).' Dateien</b>';
	echo '<hr><b>'.$imgCopienNr.' Dateien changed</b>';
}
if ($fotolia_fotolia500) 	
{
	$ids_neuedateien_path = $fileID_path.'_ids_neuedateien/';
	$file1 = $ids_neuedateien_path.'fotolia.txt';
	$file2 = $ids_neuedateien_path.'fotolia-500px.txt';

	$fotolia = array();
	$handle = @fopen($file1, "r");
	if ($handle) {
   		while (!feof($handle)) {
       		$a = preg_replace("/[^0-9\-\_\.]/",'',fgets($handle, 4096));
			if ($a) $fotolia[] = intval($a);

   		}
	}
   	fclose($handle);

	$fotolia500 = array();
	$handle = @fopen($file2, "r");
	if ($handle) {
   		while (!feof($handle)) {
       		$a = preg_replace("/[^0-9\-\_\.]/",'',fgets($handle, 4096));
			if ($a) $fotolia500[] = intval($a);

   		}
	}
   	fclose($handle);

	echo count($fotolia500).' : '.count($fotolia);

	$result = array_diff($fotolia, $fotolia500);
	print_r($result);
}

if ($doppelteTXTSuchen) 	
{
	$file = $fileID_path.'localdisk-txt/1.txt';

	if (@is_file($file)) {
		$lines = array();
		$handle = @fopen($file, "r");
		if ($handle) {
   			while (!feof($handle)) {
       			$a = preg_replace("/[^0-9\-\_\.]/",'',fgets($handle, 4096));
				if ($a) $lines[] = intval($a);
   			}
		}
   		fclose($handle);
echo count($lines);

		$vc = array_count_values($lines);
		$result = array();
		foreach($vc as $key => $value) {
		if($value > 1)
			array_push($result, $key);
		} 
  		if ($result) {
			echo '<hr><b>Doppelte Nummer </b><br>';
			print_r($result);
		}
		else echo '<hr><b>Keine</b> Doppelte Nummer<br>';
	}
	else echo "<p>keine Datei /localdisk-txt/1.txt gefunden</p>";
}

if ($vectorTXTDownload) 	
{
	$file = $fileID_path.'localdisk-txt/vectors.txt';
	$vector_path = $fileID_path.'localdisk-txt/vector-jpgs/';

	if (@is_file($file)) {
		$lines = array();
		$handle = @fopen($file, "r");
		if ($handle) {
   			while (!feof($handle)) {
       			$a = preg_replace("/[^0-9\-\_\.]/",'',fgets($handle, 4096));
				if ($a) $lines[] = $a;
   			}
		}
   		fclose($handle);
		echo '<hr><b>Vectors</b> : '.count($lines).'<br>';
		for ($i=0; $i<count($lines);$i++)
		{
		   $seachString = 'https://stock.adobe.io/Rest/Media/1/Search/Files?locale=de_DE&search_parameters[filters][premium]=false&search_parameters[words]='.intval($lines[$i]);

$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => $seachString,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_POSTFIELDS => "",
  CURLOPT_HTTPHEADER => array(
    "X-Product: CFS_Test_1.0",
    "cache-control: no-cache",
    "x-api-key: bc83e43ed44c496da2313fddd8f2484b"
  ),
));
$response = curl_exec($curl);
curl_close($curl);
$results = json_decode($response, true);

				if (is_numeric($results['nb_results'])) $nb_results = $results['nb_results'];
				if ($nb_results >= 1)
				{
					foreach($results['files'] as $key => $value) 
					{
						$value[thumbnail_url] = str_replace("http:","https:",$value[thumbnail_url]);
						$value_thumbnail_500_url = str_replace("/160_F","/500_F",$value[thumbnail_url]);
						$file = $vector_path.$lines[$i].'.jpg';
						copy($value_thumbnail_500_url, $file);
					}
				}
		}
		for ($i=0; $i<count($lines);$i++)
		{
			$file = $vector_path.$lines[$i].'.jpg';
			if (!@is_file($file)) echo '<br>Error: '.$lines[$i];
		}
		echo '<hr><b>Vectors befinden sich im Ordner</b> : '.$vector_path.'<br>';
	}
	else echo "<p>keine Datei /localdisk-txt/vectors.txt gefunden</p>";
}

?>
