<?php
  ob_start();
  session_start();
  include 'db.php';
  include '../pages/functions.php';
  include 'class.upload.php';


  $askUser=$db->prepare("SELECT * FROM user WHERE email=:email");
  $askUser->execute(array(
    'email' => $_SESSION['email']
  ));
  $checkUser=$askUser->fetch(PDO::FETCH_ASSOC);
  $_SESSION['user_status'] = $checkUser['user_status'];
    if ($_SESSION['user_status'] != "999") {
      header("Location:../index.php?status=demoMode");
    }else{
    if(!empty($_FILES)){
      $gallery_type_id = $_POST["gallery_type"]. "-" . $_POST["gallery_id"];
      // Image Upload
      $image = new Upload($_FILES['file']);
      $uploads_dir = '../../uploads';
      @$name = $_FILES['file']["name"];
      $extension = uzanti($name);
      $uniqnumber1 = rand(20000,32000);
      $uniqnumber2 = rand(20000,32000);
      $uniqname = $uniqnumber1.$uniqnumber2;
      $imagepath ='media_upload_'.$uniqname;
      $vtpath_full = substr($uploads_dir, 6)."/".$imagepath.".".$extension;
      $vtpath_large = substr($uploads_dir, 6)."/".$imagepath.'-1140x740'.".".$extension;
      $vtpath_medium = substr($uploads_dir, 6)."/".$imagepath.'-500x333'.".".$extension;
      $vtpath_thumbnail = substr($uploads_dir, 6)."/".$imagepath.'-300x300'.".".$extension;
      if($image->uploaded){
        $image->file_new_name_body = $imagepath;
        $image->allowed = array ('image/*');
        $image->Process('../../uploads/');
        // Image resize 580x377
        $image->file_new_name_body = $imagepath.'-1140x740';
        $image->image_resize = true;
        $image->image_x = 1140;
        $image->image_y = 740;
        $image->allowed = array ('image/*');
        $image->Process('../../uploads/');
        // Image resize 300x300
        $image->file_new_name_body = $imagepath.'-500x333';
        $image->image_resize = true;
        $image->image_x = 500;
        $image->image_y = 333;
        $image->allowed = array ('image/*');
        $image->Process('../../uploads/');
        // Image resize 150x150
        $image->file_new_name_body = $imagepath.'-300x300';
        $image->image_resize = true;
        $image->image_x = 300;
        $image->image_y = 300;
        $image->allowed = array ('image/*');
        $image->Process('../../uploads/');
      }
      $addData=$db->prepare("INSERT INTO media SET
        gallery_type_id=:gallery_type_id,
        media_full=:media_full,
        media_large=:media_large,
        media_medium=:media_medium,
        media_thumbnail=:media_thumbnail
        ");
      $insert=$addData->execute(array(
        'gallery_type_id'  => $gallery_type_id,
        'media_full'       => $vtpath_full,
        'media_large'      => $vtpath_large,
        'media_medium'     => $vtpath_medium,
        'media_thumbnail'  => $vtpath_thumbnail
      ));
    }

    if (isset($_POST['mediaDelete'])) {
      opControl();
      $deleteData=$db->prepare("DELETE from media where media_id=:media_id");
      $control=$deleteData->execute(array(
        'media_id' => $_POST['media_id']
      ));
      if ($control) {
        $media_full = explode("/", $_POST['media_full']);
        $media_large = explode("/", $_POST['media_large']);
        $media_medium = explode("/", $_POST['media_medium']);
        $media_thumbnail = explode("/", $_POST['media_thumbnail']);
        mkdir('dnmdizini', 7777);
        unlink("../../uploads/". $media_full[1]);
        unlink("../../uploads/". $media_large[1]);
        unlink("../../uploads/". $media_medium[1]);
        unlink("../../uploads/". $media_thumbnail[1]);
        rmdir('dnmdizini');
        Header("Location:../index.php?page=mediaUpload&gallery_type={$_POST['gallery_type']}&gallery_id={$_POST['gallery_id']}&gallery_title={$_POST['gallery_title']}");
      }else{
        Header("Location:../index.php?page=mediaUpload&gallery_type={$_POST['gallery_type']}&gallery_id={$_POST['gallery_id']}&gallery_title={$_POST['gallery_title']}");
      }
    }
  }
 ?>
