<?php
$path="../../";
include("../../map.php");
$cpath="../../"; include("../../scripts.php");
include("../../secure.php");
include("../../../deppo/tools/script/SimpleImage.php");
function irep($metin){
	$metin = str_replace("'", "", $metin);
	$metin = str_replace("×", "x", $metin);
	$metin = str_replace("|", "", $metin);
	return $metin;
}
if($_GET["islem"]=="dsablon"){
	$resize="";
}else{
	$resize="checked";
}
if($_GET["ne"]=="sayfa" or $_GET["ne"]=="haber" or $_GET["ne"]=="msayfa" or $_GET["ne"]=="kayit" or $_GET["islem"]=="dsayfa"){
	$sql=mysqli_query($conn, "select * from ".$_SESSION['lang']." where id=".$_GET["id"].""); $rs=mysqli_fetch_assoc($sql);
	$risim=$rs["t1"];
}else{
	$risim=$_GET["ne"];
}
// PHP ile izin verilen maksimum dosya boyutunu alıyoruz
$max_upload = ini_get('upload_max_filesize');
$max_post = ini_get('post_max_size');
$max_file_size_bytes = min(parse_size($max_upload), parse_size($max_post)); // Küçük olan değeri alıyoruz
// Boyutları byte cinsine çevirmek için bir fonksiyon
function parse_size($size) {
    $unit = strtolower(substr($size, -1));
    $value = (int) $size;
    switch ($unit) { case 'g': return $value * 1024 * 1024 * 1024; case 'm': return $value * 1024 * 1024; case 'k': return $value * 1024; default: return $value; }
}
?>
<style>
.col-item { border: 2px solid red; border-radius: 10px; width: 49% !important; margin-right: 5px; margin-bottom: 5px; height:180px; margin-bottom:10px; }
</style>
<!-- Nav tabs -->
<ul class="nav nav-tabs" role="tablist">
	<li role="presentation" class="active"><a href="#tb1" aria-controls="tb1" role="tab" data-toggle="tab"><?php echo $sdil[89]; //Resim Upload?> </a></li>
	<li role="presentation"><a href="#tb3" aria-controls="tb3" role="tab" data-toggle="tab"><?php echo $sdil[209]; //Resimler?></a></li>
	<?php if($_GET["islem"]<>"epost"){?>
		<li role="presentation"><a href="#tb2" aria-controls="tb2" role="tab" data-toggle="tab">Arşiv</a></li>
	<?php }?>
	<?php if($MSA<>""){?>
		<!--<li role="presentation"><a class="upload_image_search_google" href="#tb4" aria-controls="tb4" role="tab" data-toggle="tab" title="Search Image With Google">Google Keşfet</a></li>-->
	<?php }?>
		<li role="presentation"><a class="upload_image_search_pexels" href="#tb5" aria-controls="tb5" role="tab" data-toggle="tab" title="Search Image With Pexels">Keşfet</a></li>
		<li role="presentation" class="btn-warning" style="float: right;"><a href="<?php echo $weburl."/".$crmpath."/"?>modules/ajax/ajax-upload.php?ne=<?php echo $_GET["ne"];?>&islem=<?php echo $_GET["islem"];?>&id=<?php echo $_GET["id"]; ?>&s4=<?php echo $_GET["s4"]; ?>" target="modal">Multi Upload</a></li>
