HTML_Template_Flexy
[ class tree: HTML_Template_Flexy ] [ index: HTML_Template_Flexy ] [ all elements ]

Source for file SimpleTags.php

Documentation is available at SimpleTags.php

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Author:  Alan Knowles <alan@akbkhome.com>
  17. // +----------------------------------------------------------------------+
  18. //
  19.  
  20.  
  21. /**
  22. * The Standard Tag filter
  23. *
  24. @abstract
  25. *  does all the clever stuff...
  26. *
  27. *  Security Notes:
  28. *    Templates should not originate from untrusted sources,
  29. *     - the  method(#.....#) could be regarded as insecure.
  30. *     - there is no attempt to protect your from <script / <?php in templates.
  31. *
  32. @package    HTML_Template_Flexy
  33. *
  34. */
  35.  
  36.  
  37. {
  38.     /*
  39.     *   @var     object HTML_Template_Flexy   the main engine
  40.     */
  41.     var $engine// the engine (with options)
  42.     /*
  43.     *   @var    string   $start    the start tag for the template (escaped for regex)
  44.     */
  45.     var $start = '\{';
  46.  
  47.      /*
  48.     *   @var    string   $stop    the stopt tag for the template (escaped for regex)
  49.     */
  50.     var $stop = '\}';
  51.      /*
  52.     *   @var    string   $error    show/hide the PHP error messages on/off in templates
  53.     */
  54.     var $error = "@"// change to blank to debug errors.
  55.     /**
  56.     * Standard Set Engine
  57.     *
  58.     *
  59.     * @param   object HTML_Template_Flexy   the main engine
  60.     * @access   private
  61.     */
  62.     function _set_engine(&$engine{
  63.         $this->engine = &$engine;
  64.         if ($this->engine->options['debug']{
  65.             $this->error = "";
  66.         }
  67.     }
  68.  
  69.  
  70.  
  71.     /**
  72.     * Standard Variable replacement
  73.     *
  74.     *
  75.     * Maps variables
  76.     * {i.xyz}             maps to  <?php echo htmlspecialchars($i->xyz)?>
  77.     * {i.xyz:h}           maps to  <?php echo $i->xyz?>
  78.     * {i.xyz:u}           maps to  <?php echo urlencode($i->xyz)?>
  79.     * {i.xyz:ru}           maps to  <?php echo rawurlencode($i->xyz)?>
  80.     *
  81.     * {i.xyz:r}           maps to  <PRE><?php echo print_r($i->xyz)?></PRE>
  82.     * {i.xyz:n}           maps to  <?php echo nl2br(htmlspecialchars($i->xyz))?>
  83.     *
  84.     *
  85.     * @param   string    $input the template
  86.     * @return    string   the result of the filtering
  87.     * @access   public
  88.     */
  89.  
  90.  
  91.  
  92.     function variables ($input{
  93.         $input preg_replace(
  94.             "/".$this->start."([a-z0-9_.]+)".$this->stop."/ie",
  95.             "'<?php echo htmlspecialchars(".$this->error."$'.str_replace('.','->','\\1').')?>'",
  96.             $input);
  97.  
  98.  
  99.         $input preg_replace(
  100.             "/".$this->start."([a-z0-9_.]+):h".$this->stop."/ie",
  101.             "'<?php echo ".$this->error."$'.str_replace('.','->','\\1').'?>'",
  102.             $input);
  103.  
  104.         $input preg_replace(
  105.             "/".$this->start."([a-z0-9_.]+):u".$this->stop."/ie",
  106.             "'<?php echo urlencode(".$this->error."$'.str_replace('.','->','\\1').')?>'",
  107.             $input);
  108.  
  109.         $input preg_replace(
  110.             "/".$this->start."([a-z0-9_.]+):ru".$this->stop."/ie",
  111.             "'<?php echo rawurlencode(".$this->error."$'.str_replace('.','->','\\1').')?>'",
  112.             $input);
  113.  
  114.         $input preg_replace(
  115.             "/".$this->start."([a-z0-9_.]+):r".$this->stop."/ie",
  116.             "'<PRE><?php echo print_r($'.str_replace('.','->','\\1').')?></PRE>'",
  117.             $input);
  118.  
  119.         $input preg_replace(
  120.             "/".$this->start."([a-z0-9_.]+):n".$this->stop."/ie",
  121.             "'<?php echo nl2br(htmlspecialchars(".$this->error."$'.str_replace('.','->','\\1').'))?>'",
  122.             $input);
  123.         return $input;
  124.  
  125.     }
  126.      /**
  127.     * Urlencoded Variable replacement
  128.     *
  129.     * Often when you use a WYSISYG editor, it replaces { in
  130.     * the  href="{somevar}" with the urlencoded version, this bit fixes it.
  131.     *
  132.     * Maps variables
  133.     * %??i.xyz%??             maps to  <?php echo htmlspecialchars($i->xyz)?>
  134.     * %??i.xyz:h%??           maps to  <?php echo $i->xyz?>
  135.     * %??i.xyz:u%??           maps to  <?php echo urlencode($i->xyz)?>
  136.     * %??i.xyz:ru%??           maps to  <?php echo urlencode($i->xyz)?>
  137.     *           THIS IS PROBABLY THE ONE TO USE!
  138.     *
  139.     * %??i.xyz:uu%??           maps to <?php echo urlencode(urlencode($i->xyz))?>
  140.     *
  141.     *
  142.     * @param   string    $input the template
  143.     * @return    string   the result of the filtering
  144.     * @access   public
  145.     */
  146.  
  147.     function urlencoded_variables ($input{
  148.         $input preg_replace(
  149.             "/".urlencode(stripslashes($this->start))."([a-z0-9_.]+)".urlencode(stripslashes($this->stop))."/ie",
  150.             "'<?php echo htmlspecialchars(".$this->error."$'.str_replace('.','->','\\1').')?>'",
  151.             $input);
  152.  
  153.  
  154.         $input preg_replace(
  155.             "/".urlencode(stripslashes($this->start))."([a-z0-9_.]+):h".urlencode(stripslashes($this->stop))."/ie",
  156.             "'<?php echo ".$this->error."$'.str_replace('.','->','\\1').'?>'",
  157.             $input);
  158.  
  159.         $input preg_replace(
  160.             "/".urlencode(stripslashes($this->start))."([a-z0-9_.]+):u".urlencode(stripslashes($this->stop))."/ie",
  161.             "'<?php echo urlencode(".$this->error."$'.str_replace('.','->','\\1').')?>'",
  162.             $input);
  163.  
  164.         $input preg_replace(
  165.             "/".urlencode(stripslashes($this->start))."([a-z0-9_.]+):uu".urlencode(stripslashes($this->stop))."/ie",
  166.             "'<?php echo urlencode(urlencode(".$this->error."$'.str_replace('.','->','\\1').'))?>'",
  167.             $input);
  168.         return $input;
  169.     }
  170.      /**
  171.     * Calling Methods
  172.     *
  173.     * This allows you to call methods of your application
  174.     *
  175.     * Maps Methods
  176.     * {t.xxxx_xxxx()}                 maps to <?php echo htmlspecialchars($t->xxxx_xxxx())?>
  177.     * {t.xxxx_xxxx():h}               maps to <?php echo $t->xxxx_xxxx()?>
  178.     *
  179.     * {t.xxxx_xxxx(sssss.dddd)}       maps to <?php echo htmlspecialchars($t->xxxx_xxxx($ssss->dddd))?>
  180.     * {t.xxxx_xxxx(sssss.dddd):h}     maps to <?php echo $t->xxxx_xxxx($ssss->dddd)?>
  181.     * {t.xxxx_xxxx(sssss.dddd):s}     maps to <?php highlight_string($t->xxxx_xxxx($ssss->dddd))?>
  182.     *
  183.     * {t.xxxx_xxxx(#XXXXX#)}          maps to <?php echo htmlspecialchars($t->xxxx_xxxx('XXXXXX'))?>
  184.     * {t.xxxx_xxxx(#XXXXX#):h}        maps to <?php echo $t->xxxx_xxxx('XXXXXX')?>
  185.     *
  186.     * {t.xxxx_xxxx(sss.ddd,sss.ddd)}  maps to <?php echo htmlspecialchars($t->xxxx_xxxx($sss->ddd,$sss->ddd))?>
  187.     * {t.xxxx_xxxx(#aaaa#,sss.ddd)}   maps to <?php echo htmlspecialchars($t->xxxx_xxxx("aaaa",$sss->ddd))?>
  188.     * {t.xxxx_xxxx(sss.ddd,#aaaa#)}   maps to <?php echo htmlspecialchars($t->xxxx_xxxx($sss->ddd,"aaaa"))?>
  189.     *
  190.     *
  191.     *
  192.     * @param   string    $input the template
  193.     * @return    string   the result of the filtering
  194.     * @access   public
  195.     */
  196.  
  197.  
  198.  
  199.  
  200.  
  201.     function methods($input{
  202.  
  203.         /* no vars */
  204.         $input preg_replace(
  205.             "/".$this->start."([a-z0-9_.]+)\(\)".$this->stop."/ie",
  206.             "'<?php echo htmlspecialchars(".$this->error."$'.str_replace('.','->','\\1').'())?>'",
  207.             $input);
  208.  
  209.         $input preg_replace(
  210.             "/".$this->start."([a-z0-9_.]+)\(\):h".$this->stop."/ie",
  211.             "'<?php echo ".$this->error."$'.str_replace('.','->','\\1').'()?>'",
  212.             $input);
  213.         /* single vars */
  214.         $input preg_replace(
  215.             "/".$this->start."([a-z0-9_.]+)\(([a-z0-9_.]+)\)".$this->stop."/ie",
  216.             "'<?php echo htmlspecialchars(".$this->error."$'.str_replace('.','->','\\1').'($' .  str_replace('.','->','\\2') . '))?>'",
  217.             $input);
  218.  
  219.         $input preg_replace(
  220.             "/".$this->start."([a-z0-9_.]+)\(([a-z0-9_.]+)\):h".$this->stop."/ie",
  221.             "'<?php echo ".$this->error."$'.str_replace('.','->','\\1').'($' .  str_replace('.','->','\\2') . ')?>'",
  222.             $input);
  223.         $input preg_replace(
  224.             "/".$this->start."([a-z0-9_.]+)\(([a-z0-9_.]+)\):s".$this->stop."/ie",
  225.             "'<?php highlight_string($'.str_replace('.','->','\\1').'($' .  str_replace('.','->','\\2') . '));?>'",
  226.             $input);
  227.         /* double vars     */
  228.         $input preg_replace(
  229.             "/".$this->start."([a-z0-9_.]+)\(([a-z0-9_.]+),([a-z0-9_.]+)\)".$this->stop."/ie",
  230.             "'<?php echo htmlspecialchars(".$this->error."$'.str_replace('.','->','\\1').'($' .  str_replace('.','->','\\2') . ',$' .  str_replace('.','->','\\3') . '))?>'",
  231.             $input);
  232.           /* double vars:: # #'d  ,var */
  233.         $input preg_replace(
  234.             "/".$this->start."([a-z0-9_.]+)\(\#([^\#]+)\#,([a-z0-9_.]+)\)".$this->stop."/ie",
  235.             "'<?php echo htmlspecialchars(".$this->error."$'.str_replace('.','->','\\1').'(\''. str_replace(\"'\",\"\\\'\",'\\2') . '\',$' .  str_replace('.','->','\\3') . '))?>'",
  236.             $input);
  237.           /* double vars:: var , # #'d  */
  238.         $input preg_replace(
  239.             "/".$this->start."([a-z0-9_.]+)\(([a-z0-9_.]+),\#([^\#]+)\#\)".$this->stop."/ie",
  240.             "'<?php echo htmlspecialchars(".$this->error."$'.str_replace('.','->','\\1').'($' .  str_replace('.','->','\\2') . ',\''. str_replace(\"'\",\"\\\'\",'\\3') . '\'))?>'",
  241.             $input);
  242.  
  243.         /*strings or integers */
  244.         $input preg_replace(
  245.             "/".$this->start."([a-z0-9_.]+)\(\#([^\#]+)\#\)".$this->stop."/ie",
  246.             "'<?php echo htmlspecialchars(\$'.str_replace('.','->','\\1') . '(\''. str_replace(\"'\",\"\\\'\",'\\2') . '\'))?>'",
  247.             $input);
  248.  
  249.         $input preg_replace(
  250.             "/".$this->start."([a-z0-9_.]+)\(\#([^\#]+)\#\):h".$this->stop."/ie",
  251.             "'<?php echo ".$this->error."$'.str_replace('.','->','\\1').'(\"' .  str_replace(\"'\",\"\\\'\",'\\2') . '\")?>'",
  252.             $input);
  253.  
  254.         return $input;
  255.     }
  256.     /**
  257.     * Looping
  258.     *
  259.     * This allows you to do loops on variables (eg. nested/ repeated blocks!)
  260.     *
  261.     * Maps Methods
  262.     * {foreach:t.xyz,zzz}     maps to  <?php if ($i->xyz) foreach ($t->xyz as $zzz) { ?>
  263.     * {foreach:t.xyz,xxx,zzz} maps to  <?php if ($i->xyz) foreach ($t->xyz as $xxx=>$zzz) { ?>
  264.     * {end:}                  maps to  <?php }?>
  265.     * {else:}                 maps to  <?php }else{?>
  266.     *
  267.     *
  268.     *
  269.     * @param    string    $input the template
  270.     * @return   string    the result of the filtering
  271.     * @access   public
  272.     */
  273.  
  274.  
  275.     function looping($input{
  276.  
  277.  
  278.         $input preg_replace(
  279.             "/".$this->start."foreach:([a-z0-9_.]+),([a-z0-9_.]+)".$this->stop."/ie",
  280.             "'<?php if (".$this->error."$' . str_replace('.','->','\\1') . ') foreach( $' . str_replace('.','->','\\1') . ' as $' . str_replace('.','->','\\2') . ') { ?>'",
  281.             $input);
  282.         $input preg_replace(
  283.             "/".$this->start."foreach:([a-z0-9_.]+),([a-z0-9_.]+),([a-z0-9_.]+)".$this->stop."/ie",
  284.             "'<?php if (".$this->error."$' . str_replace('.','->','\\1') . ') foreach( $' . str_replace('.','->','\\1') . ' as $' . str_replace('.','->','\\2') . '=>$' .  str_replace('.','->','\\3') .') { ?>'",
  285.             $input);
  286.  
  287.         $input str_replace(stripslashes($this->start)."else:".stripslashes($this->stop),'<?php }else{?>'$input);
  288.         $input str_replace(stripslashes($this->start)."end:".stripslashes($this->stop),'<?php }?>'$input);
  289.         return $input;
  290.     }
  291.     /**
  292.     * Conditional inclusion
  293.     *
  294.     * This allows you to do conditional inclusion (eg. blocks!)
  295.     *
  296.     * Maps conditions
  297.     *
  298.     * {if:t.xxxx}         => <?php if ($t->xxxx) { ?>
  299.     * {if:t.x_xxx()}      => <?php if ($t->x_xxx()) { ?>
  300.     *
  301.     * @param    string   $input the template
  302.     * @return   string   the result of the filtering
  303.     * @access   public
  304.     */
  305.  
  306.     function conditionals($input{
  307.  
  308.         $input preg_replace(
  309.             "/".$this->start."if:([a-z0-9_.]+)".$this->stop."/ie",
  310.             "'<?php if (".$this->error."$' . str_replace('.','->','\\1') . ') { ?>'",
  311.             $input);
  312.  
  313.         $input preg_replace(
  314.             "/".$this->start."if:([a-z0-9_.]+)\(\)".$this->stop."/ie",
  315.             "'<?php if (".$this->error."$' . str_replace('.','->','\\1') . '()) { ?>'",
  316.             $input);
  317.  
  318.         return $input;
  319.     }
  320.     /**
  321.     * sub template inclusion
  322.     *
  323.     * This allows you to do include other files (either flat or generated templates.).
  324.     *
  325.     * {include:t.abcdef}    maps to  <?php
  326.     *                       if($t->abcdef && file_exists($compileDir . "/". $t->abcdef . "en.php"))
  327.     *                           include($compileDir . "/". $t->abcdef . ".en.php");
  328.     *                       ?>
  329.     *
  330.     * include abcdef.en.php (Eg. hard coded compiled template
  331.     * {include:#abcdef#}    => <?php
  332.     *                       if(file_exists($compileDir . "/abcdef.en.php"))
  333.     *                           include($compileDir . "/abcdef.en.php");
  334.     *                       ?>
  335.     *
  336.     *  include raw
  337.     * {t_include:#abcdef.html#}    => <?php
  338.     *                       if(file_exists($templateDir . "/abcdef.html"))
  339.     *                           include($compileDir . "/abcdef.html");
  340.     *                       ?>
  341.     *  Compile and include
  342.     * {q_include:#abcdef.html#}    => <?php
  343.     *                      HTML_Template_Flexy::staticQuickTemplate('abcedef.html',$t);
  344.     *                       ?>
  345.     *
  346.     *
  347.     * @param    string   $input the template
  348.     * @return   string   the result of the filtering
  349.     * @access   public
  350.     */
  351.  
  352.     function include_template($input{
  353.  
  354.         // array not supported for this type of template.
  355.         if (is_array($this->engine->options['templateDir'])) {
  356.             return $input;
  357.         }
  358.         
  359.         $input preg_replace(
  360.             "/".$this->start."include:([a-z0-9_.]+)".$this->stop."/ie",
  361.             "'<?php
  362.                 if ((".$this->error."$' . str_replace('.','->','\\1') . ') &&
  363.                     file_exists(\"" .  $this->engine->options['compileDir'.
  364.                     "/\{$' . str_replace('.','->','\\1') . '}.en.php\"))
  365.                 include(\"" .  $this->engine->options['compileDir'.
  366.                     "/\{$' . str_replace('.','->','\\1') . '}.en.php\");?>'",
  367.             $input);
  368.  
  369.         $input preg_replace(
  370.             "/".$this->start."include:#([a-z0-9_.]+)#".$this->stop."/ie",
  371.             "'<?php if (file_exists(\"" .  $this->engine->options['compileDir'"/\\1.en.php\")) include(\"" .
  372.             $this->engine->options['compileDir'"/\\1.en.php\");?>'",
  373.             $input);
  374.  
  375.         $input preg_replace(
  376.             "/".$this->start."t_include:#([a-z0-9_.]+)#".$this->stop."/ie",
  377.             "'<?php if (file_exists(\"" .  $this->engine->options['templateDir'.
  378.             "/\\1\")) include(\"" .  $this->engine->options['templateDir'"/\\1\");?>'",
  379.             $input);
  380.  
  381.         $input preg_replace(
  382.             "/".$this->start."q_include:#([a-z0-9_.]+)#".$this->stop."/ie",
  383.             "'<?php  HTML_Template_Flexy::staticQuickTemplate(\"\\1\",\$t); ?>'",
  384.             $input);
  385.  
  386.         return $input;
  387.     }
  388.  
  389.  
  390.  
  391.  
  392.  
  393. }
  394.  
  395. ?>

Documentation generated on Mon, 11 Mar 2019 15:59:57 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.