/** * Turn a string or array into valid, standards-compliant (x)HTML * * Uses configuraton options in tidy.conf - which should minimally have show-body-only set to yes * * @param mixed $text The data to be tidied up * @return mixed $result Tidied data */ function tidy($text) { static $tidy_funcs; static $tidy_conf; if (!isset($tidy_conf)) { $tidy_conf = SETTINGS_INC . 'tidy.conf'; } if (is_array($text)) { $result = array(); foreach (array_keys($text) as $key) { $result[$key] = tidy($text[$key]); } return $result; } // determine what tidy libraries are available if (empty($tidy_funcs)) { $tidy_funcs = get_extension_funcs('tidy'); } $tidy_1_lib_available = !empty($tidy_funcs) && array_search('tidy_setopt', $tidy_funcs) !== false; $tidy_2_lib_available = !empty($tidy_funcs) && array_search('tidy_setopt', $tidy_funcs) === false; $tidy_command_line_available = TIDY_EXE ? file_exists(TIDY_EXE) : false; $text = protect_string_from_tidy($text); $text = '<html><body>' . $text . '</body></html>'; if ($tidy_2_lib_available) { $tidy = new tidy(); $tidy->parseString($text, $tidy_conf, 'utf8'); $tidy->cleanRepair(); $result = $tidy; } elseif ($tidy_1_lib_available) { tidy_load_config($tidy_conf); tidy_set_encoding('utf8'); tidy_parse_string($text); tidy_clean_repair(); $result = tidy_get_output(); } elseif ($tidy_command_line_available) { $arg = escapeshellarg($text); // escape the bad stuff in the text $cmd = 'echo ' . $arg . ' | ' . TIDY_EXE . ' -q -config ' . $tidy_conf . ' 2> /dev/null'; // the actual command - pipes the input to tidy which diverts its output to the random file $result = shell_exec($cmd); // execute the command } else { trigger_error('tidy does not appear to be available within php or at the command line - no tidying is taking place.'); $result = $text; } return trim($result); }
/** * Use the HTML tidy PECL extension to use the tidy library in-process, * saving the overhead of spawning a new process. Currently written to * the PHP 4.3.x version of the extension, may not work on PHP 5. * * 'pear install tidy' should be able to compile the extension module. */ private static function internal($text) { global $wgTidyConf; $fname = 'Parser::internalTidy'; wfProfileIn($fname); tidy_load_config($wgTidyConf); tidy_set_encoding('utf8'); tidy_parse_string($text); tidy_clean_repair(); if (tidy_get_status() == 2) { // 2 is magic number for fatal error // http://www.php.net/manual/en/function.tidy-get-status.php $cleansource = null; } else { $cleansource = tidy_get_output(); } wfProfileOut($fname); return $cleansource; }