self::$_ticker + 1)) { self::$_ticker = time(); return true; } return false; } /** * clean_utf8 * * Removes characters that aren't valid in XML (which is a subset of valid * UTF-8, but close enough for our purposes.) * See http://www.w3.org/TR/2006/REC-xml-20060816/#charsets */ public static function clean_utf8($string) { if ($string) { $clean = preg_replace('/[^\x{9}\x{a}\x{d}\x{20}-\x{d7ff}\x{e000}-\x{fffd}\x{10000}-\x{10ffff}]|[\x{7f}-\x{84}\x{86}-\x{9f}\x{fdd0}-\x{fddf}\x{1fffe}-\x{1ffff}\x{2fffe}-\x{2ffff}\x{3fffe}-\x{3ffff}\x{4fffe}-\x{4ffff}\x{5fffe}-\x{5ffff}\x{6fffe}-\x{6ffff}\x{7fffe}-\x{7ffff}\x{8fffe}-\x{8ffff}\x{9fffe}-\x{9ffff}\x{afffe}-\x{affff}\x{bfffe}-\x{bffff}\x{cfffe}-\x{cffff}\x{dfffe}-\x{dffff}\x{efffe}-\x{effff}\x{ffffe}-\x{fffff}\x{10fffe}-\x{10ffff}]/u', '', $string); if ($clean) { return $clean; } debug_event('UI', 'Charset cleanup failed, something might break', 1); } } /** * flip_class * * First initialised with an array of two class names. Subsequent calls * reverse the array then return the first element. */ public static function flip_class($classes = null) { if (is_array($classes)) { self::$_classes = $classes; } else { self::$_classes = array_reverse(self::$_classes); } return self::$_classes[0]; } /** * format_bytes * * Turns a size in bytes into the best human-readable value */ public static function format_bytes($value, $precision = 2) { $pass = 0; while (strlen(floor($value)) > 3) { $value /= 1024; $pass++; } switch ($pass) { case 1: $unit = 'kB'; break; case 2: $unit = 'MB'; break; case 3: $unit = 'GB'; break; case 4: $unit = 'TB'; break; case 5: $unit = 'PB'; break; default: $unit = 'B'; break; } return round($value, $precision) . ' ' . $unit; } /** * unformat_bytes * * Parses a human-readable size */ public static function unformat_bytes($value) { if (preg_match('/^([0-9]+) *([[:alpha:]]+)$/', $value, $matches)) { $value = $matches[1]; $unit = strtolower(substr($matches[2], 0, 1)); } else { return $value; } switch($unit) { case 'p': $value *= 1024; case 't': $value *= 1024; case 'g': $value *= 1024; case 'm': $value *= 1024; case 'k': $value *= 1024; } return $value; } /** * get_icon * * Returns an tag for the specified icon */ public static function get_icon($name, $title = null, $id = null) { if (is_array($name)) { $hover_name = $name[1]; $name = $name[0]; } $title = $title ?: T_(ucfirst($name)); $icon_url = self::_find_icon($name); if (isset($hover_name)) { $hover_url = self::_find_icon($hover_text); } $tag = ' $max) { $text = iconv_substr($text, 0, $max - 3, $charset); $text .= iconv('ISO-8859-1', $charset, '...'); } } else { if (strlen($text) > $max) { $text = substr($text, 0, $max - 3) . '...'; } } return $text; } /** * update_text * * Convenience function that, if the output is going to a browser, * blarfs JS to do a fancy update. Otherwise it just outputs the text. */ public static function update_text($field, $value) { if (defined('CLI')) { echo $value . "\n"; return; } echo '\n"; ob_flush(); flush(); } }