</ul>
<!-- Tab panes -->
<div class="tab-content">
	<div role="tabpanel" class="tab-pane active" id="tb1">
		<form method="POST" id="reform" name="frm" enctype="multipart/form-data" action="?ne=<?php echo $_GET["ne"];?>&islem=<?php echo $_GET["islem"];?>&durum=uploadimage&id=<?php echo $_GET["id"];?>" onSubmit="return check(this)">
			<div class="panel">
				<div class="panel-heading panel-<?php if($_GET["durum"]==ok){?>success<?php }else{?>default<?php }?>">
					<?php if($_GET["durum"]==ok){?>
						#<?php echo $sdil[212]; //islem Basariyla Gerçeklestirilmistir !?>
					<?php }?>
				</div>
				<div class="panel-body">
					<?php for ($i = 10; $i >= 1 ; $i-- ){?>
						<input type="hidden" name="gs4" value="<?php echo $_GET["s4"];?>"><!--İç Sayfa Id-->
						<div id="i<?php echo $i?>" style="min-height:25px; <?php if($i<>1){?>display:none;<?php }?> padding:5px; position:relative;">
							<label class="ifile" style="float: left;"> Resim max(<?php echo $max_upload?>)
								<input id="r<?php echo $i-1?>" type="file" name="resim<?php echo $i?>" accept="*" onchange="document.frm.durum<?php echo $i?>.checked=true;<?php if($b3==1){?>show('i<?php echo $i+1?>');<?php }?>"  style="float:left;"/>
							</label>
							<div style="float:right; width: 10%;">
								<input type="checkbox" name="durum<?php echo $i?>" value="ON" style="" title="Resim Var/Yok">
								<?php if($a5<>0){?>
									<input type="checkbox" name="boyut<?php echo $i?>" value="ON" style="float:left; margin-left:5px;" title="Boyutlandır" <?php echo $resize?> id="boyut<?php echo $i?>" class="mini-switch">
									<label for="boyut<?php echo $i?>"></label>
								<?php }?>
								<i class="fa fa-angle-double-down" style="float:right; margin-left: -25px; margin-top: -25px; cursor:pointer; font-size:22px;" onclick="toggle('#ib<?php echo $i?>');toggle('#ibu<?php echo $i?>');toggle('#ia<?php echo $i?>')"  id="ib<?php echo $i?>" title="Açıklama"></i>
								<i class="fa fa-angle-double-up" style="float:right; margin-left: -25px; margin-top: -25px; cursor:pointer; font-size:22px; display:none" onclick="toggle('#ib<?php echo $i?>');toggle('#ibu<?php echo $i?>');toggle('#ia<?php echo $i?>')"  id="ibu<?php echo $i?>" title="Açıklama"></i>
							</div>
							<div id="ia<?php echo $i?>" style="<?php if($i<>1){?>display:none;<?php }?> margin-top:50px; position:relative;">
								<input type="text" id="t<?php echo $i?>" class="form-control" name="t2_<?php echo $i?>" placeholder="<?php echo  trim($sdil[206]); //Başlık?>" value="<?php echo $risim;?>">
								<textarea name="n3_<?php echo $i?>" class="form-control" id="n<?php echo $i?>" style="margin-top:10px;" placeholder="<?php echo trim($sdil[207]); //Açıklama?>"></textarea>
								<div class="row <?php if($_GET["islem"]=="epost"){echo "dn";}?>" style="margin-top:10px;">
									<div class="col-xs-8">
										<input type="text" name="t4_<?php echo $i?>" style="margin-right:10px;" class="form-control" value="<?php echo $rs["t4"];?>" placeholder="http://www.">
									</div>
									<div class="col-xs-4">
										<select size="1" name="n6_<?php echo $i?>" style="" class="form-control">
											<option value="<?php echo $rs["n6"];?>"> <?php if($rs["n6"]==""){echo "Target";}else{echo $rs["n6"];}?> </option>
											<option value="_self" >_self</option>
											<option value="_blank" >_blank</option>
											<option value="_top" >_top</option>
										</select>
									</div>
								</div>
								<div class="row" style="margin-top:10px;">
									<div class="col-xs-4 <?php if($_GET["islem"]=="epost"){echo "dn";}?>">
										<select size="1" name="s1_<?php echo $i?>" style="width:100%;" title="(s1)" class="v3-form-control">
											<option value="<?php echo $_GET["id"]?>">[<?php echo $_GET["id"]?>] <?php echo translate("Varsayılan");?></option>
											<?php $sqll = mysqli_query($conn, "select * from ".$_SESSION['lang']." where ne='".$_GET["ne"]."'" ); while($rsl=mysqli_fetch_assoc($sqll)){?>
												<option value="<?php echo $rsl["id"];?>" <?php if($rsl["id"]==$_GET["id"]){?>Selected<?php }?>>[<?php echo $rsl["id"];?>] <?php echo $rsl["t1"];?></option>
											<?php }?>
										</select>
									</div>
									<div class="col-xs-4 <?php if($_GET["islem"]=="epost"){echo "dn";}?>">
										<select size="1" name="s4_<?php echo $i?>" style="width:100%;" title="Grup (s4)" class="v3-form-control">
											<option value="0" <?php if($rs["s4"]=="0"){?>Selected<?php }?>>[0] <?php echo translate("Grup Seçimi");?></option>
											<?php if($m4<>""){$g=0; $rgrups = explode(',',$m4); foreach($rgrups as $rgrup) {$g++;?>
												<option value="<?php echo $g;?>" <?php if($_GET["s4"]==$g){?>Selected<?php }?>>[<?php echo $g;?>] <?php echo $rgrup;?></option>
											<?php }}?>
											<?php if($module["g1_".$_GET["ne"].""]=="1"){?>
												<?php $sql=mysqli_query($conn, "select * from t1 where ne='isayfa' and (t2='".$_SESSION['lang']."' or t2='All') and s1=".$_GET["id"]." order by s2 asc"); while($xn=mysqli_fetch_assoc($sql)){?>
													<option value="<?php echo $xn["id"];?>" <?php if($_GET["s4"]==$xn["id"]){?>Selected<?php }?>>[<?php echo $xn["id"];?>] <?php echo $xn["t1"];?></option>
												<?php }}?>
											</select>
										</div>
										<div class="col-xs-<?php if($_GET["islem"]=="epost"){echo "12";}else{echo "4";}?>">
											<button type="submit" style="float:right; margin-right:10px; margin-top:10px;" id="btn" class="btn btn-primary loading demo"><?php echo $sdil[203]; //Kaydet?></button>
										</div>
									</div>
								</div>
							</div>
							<div style="clear:both;"></div>
						<?php }?>
					</div>
				</div>
			</form>
		</div>
		<div role="tabpanel" class="tab-pane" id="tb3" style="padding:20px;">
			<script type="text/JavaScript" language="JavaScript">
				jQuery(document).ready(function () {
					loadFunction('../ajax/ajax_medya_iframe.php','#mresim', 'resim');
				});
				document.write("<div id=\"mresim\"></div>");
			</script>
		</div>
		<div role="tabpanel" class="tab-pane" id="tb2">
			<input type="text" placeholder="Resim aramak için kelime giriniz" id="q" class="form-control">
			<div class="postList">
				<div class="col-xs-6 col-md-3" style="height:180px; margin-bottom:10px; padding:5px;">
					<a href="?ne=<?php echo $_GET["ne"];?>&islem=<?php echo $_GET["islem"];?>&id=<?php echo $_GET["id"];?>&durum=addsample&mid=0" class=" btn btn-danger" style="position:absolute; right: 10px; top: 10px; font-size: 28px; padding: 0px; line-height: normal; border:0px;" id="sr0"><i class="fa fa-plus-square" style="padding: 0; font-size: 24px; background-color: unset !important;"></i></a>
					<a href="//sample.<?php echo $cdnurl?>/800x600" class="thumbnail" data-fancybox="gallery" style="height:inherit; overflow:hidden;background-image:url('//sample.<?php echo $cdnurl?>/800x600');background-size: contain; background-position: center; background-repeat: no-repeat;"></a>
				</div>
				
				<div id="upload_image_search"></div>
			</div>
			<script type="text/JavaScript" language="JavaScript">
				jQuery(document).ready(function () {
					loadFunction('upload_image_search.php?ne=<?php echo $_GET["ne"]?>&islem=<?php echo $_GET["islem"]?>&id=<?php echo $_GET["id"]?>','#upload_image_search', 'resim');
				});
				jQuery("#q").keyup(delay(function (e) {
					var q = jQuery('#q').val();
					jQuery('#upload_image_search').load('upload_image_search.php?ne=<?php echo $_GET["ne"]?>&islem=<?php echo $_GET["islem"]?>&id=<?php echo $_GET["id"]?>&q='+q+'');
				}, 1000));
			</script>
		</div>
		<?php if($MSA<>""){?>
		    <!--
			<div role="tabpanel" class="tab-pane" id="tb4">
				<input type="text" placeholder="Resim aramak için kelime giriniz" id="qw" class="form-control" value="<?php echo $rs["t1"]?>">
				<div class="postList">
					<div id="upload_image_search_google"></div>
				</div>
				<script type="text/JavaScript" language="JavaScript">
					jQuery(".upload_image_search_google").click(function () {
						loadFunction('upload_image_search_google.php?ne=<?php echo $_GET["ne"]?>&islem=<?php echo $_GET["islem"]?>&id=<?php echo $_GET["id"]?>','#upload_image_search_google', 'resim');
					});
					jQuery("#qw").keyup(delay(function (e){
						var q = jQuery('#qw').val().trim().replace(/\s+/g, '+');
						jQuery('#upload_image_search_google').load('upload_image_search_google.php?ne=<?php echo $_GET["ne"]?>&islem=<?php echo $_GET["islem"]?>&id=<?php echo $_GET["id"]?>&q='+q+'');
					}, 1000));
				</script>
			</div>
			-->
			<div role="tabpanel" class="tab-pane" id="tb5">
				<input type="text" placeholder="Resim aramak için kelime giriniz" id="qw" class="form-control" value="<?php echo $rs["t1"]?>">
				<div class="postList">
					<div id="upload_image_search_pexels"></div>
				</div>
				<script type="text/JavaScript" language="JavaScript">
					jQuery(".upload_image_search_pexels").click(function () {
						loadFunction('upload_image_search_pexels.php?ne=<?php echo $_GET["ne"]?>&islem=<?php echo $_GET["islem"]?>&id=<?php echo $_GET["id"]?>','#upload_image_search_pexels', 'resim');
					});
					jQuery("#qw").keyup(delay(function (e){
						var q = jQuery('#qw').val().trim().replace(/\s+/g, '+');
						jQuery('#upload_image_search_pexels').load('upload_image_search_pexels.php?ne=<?php echo $_GET["ne"]?>&islem=<?php echo $_GET["islem"]?>&id=<?php echo $_GET["id"]?>&q='+q+'');
					}, 1000));
				</script>
			</div>
		<?php }?>
	</div>
