<?php
session_start();

$image_id = 56;
if(isset($_SESSION['id'])){
$uploaderid = $_SESSION['id'];
}
  $uploaderid = "56";
if(isset($_SESSION['username'])){
 $username = $_SESSION['username'];
}
  $username = "dezvinni@gmail.com";
//upload.php
include('database_connection.php');

//   $query = "
//   INSERT INTO screenshots (screenshot_image, image_id, uploaderid, username) 
//   VALUES ('1', '1', '1', '1')
//   ";






if(count($_FILES["file"]["name"]) > 0)
{
 $output = '';
 sleep(3);


 for($count=0; $count<count($_FILES["file"]["name"]); $count++)
 {
  $file_name = $_FILES["file"]["name"][$count];
  $tmp_name = $_FILES["file"]['tmp_name'][$count];
  $file_array = explode(".", $file_name);
  $file_extension = end($file_array);

//   $query = "
//   INSERT INTO screenshots (screenshot_image, image_id, uploaderid, username) 
//   VALUES ('1', '1', '1', '1')
//   ";




  if(file_already_uploaded($file_name, $connect))
  {
   $file_name = $file_array[0] . '-'. rand() . '.' . $file_extension;
  }


// print $rand;
function genRandomString()
{
    $length = 27;
    $characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWZYZ";

    $real_string_length = strlen($characters) ;
    $string="img";

    for ($p = 0; $p < $length; $p++)
    {
        $string .= $characters[mt_rand(0, $real_string_length-1)];
    }

    return strtolower($string);
}

// Add rand 
$rand = genRandomString();

  $location = 'screenshots/' . $file_name;
//  $location = 'postfiles/' . $rand;
//  $location = 'postfiles/' . $file_name;
// $illegal = "false";

// if (str_contains($tmp_name), 'php')){
// $illegal = "false";
//  }
// if ($illegal == "false"){


function imageStrip($oldFileLocation,$newFileLocation){
$newLimit = "128";
ini_set( 'memory_limit', $newLimit . 'M' );

$ext = pathinfo($oldFileLocation, PATHINFO_EXTENSION);
if(strtolower($ext)=="jpg"){$img = imagecreatefromjpeg($oldFileLocation); imagejpeg($img, $newFileLocation, 23);}
if(strtolower($ext)=="jpeg"){$img = imagecreatefromjpeg($oldFileLocation); imagejpeg($img, $newFileLocation, 23);}
if(strtolower($ext)=="png"){$img = imagecreatefrompng($oldFileLocation); imagepng($img, $newFileLocation, 9);}
if(strtolower($ext)=="gif"){$img = imagecreatefromgif($oldFileLocation); imagegif($img, $newFileLocation);}

imagedestroy ($img);
}

// imageStrip('/home/coindez/public_html/postfiles/test.jpg', '/home/coindez/public_html/postfiles/testt.jpg');


  if(move_uploaded_file($tmp_name, $location))
  {
$fullpath = '/home/coindez/public_html/' . $location;
// imageStrip($fullpath, $fullpath);


   $query = "
   INSERT INTO screenshots (screenshot_image, image_id, uploaderid, username) 
   VALUES ('".$file_name."', '".$image_id."', '".$uploaderid."', '".$username."')
   ";

   $statement = $connect->prepare($query);
   $statement->execute();


//   $query = "
//   INSERT INTO screenshots (screenshot_image, image_id, uploaderid, username) 
//   VALUES ('1', '1', '1', '1')
 //  ";

//   $query = "
//   INSERT INTO screenshots (screenshot_image, image_id, uploaderid, username) 
//   VALUES ('".$file_name."', '".$image_id."', '".$uploaderid."', '".$username."')
//   ";
  
 //  $statement = $connect->prepare($query);
 //  $statement->execute();
  }
//  }

 }
}

function file_already_uploaded($file_name, $connect)
{
 
//  $query = "SELECT * FROM tbl_image WHERE image_name = '".$file_name."'";
// $statement = $connect->prepare($query);
// $statement->execute();
 $number_of_rows = $statement->rowCount();
 if($number_of_rows > 0)
 {
  return true;
 }
 else
 {
  return false;
 }
}

?>
