<?php
// INCLUDE HEADER IN PAGE
include('other_header.php');
// GET CURRENT DATE AND TIME
$date = date('Y-m-d H:i:s');
if(isset($_POST['success'])) {
	if($_POST['success'] == 'loggedin') {
	?>
	<div style="margin-top: -80px;" class="alert alert-success" role="alert">
		<strong>Well done!</strong> Successfully logged in.
	</div>
	<?php
	}
}
if(isset($_POST['editrequestcomplete'])) {
	$linkcode = $_POST['requestvalue'];
	$newtitle = $_POST['adtitle'];
	$newtitle = AntiXSS::setEncoding($newtitle, "UTF-8");
	$newtitle = str_replace('"','',$newtitle);
	$newtitle = str_replace("'","",$newtitle);
	$newtitle = str_replace(">","",$newtitle);
	$newtitle = str_replace("<","",$newtitle);
	$newtitle = str_replace("</","",$newtitle);
	$newtitle = str_replace("&%","",$newtitle);
	$newtitle = str_replace("script","",$newtitle);
	$newtitle = str_replace("(","",$newtitle);
	$newtitle = str_replace(")","",$newtitle);
	$urlredir = $_POST['url'];
	$urlredir = AntiXSS::setEncoding($urlredir, "UTF-8");
	$urlredir = str_replace('"','',$urlredir);
	$urlredir = str_replace("'","",$urlredir);
	$urlredir = str_replace(">","",$urlredir);
	$urlredir = str_replace("<","",$urlredir);
	$urlredir = str_replace("</","",$urlredir);
	$urlredir = str_replace("&%","",$urlredir);
	$urlredir = str_replace("script","",$urlredir);
	$urlredir = str_replace("(","",$urlredir);
	$urlredir = str_replace(")","",$urlredir);
	$badurl = 0;	
	if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&#\/%?=~_|!:,.;]*[-a-z0-9+&#\/%=~_|]/i",$urlredir)) {
  		$badurl = 1;
	}elseif (!preg_match("*[.]*",$urlredir)) {
  		$badurl = 1;
	}elseif (!isset($urlredir)) {
	   $badurl = 1;
	}else if (!filter_var($urlredir, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED) === true) {
		$badurl = 1;
	}
	$advtype = $_POST['adtype'];
	$advtype = AntiXSS::setEncoding($advtype, "UTF-8");
	$advtype = str_replace('"','',$advtype);
	$advtype = str_replace("'","",$advtype);
	$advtype = str_replace(">","",$advtype);
	$advtype = str_replace("<","",$advtype);
	$advtype = str_replace("</","",$advtype);
	$advtype = str_replace("&%","",$advtype);
	$advtype = str_replace("script","",$advtype);
	$advtype = str_replace("(","",$advtype);
	$advtype = str_replace(")","",$advtype);
	if($advtype == 'interstitial') {
		$advtypeedit = 'interstitial';
	}elseif($advtype == 'video') {
		$advtypeedit = 'video';
	}elseif($advtype == 'banner') {
		$advtypeedit = 'banner';
	}else {
		$advtypeedit = 'interstitial';
	}
	if($badurl == 0){
		if(isset($_POST['password'])) {
			$passwordnew = md5($_POST['password']);
			$sqledit = mysql_query("UPDATE Links_All SET advtype='$advtypeedit', url='$urlredir', title='$newtitle', password='$passwordnew' WHERE code='$linkcode' AND username='$session_username'");
		}else {
			$sqledit = mysql_query("UPDATE Links_All SET advtype='$advtypeedit', url='$urlredir', title='$newtitle' WHERE code='$linkcode' AND username='$session_username'");
		}
		?>
		<div style="margin-top: -80px;" class="alert alert-success" role="alert">
			<strong>Well done!</strong> Link successfully edited.
		</div>
		<?php	
	}else {
		?>
		<div style="margin-top: -80px;" class="alert alert-danger" role="alert">
			<strong>Sorry!</strong> Please enter URL in right format, ex: http://google.com
		</div>
		<?php	
	}
}
if(isset($_POST['deleterequest'])) {
	$linkcode = $_POST['requestvalue'];
	$delete = mysql_query("DELETE FROM Links_All WHERE code='$linkcode' AND username='$session_username'");
	?>
	<div style="margin-top: -80px;" class="alert alert-success" role="alert">
		<strong>Well done!</strong> Link successfully deleted.
	</div>
	<?php
}
if($_POST['submit']) {
$url = $_POST['url'];
	$badurl = 0;
	// Remove all illegal characters from a url
	$url = filter_var($url, FILTER_SANITIZE_URL);
	
	if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&#\/%?=~_|!:,.;]*[-a-z0-9+&#\/%=~_|]/i",$url)) {
  		$badurl = 1;
  		?>
  		<div style="margin-top: -80px;" class="alert alert-danger" role="alert">
			<strong>Sorry!</strong> Please enter URL in right format, ex: http://google.com
		</div>
		<?php
	}elseif (!preg_match("*[.]*",$url)) {
  		$badurl = 1;
  		?>
  		<div style="margin-top: -80px;" class="alert alert-danger" role="alert">
			<strong>Sorry!</strong> Please enter URL in right format, ex: http://google.com
		</div>
		<?php
	}elseif (!isset($url)) {
	   $badurl = 1;
	   ?>
  		<div style="margin-top: -80px;" class="alert alert-danger" role="alert">
			<strong>Sorry!</strong> Please enter URL in right format, ex: http://google.com
		</div>
		<?php
	}elseif (!filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED) === true) {
		$badurl = 1;
		?>
  		<div style="margin-top: -80px;" class="alert alert-danger" role="alert">
			<strong>Sorry!</strong> Please enter URL in right format, ex: http://google.com
		</div>
		<?php
	}else {
		?>
  		<div style="margin-top: -80px;" class="alert alert-success" role="alert">
			<strong>Success!</strong> Link successfully generated.
		</div>
		<?php
	}
}
?>
  <div class="container">
    <div class="text-center">
      <h1><?php echo $title; ?>
      	<small>/
      			<?php
						$accounttypenow = $_SESSION['acctype'];
						if($accounttypenow == 0){//Publisher
					?>
							Active is: Publisher Account
					<?php
						}
						if($accounttypenow == 1){//Advertiser
					?>
							Active is: Advertiser Account
					<?php
						}
					?>
			</small>
		</h1>
 	 </div>
 	 </br>
 	 <?php 
 	 	if(isset($_POST['editrequest'])) {
		$linkcode = $_POST['requestvalue'];
		$link1 = mysql_query("SELECT * FROM Links_All WHERE code='$linkcode' AND username='$session_username'") or die('1');
		$link2 = mysql_fetch_array($link1);
	?>
		<div class="row">
      <div class="col-md-8 col-md-offset-2">
		<div 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="555" class="input-group">
							<input class="form-control" value="<?php echo $link2['url']?>" placeholder="http://" name="url" type="text">
							<input type='hidden' name='requestvalue' value='<?php echo $linkcode; ?>'>
							<span class="input-group-btn">
								<input value="Edit" class="btn btn-default" style="width: 150px;" name="editrequestcomplete" 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" value="<?php echo $link2['title']?>" placeholder="Title" name="adtitle" type="text">
							<input style="width:50%" class="form-control" placeholder="******" name="password" type="text">
							<?php if($sa['interstitialactive'] == '1') {
							?>
							<input id="interstitial" class="toggle toggle-left" name="adtype" value="interstitial" type="radio" <?php if($link2['advtype'] == 'interstitial'){echo 'checked';}?>><label class="btn btn-default" for="interstitial" class="btn">Interstitial</label>
							<?php
							}
							?>
							<?php if($sa['banneractive'] == '1') {
							?>
							<input id="banner" class="toggle toggle-right" name="adtype" value="banner" type="radio" <?php if($link2['advtype'] == 'banner'){echo 'checked';}?>><label class="btn btn-default" for="banner" class="btn">Banner</label>
							<?php
							}
							?>
							<?php if($sa['videoactive'] == '1') {
							?>
							<input id="video" class="toggle toggle-right" name="adtype" value="video" type="radio" <?php if($link2['advtype'] == 'video'){echo 'checked';}?>><label class="btn btn-default" for="video" class="btn">Video</label>
							<?php
							}
							?>
							<input id="none" class="toggle toggle-right" name="adtype" value="none" type="radio" <?php if($link2['advtype'] == 'none'){echo 'checked';}?>><label class="btn btn-default" for="none" class="btn">None</label>
							</center>					
					</div>
					<input type="hidden" name="csrf_token" value="<?php echo $token; ?>">
				</form>
				</br>
				</center>
				<center>
					<h4><?php echo $a016; ?></h4>
				</center>
			</div>
        </div>
      </div>
    </div>
	<?php
	}else if($_POST['submit']) {
$url = $_POST['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']);
}
$adtype = $_POST['adtype'];
if($adtype == 'interstitial') {
	$adverttype = 'interstitial';
}elseif($adtype == 'banner') {
	$adverttype = 'banner';
}elseif($adtype == 'video') {
	$adverttype = 'video';
}elseif($adtype == 'none') {
	$adverttype = 'none';
}else {
	$badurl = 1;
}
	
	if($badurl == 1){
		?>
		<div class="row">
      <div class="col-md-8 col-md-offset-2">
		<div class="well">
			<div class="row">
				<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>
					<input type="hidden" name="csrf_token" value="<?php echo $token; ?>">
				</form>
				</br>
				<div class="col-md-10 col-md-offset-2">
					<h4>Please enter URL in right format, ex: http://google.com</h4>
				</div>
			</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);
  mysql_query("INSERT INTO Links_All VALUES ('', '$url', '$code','$session_username','$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 class="col-md-8 col-md-offset-2">
		<div class="well">
			<div class="row">
				<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 readonly="readonly" onmouseover="this.select()" style="cursor:copy; opacity: 1;text-align: center;" class="form-control" value='<?php echo"$mainurl$code"; ?>' name="url" type="text">
							<span class="input-group-btn">
								<input value="Another" class="btn btn-default" style="width: 150px;" name="createanother" type="submit">
							</span>				
					</div>
					<center>
					<div style="top: -5px;" class="fb-share-button" data-href="<?php echo"$mainurl$code"; ?>" data-layout="button"></div>
					<a href="https://twitter.com/share" class="twitter-share-button" data-url="<?php echo"$mainurl$code"; ?>" data-count="none">Tweet</a> <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
					<div class="g-plus" data-action="share" data-annotation="none" data-href="<?php echo"$mainurl$code"; ?>"></div>
					<script src="https://apis.google.com/js/platform.js" async defer>
  						{lang: 'en-GB'}
					</script>		
					<center>
					<input type="hidden" name="csrf_token" value="<?php echo $token; ?>">
				</form>
				</br>
				<div class="col-md-10 col-md-offset-2">
					<h4><?php echo $a021; ?> <?php echo $title; ?></h4>
				</div>
			</div>
        </div>
      </div>
    </div>	

<?php
  }
  else{
  echo"<div class='jbar' style='bottom: 1%; right: 1%; opacity: 1;' align='center'><div class='content error' align='center'>$a238</div></div>";
  }     
 }
 else"<script type='text/javascript'>window.location='./home'</script>";
}
else
{
?>
	<div class="row">
      <div class="col-md-8 col-md-offset-2">
		<div 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="555" 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 id="interstitial" class="toggle toggle-left" name="adtype" value="interstitial" type="radio" checked><label class="btn btn-default" for="interstitial" class="btn">Interstitial</label>
							<?php
							}
							?>
							<?php if($sa['banneractive'] == '1') {
							?>
							<input id="banner" class="toggle toggle-right" name="adtype" value="banner" type="radio"><label class="btn btn-default" for="banner" class="btn">Banner</label>
							<?php
							}
							?>
							<?php if($sa['videoactive'] == '1') {
							?>
							<input id="video" class="toggle toggle-right" name="adtype" value="video" type="radio"><label class="btn btn-default" for="video" class="btn">Video</label>
							<?php
							}
							?>
							<input id="none" class="toggle toggle-right" name="adtype" value="none" type="radio"><label class="btn btn-default" for="none" class="btn">None</label>
							</center>					
					</div>
					<input type="hidden" name="csrf_token" value="<?php echo $token; ?>">
				</form>
				</br>
				</center>
				<center>
					<h4><?php echo $a016; ?></h4>
				</center>
			</div>
        </div>
      </div>
    </div>
