. * */ namespace SP; defined('APP_ROOT') || die(_('No es posible acceder directamente a este archivo')); /** * Esta clase es la encargada de mostrar el HTML */ class Html { /** * Limpia los datos recibidos de un formulario. * * @param string $data con los datos a limpiar * @return false|string con los datos limpiados */ public static function sanitize(&$data) { if (!$data) { return false; } if (is_array($data)) { array_walk_recursive($data, '\SP\Html::sanitize'); } else { $data = strip_tags($data); // Fix &entity\n; $data = str_replace(array('&', '<', '>'), array('&amp;', '&lt;', '&gt;'), $data); $data = preg_replace('/(&#*\w+)[\x00-\x20]+;/u', '$1;', $data); $data = preg_replace('/(&#x*[0-9A-F]+);*/iu', '$1;', $data); $data = html_entity_decode($data, ENT_COMPAT, 'UTF-8'); // Remove any attribute starting with "on" or xmlns $data = preg_replace('#(<[^>]+?[\x00-\x20"\'])(?:on|xmlns)[^>]*+>#iu', '$1>', $data); // Remove javascript: and vbscript: protocols $data = preg_replace('#([a-z]*)[\x00-\x20]*=[\x00-\x20]*([`\'"]*)[\x00-\x20]*j[\x00-\x20]*a[\x00-\x20]*v[\x00-\x20]*a[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iu', '$1=$2nojavascript...', $data); $data = preg_replace('#([a-z]*)[\x00-\x20]*=([\'"]*)[\x00-\x20]*v[\x00-\x20]*b[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iu', '$1=$2novbscript...', $data); $data = preg_replace('#([a-z]*)[\x00-\x20]*=([\'"]*)[\x00-\x20]*-moz-binding[\x00-\x20]*:#u', '$1=$2nomozbinding...', $data); // Only works in IE: $data = preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?expression[\x00-\x20]*\([^>]*+>#i', '$1>', $data); $data = preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?behaviour[\x00-\x20]*\([^>]*+>#i', '$1>', $data); $data = preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:*[^>]*+>#iu', '$1>', $data); // Remove namespaced elements (we do not need them) $data = preg_replace('#]*+>#i', '', $data); do { // Remove really unwanted tags $old_data = $data; $data = preg_replace('#]*+>#i', '', $data); } while ($old_data !== $data); } return $data; } /** * Truncar un texto a una determinada longitud. * * @param string $text la cadena a truncar * @param int $limit la longitud máxima de la cadena * @param string $ellipsis * @return string con el texto truncado * * @link http://www.pjgalbraith.com/truncating-text-html-with-php/ */ public static function truncate($text, $limit, $ellipsis = '...') { if (strlen($text) > $limit) { $text = trim(mb_substr($text, 0, $limit)) . $ellipsis; } return $text; } /** * Convertir un color RGB a HEX * From: http://bavotasan.com/2011/convert-hex-color-to-rgb-using-php/ * * @param array $rgb con color en RGB * @return string */ public static function rgb2hex($rgb) { $hex = "#"; $hex .= str_pad(dechex($rgb[0]), 2, "0", STR_PAD_LEFT); $hex .= str_pad(dechex($rgb[1]), 2, "0", STR_PAD_LEFT); $hex .= str_pad(dechex($rgb[2]), 2, "0", STR_PAD_LEFT); return $hex; // returns the hex value including the number sign (#) } /** * Devolver una cadena con el tag HTML strong. * * @param string $text con la cadena de texto * @return string */ public static function strongText($text) { return ('' . $text . ''); } /** * Devolver un link HTML. * * @param string $text con la cadena de texto * @param string $link con el destino del enlace * @param string $title con el título del enlace * @param string $attribs con atributos del enlace * @return string */ public static function anchorText($text, $link = '', $title = '', $attribs = '') { $alink = (!empty($link)) ? $link : $text; $atitle = (!empty($title)) ? $title : ''; $anchor = '' . $text . ''; return $anchor; } }