<?php

// Session Start
session_start();

// If User Not Login
if(!isset($_SESSION["username"]) || $_SESSION["username"] == '' ){

  // Go to login page
  header("Location: login.php");
  // Stop Code Here
  exit();

}

// if user loged in
else {
?>

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
<style media="screen">
  #Clouds{position:absolute;top:0;right:0;bottom:0;left:0;margin:auto;height:30%;overflow:hidden;-webkit-animation:FadeIn 3s ease-out;animation:FadeIn 3s ease-out;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none}@-webkit-keyframes FadeIn{from{opacity:0}to{opacity:1}}@keyframes FadeIn{from{opacity:0}to{opacity:1}}.Cloud{position:absolute;width:100%;background-repeat:no-repeat;background-size:auto 100%;height:70px;-webkit-animation-duration:120s;animation-duration:120s;-webkit-animation-iteration-count:infinite;animation-iteration-count:infinite;-webkit-animation-fill-mode:forwards;animation-fill-mode:forwards;-webkit-animation-timing-function:linear;animation-timing-function:linear;-webkit-animation-name:Float,FadeFloat;animation-name:Float,FadeFloat;z-index:1}.Cloud.Foreground{height:10%;min-height:20px;z-index:3}.Cloud.Background{height:9.09090909%;min-height:8px;-webkit-animation-duration:210s;animation-duration:210s}@-webkit-keyframes Float{from{-webkit-transform:translateX(100%) translateZ(0);transform:translateX(100%) translateZ(0)}to{-webkit-transform:translateX(-15%) translateZ(0);transform:translateX(-15%) translateZ(0)}}@keyframes Float{from{-webkit-transform:translateX(100%) translateZ(0);transform:translateX(100%) translateZ(0)}to{-webkit-transform:translateX(-15%) translateZ(0);transform:translateX(-15%) translateZ(0)}}@-webkit-keyframes FadeFloat{0%,100%{opacity:0}5%,90%{opacity:1}}@keyframes FadeFloat{0%,100%{opacity:0}5%,90%{opacity:1}}.Cloud:nth-child(10){-webkit-animation-delay:-184.61538462s;animation-delay:-184.61538462s;top:60%}.Cloud.Foreground:nth-child(10){-webkit-animation-duration:80s;animation-duration:80s;height:35%}.Cloud.Background:nth-child(10){-webkit-animation-duration:110s;animation-duration:110s;height:-3.40909091%}.Cloud:nth-child(9){-webkit-animation-delay:-166.15384615s;animation-delay:-166.15384615s;top:54%}.Cloud.Foreground:nth-child(9){-webkit-animation-duration:84s;animation-duration:84s;height:32.5%}.Cloud.Background:nth-child(9){-webkit-animation-duration:114s;animation-duration:114s;height:-2.15909091%}.Cloud:nth-child(8){-webkit-animation-delay:-147.69230769s;animation-delay:-147.69230769s;top:48%}.Cloud.Foreground:nth-child(8){-webkit-animation-duration:88s;animation-duration:88s;height:30%}.Cloud.Background:nth-child(8){-webkit-animation-duration:118s;animation-duration:118s;height:-.90909091%}.Cloud:nth-child(7){-webkit-animation-delay:-129.23076923s;animation-delay:-129.23076923s;top:42%}.Cloud.Foreground:nth-child(7){-webkit-animation-duration:92s;animation-duration:92s;height:27.5%}.Cloud.Background:nth-child(7){-webkit-animation-duration:122s;animation-duration:122s;height:.34090909%}.Cloud:nth-child(6){-webkit-animation-delay:-110.76923077s;animation-delay:-110.76923077s;top:36%}.Cloud.Foreground:nth-child(6){-webkit-animation-duration:96s;animation-duration:96s;height:25%}.Cloud.Background:nth-child(6){-webkit-animation-duration:126s;animation-duration:126s;height:1.59090909%}.Cloud:nth-child(5){-webkit-animation-delay:-92.30769231s;animation-delay:-92.30769231s;top:30%}.Cloud.Foreground:nth-child(5){-webkit-animation-duration:100s;animation-duration:100s;height:22.5%}.Cloud.Background:nth-child(5){-webkit-animation-duration:130s;animation-duration:130s;height:2.84090909%}.Cloud:nth-child(4){-webkit-animation-delay:-73.84615385s;animation-delay:-73.84615385s;top:24%}.Cloud.Foreground:nth-child(4){-webkit-animation-duration:104s;animation-duration:104s;height:20%}.Cloud.Background:nth-child(4){-webkit-animation-duration:134s;animation-duration:134s;height:4.09090909%}.Cloud:nth-child(3){-webkit-animation-delay:-55.38461538s;animation-delay:-55.38461538s;top:18%}.Cloud.Foreground:nth-child(3){-webkit-animation-duration:108s;animation-duration:108s;height:17.5%}.Cloud.Background:nth-child(3){-webkit-animation-duration:138s;animation-duration:138s;height:5.34090909%}.Cloud:nth-child(2){-webkit-animation-delay:-36.92307692s;animation-delay:-36.92307692s;top:12%}.Cloud.Foreground:nth-child(2){-webkit-animation-duration:112s;animation-duration:112s;height:15%}.Cloud.Background:nth-child(2){-webkit-animation-duration:142s;animation-duration:142s;height:6.59090909%}.Cloud:nth-child(1){-webkit-animation-delay:-18.46153846s;animation-delay:-18.46153846s;top:6%}.Cloud.Foreground:nth-child(1){-webkit-animation-duration:116s;animation-duration:116s;height:12.5%}.Cloud.Background:nth-child(1){-webkit-animation-duration:146s;animation-duration:146s;height:7.84090909%}.Cloud{background-image:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAKQAAABgCAYAAACTzNnjAAAFCklEQVR42u3d34uVRRjA8YMsEi0iSwhdRBGhSJgZiNRFIkWhQVEXBipKUVBBLCF6k0h4UxFkBLq4QT/Qiyi80EgxCjXMWqOMtqy0bBNja92yXatN3c3pGc9sHU/v+X3emWfe93vx+QN23u+e95x5Z94pmNWFQo5NEytErzgoBsSIOCf+FqbEBTEhzophcVTsFxvFEjE152PZHGMukbcB6BAPij0uqgtl0bXCBvyjeFPcS2wEWc0y0ec++Ywnf7rwbyc8grQ6RY8Y9RhhJT+Jde4TmghzFuR08bo4ryDEpE/N58QUYsx+kFPcD4yzCkMsZz+1uwkyu0HeKYYiCLHcN2I2QWYnSPudbHubfy37NuFu4wQZeZD2k2Uw4hDLHRZdBBnnH3K/5ykcX86IeQQZl6civ0XXYv/RlhJkHLZkOMTypz7dBKnbyzmJsfTZeTdB6vRszmIsjXI5QeryWMa/M9ZinzjdTJA63CLGcxzjpD/ElQQZfq3iKDFe8lSHIAPqI8L/6SHIMB4nvorTQQsI0v+teoz4KjpBkH7tIrqaNhCkH9cnbK5C8q/uywiSHzKabCLIdM3K+QR4o8YysfVWcZDvElnD1hNkOi5XuilLu5MlOyzni3vESnGfWCRuIMjm1zgSWPM7GWvNXdonXkdM8Q0dCwmytu8Iy/vK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.Background{background-image:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEoAAAAqCAYAAAAUJM0rAAACFUlEQVRo3u3aPSwDYRzH8UZEbAYiYrDYjLZGpIPBJLGwSKwi0k1iYBKDdFJsEiMxSZgsFZJGiGjqJWEhSKuaNKRUG8r5PfI8SXOud72+PH2eu2f47Ndv7+V5/nee/G6bh5MWmIV9iEMWvkGjfiADD7AHk9BU6+PSNK0kPAINwwXkC6KU6gvC4HVyKB/clhHHCDnbTqHbSaEaYIv+OK3KyBk274RQrXBXg0B6B/QPkTIUiZTiEIm5gkbZQjXTJ5nG2ZFsocJ1iMQEZQk1VcdIGl2L9Yoeitwj3uocirgRPVRAgEjMoMihkgKFStIzK063QVHYhKF6h/IKFMlKGpaNlhM8Qq1JFIp5gQHeoSIShmJPST/PUElJQ7FN9hivUGmJQxE56OQRKiN5qL/NNY9QKQeEIpdgR7VC9cEqHeGeUyEHXHrMUiWhyKx6BV4dEsNMtNxQI4Ls33h5LidUoEYjXKEXoXZDLbgsEBOzE8qne8fmJhE7oWIujUQcwhyMmy0VSKRRF0cyWlclYMYo1LEKZOgRugpDpVWUot5ZLI8LlwN23bNQKoY1vwpVmmsS6kOFsPRJQl2qENZIqGkVwnp9xb5nUksEcxm2hZlQMcy3OYXTgx0VpOi2pl8/jwqpMP9sF5twBl08ctE7I/dws5l5D5y4OBj51HvdzluYdlik0RL06cjkKpSlB1Span5x/AQb5Hfrxyy/oU5ISeVw53AAAAAASUVORK5CYII=)}body,html{margin:0;padding:0}html{height:100%}body{min-height:100%}body{text-align:center;color:#fff;background-color:#fd940a;background-image:radial-gradient(circle,#f9a72b 0,#fa9026 70%,#fb6c1f 100%)}
</style>

<?php


// include functions
include('functions.php');

##############################################
## Add New Post
##############################################

// if go to go.php without request
if (!$_SERVER['REQUEST_METHOD'] == 'POST') {

  // returen to dashboard
  header("Location: index.php");
  // Stop script
  exit();

}

// add new post request
if (isset($_POST['addpost'])){

	// validat data
	$title = validat($_POST['title']);
	$mtitle = validat($_POST['mtitle']);
	$description = validat($_POST['description']);
	$keywords = validat($_POST['keywords']);
	$script = inlinedata(validat($_POST['script']));
	$imgalt = validat($_POST['imgalt']);

  // Generate Permalink
  $permalink = strtolower($title);
  $permalink = preg_replace("/[^a-z0-9_\s-]/", "", $permalink);
  $permalink = preg_replace("/[\s-]+/", " ", $permalink);
  $permalink = preg_replace("/[\s_]/", "-", $permalink);



	/* ----- File Upload --- */

  $target_file = $imagefolder . basename($_FILES["fileToUpload"]["name"]);
  $uploadOk = 1;
  $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
  $renamemyphoto = 0;


  // Check if image file is a actual image or fake image
  if(isset($_POST["submit"])) {
      $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
      if($check !== false) {
          echo "File is an image - " . $check["mime"] . ".";
          $uploadOk = 1;
      } else {
          echo "File is not an image.";
          $uploadOk = 0;
      }
  }

  // Check if file already exists
  if (file_exists($target_file)) {
      $renamemyphoto = 1;
      $randoo = rand(22222, 99999);
  }

  // Check file size
  if ($_FILES["fileToUpload"]["size"] > 500000) {
      echo "Sorry, your file is too large.";
      $uploadOk = 0;
  }

  // Allow certain file formats
  if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
  && $imageFileType != "gif" ) {
      echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
      $uploadOk = 0;
  }

  // Check if $uploadOk is set to 0 by an error
  if ($uploadOk == 0) {
      echo "Sorry, your file was not uploaded.";

  // if everything is ok, try to upload file
  } else {


  if($renamemyphoto == 1){

    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $imagefolder.$randoo.'-'.basename($_FILES["fileToUpload"]["name"]))) {
        $image = $imagefolder.$randoo.'-'.basename($_FILES["fileToUpload"]["name"]);
    } else {
        echo "Sorry, there was an error uploading your file.";
    }

  }else{

      if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $imagefolder.basename($_FILES["fileToUpload"]["name"]))) {
          $image = $imagefolder.basename( $_FILES["fileToUpload"]["name"]);
      } else {
          echo "Sorry, there was an error uploading your file.";
      }
    }


  }


