Skip to content
Extraits de code Groupes Projets
Valider d58be583 rédigé par JamesRezo's avatar JamesRezo :tada:
Parcourir les fichiers

faire des post avec la lib inc_distant.php: preparation des donnees de POST (debut timide)

parent 9d7814d8
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -74,6 +74,42 @@ function buildQueryString($data, $munge_charset = false) { ...@@ -74,6 +74,42 @@ function buildQueryString($data, $munge_charset = false) {
return $querystring; return $querystring;
} }
function prepare_donnees_post($donnees, $boundary = '') {
if($boundary) {
// fabrique une chaine HTTP pour un POST avec boundary
$entete = "Content-Type: multipart/form-data; boundary=$boundary\r\n";
$chaine = '';
if (is_array($donnees)) {
foreach ($donnees as $cle => $valeur) {
$chaine .= "\r\n--$boundary\r\n";
$chaine .= "Content-Disposition: form-data; name=\"$cle\"\r\n";
$chaine .= "\r\n";
$chaine .= $valeur;
}
$chaine .= "\r\n--$boundary\r\n";
}
} else {
// fabrique une chaine HTTP simple pour un POST
$entete = 'Content-Type: application/x-www-form-urlencoded'."\r\n";
$chaine = array();
if (is_array($donnees)) {
foreach ($donnees as $cle => $valeur) {
if (is_array($valeur)) {
foreach ($valeur as $val2) {
$chaine[] = urlencode($cle).'='.urlencode($val);
}
} else {
$chaine[] = urlencode($cle).'='.urlencode($valeur);
}
}
$chaine = implode('&', $chaine);
} else {
$chaine = $donnees;
}
}
return array($entete, $chaine);
}
// //
// Recupere une page sur le net // Recupere une page sur le net
// et au besoin l'encode dans le charset local // et au besoin l'encode dans le charset local
...@@ -94,7 +130,7 @@ function recuperer_page($url, $munge_charset=false, $get_headers=false, $taille_ ...@@ -94,7 +130,7 @@ function recuperer_page($url, $munge_charset=false, $get_headers=false, $taille_
if (!empty($datas) && is_array($datas)) { if (!empty($datas) && is_array($datas)) {
$get = 'POST'; $get = 'POST';
$postdata = buildQueryString($datas, $munge_charset); list($content_type, $postdata) = prepare_donnees_post($datas);
} }
for ($i=0;$i<10;$i++) { // dix tentatives maximum en cas d'entetes 301... for ($i=0;$i<10;$i++) { // dix tentatives maximum en cas d'entetes 301...
...@@ -107,7 +143,7 @@ function recuperer_page($url, $munge_charset=false, $get_headers=false, $taille_ ...@@ -107,7 +143,7 @@ function recuperer_page($url, $munge_charset=false, $get_headers=false, $taille_
} else { } else {
// Fin des entetes envoyees par SPIP // Fin des entetes envoyees par SPIP
if($get == 'POST') { if($get == 'POST') {
fputs($f, 'Content-Type: application/x-www-form-urlencoded'."\r\n"); fputs($f, $content_type);
fputs($f, 'Content-Length: '.strlen($postdata)."\r\n"); fputs($f, 'Content-Length: '.strlen($postdata)."\r\n");
fputs($f, "\r\n".$postdata); fputs($f, "\r\n".$postdata);
} else { } else {
......
...@@ -164,14 +164,17 @@ function post_ortho($url, $texte, $lang) { ...@@ -164,14 +164,17 @@ function post_ortho($url, $texte, $lang) {
return false; return false;
} }
$gz = ($GLOBALS['flag_gz'] && strlen($texte) >= 200);
$boundary = '';
$vars = array(
'op' => 'spell',
'lang' => $lang,
'texte' => $texte,
'gz' => $gz ? 1 : 0
);
// Si le texte est petit, l'overhead du multipart est dispendieux // Si le texte est petit, l'overhead du multipart est dispendieux
if (!$GLOBALS['flag_gz'] || strlen($texte) < 200) {
$gz = false;
$body = "op=spell&lang=".urlencode($lang)."&texte=".urlencode($texte);
fputs($f, "Content-Type: application/x-www-form-urlencoded\r\n");
}
// Sinon, on passe en multipart pour compresser la chaine a corriger // Sinon, on passe en multipart pour compresser la chaine a corriger
else { if ($gz) {
// Il faut eliminer les caracteres 0 sinon PHP ne lit pas la suite du parametre // Il faut eliminer les caracteres 0 sinon PHP ne lit pas la suite du parametre
// passe en multipart/form-data (gros hack bien sale) // passe en multipart/form-data (gros hack bien sale)
$texte_gz = gzcompress($texte); $texte_gz = gzcompress($texte);
...@@ -180,21 +183,14 @@ function post_ortho($url, $texte, $lang) { ...@@ -180,21 +183,14 @@ function post_ortho($url, $texte, $lang) {
if (!is_int(strpos($texte_gz, $str_echap))) break; if (!is_int(strpos($texte_gz, $str_echap))) break;
} }
$texte_gz = str_replace("\x00", $str_echap, $texte_gz); $texte_gz = str_replace("\x00", $str_echap, $texte_gz);
$gz = true; $vars['texte'] = $texte_gz;
$vars = array('op' => 'spell', 'lang' => $lang, 'texte' => $texte_gz, 'gz' => 1, 'nul_echap' => $str_echap); $vars['nul_echap'] = $str_echap;
$boundary = substr(md5(rand().'ortho'), 0, 8); $boundary = substr(md5(rand().'ortho'), 0, 8);
$body = '';
foreach ($vars as $key => $val) {
$body .= "\r\n--$boundary\r\n";
$body .= "Content-Disposition: form-data; name=\"$key\"\r\n";
$body .= "\r\n";
$body .= $val;
}
$body .= "\r\n--$boundary\r\n";
fputs($f, "Content-Type: multipart/form-data; boundary=$boundary\r\n");
} }
list($content_type, $body) = prepare_donnees_post($vars, $boundary);
// On envoie le contenu // On envoie le contenu
fputs($f, $content_type);
fputs($f, "Content-Length: ".strlen($body)."\r\n"); fputs($f, "Content-Length: ".strlen($body)."\r\n");
fputs($f, "\r\n"); // Fin des entetes fputs($f, "\r\n"); // Fin des entetes
fputs($f, $body); fputs($f, $body);
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter