1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 
<?php
/* --------------------------------------------------------------
   resource.get_usermod.php 2017-11-27
   Gambio GmbH
   http://www.gambio.de
   Copyright (c) 2017 Gambio GmbH
   Released under the GNU General Public License (Version 2)
   [http://www.gnu.org/licenses/gpl-2.0.html]
   --------------------------------------------------------------
*/

/**
 * Class Smarty_Resource_Get_Usermod
 *
 * Resource overload for the smarty include statements. It enables USERMOD-files for templates.
 *
 * USAGE:
 * {include file="get_usermod:PATH/TO/TPL.FILE"}
 */
class Smarty_Resource_Get_Usermod extends Smarty_Resource_Custom
{
    
    /**
     * Fetch a template and its modification time
     *
     * @param string  $name   template name
     * @param string  $source template source
     * @param integer $mtime  template modification timestamp (epoch)
     *
     * @return void
     */
    protected function fetch($name, &$source, &$mtime)
    {
        $filename = get_usermod($this->_getPath($name));
        
        if(stripos($filename, 'extends:') === 0)
        {
            $filename  = str_replace('extends:', '', $filename);
            $filenames = explode('|', $filename);
            $source    = '{extends file="' . $filenames[0] . '"}';
            $mtime     = 0;
            unset($filenames[0]);
            foreach($filenames as $filename)
            {
                $source .= file_get_contents($filename);
                if($mtime < filemtime($filename))
                {
                    $mtime = filemtime($filename);
                }
            }
        }
        else
        {
            $source = file_get_contents($filename);
            $mtime  = filemtime($filename);
        }
    }
    
    
    /**
     * Fetch a template's modification time
     *
     * @note implementing this method is optional. Only implement it if modification times can be accessed faster than
     *       loading the complete template source.
     *
     * @param string $name template name
     *
     * @return integer timestamp (epoch) the template was modified
     */
    protected function fetchTimestamp($name)
    {
        $filename = get_usermod($this->_getPath($name));
        
        if(stripos($filename, 'extends') === 0)
        {
            return null;
        }
        
        return filemtime($filename);
    }
    
    
    /**
     * @param $name
     *
     * @return string
     */
    protected function _getPath($name)
    {
        $path = DIR_FS_CATALOG . $name;
        
        if(defined('APPLICATION_RUN_MODE') && APPLICATION_RUN_MODE === 'backend')
        {
            $path = DIR_FS_ADMIN . 'html/content/' . $name;
        }
        
        if(!file_exists($path))
        {
            $gxModulesFiles = GXModulesCache::getInstalledModuleFiles();
            $filePath       = DIR_FS_CATALOG . 'GXModules/' . $name;
            
            if(in_array(strtolower($filePath), array_map('strtolower', $gxModulesFiles), true))
            {
                $path = $filePath;
            }
        }
        
        return $path;
    }
}