<?php
if(file_exists($_FILES['watermark']['tmp_name']) && is_uploaded_file($_FILES['watermark']['tmp_name'])) {
		
	$file_data = $_FILES['watermark']['tmp_name'];
	$file_name = $_FILES['watermark']['name'];
	$file_type = $_FILES['watermark']['type'];
			
	$extension_tmp = explode(".", $file_name);
	$extension = end($extension_tmp);
	
	$extension_new = mime2ext($file_type, $extension);
	
	$file_new = "watermark.".$extension_new;
	$file_dir = "images/";
		
	$sql_tmp = "SELECT value FROM settings_options WHERE title = 'watermark' ";
	$result_tmp = $conn->prepare($sql_tmp);
	$result_tmp->execute();
	if ($result_tmp->rowCount()){
		$arr_tmp = $result_tmp->fetch();
		if (is_file($file_dir.$arr_tmp['value'])) unlink($file_dir.$arr_tmp['value']);
	  $sql = "UPDATE settings_options SET value = :value, c_public = '0' WHERE title = 'watermark'";
		$result = $conn->prepare($sql);
		$result->bindValue(':value', $file_new, PDO::PARAM_STR);
		$result->execute();
	} else {
		if (is_file($file_dir."watermark.png")) unlink($file_dir."watermark.png");
	  $sql="INSERT INTO settings_options SET value = :value, title = 'watermark', c_public = '0'";
		$result = $conn->prepare($sql);
		$result->bindValue(':value', $file_new, PDO::PARAM_STR);
		$result->execute();
	}
	
	if (!isset($watermark_width) || !is_numeric($watermark_width) || $watermark_width <= 0 ||
    !isset($watermark_height) || !is_numeric($watermark_height) || $watermark_height <= 0) {
    
		$image_size = getimagesize($file_data);
		
		if ($image_size !== false) {
			$watermark_width = $image_size[0];
			$watermark_height = $image_size[1];
		} else {
			$watermark_width = 100;
			$watermark_height = 100;
		}
	}

	if (isset($watermark_ratio) && is_numeric($watermark_ratio) && $watermark_ratio > 0) {
	  $watermark_ratio = floatval(str_replace(',', '.', $watermark_ratio));
	  $watermark_width = floatval($watermark_width) * $watermark_ratio;
	  $watermark_height = floatval($watermark_height) * $watermark_ratio;
	}
	
	copy($file_data, $file_dir.'tmp_'.$file_new);
	resize($file_dir.'tmp_'.$file_new, $file_dir.$file_new, $watermark_width, $watermark_height);
	unlink($file_dir.'tmp_'.$file_new);
	chmod($file_dir.$file_new, 0644);
}


if(isset($_POST['update']) && $_POST['update'] == 1){
	
	if ($module != 'gallery_options') {
		$sql_tmp = "SELECT title FROM settings_options WHERE title IN ('c_lightbox_watermark', 'c_thumbnails_watermark') AND c_public = '0'";
	} else {
		$sql_tmp = "SELECT title FROM ".$module." WHERE title IN ('c_lightbox_watermark', 'c_thumbnails_watermark') AND c_public = '0'";
	}
	
	$result_tmp = $conn->prepare($sql_tmp);
	$result_tmp->execute();
	while ($arr_tmp = $result_tmp->fetch()){
		if ($module != 'gallery_options') {
	 		$sql = "UPDATE settings_options SET value = :value WHERE title = :title ";
	 	} else {
	 		$sql = "UPDATE ".$module." SET value = :value WHERE title = :title ";
	 	}
		$result = $conn->prepare($sql);
		if (isset($_POST[$arr_tmp['title']]) && $_POST[$arr_tmp['title']]){
			$result->bindValue(':value', $_POST[$arr_tmp['title']], PDO::PARAM_STR);
		} else {
			$result->bindValue(':value', 0, PDO::PARAM_STR);
		}
		$result->bindValue(':title', $arr_tmp['title'], PDO::PARAM_STR);
		$result->execute();
	}
	
	$sql_tmp = "SELECT title FROM settings_options WHERE title IN ('watermark_width', 'watermark_height', 'watermark_ratio', 'watermark_position', 'watermark_top', 'watermark_right', 'watermark_bottom', 'watermark_left') AND c_public = '0'";
	$result_tmp = $conn->prepare($sql_tmp);
	$result_tmp->execute();
	while ($arr_tmp = $result_tmp->fetch()){
		$sql = "UPDATE settings_options SET value = :value WHERE title = :title ";
		$result = $conn->prepare($sql);
		$result->bindValue(':value', trim(stripslashes(nl2br($_POST[$arr_tmp['title']]))), PDO::PARAM_STR);
		$result->bindValue(':title', $arr_tmp['title'], PDO::PARAM_STR);
		$result->execute();
	}
	
}