Skip to content
Snippets Groups Projects
TarteAuCitron.php 7.55 KiB
<?php
/**
 * Copyright (c) 2012-2021, Agence Française Informatique (AFI). All rights reserved.
 *
 * BOKEH is free software; you can redistribute it and/or modify
 * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
 * the Free Software Foundation.
 *
 * There are special exceptions to the terms and conditions of the AGPL as it
 * is applied to this software (see README file).
 *
 * BOKEH is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
 *
 * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
 * along with BOKEH; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
 */


class Class_Cookies_TarteAuCitron extends Class_Cookies_Base {
  use Trait_Translator;

  protected $_activated = [],
    $_display_panel = false,
    $_video_managed = ['youtube', 'youtube_playlist', 'allocine', 'vimeo','ina', 'commeaucinema', 'iframe', 'gallica'];

  public function initScripts() {
    $official_js_url = Class_Url::absolute("/public/tarteaucitron/tarteaucitron.js");
    $customize_js_url = Class_Url::absolute("/public/tarteaucitron-config.js");
    $bokeh_js_url = Class_Url::absolute("/public/tarteaucitron-bokeh.js");

    Class_FileManager::beOpenBar();
    if ($file = Class_FileManager::file(USERFILES.'/js/tarteaucitron-config.js'))
      $customize_js_url = $file->getUrl();

    return
      '<script src="'. $official_js_url.'"></script>
       <script src="'. $bokeh_js_url .'"></script>
       <script src="'. $customize_js_url .'"></script>
    ';
  }


  public function gTags($gtags_id) {
    return sprintf( "<script type=\"text/javascript\">
                   tarteaucitron.user.googletagmanagerId = '%s';
                   (tarteaucitron.job = tarteaucitron.job || []).push('googletagmanager');
                   </script>"
                   , $gtags_id)
      .$this->activateAll();
  }


  public function gTagsFrame($gtags_id) {
    return '';
  }


  public function analytics() {
    $js_stat = new Class_AdminVar_JsStat();
    if ($js_stat->isGoogleAnalytics())
      return $this->googleAnalytics($js_stat->getTrackerId(), $js_stat->getGoogleAnalyticsMoreJs());
    return Class_AdminVar::get('JS_STAT');
  }


  public function googleAnalytics($id,$js) {
    return sprintf( '<script type="text/javascript">
                   tarteaucitron.user.analyticsUa = "%s";
                   tarteaucitron.user.analyticsMore = function () {%s};
    (tarteaucitron.job = tarteaucitron.job || []).push("analytics");</script>', $id, $js)
      .$this->activateAll();
  }


  public function matomo($site_id, $url) {
    return sprintf('<script type="text/javascript">
    tarteaucitron.user.matomoId = %s;
    (tarteaucitron.job = tarteaucitron.job || []).push(\'matomo\');
    tarteaucitron.user.matomoHost = \'%s\';
    </script>', $site_id, $url);
  }


  protected function _getScript($name) {
    if (in_array($name, $this->_activated ))
      return '';

    return $this->activateAll();
  }


  public function activateAll() {
    $html = '<script>';
    foreach ($this->_video_managed as $name ){
      if (in_array($name, $this->_activated))
        continue;
      $html .=' (tarteaucitron.job = tarteaucitron.job || []).push("'.str_replace('_','',$name).'");';
      $this->_activated[] = $name;
    }

    return $html.'</script>';
  }


  public function getMatchTag($pattern, $string) {
    if (!preg_match('/'.$pattern.'="([^"]+)/', $string, $matches))
      return '';
    return $matches[1];
  }


  public function replace($content) {
    $firstdiv='(?P<firstdiv><div[^>]+data-oembed="(?P<src>[^"]+)[^>]+data-oembed_provider="(?P<provider>\w+)"[^>]+>)';

    $content_replaced =
      preg_replace_callback(
                            '|'.$firstdiv.'<iframe(?P<ifr>[^>]+)>[^<]*</iframe>[^<]*</div>|',
                            function ($matches) {

                              $conf=[];
                              if (!$source = $this->getMatchTag('src', $matches['ifr']))
                                return $matches['firstdiv'].$matches['ifr'].'</div>';
                              if ($height = $this->getMatchTag('height', $matches['ifr']))
                                $conf ['height'] = $height;
                              if ($width = $this->getMatchTag('width', $matches['ifr']))
                                $conf ['width'] = $width;

                              return $matches['firstdiv'].$this->getPlayer($matches['provider'],$source, $conf).'</div>';
                            },
                            $content);

    $frames='(?P<frame><iframe[^>]+src="(?P<src>[^"]+)[^>]+></iframe>)';

    return  preg_replace_callback(
                                  '|'.$frames.'|',
                                  function ($matches) {
                                    $conf = [];
                                    if ($height = $this->getMatchTag('height', $matches['frame']))
                                      $conf ['height'] = $height;
                                    if ($width = $this->getMatchTag('width', $matches['frame']))
                                      $conf ['width'] = $width;

                                    return $this->getIFrame('iframe',$matches['src'], $conf);

                                  },$content_replaced);
  }


    public function getAdminPlaylistPlayer($id) {
      $name='youtube_playlist';
      return sprintf('<div class="%s_player" %s="%s"  theme="dark" rel="1" controls="1" showinfo="0" autoplay="0" mute="0" width="750" height="450"></div>'
                     . $this->_getScript($name),
                     $name,
                     'playlistID',
                     $id);
    }


    public function getIFrame($name, $url,$attribs=[]) {

      return sprintf('<div class="tac_iframe" data-url="%s" %s></div>'
                     . $this->_getScript($name),
                     $url,
                     $this->_getStringFromAttribs($attribs));
    }


    public function getPlayer($name, $url, $attribs = []) {
      $name = strtolower($name);
      if ($name=='ina.fr')
        $name='ina';

      if (!in_array($name, $this->_video_managed) || $name=='iframe')
        return $this->getIFrame($name,$url,$attribs);

      $default_attribs =  array_merge(['theme' => 'dark', 'controls' =>1, 'autoplay'=>0],$attribs);

      $id_name = ($name == 'youtube_playlist')
        ? 'playlistID'
        : 'videoID';

      $id = $url;

      if ($name=='youtube' || $name=='vimeo' || $name=='youtube_playlist') {
        $path = parse_url($url) ?
          parse_url($url)['path']
          : $url;

        $path_array =  explode('/', $path);
        $id = $path_array[count($path_array)-1];
      }

      $html = sprintf('<div class="%s_player" %s="%s"  %s></div>'
                      . $this->_getScript($name),
                      $name,
                      $id_name,
                      $id,
                      $this->_getStringFromAttribs($default_attribs));
      return $html;

    }


    public function triggers() {
      return 'if (tarteaucitron  && tarteaucitron.job) { tarteaucitron.triggerJobsAfterAjaxCall();}';
    }


    public function link($title)  {
      return  sprintf('<a href="#" title="%s"  onclick="tarteaucitron.userInterface.openPanel();">%s</a>',
                      $this->_('Lien vers ').$title,
                      $title);
    }


    public function gallicaPlayer($url) {
      return $this->getPlayer('gallica', $url);
    }
}