Your IP : 216.73.216.240


Current Path : /home/juvelize/saulnois/tmp/install_695d2cf0b4957/src/
Upload File :
Current File : /home/juvelize/saulnois/tmp/install_695d2cf0b4957/src/TAmp.php

<?php
/*
* @package		Tech Fry Library
* @license		https://www.gnu.org/licenses/gpl-3.0.en.html
*/

namespace TechFry\Library;

defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\Uri\Uri;

// Class for handling AMP Pages
class TAmp
{
    public function __construct()
    {


    }
    
    // 1. Check whether to display amp page or not
    public function is_amp()
    {
        // Check if AMP is enabled in template options
        $app = Factory::getApplication('site');
        
        $template = $app->getTemplate(true);
        $enable_amp = $template->params->get('enable_amp', 0);
        
        if (!$enable_amp)
        {
            return false;
        }

        // Only allow for category, content, tags and tfcontent component
        $option = $app->input->get('option', '');
        if (!in_array($option, array('com_content', 'com_tags', 'com_categories', 'com_tfcontent')))
        {
            return false;
        }
        
        $v = '';
        
        // First get v from Joomla uri
        $uri = Uri::getInstance();
        if ($uri->hasVar('v'))
        {
            $v = $uri->getVar('v');
        }
        
        // Then, try to get v from PHP server uri
        if (empty($v))
        {
            $server_uri = $app->input->server->get('REQUEST_URI', '', 'RAW');
            $str = parse_url($server_uri, PHP_URL_QUERY);
          	if ($str)
            {
            	parse_str($str, $output);
            
            	$v = $output['v'] ?? '';  
            }
        }
        
        if ($v != 'amp')
        {
            return false;
        }
        
        return true;
    }
    
    // 2. Get Amp url - Only for supported components
    public function get_amp_url()
    {
        $app = Factory::getApplication('site');
        
        // Only allow for category, content, tags and tfcontent component
        $option = $app->input->get('option', '');
        if (!in_array($option, array('com_content', 'com_tags', 'com_categories', 'com_tfcontent')))
        {
            return;
        }
        
        $uri = Uri::getInstance();
        
        $amp_url = strpos($uri, '?') ? $uri . '&v=amp' : $uri . '?v=amp';
        
        return $amp_url;
    }

    // 3. Convert Html to AMP
    public function convert_html2amp($html)
    {
        //$pattern = '/<img(.*?)>/i';
        $pattern = '/<img[^>]*src=["\']([^"\']+)["\'][^>]*width=["\'](\d+)["\'][^>]*height=["\'](\d+)["\'][^>]*>/i';

        //$replacement = '<amp-img$1 layout="responsive"></amp-img>';
        $replacement = '<amp-img src="$1" width="$2" height="$3" layout="responsive"></amp-img>';

        $amp_html = preg_replace($pattern, $replacement, $html);

        return $amp_html;
    }
}