Created
November 2, 2016 09:15
-
-
Save iiic/09fec9f0adc6c278161613bb8a303b51 to your computer and use it in GitHub Desktop.
To samé co tady https://gist.github.com/iiic/97599ee45f2b354a9110cb6c80e039ce , akorát pro starší verzi Nette (2.0.13)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{block|strip} | |
{default $type = NULL} {* pokud na nic lepšího nepřijdu, tak zobrazím jako obrázek *} | |
{default $integrityCheckHash = NULL} {* @todo : pokud někdo zadá integritu atributem integrity="sha256-…něco…" tak bych to měl asi vyseparovat a vložit semka *} | |
{default $calcIntegrity = TRUE} | |
{default $noCache = FALSE} | |
{default $attributes = []} | |
{default $hashName = 'sha256'} | |
{default $appendix = NULL} | |
{default $forceSpace = ' '} {* "\n" se dá třeba ještě použít *} | |
{var $fragment = \Nette\Utils\Strings::replace($source, '~^'.$basePath.'/?~i', '')} {* základní část url adresy (celá, pokud jde o externí zdroj) *} | |
{cache $fragment, if => !$noCache, tags => [sourceLoader]} | |
{if !$type} {* 1. Zjistím si typ souboru *} | |
{if \Nette\Utils\Strings::match($fragment, '~^(http|https|ftp)?:?//.+~')} {* externí zdroj (cdn, …) *} | |
{var $mimeTypeString = get_headers($source, 1)['Content-Type']} {* funkce \mime_content_type() se tu použít nedá… neumí vzdálené soubory *} | |
{? $attributes[] = 'crossorigin="anonymous"'} {* nepřenáší se cookies - (naneštěstí) funguje POUZE na cizí doméně *} | |
{var $mimeTypeParts = explode(';', $mimeTypeString)} {* bacha na to mezeru, která se to může snadno vyskytnout před či za textem *} | |
{if preg_grep('~^\s*text/css\s*$~i', $mimeTypeParts)} | |
{var $type = 'css'} | |
{if \Nette\Utils\Strings::contains($source, '//fonts.googleapis.com/css')} {* google fonts vrací různé formáty fontů podle user agenta, z toho důvodu nejde vypočítat integrity check hash *} | |
{var $calcIntegrity = FALSE} | |
{/if} | |
{elseif FALSE} {* @todo : doplnit další mime typy *} | |
{/if} | |
{if !$integrityCheckHash && $calcIntegrity && ($type == 'css' || $type == 'script')} | |
{var $integrityCheckHash = $hashName.'-'.base64_encode(hash($hashName, file_get_contents($source), TRUE))} {* 'safe://' zdá se pro vzdálené soubory nelze použít *} | |
{/if} | |
{else} {* lokální zdroj *} | |
{var $mimeType = mime_content_type($fragment)} {* funkce \mime_content_type() na windows vyžaduje mít v Apachi modul php_fileinfo.dll (nebo na linuxu to je fileinfo.so) *} | |
{if $mimeType != 'text/plain'} {* mime typy *} | |
{if $mimeType == 'application/javascript'} | |
{var $type = 'script'} | |
{elseif $mimeType == 'image/x-icon'} {* favikona *} | |
{var $type = 'favicon'} | |
{elseif $mimeType == 'application/css'} | |
{var $type = 'css'} | |
{/if} | |
{elseif preg_grep('~^\s*(width|height)\s*=~i', $attributes)} {* tyhle atributy jsou dovoleny pouze u obrázků (NE u favikony) a iframe (které sourceLoader neumí) … takže je to tímto jasné *} | |
{$type = 'image'} | |
{* @todo : zkusit, jestli atributy async a defer mohou být je n scriptů a pokud tomu tak skutečně je, tak podle nich scripty identifikovat *} | |
{elseif $extension = \Nette\Utils\Strings::match($fragment, '~.+\.(js|css)(\?.*)*$~i')} {* přípony souborů *} | |
{if $extension[1] == 'js'} {* javascript *} | |
{var $type = 'script'} | |
{else} {* css *} | |
{var $type = 'css'} | |
{/if} | |
{else} {* cesta, doména, … *} | |
{/if} | |
{if !$integrityCheckHash && $calcIntegrity && ($type == 'css' || $type == 'script')} | |
{var $integrityCheckHash = $hashName.'-'.base64_encode(hash($hashName, file_get_contents('safe://'.$control->context->parameters[wwwDir].'/'.$fragment), TRUE))} | |
{/if} | |
{if !$appendix} | |
{var $appendix = \Nette\Utils\Strings::contains($source, '?')?'&':'?'.base_convert($noCache?time():filemtime('safe://'.$control->context->parameters[wwwDir].'/'.$fragment), 10, 36)} {* \base_convert() je převod mezi libovolnými číselnými soustavami, kde 36 je maximální možný základ *} | |
{/if} | |
{/if} | |
{/if} | |
{if $type == 'favicon'} {* 2. pokud schází, tak doplním poviné atributy a přidám nějaké nepoviné, ale důležité atributy *} | |
{var $type = 'link'} | |
{if !preg_grep('~^\s*rel\s*=\s*("|\')\s*(shortcut|.*\s*shortcut|shortcut\s*.*|icon|.*\s*icon|icon\s*.*)\s*("|\')\s*$~', $attributes)} | |
{? $attributes[] = 'rel="shortcut icon"'} | |
{/if} | |
{elseif $type == 'link'} | |
{* nenapadá mě, co dělat … zatím *} | |
{elseif $type == 'css'} | |
{var $type = 'link'} {* nyní už na tom nezáleží, tak si to můžu zjednodušit *} | |
{if $integrityCheckHash} | |
{? $attributes[] = 'integrity="'.$integrityCheckHash.'"'} | |
{/if} | |
{if !preg_grep('~^\s*rel\s*=\s*("|\')\s*(stylesheet|.*\s*stylesheet|stylesheet\s*.*)\s*("|\')\s*$~', $attributes)} | |
{? $attributes[] = 'rel="stylesheet"'} | |
{/if} | |
{elseif $type == 'script'} | |
{if $integrityCheckHash} | |
{? $attributes[] = 'integrity="'.$integrityCheckHash.'"'} | |
{/if} | |
{else} {* jinak jde o obrázek… *} | |
{var $calcIntegrity = FALSE} {* u obrázků nemá žádný vliv… vyzkoušeno *} | |
{if !(preg_grep('~^\s*width\s*=~i', $attributes) || preg_grep('~^\s*height\s*=~i', $attributes))} | |
{var $image = \Nette\Utils\Image::fromFile('safe://'.$context->parameters[wwwDir].'/'.$fragment)} | |
{? $attributes[] = 'width="'.$image->width.'"'} | |
{? $attributes[] = 'height="'.$image->height.'"'} | |
{/if} | |
{if !preg_grep('~^\s*alt\s*=~i', $attributes)} | |
{? $attributes[] = 'alt="obrázek '.$fragment.'"'} | |
{/if} | |
{/if} | |
{if $type == 'script'} {* 3. Vytvořím kód načítající tento zdroj - buď javascript *} | |
<script | |
src="{$source.$appendix|noescape}"{$forceSpace|noescape} | |
{implode($forceSpace, $attributes)|noescape} | |
></script> | |
{elseif $type == 'link'} {* link (css, favicon, …) *} | |
<link | |
href="{$source.$appendix|noescape}"{$forceSpace|noescape} {*{\Nette\Utils\Strings::contains($source, '?')?'&':'?'}{base_convert($noCache?time():filemtime('safe://'.$context->parameters[wwwDir].'/'.$fragment), 10, 36)}*} | |
{implode($forceSpace, $attributes)|noescape} | |
> | |
{else} {* jinak obrázek *} | |
<img | |
src="{$source.$appendix|noescape}"{$forceSpace|noescape} {*{\Nette\Utils\Strings::contains($source, '?')?'&':'?'}{base_convert($noCache?time():filemtime('safe://'.$context->parameters[wwwDir].'/'.$fragment), 10, 36)}*} | |
{implode($forceSpace, $attributes)|noescape} | |
{* {$forceSpace|noescape}ismap {* pokud je obrázek uvnitř odkazu, tak se při kliku na obrázek doplní i přesné souřadnice pixelu v obrázku, na který bylo kliknuto *} | |
{* longdesc="…" {* url adresa, na které je dlouhý popisek obrázku a co je důležité… může obsahovat i hash na záložku na stejné stránce, jako je obrázek, nebo odkaz a hash *} | |
> | |
{/if} | |
{/cache} | |
{/block} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment