<?php  if (!defined('PmWiki')) exit();

/*

  Google Fonts - How to use google web fonts on a per page basis
    securely using whitelists for all users

 *****
 *
 *  Copyright 2018 Carlos A. Bonamigo <cabsec.pmwiki@gmail.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *****

 Based on Google Web Font service, that uses OpenSource Licenses .

*/


$RecipeInfo['GoogleFonts']['Version']  =  '20180329';


# font names are case sensitive
# check webfont names with quick use
# preferably numeric styles
# format is "Font+Name:numeric-style:subset"
# only one font name value per key
# only one font style per key
# only one subset value per key

SDVA($GoogleWebFonts, array(
  'AdventProNormalLatin' => 'Advent+Pro:400:latin',
  'PlasterNormalLatin' => 'Plaster:400:latin',
  'EmblemaOneNormalLatin' => 'Emblema+One:400:latin',
  'HennyPennyNormalLatin' => 'Henny+Penny:400:latin',
  'ButchermanNormalLatin' => 'Butcherman:400:latin',
  'CreepsterNormalLatin' => 'Creepster:400:latin',
  'DrSugiyamaNormalLatin' => 'Dr+Sugiyama:400:latin',
  'MonotonNormalLatin' => 'Monoton:400:latin',
  'EwertNormalLatin' => 'Ewert:400:latin',
  'EaterNormalLatin' => 'Eater:400:latin',
  'NosiferNormalLatin' => 'Nosifer:400:latin',
  'TangerineNormalLatin' => 'Tangerine:400:latin',
));

SDV($GoogleWebFontsUrl, 
  "https://fonts.googleapis.com/css?");  

// with this var you can set fonts to be always loaded
SDVA($LoadWebFonts, array(  
  'EwertNormalLatin' => 1,
  'EaterNormalLatin' => 1,
  'NosiferNormalLatin' => 1,
));

Markup_e('reservewebfonts','directives',
   "/\\(:loadfont([^:]*)?:\\)/is",
   "ReserveWebFonts");

function ReserveWebFonts($m){
global $GoogleWebFonts, $RecipeInfo,
   $LoadWebFonts;

if($m[0] != '' && $m[1] == ''){
  LoadWebFonts($LoadWebFonts);
  return;
}

$f = $m[1];
$p = $pagename;

  if(!is_array($f) &&
    strtolower(trim($f)) == 'help' ){
    $h = FmtPageName("!!!$[Webfonts]:",$p).
       " (ver - ".$RecipeInfo['GoogleWebFonts'].")";
       "<ul class='webfonthelp'>";
    foreach($GoogleWebFonts as $k => $v)
      $h .= "<li>".$k."</li>";
    $h .= '</ul>';
    return $h;
  }

  if(!is_array($f) &&
    strtolower(trim($f)) == 'auto' ){
    $h = FmtPageName("$[Autoload]:",$p).
      '<ul class="webfontauto">';
    foreach($LoadWebFonts as $k => $v)
      $h .= "<li>".$k."</li>";
    $h .= '</ul>';
    return $h;
  }

  if(!is_array($f) && 
    strpos($f,',',1) === false &&
    strtolower(trim($f)) != 'auto' &&
    strtolower(trim($f)) != 'help'){
    $l[trim($f)] = 1;
  }

  if(!is_array($f) && 
    strpos($f,',',1) !== false)
    $e = explode(',',$f);

  if(is_array($e)){
    foreach($e as $k => $v){
      $n = trim($v);
      $l[$n] = 1;
    }
  }
$auto = array_merge($l,$LoadWebFonts);
LoadWebFonts($auto);
}

function LoadWebFonts($fn){
global $_SERVER, $HTMLHeaderFmt, 
   $GoogleWebFonts, $LoadWebFonts,
   $GoogleWebFontsUrl,
   $UrlScheme;

if($fn == '') return;

$x = $l = $e = $fl = $fk = '';
  foreach($fn as $l => $v){
    $x = trim($l);
    if(@$GoogleWebFonts[$x] != '' 
    && $v == 1){
      $e = explode(':',$GoogleWebFonts[$x]);
      if(count($e) == 3){
        $fl[$e[0]]['family'] = $e[0]; 
        $fl[$e[0]]['styles'][$e[1]] = 1;
        $fl[$e[0]]['subsets'][$e[2]] = 1;
      }else $fl = '';
    }else $fl = '';
  }

if(!is_array($fl)) return;

$family = ''; $subset = ''; 
  foreach($fl as $f){
    if(is_array($f['styles'])){
      if(count($fl,0) >= 2 
         && $family != '') $family .= "|";
      $family .= $f['family'].":";
      foreach($f['styles'] as $st => $v ){
        $family .= $st.",";
      }
      $family = substr($family,0,-1);
    }
    if(is_array($f['subsets'])){
      if(count($fl,0) >= 2 
        && $subset != '') $subset .= ",";
      foreach($f['subsets'] as $sb => $v ){
        if(strpos($subset,$sb) === false){
          $subset .= $sb.",";
        }
      }
      $subset = substr($subset,0,-1);
    }
  }

# how we got called
if($UrlScheme == ''){
  $shttp = $_SERVER['HTTPS'];
  $sport = $_SERVER['SERVER_PORT'];
  if($shttp != 'on' && 
     $sport != '443') 
       $UrlScheme = 'http';
  if($shttp == 'on' &&
     $sport == '443')
       $UrlScheme = 'https';
}

$surl = $GoogleWebFontsUrl;
if($UrlScheme == 'http')
  $surl = str_replace('https:','http:',$surl);
if($UrlScheme == 'https')
  $surl = str_replace('http:','https:',$surl);

$url = $surl."family=".$family.
   "&amp;subset=".$subset;
$out = "<link href='".$url.
   "' rel='stylesheet' type='text/css'>";
$ix = 'googlewebfonts-'.rand(100,999);
$HTMLHeaderFmt[$ix] = $out;

}