$lastid = getLines($dbfile);
$postid = $lastid + 1 ;
$userData = $postid .' :: '. $mtitle .' :: '. $title .' :: '. $description .' :: '. $keywords .' :: '. $script .' :: '. $image .' :: '. $imgalt .' :: '. $permalink;
if(file_put_contents($dbfile, $userData . PHP_EOL, FILE_APPEND))
{
  echo "<h1 class='text-center'>New Post created successfully</h1>";
  echo '<div class="text-center"><a  href="index.php">Dashboard</a></div>';
}else{
  echo "<h1 class='text-center'>Error: </h1>";
  echo '<div class="text-center"><a href="javascript:history.back()">Go Back</a> | <a  href="index.php">Dashboard</a></div>';
}





}

#########################################################
## Edit
#########################################################
elseif (isset($_POST['editpost'])){

  // validat data
  $lid = validat($_POST['lineid']);
  $lid = $lid - 1;
	$fakeid = validat($_POST['fakeid']);
  $imageurl = validat($_POST['imageurl']);
  $pagelink = validat($_POST['pagelink']);
  $title = validat($_POST['title']);
  $script = inlinedata(validat($_POST['script']));
  $mtitle = validat($_POST['mtitle']);
  $description = validat($_POST['description']);
  $keywords = validat($_POST['keywords']);
  $imgalt = validat($_POST['imgalt']);


  $file = file('data/data.txt');
  $lines = array_map(function ($value) { return rtrim($value, PHP_EOL); }, $file);
  $lines[$lid] = $fakeid .' :: '. $mtitle .' :: '. $title .' :: '. $description .' :: '. $keywords .' :: '. $script .' :: '. $imageurl .' :: '. $imgalt .' :: '. $pagelink;
  $lines = array_values($lines);
  $content = implode(PHP_EOL, $lines);


  if(file_put_contents($dbfile, $content))
  {
    echo "<h1 class='text-center'>Post edited successfully</h1>";
    echo '<div class="text-center"><a  href="index.php">Dashboard</a></div>';
  }else{
    echo "<h1 class='text-center'>Error: </h1>";
    echo '<div class="text-center"><a href="javascript:history.back()">Go Back</a> | <a  href="index.php">Dashboard</a></div>';
  }


}

