<?php
session_start();
include("../include/config.php");
include("../include/functions.php");
include("../include/simpleimage.php");
validate_admin();

function isValidImage($tmpPath) {
    if (!file_exists($tmpPath)) return false;

    $allowedMime = ['image/jpeg', 'image/png', 'image/webp'];

    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    $mime  = finfo_file($finfo, $tmpPath);
    finfo_close($finfo);

    if (!in_array($mime, $allowedMime)) return false;
    if (getimagesize($tmpPath) === false) return false;

    // Block embedded scripts
    $head = file_get_contents($tmpPath, false, null, 0, 512);
    if (preg_match('/<\?php|<script|<html|<!DOCTYPE/i', $head)) return false;

    return true;
}
require_once __DIR__ . '/../include/S3Helper.php';
$s3 = new S3Helper();

if ($_REQUEST['submitForm'] == 'yes') {
	$name = mysqli_real_escape_string($GLOBALS['conn'], $_REQUEST['name']);
	$url = mysqli_real_escape_string($GLOBALS['conn'], $_REQUEST['url']);
	$follow = mysqli_real_escape_string($GLOBALS['conn'], $_REQUEST['follow']);

	if ($_FILES['photo']['size'] > 0 && $_FILES['photo']['error'] === 0 && isValidImage($_FILES['photo']['tmp_name'])) {
		$photo = 'feature' . time() . substr($_FILES['photo']['name'], -5);

        $photoUpload = $s3->upload($_FILES['photo']['tmp_name'], 'images/press/',$photo);
		if (!$photoUpload) {
			die("Something went wrong, unable to upload featured image, contact to site administrator."); // do whatever it is that you want to do when you are unable to upload the file.

		}

		//ftp start
		// echo SITE_URL;
		// echo MAINSITE_URL;
		// if (SITE_URL == MAINSITE_URL) {
		// 	//$conn_id = ftp_connect("quicklly.com") or die("Error in FTP connection");  // the FTP server you want to connect to.
		// 	$conn_id = ftp_connect("3.133.214.148") or die("Error in FTP connection");
		// 	$login_status = ftp_login($conn_id, "mainuser@quicklly.com", "n3At8hv3LZKCiF6") or die("Error in FTP login");  // Login to the FTP server.

		// 	//to upload from local
		// 	ftp_pasv($conn_id, true);
		// 	//to create dir
		// 	//ftp_mkdir($conn_id, "imageststs");
		// 	$target_file = $_FILES['photo']['tmp_name'];
		// 	$destination_file = "images/press/" . $photo;  // since you want to upload to a directory called "images".
		// 	$upload_status = ftp_put($conn_id, $destination_file, $target_file, FTP_BINARY);

		// 	if (!$upload_status) {
		// 		die("Something went wrong, unable to upload featured image, contact to site administrator."); // do whatever it is that you want to do when you are unable to upload the file.
		// 	}

		// 	// Close the FTP connection after you are done.
		// 	ftp_close($conn_id);
		// } else {
		// 	die("FTP connection failed2!");
		// }
		//ftp ends

		//move_uploaded_file($_FILES['photo']['tmp_name'], "../images/press/" . $photo);
	}
	if ($_FILES['brand']['size'] > 0 && $_FILES['brand']['error'] === 0 && isValidImage($_FILES['brand']['tmp_name'])) {

		$brand = 'brand' . time() . substr($_FILES['brand']['name'], -5);
		$photoBrand = $s3->upload($_FILES['brand']['tmp_name'], 'images/press/',$brand);
		if (!$photoBrand) {
			die("Something went wrong, unable to upload featured image, contact to site administrator."); // do whatever it is that you want to do when you are unable to upload the file.

		}

		// //ftp start
		// if (SITE_URL == MAINSITE_URL) {
		// 	//$conn_id = ftp_connect("quicklly.com") or die("Error in FTP connection 2");  // the FTP server you want to connect to.
		// 	$conn_id = ftp_connect("3.133.214.148") or die("Error in FTP connection");
		// 	$login_status = ftp_login($conn_id, "mainuser@quicklly.com", "n3At8hv3LZKCiF6") or die("Error in FTP login");  // Login to the FTP server.

		// 	//to upload from local
		// 	ftp_pasv($conn_id, true);
		// 	//to create dir
		// 	//ftp_mkdir($conn_id, "imageststs");
		// 	$target_file = $_FILES['brand']['tmp_name'];
		// 	$destination_file = "images/press/" . $brand;  // since you want to upload to a directory called "images".

		// 	$upload_status = ftp_put($conn_id, $destination_file, $target_file, FTP_BINARY);

		// 	if (!$upload_status) {
		// 		echo "Something went wrong, Unable to upload brand image, contact to site administrator."; // do whatever it is that you want to do when you are unable to upload the file.
		// 	}

		// 	// Close the FTP connection after you are done.
		// 	ftp_close($conn_id);
		// } else {
		// 	die("FTP connection failed3!");
		// }
		// //ftp ends

		// move_uploaded_file($_FILES['brand']['tmp_name'], "../images/press/" . $brand);
	}


	if ($_REQUEST['id'] == '') {
		$obj->query("insert into $tbl_press set name='$name', url='$url',follow='$follow',image='$photo' ,brand='$brand'");
		$_SESSION['sess_msg'] = 'Press Release added successfully';
	} else {
		$sql = " update $tbl_press set name='$name',url='$url',follow='$follow'";

		if ($photo) {
			$imageArr = $obj->query("select image from $tbl_press where id='" . $_REQUEST['id'] . "' ");
			$resultImage = $obj->fetchNextObject($imageArr);
			@unlink("../images/press/" . $resultImage->image);
			$sql .= " ,image='$photo' ";
		}
		if ($brand) {
			$imageArr = $obj->query("select brand from $tbl_press where id='" . $_REQUEST['id'] . "' ");
			$resultImage = $obj->fetchNextObject($imageArr);
			@unlink("../images/press/" . $resultImage->brand);
			$sql .= " ,brand='$brand' ";
		}
		$sql .= " where id='" . $_REQUEST['id'] . "'";
		$obj->query($sql);
		$_SESSION['sess_msg'] = 'Press Release updated successfully';
	}
	header("location:press-list.php");
	exit();
}


