<?php
/****************************************************************************************
SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV

Version 0.9.6
Copyright (C) 2006  Sylvain FATOME

This program is free software; you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation; 
either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program
(in the directory docs); 
if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 
Boston, MA  02110-1301, USA.

Contact : galawa@free.fr
****************************************************************************************/

?>
<?PHP
					function WriteHTML($html)
					{
						// Nettoyage des tags
						$str=array(
						'<br />' => '<br>',
						'<hr />' => '<hr>',
						'[r]' => '<red>',
						'[/r]' => '</red>',
						'[l]' => '<blue>',
						'[/l]' => '</blue>',
						'&#8220;' => '"',
						'&#8221;' => '"',
						'&#8222;' => '"',
						'&#8230;' => '...',
						'&#8217;' => '\''
						);
						foreach ($str as $_from => $_to) 
							$html = str_replace($_from,$_to,$html);
			
						//Suppression de tous les tags non supportés
						$html=strip_tags($html,"<a><br><font><blockquote><h1><h2><h3><h4><h5><h6><ul><li><b><i><u><strong><em><sub><sup><ol><div><span>");
						//Remplace les retours chariots, nouvelles lignes, et tabulations par des espaces
						$html=str_replace("\n",' ',$html);
						// $repTable = array("\t" => " ", "\n" => " ", "\r" => " ", "\0" => " ", "\x0B" => " "); 
						// $html = strtr($html, $repTable);
			
						//Remplace quelques caractères spéciaux
						$html = str_replace('&trade;','™',$html);
						$html = str_replace('&copy;','©',$html);
						$html = str_replace('&euro;','€',$html);
						
						$pattern = '/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/';
			
						$a=preg_split($pattern,trim($html),-1,PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
						
						foreach($a as $key=>$element) 
						{
							if (!preg_match($pattern, $element)) 
							{
								//Texte
								if ($this->pas_afficher == FALSE)
								{
									if ($this->issetbgcolor == true)
									{
										$this->SetFillColor($this->tab_rgb[0],$this->tab_rgb[1],$this->tab_rgb[2]);
									}
									if($this->HREF) // Est-ce un lien ?
									{
										$this->PutLink($this->HREF,$element);
									}
									else 
									{
										// Vérification si blockquote, OL, ou UL
										if (($this->nb_bq > 0) OR ($this->nb_ol > 0) OR ($this->nb_ul > 0))
										{
											if (trim($element) <> "")
											{
												// On sauve la marge de gauche
												$lMargin = $this->lMargin;
												// Blockquote : blancs simples
												if ($this->nb_bq > 0)
												{
													$this->SetLeftMargin($this->nb_bq*10+$lMargin);
													$this->Write($this->lineheight,trim(stripslashes(txtentities($element))));
												}
												// OL ou UL : blancs + numérotation (ou pas)
												if ($this->nb_ol > 0)
												{
													$this->SetLeftMargin($this->nb_ol*10+$lMargin);
													$this->Write($this->lineheight,($this->index_li[$this->nb_ol]).'. '.trim(stripslashes(txtentities($element))));
												}
												if ($this->nb_ul > 0)
												{
													$this->SetLeftMargin($this->nb_ul*10+$lMargin);
													// $this->Write($this->lineheight,'» '.trim(stripslashes(txtentities($element))));
													$this->Write($this->lineheight,chr(149).' '.trim(stripslashes(txtentities($element))));
												}
												// On restaure la marge de gauche
												$this->SetLeftMargin($lMargin);
												$this->tag_precedent='';
											}
										}
										else
										{
											if (trim($element) <> "")
											{
												if ($this->issetbgcolor == true)
												{
													$this->Cell($this->GetStringWidth($element)+2,$this->lineheight,stripslashes(txtentities($element)),0,0,'L',1);
												}
												else
												{
													$this->Write($this->lineheight,stripslashes(txtentities($element)));
													
													// Si il y a un blanc à la fin de la chaîne, Write ne l'imprime pas
													// (ce qui "colle" le texte au suivant) : il faut donc le gérer.
													if ($element{(strlen($element)-1)} == " ")
													{
														$this->Cell($this->GetStringWidth(' '));
													}
													
												}
												$this->tag_precedent='';
											}
										}
									}
									$this->SetFillColor(255);
								}
							}
							else 
							{
								//Tag
								$element = substr($element, 1, -1);
								if($element{0}=='/') 
								{
									$this->CloseTag(strtoupper(substr($element,1)));
								}
								else 
								{
									// Attributs exacts
									// Récupération des tags
									preg_match('/([a-zA-Z0-9]*)/', $element, $tag);
									$tag = strtoupper($tag[0]);
									// Récupération des atributs
									preg_match_all('/([^=\s]*)=["\']?([^"\']*)["\']?/', $element, $attr_array, PREG_PATTERN_ORDER);
									$attr = array(); // reset attribute array
									while(list($id,$name)=each($attr_array[1])) {
										$attr[strtoupper($name)] = $attr_array[2][$id];
									}
									$this->OpenTag($tag,$attr);
								}
							}
						}
					}
					
					function OpenTag($tag,$attr)
					{
						//Balise ouvrante
						switch($tag)
						{
							case 'BR':
								$this->Ln($this->lineheight);
								break;
							case 'STRONG':
							case 'B':
								$this->SetStyle('B',true);
								break;
							case 'I':
							case 'EM':
								$this->SetStyle('I',true);
								break;
							case 'U':
								$this->SetStyle('U',true);
								break;
							case 'SUB': 
								$this->ex_fontsize = $this->FontSizePt;
								$this->ex_lineheight = $this->lineheight;
								$this->ex_x = $this->GetX();
								$this->ex_y = $this->GetY();
								$this->SetFontSize($this->ex_fontsize-3);
								$this->SetXY($this->ex_x, (($this->ex_y+($this->ex_fontsize*0.3))-2));					  
								break;
							case 'SUP': 
								$this->ex_fontsize = $this->FontSizePt;
								$this->ex_lineheight = $this->lineheight;
								$this->ex_x = $this->GetX();
								$this->ex_y = $this->GetY();
								$this->SetFontSize($this->ex_fontsize-3);
								$this->SetXY($this->ex_x, (($this->ex_y-($this->ex_fontsize*0.3))+2));
								break;
							case 'OL': 
								$this->nb_ol++;
								break;
							case 'UL': 
								$this->nb_ul++;
								break;
							case 'LI':
								$this->ln($this->lineheight);
								$this->nb_li++;
								if ($this->nb_ol > 0)
									$this->index_li[$this->nb_ol] = ($this->index_li[$this->nb_ol])+1;
								break;
							case 'BLOCKQUOTE':
								if($this->tag_precedent <> 'BLOCKQUOTE')
									$this->ln($this->lineheight);
								$this->nb_bq++;
								break;
							case 'H1': 
							  $this->SetFontSize(2*$this->FontSizePt); 
							  $this->lineheight *= 2;
							  break;
							case 'H2': 
							  $this->SetFontSize(1.5*$this->FontSizePt); 
							  $this->lineheight *= 1.5;
							  break;
							case 'H3':
							  $this->SetFontSize(1.17*$this->FontSizePt);
							  $this->lineheight *= 1.17;
							  break;
							case 'H4':
							  $this->SetFontSize($this->FontSizePt); 
							  break;
							case 'H5': 
							  $this->SetFontSize(0.83*$this->FontSizePt); 
							  $this->lineheight *= 0.83;
							  break;
							case 'H6': 
							  $this->SetFontSize(0.67*$this->FontSizePt); 
							  $this->lineheight *= 0.67;
							  break;
							case 'FONT':
							case 'SPAN':
							case 'DIV' :
								if ($tag == 'DIV')
								{
									$this->Ln($this->lineheight); // Toujours un saut de ligne sur balise DIV
								}
								if (isset($attr['COLOR']) and $attr['COLOR']!='') 
								{
									$coul=hex2dec($attr['COLOR']);
									$this->mySetTextColor($coul['R'],$coul['G'],$coul['B']);
									$this->issetcolor=true;
								}
								if (isset($attr['FACE']) and in_array(strtolower($attr['FACE']), $this->fontlist))
								{
									$this->SetFont(strtolower($attr['FACE']));
									$this->issetfont=true;
								}
								if (isset($attr['SIZE']) and $attr['SIZE']!='')
								{
									switch ($attr['SIZE'])
									{
										// Rapport de 1.2 entre chiffre, partant du principe que '4' = taille courante
										// Cf http://www.w3.org/TR/REC-CSS2/fonts.html#value-def-absolute-size
										case 1:
											$this->SetFontSize(0.58*$this->FontSizePt); 
											// $this->lineheight *= 0.58;
											break;
										case 2:
											$this->SetFontSize(0.69*$this->FontSizePt); 
											// $this->lineheight *= 0.69;
											break;
										case 3:
											$this->SetFontSize(0.83*$this->FontSizePt); 
											// $this->lineheight *= 0.83;
											break;
										case 4:
											// $this->SetFontSize($this->FontSizePt); 
											break;
										case 5:
											$this->SetFontSize(1.2*$this->FontSizePt); 
											// $this->lineheight *= 1.2;
											break;
										case 6:
											$this->SetFontSize(1.44*$this->FontSizePt); 
											// $this->lineheight *= 1.44;
											break;
										case 7:
											$this->SetFontSize(1.73*$this->FontSizePt); 
											// $this->lineheight *= 1.73;
											break;
									}
									$this->issetfont=true;
								}
								if (isset($attr['STYLE']) and $attr['STYLE']!='')
								{
									$attr_style = explode(";",$attr['STYLE']);
									for ($i=0; $i<count($attr_style);$i++)
									{
										if (trim($attr_style[$i]) <> "")
										{
											$detail_attr_style = explode(":",$attr_style[$i]);
											switch (strtolower(trim($detail_attr_style[0])))
											{
												case 'background-color' :
													// Mode Hexadécimal ?
													$pos = strpos(trim($detail_attr_style[1]), "#");
													if ($pos === FALSE)
													{
														// Mode RGB ?
														$pos2 = strpos(trim($detail_attr_style[1]), "(");
														if ($pos2 === FALSE)
														{
															break; // Aucun des 2, on quitte
														}
														else
														{
															$str_couleur = substr(trim($detail_attr_style[1]),$pos2+1);
															$str_couleur = substr($str_couleur,0,strlen($str_couleur)-1);
															$this->tab_rgb = explode(",",$str_couleur);
															for ($k=0; $k<count($this->tab_rgb); $k++)
															{
																settype($this->$tab_rgb[$k],"int");
															}
															$this->issetbgcolor=true;
														}
													}
													else
													{
														$coul=hex2dec(trim($detail_attr_style[1]));
														$this->SetFillColor($coul['R'],$coul['G'],$coul['B']);
														$this->issetbgcolor=true;
													}
													break;
												case 'page-break-after' :
													if (trim(strtolower($detail_attr_style[1])) == "always")
													{
														$this->AddPage();
													}
													break;
												case 'display' :
													if (trim(strtolower($detail_attr_style[1])) == "none")
													{
														$this->pas_afficher = TRUE;
													}
													break;
											}
										}
									}
								}
								break;
							case 'A':
								$this->HREF=$attr['HREF'];
								break;
							case 'SDP': // Pour conserver compatibilité avec anciens formats < SMI 0.9.5
								$this->AddPage();
								break;
							// Tableaux plus tard
							case 'TABLE':
							case 'TR':
							case 'TH':
							case 'TD':
								break;
						}
						$this->tag_precedent=$tag;
					}
					
					function CloseTag($tag)
					{
						//Balise fermante
						switch ($tag)
						{
							case 'STRONG':
							case 'B':
								$this->SetStyle('B',false);
								break;
							case 'I':
							case 'EM':
								$this->SetStyle('I',false);
								break;
							case 'U':
								$this->SetStyle('U',false);
								break;
							case 'SUB' :
							case 'SUP' :
								$this->SetFontSize($this->ex_fontsize);
								$this->SetXY($this->ex_x, $this->ex_y);					  
								$this->lineheight = $this->ex_lineheight;
								break;
							case 'OL': 
								$this->nb_ol = $this->nb_ol-1;
								if ($this->nb_ol == 0)
								{
									$this->SetLeftMargin(10);
									$this->ln($this->lineheight);
									$this->ln($this->lineheight);
								}
								break;
							case 'UL': 
								$this->nb_ul = $this->nb_ul-1;
								if ($this->nb_ul == 0)
								{
									$this->SetLeftMargin(10);
									$this->ln($this->lineheight);
									$this->ln($this->lineheight);
								}
								break;
							case 'LI' :
								$this->nb_li = $this->nb_li-1;
								break;
							case 'BLOCKQUOTE':
								$this->nb_bq = $this->nb_bq-1;
								break;
							case 'H1' :
							case 'H2' :
							case 'H3' :
							case 'H4' :
							case 'H5' :
							case 'H6' :
								$this->Ln($this->lineheight);
								$this->lineheight = 5;
								$this->SetFont('Arial','',10);
								$this->SetFontSize(11);
								$this->SetStyle('U',false);
								$this->SetStyle('B',false);
								break;
							case 'FONT' :
							case 'SPAN' :
							case 'DIV' :
								if ($tag == 'SPAN')
								{
									$this->pas_afficher = FALSE;
								}
								if ($tag == 'DIV')
								{
									$this->Ln($this->lineheight); // Toujours un saut de ligne sur balise DIV
								}
								if ($this->issetcolor==true) 
								{
									$this->SetTextColor(0,0,0);
								}
								if ($this->issetfont) 
								{
									$this->SetFont('Arial','',10);
									$this->issetfont=false;
								}
								if ($this->issetbgcolor == true) 
								{
									$this->SetFillColor(255,255,255);
									$this->issetbgcolor=false;
								}
								break;
							case 'A' :
								$this->HREF='';
								break;
							// Tableaux plus tard
							case 'TABLE':
							case 'TD':
							case 'TH':
								break;
							case 'TR':
								break;
						}
					}
					
					function SetStyle($tag,$enable)
					{
						//Modifie le style et sélectionne la police correspondante
						$this->$tag+=($enable ? 1 : -1);
						$style='';
						foreach(array('B','I','U') as $s)
							if($this->$s>0)
								$style.=$s;
						$this->SetFont('',$style);
					}
					
					function mySetTextColor($r,$g=0,$b=0)
					{
						static $_r=0, $_g=0, $_b=0;
				
						if ($r==-1) 
							$this->SetTextColor($_r,$_g,$_b);
						else 
						{
							$this->SetTextColor($r,$g,$b);
							$_r=$r;
							$_g=$g;
							$_b=$b;
						}
					}
					
					function PutLink($URL,$txt)
					{
						//Place un hyperlien
						$this->SetTextColor(0,0,255);
						$this->SetStyle('U',true);
						$this->Write($this->lineheight,$txt,$URL);
						$this->SetStyle('U',false);
						$this->SetTextColor(0);
					}
?>