<?php
if (!defined("root")) {
	define("root", $_SERVER['DOCUMENT_ROOT']);
}
require(root."/___simple_config.php");
session_start();
require(root."/".adminFolder."/_common.php");
require(root."/".adminFolder."/_lib/db.php");
require(root."/".adminFolder."/_lib/main.php");
$a = cekpost(cleanQuery($_GET["a"]), "");
$err = "";
if ($a=="readyform") {
	$formodule = trim(cekpost(cleanQuery($_GET["formodule"]), ""));
	$kd = cekpost(cleanQuery($_GET["kd"]), "0");
	$tipeform = cekpost(cleanQuery($_GET["tipeform"]), "");
	$tipedata = cekpost(cleanQuery($_GET["tipedata"]), "");
	#tipeform = 0=single file or 1=multiple files
	#tipedata = 0=image or 1=others
	if ($tipeform==0) {
		cout('<form method="post" target="quick_iframe" action="/'.adminFolder.'/_ajax/ajax_files.php?a=do_upload" role="form" enctype="multipart/form-data">');
			cout('<input type="hidden" name="kirim" id="kirim" value="1" />');
			cout('<input type="hidden" name="kddata" id="kddata" value="'.$kd.'" />');
			cout('<input type="hidden" name="tipeform" id="tipeform" value="'.$tipeform.'" />');
			cout('<input type="hidden" name="tipedata" id="tipedata" value="'.$tipedata.'" />');
			cout('<input type="hidden" name="formodule" id="formodule" value="'.$formodule.'" />');
			cout('<dl class="dl-horizontal dldata">');
				cout('<dt><label for="namafile">File</label></dt>');
				cout('<dd><input class="form-control" type="file" name="namafile" id="namafile" /></dd>');
				if ($tipedata == 0) {
					cout('<dt><label for="image_title">Image Title</label></dt>');
					cout('<dd><input class="form-control" type="text" name="image_title" id="image_title" /></dd>');
					
					cout('<dt><label for="image_alt">Image Alt</label></dt>');
					cout('<dd><input class="form-control" type="text" name="image_alt" id="image_alt" /></dd>');
					if ($formodule=="games") { $mw = 1080; $mh = 800; }
					if ($formodule=="game_logo") { $mw = 220; $mh = 100; }
					if ($formodule=="news") { $mw = 600; $mh = 350; }
					if ($formodule=="promo") { $mw = 800; $mh = 300; }
					cout('<dt>Dimension</dt>');
					cout('<dd>Minimum of dimension requirement:<br/>Width: <strong>'.$mw.'px</strong> <em>x</em> Height: <strong>'.$mh.'px</strong></dd>');
				}
				cout('<dt></dt>');
				cout('<dd class="err"></dd>');
				
				cout('<dt></dt>');
				cout('<dd><button class="btn btn-primary" type="submit">Upload</button></dd>');
			cout('</dl>');
		cout('</form>');
		$viewFrame = 1;
		if ($viewFrame==1) {
			cout('<iframe name="quick_iframe" id="quick_iframe" src="/'.adminFolder.'/_ajax/index.php" width="300" height="100" style="overflow:;border:1px red solid;" visibility="" scrolling="yes" frameborder="1"></iframe>');
		} else {
			cout('<iframe name="quick_iframe" id="quick_iframe" src="/'.adminFolder.'/_ajax/index.php" width="300" height="11" style="overflow:hidden;" visibility="hidden" scrolling="no" frameborder="0"></iframe>');
		}
	} else {
		cout('<div style="width:410px;height:410px;">
			<iframe name="frame_img" id="frame_img" src="/'.adminFolder.'/js/mini-upload-form/index.php?tipeform='.$tipeform.'&tipedata='.$tipedata.'&kd='.$kd.'&formodule='.$formodule.'"
			width="400" height="400" style="margin:5px 0;overflow:;border:1px #999 solid;" visibility="" scrolling="yes" frameborder="1"></iframe>
		</div>');
	}
	$act_ket = "File: Load Upload Form: [kddata: $kd][Module: $formodule][TipeForm: $tipeform][$TipeData: $tipedata]";
	updateUserActivities($act_ket);
}
if ($a=="do_upload") {
	$kirim = cekpost(cleanQuery($_REQUEST["kirim"]), "");
	$formodule = cekpost(cleanQuery($_REQUEST["formodule"]), "");
	$tipeform = cekpost(cleanQuery($_REQUEST["tipeform"]), "");
	$tipedata = cekpost(cleanQuery($_REQUEST["tipedata"]), "");
	#tipeform = 0=single file or 1=multiple files
	#tipedata = 0=image or 1=others
	if ($kirim=="1") {
		$kddata = cekpost(cleanQuery($_POST["kddata"]), "0");
		if ($formodule=="games") {
			$rs=array(); $q="SELECT * FROM games WHERE kdgame='$kddata'";
			callRS($rs, $q);
		}
		if ($formodule=="game_logo") {
			$rs=array(); $q="SELECT * FROM games WHERE kdgame='$kddata'";
			callRS($rs, $q);
		}
		if ($formodule=="news") {
			$rs=array(); $q="SELECT * FROM news WHERE kdnews='$kddata'";
			callRS($rs, $q);
		}
		if ($formodule=="promo") {
			$rs=array(); $q="SELECT * FROM s_promo WHERE kd='$kddata'";
			callRS($rs, $q);
		}
		$namafile = $_FILES["namafile"]["name"];
		$tmp = $_FILES["namafile"]["tmp_name"];
		$size = $_FILES["namafile"]["size"];
		$type = $_FILES["namafile"]["type"];
		$error = $_FILES["namafile"]["error"];
		if ($tipedata=="0") {
			$allowedTypes = array("image/png", "image/bmp", "image/jpeg", "image/jpg", "image/gif");
			$ext = ShowFileExtension($namafile);
			$serr = array(
				1=>"Uploaded file type (.".$ext.") are not allowed!",
				2=>"File Name are unidentified or no file are selected!",
			);
		}
		$err = array();
		if (!in_array($type, $allowedTypes)) { $err[] = 1; }
		if ($namafile=="" && !isset($namafile)) { $err[] = 2; }
		$mainDir = "../../files/";
		if (file_exists($mainDir)) {
			$createdDir = $mainDir;
		} else {
			$createdDir = createDirectory2($mainDir);
		}
		if ($createdDir[strlen($createdDir)-1]!="/") $createdDir=$createdDir."/";
		$createdDir = str_replace("//", "/", $createdDir);
		$now = time();
		$newName = $now.".".$ext;
		$path = $createdDir;
		$nnfile = $namafile;
		$flag = 0;
		do {
			$rscek = array(); $qcek = "SELECT original_file_name FROM u_files WHERE original_file_name = '$nnfile'";
			callRS($rscek, $qcek);
			if (count($rscek)!=0) {
				$flag = 0;
				#$nnfile = "_".$nnfile;
				$nnfile = "$kddata-".$nnfile;
			} else {
				$flag = 1;
			}
		} while ($flag==0);
		$fullOriName = $path.$nnfile;
		move_uploaded_file($tmp, $fullOriName);
		$fullNewName = $path.$newName;
		smartCopy($fullOriName, $fullNewName);
		if ($tipedata=="0") {
			if ($formodule=="game_logo") {
				$maxWidth = 220; $maxHeight = 100;
			}
			if ($formodule=="games") {
				$maxWidth = 1080; $maxHeight = 800;
			}
			if ($formodule=="news") {
				$maxWidth = 600; $maxHeight = 350;
			}
			if ($formodule=="promo") {
				$maxWidth = 800; $maxHeight = 300;
			}
			$tmpFileName = $path.$now."_$maxWidth.".$ext;
			smartCopy($fullNewName, $tmpFileName);
			smart_resize_image($tmpFileName, $maxWidth, $maxHeight, true, 'file', true, false);
			$image_title = cekpost(cleanQuery($_POST["image_title"]), "");
			$image_alt = cekpost(cleanQuery($_POST["image_alt"]), "");
		}
		$upby = $_SESSION["user"]["inisial"];
		$uptime = time();
		if ($formodule=="games") {
			$qi = "INSERT INTO u_files (kdgame, file_type, full_path, original_file_name, current_file_name, file_ext, image_alt, image_title, upby, uptime) 
				VALUES ('$kddata', '$type', '$fullNewName', '$nnfile', '$newName', '$ext', '$image_alt', '$image_title', '$upby', '$uptime')";
			execRS($qi);
			$act_ket = "Games: Image: [kd: $kddata][nama: ".$rs[0]["nama"]."][file: ".$nnfile."]";
		}
		if ($formodule=="game_logo") {
			$qi = "INSERT INTO u_files (kdgame_logo, file_type, full_path, original_file_name, current_file_name, file_ext, image_alt, image_title, upby, uptime) 
				VALUES ('$kddata', '$type', '$fullNewName', '$nnfile', '$newName', '$ext', '$image_alt', '$image_title', '$upby', '$uptime')";
			execRS($qi);
			$act_ket = "Games: Logo: [kd: $kddata][nama: ".$rs[0]["nama"]."][file: ".$nnfile."]";
		}
		if ($formodule=="news") {
			$qi = "INSERT INTO u_files (kdnews, file_type, full_path, original_file_name, current_file_name, file_ext, image_alt, image_title, upby, uptime) 
				VALUES ('$kddata', '$type', '$fullNewName', '$nnfile', '$newName', '$ext', '$image_alt', '$image_title', '$upby', '$uptime')";
			execRS($qi);
			$act_ket = "News: Image: [kd: $kddata][judul: ".$rs[0]["judul"]."][file: ".$nnfile."]";
		}
		if ($formodule=="promo") {
			$qi = "INSERT INTO u_files (kdpromo, file_type, full_path, original_file_name, current_file_name, file_ext, image_alt, image_title, upby, uptime) 
				VALUES ('$kddata', '$type', '$fullNewName', '$nnfile', '$newName', '$ext', '$image_alt', '$image_title', '$upby', '$uptime')";
			execRS($qi);
			$act_ket = "Promo: Image: [kd: $kddata][judul: ".$rs[0]["judul"]."][file: ".$nnfile."]";
		}
		updateUserActivities($act_ket);
		cout('<script type="text/javascript">parent.loadUploadedImage(\''.$formodule.'\', \''.$kddata.'\');</script>');
		exit;
	}
}
if ($a=="do_action") {
	$kddata = cekpost(cleanQuery($_GET["kddata"]), "0");
	$action = trim(cekpost(cleanQuery($_GET["action"]), ""));
	$kd = cekpost(cleanQuery($_GET["kd"]), "0");
	$formodule = cekpost(cleanQuery($_GET["formodule"]), "");
	if ($action=="update") {
		$image_title = cekpost(cleanQuery($_GET["image_title"]), "");
		$image_alt = cekpost(cleanQuery($_GET["image_alt"]), "");
		$image_title_id = cekpost(cleanQuery($_GET["image_title_id"]), "");
		$image_alt_id = cekpost(cleanQuery($_GET["image_alt_id"]), "");
		$image_title = $image_title!=""?$image_title:$image_title_id;
		$image_alt = $image_alt!=""?$image_alt:$image_alt_id;
		$qu="UPDATE u_files SET image_title='$image_title', image_alt='$image_alt' WHERE kdfile='$kd'";
		execRS($qu);
		$act_ket = "File: [Action: $action][kdfile: $kd][kddata: $kddata][Module: $formodule]";
		updateUserActivities($act_ket);
		if ($formodule=="slides") { $gotoThisURL = "/".adminFolder."/slides";cout('<script type="text/javascript">parent.window.location="'.$gotoThisURL.'/";</script>');exit; }
		cout('<script type="text/javascript">parent.loadUploadedImage(\''.$formodule.'\', \''.$kddata.'\');</script>');
	}
	if ($action=="delete") {
		$rsi=array(); $qi="SELECT * FROM u_files WHERE kdfile=$kd";
		callRS($rsi, $qi);
		unlink($rsi[0]["full_path"]);
		unlink("../../files/".$rsi[0]["original_file_name"]);
		if ($formodule=="games") {
			unlink(str_replace(".".$rsi[0]["file_ext"], "_300.".$rsi[0]["file_ext"], $rsi[0]["full_path"]));
		}
		if ($formodule=="slides") {
			unlink(str_replace(".".$rsi[0]["file_ext"], "_1000.".$rsi[0]["file_ext"], $rsi[0]["full_path"]));
		}
		$qd="DELETE FROM u_files WHERE kdfile=$kd";
		execRS($qd);
		$act_ket = "File: [Action: $action][kdfile: $kd][kddata: $kddata][Module: $formodule]";
		updateUserActivities($act_ket);
		if ($formodule=="games") {
			$rsi=array(); $qi="SELECT * FROM u_files WHERE kdgame='$kddata'";
			callRS($rsi, $qi);
		}
		if ($formodule=="game_logo") {
			$rsi=array(); $qi="SELECT * FROM u_files WHERE kdgame_logo='$kddata'";
			callRS($rsi, $qi);
		}
		if ($formodule=="news") {
			$rsi=array(); $qi="SELECT * FROM u_files WHERE kdnews='$kddata'";
			callRS($rsi, $qi);
		}
		if ($formodule=="promo") {
			$rsi=array(); $qi="SELECT * FROM u_files WHERE kdpromo='$kddata'";
			callRS($rsi, $qi);
		}
		if ($formodule=="slides") {
			$rsi=array(); $qi="SELECT * FROM u_files WHERE kdslide='$kddata'";
			callRS($rsi, $qi);
			$gotoThisURL = "/".adminFolder."/slides";
			cout('<script type="text/javascript">parent.window.location="'.$gotoThisURL.'/";</script>');
			exit;
		}
		if (count($rsi)!=0) {
			cout('<script type="text/javascript">parent.loadUploadedImage(\''.$formodule.'\', \''.$kddata.'\');</script>');
		} else {
			cout('<script type="text/javascript">parent.loadFormUploadImage(\''.$formodule.'\', \''.$kddata.'\', 0, 0);</script>');
		}
	}
}
if ($a=="loadfiles") {
	$kd = cekpost(cleanQuery($_GET["kd"]), "0");
	$formodule = trim(cekpost(cleanQuery($_GET["formodule"]), ""));
	if ($formodule=="games") {
		$rs=array(); $q="SELECT * FROM games WHERE kdgame='$kd'";
		callRS($rs, $q);
		$rsi=array(); $qi="SELECT * FROM u_files WHERE kdgame='$kd'";
		callRS($rsi, $qi);
	}
	if ($formodule=="game_logo") {
		$rs=array(); $q="SELECT * FROM games WHERE kdgame='$kd'";
		callRS($rs, $q);
		$rsi=array(); $qi="SELECT * FROM u_files WHERE kdgame_logo='$kd'";
		callRS($rsi, $qi);
	}
	if ($formodule=="news") {
		$rs=array(); $q="SELECT * FROM news WHERE kdnews='$kd'";
		callRS($rs, $q);
		$rsi=array(); $qi="SELECT * FROM u_files WHERE kdnews='$kd'";
		callRS($rsi, $qi);
	}
	if ($formodule=="promo") {
		$rs=array(); $q="SELECT * FROM s_promo WHERE kd='$kd'";
		callRS($rs, $q);
		$rsi=array(); $qi="SELECT * FROM u_files WHERE kdpromo='$kd'";
		callRS($rsi, $qi);
	}
	if (count($rsi)!=0) {
		cout('<div class="row">');
		for ($i=0;$i<count($rsi);$i++) {
			if ($formodule=="game_logo") {
				$path = str_replace("../../", "/", $rsi[$i]["full_path"]);
				$resized = str_replace(".".$rsi[$i]["ext"]."", "_220.".$rsi[$i]["ext"]."", $path);
				$resized = $resized."?".time();
				cout('<div class="col-sm-3">
					<img class="img-thumbnail" src="'.$resized.'" alt="'.$rsi[$i]["image_alt"].'" title="'.$rsi[$i]["image_title"].'" />
					<div><input class="form-control" type="text" id="image_title" placeholder="Image Title" title="Image Title" value="'.$rsi[$i]["image_title"].'" /></div>
					<div><input class="form-control" type="text" id="image_alt" placeholder="Image Alt" title="Image Alt" value="'.$rsi[$i]["image_alt"].'" /></div>
					<div class="text-right">
						<input class="btn btn-primary" type="button" value="Update" onclick="doAction(\'image\', \'update\', \''.$formodule.'\', \''.$kd.'\', \''.$rsi[$i]["kdfile"].'\');" />
						<input class="btn btn-danger" type="button" value="Delete" onclick="doAction(\'image\', \'delete\', \''.$formodule.'\', \''.$kd.'\', \''.$rsi[$i]["kdfile"].'\');" />
					</div>
				</div>');
			}
			if ($formodule=="games") {
				$path = str_replace("../../", "/", $rsi[$i]["full_path"]);
				$resized = str_replace(".".$rsi[$i]["ext"]."", "_1080.".$rsi[$i]["ext"]."", $path);
				$resized = $resized."?".time();
				cout('<div class="col-sm-3">
					<img class="img-thumbnail" src="'.$resized.'" alt="'.$rsi[$i]["image_alt"].'" title="'.$rsi[$i]["image_title"].'" />
					<div><input class="form-control" type="text" id="image_title" placeholder="Image Title" title="Image Title" value="'.$rsi[$i]["image_title"].'" /></div>
					<div><input class="form-control" type="text" id="image_alt" placeholder="Image Alt" title="Image Alt" value="'.$rsi[$i]["image_alt"].'" /></div>
					<div class="text-right">
						<input class="btn btn-primary" type="button" value="Update" onclick="doAction(\'image\', \'update\', \''.$formodule.'\', \''.$kd.'\', \''.$rsi[$i]["kdfile"].'\');" />
						<input class="btn btn-danger" type="button" value="Delete" onclick="doAction(\'image\', \'delete\', \''.$formodule.'\', \''.$kd.'\', \''.$rsi[$i]["kdfile"].'\');" />
					</div>
				</div>');
			}
			if ($formodule=="news") {
				$path = str_replace("../../", "/", $rsi[$i]["full_path"]);
				$resized = str_replace(".".$rsi[$i]["ext"]."", "_600.".$rsi[$i]["ext"]."", $path);
				$resized = $resized."?".time();
				cout('<div class="col-sm-3">
					<img class="img-thumbnail" src="'.$resized.'" alt="'.$rsi[$i]["image_alt"].'" title="'.$rsi[$i]["image_title"].'" />
					<div><input class="form-control" type="text" id="image_title" placeholder="Image Title" title="Image Title" value="'.$rsi[$i]["image_title"].'" /></div>
					<div><input class="form-control" type="text" id="image_alt" placeholder="Image Alt" title="Image Alt" value="'.$rsi[$i]["image_alt"].'" /></div>
					<div class="text-right">
						<input class="btn btn-primary" type="button" value="Update" onclick="doAction(\'image\', \'update\', \''.$formodule.'\', \''.$kd.'\', \''.$rsi[$i]["kdfile"].'\');" />
						<input class="btn btn-danger" type="button" value="Delete" onclick="doAction(\'image\', \'delete\', \''.$formodule.'\', \''.$kd.'\', \''.$rsi[$i]["kdfile"].'\');" />
					</div>
				</div>');
			}
			if ($formodule=="promo") {
				$path = str_replace("../../", "/", $rsi[$i]["full_path"]);
				$resized = str_replace(".".$rsi[$i]["ext"]."", "_800.".$rsi[$i]["ext"]."", $path);
				$resized = $resized."?".time();
				cout('<div class="col-sm-3">
					<img class="img-thumbnail" src="'.$resized.'" alt="'.$rsi[$i]["image_alt"].'" title="'.$rsi[$i]["image_title"].'" />
					<div><input class="form-control" type="text" id="image_title" placeholder="Image Title" title="Image Title" value="'.$rsi[$i]["image_title"].'" /></div>
					<div><input class="form-control" type="text" id="image_alt" placeholder="Image Alt" title="Image Alt" value="'.$rsi[$i]["image_alt"].'" /></div>
					<div class="text-right">
						<input class="btn btn-primary" type="button" value="Update" onclick="doAction(\'image\', \'update\', \''.$formodule.'\', \''.$kd.'\', \''.$rsi[$i]["kdfile"].'\');" />
						<input class="btn btn-danger" type="button" value="Delete" onclick="doAction(\'image\', \'delete\', \''.$formodule.'\', \''.$kd.'\', \''.$rsi[$i]["kdfile"].'\');" />
					</div>
				</div>');
			}
		}
		cout('</div>');
		$act_ket = "File: Load File: [kddata: $kd][Module: $formodule]";
		updateUserActivities($act_ket);
	} else {
		$act_ket = "File: Calling Form Upload...: [kddata: $kd][Module: $formodule]";
		updateUserActivities($act_ket);
		cout('<script type="text/javascript">parent.loadFormUploadImage(\''.$formodule.'\', \''.$kd.'\', 0, 0);</script>');
	}
}
?>