[ Index ]
 

Code source de Plume CMS 1.2.2

Accιdez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/manager/inc/ -> lib.text.php (source)

   1  <?php
   2  /* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
   3  /*
   4  # ***** BEGIN LICENSE BLOCK *****
   5  # This file is part of Plume CMS, a website management application.
   6  # Copyright (C) 2001-2005 Loic d'Anterroches and contributors.
   7  #
   8  # Plume CMS is free software; you can redistribute it and/or modify
   9  # it under the terms of the GNU General Public License as published by
  10  # the Free Software Foundation; either version 2 of the License, or
  11  # (at your option) any later version.
  12  #
  13  # Plume CMS is distributed in the hope that it will be useful,
  14  # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16  # GNU General Public License for more details.
  17  #
  18  # You should have received a copy of the GNU General Public License
  19  # along with this program; if not, write to the Free Software
  20  # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  21  #
  22  # ***** END LICENSE BLOCK ***** */
  23  
  24  require_once dirname(__FILE__).'/lib.path.php';
  25  
  26  /**
  27   * The text class provides a set of methods to manipulate
  28   * text. The methods are static methods.
  29   */
  30  class text 
  31  {
  32  
  33      /** 
  34       * Return type of text (wiki, html, etc.)
  35       *
  36       * @param string Text to get the type from
  37       * @return mixed Type or empty string if not found
  38       */
  39      function getType($text='')
  40      {
  41          if (0 == strlen($text)) 
  42              return '';
  43          $format = '';
  44          if (preg_match('/^=([A-Za-z]{4})\s*/', $text, $match)) {
  45              $format  = trim($match[1]);
  46          }
  47          return $format;
  48      }
  49  
  50      /**
  51       * Get the raw content of a text.
  52       * It removes the type of the text if any.
  53       *
  54       * @param string Text
  55       * @return string Raw text
  56       */
  57  
  58      function getRawContent($text='')
  59      {
  60          if (0 == strlen($text)) 
  61              return '';
  62          if (preg_match('/^=[A-Za-z]{4}\s*/', $text)) {
  63              $text = substr($text, 5);
  64          }
  65          return trim($text);
  66      }
  67  
  68      /** 
  69       * Call the wiki parser depending of the content of the string
  70       *
  71       * @param string unparsed content
  72       * @param string output format, ('html') or 'text'
  73       * @return string parsed content
  74       *
  75       */
  76      function parseContent($text='', $output='html')
  77      {
  78          if (0 == strlen($text)) return '';
  79          $output = 'To'.$output;
  80  
  81          $format = 'wiki';
  82          if (preg_match('/^=([A-Za-z]{4})\s*/', $text, $match)) {
  83              $text = substr($text, 5);
  84              $format  = trim($match[1]);
  85          }
  86          if (is_callable(array('text','parse'.$format.$output))) {
  87              $parsefunc = 'parse'.$format.$output;
  88          } else {
  89              $parsefunc = 'parseWikiToHtml';
  90          }
  91          $content = call_user_func(array('text', $parsefunc), $text);
  92          Hook::run('onParseContent', array('text' => &$content, 
  93                                            'output' => &$output));
  94          return $content;
  95          
  96      }
  97  
  98      /**
  99       * Parse wiki formatted text into XHTML
 100       *
 101       * @param string Wiki formatted text
 102       * @return string XHTML
 103       */
 104      function parseWikiToHtml($text)
 105      {
 106          include_once dirname(__FILE__).'/../extinc/WikiRenderer.lib.php';
 107          include_once dirname(__FILE__).'/wikirenderer_xhtml.conf.php';
 108      
 109          $wkr = new WikiRenderer(new WikiRenderXhtmlConfig(), 'xhtml_');
 110          return $wkr->render($text);
 111      }
 112  
 113      function parseWikiToText($text)
 114      {
 115          include_once dirname(__FILE__).'/../extinc/WikiRenderer.lib.php';
 116          include_once dirname(__FILE__).'/../extinc/WikiRenderer_w2text.conf.php';
 117      
 118          $wkr = new WikiRenderer(new WikiRenderer_w2text());
 119          return $wkr->render($text);
 120      }
 121  
 122      function parseHtmlToHtml($text)
 123      {
 124          while (preg_match('#<xlink\s+id="(.*)"\s+type="(.*)"\s*/>#i', $text, $match) 
 125                 or
 126                 preg_match('#<xlink\s+id="(.*)"\s+type="(.*)"\s*>(.*)</xlink>#i', $text, $match)) {
 127              $all   = quotemeta($match[0]);
 128              $id    = $match[1];
 129              $type  = $match[2];
 130              $title = '';
 131              if (isset($match[3])) $title = $match[3];
 132              $replace = text::xlinkCreate($id, $type, $title);
 133              $title   = '';
 134              $text    = preg_replace('#'.$all.'#', $replace, $text);
 135          }
 136          return $text;
 137      }
 138  
 139      function parseHtmlToText($text)
 140      {
 141          include_once dirname(__FILE__).'/../extinc/class.html2text.php';
 142          $h2t = new html2text($text);
 143          return $h2t->get_text();
 144      }
 145  
 146      /** create a link to a resource in the CMS
 147       *
 148       * @return string the HTML tag with the link
 149       * @param  string id of the resource (art-12, file-2, news-1, etc.)
 150       * @param  string format of the link 'textlink', 'img' or 'icon'
 151       * @param  string title for the link
 152       */
 153       function xlinkCreate($id, $type = 'textlink', $title = '')
 154      {
 155          include_once dirname(__FILE__).'/class.manager.php';
 156          $m = new Manager();
 157          $r = $m->getResourceByIdentifier($id);
 158          if ($r->isEmpty()) {
 159              return '';
 160          } else {        
 161              if (strlen($title)) {
 162                  $r->setField('new-title',$title);
 163              } else {
 164                  $r->setField('new-title', $r->f('title'));
 165              }
 166              if (function_exists('text::xlinkCreate'.$type)) {
 167                  $createfunc = 'xlinkCreate'.$type;
 168              } else {
 169                  $createfunc = 'xlinkCreateTextLink';
 170              }
 171              return call_user_func(array('text', $createfunc), $r);
 172                 
 173  
 174              return $createfunc($r);
 175          }
 176      }
 177  
 178  
 179      function xlinkCreateTextLink($res)
 180      {
 181          return '<a href="'.$res->getPath().'">'.$res->f('new-title').'</a>';
 182      }
 183  
 184      /** 
 185       * hex encode a string not to be 'seen' by a spam system
 186       *
 187       * @return string encoded
 188       * @param  string not encoded
 189       * @param  bool   is the string for outside a &lt;a ...&gt; link?
 190       */  
 191      function hexEncode($str, $text=false)
 192      {
 193          $encoded = '';
 194          if ($text) {
 195              for ($i=0; $i<strlen($str); $i++) {
 196                  if (ord(substr($str, $i, 1)) < 127) {
 197                      $encoded .= '&#x'.bin2hex(substr($str, $i, 1)).';';
 198                  } else {
 199                      $encoded .= substr($str, $i, 1);
 200                  }
 201              }
 202          } else {
 203              $encoded = bin2hex($str);
 204              $encoded = chunk_split($encoded, 2, '%');
 205              $encoded = '%'.substr($encoded, 0, strlen($encoded) - 1);
 206          }
 207          return $encoded;
 208      } 
 209  
 210      /** 
 211       * Remove entities of a string
 212       * 
 213       * @author Olivier Meunier
 214       *
 215       * @param  string String with entities
 216       * @return string String without entities
 217       */
 218      function removeEntities($string)
 219      {
 220          if (strtolower(config::f('encoding')) == 'utf-8') 
 221              $string = utf8_decode($string);
 222      
 223          // Table of codes  130 to 140 and 145 to 156
 224          $tags = array('‚'=>'&sbquo;','ƒ'=>'&fnof;','„'=>'&bdquo;',
 225                        '…'=>'&hellip;','†'=>'&dagger;', 
 226                        '‡'=>'&Dagger;','ˆ'=>'&circ;','‰'=>'&permil;',
 227                        'Š'=>'&Scaron;','‹'=>'&lsaquo;','Œ'=>'&OElig;',
 228                        '‘'=>'&lsquo;','’'=>'&rsquo;','“'=>'&ldquo;',
 229                        '”'=>'&rdquo;','•'=>'&bull;','–'=>'&ndash;',
 230                        '—'=>'&mdash;','˜'=>'&tilde;','™'=>'&trade;',
 231                        'š'=>'&scaron;','›'=>'&rsaquo;','œ'=>'&oelig;',
 232                        'Ÿ'=>'&Yuml','€'=>'&euro;');
 233          $vtags = array('‚'=>'&#8218;','ƒ'=>'&#402;','„'=>'&#8222;',
 234                         '…'=>'&#8230;','†'=>'&#8224;','‡'=>'&#8225;',
 235                         'ˆ'=>'&#710;','‰'=>'&#8240;','Š'=>'&#352;',
 236                         '‹'=>'&#8249;','Œ'=>'&#338;','‘'=>'&#8216;',
 237                         '’'=>'&#8217;','“'=>'&#8220;','”'=>'&#8221;',
 238                         '•'=>'&#8226;','–'=>'&#8211;','—'=>'&#8212;',
 239                         '˜'=>'&#732;','™'=>'&#8482;','š'=>'&#353;',
 240                         '›'=>'&#8250;','œ'=>'&#339;','Ÿ'=>'&#376;',
 241                         '€'=>'&#8364;');
 242      
 243          $tags = array_merge($tags,get_html_translation_table(HTML_ENTITIES));
 244          foreach($tags as $k => $v) {
 245              $ASCIItags[$k] = '&#'.ord($k).';';
 246          }
 247      
 248          $string = str_replace($tags,array_flip($tags),$string);
 249          $string = str_replace($ASCIItags,array_flip($ASCIItags),$string);
 250          $string = str_replace(array_values($vtags),array_keys($vtags),$string);
 251      
 252          return if_utf8($string);
 253      }
 254  
 255      /** 
 256       * Remove the HTML entities of a string to be XML compliant.
 257       *
 258       * @author Olivier Meunier
 259       *
 260       * @param string String to be converted
 261       * @param bool Should the output be UTF-8
 262       * @return string Converted string
 263       */
 264      function toXML($string,$utf8=true)
 265      {
 266          $string = htmlspecialchars(text::removeEntities($string),ENT_NOQUOTES);
 267          if($utf8) {
 268              $string = utf8_encode($string);
 269          }
 270          return $string;
 271      }
 272  
 273      /**
 274       * Convert an URL into words (space delimited).
 275       *
 276       * @param string URL
 277       * @return string Space delimited words
 278       */
 279      function url2words($url)
 280      {
 281          $url = ' '.removeFileExtension($url);
 282          $url = preg_replace('/[^a-z0-9]/i',' ', $url);
 283          $url = preg_replace('/[a-z]([A-Z])([A-Z])([a-z]|$)/', " \\1 \\2\\3", $url);
 284          $url = preg_replace('/([A-Z][a-z0-9]+)/', " \\1", $url);
 285          $url = preg_replace('/( [0-9]+ )/', ' ', $url);
 286          return trim(preg_replace('/([A-Z][A-Z]+)/', " \\1", $url));
 287      }
 288  
 289  
 290      /**
 291       * Convert a string into an URL string.
 292       *
 293       * Je n'aime pas ιcrire du code becomes Je-n-aime-pas-ecrire-du-code
 294       *
 295       * @author Olivier Meunier
 296       *
 297       * @param string String to convert
 298       * @param string Separator ('-')
 299       * @return string URL ready string
 300       */
 301      function str2url($str, $sep='-')
 302      {
 303          $pattern['A'] = '\x{00C0}-\x{00C5}';
 304          $pattern['AE'] = '\x{00C6}';
 305          $pattern['C'] = '\x{00C7}';
 306          $pattern['D'] = '\x{00D0}';
 307          $pattern['E'] = '\x{00C8}-\x{00CB}';
 308          $pattern['I'] = '\x{00CC}-\x{00CF}';
 309          $pattern['N'] = '\x{00D1}';
 310          $pattern['O'] = '\x{00D2}-\x{00D6}\x{00D8}';
 311          $pattern['OE'] = '\x{0152}';
 312          $pattern['S'] = '\x{0160}';
 313          $pattern['U'] = '\x{00D9}-\x{00DC}';
 314          $pattern['Y'] = '\x{00DD}';
 315          $pattern['Z'] = '\x{017D}';
 316          
 317          $pattern['a'] = '\x{00E0}-\x{00E5}';
 318          $pattern['ae'] = '\x{00E6}';
 319          $pattern['c'] = '\x{00E7}';
 320          $pattern['d'] = '\x{00F0}';
 321          $pattern['e'] = '\x{00E8}-\x{00EB}';
 322          $pattern['i'] = '\x{00EC}-\x{00EF}';
 323          $pattern['n'] = '\x{00F1}';
 324          $pattern['o'] = '\x{00F2}-\x{00F6}\x{00F8}';
 325          $pattern['oe'] = '\x{0153}';
 326          $pattern['s'] = '\x{0161}';
 327          $pattern['u'] = '\x{00F9}-\x{00FC}';
 328          $pattern['y'] = '\x{00FD}\x{00FF}';
 329          $pattern['z'] = '\x{017E}';
 330          
 331          $pattern['ss'] = '\x{00DF}';
 332          
 333          foreach ($pattern as $r => $p) {
 334              $str = preg_replace('/['.$p.']/u',$r,$str);
 335          }
 336          
 337          $str = preg_replace('/[^A-Za-z0-9_\s\'\:\/[\]-]/','',$str);
 338  
 339          $str = strip_tags($str);
 340          $str = str_replace(array('?','&','#','=','+','<','>'),'',$str);
 341          $str = str_replace("'",'',$str);
 342          $str = preg_replace('/[\s]+/',' ',trim($str));
 343          $str = str_replace('/','-',$str);
 344          $str = str_replace(' ','-',$str);
 345          $str = preg_replace('/[-]+/','-',$str);
 346          
 347          return $str;
 348      }
 349  
 350      /** 
 351       * Clean a string.
 352       * This function removes more or less everything but do not escape
 353       * the "'" for DB insertion.
 354       *
 355       * @author Olivier Meunier
 356       * 
 357       * @param string String to "secure"
 358       * @param string Secured string
 359       */
 360      function secure($str)
 361      {
 362          $str = trim($str);
 363          $str = stripslashes($str);
 364          $str = strip_tags($str);
 365          $str = htmlspecialchars($str);
 366          return $str;
 367      }
 368  
 369  
 370      /** 
 371       * Truncate a string to a certain length if necessary without
 372       * splitting in the middle of a word and appending the $etc
 373       * string. Based on smarty modifier.
 374       *
 375       * @author   Monte Ohrt <monte at ohrt dot com>
 376       * @param string
 377       * @param integer Length
 378       * @param string etc. string
 379       * @return string
 380       */
 381      function truncate($string, $length=80, $etc='...')
 382      {
 383          if ($length == 0)
 384              return '';
 385          if (strlen($string) > $length) {
 386              $length -= strlen($etc);
 387              $string = preg_replace('/\s+?(\S+)?$/', '', substr($string, 0, $length+1));
 388              return substr($string, 0, $length).$etc;
 389          } else {
 390              return $string;
 391          }
 392      }
 393  
 394  
 395  }
 396  ?>


Gιnιrι le : Mon Nov 26 11:57:01 2007 par Balluche grβce ΰ PHPXref 0.7
  Clicky Web Analytics