<?php

/**
 * @package     Sven.Bluege
 * @subpackage  com_eventgallery
 *
 * @copyright   Copyright (C) 2005 - 2019 Sven Bluege All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

// Check to ensure this file is included in Joomla!
use Joomla\Component\Eventgallery\Site\Library\Configuration\Main;

defined('_JEXEC') or die();
require_once JPATH_ROOT.'/components/com_eventgallery/config.php';

class EventgalleryLibraryFileLocal extends EventgalleryLibraryFile
{

    protected $_image_script_path = 'components/com_eventgallery/helpers/image.php?';

    /**
     * creates the lineitem object. $dblineitem is the database object of this line item
     *
     * @param object $object
     * @throws Exception
     */
    function __construct($object)
    {

        parent::__construct($object);

        if ($this->config->getImage()->doUseLegacyImageRendering()) {
            $this->_image_script_path = "index.php?option=com_eventgallery&view=resizeimage";
        }

        // this is necessary to avoid an exception while running in CLI mode
        if (array_key_exists('REQUEST_METHOD', $_SERVER)) {
            $currentApplicationName = JFactory::getApplication()->getName();

            if ($currentApplicationName == 'administrator') {
                $this->_image_script_path .= '&site=1';
            }
        }
    }

    public function getImageUrl($width=104,  $height=104, $fullsize=false, $larger=false, $relative=false) {
        if ($fullsize) {
            return $this->createThumbnailUrl(COM_EVENTGALLERY_IMAGE_ORIGINAL_MAX_WIDTH, $relative);
        } else {
            $sizeSet = new EventgalleryHelpersSizeset();
            $width = $sizeSet->getSizeCode($width, $height, $this->getWidth(), $this->getHeight());
            return $this->createThumbnailUrl($width, $relative);
        }
    }

    public function getThumbUrl ($width=104, $height=104, $larger=true, $relative=false) {
        $sizeSet = new EventgalleryHelpersSizeset();
        $width = $sizeSet->getSizeCode($width, $height, $this->getWidth(), $this->getHeight());
        return $this->createThumbnailUrl($width, $relative);
    }

    public function getOriginalImageUrl() {

    	return JUri::base().substr(JRoute::_('index.php?option=com_eventgallery&view=download&folder='.$this->getFolderName().'&file='.urlencode($this->getFileName()) ), strlen(JUri::base(true)) + 1);

    }

    public function getSharingImageUrl() {

        return JUri::base().substr(JRoute::_('index.php?option=com_eventgallery&is_for_sharing=true&view=download&&folder='.$this->getFolderName().'&file='.urlencode($this->getFileName()) ), strlen(JUri::base(true)) + 1);

    }

    /**
     * increases the hit counter in the database
     */
    public function countHit() {
        /**
         * @var EventgalleryTableFile $table
         */
        $table = JTable::getInstance('File', 'EventgalleryTable');
        $table->hit($this->_file->id);
    }

    public function syncFile() {
        $folderpath = COM_EVENTGALLERY_IMAGE_FOLDER_PATH.$this->getFolderName();
        self::updateMetadata($folderpath.DIRECTORY_SEPARATOR.$this->getFileName(), $this->getFolderName(), $this->getFileName());

        return EventgalleryLibraryManagerFolder::$SYNC_STATUS_SYNC;
    }

    /**
     * upaded meta information
     * @param $path
     * @param $foldername
     * @param $filename
     */
    public static function updateMetadata($path, $foldername, $filename) {
        $config = Main::getInstance();

        /** @noinspection PhpUnusedLocalVariableInspection */
        @list($width, $height, $type, $attr) = getimagesize($path, $info);


        $creation_date = "";
        $title = "";
        $caption = "";

        if (isset($info["APP13"]) && function_exists("iptcparse")) {
            $iptc = iptcparse($info["APP13"]);
            if (is_array($iptc)) {
                if (isset($iptc["2#005"])) {
                    $title = $iptc["2#005"][0];
                }

                if (isset($iptc["2#055"])) {
                    $creation_date = $iptc["2#055"][0];
                    if (isset($iptc["2#060"])) {
                        $creation_date .= $iptc["2#060"][0];
                    }
                }

                if (isset($iptc["2#120"])) {
                    $caption = $iptc["2#120"][0];
                }
            }
        }


        $exif = \components\com_eventgallery\site\library\Data\Exif::extract($path);

        if ($config->getImage()->doAutoRotate() && $exif->has90DegreeRotation) {
            $tempWidth = $width;
            $width = $height;
            $height = $tempWidth;
        }

	    if (empty($creation_date)) {
            $creation_date = $exif->creation_date;
        }

        // do some filtering for the content. We do not allow HTML in here.
        $filter = JFilterInput::getInstance();
        $title = $filter->clean($title, 'html');
        $caption = $filter->clean($caption, 'html');
        $creation_date = $filter->clean($creation_date, 'html');


        $use_iptc_data = $config->getImage()->doUseIPTCData();
        $override_with_iptc_data = $config->getImage()->doOverwriteWithIPTCData();

        EventgalleryLibraryFileLocal::storeMetadata($foldername, $filename, $width, $height, $exif->toJson(), $creation_date, $use_iptc_data, $override_with_iptc_data, $title, $caption);
    }

    static public function storeMetadata($foldername, $filename, $width, $height, $exifJson, $creation_date, $use_iptc_data, $override_with_iptc_data, $iptcTitle, $iptcCaption) {
        $db = JFactory::getDbo();

        $query = $db->getQuery(true);
        $query->update("#__eventgallery_file");
        $query->set("width=".$db->quote($width));
        $query->set("height=".$db->quote($height));
        $query->set("exif=".$db->quote($exifJson));
        $query->where('folder='.$db->quote($foldername));
        $query->where('file='.$db->quote($filename));
        $db->setQuery($query);
        $db->execute();

        if (!empty($creation_date)) {
            $query = $db->getQuery(true);
            $query->update("#__eventgallery_file");
            $query->set("creation_date=".$db->quote($creation_date));
            $query->where('folder=' . $db->quote($foldername));
            $query->where('file=' . $db->quote($filename));
            if ($override_with_iptc_data == false) {
                $query->where("(creation_date='' OR creation_date IS NULL)");
            }
            $db->setQuery($query);
            $db->execute();
        }

        if ($use_iptc_data && !empty($iptcCaption)) {
            $query = $db->getQuery(true);
            $query->update("#__eventgallery_file");
            $query->set("caption=" . $db->quote($iptcCaption));
            $query->where('folder=' . $db->quote($foldername));
            $query->where('file=' . $db->quote($filename));
            if ($override_with_iptc_data == false) {
                $query->where("(caption='' OR caption IS NULL)");
            }
            $db->setQuery($query);
            $db->execute();
        }

        if ($use_iptc_data && !empty($iptcTitle)) {
            $query = $db->getQuery(true);
            $query->update("#__eventgallery_file");
            $query->set("title=" . $db->quote($iptcTitle));
            $query->where('folder=' . $db->quote($foldername));
            $query->where('file=' . $db->quote($filename));
            if ($override_with_iptc_data == false) {
                $query->where("(title='' OR title IS NULL)");
            }
            $db->setQuery($query);
            $db->execute();
        }
    }

    public function createThumbnails() {
        $sizeSet = new EventgalleryHelpersSizeset();
        $availableSizes = array_unique($sizeSet->availableSizes);

        foreach($availableSizes as $availableSize) {
            try {
                EventgalleryLibraryCommonImageprocessor::createThumbnail($this->getFolderName(), $this->getFileName(), $availableSize, true, true, null, true, true);
            } catch (EventgalleryLibraryExceptionUnsupportedfileextensionexception $e){

            }
        }

        return [$availableSizes];
    }

    public function getOriginalFile()
    {
        $basename = COM_EVENTGALLERY_IMAGE_FOLDER_PATH . $this->getFolderName() . DIRECTORY_SEPARATOR;

        $filename = $basename . $this->getFileName();
        // try the path to a possible original file
        $fullFilename = $basename. COM_EVENTGALLERY_IMAGE_ORIGINAL_SUBFOLDER . DIRECTORY_SEPARATOR . $this->getFileName();

        if (file_exists($fullFilename)) {
            $filename = $fullFilename;
        }

        return file_get_contents($filename);

    }

    /**
     * Deletes the image file
     */
    public function deleteImageFile() {
        $path= COM_EVENTGALLERY_IMAGE_FOLDER_PATH . JFile::makeSafe($this->getFolderName()).DIRECTORY_SEPARATOR;
        $filename=JFile::makeSafe($this->getFileName());
        $file = $path.$filename;

        $cacheFolder = COM_EVENTGALLERY_IMAGE_CACHE_PATH . JFile::makeSafe($this->getFolderName()).DIRECTORY_SEPARATOR;

        if (file_exists($cacheFolder)) {
            $fileIterator = new FilesystemIterator($cacheFolder, FilesystemIterator::SKIP_DOTS);
            while ($fileIterator->valid()) {
                $cacheFilename = $fileIterator->getFilename();
                if (preg_match("/(mainimage_)?nocrop_[\d]{2,4}_{$this->getFileName()}$/", $cacheFilename)) {
                    unlink($fileIterator->getPathname());
                }
                $fileIterator->next();
            }
        }

        if (file_exists($file) && !is_dir($file)) {
            if (!unlink($file)) {
                return false;
            }
        }
        return true;
    }

    /**
     * @param int $width
     * @param boolean $relative
     * @return string
     */
    protected function createThumbnailUrl($width, $relative)
    {
        $config = Main::getInstance();

        if ($config->getImage()->doUsePrecalculatedThumbnailsForLocalFiles()) {
            $sizeSet = new EventgalleryHelpersSizeset();
            $saveAsSize = $sizeSet->getMatchingSize($width);
            $image_thumb_file = EventgalleryLibraryCommonImageprocessor::calculateCacheThumbnailName($saveAsSize, true, $this->getFileName(), $this->getFolderName(), $this->isMainImage());

            return Joomla\CMS\Uri\Uri::root($relative) . COM_EVENTGALLERY_IMAGE_CACHE_WEB_PATH . '/' . urlencode($this->getFolderName()) . '/' . basename($image_thumb_file) . ($config->getImage()->hasBust()?'?'.$config->getImage()->getBust():'');
        }

        return Joomla\CMS\Uri\Uri::root($relative) . $this->_image_script_path . "&width=" . $width . "&folder=" . $this->getFolderName() . "&file=" . urlencode($this->getFileName()) . ($config->getImage()->hasBust()?'&'.$config->getImage()->getBust():'');
    }
}
