/****************************************************************************** * * This class provides an API for PHP to create Rich Text Format files. * It supports the 5 standard font-families, tables, font- and backgroundcolors. * The following HTML tags can be used in the text: * , , ,
,Linktext, Text . * It needs the rtf_table.class for adding tables to the file. * * @access:public * * *Copyright (C) 2003 Nico de Haen * * $Id: rtf_file.class.php,v 1.0 2003/04/15 ********************************************************************************/ include("rtf_table.class.php"); class rtf_file{ var $fileName; var $content="\n\\viewkind4\\uc1"; //this is the string, where all the content of the file is stored var $actFontSize = "12"; var $actFontFamily = "1"; var $actFontColor = "0"; var $defaultFontColor = "0"; var $fontFamily = array("Arial", "Times New Roman", "Verdana", "Garamond", "Courier", "Comic"); var $colors = array("\\red255,green0,blue0"); var $header = '{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1031\n{\\fonttbl\n{\\f0\\fswiss\\fcharset0 Arial;}\n{\\f1\\froman\\fprq2\\fcharset0 Times New Roman;}\n{\\f2\\fswiss\\fprq2\\fcharset0 Verdana;}\n{\\f3\\froman\\fprq2\\fcharset0 Garamond;}\n{\\f4\\fmodern\\fprq1\\fcharset0 Courier;}{\\f5\\fscript\\fprq2\\fcharset0 Comic Sans MS;}}'; var $pageWidth = 8660; /** * *Cunstructor *has no args *@access public */ function rtf_file(){ } /** * *intern function * */ function getHeader(){ $colortbl = "\n\n{\\colortbl;\n"; foreach($this->colors as $c){ $colortbl.=$c."\n"; } $colortbl.="}\n"; return $this->header.$colortbl; } /** * *fontsize as argument *@param:integer *@access public */ function setFontSize($size=0){ $size*=2; $this->actFontSize = $size/2; $this->content.="\n\\fs".$size; } /** * *@param:string * *possible values: Arial, Times New Roman, Verdana, Garamond, Courier *@access public */ function setFontFamily($font){ $s = $this->getFontNumber($font); $this->actFontFamily = $s; $this->content.= "\n\\f".$s; } /** * *@param:string * *possible values: Arial, Times New Roman, Verdana, Garamond, Courier *for internal use */ function getFontNumber($fontFace){ $f = $this->fontFamily; $s = 0; for($i=0;$i1){ $count = $this->addColorToColorTbl(func_get_arg(0),func_get_arg(1),func_get_arg(2)); } else { $count = $this->addHexToColorTbl(func_get_arg(0)); } $this->actFontColor = $count; $this->content.="\n\\cf".$count; } /** * *@parameter:(int ,int ,int) * *expects either 3 integers (RGB) or one hex string as argument * *@access:public */ function setBgColor(){ if(func_num_args()>1){ $count = $this->addColorToColorTbl(func_get_arg(0),func_get_arg(1),func_get_arg(2)); } else { $count = $this->addHexToColorTbl(func_get_arg(0)); } $this->content.="\n\\cbpat".$count; } /** * *resets the font color to the default color *@access:public * */ function resetFontColor(){ $default = $this->defaultFontColor; $this->content.="\n\\cf".$default; $this->actFontColor = $default; } /** * *resets the background color to the default color * *@access:public */ function resetBgColor(){ $this->content.="\n\\cbpat0"; } //for internal use function addHexToColorTbl($color){ $red = base_convert(substr($color,0,2),16,10); $green = base_convert(substr($color,2,2),16,10); $blue = base_convert(substr($color,4,2),16,10); $count = $this->addColorToColorTbl($red, $green, $blue); return $count; } function addColorToColorTbl($red, $green, $blue){ $s = "\\red".$red."\\green".$green."\\blue".$blue.";"; $colors = $this->colors; $count = 0; for ($i=0;$icolors,$s); $count = count($this->colors); } return $count; } /** * *@param:string * * * *@param:(textString, [fontSize], [alignment], [fontColor], [fontFamily] ) *@access:public */ function addText($textString){ $fontSize=""; $originFontSize=$this->actFontSize; $fontSizeEnd=""; $content = $this->content; $colorStart = ""; $originFontColor=$this->actFontColor; $colorEnd = ""; $align = ""; $args = func_num_args(); if($args>1){ $fontSize = "\\fs".(2*func_get_arg(1)); $this->actFontSize = func_get_arg(1); $fontSizeEnd="\\fs".($originFontSize*2); if($args>2){ $alignment = func_get_arg(2); if($alignment == "center")$align = "\\qc"; if($alignment == "left")$align = "\\ql"; if($alignment == "right")$align = "\\qr"; if($args>3){ $colorCount = $this->addHexToColorTbl(func_get_arg(3)); $colorStart = "\\cf".$colorCount; $colorEnd = "\\cf".$this->actFontColor; $this->actFontColor = $colorCount; } } } $textString = $this->replaceTags($textString); $content = "\n".$colorStart.$fontSize.$align." ".$textString.$colorEnd.$fontSizeEnd."\\par\n"; $this->actFontColor = $originFontColor; $this->actFontSize = $originFontSize; $this->content.=$content; } function replaceTags($content){ $content = str_replace("","\\b ",$content); $content = str_replace("","\\b0 ",$content); $content = str_replace("","\\ul ",$content); $content = str_replace("","\\ul0 ",$content); $content = str_replace("","\\uldb ",$content); $content = str_replace("","\\ul0 ",$content); $content = str_replace("","\\i ",$content); $content = str_replace("","\\i0 ",$content); $content = str_replace("
","\\line\n",$content); $content = str_replace("","\\ql ",$content); $content = str_replace("
","\\qc ",$content); $content = str_replace("","\\qr ",$content); return $this->parseTags($content); } //replace tags with the appropriate Strings function parseTags($string){ //hyperlinks preg_match_all("|]*>|",$string,$arrTags); $results = array(); $newString = $string; foreach($arrTags[0] as $r){ $arrAttr = $this->getTagAttributes($r); $arrAttrKeys = array_keys($arrAttr); $replaceString = "{\\field{\\fldinst HYPERLINK \"".$arrAttr["href"]."\" }{\\fldrslt \\cf1 \\ul"; $newString = str_replace($r,$replaceString, $newString); } $newString = preg_replace("|\s?|","\\ul0 }}",$newString); //font styles preg_match_all("|]*>|",$string,$arrTags); $results = array(); foreach($arrTags[0] as $r){ $arrAttr = $this->getTagAttributes($r); $arrAttrKeys = array_keys($arrAttr); $replaceString = ""; if(in_array("size",$arrAttrKeys))$replaceString.="\\fs".($arrAttr["size"]*2)." "; if(in_array("face",$arrAttrKeys))$replaceString.="\\f".($this->getFontNumber($arrAttr["face"]))." "; if(in_array("color",$arrAttrKeys)){ $color = $arrAttr["color"]; $replaceString.="\\cf".$this->addHexToColorTbl($color)." "; } $newString = str_replace($r,$replaceString, $newString); } $newString = str_replace("","\\fs".($this->actFontSize*2)."\\f".$this->actFontFamily."\\cf".$this->actFontColor." ",$newString); return $newString; } /** * *reads attributes from tag strings and returns them as a hashtable * */ function getTagAttributes($s){ $arrAttr = array(); $s = str_replace("<","",$s); $s = str_replace(">","",$s); $arrTemp = split(" ",$s); $arrAttr["tagType"] = array_shift($arrTemp); foreach ($arrTemp as $attr){ $attr = trim($attr); if(strpos($attr,"=")){ $temp = split("=",$attr); $temp[1] = str_replace("'","",$temp[1]); $temp[1] = str_replace("\"","",$temp[1]); $arrAttr[$temp[0]] = $temp[1]; } } return $arrAttr; } /** * * *@access:public *createTable(cols, [width], [align], [bgcolor]) */ function createTable($cols){ $tblWidth = ""; $tblAlign = "\\trql"; $tblBgcolor = 0; $left = 0; if(func_num_args()>1){ $tblWidth = func_get_arg(1)*10; if(func_num_args()>2){ $align = func_get_arg(2); if($align=="center")$tblAlign="\\trqc"; if($align=="right")$tblAlign="\\trqr"; if($align=="left")$tblAlign="\\trql"; if(is_int($align)){ $tblAlign ="\\trleft".$align; $left = $align; if(($align + $tblWidth)>$this->pageWidth){ $tblWidth=$this->pageWidth-$align; //Maximale Tabellenbreite } } if(func_num_args()>3){ $bgcolor = func_get_arg(3); $tblBgcolor = $this->addHexToColorTbl($bgcolor); } } } $table = new rtf_table($cols, $tblWidth, $tblAlign, $left, $tblBgcolor, &$this->colors); return $table; } function addTable($table){ $this->content.=$table->getString(); } /** * * *@access:public * */ function getContent(){ $header = $this->getHeader(); $content = $header.$this->content."\n}"; return $this->replaceTags($content); } /** * * *@access:public * */ function createFile($fileName){ $content = $this->getContent(); $handle = fopen($fileName,"w"); if($handle){ fwrite($handle,$content); fclose($handle); return true; } else return false; } /** * * *@access:public * */ function newLine(){ if(func_num_args()>0){ for($i=0;$icontent.="\\line\n"; } } else $this->content.="\\par\n"; } /** * * *@access:public * */ function newPage(){ $this->content.="\\page\n"; } }