@@ -17,7 +17,7 @@ discard block |
||
| 17 | 17 | **/ |
| 18 | 18 | |
| 19 | 19 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 20 | - return; |
|
| 20 | + return; |
|
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | |
@@ -38,118 +38,118 @@ discard block |
||
| 38 | 38 | * @param int $status Code de redirection (301 ou 302) |
| 39 | 39 | **/ |
| 40 | 40 | function redirige_par_entete($url, $equiv = '', $status = 302) { |
| 41 | - if (!in_array($status, array(301, 302))) { |
|
| 42 | - $status = 302; |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - $url = trim(strtr($url, "\n\r", " ")); |
|
| 46 | - # si l'url de redirection est relative, on la passe en absolue |
|
| 47 | - if (!preg_match(",^(\w+:)?//,", $url)) { |
|
| 48 | - include_spip("inc/filtres_mini"); |
|
| 49 | - $url = url_absolue($url); |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - if (defined('_AJAX') and _AJAX) { |
|
| 53 | - $url = parametre_url($url, 'var_ajax_redir', 1, '&'); |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - // ne pas laisser passer n'importe quoi dans l'url |
|
| 57 | - $url = str_replace(array('<', '"'), array('<', '"'), $url); |
|
| 58 | - // interdire les url inline avec des pseudo-protocoles : |
|
| 59 | - if ( |
|
| 60 | - (preg_match(",data:,i", $url) and preg_match("/base64\s*,/i", $url)) |
|
| 61 | - or preg_match(",(javascript|mailto):,i", $url) |
|
| 62 | - ) { |
|
| 63 | - $url = "./"; |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - // Il n'y a que sous Apache que setcookie puis redirection fonctionne |
|
| 67 | - include_spip('inc/cookie'); |
|
| 68 | - if ((!$equiv and !spip_cookie_envoye()) or ( |
|
| 69 | - (strncmp("Apache", $_SERVER['SERVER_SOFTWARE'], 6) == 0) |
|
| 70 | - or (stripos($_SERVER['SERVER_SIGNATURE'], 'Apache') !== false) |
|
| 71 | - or function_exists('apache_getenv') |
|
| 72 | - or defined('_SERVER_APACHE') |
|
| 73 | - ) |
|
| 74 | - ) { |
|
| 75 | - @header("Location: " . $url); |
|
| 76 | - $equiv = ""; |
|
| 77 | - } else { |
|
| 78 | - @header("Refresh: 0; url=" . $url); |
|
| 79 | - if (isset($GLOBALS['meta']['charset'])) { |
|
| 80 | - @header("Content-Type: text/html; charset=" . $GLOBALS['meta']['charset']); |
|
| 81 | - } |
|
| 82 | - $equiv = "<meta http-equiv='Refresh' content='0; url=$url'>"; |
|
| 83 | - } |
|
| 84 | - include_spip('inc/lang'); |
|
| 85 | - if ($status != 302) { |
|
| 86 | - http_status($status); |
|
| 87 | - } |
|
| 88 | - echo '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">', "\n", |
|
| 89 | - html_lang_attributes(), ' |
|
| 41 | + if (!in_array($status, array(301, 302))) { |
|
| 42 | + $status = 302; |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + $url = trim(strtr($url, "\n\r", " ")); |
|
| 46 | + # si l'url de redirection est relative, on la passe en absolue |
|
| 47 | + if (!preg_match(",^(\w+:)?//,", $url)) { |
|
| 48 | + include_spip("inc/filtres_mini"); |
|
| 49 | + $url = url_absolue($url); |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + if (defined('_AJAX') and _AJAX) { |
|
| 53 | + $url = parametre_url($url, 'var_ajax_redir', 1, '&'); |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + // ne pas laisser passer n'importe quoi dans l'url |
|
| 57 | + $url = str_replace(array('<', '"'), array('<', '"'), $url); |
|
| 58 | + // interdire les url inline avec des pseudo-protocoles : |
|
| 59 | + if ( |
|
| 60 | + (preg_match(",data:,i", $url) and preg_match("/base64\s*,/i", $url)) |
|
| 61 | + or preg_match(",(javascript|mailto):,i", $url) |
|
| 62 | + ) { |
|
| 63 | + $url = "./"; |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + // Il n'y a que sous Apache que setcookie puis redirection fonctionne |
|
| 67 | + include_spip('inc/cookie'); |
|
| 68 | + if ((!$equiv and !spip_cookie_envoye()) or ( |
|
| 69 | + (strncmp("Apache", $_SERVER['SERVER_SOFTWARE'], 6) == 0) |
|
| 70 | + or (stripos($_SERVER['SERVER_SIGNATURE'], 'Apache') !== false) |
|
| 71 | + or function_exists('apache_getenv') |
|
| 72 | + or defined('_SERVER_APACHE') |
|
| 73 | + ) |
|
| 74 | + ) { |
|
| 75 | + @header("Location: " . $url); |
|
| 76 | + $equiv = ""; |
|
| 77 | + } else { |
|
| 78 | + @header("Refresh: 0; url=" . $url); |
|
| 79 | + if (isset($GLOBALS['meta']['charset'])) { |
|
| 80 | + @header("Content-Type: text/html; charset=" . $GLOBALS['meta']['charset']); |
|
| 81 | + } |
|
| 82 | + $equiv = "<meta http-equiv='Refresh' content='0; url=$url'>"; |
|
| 83 | + } |
|
| 84 | + include_spip('inc/lang'); |
|
| 85 | + if ($status != 302) { |
|
| 86 | + http_status($status); |
|
| 87 | + } |
|
| 88 | + echo '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">', "\n", |
|
| 89 | + html_lang_attributes(), ' |
|
| 90 | 90 | <head>', |
| 91 | - $equiv, ' |
|
| 91 | + $equiv, ' |
|
| 92 | 92 | <title>HTTP ' . $status . '</title> |
| 93 | 93 | ' . ((isset($GLOBALS['meta']['charset'])) ? '<meta http-equiv="Content-Type" content="text/html;charset=' . $GLOBALS['meta']['charset'] . '">' : '') . ' |
| 94 | 94 | </head> |
| 95 | 95 | <body> |
| 96 | 96 | <h1>HTTP ' . $status . '</h1> |
| 97 | 97 | <a href="', |
| 98 | - quote_amp($url), |
|
| 99 | - '">', |
|
| 100 | - _T('navigateur_pas_redirige'), |
|
| 101 | - '</a></body></html>'; |
|
| 98 | + quote_amp($url), |
|
| 99 | + '">', |
|
| 100 | + _T('navigateur_pas_redirige'), |
|
| 101 | + '</a></body></html>'; |
|
| 102 | 102 | |
| 103 | - spip_log("redirige $status: $url"); |
|
| 103 | + spip_log("redirige $status: $url"); |
|
| 104 | 104 | |
| 105 | - exit; |
|
| 105 | + exit; |
|
| 106 | 106 | } |
| 107 | 107 | |
| 108 | 108 | // http://code.spip.net/@redirige_formulaire |
| 109 | 109 | function redirige_formulaire($url, $equiv = '', $format = 'message') { |
| 110 | - if (!_AJAX |
|
| 111 | - and !headers_sent() |
|
| 112 | - and !_request('var_ajax') |
|
| 113 | - ) { |
|
| 114 | - redirige_par_entete(str_replace('&', '&', $url), $equiv); |
|
| 115 | - } // si c'est une ancre, fixer simplement le window.location.hash |
|
| 116 | - elseif ($format == 'ajaxform' and preg_match(',^#[0-9a-z\-_]+$,i', $url)) { |
|
| 117 | - return array( |
|
| 118 | - // on renvoie un lien masque qui sera traite par ajaxCallback.js |
|
| 119 | - "<a href='$url' name='ajax_ancre' style='display:none;'>anchor</a>", |
|
| 120 | - // et rien dans le message ok |
|
| 121 | - '' |
|
| 122 | - ); |
|
| 123 | - } else { |
|
| 124 | - // ne pas laisser passer n'importe quoi dans l'url |
|
| 125 | - $url = str_replace(array('<', '"'), array('<', '"'), $url); |
|
| 126 | - |
|
| 127 | - $url = strtr($url, "\n\r", " "); |
|
| 128 | - # en theorie on devrait faire ca tout le temps, mais quand la chaine |
|
| 129 | - # commence par ? c'est imperatif, sinon l'url finale n'est pas la bonne |
|
| 130 | - if ($url[0] == '?') { |
|
| 131 | - $url = url_de_base() . $url; |
|
| 132 | - } |
|
| 133 | - $url = str_replace('&', '&', $url); |
|
| 134 | - spip_log("redirige formulaire ajax: $url"); |
|
| 135 | - include_spip('inc/filtres'); |
|
| 136 | - if ($format == 'ajaxform') { |
|
| 137 | - return array( |
|
| 138 | - // on renvoie un lien masque qui sera traite par ajaxCallback.js |
|
| 139 | - '<a href="' . quote_amp($url) . '" name="ajax_redirect" style="display:none;">' . _T('navigateur_pas_redirige') . '</a>', |
|
| 140 | - // et un message au cas ou |
|
| 141 | - '<br /><a href="' . quote_amp($url) . '">' . _T('navigateur_pas_redirige') . '</a>' |
|
| 142 | - ); |
|
| 143 | - } else // format message texte, tout en js inline |
|
| 144 | - { |
|
| 145 | - return |
|
| 146 | - // ie poste les formulaires dans une iframe, il faut donc rediriger son parent |
|
| 147 | - "<script type='text/javascript'>if (parent.window){parent.window.document.location.replace(\"$url\");} else {document.location.replace(\"$url\");}</script>" |
|
| 148 | - . http_img_pack('searching.gif', '') |
|
| 149 | - . '<br />' |
|
| 150 | - . '<a href="' . quote_amp($url) . '">' . _T('navigateur_pas_redirige') . '</a>'; |
|
| 151 | - } |
|
| 152 | - } |
|
| 110 | + if (!_AJAX |
|
| 111 | + and !headers_sent() |
|
| 112 | + and !_request('var_ajax') |
|
| 113 | + ) { |
|
| 114 | + redirige_par_entete(str_replace('&', '&', $url), $equiv); |
|
| 115 | + } // si c'est une ancre, fixer simplement le window.location.hash |
|
| 116 | + elseif ($format == 'ajaxform' and preg_match(',^#[0-9a-z\-_]+$,i', $url)) { |
|
| 117 | + return array( |
|
| 118 | + // on renvoie un lien masque qui sera traite par ajaxCallback.js |
|
| 119 | + "<a href='$url' name='ajax_ancre' style='display:none;'>anchor</a>", |
|
| 120 | + // et rien dans le message ok |
|
| 121 | + '' |
|
| 122 | + ); |
|
| 123 | + } else { |
|
| 124 | + // ne pas laisser passer n'importe quoi dans l'url |
|
| 125 | + $url = str_replace(array('<', '"'), array('<', '"'), $url); |
|
| 126 | + |
|
| 127 | + $url = strtr($url, "\n\r", " "); |
|
| 128 | + # en theorie on devrait faire ca tout le temps, mais quand la chaine |
|
| 129 | + # commence par ? c'est imperatif, sinon l'url finale n'est pas la bonne |
|
| 130 | + if ($url[0] == '?') { |
|
| 131 | + $url = url_de_base() . $url; |
|
| 132 | + } |
|
| 133 | + $url = str_replace('&', '&', $url); |
|
| 134 | + spip_log("redirige formulaire ajax: $url"); |
|
| 135 | + include_spip('inc/filtres'); |
|
| 136 | + if ($format == 'ajaxform') { |
|
| 137 | + return array( |
|
| 138 | + // on renvoie un lien masque qui sera traite par ajaxCallback.js |
|
| 139 | + '<a href="' . quote_amp($url) . '" name="ajax_redirect" style="display:none;">' . _T('navigateur_pas_redirige') . '</a>', |
|
| 140 | + // et un message au cas ou |
|
| 141 | + '<br /><a href="' . quote_amp($url) . '">' . _T('navigateur_pas_redirige') . '</a>' |
|
| 142 | + ); |
|
| 143 | + } else // format message texte, tout en js inline |
|
| 144 | + { |
|
| 145 | + return |
|
| 146 | + // ie poste les formulaires dans une iframe, il faut donc rediriger son parent |
|
| 147 | + "<script type='text/javascript'>if (parent.window){parent.window.document.location.replace(\"$url\");} else {document.location.replace(\"$url\");}</script>" |
|
| 148 | + . http_img_pack('searching.gif', '') |
|
| 149 | + . '<br />' |
|
| 150 | + . '<a href="' . quote_amp($url) . '">' . _T('navigateur_pas_redirige') . '</a>'; |
|
| 151 | + } |
|
| 152 | + } |
|
| 153 | 153 | } |
| 154 | 154 | |
| 155 | 155 | /** |
@@ -170,7 +170,7 @@ discard block |
||
| 170 | 170 | * @return void |
| 171 | 171 | **/ |
| 172 | 172 | function redirige_url_ecrire($script = '', $args = '', $equiv = '') { |
| 173 | - return redirige_par_entete(generer_url_ecrire($script, $args, true), $equiv); |
|
| 173 | + return redirige_par_entete(generer_url_ecrire($script, $args, true), $equiv); |
|
| 174 | 174 | } |
| 175 | 175 | |
| 176 | 176 | /** |
@@ -185,49 +185,49 @@ discard block |
||
| 185 | 185 | **/ |
| 186 | 186 | function http_status($status) { |
| 187 | 187 | |
| 188 | - static $status_string = array( |
|
| 189 | - 200 => '200 OK', |
|
| 190 | - 204 => '204 No Content', |
|
| 191 | - 301 => '301 Moved Permanently', |
|
| 192 | - 302 => '302 Found', |
|
| 193 | - 304 => '304 Not Modified', |
|
| 194 | - 401 => '401 Unauthorized', |
|
| 195 | - 403 => '403 Forbidden', |
|
| 196 | - 404 => '404 Not Found', |
|
| 197 | - 503 => '503 Service Unavailable' |
|
| 198 | - ); |
|
| 199 | - |
|
| 200 | - if (!empty($GLOBALS['REDIRECT_STATUS']) && $GLOBALS['REDIRECT_STATUS'] == $status) { |
|
| 201 | - return; |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - $php_cgi = ($GLOBALS['flag_sapi_name'] and preg_match(",cgi,i", @php_sapi_name())); |
|
| 205 | - if ($php_cgi) { |
|
| 206 | - header("Status: " . $status_string[$status]); |
|
| 207 | - } else { |
|
| 208 | - header("HTTP/1.0 " . $status_string[$status]); |
|
| 209 | - } |
|
| 188 | + static $status_string = array( |
|
| 189 | + 200 => '200 OK', |
|
| 190 | + 204 => '204 No Content', |
|
| 191 | + 301 => '301 Moved Permanently', |
|
| 192 | + 302 => '302 Found', |
|
| 193 | + 304 => '304 Not Modified', |
|
| 194 | + 401 => '401 Unauthorized', |
|
| 195 | + 403 => '403 Forbidden', |
|
| 196 | + 404 => '404 Not Found', |
|
| 197 | + 503 => '503 Service Unavailable' |
|
| 198 | + ); |
|
| 199 | + |
|
| 200 | + if (!empty($GLOBALS['REDIRECT_STATUS']) && $GLOBALS['REDIRECT_STATUS'] == $status) { |
|
| 201 | + return; |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + $php_cgi = ($GLOBALS['flag_sapi_name'] and preg_match(",cgi,i", @php_sapi_name())); |
|
| 205 | + if ($php_cgi) { |
|
| 206 | + header("Status: " . $status_string[$status]); |
|
| 207 | + } else { |
|
| 208 | + header("HTTP/1.0 " . $status_string[$status]); |
|
| 209 | + } |
|
| 210 | 210 | } |
| 211 | 211 | |
| 212 | 212 | // Retourne ce qui va bien pour que le navigateur ne mette pas la page en cache |
| 213 | 213 | // http://code.spip.net/@http_no_cache |
| 214 | 214 | function http_no_cache() { |
| 215 | - if (headers_sent()) { |
|
| 216 | - spip_log("http_no_cache arrive trop tard"); |
|
| 217 | - |
|
| 218 | - return; |
|
| 219 | - } |
|
| 220 | - $charset = empty($GLOBALS['meta']['charset']) ? 'utf-8' : $GLOBALS['meta']['charset']; |
|
| 221 | - |
|
| 222 | - // selon http://developer.apple.com/internet/safari/faq.html#anchor5 |
|
| 223 | - // il faudrait aussi pour Safari |
|
| 224 | - // header("Cache-Control: post-check=0, pre-check=0", false) |
|
| 225 | - // mais ca ne respecte pas |
|
| 226 | - // http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9 |
|
| 227 | - |
|
| 228 | - header("Content-Type: text/html; charset=$charset"); |
|
| 229 | - header("Expires: 0"); |
|
| 230 | - header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); |
|
| 231 | - header("Cache-Control: no-cache, must-revalidate"); |
|
| 232 | - header("Pragma: no-cache"); |
|
| 215 | + if (headers_sent()) { |
|
| 216 | + spip_log("http_no_cache arrive trop tard"); |
|
| 217 | + |
|
| 218 | + return; |
|
| 219 | + } |
|
| 220 | + $charset = empty($GLOBALS['meta']['charset']) ? 'utf-8' : $GLOBALS['meta']['charset']; |
|
| 221 | + |
|
| 222 | + // selon http://developer.apple.com/internet/safari/faq.html#anchor5 |
|
| 223 | + // il faudrait aussi pour Safari |
|
| 224 | + // header("Cache-Control: post-check=0, pre-check=0", false) |
|
| 225 | + // mais ca ne respecte pas |
|
| 226 | + // http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9 |
|
| 227 | + |
|
| 228 | + header("Content-Type: text/html; charset=$charset"); |
|
| 229 | + header("Expires: 0"); |
|
| 230 | + header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); |
|
| 231 | + header("Cache-Control: no-cache, must-revalidate"); |
|
| 232 | + header("Pragma: no-cache"); |
|
| 233 | 233 | } |
@@ -72,12 +72,12 @@ discard block |
||
| 72 | 72 | or defined('_SERVER_APACHE') |
| 73 | 73 | ) |
| 74 | 74 | ) { |
| 75 | - @header("Location: " . $url); |
|
| 75 | + @header("Location: ".$url); |
|
| 76 | 76 | $equiv = ""; |
| 77 | 77 | } else { |
| 78 | - @header("Refresh: 0; url=" . $url); |
|
| 78 | + @header("Refresh: 0; url=".$url); |
|
| 79 | 79 | if (isset($GLOBALS['meta']['charset'])) { |
| 80 | - @header("Content-Type: text/html; charset=" . $GLOBALS['meta']['charset']); |
|
| 80 | + @header("Content-Type: text/html; charset=".$GLOBALS['meta']['charset']); |
|
| 81 | 81 | } |
| 82 | 82 | $equiv = "<meta http-equiv='Refresh' content='0; url=$url'>"; |
| 83 | 83 | } |
@@ -89,11 +89,11 @@ discard block |
||
| 89 | 89 | html_lang_attributes(), ' |
| 90 | 90 | <head>', |
| 91 | 91 | $equiv, ' |
| 92 | -<title>HTTP ' . $status . '</title> |
|
| 93 | -' . ((isset($GLOBALS['meta']['charset'])) ? '<meta http-equiv="Content-Type" content="text/html;charset=' . $GLOBALS['meta']['charset'] . '">' : '') . ' |
|
| 92 | +<title>HTTP ' . $status.'</title> |
|
| 93 | +' . ((isset($GLOBALS['meta']['charset'])) ? '<meta http-equiv="Content-Type" content="text/html;charset='.$GLOBALS['meta']['charset'].'">' : '').' |
|
| 94 | 94 | </head> |
| 95 | 95 | <body> |
| 96 | -<h1>HTTP ' . $status . '</h1> |
|
| 96 | +<h1>HTTP ' . $status.'</h1> |
|
| 97 | 97 | <a href="', |
| 98 | 98 | quote_amp($url), |
| 99 | 99 | '">', |
@@ -128,7 +128,7 @@ discard block |
||
| 128 | 128 | # en theorie on devrait faire ca tout le temps, mais quand la chaine |
| 129 | 129 | # commence par ? c'est imperatif, sinon l'url finale n'est pas la bonne |
| 130 | 130 | if ($url[0] == '?') { |
| 131 | - $url = url_de_base() . $url; |
|
| 131 | + $url = url_de_base().$url; |
|
| 132 | 132 | } |
| 133 | 133 | $url = str_replace('&', '&', $url); |
| 134 | 134 | spip_log("redirige formulaire ajax: $url"); |
@@ -136,9 +136,9 @@ discard block |
||
| 136 | 136 | if ($format == 'ajaxform') { |
| 137 | 137 | return array( |
| 138 | 138 | // on renvoie un lien masque qui sera traite par ajaxCallback.js |
| 139 | - '<a href="' . quote_amp($url) . '" name="ajax_redirect" style="display:none;">' . _T('navigateur_pas_redirige') . '</a>', |
|
| 139 | + '<a href="'.quote_amp($url).'" name="ajax_redirect" style="display:none;">'._T('navigateur_pas_redirige').'</a>', |
|
| 140 | 140 | // et un message au cas ou |
| 141 | - '<br /><a href="' . quote_amp($url) . '">' . _T('navigateur_pas_redirige') . '</a>' |
|
| 141 | + '<br /><a href="'.quote_amp($url).'">'._T('navigateur_pas_redirige').'</a>' |
|
| 142 | 142 | ); |
| 143 | 143 | } else // format message texte, tout en js inline |
| 144 | 144 | { |
@@ -147,7 +147,7 @@ discard block |
||
| 147 | 147 | "<script type='text/javascript'>if (parent.window){parent.window.document.location.replace(\"$url\");} else {document.location.replace(\"$url\");}</script>" |
| 148 | 148 | . http_img_pack('searching.gif', '') |
| 149 | 149 | . '<br />' |
| 150 | - . '<a href="' . quote_amp($url) . '">' . _T('navigateur_pas_redirige') . '</a>'; |
|
| 150 | + . '<a href="'.quote_amp($url).'">'._T('navigateur_pas_redirige').'</a>'; |
|
| 151 | 151 | } |
| 152 | 152 | } |
| 153 | 153 | } |
@@ -203,9 +203,9 @@ discard block |
||
| 203 | 203 | |
| 204 | 204 | $php_cgi = ($GLOBALS['flag_sapi_name'] and preg_match(",cgi,i", @php_sapi_name())); |
| 205 | 205 | if ($php_cgi) { |
| 206 | - header("Status: " . $status_string[$status]); |
|
| 206 | + header("Status: ".$status_string[$status]); |
|
| 207 | 207 | } else { |
| 208 | - header("HTTP/1.0 " . $status_string[$status]); |
|
| 208 | + header("HTTP/1.0 ".$status_string[$status]); |
|
| 209 | 209 | } |
| 210 | 210 | } |
| 211 | 211 | |
@@ -227,7 +227,7 @@ discard block |
||
| 227 | 227 | |
| 228 | 228 | header("Content-Type: text/html; charset=$charset"); |
| 229 | 229 | header("Expires: 0"); |
| 230 | - header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); |
|
| 230 | + header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT"); |
|
| 231 | 231 | header("Cache-Control: no-cache, must-revalidate"); |
| 232 | 232 | header("Pragma: no-cache"); |
| 233 | 233 | } |
@@ -16,32 +16,32 @@ discard block |
||
| 16 | 16 | * @package SPIP\Core\Distant |
| 17 | 17 | **/ |
| 18 | 18 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 19 | - return; |
|
| 19 | + return; |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | if (!defined('_INC_DISTANT_VERSION_HTTP')) { |
| 23 | - define('_INC_DISTANT_VERSION_HTTP', 'HTTP/1.0'); |
|
| 23 | + define('_INC_DISTANT_VERSION_HTTP', 'HTTP/1.0'); |
|
| 24 | 24 | } |
| 25 | 25 | if (!defined('_INC_DISTANT_CONTENT_ENCODING')) { |
| 26 | - define('_INC_DISTANT_CONTENT_ENCODING', 'gzip'); |
|
| 26 | + define('_INC_DISTANT_CONTENT_ENCODING', 'gzip'); |
|
| 27 | 27 | } |
| 28 | 28 | if (!defined('_INC_DISTANT_USER_AGENT')) { |
| 29 | - define('_INC_DISTANT_USER_AGENT', 'SPIP-' . $GLOBALS['spip_version_affichee'] . ' (' . $GLOBALS['home_server'] . ')'); |
|
| 29 | + define('_INC_DISTANT_USER_AGENT', 'SPIP-' . $GLOBALS['spip_version_affichee'] . ' (' . $GLOBALS['home_server'] . ')'); |
|
| 30 | 30 | } |
| 31 | 31 | if (!defined('_INC_DISTANT_MAX_SIZE')) { |
| 32 | - define('_INC_DISTANT_MAX_SIZE', 2097152); |
|
| 32 | + define('_INC_DISTANT_MAX_SIZE', 2097152); |
|
| 33 | 33 | } |
| 34 | 34 | if (!defined('_INC_DISTANT_CONNECT_TIMEOUT')) { |
| 35 | - define('_INC_DISTANT_CONNECT_TIMEOUT', 10); |
|
| 35 | + define('_INC_DISTANT_CONNECT_TIMEOUT', 10); |
|
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | define('_REGEXP_COPIE_LOCALE', ',' . |
| 39 | - preg_replace( |
|
| 40 | - '@^https?:@', |
|
| 41 | - 'https?:', |
|
| 42 | - (isset($GLOBALS['meta']['adresse_site']) ? $GLOBALS['meta']['adresse_site'] : '') |
|
| 43 | - ) |
|
| 44 | - . '/?spip.php[?]action=acceder_document.*file=(.*)$,'); |
|
| 39 | + preg_replace( |
|
| 40 | + '@^https?:@', |
|
| 41 | + 'https?:', |
|
| 42 | + (isset($GLOBALS['meta']['adresse_site']) ? $GLOBALS['meta']['adresse_site'] : '') |
|
| 43 | + ) |
|
| 44 | + . '/?spip.php[?]action=acceder_document.*file=(.*)$,'); |
|
| 45 | 45 | |
| 46 | 46 | //@define('_COPIE_LOCALE_MAX_SIZE',2097152); // poids (inc/utils l'a fait) |
| 47 | 47 | |
@@ -68,77 +68,77 @@ discard block |
||
| 68 | 68 | */ |
| 69 | 69 | function copie_locale($source, $mode = 'auto', $local = null, $taille_max = null) { |
| 70 | 70 | |
| 71 | - // si c'est la protection de soi-meme, retourner le path |
|
| 72 | - if ($mode !== 'force' and preg_match(_REGEXP_COPIE_LOCALE, $source, $match)) { |
|
| 73 | - $source = substr(_DIR_IMG, strlen(_DIR_RACINE)) . urldecode($match[1]); |
|
| 74 | - |
|
| 75 | - return @file_exists($source) ? $source : false; |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - if (is_null($local)) { |
|
| 79 | - $local = fichier_copie_locale($source); |
|
| 80 | - } else { |
|
| 81 | - if (_DIR_RACINE and strncmp(_DIR_RACINE, $local, strlen(_DIR_RACINE)) == 0) { |
|
| 82 | - $local = substr($local, strlen(_DIR_RACINE)); |
|
| 83 | - } |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - // si $local = '' c'est un fichier refuse par fichier_copie_locale(), |
|
| 87 | - // par exemple un fichier qui ne figure pas dans nos documents ; |
|
| 88 | - // dans ce cas on n'essaie pas de le telecharger pour ensuite echouer |
|
| 89 | - if (!$local) { |
|
| 90 | - return false; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - $localrac = _DIR_RACINE . $local; |
|
| 94 | - $t = ($mode == 'force') ? false : @file_exists($localrac); |
|
| 95 | - |
|
| 96 | - // test d'existence du fichier |
|
| 97 | - if ($mode == 'test') { |
|
| 98 | - return $t ? $local : ''; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - // sinon voir si on doit/peut le telecharger |
|
| 102 | - if ($local == $source or !tester_url_absolue($source)) { |
|
| 103 | - return $local; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - if ($mode == 'modif' or !$t) { |
|
| 107 | - // passer par un fichier temporaire unique pour gerer les echecs en cours de recuperation |
|
| 108 | - // et des eventuelles recuperations concurantes |
|
| 109 | - include_spip('inc/acces'); |
|
| 110 | - if (!$taille_max) { |
|
| 111 | - $taille_max = _COPIE_LOCALE_MAX_SIZE; |
|
| 112 | - } |
|
| 113 | - $res = recuperer_url( |
|
| 114 | - $source, |
|
| 115 | - array('file' => $localrac, 'taille_max' => $taille_max, 'if_modified_since' => $t ? filemtime($localrac) : '') |
|
| 116 | - ); |
|
| 117 | - if (!$res or (!$res['length'] and $res['status'] != 304)) { |
|
| 118 | - spip_log("copie_locale : Echec recuperation $source sur $localrac status : " . $res['status'], _LOG_INFO_IMPORTANTE); |
|
| 119 | - } |
|
| 120 | - if (!$res['length']) { |
|
| 121 | - // si $t c'est sans doute juste un not-modified-since |
|
| 122 | - return $t ? $local : false; |
|
| 123 | - } |
|
| 124 | - spip_log("copie_locale : recuperation $source sur $localrac taille " . $res['length'] . ' OK'); |
|
| 125 | - |
|
| 126 | - // pour une eventuelle indexation |
|
| 127 | - pipeline( |
|
| 128 | - 'post_edition', |
|
| 129 | - array( |
|
| 130 | - 'args' => array( |
|
| 131 | - 'operation' => 'copie_locale', |
|
| 132 | - 'source' => $source, |
|
| 133 | - 'fichier' => $local, |
|
| 134 | - 'http_res' => $res['length'], |
|
| 135 | - ), |
|
| 136 | - 'data' => null |
|
| 137 | - ) |
|
| 138 | - ); |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - return $local; |
|
| 71 | + // si c'est la protection de soi-meme, retourner le path |
|
| 72 | + if ($mode !== 'force' and preg_match(_REGEXP_COPIE_LOCALE, $source, $match)) { |
|
| 73 | + $source = substr(_DIR_IMG, strlen(_DIR_RACINE)) . urldecode($match[1]); |
|
| 74 | + |
|
| 75 | + return @file_exists($source) ? $source : false; |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + if (is_null($local)) { |
|
| 79 | + $local = fichier_copie_locale($source); |
|
| 80 | + } else { |
|
| 81 | + if (_DIR_RACINE and strncmp(_DIR_RACINE, $local, strlen(_DIR_RACINE)) == 0) { |
|
| 82 | + $local = substr($local, strlen(_DIR_RACINE)); |
|
| 83 | + } |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + // si $local = '' c'est un fichier refuse par fichier_copie_locale(), |
|
| 87 | + // par exemple un fichier qui ne figure pas dans nos documents ; |
|
| 88 | + // dans ce cas on n'essaie pas de le telecharger pour ensuite echouer |
|
| 89 | + if (!$local) { |
|
| 90 | + return false; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + $localrac = _DIR_RACINE . $local; |
|
| 94 | + $t = ($mode == 'force') ? false : @file_exists($localrac); |
|
| 95 | + |
|
| 96 | + // test d'existence du fichier |
|
| 97 | + if ($mode == 'test') { |
|
| 98 | + return $t ? $local : ''; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + // sinon voir si on doit/peut le telecharger |
|
| 102 | + if ($local == $source or !tester_url_absolue($source)) { |
|
| 103 | + return $local; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + if ($mode == 'modif' or !$t) { |
|
| 107 | + // passer par un fichier temporaire unique pour gerer les echecs en cours de recuperation |
|
| 108 | + // et des eventuelles recuperations concurantes |
|
| 109 | + include_spip('inc/acces'); |
|
| 110 | + if (!$taille_max) { |
|
| 111 | + $taille_max = _COPIE_LOCALE_MAX_SIZE; |
|
| 112 | + } |
|
| 113 | + $res = recuperer_url( |
|
| 114 | + $source, |
|
| 115 | + array('file' => $localrac, 'taille_max' => $taille_max, 'if_modified_since' => $t ? filemtime($localrac) : '') |
|
| 116 | + ); |
|
| 117 | + if (!$res or (!$res['length'] and $res['status'] != 304)) { |
|
| 118 | + spip_log("copie_locale : Echec recuperation $source sur $localrac status : " . $res['status'], _LOG_INFO_IMPORTANTE); |
|
| 119 | + } |
|
| 120 | + if (!$res['length']) { |
|
| 121 | + // si $t c'est sans doute juste un not-modified-since |
|
| 122 | + return $t ? $local : false; |
|
| 123 | + } |
|
| 124 | + spip_log("copie_locale : recuperation $source sur $localrac taille " . $res['length'] . ' OK'); |
|
| 125 | + |
|
| 126 | + // pour une eventuelle indexation |
|
| 127 | + pipeline( |
|
| 128 | + 'post_edition', |
|
| 129 | + array( |
|
| 130 | + 'args' => array( |
|
| 131 | + 'operation' => 'copie_locale', |
|
| 132 | + 'source' => $source, |
|
| 133 | + 'fichier' => $local, |
|
| 134 | + 'http_res' => $res['length'], |
|
| 135 | + ), |
|
| 136 | + 'data' => null |
|
| 137 | + ) |
|
| 138 | + ); |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + return $local; |
|
| 142 | 142 | } |
| 143 | 143 | |
| 144 | 144 | /** |
@@ -153,88 +153,88 @@ discard block |
||
| 153 | 153 | * url ou false en cas d'echec |
| 154 | 154 | */ |
| 155 | 155 | function valider_url_distante($url, $known_hosts = array()) { |
| 156 | - if (!function_exists('protocole_verifier')){ |
|
| 157 | - include_spip('inc/filtres_mini'); |
|
| 158 | - } |
|
| 156 | + if (!function_exists('protocole_verifier')){ |
|
| 157 | + include_spip('inc/filtres_mini'); |
|
| 158 | + } |
|
| 159 | 159 | |
| 160 | - if (!protocole_verifier($url, array('http', 'https'))) { |
|
| 161 | - return false; |
|
| 162 | - } |
|
| 160 | + if (!protocole_verifier($url, array('http', 'https'))) { |
|
| 161 | + return false; |
|
| 162 | + } |
|
| 163 | 163 | |
| 164 | - $parsed_url = parse_url($url); |
|
| 165 | - if (!$parsed_url or empty($parsed_url['host']) ) { |
|
| 166 | - return false; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - if (isset($parsed_url['user']) or isset($parsed_url['pass'])) { |
|
| 170 | - return false; |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - if (false !== strpbrk($parsed_url['host'], ':#?[]')) { |
|
| 174 | - return false; |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - if (!is_array($known_hosts)) { |
|
| 178 | - $known_hosts = array($known_hosts); |
|
| 179 | - } |
|
| 180 | - $known_hosts[] = $GLOBALS['meta']['adresse_site']; |
|
| 181 | - $known_hosts[] = url_de_base(); |
|
| 182 | - $known_hosts = pipeline('declarer_hosts_distants', $known_hosts); |
|
| 183 | - |
|
| 184 | - $is_known_host = false; |
|
| 185 | - foreach ($known_hosts as $known_host) { |
|
| 186 | - $parse_known = parse_url($known_host); |
|
| 187 | - if ($parse_known |
|
| 188 | - and strtolower($parse_known['host']) === strtolower($parsed_url['host'])) { |
|
| 189 | - $is_known_host = true; |
|
| 190 | - break; |
|
| 191 | - } |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - if (!$is_known_host) { |
|
| 195 | - $host = trim($parsed_url['host'], '.'); |
|
| 196 | - if (preg_match('#^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$#', $host)) { |
|
| 197 | - $ip = $host; |
|
| 198 | - } else { |
|
| 199 | - $ip = gethostbyname($host); |
|
| 200 | - if ($ip === $host) { |
|
| 201 | - // Error condition for gethostbyname() |
|
| 202 | - $ip = false; |
|
| 203 | - } |
|
| 204 | - } |
|
| 205 | - if ($ip) { |
|
| 206 | - $parts = array_map('intval', explode( '.', $ip )); |
|
| 207 | - if (127 === $parts[0] or 10 === $parts[0] or 0 === $parts[0] |
|
| 208 | - or ( 172 === $parts[0] and 16 <= $parts[1] and 31 >= $parts[1] ) |
|
| 209 | - or ( 192 === $parts[0] && 168 === $parts[1] ) |
|
| 210 | - ) { |
|
| 211 | - return false; |
|
| 212 | - } |
|
| 213 | - } |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - if (empty($parsed_url['port'])) { |
|
| 217 | - return $url; |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - $port = $parsed_url['port']; |
|
| 221 | - if ($port === 80 or $port === 443 or $port === 8080) { |
|
| 222 | - return $url; |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - if ($is_known_host) { |
|
| 226 | - foreach ($known_hosts as $known_host) { |
|
| 227 | - $parse_known = parse_url($known_host); |
|
| 228 | - if ($parse_known |
|
| 229 | - and !empty($parse_known['port']) |
|
| 230 | - and strtolower($parse_known['host']) === strtolower($parsed_url['host']) |
|
| 231 | - and $parse_known['port'] == $port) { |
|
| 232 | - return $url; |
|
| 233 | - } |
|
| 234 | - } |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - return false; |
|
| 164 | + $parsed_url = parse_url($url); |
|
| 165 | + if (!$parsed_url or empty($parsed_url['host']) ) { |
|
| 166 | + return false; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + if (isset($parsed_url['user']) or isset($parsed_url['pass'])) { |
|
| 170 | + return false; |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + if (false !== strpbrk($parsed_url['host'], ':#?[]')) { |
|
| 174 | + return false; |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + if (!is_array($known_hosts)) { |
|
| 178 | + $known_hosts = array($known_hosts); |
|
| 179 | + } |
|
| 180 | + $known_hosts[] = $GLOBALS['meta']['adresse_site']; |
|
| 181 | + $known_hosts[] = url_de_base(); |
|
| 182 | + $known_hosts = pipeline('declarer_hosts_distants', $known_hosts); |
|
| 183 | + |
|
| 184 | + $is_known_host = false; |
|
| 185 | + foreach ($known_hosts as $known_host) { |
|
| 186 | + $parse_known = parse_url($known_host); |
|
| 187 | + if ($parse_known |
|
| 188 | + and strtolower($parse_known['host']) === strtolower($parsed_url['host'])) { |
|
| 189 | + $is_known_host = true; |
|
| 190 | + break; |
|
| 191 | + } |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + if (!$is_known_host) { |
|
| 195 | + $host = trim($parsed_url['host'], '.'); |
|
| 196 | + if (preg_match('#^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$#', $host)) { |
|
| 197 | + $ip = $host; |
|
| 198 | + } else { |
|
| 199 | + $ip = gethostbyname($host); |
|
| 200 | + if ($ip === $host) { |
|
| 201 | + // Error condition for gethostbyname() |
|
| 202 | + $ip = false; |
|
| 203 | + } |
|
| 204 | + } |
|
| 205 | + if ($ip) { |
|
| 206 | + $parts = array_map('intval', explode( '.', $ip )); |
|
| 207 | + if (127 === $parts[0] or 10 === $parts[0] or 0 === $parts[0] |
|
| 208 | + or ( 172 === $parts[0] and 16 <= $parts[1] and 31 >= $parts[1] ) |
|
| 209 | + or ( 192 === $parts[0] && 168 === $parts[1] ) |
|
| 210 | + ) { |
|
| 211 | + return false; |
|
| 212 | + } |
|
| 213 | + } |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + if (empty($parsed_url['port'])) { |
|
| 217 | + return $url; |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + $port = $parsed_url['port']; |
|
| 221 | + if ($port === 80 or $port === 443 or $port === 8080) { |
|
| 222 | + return $url; |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + if ($is_known_host) { |
|
| 226 | + foreach ($known_hosts as $known_host) { |
|
| 227 | + $parse_known = parse_url($known_host); |
|
| 228 | + if ($parse_known |
|
| 229 | + and !empty($parse_known['port']) |
|
| 230 | + and strtolower($parse_known['host']) === strtolower($parsed_url['host']) |
|
| 231 | + and $parse_known['port'] == $port) { |
|
| 232 | + return $url; |
|
| 233 | + } |
|
| 234 | + } |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + return false; |
|
| 238 | 238 | } |
| 239 | 239 | |
| 240 | 240 | /** |
@@ -253,86 +253,86 @@ discard block |
||
| 253 | 253 | */ |
| 254 | 254 | function prepare_donnees_post($donnees, $boundary = '') { |
| 255 | 255 | |
| 256 | - // permettre a la fonction qui a demande le post de formater elle meme ses donnees |
|
| 257 | - // pour un appel soap par exemple |
|
| 258 | - // l'entete est separe des donnees par un double retour a la ligne |
|
| 259 | - // on s'occupe ici de passer tous les retours lignes (\r\n, \r ou \n) en \r\n |
|
| 260 | - if (is_string($donnees) && strlen($donnees)) { |
|
| 261 | - $entete = ''; |
|
| 262 | - // on repasse tous les \r\n et \r en simples \n |
|
| 263 | - $donnees = str_replace("\r\n", "\n", $donnees); |
|
| 264 | - $donnees = str_replace("\r", "\n", $donnees); |
|
| 265 | - // un double retour a la ligne signifie la fin de l'entete et le debut des donnees |
|
| 266 | - $p = strpos($donnees, "\n\n"); |
|
| 267 | - if ($p !== false) { |
|
| 268 | - $entete = str_replace("\n", "\r\n", substr($donnees, 0, $p + 1)); |
|
| 269 | - $donnees = substr($donnees, $p + 2); |
|
| 270 | - } |
|
| 271 | - $chaine = str_replace("\n", "\r\n", $donnees); |
|
| 272 | - } else { |
|
| 273 | - /* boundary automatique */ |
|
| 274 | - // Si on a plus de 500 octects de donnees, on "boundarise" |
|
| 275 | - if ($boundary === '') { |
|
| 276 | - $taille = 0; |
|
| 277 | - foreach ($donnees as $cle => $valeur) { |
|
| 278 | - if (is_array($valeur)) { |
|
| 279 | - foreach ($valeur as $val2) { |
|
| 280 | - $taille += strlen($val2); |
|
| 281 | - } |
|
| 282 | - } else { |
|
| 283 | - // faut-il utiliser spip_strlen() dans inc/charsets ? |
|
| 284 | - $taille += strlen($valeur); |
|
| 285 | - } |
|
| 286 | - } |
|
| 287 | - if ($taille > 500) { |
|
| 288 | - $boundary = substr(md5(rand() . 'spip'), 0, 8); |
|
| 289 | - } |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - if (is_string($boundary) and strlen($boundary)) { |
|
| 293 | - // fabrique une chaine HTTP pour un POST avec boundary |
|
| 294 | - $entete = "Content-Type: multipart/form-data; boundary=$boundary\r\n"; |
|
| 295 | - $chaine = ''; |
|
| 296 | - if (is_array($donnees)) { |
|
| 297 | - foreach ($donnees as $cle => $valeur) { |
|
| 298 | - if (is_array($valeur)) { |
|
| 299 | - foreach ($valeur as $val2) { |
|
| 300 | - $chaine .= "\r\n--$boundary\r\n"; |
|
| 301 | - $chaine .= "Content-Disposition: form-data; name=\"{$cle}[]\"\r\n"; |
|
| 302 | - $chaine .= "\r\n"; |
|
| 303 | - $chaine .= $val2; |
|
| 304 | - } |
|
| 305 | - } else { |
|
| 306 | - $chaine .= "\r\n--$boundary\r\n"; |
|
| 307 | - $chaine .= "Content-Disposition: form-data; name=\"$cle\"\r\n"; |
|
| 308 | - $chaine .= "\r\n"; |
|
| 309 | - $chaine .= $valeur; |
|
| 310 | - } |
|
| 311 | - } |
|
| 312 | - $chaine .= "\r\n--$boundary\r\n"; |
|
| 313 | - } |
|
| 314 | - } else { |
|
| 315 | - // fabrique une chaine HTTP simple pour un POST |
|
| 316 | - $entete = 'Content-Type: application/x-www-form-urlencoded' . "\r\n"; |
|
| 317 | - $chaine = array(); |
|
| 318 | - if (is_array($donnees)) { |
|
| 319 | - foreach ($donnees as $cle => $valeur) { |
|
| 320 | - if (is_array($valeur)) { |
|
| 321 | - foreach ($valeur as $val2) { |
|
| 322 | - $chaine[] = rawurlencode($cle) . '[]=' . rawurlencode($val2); |
|
| 323 | - } |
|
| 324 | - } else { |
|
| 325 | - $chaine[] = rawurlencode($cle) . '=' . rawurlencode($valeur); |
|
| 326 | - } |
|
| 327 | - } |
|
| 328 | - $chaine = implode('&', $chaine); |
|
| 329 | - } else { |
|
| 330 | - $chaine = $donnees; |
|
| 331 | - } |
|
| 332 | - } |
|
| 333 | - } |
|
| 334 | - |
|
| 335 | - return array($entete, $chaine); |
|
| 256 | + // permettre a la fonction qui a demande le post de formater elle meme ses donnees |
|
| 257 | + // pour un appel soap par exemple |
|
| 258 | + // l'entete est separe des donnees par un double retour a la ligne |
|
| 259 | + // on s'occupe ici de passer tous les retours lignes (\r\n, \r ou \n) en \r\n |
|
| 260 | + if (is_string($donnees) && strlen($donnees)) { |
|
| 261 | + $entete = ''; |
|
| 262 | + // on repasse tous les \r\n et \r en simples \n |
|
| 263 | + $donnees = str_replace("\r\n", "\n", $donnees); |
|
| 264 | + $donnees = str_replace("\r", "\n", $donnees); |
|
| 265 | + // un double retour a la ligne signifie la fin de l'entete et le debut des donnees |
|
| 266 | + $p = strpos($donnees, "\n\n"); |
|
| 267 | + if ($p !== false) { |
|
| 268 | + $entete = str_replace("\n", "\r\n", substr($donnees, 0, $p + 1)); |
|
| 269 | + $donnees = substr($donnees, $p + 2); |
|
| 270 | + } |
|
| 271 | + $chaine = str_replace("\n", "\r\n", $donnees); |
|
| 272 | + } else { |
|
| 273 | + /* boundary automatique */ |
|
| 274 | + // Si on a plus de 500 octects de donnees, on "boundarise" |
|
| 275 | + if ($boundary === '') { |
|
| 276 | + $taille = 0; |
|
| 277 | + foreach ($donnees as $cle => $valeur) { |
|
| 278 | + if (is_array($valeur)) { |
|
| 279 | + foreach ($valeur as $val2) { |
|
| 280 | + $taille += strlen($val2); |
|
| 281 | + } |
|
| 282 | + } else { |
|
| 283 | + // faut-il utiliser spip_strlen() dans inc/charsets ? |
|
| 284 | + $taille += strlen($valeur); |
|
| 285 | + } |
|
| 286 | + } |
|
| 287 | + if ($taille > 500) { |
|
| 288 | + $boundary = substr(md5(rand() . 'spip'), 0, 8); |
|
| 289 | + } |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + if (is_string($boundary) and strlen($boundary)) { |
|
| 293 | + // fabrique une chaine HTTP pour un POST avec boundary |
|
| 294 | + $entete = "Content-Type: multipart/form-data; boundary=$boundary\r\n"; |
|
| 295 | + $chaine = ''; |
|
| 296 | + if (is_array($donnees)) { |
|
| 297 | + foreach ($donnees as $cle => $valeur) { |
|
| 298 | + if (is_array($valeur)) { |
|
| 299 | + foreach ($valeur as $val2) { |
|
| 300 | + $chaine .= "\r\n--$boundary\r\n"; |
|
| 301 | + $chaine .= "Content-Disposition: form-data; name=\"{$cle}[]\"\r\n"; |
|
| 302 | + $chaine .= "\r\n"; |
|
| 303 | + $chaine .= $val2; |
|
| 304 | + } |
|
| 305 | + } else { |
|
| 306 | + $chaine .= "\r\n--$boundary\r\n"; |
|
| 307 | + $chaine .= "Content-Disposition: form-data; name=\"$cle\"\r\n"; |
|
| 308 | + $chaine .= "\r\n"; |
|
| 309 | + $chaine .= $valeur; |
|
| 310 | + } |
|
| 311 | + } |
|
| 312 | + $chaine .= "\r\n--$boundary\r\n"; |
|
| 313 | + } |
|
| 314 | + } else { |
|
| 315 | + // fabrique une chaine HTTP simple pour un POST |
|
| 316 | + $entete = 'Content-Type: application/x-www-form-urlencoded' . "\r\n"; |
|
| 317 | + $chaine = array(); |
|
| 318 | + if (is_array($donnees)) { |
|
| 319 | + foreach ($donnees as $cle => $valeur) { |
|
| 320 | + if (is_array($valeur)) { |
|
| 321 | + foreach ($valeur as $val2) { |
|
| 322 | + $chaine[] = rawurlencode($cle) . '[]=' . rawurlencode($val2); |
|
| 323 | + } |
|
| 324 | + } else { |
|
| 325 | + $chaine[] = rawurlencode($cle) . '=' . rawurlencode($valeur); |
|
| 326 | + } |
|
| 327 | + } |
|
| 328 | + $chaine = implode('&', $chaine); |
|
| 329 | + } else { |
|
| 330 | + $chaine = $donnees; |
|
| 331 | + } |
|
| 332 | + } |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + return array($entete, $chaine); |
|
| 336 | 336 | } |
| 337 | 337 | |
| 338 | 338 | /** |
@@ -346,18 +346,18 @@ discard block |
||
| 346 | 346 | */ |
| 347 | 347 | function url_to_ascii($url_idn) { |
| 348 | 348 | |
| 349 | - if ($parts = parse_url($url_idn)) { |
|
| 350 | - $host = $parts['host']; |
|
| 351 | - if (!preg_match(',^[a-z0-9_\.\-]+$,i', $host)) { |
|
| 352 | - include_spip('inc/idna_convert.class'); |
|
| 353 | - $IDN = new idna_convert(); |
|
| 354 | - $host_ascii = $IDN->encode($host); |
|
| 355 | - $url_idn = explode($host, $url_idn, 2); |
|
| 356 | - $url_idn = implode($host_ascii, $url_idn); |
|
| 357 | - } |
|
| 358 | - } |
|
| 359 | - |
|
| 360 | - return $url_idn; |
|
| 349 | + if ($parts = parse_url($url_idn)) { |
|
| 350 | + $host = $parts['host']; |
|
| 351 | + if (!preg_match(',^[a-z0-9_\.\-]+$,i', $host)) { |
|
| 352 | + include_spip('inc/idna_convert.class'); |
|
| 353 | + $IDN = new idna_convert(); |
|
| 354 | + $host_ascii = $IDN->encode($host); |
|
| 355 | + $url_idn = explode($host, $url_idn, 2); |
|
| 356 | + $url_idn = implode($host_ascii, $url_idn); |
|
| 357 | + } |
|
| 358 | + } |
|
| 359 | + |
|
| 360 | + return $url_idn; |
|
| 361 | 361 | } |
| 362 | 362 | |
| 363 | 363 | /** |
@@ -396,172 +396,172 @@ discard block |
||
| 396 | 396 | * string file : nom du fichier si enregistre dans un fichier |
| 397 | 397 | */ |
| 398 | 398 | function recuperer_url($url, $options = array()) { |
| 399 | - $default = array( |
|
| 400 | - 'transcoder' => false, |
|
| 401 | - 'methode' => 'GET', |
|
| 402 | - 'taille_max' => null, |
|
| 403 | - 'datas' => '', |
|
| 404 | - 'boundary' => '', |
|
| 405 | - 'refuser_gz' => false, |
|
| 406 | - 'if_modified_since' => '', |
|
| 407 | - 'uri_referer' => '', |
|
| 408 | - 'file' => '', |
|
| 409 | - 'follow_location' => 10, |
|
| 410 | - 'version_http' => _INC_DISTANT_VERSION_HTTP, |
|
| 411 | - ); |
|
| 412 | - $options = array_merge($default, $options); |
|
| 413 | - // copier directement dans un fichier ? |
|
| 414 | - $copy = $options['file']; |
|
| 415 | - |
|
| 416 | - if ($options['methode'] == 'HEAD') { |
|
| 417 | - $options['taille_max'] = 0; |
|
| 418 | - } |
|
| 419 | - if (is_null($options['taille_max'])) { |
|
| 420 | - $options['taille_max'] = $copy ? _COPIE_LOCALE_MAX_SIZE : _INC_DISTANT_MAX_SIZE; |
|
| 421 | - } |
|
| 422 | - |
|
| 423 | - if (!empty($options['datas'])) { |
|
| 424 | - list($head, $postdata) = prepare_donnees_post($options['datas'], $options['boundary']); |
|
| 425 | - if (stripos($head, 'Content-Length:') === false) { |
|
| 426 | - $head .= 'Content-Length: ' . strlen($postdata); |
|
| 427 | - } |
|
| 428 | - $options['datas'] = $head . "\r\n\r\n" . $postdata; |
|
| 429 | - if (strlen($postdata)) { |
|
| 430 | - $options['methode'] = 'POST'; |
|
| 431 | - } |
|
| 432 | - } |
|
| 433 | - |
|
| 434 | - // Accepter les URLs au format feed:// ou qui ont oublie le http:// ou les urls relatives au protocole |
|
| 435 | - $url = preg_replace(',^feed://,i', 'http://', $url); |
|
| 436 | - if (!tester_url_absolue($url)) { |
|
| 437 | - $url = 'http://' . $url; |
|
| 438 | - } elseif (strncmp($url, '//', 2) == 0) { |
|
| 439 | - $url = 'http:' . $url; |
|
| 440 | - } |
|
| 441 | - |
|
| 442 | - $url = url_to_ascii($url); |
|
| 443 | - |
|
| 444 | - $result = array( |
|
| 445 | - 'status' => 0, |
|
| 446 | - 'headers' => '', |
|
| 447 | - 'page' => '', |
|
| 448 | - 'length' => 0, |
|
| 449 | - 'last_modified' => '', |
|
| 450 | - 'location' => '', |
|
| 451 | - 'url' => $url |
|
| 452 | - ); |
|
| 453 | - |
|
| 454 | - // si on ecrit directement dans un fichier, pour ne pas manipuler en memoire refuser gz |
|
| 455 | - $refuser_gz = (($options['refuser_gz'] or $copy) ? true : false); |
|
| 456 | - |
|
| 457 | - // ouvrir la connexion et envoyer la requete et ses en-tetes |
|
| 458 | - list($handle, $fopen) = init_http( |
|
| 459 | - $options['methode'], |
|
| 460 | - $url, |
|
| 461 | - $refuser_gz, |
|
| 462 | - $options['uri_referer'], |
|
| 463 | - $options['datas'], |
|
| 464 | - $options['version_http'], |
|
| 465 | - $options['if_modified_since'] |
|
| 466 | - ); |
|
| 467 | - if (!$handle) { |
|
| 468 | - spip_log("ECHEC init_http $url"); |
|
| 469 | - |
|
| 470 | - return false; |
|
| 471 | - } |
|
| 472 | - |
|
| 473 | - // Sauf en fopen, envoyer le flux d'entree |
|
| 474 | - // et recuperer les en-tetes de reponses |
|
| 475 | - if (!$fopen) { |
|
| 476 | - $res = recuperer_entetes_complets($handle, $options['if_modified_since']); |
|
| 477 | - if (!$res) { |
|
| 478 | - fclose($handle); |
|
| 479 | - $t = @parse_url($url); |
|
| 480 | - $host = $t['host']; |
|
| 481 | - // Chinoisierie inexplicable pour contrer |
|
| 482 | - // les actions liberticides de l'empire du milieu |
|
| 483 | - if (!need_proxy($host) |
|
| 484 | - and $res = @file_get_contents($url) |
|
| 485 | - ) { |
|
| 486 | - $result['length'] = strlen($res); |
|
| 487 | - if ($copy) { |
|
| 488 | - ecrire_fichier($copy, $res); |
|
| 489 | - $result['file'] = $copy; |
|
| 490 | - } else { |
|
| 491 | - $result['page'] = $res; |
|
| 492 | - } |
|
| 493 | - $res = array( |
|
| 494 | - 'status' => 200, |
|
| 495 | - ); |
|
| 496 | - } else { |
|
| 497 | - return false; |
|
| 498 | - } |
|
| 499 | - } elseif ($res['location'] and $options['follow_location']) { |
|
| 500 | - $options['follow_location']--; |
|
| 501 | - fclose($handle); |
|
| 502 | - include_spip('inc/filtres'); |
|
| 503 | - $url = suivre_lien($url, $res['location']); |
|
| 504 | - spip_log("recuperer_url recommence sur $url"); |
|
| 505 | - |
|
| 506 | - return recuperer_url($url, $options); |
|
| 507 | - } elseif ($res['status'] !== 200) { |
|
| 508 | - spip_log('HTTP status ' . $res['status'] . " pour $url"); |
|
| 509 | - } |
|
| 510 | - $result['status'] = $res['status']; |
|
| 511 | - if (isset($res['headers'])) { |
|
| 512 | - $result['headers'] = $res['headers']; |
|
| 513 | - } |
|
| 514 | - if (isset($res['last_modified'])) { |
|
| 515 | - $result['last_modified'] = $res['last_modified']; |
|
| 516 | - } |
|
| 517 | - if (isset($res['location'])) { |
|
| 518 | - $result['location'] = $res['location']; |
|
| 519 | - } |
|
| 520 | - } |
|
| 521 | - |
|
| 522 | - // on ne veut que les entetes |
|
| 523 | - if (!$options['taille_max'] or $options['methode'] == 'HEAD' or $result['status'] == '304') { |
|
| 524 | - return $result; |
|
| 525 | - } |
|
| 526 | - |
|
| 527 | - |
|
| 528 | - // s'il faut deballer, le faire via un fichier temporaire |
|
| 529 | - // sinon la memoire explose pour les gros flux |
|
| 530 | - |
|
| 531 | - $gz = false; |
|
| 532 | - if (preg_match(",\bContent-Encoding: .*gzip,is", $result['headers'])) { |
|
| 533 | - $gz = (_DIR_TMP . md5(uniqid(mt_rand())) . '.tmp.gz'); |
|
| 534 | - } |
|
| 535 | - |
|
| 536 | - // si on a pas deja recuperer le contenu par une methode detournee |
|
| 537 | - if (!$result['length']) { |
|
| 538 | - $res = recuperer_body($handle, $options['taille_max'], $gz ? $gz : $copy); |
|
| 539 | - fclose($handle); |
|
| 540 | - if ($copy) { |
|
| 541 | - $result['length'] = $res; |
|
| 542 | - $result['file'] = $copy; |
|
| 543 | - } elseif ($res) { |
|
| 544 | - $result['page'] = &$res; |
|
| 545 | - $result['length'] = strlen($result['page']); |
|
| 546 | - } |
|
| 547 | - } |
|
| 548 | - if (!$result['page']) { |
|
| 549 | - return $result; |
|
| 550 | - } |
|
| 551 | - |
|
| 552 | - // Decompresser au besoin |
|
| 553 | - if ($gz) { |
|
| 554 | - $result['page'] = implode('', gzfile($gz)); |
|
| 555 | - supprimer_fichier($gz); |
|
| 556 | - } |
|
| 557 | - |
|
| 558 | - // Faut-il l'importer dans notre charset local ? |
|
| 559 | - if ($options['transcoder']) { |
|
| 560 | - include_spip('inc/charsets'); |
|
| 561 | - $result['page'] = transcoder_page($result['page'], $result['headers']); |
|
| 562 | - } |
|
| 563 | - |
|
| 564 | - return $result; |
|
| 399 | + $default = array( |
|
| 400 | + 'transcoder' => false, |
|
| 401 | + 'methode' => 'GET', |
|
| 402 | + 'taille_max' => null, |
|
| 403 | + 'datas' => '', |
|
| 404 | + 'boundary' => '', |
|
| 405 | + 'refuser_gz' => false, |
|
| 406 | + 'if_modified_since' => '', |
|
| 407 | + 'uri_referer' => '', |
|
| 408 | + 'file' => '', |
|
| 409 | + 'follow_location' => 10, |
|
| 410 | + 'version_http' => _INC_DISTANT_VERSION_HTTP, |
|
| 411 | + ); |
|
| 412 | + $options = array_merge($default, $options); |
|
| 413 | + // copier directement dans un fichier ? |
|
| 414 | + $copy = $options['file']; |
|
| 415 | + |
|
| 416 | + if ($options['methode'] == 'HEAD') { |
|
| 417 | + $options['taille_max'] = 0; |
|
| 418 | + } |
|
| 419 | + if (is_null($options['taille_max'])) { |
|
| 420 | + $options['taille_max'] = $copy ? _COPIE_LOCALE_MAX_SIZE : _INC_DISTANT_MAX_SIZE; |
|
| 421 | + } |
|
| 422 | + |
|
| 423 | + if (!empty($options['datas'])) { |
|
| 424 | + list($head, $postdata) = prepare_donnees_post($options['datas'], $options['boundary']); |
|
| 425 | + if (stripos($head, 'Content-Length:') === false) { |
|
| 426 | + $head .= 'Content-Length: ' . strlen($postdata); |
|
| 427 | + } |
|
| 428 | + $options['datas'] = $head . "\r\n\r\n" . $postdata; |
|
| 429 | + if (strlen($postdata)) { |
|
| 430 | + $options['methode'] = 'POST'; |
|
| 431 | + } |
|
| 432 | + } |
|
| 433 | + |
|
| 434 | + // Accepter les URLs au format feed:// ou qui ont oublie le http:// ou les urls relatives au protocole |
|
| 435 | + $url = preg_replace(',^feed://,i', 'http://', $url); |
|
| 436 | + if (!tester_url_absolue($url)) { |
|
| 437 | + $url = 'http://' . $url; |
|
| 438 | + } elseif (strncmp($url, '//', 2) == 0) { |
|
| 439 | + $url = 'http:' . $url; |
|
| 440 | + } |
|
| 441 | + |
|
| 442 | + $url = url_to_ascii($url); |
|
| 443 | + |
|
| 444 | + $result = array( |
|
| 445 | + 'status' => 0, |
|
| 446 | + 'headers' => '', |
|
| 447 | + 'page' => '', |
|
| 448 | + 'length' => 0, |
|
| 449 | + 'last_modified' => '', |
|
| 450 | + 'location' => '', |
|
| 451 | + 'url' => $url |
|
| 452 | + ); |
|
| 453 | + |
|
| 454 | + // si on ecrit directement dans un fichier, pour ne pas manipuler en memoire refuser gz |
|
| 455 | + $refuser_gz = (($options['refuser_gz'] or $copy) ? true : false); |
|
| 456 | + |
|
| 457 | + // ouvrir la connexion et envoyer la requete et ses en-tetes |
|
| 458 | + list($handle, $fopen) = init_http( |
|
| 459 | + $options['methode'], |
|
| 460 | + $url, |
|
| 461 | + $refuser_gz, |
|
| 462 | + $options['uri_referer'], |
|
| 463 | + $options['datas'], |
|
| 464 | + $options['version_http'], |
|
| 465 | + $options['if_modified_since'] |
|
| 466 | + ); |
|
| 467 | + if (!$handle) { |
|
| 468 | + spip_log("ECHEC init_http $url"); |
|
| 469 | + |
|
| 470 | + return false; |
|
| 471 | + } |
|
| 472 | + |
|
| 473 | + // Sauf en fopen, envoyer le flux d'entree |
|
| 474 | + // et recuperer les en-tetes de reponses |
|
| 475 | + if (!$fopen) { |
|
| 476 | + $res = recuperer_entetes_complets($handle, $options['if_modified_since']); |
|
| 477 | + if (!$res) { |
|
| 478 | + fclose($handle); |
|
| 479 | + $t = @parse_url($url); |
|
| 480 | + $host = $t['host']; |
|
| 481 | + // Chinoisierie inexplicable pour contrer |
|
| 482 | + // les actions liberticides de l'empire du milieu |
|
| 483 | + if (!need_proxy($host) |
|
| 484 | + and $res = @file_get_contents($url) |
|
| 485 | + ) { |
|
| 486 | + $result['length'] = strlen($res); |
|
| 487 | + if ($copy) { |
|
| 488 | + ecrire_fichier($copy, $res); |
|
| 489 | + $result['file'] = $copy; |
|
| 490 | + } else { |
|
| 491 | + $result['page'] = $res; |
|
| 492 | + } |
|
| 493 | + $res = array( |
|
| 494 | + 'status' => 200, |
|
| 495 | + ); |
|
| 496 | + } else { |
|
| 497 | + return false; |
|
| 498 | + } |
|
| 499 | + } elseif ($res['location'] and $options['follow_location']) { |
|
| 500 | + $options['follow_location']--; |
|
| 501 | + fclose($handle); |
|
| 502 | + include_spip('inc/filtres'); |
|
| 503 | + $url = suivre_lien($url, $res['location']); |
|
| 504 | + spip_log("recuperer_url recommence sur $url"); |
|
| 505 | + |
|
| 506 | + return recuperer_url($url, $options); |
|
| 507 | + } elseif ($res['status'] !== 200) { |
|
| 508 | + spip_log('HTTP status ' . $res['status'] . " pour $url"); |
|
| 509 | + } |
|
| 510 | + $result['status'] = $res['status']; |
|
| 511 | + if (isset($res['headers'])) { |
|
| 512 | + $result['headers'] = $res['headers']; |
|
| 513 | + } |
|
| 514 | + if (isset($res['last_modified'])) { |
|
| 515 | + $result['last_modified'] = $res['last_modified']; |
|
| 516 | + } |
|
| 517 | + if (isset($res['location'])) { |
|
| 518 | + $result['location'] = $res['location']; |
|
| 519 | + } |
|
| 520 | + } |
|
| 521 | + |
|
| 522 | + // on ne veut que les entetes |
|
| 523 | + if (!$options['taille_max'] or $options['methode'] == 'HEAD' or $result['status'] == '304') { |
|
| 524 | + return $result; |
|
| 525 | + } |
|
| 526 | + |
|
| 527 | + |
|
| 528 | + // s'il faut deballer, le faire via un fichier temporaire |
|
| 529 | + // sinon la memoire explose pour les gros flux |
|
| 530 | + |
|
| 531 | + $gz = false; |
|
| 532 | + if (preg_match(",\bContent-Encoding: .*gzip,is", $result['headers'])) { |
|
| 533 | + $gz = (_DIR_TMP . md5(uniqid(mt_rand())) . '.tmp.gz'); |
|
| 534 | + } |
|
| 535 | + |
|
| 536 | + // si on a pas deja recuperer le contenu par une methode detournee |
|
| 537 | + if (!$result['length']) { |
|
| 538 | + $res = recuperer_body($handle, $options['taille_max'], $gz ? $gz : $copy); |
|
| 539 | + fclose($handle); |
|
| 540 | + if ($copy) { |
|
| 541 | + $result['length'] = $res; |
|
| 542 | + $result['file'] = $copy; |
|
| 543 | + } elseif ($res) { |
|
| 544 | + $result['page'] = &$res; |
|
| 545 | + $result['length'] = strlen($result['page']); |
|
| 546 | + } |
|
| 547 | + } |
|
| 548 | + if (!$result['page']) { |
|
| 549 | + return $result; |
|
| 550 | + } |
|
| 551 | + |
|
| 552 | + // Decompresser au besoin |
|
| 553 | + if ($gz) { |
|
| 554 | + $result['page'] = implode('', gzfile($gz)); |
|
| 555 | + supprimer_fichier($gz); |
|
| 556 | + } |
|
| 557 | + |
|
| 558 | + // Faut-il l'importer dans notre charset local ? |
|
| 559 | + if ($options['transcoder']) { |
|
| 560 | + include_spip('inc/charsets'); |
|
| 561 | + $result['page'] = transcoder_page($result['page'], $result['headers']); |
|
| 562 | + } |
|
| 563 | + |
|
| 564 | + return $result; |
|
| 565 | 565 | } |
| 566 | 566 | |
| 567 | 567 | /** |
@@ -577,72 +577,72 @@ discard block |
||
| 577 | 577 | * @return array|bool|mixed |
| 578 | 578 | */ |
| 579 | 579 | function recuperer_url_cache($url, $options = array()) { |
| 580 | - if (!defined('_DELAI_RECUPERER_URL_CACHE')) { |
|
| 581 | - define('_DELAI_RECUPERER_URL_CACHE', 3600); |
|
| 582 | - } |
|
| 583 | - $default = array( |
|
| 584 | - 'transcoder' => false, |
|
| 585 | - 'methode' => 'GET', |
|
| 586 | - 'taille_max' => null, |
|
| 587 | - 'datas' => '', |
|
| 588 | - 'boundary' => '', |
|
| 589 | - 'refuser_gz' => false, |
|
| 590 | - 'if_modified_since' => '', |
|
| 591 | - 'uri_referer' => '', |
|
| 592 | - 'file' => '', |
|
| 593 | - 'follow_location' => 10, |
|
| 594 | - 'version_http' => _INC_DISTANT_VERSION_HTTP, |
|
| 595 | - 'delai_cache' => _DELAI_RECUPERER_URL_CACHE, |
|
| 596 | - ); |
|
| 597 | - $options = array_merge($default, $options); |
|
| 598 | - |
|
| 599 | - // cas ou il n'est pas possible de cacher |
|
| 600 | - if (!empty($options['data']) or $options['methode'] == 'POST') { |
|
| 601 | - return recuperer_url($url, $options); |
|
| 602 | - } |
|
| 603 | - |
|
| 604 | - // ne pas tenter plusieurs fois la meme url en erreur (non cachee donc) |
|
| 605 | - static $errors = array(); |
|
| 606 | - if (isset($errors[$url])) { |
|
| 607 | - return $errors[$url]; |
|
| 608 | - } |
|
| 609 | - |
|
| 610 | - $sig = $options; |
|
| 611 | - unset($sig['if_modified_since']); |
|
| 612 | - unset($sig['delai_cache']); |
|
| 613 | - $sig['url'] = $url; |
|
| 614 | - |
|
| 615 | - $dir = sous_repertoire(_DIR_CACHE, 'curl'); |
|
| 616 | - $cache = md5(serialize($sig)) . '-' . substr(preg_replace(',\W+,', '_', $url), 0, 80); |
|
| 617 | - $sub = sous_repertoire($dir, substr($cache, 0, 2)); |
|
| 618 | - $cache = "$sub$cache"; |
|
| 619 | - |
|
| 620 | - $res = false; |
|
| 621 | - $is_cached = file_exists($cache); |
|
| 622 | - if ($is_cached |
|
| 623 | - and (filemtime($cache) > $_SERVER['REQUEST_TIME'] - $options['delai_cache']) |
|
| 624 | - ) { |
|
| 625 | - lire_fichier($cache, $res); |
|
| 626 | - if ($res = unserialize($res)) { |
|
| 627 | - // mettre le last_modified et le status=304 ? |
|
| 628 | - } |
|
| 629 | - } |
|
| 630 | - if (!$res) { |
|
| 631 | - $res = recuperer_url($url, $options); |
|
| 632 | - // ne pas recharger cette url non cachee dans le meme hit puisque non disponible |
|
| 633 | - if (!$res) { |
|
| 634 | - if ($is_cached) { |
|
| 635 | - // on a pas reussi a recuperer mais on avait un cache : l'utiliser |
|
| 636 | - lire_fichier($cache, $res); |
|
| 637 | - $res = unserialize($res); |
|
| 638 | - } |
|
| 639 | - |
|
| 640 | - return $errors[$url] = $res; |
|
| 641 | - } |
|
| 642 | - ecrire_fichier($cache, serialize($res)); |
|
| 643 | - } |
|
| 644 | - |
|
| 645 | - return $res; |
|
| 580 | + if (!defined('_DELAI_RECUPERER_URL_CACHE')) { |
|
| 581 | + define('_DELAI_RECUPERER_URL_CACHE', 3600); |
|
| 582 | + } |
|
| 583 | + $default = array( |
|
| 584 | + 'transcoder' => false, |
|
| 585 | + 'methode' => 'GET', |
|
| 586 | + 'taille_max' => null, |
|
| 587 | + 'datas' => '', |
|
| 588 | + 'boundary' => '', |
|
| 589 | + 'refuser_gz' => false, |
|
| 590 | + 'if_modified_since' => '', |
|
| 591 | + 'uri_referer' => '', |
|
| 592 | + 'file' => '', |
|
| 593 | + 'follow_location' => 10, |
|
| 594 | + 'version_http' => _INC_DISTANT_VERSION_HTTP, |
|
| 595 | + 'delai_cache' => _DELAI_RECUPERER_URL_CACHE, |
|
| 596 | + ); |
|
| 597 | + $options = array_merge($default, $options); |
|
| 598 | + |
|
| 599 | + // cas ou il n'est pas possible de cacher |
|
| 600 | + if (!empty($options['data']) or $options['methode'] == 'POST') { |
|
| 601 | + return recuperer_url($url, $options); |
|
| 602 | + } |
|
| 603 | + |
|
| 604 | + // ne pas tenter plusieurs fois la meme url en erreur (non cachee donc) |
|
| 605 | + static $errors = array(); |
|
| 606 | + if (isset($errors[$url])) { |
|
| 607 | + return $errors[$url]; |
|
| 608 | + } |
|
| 609 | + |
|
| 610 | + $sig = $options; |
|
| 611 | + unset($sig['if_modified_since']); |
|
| 612 | + unset($sig['delai_cache']); |
|
| 613 | + $sig['url'] = $url; |
|
| 614 | + |
|
| 615 | + $dir = sous_repertoire(_DIR_CACHE, 'curl'); |
|
| 616 | + $cache = md5(serialize($sig)) . '-' . substr(preg_replace(',\W+,', '_', $url), 0, 80); |
|
| 617 | + $sub = sous_repertoire($dir, substr($cache, 0, 2)); |
|
| 618 | + $cache = "$sub$cache"; |
|
| 619 | + |
|
| 620 | + $res = false; |
|
| 621 | + $is_cached = file_exists($cache); |
|
| 622 | + if ($is_cached |
|
| 623 | + and (filemtime($cache) > $_SERVER['REQUEST_TIME'] - $options['delai_cache']) |
|
| 624 | + ) { |
|
| 625 | + lire_fichier($cache, $res); |
|
| 626 | + if ($res = unserialize($res)) { |
|
| 627 | + // mettre le last_modified et le status=304 ? |
|
| 628 | + } |
|
| 629 | + } |
|
| 630 | + if (!$res) { |
|
| 631 | + $res = recuperer_url($url, $options); |
|
| 632 | + // ne pas recharger cette url non cachee dans le meme hit puisque non disponible |
|
| 633 | + if (!$res) { |
|
| 634 | + if ($is_cached) { |
|
| 635 | + // on a pas reussi a recuperer mais on avait un cache : l'utiliser |
|
| 636 | + lire_fichier($cache, $res); |
|
| 637 | + $res = unserialize($res); |
|
| 638 | + } |
|
| 639 | + |
|
| 640 | + return $errors[$url] = $res; |
|
| 641 | + } |
|
| 642 | + ecrire_fichier($cache, serialize($res)); |
|
| 643 | + } |
|
| 644 | + |
|
| 645 | + return $res; |
|
| 646 | 646 | } |
| 647 | 647 | |
| 648 | 648 | /** |
@@ -680,52 +680,52 @@ discard block |
||
| 680 | 680 | * - false si la page n'a pu être récupérée (status different de 200) |
| 681 | 681 | **/ |
| 682 | 682 | function recuperer_page( |
| 683 | - $url, |
|
| 684 | - $trans = false, |
|
| 685 | - $get_headers = false, |
|
| 686 | - $taille_max = null, |
|
| 687 | - $datas = '', |
|
| 688 | - $boundary = '', |
|
| 689 | - $refuser_gz = false, |
|
| 690 | - $date_verif = '', |
|
| 691 | - $uri_referer = '' |
|
| 683 | + $url, |
|
| 684 | + $trans = false, |
|
| 685 | + $get_headers = false, |
|
| 686 | + $taille_max = null, |
|
| 687 | + $datas = '', |
|
| 688 | + $boundary = '', |
|
| 689 | + $refuser_gz = false, |
|
| 690 | + $date_verif = '', |
|
| 691 | + $uri_referer = '' |
|
| 692 | 692 | ) { |
| 693 | - // $copy = copier le fichier ? |
|
| 694 | - $copy = (is_string($trans) and strlen($trans) > 5); // eviter "false" :-) |
|
| 695 | - |
|
| 696 | - if (!is_null($taille_max) and ($taille_max == 0)) { |
|
| 697 | - $get = 'HEAD'; |
|
| 698 | - } else { |
|
| 699 | - $get = 'GET'; |
|
| 700 | - } |
|
| 701 | - |
|
| 702 | - $options = array( |
|
| 703 | - 'transcoder' => $trans === true, |
|
| 704 | - 'methode' => $get, |
|
| 705 | - 'datas' => $datas, |
|
| 706 | - 'boundary' => $boundary, |
|
| 707 | - 'refuser_gz' => $refuser_gz, |
|
| 708 | - 'if_modified_since' => $date_verif, |
|
| 709 | - 'uri_referer' => $uri_referer, |
|
| 710 | - 'file' => $copy ? $trans : '', |
|
| 711 | - 'follow_location' => 10, |
|
| 712 | - ); |
|
| 713 | - if (!is_null($taille_max)) { |
|
| 714 | - $options['taille_max'] = $taille_max; |
|
| 715 | - } |
|
| 716 | - // dix tentatives maximum en cas d'entetes 301... |
|
| 717 | - $res = recuperer_url($url, $options); |
|
| 718 | - if (!$res) { |
|
| 719 | - return false; |
|
| 720 | - } |
|
| 721 | - if ($res['status'] !== 200) { |
|
| 722 | - return false; |
|
| 723 | - } |
|
| 724 | - if ($get_headers) { |
|
| 725 | - return $res['headers'] . "\n" . $res['page']; |
|
| 726 | - } |
|
| 727 | - |
|
| 728 | - return $res['page']; |
|
| 693 | + // $copy = copier le fichier ? |
|
| 694 | + $copy = (is_string($trans) and strlen($trans) > 5); // eviter "false" :-) |
|
| 695 | + |
|
| 696 | + if (!is_null($taille_max) and ($taille_max == 0)) { |
|
| 697 | + $get = 'HEAD'; |
|
| 698 | + } else { |
|
| 699 | + $get = 'GET'; |
|
| 700 | + } |
|
| 701 | + |
|
| 702 | + $options = array( |
|
| 703 | + 'transcoder' => $trans === true, |
|
| 704 | + 'methode' => $get, |
|
| 705 | + 'datas' => $datas, |
|
| 706 | + 'boundary' => $boundary, |
|
| 707 | + 'refuser_gz' => $refuser_gz, |
|
| 708 | + 'if_modified_since' => $date_verif, |
|
| 709 | + 'uri_referer' => $uri_referer, |
|
| 710 | + 'file' => $copy ? $trans : '', |
|
| 711 | + 'follow_location' => 10, |
|
| 712 | + ); |
|
| 713 | + if (!is_null($taille_max)) { |
|
| 714 | + $options['taille_max'] = $taille_max; |
|
| 715 | + } |
|
| 716 | + // dix tentatives maximum en cas d'entetes 301... |
|
| 717 | + $res = recuperer_url($url, $options); |
|
| 718 | + if (!$res) { |
|
| 719 | + return false; |
|
| 720 | + } |
|
| 721 | + if ($res['status'] !== 200) { |
|
| 722 | + return false; |
|
| 723 | + } |
|
| 724 | + if ($get_headers) { |
|
| 725 | + return $res['headers'] . "\n" . $res['page']; |
|
| 726 | + } |
|
| 727 | + |
|
| 728 | + return $res['page']; |
|
| 729 | 729 | } |
| 730 | 730 | |
| 731 | 731 | |
@@ -762,48 +762,48 @@ discard block |
||
| 762 | 762 | * - false sinon |
| 763 | 763 | **/ |
| 764 | 764 | function recuperer_lapage( |
| 765 | - $url, |
|
| 766 | - $trans = false, |
|
| 767 | - $get = 'GET', |
|
| 768 | - $taille_max = 1048576, |
|
| 769 | - $datas = '', |
|
| 770 | - $refuser_gz = false, |
|
| 771 | - $date_verif = '', |
|
| 772 | - $uri_referer = '' |
|
| 765 | + $url, |
|
| 766 | + $trans = false, |
|
| 767 | + $get = 'GET', |
|
| 768 | + $taille_max = 1048576, |
|
| 769 | + $datas = '', |
|
| 770 | + $refuser_gz = false, |
|
| 771 | + $date_verif = '', |
|
| 772 | + $uri_referer = '' |
|
| 773 | 773 | ) { |
| 774 | - // $copy = copier le fichier ? |
|
| 775 | - $copy = (is_string($trans) and strlen($trans) > 5); // eviter "false" :-) |
|
| 776 | - |
|
| 777 | - // si on ecrit directement dans un fichier, pour ne pas manipuler |
|
| 778 | - // en memoire refuser gz |
|
| 779 | - if ($copy) { |
|
| 780 | - $refuser_gz = true; |
|
| 781 | - } |
|
| 782 | - |
|
| 783 | - $options = array( |
|
| 784 | - 'transcoder' => $trans === true, |
|
| 785 | - 'methode' => $get, |
|
| 786 | - 'datas' => $datas, |
|
| 787 | - 'refuser_gz' => $refuser_gz, |
|
| 788 | - 'if_modified_since' => $date_verif, |
|
| 789 | - 'uri_referer' => $uri_referer, |
|
| 790 | - 'file' => $copy ? $trans : '', |
|
| 791 | - 'follow_location' => false, |
|
| 792 | - ); |
|
| 793 | - if (!is_null($taille_max)) { |
|
| 794 | - $options['taille_max'] = $taille_max; |
|
| 795 | - } |
|
| 796 | - // dix tentatives maximum en cas d'entetes 301... |
|
| 797 | - $res = recuperer_url($url, $options); |
|
| 798 | - |
|
| 799 | - if (!$res) { |
|
| 800 | - return false; |
|
| 801 | - } |
|
| 802 | - if ($res['status'] !== 200) { |
|
| 803 | - return false; |
|
| 804 | - } |
|
| 805 | - |
|
| 806 | - return array($res['headers'], $res['page']); |
|
| 774 | + // $copy = copier le fichier ? |
|
| 775 | + $copy = (is_string($trans) and strlen($trans) > 5); // eviter "false" :-) |
|
| 776 | + |
|
| 777 | + // si on ecrit directement dans un fichier, pour ne pas manipuler |
|
| 778 | + // en memoire refuser gz |
|
| 779 | + if ($copy) { |
|
| 780 | + $refuser_gz = true; |
|
| 781 | + } |
|
| 782 | + |
|
| 783 | + $options = array( |
|
| 784 | + 'transcoder' => $trans === true, |
|
| 785 | + 'methode' => $get, |
|
| 786 | + 'datas' => $datas, |
|
| 787 | + 'refuser_gz' => $refuser_gz, |
|
| 788 | + 'if_modified_since' => $date_verif, |
|
| 789 | + 'uri_referer' => $uri_referer, |
|
| 790 | + 'file' => $copy ? $trans : '', |
|
| 791 | + 'follow_location' => false, |
|
| 792 | + ); |
|
| 793 | + if (!is_null($taille_max)) { |
|
| 794 | + $options['taille_max'] = $taille_max; |
|
| 795 | + } |
|
| 796 | + // dix tentatives maximum en cas d'entetes 301... |
|
| 797 | + $res = recuperer_url($url, $options); |
|
| 798 | + |
|
| 799 | + if (!$res) { |
|
| 800 | + return false; |
|
| 801 | + } |
|
| 802 | + if ($res['status'] !== 200) { |
|
| 803 | + return false; |
|
| 804 | + } |
|
| 805 | + |
|
| 806 | + return array($res['headers'], $res['page']); |
|
| 807 | 807 | } |
| 808 | 808 | |
| 809 | 809 | /** |
@@ -821,41 +821,41 @@ discard block |
||
| 821 | 821 | * string contenu de la resource |
| 822 | 822 | */ |
| 823 | 823 | function recuperer_body($handle, $taille_max = _INC_DISTANT_MAX_SIZE, $fichier = '') { |
| 824 | - $taille = 0; |
|
| 825 | - $result = ''; |
|
| 826 | - $fp = false; |
|
| 827 | - if ($fichier) { |
|
| 828 | - include_spip('inc/acces'); |
|
| 829 | - $tmpfile = "$fichier." . creer_uniqid() . '.tmp'; |
|
| 830 | - $fp = spip_fopen_lock($tmpfile, 'w', LOCK_EX); |
|
| 831 | - if (!$fp and file_exists($fichier)) { |
|
| 832 | - return filesize($fichier); |
|
| 833 | - } |
|
| 834 | - if (!$fp) { |
|
| 835 | - return false; |
|
| 836 | - } |
|
| 837 | - $result = 0; // on renvoie la taille du fichier |
|
| 838 | - } |
|
| 839 | - while (!feof($handle) and $taille < $taille_max) { |
|
| 840 | - $res = fread($handle, 16384); |
|
| 841 | - $taille += strlen($res); |
|
| 842 | - if ($fp) { |
|
| 843 | - fwrite($fp, $res); |
|
| 844 | - $result = $taille; |
|
| 845 | - } else { |
|
| 846 | - $result .= $res; |
|
| 847 | - } |
|
| 848 | - } |
|
| 849 | - if ($fp) { |
|
| 850 | - spip_fclose_unlock($fp); |
|
| 851 | - spip_unlink($fichier); |
|
| 852 | - @rename($tmpfile, $fichier); |
|
| 853 | - if (!file_exists($fichier)) { |
|
| 854 | - return false; |
|
| 855 | - } |
|
| 856 | - } |
|
| 857 | - |
|
| 858 | - return $result; |
|
| 824 | + $taille = 0; |
|
| 825 | + $result = ''; |
|
| 826 | + $fp = false; |
|
| 827 | + if ($fichier) { |
|
| 828 | + include_spip('inc/acces'); |
|
| 829 | + $tmpfile = "$fichier." . creer_uniqid() . '.tmp'; |
|
| 830 | + $fp = spip_fopen_lock($tmpfile, 'w', LOCK_EX); |
|
| 831 | + if (!$fp and file_exists($fichier)) { |
|
| 832 | + return filesize($fichier); |
|
| 833 | + } |
|
| 834 | + if (!$fp) { |
|
| 835 | + return false; |
|
| 836 | + } |
|
| 837 | + $result = 0; // on renvoie la taille du fichier |
|
| 838 | + } |
|
| 839 | + while (!feof($handle) and $taille < $taille_max) { |
|
| 840 | + $res = fread($handle, 16384); |
|
| 841 | + $taille += strlen($res); |
|
| 842 | + if ($fp) { |
|
| 843 | + fwrite($fp, $res); |
|
| 844 | + $result = $taille; |
|
| 845 | + } else { |
|
| 846 | + $result .= $res; |
|
| 847 | + } |
|
| 848 | + } |
|
| 849 | + if ($fp) { |
|
| 850 | + spip_fclose_unlock($fp); |
|
| 851 | + spip_unlink($fichier); |
|
| 852 | + @rename($tmpfile, $fichier); |
|
| 853 | + if (!file_exists($fichier)) { |
|
| 854 | + return false; |
|
| 855 | + } |
|
| 856 | + } |
|
| 857 | + |
|
| 858 | + return $result; |
|
| 859 | 859 | } |
| 860 | 860 | |
| 861 | 861 | /** |
@@ -877,34 +877,34 @@ discard block |
||
| 877 | 877 | * string location |
| 878 | 878 | */ |
| 879 | 879 | function recuperer_entetes_complets($handle, $if_modified_since = false) { |
| 880 | - $result = array('status' => 0, 'headers' => array(), 'last_modified' => 0, 'location' => ''); |
|
| 881 | - |
|
| 882 | - $s = @trim(fgets($handle, 16384)); |
|
| 883 | - if (!preg_match(',^HTTP/[0-9]+\.[0-9]+ ([0-9]+),', $s, $r)) { |
|
| 884 | - return false; |
|
| 885 | - } |
|
| 886 | - $result['status'] = intval($r[1]); |
|
| 887 | - while ($s = trim(fgets($handle, 16384))) { |
|
| 888 | - $result['headers'][] = $s . "\n"; |
|
| 889 | - preg_match(',^([^:]*): *(.*)$,i', $s, $r); |
|
| 890 | - list(, $d, $v) = $r; |
|
| 891 | - if (strtolower(trim($d)) == 'location' and $result['status'] >= 300 and $result['status'] < 400) { |
|
| 892 | - $result['location'] = $v; |
|
| 893 | - } elseif ($d == 'Last-Modified') { |
|
| 894 | - $result['last_modified'] = strtotime($v); |
|
| 895 | - } |
|
| 896 | - } |
|
| 897 | - if ($if_modified_since |
|
| 898 | - and $result['last_modified'] |
|
| 899 | - and $if_modified_since > $result['last_modified'] |
|
| 900 | - and $result['status'] == 200 |
|
| 901 | - ) { |
|
| 902 | - $result['status'] = 304; |
|
| 903 | - } |
|
| 904 | - |
|
| 905 | - $result['headers'] = implode('', $result['headers']); |
|
| 906 | - |
|
| 907 | - return $result; |
|
| 880 | + $result = array('status' => 0, 'headers' => array(), 'last_modified' => 0, 'location' => ''); |
|
| 881 | + |
|
| 882 | + $s = @trim(fgets($handle, 16384)); |
|
| 883 | + if (!preg_match(',^HTTP/[0-9]+\.[0-9]+ ([0-9]+),', $s, $r)) { |
|
| 884 | + return false; |
|
| 885 | + } |
|
| 886 | + $result['status'] = intval($r[1]); |
|
| 887 | + while ($s = trim(fgets($handle, 16384))) { |
|
| 888 | + $result['headers'][] = $s . "\n"; |
|
| 889 | + preg_match(',^([^:]*): *(.*)$,i', $s, $r); |
|
| 890 | + list(, $d, $v) = $r; |
|
| 891 | + if (strtolower(trim($d)) == 'location' and $result['status'] >= 300 and $result['status'] < 400) { |
|
| 892 | + $result['location'] = $v; |
|
| 893 | + } elseif ($d == 'Last-Modified') { |
|
| 894 | + $result['last_modified'] = strtotime($v); |
|
| 895 | + } |
|
| 896 | + } |
|
| 897 | + if ($if_modified_since |
|
| 898 | + and $result['last_modified'] |
|
| 899 | + and $if_modified_since > $result['last_modified'] |
|
| 900 | + and $result['status'] == 200 |
|
| 901 | + ) { |
|
| 902 | + $result['status'] = 304; |
|
| 903 | + } |
|
| 904 | + |
|
| 905 | + $result['headers'] = implode('', $result['headers']); |
|
| 906 | + |
|
| 907 | + return $result; |
|
| 908 | 908 | } |
| 909 | 909 | |
| 910 | 910 | /** |
@@ -926,20 +926,20 @@ discard block |
||
| 926 | 926 | * - le tableau des entetes dans tous les autres cas |
| 927 | 927 | **/ |
| 928 | 928 | function recuperer_entetes($f, $date_verif = '') { |
| 929 | - //Cas ou la page distante n'a pas bouge depuis |
|
| 930 | - //la derniere visite |
|
| 931 | - $res = recuperer_entetes_complets($f, $date_verif); |
|
| 932 | - if (!$res) { |
|
| 933 | - return false; |
|
| 934 | - } |
|
| 935 | - if ($res['location']) { |
|
| 936 | - return $res['location']; |
|
| 937 | - } |
|
| 938 | - if ($res['status'] != 200) { |
|
| 939 | - return $res['status']; |
|
| 940 | - } |
|
| 941 | - |
|
| 942 | - return explode("\n", $res['headers']); |
|
| 929 | + //Cas ou la page distante n'a pas bouge depuis |
|
| 930 | + //la derniere visite |
|
| 931 | + $res = recuperer_entetes_complets($f, $date_verif); |
|
| 932 | + if (!$res) { |
|
| 933 | + return false; |
|
| 934 | + } |
|
| 935 | + if ($res['location']) { |
|
| 936 | + return $res['location']; |
|
| 937 | + } |
|
| 938 | + if ($res['status'] != 200) { |
|
| 939 | + return $res['status']; |
|
| 940 | + } |
|
| 941 | + |
|
| 942 | + return explode("\n", $res['headers']); |
|
| 943 | 943 | } |
| 944 | 944 | |
| 945 | 945 | /** |
@@ -961,22 +961,22 @@ discard block |
||
| 961 | 961 | * Nom du fichier pour copie locale |
| 962 | 962 | **/ |
| 963 | 963 | function nom_fichier_copie_locale($source, $extension) { |
| 964 | - include_spip('inc/documents'); |
|
| 964 | + include_spip('inc/documents'); |
|
| 965 | 965 | |
| 966 | - $d = creer_repertoire_documents('distant'); # IMG/distant/ |
|
| 967 | - $d = sous_repertoire($d, $extension); # IMG/distant/pdf/ |
|
| 966 | + $d = creer_repertoire_documents('distant'); # IMG/distant/ |
|
| 967 | + $d = sous_repertoire($d, $extension); # IMG/distant/pdf/ |
|
| 968 | 968 | |
| 969 | - // on se place tout le temps comme si on etait a la racine |
|
| 970 | - if (_DIR_RACINE) { |
|
| 971 | - $d = preg_replace(',^' . preg_quote(_DIR_RACINE) . ',', '', $d); |
|
| 972 | - } |
|
| 969 | + // on se place tout le temps comme si on etait a la racine |
|
| 970 | + if (_DIR_RACINE) { |
|
| 971 | + $d = preg_replace(',^' . preg_quote(_DIR_RACINE) . ',', '', $d); |
|
| 972 | + } |
|
| 973 | 973 | |
| 974 | - $m = md5($source); |
|
| 974 | + $m = md5($source); |
|
| 975 | 975 | |
| 976 | - return $d |
|
| 977 | - . substr(preg_replace(',[^\w-],', '', basename($source)) . '-' . $m, 0, 12) |
|
| 978 | - . substr($m, 0, 4) |
|
| 979 | - . ".$extension"; |
|
| 976 | + return $d |
|
| 977 | + . substr(preg_replace(',[^\w-],', '', basename($source)) . '-' . $m, 0, 12) |
|
| 978 | + . substr($m, 0, 4) |
|
| 979 | + . ".$extension"; |
|
| 980 | 980 | } |
| 981 | 981 | |
| 982 | 982 | /** |
@@ -994,68 +994,68 @@ discard block |
||
| 994 | 994 | * Nom du fichier calculé |
| 995 | 995 | **/ |
| 996 | 996 | function fichier_copie_locale($source) { |
| 997 | - // Si c'est deja local pas de souci |
|
| 998 | - if (!tester_url_absolue($source)) { |
|
| 999 | - if (_DIR_RACINE) { |
|
| 1000 | - $source = preg_replace(',^' . preg_quote(_DIR_RACINE) . ',', '', $source); |
|
| 1001 | - } |
|
| 1002 | - |
|
| 1003 | - return $source; |
|
| 1004 | - } |
|
| 1005 | - |
|
| 1006 | - // optimisation : on regarde si on peut deviner l'extension dans l'url et si le fichier |
|
| 1007 | - // a deja ete copie en local avec cette extension |
|
| 1008 | - // dans ce cas elle est fiable, pas la peine de requeter en base |
|
| 1009 | - $path_parts = pathinfo($source); |
|
| 1010 | - if (!isset($path_parts['extension'])) { |
|
| 1011 | - $path_parts['extension'] = ''; |
|
| 1012 | - } |
|
| 1013 | - $ext = $path_parts ? $path_parts['extension'] : ''; |
|
| 1014 | - if ($ext |
|
| 1015 | - and preg_match(',^\w+$,', $ext) // pas de php?truc=1&... |
|
| 1016 | - and $f = nom_fichier_copie_locale($source, $ext) |
|
| 1017 | - and file_exists(_DIR_RACINE . $f) |
|
| 1018 | - ) { |
|
| 1019 | - return $f; |
|
| 1020 | - } |
|
| 1021 | - |
|
| 1022 | - |
|
| 1023 | - // Si c'est deja dans la table des documents, |
|
| 1024 | - // ramener le nom de sa copie potentielle |
|
| 1025 | - $ext = sql_getfetsel('extension', 'spip_documents', 'fichier=' . sql_quote($source) . " AND distant='oui' AND extension <> ''"); |
|
| 1026 | - |
|
| 1027 | - if ($ext) { |
|
| 1028 | - return nom_fichier_copie_locale($source, $ext); |
|
| 1029 | - } |
|
| 1030 | - |
|
| 1031 | - // voir si l'extension indiquee dans le nom du fichier est ok |
|
| 1032 | - // et si il n'aurait pas deja ete rapatrie |
|
| 1033 | - |
|
| 1034 | - $ext = $path_parts ? $path_parts['extension'] : ''; |
|
| 1035 | - |
|
| 1036 | - if ($ext and sql_getfetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($ext))) { |
|
| 1037 | - $f = nom_fichier_copie_locale($source, $ext); |
|
| 1038 | - if (file_exists(_DIR_RACINE . $f)) { |
|
| 1039 | - return $f; |
|
| 1040 | - } |
|
| 1041 | - } |
|
| 1042 | - |
|
| 1043 | - // Ping pour voir si son extension est connue et autorisee |
|
| 1044 | - // avec mise en cache du resultat du ping |
|
| 1045 | - |
|
| 1046 | - $cache = sous_repertoire(_DIR_CACHE, 'rid') . md5($source); |
|
| 1047 | - if (!@file_exists($cache) |
|
| 1048 | - or !$path_parts = @unserialize(spip_file_get_contents($cache)) |
|
| 1049 | - or _request('var_mode') == 'recalcul' |
|
| 1050 | - ) { |
|
| 1051 | - $path_parts = recuperer_infos_distantes($source, 0, false); |
|
| 1052 | - ecrire_fichier($cache, serialize($path_parts)); |
|
| 1053 | - } |
|
| 1054 | - $ext = !empty($path_parts['extension']) ? $path_parts['extension'] : ''; |
|
| 1055 | - if ($ext and sql_getfetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($ext))) { |
|
| 1056 | - return nom_fichier_copie_locale($source, $ext); |
|
| 1057 | - } |
|
| 1058 | - spip_log("pas de copie locale pour $source"); |
|
| 997 | + // Si c'est deja local pas de souci |
|
| 998 | + if (!tester_url_absolue($source)) { |
|
| 999 | + if (_DIR_RACINE) { |
|
| 1000 | + $source = preg_replace(',^' . preg_quote(_DIR_RACINE) . ',', '', $source); |
|
| 1001 | + } |
|
| 1002 | + |
|
| 1003 | + return $source; |
|
| 1004 | + } |
|
| 1005 | + |
|
| 1006 | + // optimisation : on regarde si on peut deviner l'extension dans l'url et si le fichier |
|
| 1007 | + // a deja ete copie en local avec cette extension |
|
| 1008 | + // dans ce cas elle est fiable, pas la peine de requeter en base |
|
| 1009 | + $path_parts = pathinfo($source); |
|
| 1010 | + if (!isset($path_parts['extension'])) { |
|
| 1011 | + $path_parts['extension'] = ''; |
|
| 1012 | + } |
|
| 1013 | + $ext = $path_parts ? $path_parts['extension'] : ''; |
|
| 1014 | + if ($ext |
|
| 1015 | + and preg_match(',^\w+$,', $ext) // pas de php?truc=1&... |
|
| 1016 | + and $f = nom_fichier_copie_locale($source, $ext) |
|
| 1017 | + and file_exists(_DIR_RACINE . $f) |
|
| 1018 | + ) { |
|
| 1019 | + return $f; |
|
| 1020 | + } |
|
| 1021 | + |
|
| 1022 | + |
|
| 1023 | + // Si c'est deja dans la table des documents, |
|
| 1024 | + // ramener le nom de sa copie potentielle |
|
| 1025 | + $ext = sql_getfetsel('extension', 'spip_documents', 'fichier=' . sql_quote($source) . " AND distant='oui' AND extension <> ''"); |
|
| 1026 | + |
|
| 1027 | + if ($ext) { |
|
| 1028 | + return nom_fichier_copie_locale($source, $ext); |
|
| 1029 | + } |
|
| 1030 | + |
|
| 1031 | + // voir si l'extension indiquee dans le nom du fichier est ok |
|
| 1032 | + // et si il n'aurait pas deja ete rapatrie |
|
| 1033 | + |
|
| 1034 | + $ext = $path_parts ? $path_parts['extension'] : ''; |
|
| 1035 | + |
|
| 1036 | + if ($ext and sql_getfetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($ext))) { |
|
| 1037 | + $f = nom_fichier_copie_locale($source, $ext); |
|
| 1038 | + if (file_exists(_DIR_RACINE . $f)) { |
|
| 1039 | + return $f; |
|
| 1040 | + } |
|
| 1041 | + } |
|
| 1042 | + |
|
| 1043 | + // Ping pour voir si son extension est connue et autorisee |
|
| 1044 | + // avec mise en cache du resultat du ping |
|
| 1045 | + |
|
| 1046 | + $cache = sous_repertoire(_DIR_CACHE, 'rid') . md5($source); |
|
| 1047 | + if (!@file_exists($cache) |
|
| 1048 | + or !$path_parts = @unserialize(spip_file_get_contents($cache)) |
|
| 1049 | + or _request('var_mode') == 'recalcul' |
|
| 1050 | + ) { |
|
| 1051 | + $path_parts = recuperer_infos_distantes($source, 0, false); |
|
| 1052 | + ecrire_fichier($cache, serialize($path_parts)); |
|
| 1053 | + } |
|
| 1054 | + $ext = !empty($path_parts['extension']) ? $path_parts['extension'] : ''; |
|
| 1055 | + if ($ext and sql_getfetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($ext))) { |
|
| 1056 | + return nom_fichier_copie_locale($source, $ext); |
|
| 1057 | + } |
|
| 1058 | + spip_log("pas de copie locale pour $source"); |
|
| 1059 | 1059 | } |
| 1060 | 1060 | |
| 1061 | 1061 | |
@@ -1083,141 +1083,141 @@ discard block |
||
| 1083 | 1083 | **/ |
| 1084 | 1084 | function recuperer_infos_distantes($source, $max = 0, $charger_si_petite_image = true) { |
| 1085 | 1085 | |
| 1086 | - // pas la peine de perdre son temps |
|
| 1087 | - if (!tester_url_absolue($source)) { |
|
| 1088 | - return false; |
|
| 1089 | - } |
|
| 1090 | - |
|
| 1091 | - # charger les alias des types mime |
|
| 1092 | - include_spip('base/typedoc'); |
|
| 1093 | - |
|
| 1094 | - $a = array(); |
|
| 1095 | - $mime_type = ''; |
|
| 1096 | - // On va directement charger le debut des images et des fichiers html, |
|
| 1097 | - // de maniere a attrapper le maximum d'infos (titre, taille, etc). Si |
|
| 1098 | - // ca echoue l'utilisateur devra les entrer... |
|
| 1099 | - if ($headers = recuperer_page($source, false, true, $max, '', '', true)) { |
|
| 1100 | - list($headers, $a['body']) = preg_split(',\n\n,', $headers, 2); |
|
| 1101 | - |
|
| 1102 | - if (preg_match(",\nContent-Type: *([^[:space:];]*),i", "\n$headers", $regs)) { |
|
| 1103 | - $mime_type = (trim($regs[1])); |
|
| 1104 | - } else { |
|
| 1105 | - $mime_type = ''; |
|
| 1106 | - } // inconnu |
|
| 1107 | - |
|
| 1108 | - // Appliquer les alias |
|
| 1109 | - while (isset($GLOBALS['mime_alias'][$mime_type])) { |
|
| 1110 | - $mime_type = $GLOBALS['mime_alias'][$mime_type]; |
|
| 1111 | - } |
|
| 1112 | - |
|
| 1113 | - // Si on a un mime-type insignifiant |
|
| 1114 | - // text/plain,application/octet-stream ou vide |
|
| 1115 | - // c'est peut-etre que le serveur ne sait pas |
|
| 1116 | - // ce qu'il sert ; on va tenter de detecter via l'extension de l'url |
|
| 1117 | - // ou le Content-Disposition: attachment; filename=... |
|
| 1118 | - $t = null; |
|
| 1119 | - if (in_array($mime_type, array('text/plain', '', 'application/octet-stream'))) { |
|
| 1120 | - if (!$t |
|
| 1121 | - and preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $source, $rext) |
|
| 1122 | - ) { |
|
| 1123 | - $t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($rext[1], '', 'text')); |
|
| 1124 | - } |
|
| 1125 | - if (!$t |
|
| 1126 | - and preg_match(',^Content-Disposition:\s*attachment;\s*filename=(.*)$,Uims', $headers, $m) |
|
| 1127 | - and preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $m[1], $rext) |
|
| 1128 | - ) { |
|
| 1129 | - $t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($rext[1], '', 'text')); |
|
| 1130 | - } |
|
| 1131 | - } |
|
| 1132 | - |
|
| 1133 | - // Autre mime/type (ou text/plain avec fichier d'extension inconnue) |
|
| 1134 | - if (!$t) { |
|
| 1135 | - $t = sql_fetsel('extension', 'spip_types_documents', 'mime_type=' . sql_quote($mime_type)); |
|
| 1136 | - } |
|
| 1137 | - |
|
| 1138 | - // Toujours rien ? (ex: audio/x-ogg au lieu de application/ogg) |
|
| 1139 | - // On essaie de nouveau avec l'extension |
|
| 1140 | - if (!$t |
|
| 1141 | - and $mime_type != 'text/plain' |
|
| 1142 | - and preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $source, $rext) |
|
| 1143 | - ) { |
|
| 1144 | - # eviter xxx.3 => 3gp (> SPIP 3) |
|
| 1145 | - $t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($rext[1], '', 'text')); |
|
| 1146 | - } |
|
| 1147 | - |
|
| 1148 | - if ($t) { |
|
| 1149 | - spip_log("mime-type $mime_type ok, extension " . $t['extension']); |
|
| 1150 | - $a['extension'] = $t['extension']; |
|
| 1151 | - } else { |
|
| 1152 | - # par defaut on retombe sur '.bin' si c'est autorise |
|
| 1153 | - spip_log("mime-type $mime_type inconnu"); |
|
| 1154 | - $t = sql_fetsel('extension', 'spip_types_documents', "extension='bin'"); |
|
| 1155 | - if (!$t) { |
|
| 1156 | - return false; |
|
| 1157 | - } |
|
| 1158 | - $a['extension'] = $t['extension']; |
|
| 1159 | - } |
|
| 1160 | - |
|
| 1161 | - if (preg_match(",\nContent-Length: *([^[:space:]]*),i", "\n$headers", $regs)) { |
|
| 1162 | - $a['taille'] = intval($regs[1]); |
|
| 1163 | - } |
|
| 1164 | - } |
|
| 1165 | - |
|
| 1166 | - // Echec avec HEAD, on tente avec GET |
|
| 1167 | - if (!$a and !$max) { |
|
| 1168 | - spip_log("tenter GET $source"); |
|
| 1169 | - $a = recuperer_infos_distantes($source, _INC_DISTANT_MAX_SIZE); |
|
| 1170 | - } |
|
| 1171 | - |
|
| 1172 | - // si on a rien trouve pas la peine d'insister |
|
| 1173 | - if (!$a) { |
|
| 1174 | - return false; |
|
| 1175 | - } |
|
| 1176 | - |
|
| 1177 | - // S'il s'agit d'une image pas trop grosse ou d'un fichier html, on va aller |
|
| 1178 | - // recharger le document en GET et recuperer des donnees supplementaires... |
|
| 1179 | - if (preg_match(',^image/(jpeg|gif|png|swf),', $mime_type)) { |
|
| 1180 | - if ($max == 0 |
|
| 1181 | - and (empty($a['taille']) or $a['taille'] < _INC_DISTANT_MAX_SIZE) |
|
| 1182 | - and isset($GLOBALS['meta']['formats_graphiques']) |
|
| 1183 | - and (strpos($GLOBALS['meta']['formats_graphiques'], $a['extension']) !== false) |
|
| 1184 | - and $charger_si_petite_image |
|
| 1185 | - ) { |
|
| 1186 | - $a = recuperer_infos_distantes($source, _INC_DISTANT_MAX_SIZE); |
|
| 1187 | - } else { |
|
| 1188 | - if ($a['body']) { |
|
| 1189 | - $a['fichier'] = _DIR_RACINE . nom_fichier_copie_locale($source, $a['extension']); |
|
| 1190 | - ecrire_fichier($a['fichier'], $a['body']); |
|
| 1191 | - $size_image = @getimagesize($a['fichier']); |
|
| 1192 | - $a['largeur'] = intval($size_image[0]); |
|
| 1193 | - $a['hauteur'] = intval($size_image[1]); |
|
| 1194 | - $a['type_image'] = true; |
|
| 1195 | - } |
|
| 1196 | - } |
|
| 1197 | - } |
|
| 1198 | - |
|
| 1199 | - // Fichier swf, si on n'a pas la taille, on va mettre 425x350 par defaut |
|
| 1200 | - // ce sera mieux que 0x0 |
|
| 1201 | - if ($a and isset($a['extension']) and $a['extension'] == 'swf' |
|
| 1202 | - and empty($a['largeur']) |
|
| 1203 | - ) { |
|
| 1204 | - $a['largeur'] = 425; |
|
| 1205 | - $a['hauteur'] = 350; |
|
| 1206 | - } |
|
| 1207 | - |
|
| 1208 | - if ($mime_type == 'text/html') { |
|
| 1209 | - include_spip('inc/filtres'); |
|
| 1210 | - $page = recuperer_page($source, true, false, _INC_DISTANT_MAX_SIZE); |
|
| 1211 | - if (preg_match(',<title>(.*?)</title>,ims', $page, $regs)) { |
|
| 1212 | - $a['titre'] = corriger_caracteres(trim($regs[1])); |
|
| 1213 | - } |
|
| 1214 | - if (!isset($a['taille']) or !$a['taille']) { |
|
| 1215 | - $a['taille'] = strlen($page); # a peu pres |
|
| 1216 | - } |
|
| 1217 | - } |
|
| 1218 | - $a['mime_type'] = $mime_type; |
|
| 1219 | - |
|
| 1220 | - return $a; |
|
| 1086 | + // pas la peine de perdre son temps |
|
| 1087 | + if (!tester_url_absolue($source)) { |
|
| 1088 | + return false; |
|
| 1089 | + } |
|
| 1090 | + |
|
| 1091 | + # charger les alias des types mime |
|
| 1092 | + include_spip('base/typedoc'); |
|
| 1093 | + |
|
| 1094 | + $a = array(); |
|
| 1095 | + $mime_type = ''; |
|
| 1096 | + // On va directement charger le debut des images et des fichiers html, |
|
| 1097 | + // de maniere a attrapper le maximum d'infos (titre, taille, etc). Si |
|
| 1098 | + // ca echoue l'utilisateur devra les entrer... |
|
| 1099 | + if ($headers = recuperer_page($source, false, true, $max, '', '', true)) { |
|
| 1100 | + list($headers, $a['body']) = preg_split(',\n\n,', $headers, 2); |
|
| 1101 | + |
|
| 1102 | + if (preg_match(",\nContent-Type: *([^[:space:];]*),i", "\n$headers", $regs)) { |
|
| 1103 | + $mime_type = (trim($regs[1])); |
|
| 1104 | + } else { |
|
| 1105 | + $mime_type = ''; |
|
| 1106 | + } // inconnu |
|
| 1107 | + |
|
| 1108 | + // Appliquer les alias |
|
| 1109 | + while (isset($GLOBALS['mime_alias'][$mime_type])) { |
|
| 1110 | + $mime_type = $GLOBALS['mime_alias'][$mime_type]; |
|
| 1111 | + } |
|
| 1112 | + |
|
| 1113 | + // Si on a un mime-type insignifiant |
|
| 1114 | + // text/plain,application/octet-stream ou vide |
|
| 1115 | + // c'est peut-etre que le serveur ne sait pas |
|
| 1116 | + // ce qu'il sert ; on va tenter de detecter via l'extension de l'url |
|
| 1117 | + // ou le Content-Disposition: attachment; filename=... |
|
| 1118 | + $t = null; |
|
| 1119 | + if (in_array($mime_type, array('text/plain', '', 'application/octet-stream'))) { |
|
| 1120 | + if (!$t |
|
| 1121 | + and preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $source, $rext) |
|
| 1122 | + ) { |
|
| 1123 | + $t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($rext[1], '', 'text')); |
|
| 1124 | + } |
|
| 1125 | + if (!$t |
|
| 1126 | + and preg_match(',^Content-Disposition:\s*attachment;\s*filename=(.*)$,Uims', $headers, $m) |
|
| 1127 | + and preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $m[1], $rext) |
|
| 1128 | + ) { |
|
| 1129 | + $t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($rext[1], '', 'text')); |
|
| 1130 | + } |
|
| 1131 | + } |
|
| 1132 | + |
|
| 1133 | + // Autre mime/type (ou text/plain avec fichier d'extension inconnue) |
|
| 1134 | + if (!$t) { |
|
| 1135 | + $t = sql_fetsel('extension', 'spip_types_documents', 'mime_type=' . sql_quote($mime_type)); |
|
| 1136 | + } |
|
| 1137 | + |
|
| 1138 | + // Toujours rien ? (ex: audio/x-ogg au lieu de application/ogg) |
|
| 1139 | + // On essaie de nouveau avec l'extension |
|
| 1140 | + if (!$t |
|
| 1141 | + and $mime_type != 'text/plain' |
|
| 1142 | + and preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $source, $rext) |
|
| 1143 | + ) { |
|
| 1144 | + # eviter xxx.3 => 3gp (> SPIP 3) |
|
| 1145 | + $t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($rext[1], '', 'text')); |
|
| 1146 | + } |
|
| 1147 | + |
|
| 1148 | + if ($t) { |
|
| 1149 | + spip_log("mime-type $mime_type ok, extension " . $t['extension']); |
|
| 1150 | + $a['extension'] = $t['extension']; |
|
| 1151 | + } else { |
|
| 1152 | + # par defaut on retombe sur '.bin' si c'est autorise |
|
| 1153 | + spip_log("mime-type $mime_type inconnu"); |
|
| 1154 | + $t = sql_fetsel('extension', 'spip_types_documents', "extension='bin'"); |
|
| 1155 | + if (!$t) { |
|
| 1156 | + return false; |
|
| 1157 | + } |
|
| 1158 | + $a['extension'] = $t['extension']; |
|
| 1159 | + } |
|
| 1160 | + |
|
| 1161 | + if (preg_match(",\nContent-Length: *([^[:space:]]*),i", "\n$headers", $regs)) { |
|
| 1162 | + $a['taille'] = intval($regs[1]); |
|
| 1163 | + } |
|
| 1164 | + } |
|
| 1165 | + |
|
| 1166 | + // Echec avec HEAD, on tente avec GET |
|
| 1167 | + if (!$a and !$max) { |
|
| 1168 | + spip_log("tenter GET $source"); |
|
| 1169 | + $a = recuperer_infos_distantes($source, _INC_DISTANT_MAX_SIZE); |
|
| 1170 | + } |
|
| 1171 | + |
|
| 1172 | + // si on a rien trouve pas la peine d'insister |
|
| 1173 | + if (!$a) { |
|
| 1174 | + return false; |
|
| 1175 | + } |
|
| 1176 | + |
|
| 1177 | + // S'il s'agit d'une image pas trop grosse ou d'un fichier html, on va aller |
|
| 1178 | + // recharger le document en GET et recuperer des donnees supplementaires... |
|
| 1179 | + if (preg_match(',^image/(jpeg|gif|png|swf),', $mime_type)) { |
|
| 1180 | + if ($max == 0 |
|
| 1181 | + and (empty($a['taille']) or $a['taille'] < _INC_DISTANT_MAX_SIZE) |
|
| 1182 | + and isset($GLOBALS['meta']['formats_graphiques']) |
|
| 1183 | + and (strpos($GLOBALS['meta']['formats_graphiques'], $a['extension']) !== false) |
|
| 1184 | + and $charger_si_petite_image |
|
| 1185 | + ) { |
|
| 1186 | + $a = recuperer_infos_distantes($source, _INC_DISTANT_MAX_SIZE); |
|
| 1187 | + } else { |
|
| 1188 | + if ($a['body']) { |
|
| 1189 | + $a['fichier'] = _DIR_RACINE . nom_fichier_copie_locale($source, $a['extension']); |
|
| 1190 | + ecrire_fichier($a['fichier'], $a['body']); |
|
| 1191 | + $size_image = @getimagesize($a['fichier']); |
|
| 1192 | + $a['largeur'] = intval($size_image[0]); |
|
| 1193 | + $a['hauteur'] = intval($size_image[1]); |
|
| 1194 | + $a['type_image'] = true; |
|
| 1195 | + } |
|
| 1196 | + } |
|
| 1197 | + } |
|
| 1198 | + |
|
| 1199 | + // Fichier swf, si on n'a pas la taille, on va mettre 425x350 par defaut |
|
| 1200 | + // ce sera mieux que 0x0 |
|
| 1201 | + if ($a and isset($a['extension']) and $a['extension'] == 'swf' |
|
| 1202 | + and empty($a['largeur']) |
|
| 1203 | + ) { |
|
| 1204 | + $a['largeur'] = 425; |
|
| 1205 | + $a['hauteur'] = 350; |
|
| 1206 | + } |
|
| 1207 | + |
|
| 1208 | + if ($mime_type == 'text/html') { |
|
| 1209 | + include_spip('inc/filtres'); |
|
| 1210 | + $page = recuperer_page($source, true, false, _INC_DISTANT_MAX_SIZE); |
|
| 1211 | + if (preg_match(',<title>(.*?)</title>,ims', $page, $regs)) { |
|
| 1212 | + $a['titre'] = corriger_caracteres(trim($regs[1])); |
|
| 1213 | + } |
|
| 1214 | + if (!isset($a['taille']) or !$a['taille']) { |
|
| 1215 | + $a['taille'] = strlen($page); # a peu pres |
|
| 1216 | + } |
|
| 1217 | + } |
|
| 1218 | + $a['mime_type'] = $mime_type; |
|
| 1219 | + |
|
| 1220 | + return $a; |
|
| 1221 | 1221 | } |
| 1222 | 1222 | |
| 1223 | 1223 | |
@@ -1233,45 +1233,45 @@ discard block |
||
| 1233 | 1233 | * @return string |
| 1234 | 1234 | */ |
| 1235 | 1235 | function need_proxy($host, $http_proxy = null, $http_noproxy = null) { |
| 1236 | - if (is_null($http_proxy)) { |
|
| 1237 | - $http_proxy = isset($GLOBALS['meta']['http_proxy']) ? $GLOBALS['meta']['http_proxy'] : null; |
|
| 1238 | - } |
|
| 1239 | - // rien a faire si pas de proxy :) |
|
| 1240 | - if (is_null($http_proxy) or !$http_proxy = trim($http_proxy)) { |
|
| 1241 | - return ''; |
|
| 1242 | - } |
|
| 1243 | - |
|
| 1244 | - if (is_null($http_noproxy)) { |
|
| 1245 | - $http_noproxy = isset($GLOBALS['meta']['http_noproxy']) ? $GLOBALS['meta']['http_noproxy'] : null; |
|
| 1246 | - } |
|
| 1247 | - // si pas d'exception, on retourne le proxy |
|
| 1248 | - if (is_null($http_noproxy) or !$http_noproxy = trim($http_noproxy)) { |
|
| 1249 | - return $http_proxy; |
|
| 1250 | - } |
|
| 1251 | - |
|
| 1252 | - // si le host ou l'un des domaines parents est dans $http_noproxy on fait exception |
|
| 1253 | - // $http_noproxy peut contenir plusieurs domaines separes par des espaces ou retour ligne |
|
| 1254 | - $http_noproxy = str_replace("\n", " ", $http_noproxy); |
|
| 1255 | - $http_noproxy = str_replace("\r", " ", $http_noproxy); |
|
| 1256 | - $http_noproxy = " $http_noproxy "; |
|
| 1257 | - $domain = $host; |
|
| 1258 | - // si le domaine exact www.example.org est dans les exceptions |
|
| 1259 | - if (strpos($http_noproxy, " $domain ") !== false) |
|
| 1260 | - return ''; |
|
| 1261 | - |
|
| 1262 | - while (strpos($domain, '.') !== false) { |
|
| 1263 | - $domain = explode('.', $domain); |
|
| 1264 | - array_shift($domain); |
|
| 1265 | - $domain = implode('.', $domain); |
|
| 1266 | - |
|
| 1267 | - // ou si un domaine parent commencant par un . est dans les exceptions (indiquant qu'il couvre tous les sous-domaines) |
|
| 1268 | - if (strpos($http_noproxy, " .$domain ") !== false) { |
|
| 1269 | - return ''; |
|
| 1270 | - } |
|
| 1271 | - } |
|
| 1272 | - |
|
| 1273 | - // ok c'est pas une exception |
|
| 1274 | - return $http_proxy; |
|
| 1236 | + if (is_null($http_proxy)) { |
|
| 1237 | + $http_proxy = isset($GLOBALS['meta']['http_proxy']) ? $GLOBALS['meta']['http_proxy'] : null; |
|
| 1238 | + } |
|
| 1239 | + // rien a faire si pas de proxy :) |
|
| 1240 | + if (is_null($http_proxy) or !$http_proxy = trim($http_proxy)) { |
|
| 1241 | + return ''; |
|
| 1242 | + } |
|
| 1243 | + |
|
| 1244 | + if (is_null($http_noproxy)) { |
|
| 1245 | + $http_noproxy = isset($GLOBALS['meta']['http_noproxy']) ? $GLOBALS['meta']['http_noproxy'] : null; |
|
| 1246 | + } |
|
| 1247 | + // si pas d'exception, on retourne le proxy |
|
| 1248 | + if (is_null($http_noproxy) or !$http_noproxy = trim($http_noproxy)) { |
|
| 1249 | + return $http_proxy; |
|
| 1250 | + } |
|
| 1251 | + |
|
| 1252 | + // si le host ou l'un des domaines parents est dans $http_noproxy on fait exception |
|
| 1253 | + // $http_noproxy peut contenir plusieurs domaines separes par des espaces ou retour ligne |
|
| 1254 | + $http_noproxy = str_replace("\n", " ", $http_noproxy); |
|
| 1255 | + $http_noproxy = str_replace("\r", " ", $http_noproxy); |
|
| 1256 | + $http_noproxy = " $http_noproxy "; |
|
| 1257 | + $domain = $host; |
|
| 1258 | + // si le domaine exact www.example.org est dans les exceptions |
|
| 1259 | + if (strpos($http_noproxy, " $domain ") !== false) |
|
| 1260 | + return ''; |
|
| 1261 | + |
|
| 1262 | + while (strpos($domain, '.') !== false) { |
|
| 1263 | + $domain = explode('.', $domain); |
|
| 1264 | + array_shift($domain); |
|
| 1265 | + $domain = implode('.', $domain); |
|
| 1266 | + |
|
| 1267 | + // ou si un domaine parent commencant par un . est dans les exceptions (indiquant qu'il couvre tous les sous-domaines) |
|
| 1268 | + if (strpos($http_noproxy, " .$domain ") !== false) { |
|
| 1269 | + return ''; |
|
| 1270 | + } |
|
| 1271 | + } |
|
| 1272 | + |
|
| 1273 | + // ok c'est pas une exception |
|
| 1274 | + return $http_proxy; |
|
| 1275 | 1275 | } |
| 1276 | 1276 | |
| 1277 | 1277 | |
@@ -1294,58 +1294,58 @@ discard block |
||
| 1294 | 1294 | * @return array |
| 1295 | 1295 | */ |
| 1296 | 1296 | function init_http($method, $url, $refuse_gz = false, $referer = '', $datas = '', $vers = 'HTTP/1.0', $date = '') { |
| 1297 | - $user = $via_proxy = $proxy_user = ''; |
|
| 1298 | - $fopen = false; |
|
| 1299 | - |
|
| 1300 | - $t = @parse_url($url); |
|
| 1301 | - $host = $t['host']; |
|
| 1302 | - if ($t['scheme'] == 'http') { |
|
| 1303 | - $scheme = 'http'; |
|
| 1304 | - $noproxy = ''; |
|
| 1305 | - } elseif ($t['scheme'] == 'https') { |
|
| 1306 | - $scheme = 'ssl'; |
|
| 1307 | - $noproxy = 'ssl://'; |
|
| 1308 | - if (!isset($t['port']) || !($port = $t['port'])) { |
|
| 1309 | - $t['port'] = 443; |
|
| 1310 | - } |
|
| 1311 | - } else { |
|
| 1312 | - $scheme = $t['scheme']; |
|
| 1313 | - $noproxy = $scheme . '://'; |
|
| 1314 | - } |
|
| 1315 | - if (isset($t['user'])) { |
|
| 1316 | - $user = array($t['user'], $t['pass']); |
|
| 1317 | - } |
|
| 1318 | - |
|
| 1319 | - if (!isset($t['port']) || !($port = $t['port'])) { |
|
| 1320 | - $port = 80; |
|
| 1321 | - } |
|
| 1322 | - if (!isset($t['path']) || !($path = $t['path'])) { |
|
| 1323 | - $path = '/'; |
|
| 1324 | - } |
|
| 1325 | - |
|
| 1326 | - if (!empty($t['query'])) { |
|
| 1327 | - $path .= '?' . $t['query']; |
|
| 1328 | - } |
|
| 1329 | - |
|
| 1330 | - $f = lance_requete($method, $scheme, $user, $host, $path, $port, $noproxy, $refuse_gz, $referer, $datas, $vers, $date); |
|
| 1331 | - if (!$f or !is_resource($f)) { |
|
| 1332 | - // fallback : fopen si on a pas fait timeout dans lance_requete |
|
| 1333 | - // ce qui correspond a $f===110 |
|
| 1334 | - if ($f !== 110 |
|
| 1335 | - and !need_proxy($host) |
|
| 1336 | - and !_request('tester_proxy') |
|
| 1337 | - and (!isset($GLOBALS['inc_distant_allow_fopen']) or $GLOBALS['inc_distant_allow_fopen']) |
|
| 1338 | - ) { |
|
| 1339 | - $f = @fopen($url, 'rb'); |
|
| 1340 | - spip_log("connexion vers $url par simple fopen"); |
|
| 1341 | - $fopen = true; |
|
| 1342 | - } else { |
|
| 1343 | - // echec total |
|
| 1344 | - $f = false; |
|
| 1345 | - } |
|
| 1346 | - } |
|
| 1347 | - |
|
| 1348 | - return array($f, $fopen); |
|
| 1297 | + $user = $via_proxy = $proxy_user = ''; |
|
| 1298 | + $fopen = false; |
|
| 1299 | + |
|
| 1300 | + $t = @parse_url($url); |
|
| 1301 | + $host = $t['host']; |
|
| 1302 | + if ($t['scheme'] == 'http') { |
|
| 1303 | + $scheme = 'http'; |
|
| 1304 | + $noproxy = ''; |
|
| 1305 | + } elseif ($t['scheme'] == 'https') { |
|
| 1306 | + $scheme = 'ssl'; |
|
| 1307 | + $noproxy = 'ssl://'; |
|
| 1308 | + if (!isset($t['port']) || !($port = $t['port'])) { |
|
| 1309 | + $t['port'] = 443; |
|
| 1310 | + } |
|
| 1311 | + } else { |
|
| 1312 | + $scheme = $t['scheme']; |
|
| 1313 | + $noproxy = $scheme . '://'; |
|
| 1314 | + } |
|
| 1315 | + if (isset($t['user'])) { |
|
| 1316 | + $user = array($t['user'], $t['pass']); |
|
| 1317 | + } |
|
| 1318 | + |
|
| 1319 | + if (!isset($t['port']) || !($port = $t['port'])) { |
|
| 1320 | + $port = 80; |
|
| 1321 | + } |
|
| 1322 | + if (!isset($t['path']) || !($path = $t['path'])) { |
|
| 1323 | + $path = '/'; |
|
| 1324 | + } |
|
| 1325 | + |
|
| 1326 | + if (!empty($t['query'])) { |
|
| 1327 | + $path .= '?' . $t['query']; |
|
| 1328 | + } |
|
| 1329 | + |
|
| 1330 | + $f = lance_requete($method, $scheme, $user, $host, $path, $port, $noproxy, $refuse_gz, $referer, $datas, $vers, $date); |
|
| 1331 | + if (!$f or !is_resource($f)) { |
|
| 1332 | + // fallback : fopen si on a pas fait timeout dans lance_requete |
|
| 1333 | + // ce qui correspond a $f===110 |
|
| 1334 | + if ($f !== 110 |
|
| 1335 | + and !need_proxy($host) |
|
| 1336 | + and !_request('tester_proxy') |
|
| 1337 | + and (!isset($GLOBALS['inc_distant_allow_fopen']) or $GLOBALS['inc_distant_allow_fopen']) |
|
| 1338 | + ) { |
|
| 1339 | + $f = @fopen($url, 'rb'); |
|
| 1340 | + spip_log("connexion vers $url par simple fopen"); |
|
| 1341 | + $fopen = true; |
|
| 1342 | + } else { |
|
| 1343 | + // echec total |
|
| 1344 | + $f = false; |
|
| 1345 | + } |
|
| 1346 | + } |
|
| 1347 | + |
|
| 1348 | + return array($f, $fopen); |
|
| 1349 | 1349 | } |
| 1350 | 1350 | |
| 1351 | 1351 | /** |
@@ -1380,123 +1380,123 @@ discard block |
||
| 1380 | 1380 | * resource socket vers l'url demandee |
| 1381 | 1381 | */ |
| 1382 | 1382 | function lance_requete( |
| 1383 | - $method, |
|
| 1384 | - $scheme, |
|
| 1385 | - $user, |
|
| 1386 | - $host, |
|
| 1387 | - $path, |
|
| 1388 | - $port, |
|
| 1389 | - $noproxy, |
|
| 1390 | - $refuse_gz = false, |
|
| 1391 | - $referer = '', |
|
| 1392 | - $datas = '', |
|
| 1393 | - $vers = 'HTTP/1.0', |
|
| 1394 | - $date = '' |
|
| 1383 | + $method, |
|
| 1384 | + $scheme, |
|
| 1385 | + $user, |
|
| 1386 | + $host, |
|
| 1387 | + $path, |
|
| 1388 | + $port, |
|
| 1389 | + $noproxy, |
|
| 1390 | + $refuse_gz = false, |
|
| 1391 | + $referer = '', |
|
| 1392 | + $datas = '', |
|
| 1393 | + $vers = 'HTTP/1.0', |
|
| 1394 | + $date = '' |
|
| 1395 | 1395 | ) { |
| 1396 | 1396 | |
| 1397 | - $proxy_user = ''; |
|
| 1398 | - $http_proxy = need_proxy($host); |
|
| 1399 | - if ($user) { |
|
| 1400 | - $user = urlencode($user[0]) . ':' . urlencode($user[1]); |
|
| 1401 | - } |
|
| 1402 | - |
|
| 1403 | - $connect = ''; |
|
| 1404 | - if ($http_proxy) { |
|
| 1405 | - if (defined('_PROXY_HTTPS_VIA_CONNECT') and in_array($scheme , array('tls','ssl'))) { |
|
| 1406 | - $path_host = (!$user ? '' : "$user@") . $host . (($port != 80) ? ":$port" : ''); |
|
| 1407 | - $connect = 'CONNECT ' . $path_host . " $vers\r\n" |
|
| 1408 | - . "Host: $path_host\r\n" |
|
| 1409 | - . "Proxy-Connection: Keep-Alive\r\n"; |
|
| 1410 | - } else { |
|
| 1411 | - $path = (in_array($scheme , array('tls','ssl')) ? 'https://' : "$scheme://") |
|
| 1412 | - . (!$user ? '' : "$user@") |
|
| 1413 | - . "$host" . (($port != 80) ? ":$port" : '') . $path; |
|
| 1414 | - } |
|
| 1415 | - $t2 = @parse_url($http_proxy); |
|
| 1416 | - $first_host = $t2['host']; |
|
| 1417 | - if (!($port = $t2['port'])) { |
|
| 1418 | - $port = 80; |
|
| 1419 | - } |
|
| 1420 | - if ($t2['user']) { |
|
| 1421 | - $proxy_user = base64_encode($t2['user'] . ':' . $t2['pass']); |
|
| 1422 | - } |
|
| 1423 | - } else { |
|
| 1424 | - $first_host = $noproxy . $host; |
|
| 1425 | - } |
|
| 1426 | - |
|
| 1427 | - if ($connect) { |
|
| 1428 | - $streamContext = stream_context_create(array( |
|
| 1429 | - 'ssl' => array( |
|
| 1430 | - 'verify_peer' => false, |
|
| 1431 | - 'allow_self_signed' => true, |
|
| 1432 | - 'SNI_enabled' => true, |
|
| 1433 | - 'peer_name' => $host, |
|
| 1434 | - ) |
|
| 1435 | - )); |
|
| 1436 | - if (version_compare(phpversion(), '5.6', '<')) { |
|
| 1437 | - stream_context_set_option($streamContext, 'ssl', 'SNI_server_name', $host); |
|
| 1438 | - } |
|
| 1439 | - $f = @stream_socket_client( |
|
| 1440 | - "tcp://$first_host:$port", |
|
| 1441 | - $errno, |
|
| 1442 | - $errstr, |
|
| 1443 | - _INC_DISTANT_CONNECT_TIMEOUT, |
|
| 1444 | - STREAM_CLIENT_CONNECT, |
|
| 1445 | - $streamContext |
|
| 1446 | - ); |
|
| 1447 | - spip_log("Recuperer $path sur $first_host:$port par $f (via CONNECT)", 'connect'); |
|
| 1448 | - if (!$f) { |
|
| 1449 | - spip_log("Erreur connexion $errno $errstr", _LOG_ERREUR); |
|
| 1450 | - return $errno; |
|
| 1451 | - } |
|
| 1452 | - stream_set_timeout($f, _INC_DISTANT_CONNECT_TIMEOUT); |
|
| 1453 | - |
|
| 1454 | - fputs($f, $connect); |
|
| 1455 | - fputs($f, "\r\n"); |
|
| 1456 | - $res = fread($f, 1024); |
|
| 1457 | - if (!$res |
|
| 1458 | - or !count($res = explode(' ', $res)) |
|
| 1459 | - or $res[1] !== '200' |
|
| 1460 | - ) { |
|
| 1461 | - spip_log("Echec CONNECT sur $first_host:$port", 'connect' . _LOG_INFO_IMPORTANTE); |
|
| 1462 | - fclose($f); |
|
| 1463 | - |
|
| 1464 | - return false; |
|
| 1465 | - } |
|
| 1466 | - // important, car sinon on lit trop vite et les donnees ne sont pas encore dispo |
|
| 1467 | - stream_set_blocking($f, true); |
|
| 1468 | - // envoyer le handshake |
|
| 1469 | - stream_socket_enable_crypto($f, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT); |
|
| 1470 | - spip_log("OK CONNECT sur $first_host:$port", 'connect'); |
|
| 1471 | - } else { |
|
| 1472 | - $ntry = 3; |
|
| 1473 | - do { |
|
| 1474 | - $f = @fsockopen($first_host, $port, $errno, $errstr, _INC_DISTANT_CONNECT_TIMEOUT); |
|
| 1475 | - } while (!$f and $ntry-- and $errno !== 110 and sleep(1)); |
|
| 1476 | - spip_log("Recuperer $path sur $first_host:$port par $f"); |
|
| 1477 | - if (!$f) { |
|
| 1478 | - spip_log("Erreur connexion $errno $errstr", _LOG_ERREUR); |
|
| 1479 | - |
|
| 1480 | - return $errno; |
|
| 1481 | - } |
|
| 1482 | - stream_set_timeout($f, _INC_DISTANT_CONNECT_TIMEOUT); |
|
| 1483 | - } |
|
| 1484 | - |
|
| 1485 | - $site = isset($GLOBALS['meta']['adresse_site']) ? $GLOBALS['meta']['adresse_site'] : ''; |
|
| 1486 | - |
|
| 1487 | - $req = "$method $path $vers\r\n" |
|
| 1488 | - . "Host: $host\r\n" |
|
| 1489 | - . 'User-Agent: ' . _INC_DISTANT_USER_AGENT . "\r\n" |
|
| 1490 | - . ($refuse_gz ? '' : ('Accept-Encoding: ' . _INC_DISTANT_CONTENT_ENCODING . "\r\n")) |
|
| 1491 | - . (!$site ? '' : "Referer: $site/$referer\r\n") |
|
| 1492 | - . (!$date ? '' : 'If-Modified-Since: ' . (gmdate('D, d M Y H:i:s', $date) . " GMT\r\n")) |
|
| 1493 | - . (!$user ? '' : ('Authorization: Basic ' . base64_encode($user) . "\r\n")) |
|
| 1494 | - . (!$proxy_user ? '' : "Proxy-Authorization: Basic $proxy_user\r\n") |
|
| 1495 | - . (!strpos($vers, '1.1') ? '' : "Keep-Alive: 300\r\nConnection: keep-alive\r\n"); |
|
| 1397 | + $proxy_user = ''; |
|
| 1398 | + $http_proxy = need_proxy($host); |
|
| 1399 | + if ($user) { |
|
| 1400 | + $user = urlencode($user[0]) . ':' . urlencode($user[1]); |
|
| 1401 | + } |
|
| 1402 | + |
|
| 1403 | + $connect = ''; |
|
| 1404 | + if ($http_proxy) { |
|
| 1405 | + if (defined('_PROXY_HTTPS_VIA_CONNECT') and in_array($scheme , array('tls','ssl'))) { |
|
| 1406 | + $path_host = (!$user ? '' : "$user@") . $host . (($port != 80) ? ":$port" : ''); |
|
| 1407 | + $connect = 'CONNECT ' . $path_host . " $vers\r\n" |
|
| 1408 | + . "Host: $path_host\r\n" |
|
| 1409 | + . "Proxy-Connection: Keep-Alive\r\n"; |
|
| 1410 | + } else { |
|
| 1411 | + $path = (in_array($scheme , array('tls','ssl')) ? 'https://' : "$scheme://") |
|
| 1412 | + . (!$user ? '' : "$user@") |
|
| 1413 | + . "$host" . (($port != 80) ? ":$port" : '') . $path; |
|
| 1414 | + } |
|
| 1415 | + $t2 = @parse_url($http_proxy); |
|
| 1416 | + $first_host = $t2['host']; |
|
| 1417 | + if (!($port = $t2['port'])) { |
|
| 1418 | + $port = 80; |
|
| 1419 | + } |
|
| 1420 | + if ($t2['user']) { |
|
| 1421 | + $proxy_user = base64_encode($t2['user'] . ':' . $t2['pass']); |
|
| 1422 | + } |
|
| 1423 | + } else { |
|
| 1424 | + $first_host = $noproxy . $host; |
|
| 1425 | + } |
|
| 1426 | + |
|
| 1427 | + if ($connect) { |
|
| 1428 | + $streamContext = stream_context_create(array( |
|
| 1429 | + 'ssl' => array( |
|
| 1430 | + 'verify_peer' => false, |
|
| 1431 | + 'allow_self_signed' => true, |
|
| 1432 | + 'SNI_enabled' => true, |
|
| 1433 | + 'peer_name' => $host, |
|
| 1434 | + ) |
|
| 1435 | + )); |
|
| 1436 | + if (version_compare(phpversion(), '5.6', '<')) { |
|
| 1437 | + stream_context_set_option($streamContext, 'ssl', 'SNI_server_name', $host); |
|
| 1438 | + } |
|
| 1439 | + $f = @stream_socket_client( |
|
| 1440 | + "tcp://$first_host:$port", |
|
| 1441 | + $errno, |
|
| 1442 | + $errstr, |
|
| 1443 | + _INC_DISTANT_CONNECT_TIMEOUT, |
|
| 1444 | + STREAM_CLIENT_CONNECT, |
|
| 1445 | + $streamContext |
|
| 1446 | + ); |
|
| 1447 | + spip_log("Recuperer $path sur $first_host:$port par $f (via CONNECT)", 'connect'); |
|
| 1448 | + if (!$f) { |
|
| 1449 | + spip_log("Erreur connexion $errno $errstr", _LOG_ERREUR); |
|
| 1450 | + return $errno; |
|
| 1451 | + } |
|
| 1452 | + stream_set_timeout($f, _INC_DISTANT_CONNECT_TIMEOUT); |
|
| 1453 | + |
|
| 1454 | + fputs($f, $connect); |
|
| 1455 | + fputs($f, "\r\n"); |
|
| 1456 | + $res = fread($f, 1024); |
|
| 1457 | + if (!$res |
|
| 1458 | + or !count($res = explode(' ', $res)) |
|
| 1459 | + or $res[1] !== '200' |
|
| 1460 | + ) { |
|
| 1461 | + spip_log("Echec CONNECT sur $first_host:$port", 'connect' . _LOG_INFO_IMPORTANTE); |
|
| 1462 | + fclose($f); |
|
| 1463 | + |
|
| 1464 | + return false; |
|
| 1465 | + } |
|
| 1466 | + // important, car sinon on lit trop vite et les donnees ne sont pas encore dispo |
|
| 1467 | + stream_set_blocking($f, true); |
|
| 1468 | + // envoyer le handshake |
|
| 1469 | + stream_socket_enable_crypto($f, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT); |
|
| 1470 | + spip_log("OK CONNECT sur $first_host:$port", 'connect'); |
|
| 1471 | + } else { |
|
| 1472 | + $ntry = 3; |
|
| 1473 | + do { |
|
| 1474 | + $f = @fsockopen($first_host, $port, $errno, $errstr, _INC_DISTANT_CONNECT_TIMEOUT); |
|
| 1475 | + } while (!$f and $ntry-- and $errno !== 110 and sleep(1)); |
|
| 1476 | + spip_log("Recuperer $path sur $first_host:$port par $f"); |
|
| 1477 | + if (!$f) { |
|
| 1478 | + spip_log("Erreur connexion $errno $errstr", _LOG_ERREUR); |
|
| 1479 | + |
|
| 1480 | + return $errno; |
|
| 1481 | + } |
|
| 1482 | + stream_set_timeout($f, _INC_DISTANT_CONNECT_TIMEOUT); |
|
| 1483 | + } |
|
| 1484 | + |
|
| 1485 | + $site = isset($GLOBALS['meta']['adresse_site']) ? $GLOBALS['meta']['adresse_site'] : ''; |
|
| 1486 | + |
|
| 1487 | + $req = "$method $path $vers\r\n" |
|
| 1488 | + . "Host: $host\r\n" |
|
| 1489 | + . 'User-Agent: ' . _INC_DISTANT_USER_AGENT . "\r\n" |
|
| 1490 | + . ($refuse_gz ? '' : ('Accept-Encoding: ' . _INC_DISTANT_CONTENT_ENCODING . "\r\n")) |
|
| 1491 | + . (!$site ? '' : "Referer: $site/$referer\r\n") |
|
| 1492 | + . (!$date ? '' : 'If-Modified-Since: ' . (gmdate('D, d M Y H:i:s', $date) . " GMT\r\n")) |
|
| 1493 | + . (!$user ? '' : ('Authorization: Basic ' . base64_encode($user) . "\r\n")) |
|
| 1494 | + . (!$proxy_user ? '' : "Proxy-Authorization: Basic $proxy_user\r\n") |
|
| 1495 | + . (!strpos($vers, '1.1') ? '' : "Keep-Alive: 300\r\nConnection: keep-alive\r\n"); |
|
| 1496 | 1496 | |
| 1497 | 1497 | # spip_log("Requete\n$req"); |
| 1498 | - fputs($f, $req); |
|
| 1499 | - fputs($f, $datas ? $datas : "\r\n"); |
|
| 1498 | + fputs($f, $req); |
|
| 1499 | + fputs($f, $datas ? $datas : "\r\n"); |
|
| 1500 | 1500 | |
| 1501 | - return $f; |
|
| 1501 | + return $f; |
|
| 1502 | 1502 | } |
@@ -1256,8 +1256,9 @@ |
||
| 1256 | 1256 | $http_noproxy = " $http_noproxy "; |
| 1257 | 1257 | $domain = $host; |
| 1258 | 1258 | // si le domaine exact www.example.org est dans les exceptions |
| 1259 | - if (strpos($http_noproxy, " $domain ") !== false) |
|
| 1260 | - return ''; |
|
| 1259 | + if (strpos($http_noproxy, " $domain ") !== false) { |
|
| 1260 | + return ''; |
|
| 1261 | + } |
|
| 1261 | 1262 | |
| 1262 | 1263 | while (strpos($domain, '.') !== false) { |
| 1263 | 1264 | $domain = explode('.', $domain); |