/** * */ public function testUseFilterExt() { $this->assertTrue(Inspekt::useFilterExt(true)); $this->assertTrue(Inspekt::useFilterExt()); $this->assertFalse(Inspekt::useFilterExt(false)); $this->assertFalse(Inspekt::useFilterExt()); }
/** * returns value with tags stripped and the chars '"&<> and all ascii chars under 32 encoded as html entities * * This will utilize the PHP Filter extension if available * * @param mixed $value * @return @mixed * * @tag filter * */ public static function noTagsOrSpecial($value) { if (Inspekt::isArrayOrArrayObject($value)) { return Inspekt::_walkArray($value, 'noTagsOrSpecial'); } else { if (Inspekt::useFilterExt()) { $newval = filter_var($value, FILTER_SANITIZE_STRING); $newval = filter_var($newval, FILTER_SANITIZE_SPECIAL_CHARS); return $newval; } else { $newval = strip_tags($value); $newval = htmlspecialchars($newval, ENT_QUOTES, 'UTF-8'); // for sake of simplicity and safety we assume UTF-8 /* convert low ascii chars to entities */ $newval = str_split($newval); for ($i = 0; $i < count($newval); $i++) { $ascii_code = ord($newval[$i]); if ($ascii_code < 32) { $newval[$i] = "&#{$ascii_code};"; } } $newval = implode($newval); return $newval; } } }
<?php /** * Demonstration of: * - use of static filter methods on arrays * - creating a cage on an arbitrary array * - accessing a deep key in a multidim array with the "Array Query" approach */ require_once '../Inspekt.php'; Inspekt::useFilterExt(false); echo "<p>Filtering an arbitrary array using Inspekt::noTags()</p>\n\n"; $d = array(); $d['input'] = '<img id="475">yes</img>'; $d['lowascii'] = ' '; $d[] = array('foo', 'bar<br />', 'yes<P>', 1776); $d['x']['woot'] = array('booyah' => 'meet at the bar at 7:30 pm', 'ultimate' => '<strong>hi there!</strong>'); $d['lemon'][][][][][][][][][][][][][][] = 'far'; echo "<pre>BEFORE:"; echo var_dump($d); echo "</pre>\n"; $newd = Inspekt::noTags($d); echo "<pre>noTags:"; echo var_dump($newd); echo "</pre>\n"; $newd = Inspekt::noTagsOrSpecial($d); echo "<pre>noTagsOrSpecial:"; echo var_dump($newd); echo "</pre>\n"; $newd = Inspekt::getDigits($d); echo "<pre>getDigits:"; echo var_dump($newd);