<?php

function fnc_getNavPathArray($tblPrefix, $navId)
{
	global $db;

	$return = $navId;

	if ($navId !== '0')
	{
		$result = $db->query('SELECT * FROM ' . $tblPrefix . '_' . TBL_NAVI . ' WHERE ' . $tblPrefix . '_navId = "' . $navId . '";');
		if ($result->num_rows !== 0)
		{
			$navPoint = $result->fetch_assoc();
			$return .= ' ' . fnc_getNavPathArray($tblPrefix, $navPoint[$tblPrefix . '_navStart']);
		}
	}

	return $return;
}

function fnc_buildUniqueLinkName($tblPrefix, $navStart, $navName)
{
	global $db;

	$search = array('ä', 'ö', 'ü', 'ß', ' ', '.', '/');
	$replace = array('ae', 'oe', 'ue', 'ss', '_', '', '');
	$ent = array_keys(get_html_translation_table(ENT_HTML5));

	$return = str_replace($search, $replace, mb_strtolower($navName, mb_detect_encoding($navName)));
	$return = str_replace($ent, '', $return);

	$result = $db->query('SELECT * FROM ' . $tblPrefix . '_' . TBL_NAVI . ' WHERE ' . $tblPrefix . '_navStart = "' . $navStart . '" AND ( ' . $tblPrefix . '_navLink = "' . $return . '" OR ' . $tblPrefix . '_navLink REGEXP "' . $return . '\_[0-9]" );');
	$count = $result->num_rows;

	$return = ($count > 0)
		? $return . '_' . $count
		: $return;

	return $return;
}

function fnc_buildUniqueFileName($prefix, $navId, $fileName, $fileExt)
{
	$search = array('ä', 'ö', 'ü', 'ß', ' ', '/');
	$replace = array('ae', 'oe', 'ue', 'ss', '_', '');
	$ent = array_keys(get_html_translation_table(ENT_HTML5));

	$return = str_replace($search, $replace, mb_strtolower($fileName, mb_detect_encoding($fileName)));
	$return = str_replace($ent, '', $return);

	$result = glob(PATH_MED . $prefix . '/' . $navId . '/' . $return . '*_orig.' . $fileExt);
	$count = count($result);
	$return = ($count > 0)
		? $return . '_' . $count
		: $return;

	$return .= '.' . $fileExt;

	return $return;
}

function fnc_checkOrigResize($filePath, $fileName_orig, $fileName_temp, $fileExt, $newSize)
{
	global $vC;
	list ($width, $height) = getimagesize($filePath . $fileName_temp);

	if ($width > $newSize['width'] || $height > $newSize['height'])
	{
		if ($newSize['width'] / $width < $newSize['height'] / $height)
		{
			$newWidth = $newSize['width'];
			$newHeight = $height * $newSize['width'] / $width;
		}
		else
		{
			$newWidth = $width * $newSize['height'] / $height;
			$newHeight = $newSize['height'];
		}
		$newImage = imagecreatetruecolor($newWidth, $newHeight);

		if ($fileExt === 'jpg')
		{
			$origImg = imagecreatefromjpeg($filePath . $fileName_temp);
		}
		else if ($fileExt === 'png')
		{
			$origImg = imagecreatefrompng($filePath . $fileName_temp);
		}
		else if ($fileExt === 'gif')
		{
			$origImg = imagecreatefromgif($filePath . $fileName_temp);
		}
		else
		{
			return false;
		}

		$return = imagecopyresampled($newImage, $origImg, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);

		if ($return === true)
		{
			if ($fileExt === 'jpg')
			{
				$return = imagejpeg($newImage, $filePath . $fileName_orig, $vC['portal']['imgQuality']);
			}
			else if ($fileExt === 'png')
			{
				$return = imagepng($newImage, $filePath . $fileName_orig);
			}
			else if ($fileExt === 'gif')
			{
				$return = imagegif($newImage, $filePath . $fileName_orig);
			}
		}

		unlink($filePath . $fileName_temp);
		return $return;
	}
	else
	{
		rename($filePath . $fileName_temp, $filePath . $fileName_orig);
	}

	return true;
}

function fnc_buildImage($filePath, $fileName, $fileExt, $newSize, $previewParams)
{
	global $vC;

	list ($width, $height) = getimagesize($filePath . $fileName);
	$newImage = imagecreatetruecolor($newSize['width'], $newSize['height']);

	if ($fileExt === 'jpg')
	{
		$origImg = imagecreatefromjpeg($filePath . $fileName);
	}
	else if ($fileExt === 'png')
	{
		$origImg = imagecreatefrompng($filePath . $fileName);
	}
	else if ($fileExt === 'gif')
	{
		$origImg = imagecreatefromgif($filePath . $fileName);
	}
	else
	{
		return false;
	}

	$return = imagecopyresampled($newImage, $origImg, 0, 0, $previewParams['previewLeft'] * ($width / $newSize['width']), $previewParams['previewTop'] * ($width / $newSize['width']), $newSize['width'], $newSize['height'], $width * ($previewParams['previewWidth'] / $newSize['width']), $newSize['height'] * ($width / $newSize['width']) * ($previewParams['previewWidth'] / $newSize['width']));

	if ($return === true)
	{
		if ($fileExt === 'jpg')
		{
			$return = imagejpeg($newImage, $filePath . str_replace('_orig', '', $fileName), $vC['portal']['imgQuality']);
		}
		else if ($fileExt === 'png')
		{
			$return = imagepng($newImage, $filePath . str_replace('_orig', '', $fileName));
		}
		else if ($fileExt === 'gif')
		{
			$return = imagegif($newImage, $filePath . str_replace('_orig', '', $fileName));
		}
	}

	return $return;
}

function fnc_buildThumbnail($filePath, $fileName, $fileExt, $newSize)
{
	global $vC;

	list ($width, $height) = getimagesize($filePath . $fileName);
	$newImage = imagecreatetruecolor($newSize['width'], $newSize['height']);

	if ($fileExt === 'jpg')
	{
		$origImg = imagecreatefromjpeg($filePath . $fileName);
	}
	else if ($fileExt === 'png')
	{
		$origImg = imagecreatefrompng($filePath . $fileName);
	}
	else if ($fileExt === 'gif')
	{
		$origImg = imagecreatefromgif($filePath . $fileName);
	}
	else
	{
		return false;
	}

	$return = imagecopyresampled($newImage, $origImg, 0, 0, 0, 0, $newSize['width'], $newSize['height'], $width, $height);

	if ($return === true)
	{
		if ($fileExt === 'jpg')
		{
			$return = imagejpeg($newImage, $filePath . str_replace('.' . $fileExt, '_thumb.' . $fileExt, $fileName), $vC['portal']['imgQuality']);
		}
		else if ($fileExt === 'png')
		{
			$return = imagepng($newImage, $filePath . str_replace('.' . $fileExt, '_thumb.' . $fileExt, $fileName));
		}
		else if ($fileExt === 'gif')
		{
			$return = imagegif($newImage, $filePath . str_replace('.' . $fileExt, '_thumb.' . $fileExt, $fileName));
		}
	}

	return $return;
}