<?php
$order_id=$_POST["order_id"];

## Include the ini file (required)
require 'ini.inc.php';

## Include core functions
require 'includes/functions.inc.php';

## Include admin presession controller
include CC_ROOT_DIR.CC_DS.'controllers'.CC_DS.'controller.admin.pre_session.inc.php';

$config=$GLOBALS['config']->get('config');
echo $config['dbhost'];
        
$target_dir = "uploads/";

$uploadOk = 1;
//$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$imageFileType = pathinfo(basename($_FILES["fileToUpload"]["name"]),PATHINFO_EXTENSION);

$target_file = $target_dir . $order_id . "." . $imageFileType;

//$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);

// 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)) {
    unlink($target_file);
    //echo "Sorry, file already exists." . $target_file;
    //$uploadOk = 0;
}
 // 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." . $target_file;
    $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 (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        $GLOBALS['db']->update('CubeCart_order_summary', array('status' => '2'), array('cart_order_id' => $order_id));

        if ($GLOBALS['db']->select('CubeCart_bankslip', 'cart_order_id', array('cart_order_id' => $order_id))) {
	  $GLOBALS['db']->update('CubeCart_bankslip', array('filename' => basename($target_file), 'dtUpload' => date('Y-m-d H:i:s')), array('cart_order_id' => $order_id));
        } else {
          $record = array(
	    'cart_order_id' => $order_id,
            'dtUpload'   => date('Y-m-d H:i:s'),
            'filename'  => basename($target_file),
	  );
          $GLOBALS['db']->insert('CubeCart_bankslip', $record);
        }
				
        header("Location: index.php?_a=vieworder");
        die();
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
?> 