/* Wünsche und Beschwerden an Alex P. liest ein rtf file. Erzeugt durch Wordpad oder Hypersnap und generiert HTML Code und kapselt es in ein
WORD FUNKT NICHT! Test: include('rtfparser.php'); $html_code = rtf_parsefile('test.rtf'); //..... in string echo $html_code; */ global $rtf_color_index; global $rtf_color_class; $rtf_color_index[ 0 ][ 0 ] = 0; $rtf_color_index[ 0 ][ 1 ] = 0; $rtf_color_index[ 0 ][ 2 ] = 0; $rtf_color_index[ 1 ][ 0 ] = 0; $rtf_color_index[ 1 ][ 1 ] = 0; $rtf_color_index[ 1 ][ 2 ] = 119; $rtf_color_index[ 2 ][ 0 ] = 0; $rtf_color_index[ 2 ][ 1 ] = 0; $rtf_color_index[ 2 ][ 2 ] = 255; $rtf_color_index[ 3 ][ 0 ] = 0; $rtf_color_index[ 3 ][ 1 ] = 128; $rtf_color_index[ 3 ][ 2 ] = 0; $rtf_color_index[ 4 ][ 0 ] = 0; $rtf_color_index[ 4 ][ 1 ] = 153; $rtf_color_index[ 4 ][ 2 ] = 0; $rtf_color_index[ 5 ][ 0 ] = 128; $rtf_color_index[ 5 ][ 1 ] = 0; $rtf_color_index[ 5 ][ 2 ] = 0; $rtf_color_index[ 6 ][ 0 ] = 128; $rtf_color_index[ 6 ][ 1 ] = 128; $rtf_color_index[ 6 ][ 2 ] = 128; $rtf_color_index[ 7 ][ 0 ] = 136; $rtf_color_index[ 7 ][ 1 ] = 0; $rtf_color_index[ 7 ][ 2 ] = 0; $rtf_color_index[ 8 ][ 0 ] = 160; $rtf_color_index[ 8 ][ 1 ] = 0; $rtf_color_index[ 8 ][ 2 ] = 160; $rtf_color_class[0]=0; $rtf_color_class[1]=1; $rtf_color_class[2]=2; $rtf_color_class[3]=3; $rtf_color_class[4]=4; $rtf_color_class[5]=5; $rtf_color_class[6]=6; $rtf_color_class[7]=7; $rtf_color_class[8]=8; $rtf_color_class[9]=9; function rtf_parsefile( $filename ,$classname ) { global $rtf_color_index; global $rtf_color_class; $rtf = file_get_contents( $filename ); //Achtung vorher wurde parse_styles getrennt aufgerufen //weil es im header passieren musste. //Diesmal ist es hier, weil die classnamen dem css angepasst //werden. parse_styles( $rtf , $classname ); $html_code .= ""; $pos = strpos($rtf, '\fs20'); if( $pos) { $rtf = substr( $rtf, $pos ); } $rtf = str_replace( "<", "<", $rtf ); $rtf = str_replace( ">", ">", $rtf ); /* Per Default sind die ersten Zeilen ohne Farbwechsel code. RTF nimmt den ersten Farbtopf. Damit dieser Parser funktioniert, muss am Anfang der Zeichen ein \cf1 Farbwechsel Befehl eingefügt werden. alt: $html_code .= ''; // mit farbe 1 eröffnen $span_open = true; neu: $rtf = '\cf1 ' . $rtf <--- farbwechsel forciert, dann wird richtige farbe eingesetzt */ $rtf = '\cf1 ' . $rtf; // Farbwechsel auf Farbe1 erzwingen $rtf_array = explode( "\\", $rtf ); $index = 0; // zähler zum index des rtf_arrays $span_open = false; // flag zum schliessen eines vorherigen spans $max_index = count($rtf_array) - 1 ; // letztes item weg, ist eine störende geschwungene Klammer for( $index = 1; $index < $max_index; $index ++ ) { $item = $rtf_array[ $index ]; // Escape Chars if( strlen($item) == 0 ) { // dieser leerer String wird \ $html_code .= "\\"; // nächster braucht ein blank, das geschluckt wird $rtf_array[ $index + 1 ] = "x " . $rtf_array[ $index + 1 ]; // aus \n quelle wird // item[0]="" item[1]="n" // für den parser muss aus item 0 der \ werden // und item[1] ein "xpseudo n", es wird bis zum Blank gelöscht } if( strncmp( $item, "cf", 2 ) == 0 ) { // cf ist ein color feld // zuerst vorherigen offenen span tag schliessen // dann neuen span tag aufbauen if( $span_open ) $html_tag = ""; else $html_tag = ""; $span_open = true; // span bleibt offen, wir machen neuen auf $xx = $item[2]; $html_tag .= ''; // cf entfernen, wenn ein blank hinter cf? steht dann // dieses auch mit entfernen $cnt = 3; if( $item[3] == ' ' ) $cnt = 4; $item = substr( $item, $cnt ); // ins array zurück $item = $html_tag . $item; } else if( strncmp( $item, "pard", 4 ) == 0 ) { // pard entfernen und ein nachfolgendes blank auch if( $item[4] == ' ' ) { $item = substr( $item, 5 ); }else{ $item = substr( $item, 4 ); } } else if( strncmp( $item, "par", 3 ) == 0 ) { // par entfernen und ein nachfolgendes blank auch if( $item[3] == ' ' ) { $item = substr( $item, 4 ); }else{ $item = substr( $item, 3 ); } } else if( strncmp( $item, "tab", 3 ) == 0 ) { // par entfernen und ein nachfolgendes blank auch if( $item[3] == ' ' ) { $item = substr( $item, 4 ); }else{ $item = substr( $item, 3 ); } $item = " " . $item; // 4 leerzeichen statt tab } else if ( strncmp( $item, "{", 1 ) == 0 ) { } else if ( strncmp ($item, "}", 1 ) == 0 ) { } else if ( strcmp ($item, "b" ) == 0 ) { $item = ""; return $html_code; } /* so sieht das css aus im default.css: -------------------------------------- .rtfcol0 { color: rgb(0,0,0);} .rtfcol1 { color: rgb(0,0,119);} .rtfcol2 { color: rgb(0,0,255);} .rtfcol3 { color: rgb(0,128,0);} .rtfcol4 { color: rgb(0,153,0);} .rtfcol5 { color: rgb(128,0,0);} .rtfcol6 { color: rgb(128,128,128);} .rtfcol7 { color: rgb(136,0,0);} .rtfcol8 { color: rgb(160,0,160);} */ function parse_styles( $rtf , $classname ) { global $rtf_color_index; global $rtf_color_class; $pos = strpos($rtf, '\fs20'); if( $pos) $rtf = substr( $rtf, 0, $pos + 5 ); $rtf_array = explode( "\\", $rtf ); /* beispiel array inhalt red0 green0 blue255; red0 green0 blue0; red136 green0 blue0; red128 green128 blue128; red160 green0 blue160; red0 green128 blue0;} */ $style_header_done = false; $index = 0; foreach( $rtf_array as $item ) { if( strncmp( "red", $item, 3 ) == 0 ) $red = substr( $item, 3 ); if( strncmp( "green", $item, 5 ) == 0 ) $green = substr( $item, 5 ); if( strncmp( "blue", $item, 4 ) == 0 ) { $blue = substr( $item, 4 ); $blue = preg_replace( "[\D]","",$blue); // farbcode suchen $i=0; for( $i=0; $i < 9; $i++ ){ if( $red == $rtf_color_index[$i][0] && $green == $rtf_color_index[$i][1] && $blue == $rtf_color_index[$i][2] ) { $rtf_color_class[ $index ] = $i; break; // fertig, gefunden } } // Wenn keine Farbe gefunden wurde // dann setzen wir die classe auf 0 (schwarz) if( $i == 9 ){ //echo ""; } else if ( strncmp ($item, "b ",2 ) == 0 ) { $item = substr($item, 2); $item = " "; $html_code .= "" . $item; } else if ( $item[0] == "'" ) { $item = str_replace( "'c1","Á",$item ); $item = str_replace( "'e1","á",$item ); $item = str_replace( "'c0","À",$item ); $item = str_replace( "'e0","à",$item ); $item = str_replace( "'c9","É",$item ); $item = str_replace( "'e9","é",$item ); $item = str_replace( "'c8","È",$item ); $item = str_replace( "'e8","è",$item ); $item = str_replace( "'cd","Í",$item ); $item = str_replace( "'ed","í",$item ); $item = str_replace( "'cc","Ì",$item ); $item = str_replace( "'ec","ì",$item ); $item = str_replace( "'d3","Ó",$item ); $item = str_replace( "'f3","ó",$item ); $item = str_replace( "'d2","Ò",$item ); $item = str_replace( "'f2","ò",$item ); $item = str_replace( "'da","Ú",$item ); $item = str_replace( "'fa","ú",$item ); $item = str_replace( "'d9","Ù",$item ); $item = str_replace( "'f9","ù",$item ); $item = str_replace( "'80","€",$item ); $item = str_replace( "'d1","Ñ",$item ); $item = str_replace( "'f1","ñ",$item ); $item = str_replace( "'c7","Ç",$item ); $item = str_replace( "'e7","ç",$item ); $item = str_replace( "'dc","Ü",$item ); $item = str_replace( "'fc","ü",$item ); $item = str_replace( "'bf","¿",$item ); $item = str_replace( "'a1","¡",$item ); $item = str_replace( "'b7","·",$item ); $item = str_replace( "'a9","©",$item ); $item = str_replace( "'ae","®",$item ); $item = str_replace( "'ba","º",$item ); $item = str_replace( "'aa","ª",$item ); $item = str_replace( "'b2","²",$item ); $item = str_replace( "'b3","³",$item ); $item = str_replace( "'e4","ä",$item ); $item = str_replace( "'c4","Ä",$item ); $item = str_replace( "'d6","Ö",$item ); $item = str_replace( "'f6","ö",$item ); $item = str_replace( "'df","ß",$item ); } else { //code bis zum ersten blank löschen $pos1 = strpos( $item, ' ' ); if( $pos1 ) $item = substr( $item, $pos1+1 ); $pos2 = strpos( $item, '{' ); if( $pos2 ) $item = substr( $item, $pos2+1 ); if( $pos2 == false && $pos1 == false ) $item = ""; } $rtf_array[ $index ] = $item; $html_code .= $rtf_array[ $index ]; } if( $span_open ) $html_code .=" WARNUNG color $red $green $blue nicht gefunden (rtf color $index)
"; //exit; $rtf_color_class[ $index ] = 0; } // Nächster Farbcode index vom rtffile $index ++; } } } ?>