#########################################################
## Delete
#########################################################
elseif (isset($_GET['d'])){

  // Delete ID
	$deleteid = stripslashes($_GET['d']);

  // DB
  $file_out = file($dbfile);

  //Delete the recorded line
  unset($file_out[$deleteid]);

  //Recorded in a file
  if(file_put_contents($dbfile, implode("", $file_out)))
  {
    echo "<h1 class='text-center'>Post Deleted successfully</h1>";
    echo '<div class="text-center"><a href="add.php">Add New Post</a> | <a  href="index.php">Dashboard</a></div>';
  }else{
    echo "<h1 class='text-center'>Post Deleted successfully</h1>";
    echo '<div class="text-center"><a href="add.php">Add New Post</a> | <a  href="index.php">Dashboard</a></div>';
  }

}


}

?>


<div id="Clouds">
<div class="Cloud Foreground"></div>
<div class="Cloud Background"></div>
<div class="Cloud Foreground"></div>
<div class="Cloud Background"></div>
<div class="Cloud Foreground"></div>
<div class="Cloud Background"></div>
<div class="Cloud Background"></div>
<div class="Cloud Foreground"></div>
<div class="Cloud Background"></div>
<div class="Cloud Background"></div>
<!--  <svg viewBox="0 0 40 24" class="Cloud"><use xlink:href="#Cloud"></use></svg>-->
</div>

<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
width="40px" height="24px" viewBox="0 0 40 24" enable- xml:space="preserve">
<defs>
<path id="Cloud" d="M33.85,14.388c-0.176,0-0.343,0.034-0.513,0.054c0.184-0.587,0.279-1.208,0.279-1.853c0-3.463-2.809-6.271-6.272-6.271
c-0.38,0-0.752,0.039-1.113,0.104C24.874,2.677,21.293,0,17.083,0c-5.379,0-9.739,4.361-9.739,9.738
c0,0.418,0.035,0.826,0.084,1.229c-0.375-0.069-0.761-0.11-1.155-0.11C2.811,10.856,0,13.665,0,17.126
c0,3.467,2.811,6.275,6.272,6.275c0.214,0,27.156,0.109,27.577,0.109c2.519,0,4.56-2.043,4.56-4.562
C38.409,16.43,36.368,14.388,33.85,14.388z"/>
</defs>
</svg>
</body>
</html>
