HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers68/resortouddorpduin.nl/wwwroot/kms/lib/files/file_handler.class.php
<?php
/**
 * file_handler.class.php
 * Created by Komma Mediadesign.
 * Author: mike
 * Date: 4/15/13
 */




class File_Handler
{
    /*
     * Name of this file handler. Used f.e. in the session name.
     */
    private $_name = 'images';

    /*
     * Maximum images allowed in the session
     */
    private $_maxImages = 0;

    /*
     * Name of the table we're working in
     */
    private $_tableName;

    /*
     * Image dimensions
     * Large Width, Large Height, Thumb Width, Thumb Height, KMS Thumb Width, KMS Thumb Height.
     */
    private $_dim = array('lw'=>null,
                          'lh'=>null,
                          'tw'=>null,
                          'th'=>null,
                          'kw'=>90,
                          'kh'=>90);

    /*
     * Holds all errors that occur.
     */
    private $_errors = array();

    /*
     * Constructor
     */
    public function __construct(){}

    /*
     * Set
     */
    public function set($prop,$val)
    {
        if( ! empty($prop))
        {
            $this->{$prop} = $val;
        }
    }

    /*
     * Get
     */
    public function get($prop)
    {
        if( ! empty($prop))
        {
            return $this->{$prop};
        }
        return false;
    }

    /*
     * Add images to the current Session.
     */
    public function add()
    {
        // Check if any images are selected
        if(empty($_FILES['images']))
        {
            $this->_errors[] = 'No images.';
            return false;
        }

        // Crop images?
        $crop = FALSE;

        // Get current session image_data
        $arr = Session::get($this->_name . '_data');
        is_array($arr) ? $numImages = count($arr) : $numImages = 0;

        // Check for each image if it is valid
        foreach($_FILES['images']['name'] as $key => $name)
        {
            // Create file array
            $file = array('name'=>$_FILES['images']['name'][$key],
                          'tmp_name'=>$_FILES['images']['tmp_name'][$key],
                          'size'=>$_FILES['images']['size'][$key]);

            // Check for name
            if( ! empty($name))
            {
                // Check if max images is reached
                if($numImages == $this->_maxImages && $this->_maxImages != 0)
                {
                    $this->_errors[] = $file['name'] . ': Max images reached.';
                    return false;
                }

                // Add a File_Uploader class and validate the image.
                $FileUploader = new File_Uploader($file);

                if($FileUploader->Validate())
                {
                    if($tempFile = $FileUploader->upload())
                    {
                        $fileExt = $FileUploader->getExt($tempFile);

                        switch($fileExt)
                        {
                            case 'jpg':
                            case 'jpeg':
                            case 'gif':
                            case 'png':
                                if( ! $crop)
                                {
                                    // Create images
                                    $this->createImages($tempFile, $key);

                                }
                                else
                                {
                                    // crop by hand
                                    $_SESSION['images_to_crop'][]['original'] = $tempFile;
                                }
                                break;

                            case 'pdf':
                            case 'doc':
                            case 'zip':
                                break;
                        }
                    }
                }
            }
            else
            {
                $this->_errors[] = $file['name'] . ': Empty name.';
            }
        }
        if(count($this->_errors) == 0)
        {
            return true;
        }
        return FALSE;
    }

