<?php
// INCLUDE HEADER IN PAGE
include('other_header.php');
// GET CURRENT DATE AND TIME
$date = date('Y-m-d H:i:s');
$api = $_POST['api'];
?>
  <div class="container">

    <!-- BASE CSS   =========================================================== -->

    <!-- Tables -->
<?php
if($_POST['submit']) {
	$url = $_POST['url'];
	$url = AntiXSS::setEncoding($url, "UTF-8");
	$url = str_replace('"','',$url);
	$url = str_replace("'","",$url);
	$url = str_replace(">","",$url);
	$url = str_replace("<","",$url);
	$url = str_replace("</","",$url);
	$url = str_replace("&%","",$url);
	$url = str_replace("script","",$url);
	$url = str_replace("(","",$url);
	$url = str_replace(")","",$url);
	$adtitle = $_POST['adtitle'];
	$adtitle = AntiXSS::setEncoding($adtitle, "UTF-8");
	$adtitle = str_replace('"','',$adtitle);
	$adtitle = str_replace("'","",$adtitle);
	$adtitle = str_replace(">","",$adtitle);
	$adtitle = str_replace("<","",$adtitle);
	$adtitle = str_replace("</","",$adtitle);
	$adtitle = str_replace("&%","",$adtitle);
	$adtitle = str_replace("script","",$adtitle);
	$adtitle = str_replace("(","",$adtitle);
	$adtitle = str_replace(")","",$adtitle);
	if(isset($_POST['password'])) {
		$passwordnew = md5($_POST['password']);
	}
	$passworddecoded = $_POST['password'];
	$passworddecoded = AntiXSS::setEncoding($passworddecoded, "UTF-8");
	$passworddecoded = str_replace('"','',$passworddecoded);
	$passworddecoded = str_replace("'","",$passworddecoded);
	$passworddecoded = str_replace(">","",$passworddecoded);
	$passworddecoded = str_replace("<","",$passworddecoded);
	$passworddecoded = str_replace("</","",$passworddecoded);
	$passworddecoded = str_replace("&%","",$passworddecoded);
	$passworddecoded = str_replace("script","",$passworddecoded);
	$passworddecoded = str_replace("(","",$passworddecoded);
	$passworddecoded = str_replace(")","",$passworddecoded);
	
	$adtype = $_POST['adtype'];
	if($adtype == 'interstitial') {
		$adverttype = 'interstitial';
	}elseif($adtype == 'banner') {
		$adverttype = 'banner';
	}elseif($adtype == 'video') {
		$adverttype = 'video';
	}elseif($adtype == 'none') {
		$adverttype = 'none';
	}else {
		$adverttype = 'interstitial';
	}

	$badurl = 0;
	$url = filter_var($url, FILTER_SANITIZE_URL);
	
	if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&#\/%?=~_|!:,.;]*[-a-z0-9+&#\/%=~_|]/i",$url)) {
  		$badurl = 1;
	}elseif (!preg_match("*[.]*",$url)) {
  		$badurl = 1;
	}elseif (!isset($url)) {
	   $badurl = 1;
	}elseif (!filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED) === true) {
		$badurl = 1;
	}
	if($badurl == 1){
		?>
		<div class="row">
      <div style="margin-bottom: 0px;" class="col-md-8 col-md-offset-2">
		<div style="margin-bottom: 0px;" class="well">
			<div class="row">
				<center>
				<form name="csrf_form" id="shrinkform" action="" method="POST">
					<input value="Try again" class="btn btn-default" style="width: 150px;" name="again" type="submit">
					<input type="hidden" name="api" value="<?php echo $api; ?>">
					<input type="hidden" name="csrf_token" value="<?php echo $token; ?>">
				</form>
				</center>
			</div>
        </div>
      </div>
    </div>
    <?php
	}elseif ($url){

	// THESE CHARACTERS ARE ALLOWED IN SHORTENED LINK ex. adp.ay/CHARACTER >> adp.ay/d6w8s9a4
  $charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  $len = 5;
  $numrows = 1;
  
  while ($numrows != 0){
  for ($i = 0; $i <= $len; $i++) {
      $rand = rand() % strlen($charset);
      $tmp = substr($charset,$rand, 1);
      $code .= $tmp;
  }
  
  $query = mysql_query("SELECT * FROM Links_All WHERE code='$code'");
  $numrows = mysql_num_rows($query);
  }

  $url = mysql_real_escape_string($url);
  $q1 = "SELECT `username` FROM `Accounts_Classic_Publishers` WHERE `username` = '$api'";
  $r1 = mysql_query($q1) or die($q1."<br/><br/>".mysql_error());
  $d1 = mysql_result($r1, 0);
  mysql_query("INSERT INTO Links_All VALUES ('', '$url', '$code','$d1','$adverttype','0','0.00','$adtitle','$password')");
  $qstats = mysql_query("UPDATE Web_Stats SET sclinks7=sclinks7 + '1' WHERE id='1'");
  $query = mysql_query("SELECT * FROM Links_All WHERE code='$code'");
  $numrows = mysql_num_rows($query);
	
  if($numrows == 1){

?>
	<div class="row">
      <div style="margin-bottom: 0px;" class="col-md-8 col-md-offset-2">
		<div style="margin-bottom: 0px;" class="well">
			<div class="row">
				<center>
				<form name="csrf_form" id="shrinkform" action="" method="POST">
							<div data-original-title="Enter your long URL" data-toggle="tooltip" data-placement="top" title="" class="input-group">
								<center>
								<span class="input-group-btn">
									<input value="Another" class="btn btn-default" style="width: 150px;" name="again" type="submit">
									<input value="Open" onclick="window.open('<?php echo $mainurl.$code; ?>')" class="btn btn-default" style="width: 150px;" type="button">
								</span>
								</center>
							</div>
							<div data-original-title="Enter your long URL" data-toggle="tooltip" data-placement="top" title="555" class="input-group">
								<center>
									<input style="width:50%" class="form-control" placeholder="Title" value="<?php echo $adtitle; ?>" name="adtitle" type="text">
									<input style="width:50%" class="form-control" placeholder="Password" value="<?php echo $passworddecoded; ?>" name="password" type="text">
									<?php if($sa['interstitialactive'] == '1') {
									?>
									<input style="width: auto" id="interstitial" class="toggle toggle-left" name="adtype" value="interstitial" type="radio" <?php if($adverttype == 'interstitial'){echo "checked";}?>><label style="width: auto" class="btn btn-default" for="interstitial" class="btn">Interstitial</label>
									<?php
									}
									?>
									<?php if($sa['banneractive'] == '1') {
									?>
									<input style="width: auto" id="banner" class="toggle toggle-right" name="adtype" value="banner" type="radio" <?php if($adverttype == 'banner'){echo "checked";}?>><label style="width: auto" class="btn btn-default" for="banner" class="btn">Banner</label>
									<?php
									}
									?>
									<?php if($sa['videoactive'] == '1') {
									?>
									<input style="width: auto" id="video" class="toggle toggle-right" name="adtype" value="video" type="radio" <?php if($adverttype == 'video'){echo "checked";}?>><label style="width: auto" class="btn btn-default" for="video" class="btn">Video</label>
									<?php
									}
									?>
									<input style="width: auto" id="none" class="toggle toggle-right" name="adtype" value="none" type="radio" <?php if($adverttype == 'none'){echo "checked";}?>><label style="width: auto" class="btn btn-default" for="none" class="btn">None</label>
								</center>					
							</div>			
					</div>
					<input type="hidden" name="csrf_token" value="<?php echo $token; ?>">
				</form>
				</center>
			</div>
        </div>
      </div>
    </div>	
<?php
  }
  else{
  echo"Link was't imported into our database. Please wait a minute and try again.";
  }     
 }
 else"<script type='text/javascript'>window.location='f.home'</script>";
}
else
{
?>
	<div class="row">
      <div style="margin-bottom: 0px;" class="col-md-8 col-md-offset-2">
		<div style="margin-bottom: 0px;" class="well">
			<div class="row">
				<center>
				<form name="csrf_form" id="shrinkform" action="" method="POST">
					<div data-original-title="Enter your long URL" data-toggle="tooltip" data-placement="top" title="" class="input-group">
							<input class="form-control" placeholder="http://" name="url" type="text">
							<span class="input-group-btn">
								<input value="<?php echo $a012; ?>" class="btn btn-default" style="width: 150px;" name="submit" type="submit">
							</span>				
					</div>
					<div data-original-title="Enter your long URL" data-toggle="tooltip" data-placement="top" title="555" class="input-group">
							<center>
							<input style="width:50%" class="form-control" placeholder="Title" name="adtitle" type="text">
							<input style="width:50%" class="form-control" placeholder="Password" name="password" type="text">
							<?php if($sa['interstitialactive'] == '1') {
									?>
									<input style="width: auto" id="interstitial" class="toggle toggle-left" name="adtype" value="interstitial" type="radio" <?php if($adverttype == 'interstitial'){echo "checked";}?>><label style="width: auto" class="btn btn-default" for="interstitial" class="btn">Interstitial</label>
									<?php
									}
									?>
									<?php if($sa['banneractive'] == '1') {
									?>
									<input style="width: auto" id="banner" class="toggle toggle-right" name="adtype" value="banner" type="radio" <?php if($adverttype == 'banner'){echo "checked";}?>><label style="width: auto" class="btn btn-default" for="banner" class="btn">Banner</label>
									<?php
									}
									?>
									<?php if($sa['videoactive'] == '1') {
									?>
									<input style="width: auto" id="video" class="toggle toggle-right" name="adtype" value="video" type="radio" <?php if($adverttype == 'video'){echo "checked";}?>><label style="width: auto" class="btn btn-default" for="video" class="btn">Video</label>
									<?php
									}
									?>
							<input style="width: auto" id="none" class="toggle toggle-right" name="adtype" value="none" type="radio"><label style="width: auto" class="btn btn-default" for="none" class="btn">None</label>
							</center>					
					</div>
					<input type="hidden" name="csrf_token" value="<?php echo $token; ?>">
				</form>
				</center>
			</div>
        </div>
      </div>
    </div>	
<?php
}
?>
</div>

<?php
include('other_footer.php');
?>
