<?php

/*
 * this function created to used easy class upload
 * every function have job
 * you can add more function and modied without damage other
 * function used in files
 */

require_once 'class.upload.php';

//upload new image with random name
#$filename for input file name
#$boolresize with true or false to resize image
#$width for image width
#$height for image hieght
#$uploadfolder the desitation folder > must be in upload folder
#$imageNewName the retun name of the upload image
#$uploadtype the upload type
function uploadnew($filename, $boolresize, $width, $height, $uploadfolder, $uploadtype = TRUE) {
    //create random name for image ,that will be upload
    $randomname = md5(uniqid(mt_rand(), true));
    //upload image by using the classes
    $handle = new upload($_FILES[$filename]);
    $imageNewName = "no image";
    //using the upload classes
//    print_r($handle->error);


    if ($handle->uploaded) {
        $handle->image_interlace = true;
        $handle->image_resize = $boolresize;
        $handle->image_x = $width;
        $handle->image_y = $height;
        if ($uploadtype) {
            $handle->allowed = array('image/*');
        } else {
            $handle->allowed = array('application/pdf', 'application/msword', 'application/x-zip', 'image/*'
                , 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
        }
        $handle->file_new_name_body = $randomname;
        $handle->process("../../upload/" . $uploadfolder);




        if ($handle->processed) {
            $handle->clean();
            $imageNewName = $randomname . "." . $handle->file_src_name_ext;
        }
    }



    #end of image upload
    return $imageNewName;
}

//upload update image with random name
#$filename for input file name
#$boolresize with true or false to resize image
#$width for image width
#$height for image hieght
#$uploadfolder the desitation folder > must be in upload folder
#$imageNewName the retun name of the upload image
#$oldname is old image name
#$uploadtype the upload type
function uploadupdate($filename, $oldname, $boolresize, $width, $height, $uploadfolder, $uploadtype = TRUE) {

    $imageUrl = filter_input(INPUT_POST, $oldname);
    //create random name for image ,that will be upload
    $randomname = md5(uniqid(mt_rand(), true));
    //upload image by using the classes
    $handle = new upload($_FILES[$filename]);
    $imageNewName = $imageUrl;
    //using the upload classes
    if ($handle->uploaded) {
        $handle->image_interlace = true;
        $handle->image_resize = $boolresize;
        $handle->image_x = $width;
        $handle->image_y = $height;
        if ($uploadtype) {
            $handle->allowed = array('image/*');
        } else {
            $handle->allowed = array('application/pdf', 'application/msword', 'image/*', 'application/x-zip');
        }
        $handle->file_new_name_body = $randomname;
        $handle->process("../../upload/" . $uploadfolder);
        if ($handle->processed) {
            $handle->clean();
            //delete old image
            if ($imageNewName == "no image") {
                
            } else {
                if (file_exists('../../upload/' . $uploadfolder . '/' . $imageNewName)) {
                    //echo $imageNewName;
                    chmod('../../upload/' . $uploadfolder . '/' . $imageNewName, 0777);
                    unlink('../../upload/' . $uploadfolder . '/' . $imageNewName);
                }
            }

            $imageNewName = $randomname . "." . $handle->file_src_name_ext;
        }
    }
    #end of image upload
    return $imageNewName;
}

//upload new image with random name
#$filename for input file name
#$boolresize with true or false to resize image
#$width for image width
#$height for image hieght
#$uploadfolder the desitation folder > must be in upload folder
#$imageNewName the retun name of the upload image
#$uploadtype the upload type
function uploadnewout($filename, $boolresize, $width, $height, $uploadfolder, $uploadtype = TRUE) {
    //create random name for image ,that will be upload
    $randomname = md5(uniqid(mt_rand(), true));
    //upload image by using the classes
    $handle = new upload($_FILES[$filename]);
    $imageNewName = "no image";
    //using the upload classes
    if ($handle->uploaded) {
        $handle->image_interlace = true;
        $handle->image_resize = $boolresize;
        $handle->image_x = $width;
        $handle->image_y = $height;
        if ($uploadtype) {
            $handle->allowed = array('image/*');
        } else {
            $handle->allowed = array('application/pdf', 'application/msword', 'application/x-zip', 'image/*');
        }
        $handle->file_new_name_body = $randomname;
        $handle->process("../upload/" . $uploadfolder);
        if ($handle->processed) {
            $handle->clean();
            $imageNewName = $randomname . "." . $handle->file_src_name_ext;
        }
    }
    #end of image upload
    return $imageNewName;
}

//upload update image with random name
#$filename for input file name
#$boolresize with true or false to resize image
#$width for image width
#$height for image hieght
#$uploadfolder the desitation folder > must be in upload folder
#$imageNewName the retun name of the upload image
#$oldname is old image name
#$uploadtype the upload type
function uploadupdateout($filename, $oldname, $boolresize, $width, $height, $uploadfolder, $uploadtype = TRUE) {

    $imageUrl = filter_input(INPUT_POST, $oldname);
    //create random name for image ,that will be upload
    $randomname = md5(uniqid(mt_rand(), true));
    //upload image by using the classes
    $handle = new upload($_FILES[$filename]);
    $imageNewName = $imageUrl;
    //using the upload classes
    if ($handle->uploaded) {
        $handle->image_interlace = true;
        $handle->image_resize = $boolresize;
        $handle->image_x = $width;
        $handle->image_y = $height;
        if ($uploadtype) {
            $handle->allowed = array('image/*');
        } else {
            $handle->allowed = array('application/pdf', 'application/msword', 'image/*', 'application/x-zip');
        }
        $handle->file_new_name_body = $randomname;
        $handle->process("../upload/" . $uploadfolder);
        if ($handle->processed) {
            $handle->clean();
            //delete old image
            if ($imageNewName == "no image") {
                
            } else {
                if (file_exists('../upload/' . $uploadfolder . '/' . $imageNewName)) {
                    //echo $imageNewName;
                    chmod('../upload/' . $uploadfolder . '/' . $imageNewName, 0777);
                    unlink('../upload/' . $uploadfolder . '/' . $imageNewName);
                }
            }

            $imageNewName = $randomname . "." . $handle->file_src_name_ext;
        }
    }
    #end of image upload
    return $imageNewName;
}

function uploadH($filename, $uploadfolder, $advId, $storedName) {
    //print_r($filename);
    $ffff = $_FILES[$filename]['name'];     // file name
//        print_r($filename);
//        print_r($ffff);
    $value = explode(".", $ffff);
    $file_ext = strtolower(array_pop($value));
    $target_dir = "../upload/" . $uploadfolder;
    $target_file = $target_dir . '/' . $advId . '/' . $storedName . '.' . $file_ext;
    if (!file_exists($target_dir . '/' . $advId)) {
        mkdir($target_dir . '/' . $advId, 0777, true);
    }
    move_uploaded_file($_FILES[$filename]["tmp_name"], $target_file);
    #end of image upload
    return $storedName . '.' . $file_ext;
}

function uploadHEX($filename, $uploadfolder, $advId, $storedName, $type) {   // $type  = 0  is admin
    $ffff = $_FILES[$filename]['name'];     // file name;
    $value = explode(".", $ffff);
    $file_ext = strtolower(array_pop($value));
    if ($type == 0) {
        $target_dir = "../../upload/" . $uploadfolder;
        //echo $target_dir;
    } else {
        $target_dir = "../upload/" . $uploadfolder;
    }


    $target_file = $target_dir . '/' . $advId . '/' . $storedName . '.' . $file_ext;
    if (!file_exists($target_dir . '/' . $advId)) {
        mkdir($target_dir . '/' . $advId, 0777, true);
    }
    move_uploaded_file($_FILES[$filename]["tmp_name"], $target_file);
    $_FILES[$filename]["tmp_name"] = NULL;
    #end of image upload
    return $storedName . '.' . $file_ext;
}

function uploadfile($handle, $url) {
    if ($handle->uploaded) {
        $randomname = md5(uniqid(mt_rand(), true));
        $handle->file_new_name_body = $randomname;
        $handle->process($url);
        if ($handle->processed) {
            $handle->clean();
        } else {
            echo 'error : ' . $handle->error;
        }
    }
    $full = $randomname . "." . $handle->file_src_name_ext;
    return $full;
}