</div>
<?php
if ($_GET["durum"]=="uploadimage"){
	if(!empty($_SESSION["user_id"])){$s19=$_SESSION["user_id"];}else{$s19=0;}
	if(!empty($_GET["id"])){$ID=$_GET["id"];}else{$ID="0";}
    //if(!empty($_POST["gs4"])){$s4=$_POST["gs4"];}elseif(!empty($_POST["gs4"])){$s4=$_POST["s4"];}else{$s4=0;}
	$kaydedilecekyer = "../../../deppo/image";
	if(!empty($_POST["s1_1"])){$ID=$_POST["s1_1"];}else{$ID=0;}
	if(!empty($_POST["s4_1"])){$s4=$_POST["s4_1"];}else{$s4=0;}
	for ($i = 1; $i <= 10; $i++) {
		$i1isim = !empty($_POST["t2_{$i}"]) ? substr(cevir($_POST["t2_{$i}"]), 0, 30) : '';
		if (!empty($_POST["durum{$i}"])) {
			$sqlk = mysqli_query($conn, "SELECT * FROM medya WHERE ne='resim' AND t3='{$_GET['ne']}' AND s1='{$_POST["s1_{$i}"]}'");
			$ks2 = (mysqli_num_rows($sqlk) > 0) ? "0" : "1";
			$kaynak = $_FILES["resim{$i}"]['tmp_name'];
			$uzanti = pathinfo($_FILES["resim{$i}"]['name'], PATHINFO_EXTENSION);
			$rand = substr(md5(uniqid(rand())), 0, 10);
			$yeni = "{$i1isim}-{$rand}.{$uzanti}";
			$k_klasor = "../../../deppo/image/";
			move_uploaded_file($kaynak, $kaydedilecekyer . "/{$yeni}");
			// Görüntü boyutlandırma ve işleme
			$image = new SimpleImage();
			$image->load($k_klasor . $yeni);
			if (!empty($_POST["boyut{$i}"])) {
				$image->resizeToWidth($a9);
				$image->save($k_klasor . $yeni);
			}
			$image->resizeToWidth($a10);
			$image->save($k_klasor . "k_{$yeni}");
			// Damga ekleme
			if (!empty($_POST["boyut{$i}"]) && $e1 == 1) {
				$damga = imagecreatefrompng("{$k_klasor}{$t47}");
				$oyeni = imagecreatefromjpeg($k_klasor . $yeni);
				$sx = imagesx($damga);
				$sy = imagesy($damga);
				imagecopy($oyeni, $damga, imagesx($oyeni) - $sx - 10, imagesy($oyeni) - $sy - 10, 0, 0, $sx, $sy);
				imagepng($oyeni, "{$k_klasor}{$yeni}");
				imagedestroy($oyeni);
			}
			// WEBP dönüşümü
			if ($module["webp"] == 1) {
				towebp("k_{$yeni}", $k_klasor);
				$yeni = towebp($yeni, $k_klasor);
			}
			// Veritabanı kaydı
			$conn->query(" INSERT INTO medya (ne, s1, s2, s3, s4, s13, t1, t3, t4, t6, t2, n3, n4, n6, s19) VALUES ( 'resim', '{$ID}', '{$ks2}', '0', '{$s4}', '1', '" . addslashes($yeni) . "', '{$_GET['ne']}', '{$_POST["t4_{$i}"]}', '{$_SESSION["lang"]}', '" . addslashes($_POST["t2_{$i}"]) . "', '" . addslashes($_POST["n3_{$i}"]) . "', '{$_POST["n4_{$i}"]}', '{$_POST["n6_{$i}"]}', '{$s19}' ) ");
		}
	}

	if($_GET["islem"]<>"cdetail"){
		echo location("?ne=".$_GET["ne"]."&islem=".$_GET["islem"]."&id=".$_GET["id"]."&durum=ok",self,1,1);
	}else{
		echo location("../../fonksiyon.php?ne=".$_GET["ne"]."&islem=".$_GET["islem"]."&id=".$_GET["id"]."&iid=".$_GET["iid"]."&s5=".$_GET["s5"]."&durum=ok#files",self,1,1);
	}

}elseif ($_GET["durum"]=="addsample"){
	$sqlm=mysqli_query($conn, "select * from medya where ne='resim' and t3='".$_GET['ne']."' and s1='".$_GET['id']."'"); $rski=mysqli_fetch_assoc($sqlm);
	$sqlk=mysqli_query($conn, "select * from ".$tbl." where ne='".$_GET["ne"]."' and id='".$_GET['id']."'"); $rsk=mysqli_fetch_assoc($sqlk);
	if(mysqli_num_rows($sqlm)>0){$ks2="0";}else{$ks2="1";}
	$risim=substr(cevir($rsk["t1"]),0,30);
	$randx  = substr(md5(uniqid(rand())),0,10);
	$yenix  = "".$risim."-".$randx.".png";
	$imageString = file_get_contents("https://sample.".$cdnurl."/800x600");
	$save = file_put_contents("../../../deppo/image/".$risim."-".$randx.".png", $imageString);
	$save = file_put_contents("../../../deppo/image/k_".$risim."-".$randx.".png", $imageString);
	$conn->query("insert INTO medya (ne,s1,s2,s3,s4,s13,t1,t3,t6,t2,n3,s19) VALUES ('resim', '".$_GET["id"]."', '".$ks2."', '1', '0', '1', '".$yenix."', '".$_GET["ne"]."', '".$_SESSION["lang"]."', '".$rsk["t1"]."', '".$rsk["n2"]."','".$s19."')");
	echo location("?ne=".$_GET["ne"]."&islem=".$_GET["islem"]."&id=".$_GET["id"]."&durum=ok",self,1,1);

}elseif ($_GET["durum"]=="addimage"){
    $sqlm=mysqli_query($conn, "select * from medya where ne='resim' and id='".$_GET['mid']."'"); $rsm=mysqli_fetch_assoc($sqlm); //Mevcut Resim
    $sqlk=mysqli_query($conn, "select * from ".$tbl." where ne='".$_GET["ne"]."' and id='".$_GET['id']."'"); $rsk=mysqli_fetch_assoc($sqlk);	//Yüklenecek içerik
    $sqlm=mysqli_query($conn, "select * from medya where ne='resim' and t3='".$_GET['ne']."' and s1='".$_GET['id']."'"); $rski=mysqli_fetch_assoc($sqlm);
    if(mysqli_num_rows($sqlm)>0){$ks2="0";}else{$ks2="1";}
    $risim=substr(cevir($rsk["t1"]),0,30);
    $uzantix = explode('.',$rsm["t1"]);
    $randx  = substr(md5(uniqid(rand())),0,10);
    $yenix  = "".$risim."-".$randx.".".$uzantix[count($uzantix)-1]."";
    $imageString = file_get_contents("../../../deppo/image/".$rsm["t1"]."");
    $save = file_put_contents("../../../deppo/image/".$risim."-".$randx.".".$uzantix[count($uzantix)-1]."", $imageString);
    $save = file_put_contents("../../../deppo/image/k_".$risim."-".$randx.".".$uzantix[count($uzantix)-1]."", $imageString);
    $conn->query("insert INTO medya (ne,s1,s2,s3,s4,s13,t1,t3,t6,t2,n3,s19) VALUES ('resim', '".$_GET["id"]."', '".$ks2."', '1', '".$rsm["s4"]."', '1', '".$yenix."', '".$_GET["ne"]."', '".$_SESSION["lang"]."', '".$rsk["t1"]."', '".$rsk["n2"]."','".$s19."')");
    echo location("?ne=".$_GET["ne"]."&islem=".$_GET["islem"]."&id=".$_GET["id"]."&durum=ok",self,1,1);

}elseif ($_GET["durum"]=="addimagewgoogle"){
	$risim = str_replace("...", "", kisalt($risim, 2));
	$src = $_GET["src"] ?? '';
	if(empty($src)) { die("Resim kaynağı boş"); }

	$imageString = @file_get_contents($src);
	if(!$imageString){ die("Resim alınamadı"); }

	$uzanti_ = pathinfo($src);
	$uzanti = strtolower($uzanti_['extension']);
	$randx = substr(md5(uniqid(rand())),0,10);

	$dosya1 = "../../../deppo/image/".cevir($risim)."-".$randx.".".$uzanti;
	$dosya2 = "../../../deppo/image/k_".cevir($risim)."-".$randx.".".$uzanti;

	if(!file_put_contents($dosya1, $imageString) || !file_put_contents($dosya2, $imageString)){
		die("Dosya kaydedilemedi, yazma izinlerini kontrol edin");
	}

	$rsi = $conn->query("INSERT INTO medya(ne,t3,t1,s1,s2,s3,s19) VALUES ('resim', '".$_GET["ne"]."', '".cevir($risim)."-".$randx.".".$uzanti."', '".$_GET["id"]."', '1', '0','".$s19."')");
	if(!$rsi){ die("Veritabanı hatası: ".$conn->error); }
	echo location("?ne=".$_GET["ne"]."&islem=".$_GET["islem"]."&id=".$_GET["id"]."&durum=ok",self,1,1);

}
?>
<?php include("../public/log.php");?>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>