[SPIP][PLUGINS] v3.0-->v3.2
[lhc/web/www.git] / www / plugins-dist / compresseur / compresseur_options.php
1 <?php
2
3 /**
4 * Réglage de l'output buffering
5 *
6 * Si possible, générer une sortie compressée pour économiser de la bande passante
7 *
8 * Utilisation déconseillee et désactivee par défaut.
9 * Utilisable uniquement via define('_AUTO_GZIP_HTTP',true)
10 *
11 * @package SPIP\Compresseur\Options
12 */
13
14 // si un buffer est deja ouvert, stop
15 if ($GLOBALS['flag_ob']
16 and defined('_AUTO_GZIP_HTTP') and _AUTO_GZIP_HTTP
17 and strlen(ob_get_contents()) == 0
18 and !headers_sent()) {
19 if (
20 // special bug de proxy
21 !(isset($_SERVER['HTTP_VIA']) and preg_match(',NetCache|Hasd_proxy,i', $_SERVER['HTTP_VIA']))
22 // special bug Netscape Win 4.0x
23 and (strpos($_SERVER['HTTP_USER_AGENT'], 'Mozilla/4.0') === false)
24 // special bug Apache2x
25 #&& !preg_match(",Apache(-[^ ]+)?/2,i", $_SERVER['SERVER_SOFTWARE'])
26 // test suspendu: http://article.gmane.org/gmane.comp.web.spip.devel/32038/
27 #&& !($GLOBALS['flag_sapi_name'] AND preg_match(",^apache2,", @php_sapi_name()))
28 // si la compression est deja commencee, stop
29 # && !@ini_get("zlib.output_compression")
30 and !@ini_get('output_handler')
31 and !isset($_GET['var_mode']) # bug avec le debugueur qui appelle ob_end_clean()
32 ) {
33 ob_start('ob_gzhandler');
34 }
35 }
36
37
38 /**
39 * Transformer toutes les URLs relatives image,js en url absolues qui pointent sur le domaine statique
40 * on applique pas a l'URL de la CSS, car on envoie un header http link qui permet au navigateur de la pre-fetch
41 * sur le meme domaine, sans avoir a faire de requete DNS
42 * @param string $flux
43 * @return string
44 */
45 function compresseur_affichage_final($flux) {
46 if (isset($GLOBALS['meta']['url_statique_ressources'])
47 and isset($GLOBALS['html'])
48 and $GLOBALS['html']
49 and $url_statique = $GLOBALS['meta']['url_statique_ressources']) {
50 $url_statique = rtrim(protocole_implicite($url_statique), '/') . '/';
51 $flux = preg_replace(",(href|src)=([\"'])([^/][^:\"']*[.](?:png|gif|jpg|js)(?:\?[0-9]+)?)\\2,Uims", "\\1=\\2".$url_statique."\\3\\2", $flux);
52
53 // prefetching
54 // <link rel="dns-prefetch" href="//host_name_to_prefetch.com">
55 if (($p = strpos($url_statique, '/', 2)) !== false) {
56 $url_statique = substr($url_statique, 0, $p);
57 $link = "<link rel=\"dns-prefetch\" href=\"$url_statique\">";
58 if ($p = stripos($flux, '</title>')) {
59 $flux = substr_replace($flux, "\n" . $link, $p+8, 0);
60 }
61 }
62 }
63
64 return $flux;
65 }