vendor/numero2/contao-opengraph3/src/Resources/contao/classes/OpenGraph.php line 39

Open in your IDE?
  1. <?php
  2. /**
  3.  * Contao Open Source CMS
  4.  *
  5.  * Copyright (c) 2005-2021 Leo Feyer
  6.  *
  7.  * @package   Opengraph3
  8.  * @author    Benny Born <benny.born@numero2.de>
  9.  * @author    Michael Bösherz <michael.boesherz@numero2.de>
  10.  * @license   LGPL
  11.  * @copyright 2021 numero2 - Agentur für digitales Marketing GbR
  12.  */
  13. namespace numero2\OpenGraph3;
  14. use Contao\Config;
  15. use Contao\ContentElement;
  16. use Contao\Controller;
  17. use Contao\Environment;
  18. use Contao\File;
  19. use Contao\FilesModel;
  20. use Contao\Frontend;
  21. use Contao\ModuleModel;
  22. use Contao\PageModel;
  23. use Contao\StringUtil;
  24. use Contao\System;
  25. class OpenGraph3 extends Frontend {
  26.     /**
  27.      * Add OpenGraph tags to the current page
  28.      *
  29.      * @param Model $ref
  30.      */
  31.     public static function addTagsToPage$ref=null ): void {
  32.         Controller::loadDataContainer('opengraph_fields');
  33.         System::loadLanguageFile('opengraph_fields');
  34.         global $objPage;
  35.         $objRef = !$ref $objPage $ref;
  36.         $objRootPage = ($objRef instanceof PageModel) ? PageModel::findById($objPage->rootId) : null;
  37.         self::parseAdditionalProperties$objRef );
  38.         self::parseAdditionalProperties$objRootPage );
  39.         // add og tags
  40.         if( !empty($GLOBALS['TL_DCA']['opengraph_fields']['fields']) ) {
  41.             foreach( $GLOBALS['TL_DCA']['opengraph_fields']['fields'] as $fieldName => $field ) {
  42.                 // add tag if missing
  43.                 if( ($objRef->{$fieldName} || $objRootPage->{$fieldName}) && !self::checkTag($field['label'][0]) ) {
  44.                     $tag $field['label'][0];
  45.                     $value null;
  46.                     $value $objRef->{$fieldName} ? $objRef->{$fieldName} : $objRootPage->{$fieldName};
  47.                     // get value based on inputType
  48.                     switch( $field['inputType'] ) {
  49.                         case 'textarea':
  50.                         case 'select':
  51.                         case 'text':
  52.                             switch( $fieldName ) {
  53.                                 case 'og_country_name':
  54.                                 case 'og_business_contact_data_country_name':
  55.                                     $arrCountries = [];
  56.                                     $arrCountries System::getCountries();
  57.                                     $value $arrCountries[$value];
  58.                                 break;
  59.                             }
  60.                             if( !empty($field['eval']['rgxp']) ) {
  61.                                 switch( $field['eval']['rgxp'] ) {
  62.                                     case 'datim':
  63.                                         $date null;
  64.                                         $date = \DateTime::createFromFormatConfig::get('datimFormat'), $value );
  65.                                         $value $date->format('Y-m-d\TH:i:s');
  66.                                     break;
  67.                                 }
  68.                             }
  69.                         break;
  70.                         case 'fileTree':
  71.                             $objFile null;
  72.                             $objFile FilesModel::findByUuid$value );
  73.                             if( $objFile ) {
  74.                                 $value Environment::get('base') . System::urlEncode($objFile->path);
  75.                                 $size Config::get($fieldName.'_size');
  76.                                 if( $size ) {
  77.                                     $oFile = new File($objFile->path);
  78.                                     $size StringUtil::deserialize($size);
  79.                                     if( is_numeric($size) ){
  80.                                         $size = [00, (int) $size];
  81.                                     } else if( !is_array($size) ) {
  82.                                         $size = [];
  83.                                     }
  84.                                     $size += [00'crop'];
  85.                                     if( $oFile && $oFile->exists() && $oFile->isGdImage ) {
  86.                                         try {
  87.                                             $src System::getContainer()->get('contao.image.image_factory')->create(TL_ROOT '/' $objFile->path$size)->getUrl(TL_ROOT);
  88.                                             if( $src !== $objFile->path ) {
  89.                                                 $value Environment::get('base') . System::urlEncode($src);
  90.                                             }
  91.                                         } catch( \Exception $e ) {
  92.                                         }
  93.                                     }
  94.                                 }
  95.                             } else {
  96.                                 continue 2;
  97.                             }
  98.                         break;
  99.                         case 'openGraphProperties':
  100.                             continue 2;
  101.                         break;
  102.                         default:
  103.                             throw new \Exception("Unhandled field type ".$field['inputType']);
  104.                         break;
  105.                     }
  106.                     // add og:image:secure_url if applicable
  107.                     if( $tag === 'og:image' && strpos($value,'https:') !== false ) {
  108.                         self::addTag('og:image:secure_url'$value);
  109.                     }
  110.                     self::addTag($tag$value);
  111.                 }
  112.             }
  113.             // og:locale
  114.             if( !self::checkTag('og:locale') ) {
  115.                 $value $objRef->og_locale $objRef->og_locale $objRootPage->og_locale;
  116.                 // set default locale based on the page´s language
  117.                 if( !$value ) {
  118.                     switch( $objPage->language ) {
  119.                         case 'en' :
  120.                             $value 'en_US';
  121.                             break;
  122.                         default :
  123.                             $value sprintf("%s_%s",$objPage->language,strtoupper($objPage->language));
  124.                             break;
  125.                     }
  126.                 }
  127.                 self::addTag'og:locale'$value );
  128.             }
  129.             // og:url added automatically
  130.             if( !self::checkTag('og:url') ) {
  131.                 self::addTag'og:url'Environment::get('url') . Environment::get('requestUri') );
  132.             }
  133.         }
  134.     }
  135.     /**
  136.      * Appends a property to the given object
  137.      *
  138.      * @param string $prop
  139.      * @param string $value
  140.      * @param Model $objRef
  141.      */
  142.     public static function addProperty$prop$value$objRef ): void {
  143.         $aProperties = [];
  144.         $aProperties $objRef->og_properties StringUtil::deserialize($objRef->og_properties) : [];
  145.         $aProperties[] = [$prop$value];
  146.         $objRef->og_properties serialize($aProperties);
  147.     }
  148.     /**
  149.      * Appends OpenGraph data for the given module
  150.      *
  151.      * @param Model $objRow
  152.      * @param String $strBuffer
  153.      * @param Module $objModule
  154.      *
  155.      * @return String
  156.      */
  157.     public function appendTagsByModule$objRow$strBuffer$objModule ): string {
  158.         $moduleClass null;
  159.         $moduleClass get_class($objModule);
  160.         // find and import a matching module to parse the OpenGraph data from
  161.         foreach( $GLOBALS['TL_OG_MODULES'] as $module ) {
  162.             if( $moduleClass === "Contao\ModuleModel" && $objModule->type === $module[0] || $moduleClass === $module[1] ) {
  163.                 $this->import($module[2]);
  164.                 $this->{$module[2]}->addModuleData($objModule);
  165.             }
  166.         }
  167.         return $strBuffer?:'';
  168.     }
  169.     /**
  170.      * Checks if the given Content Element is a module and tries to use it
  171.      * for OpenGraph data
  172.      *
  173.      * @param Model $objRow
  174.      * @param String $strBuffer
  175.      * @param Contao\ContentElement $objElement
  176.      *
  177.      * @return String
  178.      */
  179.     public function findCompatibleModules$objRow$strBuffer$objElement ): string {
  180.         if( get_class($objElement) === "Contao\ContentModule" ) {
  181.             $objModule null;
  182.             $objModule ModuleModel::findById($objElement->module);
  183.             if( $objModule ) {
  184.                 self::appendTagsByModule(null$strBuffer$objModule);
  185.             }
  186.         }
  187.         return $strBuffer?:'';
  188.     }
  189.     /**
  190.     * Adds the additional og_properties as individual attributes
  191.     *
  192.     * @param Model $ref
  193.     */
  194.     private static function parseAdditionalProperties$ref ): void {
  195.         if( !empty($ref->og_properties) ) {
  196.             $props null;
  197.             $props StringUtil::deserialize($ref->og_properties);
  198.             if( !empty($props) ) {
  199.                 foreach( $props as $p ) {
  200.                     if( !isset($ref->{$p[0]}) || empty($ref->{$p[0]}) ) {
  201.                         $ref->{$p[0]} = $p[1];
  202.                     }
  203.                 }
  204.             }
  205.         }
  206.     }
  207.     /**
  208.     * Adds a specific OpenGraph tag to the head
  209.     *
  210.     * @param String $tagName
  211.     * @param String $tagValue
  212.     *
  213.     * @return bool
  214.     */
  215.     private static function addTag$tagName=null$tagValue=null ): bool {
  216.         if( empty($tagName) || empty($tagValue) ) {
  217.             return false;
  218.         }
  219.         $attribute 'property';
  220.         if( strpos($tagName'twitter') === ) {
  221.             $attribute 'name';
  222.         }
  223.         $GLOBALS['TL_HEAD'][] = sprintf(
  224.             '<meta %s="%s" content="%s" />'
  225.         ,   $attribute
  226.         ,   $tagName
  227.         ,   htmlspecialcharsself::replaceInsertTags($tagValue) )
  228.         );
  229.         return true;
  230.     }
  231.     /**
  232.      * Checks if a specific OpenGraph tag already exists
  233.      *
  234.      * @param String $tagName
  235.      * @param String $tagValue
  236.      *
  237.      * @return bool
  238.      */
  239.     private static function checkTag$tagName=null ): bool {
  240.         if( empty($tagName) ) {
  241.             return false;
  242.         }
  243.         if( !empty($GLOBALS['TL_HEAD']) && is_array($GLOBALS['TL_HEAD']) ) {
  244.             foreach( $GLOBALS['TL_HEAD'] as $i => $v ) {
  245.                 if( strpos($v$tagName) !== FALSE ) {
  246.                     return true;
  247.                 }
  248.             }
  249.         }
  250.         return false;
  251.     }
  252. }