//le titre
array_push($cherche, "NOM_SITE"); array_push($remplace, formate_pour_rtf('#NOM_SITE_SPIP'));
array_push($cherche, "URL_ARTICLE"); array_push($remplace, formate_pour_rtf('[(#URL_SITE_SPIP|texte_script)]/[(#URL_ARTICLE|texte_script)]'));
array_push($cherche, "SURTITRE"); array_push($remplace, formate_pour_rtf('[(#SURTITRE|texte_script)]'));
array_push($cherche, "TITRE"); array_push($remplace, formate_pour_rtf('[(#TITRE|supprimer_numero|texte_script)]'));
array_push($cherche, "DATE"); array_push($remplace, formate_pour_rtf('[(#DATE|affdate)]'));
array_push($cherche, "CHAPO"); array_push($remplace, formate_pour_rtf('[(#CHAPO|texte_script)]'));
array_push($cherche, "DESCRIPTIF"); array_push($remplace, formate_pour_rtf('[(#DESCRIPTIF|texte_script)]'));
array_push($cherche, "TEXTE"); array_push($remplace, formate_pour_rtf('[(#TEXTE|texte_script)]'));
array_push($cherche, "PS"); array_push($remplace, formate_pour_rtf('[(#PS|texte_script)]'));
$AUTEURS = $AUTEURS.formate_pour_rtf('#NOM').", ";
array_push($cherche, "AUTEURS"); array_push($remplace, substr($AUTEURS, 0, -2));
//on va créer le fichier rtf sur la base du numero d'article
$id_article = #ID_ARTICLE;
$path_lib = "libs/RTF/";
$path_rtf = "IMG/_article_RTF/";
$files_rtf = $path_rtf."article_".$id_article.".rtf";
$handle = fopen($path_lib."Article.rtf", "r");
$contents = fread($handle, filesize($path_lib."Article.rtf"));
fclose($handle);
$contents = str_replace($cherche, $remplace, $contents);
//puis on va écrire le document rtf assez brutalement
$fp = fopen($files_rtf, "w");
fwrite($fp, $contents);
fclose($fp);
header("location: ".$files_rtf."" );
function formate_pour_rtf($texte){
global $racine;
//les caracteres accentué comme les entité html corrrespondante
//doivent etre reconvertie
$motif = array(
"é", "é", "é",
"è", "è", "è",
"ê", "ê", "ê",
"à", "à", "à",
"â", "â", "â",
"î", "î", "î",
"ô", "ô", "ô",
"ù", "ù", "ù",
"&", "&", //& doit etre reconverti en &
"<", "<", //< doit etre reconverti en <
">", ">", //> doit etre reconverti en >
"«", "«", //le guillmet ouvrant doit etre reconverti en guillemet simple
"»", "»", //le guillmet fermant doit etre reconverti en guillemet simple
""", """, //le guillmet SIMPLE doit etre reconverti en guillemet simple
"{", "}", //devient \{ et \}
"’",
" "
);
$remplacement = array(
"\\'e9", "\\'e9", "\\'e9", //é
"\\'e8", "\\'e8", "\\'e8", //è
"\\'ea", "\\'ea", "\\'ea", //ê
"\\'e0", "\\'e0", "\\'e0", //à
"\\'e2", "\\'e2", "\\'e2", //â
"\\'ee", "\\'ee", "\\'ee", //î
"\\'f4", "\\'f4", "\\'f4", //ô
"\\'f9", "\\'f9", "\\'f9", //ù
"&", "&",
"<", "<",
">", ">",
"\"", "\"",
"\"", "\"",
"\"", "\"",
"\\{", "\\}",
"'",
" "
);
$texte=str_replace($motif,$remplacement,$texte);
//Il faut maintenant reconvertir tous les
et
en passage à la ligne
$br = array("
","
","
","
","
","
");
$br_replace = array("\n\\par\n","\n\\par\n","\n\\par\n","\n\\par\n","\n\\par\n","\n\\par\n");
$texte = str_replace($br,$br_replace,$texte);
//et changer les images
$liste_image = liste_balise("img",$texte);
for($i = 0; $i < count($liste_image); $i++){
$source_image = $racine.attribut("src",$liste_image[$i]);
$remplace = "{{\field\fldpriv{\*\fldinst{\\\import $source_image}}{\fldrslt }}}";
$texte = str_replace($liste_image[$i],$remplace,$texte);
}
//puis le nettoyage finale
return nettoie_balise($texte);
}
/**
* @return Array un tableau contenant toute les balises commencant par $balise 0 sinon ou si une balise n'est pas refermée
* @param String $balise le nom de la balise dont on veut faire la liste.
* @param String $texte le texte d'ou l'on va tirer la liste
* @desc Cette fonction renvoie un tableau contenant toute les balises contenu dans texte. elle renvoie 0 sinon.
exemple
$texte = "<img src='uyuy'> blala <div align=center> iuiu <img src='hoho'> ";
print_r(liste_balise("img", $texte));
//affiche Array([0]=><img src='uyuy'>,[1]=><img src='hoho'>)
*/
function liste_balise($balise, $texte){
//initilisation on cherche la premiere occurence de <$balise
//pour ne pas gérer les problemes de casse je vais to mettre en minuscule
$texte_originale = $texte;
$balise = strtolower($balise);
$texte = strtolower($texte);
$debut_recherche = 0;
$compteur = 0;
$resultat = array();
$recherche = "<".$balise;
do{
$pos_debut = strpos($texte, $recherche, $debut_recherche);
if($pos_debut === FALSE){ //on a rien trouvé
return $resultat;
}else{
$pos_fin=strpos($texte,">",$pos_debut+1);
if ($pos_fin === FALSE){ //la balise n'est pas refermée on renvoie 0
return $resultat;
}else{
$resultat[$compteur] = substr($texte_originale, $pos_debut, $pos_fin-$pos_debut + 1);
$compteur++;
$debut_recherche = $pos_fin;
}
}
}while ($pos_debut != FALSE);
return $resultat;
}
/**
* @return String
* @param String $attr
* @param String $balise
* @desc renvoie la valeur de l'attribut
*/
function attribut($attr, $balise){
//initilisation on cherche la premiere occurence de $attr="
//pour ne pas gérer les problemes de casse je vais to mettre en minuscule
$balise_originale = $balise;
$balise = strtolower($balise);
$attr = strtolower($attr);
$resultat = "";
//on a deux cas de figure soit l'attribut commence avec un guillemet simple
//soit avec un guillemet double
$recherche1 = $attr."=\"";
$recherche2 = $attr."='";
$pos_debut = strpos($balise, $recherche1);
if ($pos_debut === FALSE){
$pos_debut = strpos($balise, $recherche2);
if (pos_debut === FALSE){ return $resultat; }
$pos_fin = strpos($balise, "'", $pos_debut + strlen($recherche2));
$resultat = substr($balise_originale, $pos_debut + strlen($recherche2), $pos_fin - ($pos_debut + strlen($recherche2)));
return $resultat;
}else{
$pos_fin = strpos($balise,"\"",$pos_debut + strlen($recherche1));
$resultat = substr($balise_originale, $pos_debut + strlen($recherche1), $pos_fin - ($pos_debut + strlen($recherche1)));
return $resultat;
}
}
/**
* @return String
* @param String $texte
* @desc Vire toutes les balises
*/
function nettoie_balise($texte){
do{
$start = strpos($texte,"<");
if($start !== FALSE){
$fin = strpos($texte, ">", $start+1);
if($fin !== FALSE){
$texte = substr_replace($texte, "", $start, $fin-$start+1);
}
}
}while($start !== FALSE);
return $texte;
}
?>