<?php
/** 
 *------------------------------------------------------------------------------
 * @package       T4 Page Builder for Joomla!
 *------------------------------------------------------------------------------
 * @copyright     Copyright (C) 2004-2020 JoomlArt.com. All Rights Reserved.
 * @license       GNU General Public License version 2 or later; see LICENSE.txt
 * @authors       JoomlArt, JoomlaBamboo, (contribute to this project at github 
 *                & Google group to become co-author)
 *------------------------------------------------------------------------------
 */


defined('_JEXEC') or die;


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

abstract class FrontEndT4pagebuilderHelper {

  public static $metaNr = 0;
  public static function renderTag($name, $content, $type = 1)
  {
    $document         = Factory::getDocument();
    // Encoded html tags can still be rendered, decode and strip tags first.
    $value                  = trim(strip_tags(html_entity_decode($content)));

    // OG tag
    if ($type == 1) {
      $document->setMetadata(htmlspecialchars($name, ENT_COMPAT, 'UTF-8'), htmlspecialchars($value, ENT_COMPAT, 'UTF-8'));
    } else {
      $attributes = '';
      if ($name == 'og:image') {
        $attributes = ' itemprop="image"';
      }
      $document->addCustomTag('<meta property="'.htmlspecialchars($name, ENT_COMPAT, 'UTF-8').'"'.$attributes.' content="' . htmlspecialchars($value, ENT_COMPAT, 'UTF-8') . '" />');
    }
  }
  public static function renderOpenGraph($params)
  {
    if(!$params) return;

    $app = Factory::getApplication();
    $doc = Factory::getDocument();
    $config = Factory::getConfig();
    $option = $app->input->getCmd('option','');
    if(is_array($params)){
      $params_tmp = $params;
      $params = new Registry();
      $params->loadArray($params_tmp);
    }elseif(is_string($params)){
      $params_tmp = $params;
      $params = new Registry();
      $params->loadString($params_tmp);
    }
    $uri = Uri::getInstance();
    $og_url = $uri->toString();
    $og_title = $params->get('og_title', '');
    $og_desc = $params->get('og_desc','');
    $og_image = $params->get('og_image','');
    $views = $app->input->getCmd('view','');
    if($views == 'tag') return;
    if(self::$metaNr > 0) return;
    if(!$og_title && !$og_desc) return;
    $img_url = self::cleanImageURL($og_image);
    //check more space for each word
    if($og_desc){
      $og_desc_arr = explode("  ",$og_desc);
      $og_desc_arr = array_filter(array_map(
        function($a){ 
          if(!is_null(str_replace("\r\n","",$a))){
            return str_replace("\r\n","",$a);
          };
        },
      $og_desc_arr));
      $og_desc = implode(" ",$og_desc_arr);
    }else{
      $og_desc = $doc->description;
    }
    
    self::renderTag('title', $og_title);
    self::renderTag('description', trim($og_desc)); 
    self::renderTag('og:title', $og_title, 2);
    self::renderTag('og:description', trim($og_desc), 2);
    if($img_url->url) self::renderTag('og:image', self::fullImageURL($img_url->url), 2);
    self::renderTag('og:url', $og_url, 2);
    self::renderTag('twitter:title', $og_title, 2);
    self::renderTag('twitter:description', trim($og_desc), 2);
    if($img_url->url) self::renderTag('twitter:image', self::fullImageURL($img_url->url), 2);
    self::renderTag('twitter:url', $og_url, 2);

  }
  public static function findImage($content)
  {
    if(gettype($content) !== 'string') return "";
    $img_url = '';
    preg_match('/< *img[^>]*src *= *["\']?([^"\']*)/i', $content, $src);
    if (isset($src[1]) && $src[1] != '') {
      $img_url = $src[1];
    }
    return $img_url;
  }
  public static function fullImageURL($image)
  {
    $linkImg        = $image;

    $absU = 0;
    // Test if this link is absolute http:// then do not change it
    $pos1       = strpos($image, 'http://');
    if ($pos1 === false) {
    } else {
      $absU = 1;
    }

    // Test if this link is absolute https:// then do not change it
    $pos2       = strpos($image, 'https://');
    if ($pos2 === false) {
    } else {
      $absU = 1;
    }


    if ($absU == 1) {
      $linkImg = $image;
    } else {
      $linkImg = Uri::base(false).$image;
      if ($image[0] == '/') {
        $myURI = new Uri(Uri::base(false));
        $myURI->setPath($image);
        $linkImg = $myURI->toString();
      }
    }
    return $linkImg;
  }
  /**
   * Gets a URL, cleans the Joomla specific params and returns an object
   *
   * @param    string  $url  The relative or absolute URL to use for the src attribute.
   *
   * @return   object
   * @example  {
   *             url: 'string',
   *             attributes: [
   *               width:  integer,
   *               height: integer,
   *             ]
   *           }
   *
   * @since    4.0.0
   */
  public static function cleanImageURL($url)
  {
    $obj = new \stdClass;

    $obj->attributes = [
      'width'  => 0,
      'height' => 0,
    ];

    if (!strpos($url, '?'))
    {
      $obj->url = $url;

      return $obj;
    }

    $mediaUri = new Uri($url);

    // Old image URL format
    if ($mediaUri->hasVar('joomla_image_height'))
    {
      $height = (int) $mediaUri->getVar('joomla_image_height');
      $width  = (int) $mediaUri->getVar('joomla_image_width');

      $mediaUri->delVar('joomla_image_height');
      $mediaUri->delVar('joomla_image_width');
    }
    else
    {
      // New Image URL format
      $fragmentUri = new Uri($mediaUri->getFragment());
      $width       = (int) $fragmentUri->getVar('width', 0);
      $height      = (int) $fragmentUri->getVar('height', 0);
    }

    if ($width > 0)
    {
      $obj->attributes['width'] = $width;
    }

    if ($height > 0)
    {
      $obj->attributes['height'] = $height;
    }

    $mediaUri->setFragment('');
    $obj->url = $mediaUri->toString();

    return $obj;
  }
	public static function isJ4(){
		return version_compare(JVERSION, '4.0.0', 'ge') ? true : false;
	}
}

?>