if ($_REQUEST['id'] != '') {
	$sql = $obj->query("select * from $tbl_press where id=" . $_REQUEST['id']);
	$result = $obj->fetchNextObject($sql);
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
	<title><?php echo SITE_TITLE; ?></title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<link href="css/admin.css" rel="stylesheet" type="text/css" />
	<script type="text/javascript" language="javascript">
		function validate(obj) {
			if (obj.name.value == '') {
				alert("Please enter Heading");
				obj.name.focus();
				return false;
			}
			if (obj.url.value == '') {
				alert("Please enter url");
				obj.url.focus();
				return false;
			}


		}
	</script>
	<script type="text/javascript" src="../include/ckeditor/ckeditor.js"></script>
</head>

<body>
	<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
		<?php include("header.php") ?>

		<tr>
			<td align="right" class="paddRtLt70" valign="top">
				<table width="99%" border="0" cellspacing="0" cellpadding="0">
					<tr>

						<td align="right" valign="top">
							<table width="100%" border="0" cellspacing="0" cellpadding="0">
								<tr>
									<td align="left" valign="middle" class="headingbg bodr text14">
										<em><img src="images/arrow2.gif" width="21" height="21" hspace="10" align="absmiddle" /></em>Admin:<?php if ($_REQUEST['id'] == '') { ?> Add<?php } else { ?>Update<?php } ?> Press Release
										<span style="float:right; padding-right:10px;">
											<input type="button" name="add" value="View  Press Release" class="button" onclick="location.href='press-list.php'" /></span>
									</td>
								</tr>

								<tr>
									<td height="100" align="left" valign="top" bgcolor="#f3f4f6" class="bodr">
										<form name="frm" method="POST" enctype="multipart/form-data" action="" onsubmit="return validate(this)">
											<input type="hidden" name="submitForm" value="yes" />
											<input type="hidden" name="id" value="<?php echo $_REQUEST['id']; ?>" />
											<table width="100%" cellpadding="0" cellspacing="0">
												<tr>
													<td align="center" colspan="2" class="paddRt14 paddBot11">
														<font color="#FF0000"><strong><?php echo $_SESSION['sess_msg'];
																						$_SESSION['sess_msg'] = ''; ?></strong></font>
													</td>
												</tr>

												<tr>
													<td align="right" class="paddBot11 paddRt14"><strong> Heading:</strong></td>
													<td align="left" class="paddBot11">
														<input type="text" name="name" id="name" size="150" value="<?php echo stripslashes($result->name); ?>">
												</tr>
												<tr>
													<td align="right" class="paddBot11 paddRt14"><strong> Press URL:</strong></td>
													<td align="left" class="paddBot11">
														<input type="text" name="url" id="url" size="150" value="<?php echo stripslashes($result->url); ?>">
												</tr>

												<tr>
													<td align="right" class="paddBot11 paddRt14"><strong> Follow Link:</strong></td>
													<td align="left" class="paddBot11">
														<select name="follow" id="follow">
															<option value=""> Select Option</option>
															<option value="nofollow" <?php if ($result->follow == 'nofollow') { ?>selected<?php } ?>>No Follow</option>
															<option value="dofollow" <?php if ($result->follow == 'dofollow') { ?>selected<?php } ?>>Do Follow</option>
														</select>
												</tr>
												<tr>
													<td align="right" class="paddBot11 paddRt14"><strong>Featured Image:</strong></td>
													<td align="left" class="paddBot11"><input name="photo" size="150" type="file" /><br />
															<img src="<?php echo SITE_URL_CDN; ?>/images/press/<?php echo $result->image; ?>" width="100" height="100" />
													</td>
												</tr>

												<tr>
													<td align="right" class="paddBot11 paddRt14"><strong>Brand Image:</strong></td>
													<td align="left" class="paddBot11"><input name="brand" size="150" type="file" /><br />
															<img src="<?php echo SITE_URL_CDN; ?>/images/press/<?php echo $result->brand; ?>" width="100" height="100" />
													</td>
												</tr>
												<tr>
													<td align="right" class="paddRt14 paddBot11">&nbsp;</td>
													<td align="left" class="paddBot11">&nbsp;</td>
												</tr>
												<tr>
													<td width="18%" align="right" class="paddRt14 paddBot11">&nbsp;</td>
													<td width="82%" align="left" class="paddBot11">
														<input type="submit" name="submit" value="Submit" class="submit" border="0" /> &nbsp;&nbsp;
														<input name="Reset" type="reset" id="Reset" value="Reset" class="submit" border="0" />
													</td>
												</tr>
											</table>
										</form>
									</td>
								</tr>

							</table>
						</td>
					</tr>
				</table>
			</td>
		</tr>
		<?php include('footer.php'); ?>
	</table>
</body>

</html>