<?php
}
?>
	<div class="text-center">
      <h1><small>Publisher's links</small></h1>
 	</div>
		<div class="col-sm-12">
          <table class="table table-bordered" style="width:100%;max-width:100%"  data-effect="fade">
										<thead>
											<tr>
												<th style='color: #DF691A; background-color: #2B3E50;border-collapse: collapse;border: 1px solid #DF691A;'>ID</th>
												<th style='max-width:140px;color: #DF691A; background-color: #2B3E50;border-collapse: collapse;border: 1px solid #DF691A;'><?php echo $a004; ?></th>
												<th style='color: #DF691A; background-color: #2B3E50;border-collapse: collapse;border: 1px solid #DF691A;'><?php echo $a132; ?></th>
												<th style='color: #DF691A; background-color: #2B3E50;border-collapse: collapse;border: 1px solid #DF691A;'><?php echo $a133; ?></th>
												<th style='color: #DF691A; background-color: #2B3E50;border-collapse: collapse;border: 1px solid #DF691A;'><?php echo $a025; ?></th>
												<th style='color: #DF691A; background-color: #2B3E50;border-collapse: collapse;border: 1px solid #DF691A;'><?php echo $a020; ?></th>
												<th style='color: #DF691A; background-color: #2B3E50;border-collapse: collapse;border: 1px solid #DF691A;'><?php echo $a135; ?> <i class="fa fa-clipboard"></i></th>
												<th style='color: #DF691A; background-color: #2B3E50;border-collapse: collapse;border: 1px solid #DF691A;'><?php echo $a061; ?></th>
											</tr>
										</thead>
											<?php
			$query = " SELECT * FROM Links_All WHERE username='$session_username' ORDER BY id ASC";
			$result = mysql_query($query) or die('Error, query failed');

			// print the random numbers
			
			if(mysql_num_rows($result)<1){
   echo "
						<tr>
							<td>
								{$row['code']}
							</td>
							<td>
							</td>
							<td>
							</td>
							<td>
							</td>
							<td>
							</td>
							<td>
							</td>
						</tr>
   ";
   }else{
	$numberoflink = 0;
     while($row = mysql_fetch_array($result))
				{
					$numberoflink = $numberoflink + 1;
					if (empty($row['title'])) {
									$row['title'] = "---";
					};
					$urloflink = "$mainurl{$row['code']}";
					if ($row['advtype'] == "interstitial"){
						$adtypenow = $a022;
					}else if ($row['advtype'] == "video"){
						$adtypenow = $a023;
					}else if ($row['advtype'] == "banner"){
						$adtypenow = $a062;
					}else if ($row['advtype'] == "none"){
						$adtypenow = $a024;
					}else $adtypenow = "Error";
					echo "
										<tr>
											<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
												<h5>$numberoflink</h5>
											</td>
											<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
												<h5><a href='editlink.{$row['code']}'> {$row['title']}</a></h5>
											</td>
											<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
												<input style='cursor:copy; opacity: 1' class='form-control autosize' rows='1' value='" . $row['url'] . "' onmouseover='this.select()' readonly>
											</td>
											<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
												<a target='_blank' href='$mainurl{$row['code']}'>{$row['code']}</a>
											<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
												<strong>$adtypenow</strong>
											</td>
											<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
												<span style='padding: 10px;' class='label label-success'>{$acu['ISO']} {$row['mearned']}</span>
											</td>
											<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
												<input style='cursor:copy; opacity: 1' class='form-control autosize' rows='1' value='" . $urloflink . "' onmouseover='this.select()' readonly>
											</td>
											<form name='actions' action='' method='POST'>
											<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
												<input type='hidden' name='requestvalue' value='{$row['code']}'>
												<input value='$a090' class='btn btn-default' name='editrequest' type='submit'>
												<input value='$a111' class='btn btn-default' name='deleterequest' type='submit'>
											</td>
											</form>
										</tr>";

				}

       }
	?>
									</table>
        </div>
</div>

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