"TYPO3 4.0.1: typo3_src-4.0.1/typo3/sysext/cms/tslib/media/scripts/plaintextLib.inc Source File", "datetime" => "Sat Dec 2 19:22:30 2006", "date" => "2 Dec 2006", "doxygenversion" => "1.4.6", "projectname" => "TYPO3 4.0.1", "projectnumber" => "4.0.1" ); get_header($doxygen_vars); ?>

plaintextLib.inc

00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 1999-2005 Kasper Skaarhoj (kasperYYYY@typo3.com)
00006 *  All rights reserved
00007 *
00008 *  This script is part of the TYPO3 project. The TYPO3 project is
00009 *  free software; you can redistribute it and/or modify
00010 *  it under the terms of the GNU General Public License as published by
00011 *  the Free Software Foundation; either version 2 of the License, or
00012 *  (at your option) any later version.
00013 *
00014 *  The GNU General Public License can be found at
00015 *  http://www.gnu.org/copyleft/gpl.html.
00016 *  A copy is found in the textfile GPL.txt and important notices to the license
00017 *  from the author is found in LICENSE.txt distributed with these scripts.
00018 *
00019 *
00020 *  This script is distributed in the hope that it will be useful,
00021 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023 *  GNU General Public License for more details.
00024 *
00025 *  This copyright notice MUST APPEAR in all copies of the script!
00026 ***************************************************************/
00123 class user_plaintext {
00124         var $cObj;
00125         var $conf=array();
00126         var $charWidth=76;
00127 
00137         function main_plaintext($content,$conf) {
00138                 $this->conf = $conf;
00139                 $this->siteUrl=$conf['siteUrl'];
00140                 $lines = array();
00141                 $CType= (string)$this->cObj->data['CType'];
00142                 switch($CType)  {
00143                         case 'header':
00144                                 $lines[]=$this->getHeader();
00145                                 if ($this->cObj->data['subheader'])     {
00146                                         $lines[]=$this->breakContent(strip_tags($this->cObj->data['subheader']));
00147                                 }
00148                         break;
00149                         case 'text':
00150                         case 'textpic':
00151                                 $lines[]=$this->getHeader();
00152                                 if ($CType=='textpic' && !($this->cObj->data['imageorient']&24))        {
00153                                         $lines[]=$this->getImages();
00154                                         $lines[]='';
00155                                 }
00156                                 $lines[]=$this->breakContent(strip_tags($this->parseBody($this->cObj->data['bodytext'])));
00157                                 if ($CType=='textpic' && ($this->cObj->data['imageorient']&24)) {
00158                                         $lines[]='';
00159                                         $lines[]=$this->getImages();
00160                                 }
00161                         break;
00162                         case 'image':
00163                                 $lines[]=$this->getHeader();
00164                                 $lines[]=$this->getImages();
00165                         break;
00166                         case 'uploads':
00167                                 $lines[]=$this->getHeader();
00168                                 $lines[]=$this->renderUploads($this->cObj->data['media']);
00169                         break;
00170                         case 'menu':
00171                                 $lines[]=$this->getHeader();
00172                                 $lines[]=$this->getMenuSitemap();
00173                         break;
00174                         case 'shortcut':
00175                                 $lines[]=$this->getShortcut();
00176                         break;
00177                         case 'bullets':
00178                                 $lines[]=$this->getHeader();
00179                                 $lines[]=$this->breakBulletlist(strip_tags($this->parseBody($this->cObj->data['bodytext'])));
00180                         break;
00181                         case 'table':
00182                                 $lines[]=$this->getHeader();
00183                                 $lines[]=$this->breakTable(strip_tags($this->parseBody($this->cObj->data['bodytext'])));
00184                         break;
00185                         case 'html':
00186                                 $lines[]=$this->getHTML();
00187                         break;
00188                         default:
00189                                 $defaultOutput = $this->getString($this->conf['defaultOutput']);
00190                                 if ($defaultOutput)     {
00191                                         $lines[]=str_replace('###CType###',$CType,$defaultOutput);
00192                                 }
00193                         break;
00194                 }
00195 
00196                 $lines[]='';    // First break.
00197                 $content = implode(chr(10),$lines);
00198 
00199                         // User processing:
00200                 $content=$this->userProcess('userProc',$content);
00201                 return $content;
00202         }
00203 
00209         function getMenuSitemap()       {
00210                 $str = $this->cObj->cObjGetSingle($this->conf['menu'],$this->conf['menu.']);
00211                 $str = $this->breakBulletlist(trim(strip_tags(eregi_replace('<br[ /]*>',chr(10),$this->parseBody($str)))));
00212                 return $str;
00213         }
00214 
00220         function getShortcut()  {
00221                 $str = $this->cObj->cObjGetSingle($this->conf['shortcut'],$this->conf['shortcut.']);
00222                 return $str;
00223         }
00224 
00231         function getHTML($str=array())  {
00232                 return $this->breakContent(strip_tags(eregi_replace('<br[ /]*>',chr(10),$this->parseBody(is_string($str)?$str:$this->cObj->data['bodytext']))));
00233         }
00234 
00241         function getHeader()    {
00242                 // links...
00243                 return $this->renderHeader($this->cObj->data['header'],$this->cObj->data['header_layout']);
00244         }
00245 
00251         function getImages()    {
00252                 $images = $this->renderImages($this->cObj->data['image'],!$this->cObj->data['image_zoom']?$this->cObj->data['image_link']:'',$this->cObj->data['imagecaption']);
00253                 return $images;
00254         }
00255 
00262         function parseBody($str)        {
00263                         // First, regular parsing:
00264                 $str = eregi_replace('<br[ /]*>',' ',$str);
00265                 $str = $this->cObj->stdWrap($str,$this->conf['bodytext.']['stdWrap.']);
00266                         // Then all a-tags:
00267                 $aConf = array();
00268                 $aConf['parseFunc.']['tags.']['a']='USER';
00269                 $aConf['parseFunc.']['tags.']['a.']['userFunc']='user_plaintext->atag_to_http';
00270                 $aConf['parseFunc.']['tags.']['a.']['siteUrl'] = $this->siteUrl;
00271 
00272                 $str = $this->cObj->stdWrap($str,$aConf);
00273                 $str = str_replace('&nbsp;',' ',t3lib_div::htmlspecialchars_decode($str));
00274                 return $str;
00275         }
00276 
00284         function renderUploads($str,$upload_path='uploads/media/')      {
00285                 $files = explode(',',$str);
00286                 reset($files);
00287                 $lines=array();
00288                 if ($this->conf['uploads.']['header'])  {$lines[]=$this->getString($this->conf['uploads.']['header']);}
00289                 while(list($k,$file)=each($files))      {
00290                         $lines[]=$this->siteUrl.$upload_path.$file;
00291                 }
00292                 return implode(chr(10),$lines);
00293         }
00294 
00302         function renderHeader($str,$type=0)     {
00303                 if ($str)       {
00304                         $hConf = $this->conf['header.'];
00305                         $defaultType = t3lib_div::intInRange($hConf['defaultType'],1,5);
00306                         $type=t3lib_div::intInRange($type,0,6);
00307                         if (!$type)     $type=$defaultType;
00308                         if ($type!=6)   {       // not hidden
00309                                 $tConf = $hConf[$type.'.'];
00310 
00311                                 $lines=array();
00312 
00313                                 $blanks = t3lib_div::intInRange($tConf['preBlanks'],0,1000);
00314                                 if ($blanks)    {
00315                                         $lines[]=str_pad('', $blanks-1, chr(10));
00316                                 }
00317 
00318                                 $lines=$this->pad($lines,$tConf['preLineChar'],$tConf['preLineLen']);
00319 
00320                                 $blanks = t3lib_div::intInRange($tConf['preLineBlanks'],0,1000);
00321                                 if ($blanks)    {$lines[]=str_pad('', $blanks-1, chr(10));}
00322 
00323                                 if ($this->cObj->data['date'])          {$lines[] = $this->getString($hConf['datePrefix']).date($hConf['date']?$hConf['date']:'d-m-Y',$this->cObj->data['date']);}
00324                                 $prefix='';
00325                                 $str=$this->getString($tConf['prefix']).$str;
00326                                 if ($tConf['autonumber'])       $str=$this->cObj->parentRecordNumber.$str;
00327                                 if ($this->cObj->data['header_position']=='right')      {$prefix=str_pad(' ',($this->charWidth-strlen($str)));}
00328                                 if ($this->cObj->data['header_position']=='center')     {$prefix=str_pad(' ',floor(($this->charWidth-strlen($str))/2));}
00329                                 $lines[]=$this->cObj->stdWrap($prefix.$str,$tConf['stdWrap.']);
00330                                 if ($this->cObj->data['header_link'])           {$lines[] = $this->getString($hConf['linkPrefix']).$this->getLink($this->cObj->data['header_link']);}
00331 
00332                                 $blanks = t3lib_div::intInRange($tConf['postLineBlanks'],0,1000);
00333                                 if ($blanks)    {$lines[]=str_pad('', $blanks-1, chr(10));}
00334 
00335                                 $lines=$this->pad($lines,$tConf['postLineChar'],$tConf['postLineLen']);
00336 
00337                                 $blanks = t3lib_div::intInRange($tConf['postBlanks'],0,1000);
00338                                 if ($blanks)    {$lines[]=str_pad('', $blanks-1, chr(10));}
00339                                 return implode(chr(10),$lines);
00340                         }
00341                 }
00342         }
00343 
00353         function pad($lines,$preLineChar,$len)  {
00354                 $strPad = t3lib_div::intInRange($len,0,1000);
00355                 $strPadChar = $preLineChar?$preLineChar:'-';
00356                 if ($strPad)    {
00357                         $lines[]=str_pad('', $strPad, $strPadChar);
00358                 }
00359                 return $lines;
00360         }
00361 
00369         function breakContent($str)     {
00370                 $cParts = explode(chr(10),$str);
00371                 reset($cParts);
00372                 $lines=array();
00373                 while(list(,$substrs)=each($cParts))    {
00374                         $lines[]=$this->breakLines($substrs);
00375                 }
00376                 return implode(chr(10),$lines);
00377         }
00378 
00385         function breakBulletlist($str)  {
00386                 $type = $this->cObj->data['layout'];
00387                 $type=t3lib_div::intInRange($type,0,3);
00388 
00389                 $tConf = $this->conf['bulletlist.'][$type.'.'];
00390 
00391                 $cParts = explode(chr(10),$str);
00392                 reset($cParts);
00393                 $lines=array();
00394                 $c=0;
00395                 while(list(,$substrs)=each($cParts))    {
00396                         $c++;
00397                         $bullet = $tConf['bullet'] ? $this->getString($tConf['bullet']) : ' - ';
00398                         $bLen=strlen($bullet);
00399                         $bullet = substr(str_replace('#',$c,$bullet),0,$bLen);
00400                         $secondRow = substr($tConf['secondRow']?$this->getString($tConf['secondRow']):str_pad('',strlen($bullet),' '),0,$bLen);
00401 
00402                         $lines[]=$bullet.$this->breakLines($substrs,chr(10).$secondRow,$this->charWidth-$bLen);
00403 
00404                         $blanks = t3lib_div::intInRange($tConf['blanks'],0,1000);
00405                         if ($blanks)    {$lines[]=str_pad('', $blanks-1, chr(10));}
00406                 }
00407                 return implode(chr(10),$lines);
00408         }
00409 
00416         function breakTable($str)       {
00417                 $cParts = explode(chr(10),$str);
00418                 reset($cParts);
00419                 $lines=array();
00420                 $cols = intval($this->conf['cols']) ? intval($this->conf['cols']) : 0 ;
00421                 $c=0;
00422                 while(list(,$substrs)=each($cParts))    {
00423                         $c++;
00424                         if (trim($substrs))     {
00425                                 $lineParts=explode('|',$substrs);
00426                                 if (!$cols)     $cols=count($lineParts);
00427 
00428                                 for ($a=0;$a<$cols;$a++)        {
00429                                         $jdu = explode(chr(10),$this->breakLines($lineParts[$a],chr(10),ceil($this->charWidth/$cols)));
00430                                         $lines[$c][$a]=$jdu;
00431                                 }
00432                         }
00433                 }
00434                 $messure = $this->traverseTable($lines);
00435 
00436 
00437                 $divChar='-';
00438                 $joinChar='+';
00439                 $colChar='|';
00440 
00441                 // Make table:
00442                 $outLines = array();
00443                 $outLines[]=$this->addDiv($messure,'',$divChar,$joinChar,$cols);
00444 
00445                 reset($lines);
00446                 while(list($k,$v)=each($lines)) {
00447                         $top = intval($messure[1][$k]);
00448                         for ($aa=0;$aa<$top;$aa++)      {
00449                                 $tempArr=array();
00450                                 for ($bb=0;$bb<$cols;$bb++)     {
00451                                         $tempArr[$bb]=str_pad($v[$bb][$aa],$messure[0][$bb],' ');
00452                                 }
00453                                 $outLines[]=$colChar.implode($colChar,$tempArr).$colChar;
00454                         }
00455                         $outLines[]=$this->addDiv($messure,'',$divChar,$joinChar,$cols);
00456                 }
00457                 return implode(chr(10),$outLines);
00458         }
00459 
00472         function addDiv($messure,$content,$divChar,$joinChar,$cols)     {
00473                 $tempArr=array();
00474                 for ($a=0;$a<$cols;$a++)        {
00475                         $tempArr[$a]=str_pad($content,$messure[0][$a],$divChar);
00476                 }
00477                 return $joinChar.implode($joinChar,$tempArr).$joinChar;
00478         }
00479 
00488         function traverseTable($tableLines)     {
00489                 $maxLen=array();
00490                 $maxLines=array();
00491                 reset($tableLines);
00492                 while(list($k,$v)=each($tableLines))    {
00493                         reset($v);
00494                         while(list($kk,$vv)=each($v))   {
00495                                 reset($vv);
00496                                 while(list($lk,$lv)=each($vv))  {
00497                                         if (strlen($lv)>intval($maxLen[$kk]))   $maxLen[$kk]=strlen($lv);
00498                                 }
00499                                 if (count($vv)>intval($maxLines[$k]))   $maxLines[$k]=count($vv);
00500                         }
00501                 }
00502                 return array($maxLen,$maxLines);
00503         }
00504 
00515         function renderImages($str,$links,$caption,$upload_path='uploads/pics/')        {
00516                 $images = explode(',',$str);
00517                 $linksArr = explode(',',$links);
00518                 reset($images);
00519                 $lines=array();
00520                 if ($this->conf['images.']['header'])   {$lines[]=$this->getString($this->conf['images.']['header']);}
00521                 while(list($k,$file)=each($images))     {
00522                         $lines[]=$this->siteUrl.$upload_path.$file;
00523                         if ($links && count($linksArr)>1)       {
00524                                 if (isset($linksArr[$k]))       {
00525                                         $ll=$linksArr[$k];
00526                                 } else {
00527                                         $ll=$linksArr[0];
00528                                 }
00529 
00530                                 $theLink = $this->getLink($ll);
00531                                 if ($theLink)   {$lines[]=$this->getString($this->conf['images.']['linkPrefix']).$theLink;}
00532                         }
00533                 }
00534                 if ($links && count($linksArr)==1)      {
00535                         $theLink = $this->getLink($links);
00536                         if ($theLink)   {$lines[]=$this->getString($this->conf['images.']['linkPrefix']).$theLink;}
00537                 }
00538                 if ($caption)   {
00539                         $lines[]='';
00540                         $cHeader = trim($this->getString($this->conf['images.']['captionHeader']));
00541                         if ($cHeader)           $lines[]=$cHeader;
00542                         $lines[]=$this->breakContent($caption);
00543                 }
00544 
00545                 return implode(chr(10),$lines);
00546         }
00547 
00554         function getLink($ll)   {
00555                 $theLink=$this->cObj->getTypoLink_URL($ll);
00556                 if (substr($theLink,0,4)!='http')       {
00557                         $theLink=$this->siteUrl.$theLink;
00558                 }
00559                 return $theLink;
00560         }
00561 
00571         function breakLines($str,$implChar="\n",$charWidth=0)   {
00572                 return t3lib_div::breakLinesForEmail($str,$implChar,$charWidth?$charWidth:$this->charWidth);
00573         }
00574 
00583         function getString($str)        {
00584                 $parts = explode('|',$str);
00585                 return strcmp($parts[1],'')?$parts[1]:$parts[0];
00586         }
00587 
00595         function userProcess($mConfKey,$passVar)        {
00596                 if ($this->conf[$mConfKey])     {
00597                         $funcConf = $this->conf[$mConfKey.'.'];
00598                         $funcConf['parentObj']=&$this;
00599                         $passVar = $GLOBALS['TSFE']->cObj->callUserFunction($this->conf[$mConfKey], $funcConf, $passVar);
00600                 }
00601                 return $passVar;
00602         }
00603 
00613         function atag_to_http($content,$conf)   {
00614                 $this->conf = $conf;
00615                 $this->siteUrl=$conf['siteUrl'];
00616                 $theLink  = trim($this->cObj->parameters['href']);
00617                 if (strtolower(substr($theLink,0,7))=='mailto:')        {
00618                         $theLink=substr($theLink,7);
00619                 } elseif (substr($theLink,0,4)!='http') {
00620                         $theLink=$this->siteUrl.$theLink;
00621                 }
00622                 return $this->cObj->getCurrentVal().' (Link: '.$theLink.' )';
00623         }
00624 
00632         function typolist($content,$conf)       {
00633                 $this->conf = $this->cObj->mergeTSRef($conf,'bulletlist');
00634                 $this->siteUrl=$conf['siteUrl'];
00635                 $str = trim($this->cObj->getCurrentVal());
00636                 $this->cObj->data['layout'] = $this->cObj->parameters['type'];
00637                 return $this->breakBulletlist($str);
00638         }
00639 
00647         function typohead($content,$conf)       {
00648                 $this->conf = $this->cObj->mergeTSRef($conf,'header');
00649 
00650                 $this->siteUrl=$conf['siteUrl'];
00651                 $str = trim($this->cObj->getCurrentVal());
00652                 $this->cObj->data['header_layout'] = $this->cObj->parameters['type'];
00653                 $this->cObj->data['header_position'] = $this->cObj->parameters['align'];
00654                 $this->cObj->data['header']=$str;
00655 
00656                 return $this->getHeader();
00657         }
00658 
00666         function typocode($content,$conf)       {
00667                         // Nothing is really done here...
00668                 $this->conf = $conf;
00669                 $this->siteUrl=$conf['siteUrl'];
00670                 return $this->cObj->getCurrentVal();
00671         }
00672 }
00673 
00674 
00675 
00676 
00677 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['media/scripts/plaintextLib.inc'])  {
00678         include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['media/scripts/plaintextLib.inc']);
00679 }
00680 ?>