require_once 'header.php';
$controller = new ControllerPhoto();
$controllerStore = new ControllerStore();
$extras = new Extras();
$photo_id = $extras->decryptQuery1(KEY_SALT, $_SERVER['QUERY_STRING']);
if ($photo_id != null) {
    $photo_store = $controller->getPhotoByPhotoId($photo_id);
    $viewUrl = $extras->encryptQuery1(KEY_SALT, 'store_id', $photo_store->store_id, 'photo_store_view.php');
    if (isset($_POST['url_upload'])) {
        $itm = new Photo();
        $itm->photo_url = trim($_POST['photo_url']);
        $itm->thumb_url = trim($_POST['thumb_url']);
        $itm->store_id = $photo_store->store_id;
        $itm->photo_id = $photo_store->photo_id;
        $controller->updatePhoto($itm);
        echo "<script type='text/javascript'>location.href='{$viewUrl}';</script>";
    }
    if (isset($_POST['file_upload'])) {
        $count = count($_FILES["file"]["name"]);
        if (!empty($_FILES["file"]["name"][0]) && !empty($_FILES["file"]["name"][1])) {
            uploadFile($controller, $photo_store);
        } else {
            echo "<script>alert('You must provide both Photo and Thumbnail file.');</script>";
        }
    }
} else {
    echo "<script type='text/javascript'>location.href='403.php';</script>";
}
?>
     $id = uniqid();
     $temp = explode(".", $_FILES["thumb_file"]["name"]);
     $extension = end($temp);
     $thumb_new_file_name = $desired_dir . "/" . "thumb_" . $id . "." . $extension;
     $thumb_new_file_name_path = $desired_dir_path . "/" . "thumb_" . $id . "." . $extension;
     move_uploaded_file($_FILES['thumb_file']['tmp_name'], $thumb_new_file_name_path);
     $itm->thumb_url = ROOT_URL . $thumb_new_file_name;
     $id = uniqid();
     $temp = explode(".", $_FILES["photo_file"]["name"]);
     $extension = end($temp);
     $photo_new_file_name = $desired_dir . "/" . "photo_" . $id . "." . $extension;
     $photo_new_file_name_path = $desired_dir_path . "/" . "photo_" . $id . "." . $extension;
     move_uploaded_file($_FILES['photo_file']['tmp_name'], $photo_new_file_name_path);
     $itm->photo_url = ROOT_URL . $photo_new_file_name;
     if ($photo != null) {
         $controllerPhoto->updatePhoto($itm);
         $photo_id = $itm->photo_id;
     } else {
         $controllerPhoto->insertPhoto($itm);
         $photo_id = $controllerPhoto->getLastInsertedPhotoId();
     }
     $itm = $controllerPhoto->getPhotoByPhotoId($photo_id);
     $json = "{\n                            \"photo_realestate_info\" : {\n                                          \"photo_id\" : \"{$itm->photo_id}\",\n                                          \"photo_url\" : \"{$itm->photo_url}\",\n                                          \"thumb_url\" : \"{$itm->thumb_url}\",\n                                          \"realestate_id\" : \"{$itm->realestate_id}\",\n                                          \"created_at\" : \"{$itm->created_at}\",\n                                          \"updated_at\" : \"{$itm->updated_at}\",\n                                          \"is_deleted\" : \"0\"\n                                          },\n                            \"status\" : {\n                                          \"status_code\" : \"-1\",\n                                          \"status_text\" : \"Success.\"\n                                        }\n                            }";
 } else {
     if ($photo != null) {
         $controllerPhoto->updatePhoto($itm);
         $photo_id = $itm->photo_id;
     } else {
         $controllerPhoto->insertPhoto($itm);
         $photo_id = $controllerPhoto->getLastInsertedPhotoId();
     }