<?php
/***************************************************************************
 *                                                                          *
 * Copyright (c) 2005 ISPG Technologies (I) Pvt Ltd. All rights reserved.   *
 *                                                                          *
 * This  is  commercial  software,  only  users  who have purchased a valid *
 * license  and  accept  to the terms of the  License Agreement can install *
 * and use this program.                                                    *
 *                                                                          *
 ****************************************************************************
 * PLEASE READ THE FULL TEXT  OF THE SOFTWARE  LICENSE   AGREEMENT  IN  THE *
 * "copyright.txt" FILE PROVIDED WITH THIS DISTRIBUTION PACKAGE.            *
 ****************************************************************************/
//
//$Id: AjaxUploadImageController.php, v 1.0 2011/06/14 20:52:11
//
class AjaxUploadImageController extends Controller {
    //IMAGE UPLOAD ACTION
    public function uploadsAction() {
        $ObjImage        = new ImageModel();
        $fileFieldName   = $_REQUEST["fileFieldName"];
        $requestFileName = urldecode($_REQUEST["fileFieldName"]);
        $imageAttribite  = isset($_REQUEST["imageAttribite"]) ? $_REQUEST["imageAttribite"] : '';
        if (isset($_REQUEST["method"]) && ($_REQUEST["method"] == 'delete')) {
            $rel_fs_path = ltrim($requestFileName, '/'); // Delete files from public only
            if ($ObjImage->unlinkImage($rel_fs_path)) {
                echo "success";
            }
            exit;
        } else if (isset($_FILES[$fileFieldName])) {
            $validImageFiles = explode(",", VALID_IMAGE_FILES);
            $fileExtention   = strtolower(substr($_FILES[$fileFieldName]['name'], strrpos($_FILES[$fileFieldName]['name'], '.') + 1));
            //Checking Valid File Size & Image File Extension
            if (($_FILES[$fileFieldName]["size"] > FILE_UPLOAD_LIMIT) || (!in_array($fileExtention, $validImageFiles))) {
                echo "error";
                exit;
            } else {
                $imagename       = $_FILES[$fileFieldName]['name'];
                $tmpname         = $_FILES[$fileFieldName]['tmp_name'];
                $info            = pathinfo($imagename);
                $file_name       = basename($imagename, '.' . $info['extension']);
                $filename_image  = $file_name . "__" . rand() . "." . $info['extension'];
                $rel_fs_path_tmp = __DIR_PUBLIC . '/uploads/temp/'; // Relative file system path to temp folder to upload image through AJAX
                $dest            = $rel_fs_path_tmp . $filename_image;
                if (is_uploaded_file($tmpname)) {
                    $tmp_name = $tmpname;
                    move_uploaded_file($tmpname, $dest);
                    echo $filename_image;
                }
            }
        }
    }
    //END OF FUNCTION
}