    /*
     * Create images from a temp file
     */
    private function createImages($tempFile, $key)
    {
        $temp = explode('.', $tempFile);
        $fileExt = strtolower($temp[count($temp) - 1]);

        // set data
        $data = array();
        $data['short_code'] = $key . time();
        $data['filename'] = FILE_PREFIX.'_'.$data['short_code'].'.'.$fileExt;
        $data['thumb'] = FILE_PREFIX.'_'.$data['short_code'].'_thumb.'.$fileExt;
        $data['kms_thumb'] = FILE_PREFIX.'_'.$data['short_code'].'_kms_thumb.'.$fileExt;
        $data['caption'] = '';

        // add data to session
        $count = 0;
        if($images = Session::get($this->_name.'_data'))
        {
            $count = count($images);
        }
        $key = array($this->_name.'_data', $count);
        Session::set($key,$data);

        list($tempW,$tempH) = getimagesize(DOCUMENT_UPLOADS_ROOT . $tempFile);
        if( ($tempW / $tempH) < 0.5 )
        {
            $this->_dim['lw'] = null;
            $this->_dim['lh'] = 1000;

            $this->_dim['tw'] = null;
            $this->_dim['th'] = 420;
        }

        // Create large image
        $imageCropper = new Image_Cropper(DOCUMENT_UPLOADS_ROOT . $tempFile, $data['filename']);
        $imageCropper->setSides($this->_dim['lw'],$this->_dim['lh']);
        $imageCropper->createImage();

        // Check if a thumbnail is necessary
        if( $this->_dim['tw'] != null || $this->_dim['th'] != null )
        {
            $imageCropper->setName($data['thumb']);
            $imageCropper->setSides($this->_dim['tw'],$this->_dim['th']);
            $imageCropper->createImage();
        }
        else
        {
            // if no thumb necessary, set large image as thumb data to avoid problems.
            $data['thumb'] = $data['filename'];
        }

        // Create a thumbnail for the backend.
        $imageCropper->setName($data['kms_thumb']);
        $imageCropper->setSides($this->_dim['kw'],$this->_dim['kh']);
        $imageCropper->createImage();

        // Unlink the temporary file.
        $this->unlink(DOCUMENT_UPLOADS_ROOT . $tempFile);
    }

    /**
     * Unlink the image
     *
     * @param string
     * @return boolean
     */
    public function unlink($path)
    {
        if( ! empty($path) && is_file($path) ) unlink($path);
    }

    /*
     * Clean up images which are not in the database, but still in the session.
     */
    public function clean($pageName)
    {
        // Check the session for images which are not in the database.
        $sessionImages = Session::get($this->_name . '_data');
        if(is_array($sessionImages))
        {
            foreach($sessionImages as $key => $sessionImage)
            {
                $this->remove($pageName,$key);
            }
        }

        // Destroy image session
        Session::destroy($this->_name . '_data');
    }

    /*
     * Remove image from a session if it doesn't exist in db
     */
    public function remove($pageName, $key)
    {
        // what image to remove
        $sessionImage = Session::get(array($this->_name . '_data',$key));
        if(is_array($sessionImage))
        {
            // if not in the database
            $FileStorage = new File_Storage();
            $FileStorage->set('_fileSessionName',$this->_name . '_data');
            $FileStorage->set('_tableName',TABLE_PREFIX . $pageName . '_images');
            if( ! $FileStorage->get('short_code',$sessionImage['short_code']))
            {
                if( ! empty($sessionImage['filename']) && ! empty($sessionImage['thumb']) && ! empty($sessionImage['kms_thumb']))
                {
                    $this->unlink(DOCUMENT_UPLOADS_ROOT . $sessionImage['filename']);
                    $this->unlink(DOCUMENT_UPLOADS_ROOT . $sessionImage['thumb']);
                    $this->unlink(DOCUMENT_UPLOADS_ROOT . $sessionImage['kms_thumb']);
                }
            }
        }
    }

    /*
     * Set image session.
     */
    public function fillSession()
    {
        $FileStorage = new File_Storage();
        $FileStorage->set('_fileSessionName',$this->_name . '_data');
        $FileStorage->set('_tableName',$this->_tableName);

        $key = $value = null;
        if(defined('URL_SUB2'))
        {
            // set item id
            $key = 'itemId';
            $value = URL_SUB2;
        }
        if($images = $FileStorage->get($key,$value))
        {
            $sessionKey = $this->_name . '_data';
            if( ! isset($_SESSION[$sessionKey]))
            {
                Session::set($sessionKey,$images);
            }
        }
    }
}