TYPO3 API  SVNRelease
EscapeViewHelper.php
Go to the documentation of this file.
00001 <?php
00002 
00003 /*                                                                        *
00004  * This script belongs to the FLOW3 package "Fluid".                      *
00005  *                                                                        *
00006  * It is free software; you can redistribute it and/or modify it under    *
00007  * the terms of the GNU Lesser General Public License as published by the *
00008  * Free Software Foundation, either version 3 of the License, or (at your *
00009  * option) any later version.                                             *
00010  *                                                                        *
00011  * This script is distributed in the hope that it will be useful, but     *
00012  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
00013  * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser       *
00014  * General Public License for more details.                               *
00015  *                                                                        *
00016  * You should have received a copy of the GNU Lesser General Public       *
00017  * License along with the script.                                         *
00018  * If not, see http://www.gnu.org/licenses/lgpl.html                      *
00019  *                                                                        *
00020  * The TYPO3 project - inspiring people to share!                         *
00021  *                                                                        */
00022 
00023 /**
00024  * The EscapeViewHelper is used to escape variable content in various ways. By
00025  * default HTML is the target.
00026  *
00027  * = Examples =
00028  *
00029  * <code title="HTML">
00030  * <f:escape>{text}</f:escape>
00031  * </code>
00032  * <output>
00033  * Text with & " ' < > * replaced by HTML entities (htmlspecialchars applied).
00034  * </output>
00035  *
00036  * <code title="Entities">
00037  * <f:escape type="entities">{text}</f:escape>
00038  * </code>
00039  * <output>
00040  * Text with all possible chars replaced by HTML entities (htmlentities applied).
00041  * </output>
00042  *
00043  * <code title="URL">
00044  * <f:escape type="url">{text}</f:escape>
00045  * </code>
00046  * <output>
00047  * Text encoded for URL use (rawurlencode applied).
00048  * </output>
00049  *
00050  * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
00051  * @api
00052  */
00053 class Tx_Fluid_ViewHelpers_EscapeViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractViewHelper {
00054 
00055     /**
00056      * Escapes special characters with their escaped counterparts as needed.
00057      *
00058      * @param string $value
00059      * @param string $type The type, one of html, entities, url
00060      * @param string $encoding
00061      * @return string the altered string.
00062      * @author Karsten Dambekalns <karsten@typo3.org>
00063      * @api
00064      */
00065     public function render($value = NULL, $type = 'html', $encoding = NULL) {
00066         if ($value === NULL) {
00067             $value = $this->renderChildren();
00068         }
00069 
00070         if (!is_string($value)) {
00071             return $value;
00072         }
00073 
00074         if ($encoding === NULL) {
00075             $encoding = $this->resolveDefaultEncoding();
00076         }
00077 
00078         switch ($type) {
00079             case 'html':
00080                 return htmlspecialchars($value, ENT_COMPAT, $encoding);
00081             break;
00082             case 'entities':
00083                 return htmlentities($value, ENT_COMPAT, $encoding);
00084             break;
00085             case 'url':
00086                 return rawurlencode($value);
00087             default:
00088                 return $value;
00089             break;
00090         }
00091     }
00092 
00093     /**
00094      * Resolve the default encoding. If none is set in Frontend or Backend, uses UTF-8.
00095      *
00096      * @return string the encoding
00097      */
00098     protected function resolveDefaultEncoding() {
00099         if (TYPO3_MODE === 'BE') {
00100             $encoding = strtoupper($GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset']);
00101 
00102             if ($encoding === NULL) {
00103                 $encoding = 'UTF-8';
00104             }
00105             return $encoding;
00106         } else {
00107             return strtoupper($GLOBALS['TSFE']->renderCharset);
00108         }
00109     }
00110 }
00111 ?>