2 // $Id: spam_util.php,v 1.3 2011/01/24 14:46:45 henoheno Exp $
3 // Copyright (C) 2006-2009 PukiWiki Developers Team
4 // License: GPL v2 or (at your option) any later version
6 // Functions for Concept-work of spam-uri metrics
9 // ---------------------
12 // (PHP 4 >= 4.2.0): var_export(): mail-reporting and dump related
13 if (! function_exists('var_export')) {
14 function var_export() {
15 return 'var_export() is not found on this server' . "\n";
19 // (PHP 4 >= 4.2.0): preg_grep() enables invert option
20 function preg_grep_invert($pattern = '//', $input = array())
23 if (! isset($invert)) $invert = defined('PREG_GREP_INVERT');
26 return preg_grep($pattern, $input, PREG_GREP_INVERT);
28 $result = preg_grep($pattern, $input);
30 return array_diff($input, preg_grep($pattern, $input));
38 // ---------------------
42 if (! function_exists('htmlsc')) {
43 // Interface with PukiWiki
44 if (! defined('CONTENT_CHARSET')) define('CONTENT_CHARSET', 'ISO-8859-1');
46 // Sugar with default settings
47 function htmlsc($string = '', $flags = ENT_QUOTES, $charset = CONTENT_CHARSET)
49 return htmlspecialchars($string, $flags, $charset); // htmlsc()
53 // Very roughly, shrink the lines of var_export()
54 // NOTE: If the same data exists, it must be corrupted.
55 function var_export_shrink($expression, $return = FALSE, $ignore_numeric_keys = FALSE)
57 $result = var_export($expression, TRUE);
59 $result = preg_replace(
60 // Remove a newline and spaces
61 '# => \n *array \(#', ' => array (',
65 if ($ignore_numeric_keys) {
66 $result =preg_replace(
67 // Remove numeric keys
68 '#^( *)[0-9]+ => #m', '$1',
81 // Data structure: Create an array they _refer_only_one_ value
82 function one_value_array($num = 0, $value = NULL)
84 $num = max(0, intval($num));
87 for ($i = 0; $i < $num; $i++) {
94 // Reverse $string with specified delimiter
95 function delimiter_reverse($string = 'foo.bar.example.com', $from_delim = '.', $to_delim = NULL)
97 $to_null = ($to_delim === NULL);
99 if (! is_string($from_delim) || (! $to_null && ! is_string($to_delim))) {
102 if (is_array($string)) {
104 $count = count($string);
105 $from = one_value_array($count, $from_delim);
107 // Note: array_map() vanishes all keys
108 return array_map('delimiter_reverse', $string, $from);
110 $to = one_value_array($count, $to_delim);
111 // Note: array_map() vanishes all keys
112 return array_map('delimiter_reverse', $string, $from, $to);
115 if (! is_string($string)) {
119 // Returns com.example.bar.foo
120 if ($to_null) $to_delim = & $from_delim;
121 return implode($to_delim, array_reverse(explode($from_delim, $string)));
125 function ksort_by_domain(& $array)
128 foreach(array_keys($array) as $key) {
129 $reversed = delimiter_reverse($key);
130 if ($reversed !== FALSE) {
131 $sort[$reversed] = $key;
134 ksort($sort, SORT_STRING);
137 foreach($sort as $key) {
138 $result[$key] = & $array[$key];
144 // Roughly strings(1) using PCRE
145 // This function is useful to:
146 // * Reduce the size of data, from removing unprintable binary data
147 // * Detect _bare_strings_ from binary data
149 // http://www.freebsd.org/cgi/man.cgi?query=strings (Man-page of GNU strings)
150 // http://www.pcre.org/pcre.txt
151 // Note: mb_ereg_replace() is one of mbstring extension's functions
152 // and need to init its encoding.
153 function strings($binary = '', $min_len = 4, $ignore_space = FALSE, $multibyte = FALSE)
156 $binary = (is_array($binary) || $binary === TRUE) ? '' : strval($binary);
158 $regex = $ignore_space ?
159 '[^[:graph:] \t\n]+' : // Remove "\0" etc, and readable spaces
160 '[^[:graph:][:space:]]+'; // Preserve readable spaces if possible
162 $binary = $multibyte ?
163 mb_ereg_replace($regex, "\n", $binary) :
164 preg_replace('/' . $regex . '/s', "\n", $binary);
167 $binary = preg_replace(
182 // The last character seems "\n" or not
183 $br = (! empty($binary) && $binary[strlen($binary) - 1] == "\n") ? "\n" : '';
185 $min_len = min(1024, intval($min_len));
186 $regex = '/^.{' . $min_len . ',}/S';
187 $binary = implode("\n", preg_grep($regex, explode("\n", $binary))) . $br;
194 // ---------------------
197 // Count leaves (A leaf = value that is not an array, or an empty array)
198 function array_count_leaves($array = array(), $count_empty = FALSE)
200 if (! is_array($array) || (empty($array) && $count_empty)) return 1;
204 foreach ($array as $part) {
205 $count += array_count_leaves($part, $count_empty);
211 // Similar to PHP array_merge_leaves(), except strictly preserving keys as string
212 function array_merge_leaves($array1, $array2, $sort_keys = TRUE)
215 $is_array1 = is_array($array1);
216 $is_array2 = is_array($array2);
223 } else if ($is_array2) {
226 return $array2; // Not array ($array1 is overwritten)
229 $keys_all = array_merge(array_keys($array1), array_keys($array2));
230 if ($sort_keys) sort($keys_all, SORT_STRING);
233 foreach($keys_all as $key) {
234 $isset1 = isset($array1[$key]);
235 $isset2 = isset($array2[$key]);
236 if ($isset1 && $isset2) {
238 $result[$key] = array_merge_leaves($array1[$key], $array2[$key], $sort_keys);
239 } else if ($isset1) {
240 $result[$key] = & $array1[$key];
242 $result[$key] = & $array2[$key];
248 // An array-leaves to a flat array
249 function array_flat_leaves($array, $unique = TRUE)
251 if (! is_array($array)) return $array;
254 foreach(array_keys($array) as $key) {
255 if (is_array($array[$key])) {
257 foreach(array_flat_leaves($array[$key]) as $_value) {
261 $tmp[] = & $array[$key];
265 return $unique ? array_values(array_unique($tmp)) : $tmp;
268 // $array['something'] => $array['wanted']
269 function array_rename_keys(& $array, $keys = array('from' => 'to'), $force = FALSE, $default = '')
271 if (! is_array($array) || ! is_array($keys)) return FALSE;
273 // Nondestructive test
275 foreach(array_keys($keys) as $from) {
276 if (! isset($array[$from])) {
282 foreach($keys as $from => $to) {
283 if ($from === $to) continue;
284 if (! $force || isset($array[$from])) {
285 $array[$to] = & $array[$from];
286 unset($array[$from]);
288 $array[$to] = $default;
295 // Remove redundant values from array()
296 function array_unique_recursive($array = array())
298 if (! is_array($array)) return $array;
301 foreach($array as $key => $value){
302 if (is_array($value)) {
303 $array[$key] = array_unique_recursive($value);
305 if (isset($tmp[$value])) {