root/branches/release-41/php/lib/MTUtil.php @ 2651

Revision 2651, 55.9 kB (checked in by takayama, 17 months ago)

Fixed BugId:80372
* Shows warning messages if GD-php was not available.

  • Property svn:keywords set to Author Date Id Revision
Line 
1<?php
2# Movable Type (r) Open Source (C) 2001-2008 Six Apart, Ltd.
3# This program is distributed under the terms of the
4# GNU General Public License, version 2.
5#
6# $Id$
7
8function start_end_ts($ts) {
9    if ($ts) {
10        if (strlen($ts) == 4) {
11            $ts_start = $ts . '0101';
12            $ts_end = $ts . '1231';
13        } elseif (strlen($ts) == 6) {
14            $ts_start = $ts . '01';
15            $ts_end = $ts . sprintf("%02d", days_in(substr($ts, 4, 2), substr($ts, 0, 4)));
16        } else {
17            $ts_start = $ts;
18            $ts_end = $ts;
19        }
20    }
21    return array($ts_start . '000000', $ts_end . '235959');
22}
23
24function start_end_month($ts) {
25    $y = substr($ts, 0, 4);
26    $mo = substr($ts, 4, 2);
27    $start = sprintf("%04d%02d01000000", $y, $mo);
28    $end = sprintf("%04d%02d%02d235959", $y, $mo, days_in($mo, $y));
29    return array($start, $end);
30}
31
32function days_in($m, $y) {
33    return date('t', mktime(0, 0, 0, $m, 1, $y));
34}
35
36function start_end_day($ts) {
37    $day = substr($ts, 0, 8);
38    return array($day . "000000", $day . "235959");
39}
40
41function start_end_year($ts) {
42    $year = substr($ts, 0, 4);
43    return array($year . "0101000000", $year . "1231235959");
44}
45
46function start_end_week($ts) {
47    $y = substr($ts, 0, 4);
48    $mo = substr($ts, 4, 2);
49    $d = substr($ts, 6, 2);
50    $h = substr($ts, 8, 2);
51    $s = substr($ts, 10, 2);
52    $wday = wday_from_ts($y, $mo, $d);
53    list($sd, $sm, $sy) = array($d - $wday, $mo, $y);
54    if ($sd < 1) {
55        $sm--;
56        if ($sm < 1) {
57            $sm = 12; $sy--;
58        }
59        $sd += days_in($sm, $sy);
60    }
61    $start = sprintf("%04d%02d%02d%s", $sy, $sm, $sd, "000000");
62    list($ed, $em, $ey) = array($d + 6 - $wday, $mo, $y);
63    if ($ed > days_in($em, $ey)) {
64        $ed -= days_in($em, $ey);
65        $em++;
66        if ($em > 12) {
67            $em = 1; $ey++;
68        }
69    }
70    $end = sprintf("%04d%02d%02d%s", $ey, $em, $ed, "235959");
71    return array($start, $end);
72}
73
74global $In_Year;
75$In_Year = array(
76    array( 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 ),
77    array( 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 ),
78);
79function week2ymd($y, $week) {
80    $jan_one_dow_m1 = (ymd2rd($y, 1, 1) + 6) % 7;
81
82    if ($jan_one_dow_m1 < 4) $week--;
83    $day_of_year = $week * 7 - $jan_one_dow_m1;
84    $leap_year = is_leap_year($y);
85    if ($day_of_year < 1) {
86        $y--;
87        $day_of_year = ($leap_year ? 366 : 365) + $day_of_year;
88    }
89    if ($leap_year) {
90        $ref = array(0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335);
91    } else {
92        $ref = array(0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334);
93    }
94    $m = 0;
95    for ($i = count($ref); $i > 0; $i--) {
96        if ($day_of_year > $ref[$i-1]) {
97            $m = $i;
98            break;
99        }
100    }
101    return array($y, $m, $day_of_year - $ref[$m-1]);
102}
103
104function is_leap_year($y) {
105    return (!($y % 4) && ($y % 100)) || !($y % 400) ? true : false;
106}
107
108function ymd2rd($y,$m,$d) {
109    # make month in range 3..14 (treat Jan & Feb as months 13..14 of
110    # prev year)
111    if ( $m <= 2 ) {
112        $adj = (int)(( 14 - $m ) / 12);
113        $y -= $adj;
114        $m += 12 * $adj;
115    }
116    elseif ( $m > 14 )
117    {
118        $adj = (int)(( $m - 3 ) / 12);
119        $y += $adj;
120        $m -= 12 * $adj;
121    }
122
123    # make year positive (oh, for a use integer 'sane_div'!)
124    if ( $y < 0 )
125    {
126        $adj = (int)(( 399 - $y ) / 400);
127        $d -= 146097 * $adj; 
128        $y += 400 * $adj;
129    }
130
131    # add: day of month, days of previous 0-11 month period that began
132    # w/March, days of previous 0-399 year period that began w/March
133    # of a 400-multiple year), days of any 400-year periods before
134    # that, and 306 days to adjust from Mar 1, year 0-relative to Jan
135    # 1, year 1-relative (whew)
136    $d += (int)(( $m * 367 - 1094 ) / 12) + (int)((($y % 100) * 1461) / 4) +
137          ( (int)($y / 100) * 36524 + (int)($y / 400) ) - 306;
138    return $d;
139}
140
141function wday_from_ts($y, $m, $d) {
142    global $In_Year;
143    $leap = $y % 4 == 0 && ($y % 100 != 0 || $y % 400 == 0) ? 1 : 0;
144    $y--;
145
146    ## Copied from Date::Calc.
147    $days = $y * 365;
148    $days += $y >>= 2;
149    $days -= intval($y /= 25);
150    $days += $y >> 2;
151    $days += $In_Year[$leap][$m-1] + $d;
152    return $days % 7;
153}
154
155function yday_from_ts($y, $m, $d) {
156    global $In_Year;
157    $leap = $y % 4 == 0 && ($y % 100 != 0 || $y % 400 == 0) ? 1 : 0;
158    return $In_Year[$leap][$m-1] + $d;
159}
160
161function substr_wref($str, $start, $length) {
162    if (preg_match_all('/(&[^;]*;|.)/', $str, $character_entities)) {
163        return implode('', array_slice($character_entities[0], $start, $length));
164    } else {
165        return '';
166    }
167}
168
169function format_ts($format, $ts, $blog, $lang = null) {
170    global $Languages;
171    if (!isset($lang) || empty($lang)) { 
172        global $mt;
173        $lang = ($blog && $blog['blog_language'] ? $blog['blog_language'] : 
174                     $mt->config('DefaultLanguage'));
175    }
176    if ($lang == 'jp') {
177        $lang = 'ja';
178    }
179    $lang = strtolower(substr($lang, 0, 2));
180    if (!isset($format) || empty($format)) {
181        if (count($Languages[$lang]) >= 4)
182            $format = $Languages[$lang][3];
183        $format or $format = "%B %e, %Y %l:%M %p";
184    }
185    global $_format_ts_cache;
186    if (!isset($_format_ts_cache)) {
187        $_format_ts_cache = array();
188    }   
189    if (isset($_format_ts_cache[$ts.$lang])) {
190        $f = $_format_ts_cache[$ts.$lang];
191    } else {
192        $L = $Languages[$lang];
193        $tsa = array(substr($ts, 0, 4), substr($ts, 4, 2), substr($ts, 6, 2),
194                     substr($ts, 8, 2), substr($ts, 10, 2), substr($ts, 12, 2));
195        list($f['Y'], $f['m'], $f['d'], $f['H'], $f['M'], $f['S']) = $tsa;
196        $f['w'] = wday_from_ts($tsa[0],$tsa[1],$tsa[2]);
197        $f['j'] = yday_from_ts($tsa[0],$tsa[1],$tsa[2]);
198        $f['y'] = substr($f['Y'], 2);
199        $f['b'] = substr_wref($L[1][$f['m']-1], 0, 3);
200        $f['B'] = $L[1][$f['m']-1];
201        if ($lang == 'ja') {
202            $f['a'] = substr($L[0][$f['w']], 0, 8);
203        } else {
204            $f['a'] = substr_wref($L[0][$f['w']], 0, 3);
205        }
206        $f['A'] = $L[0][$f['w']];
207        $f['e'] = $f['d'];
208        $f['e'] = preg_replace('!^0!', ' ', $f['e']);
209        $f['I'] = $f['H'];
210        if ($f['I'] > 12) {
211            $f['I'] -= 12;
212            $f['p'] = $L[2][1];
213        } elseif ($f['I'] == 0) {
214            $f['I'] = 12;
215            $f['p'] = $L[2][0];
216        } elseif ($f['I'] == 12) {
217            $f['p'] = $L[2][1];
218        } else {
219            $f['p'] = $L[2][0];
220        }
221        $f['I'] = sprintf("%02d", $f['I']);
222        $f['k'] = $f['H'];
223        $f['k'] = preg_replace('!^0!', ' ', $f['k']);
224        $f['l'] = $f['I'];
225        $f['l'] = preg_replace('!^0!', ' ', $f['l']);
226        $f['j'] = sprintf("%03d", $f['j']);
227        $f['Z'] = '';
228        $_format_ts_cache[$ts . $lang] = $f;
229    }
230    $date_format = null;
231    if (count($Languages[$lang]) >= 5)
232        $date_format = $Languages[$lang][4];
233    $date_format or $date_format = "%B %e, %Y";
234    $time_format = null;
235    if (count($Languages[$lang]) >= 6)
236        $time_format = $Languages[$lang][5];
237    $time_format or $time_format = "%l:%M %p";
238    $format = preg_replace('!%x!', $date_format, $format);
239    $format = preg_replace('!%X!', $time_format, $format);
240    ## This is a dreadful hack. I can't think of a good format specifier
241    ## for "%B %Y" (which is used for monthly archives, for example) so
242    ## I'll just hardcode this, for Japanese dates.
243    if ($lang == 'ja') {
244        if (count($Languages[$lang]) >= 8) {
245            $format = preg_replace('!%B %Y!', $Languages[$lang][6], $format);
246            $format = preg_replace('!%B %E,? %Y!i', $Languages[$lang][4], $format);
247            $format = preg_replace('!%B %E!', $Languages[$lang][7], $format);
248        }
249    }
250    elseif ( $lang == 'it' ) {
251        ## Hack for the Italian dates
252        ## In Italian, the date always come before the month.
253        $format = preg_replace('!%b %e!', '%e %b', $format);
254    }
255    if (isset($format)) {
256        $format = preg_replace('!%(\w)!e', '\$f[\'\1\']', $format);
257    }
258    return $format;
259}
260
261function dirify($s, $sep = '_') {
262    global $mt;
263    $charset = $mt->config('PublishCharset');
264    $charset or $charset = 'utf-8';
265    if (preg_match('/utf-?8/i', $charset)) {
266        return utf8_dirify($s, $sep);
267    } else {
268        return iso_dirify($s, $sep);
269    }
270}
271
272function utf8_dirify($s, $sep = '_') {
273    if ($sep == '1') $sep = '_';
274    $s = xliterate_utf8($s);  ## convert high-ASCII chars to 7bit.
275    $s = strtolower($s);                   ## lower-case.
276    $s = strip_tags($s);          ## remove HTML tags.
277    $s = preg_replace('!&[^;\s]+;!', '', $s); ## remove HTML entities.
278    $s = preg_replace('![^\w\s]!', '', $s);   ## remove non-word/space chars.
279    $s = preg_replace('/\s+/',$sep,$s);         ## change space chars to underscores.
280    return($s);
281}
282
283global $Utf8_ASCII;
284$Utf8_ASCII = array(
285    "\xc3\x80" => 'A',    # A`
286    "\xc3\xa0" => 'a',    # a`
287    "\xc3\x81" => 'A',    # A'
288    "\xc3\xa1" => 'a',    # a'
289    "\xc3\x82" => 'A',    # A^
290    "\xc3\xa2" => 'a',    # a^
291    "\xc4\x82" => 'A',    # latin capital letter a with breve
292    "\xc4\x83" => 'a',    # latin small letter a with breve
293    "\xc3\x86" => 'AE',   # latin capital letter AE
294    "\xc3\xa6" => 'ae',   # latin small letter ae
295    "\xc3\x85" => 'A',    # latin capital letter a with ring above
296    "\xc3\xa5" => 'a',    # latin small letter a with ring above
297    "\xc4\x80" => 'A',    # latin capital letter a with macron
298    "\xc4\x81" => 'a',    # latin small letter a with macron
299    "\xc4\x84" => 'A',    # latin capital letter a with ogonek
300    "\xc4\x85" => 'a',    # latin small letter a with ogonek
301    "\xc3\x84" => 'A',    # A:
302    "\xc3\xa4" => 'a',    # a:
303    "\xc3\x83" => 'A',    # A~
304    "\xc3\xa3" => 'a',    # a~
305    "\xc3\x88" => 'E',    # E`
306    "\xc3\xa8" => 'e',    # e`
307    "\xc3\x89" => 'E',    # E'
308    "\xc3\xa9" => 'e',    # e'
309    "\xc3\x8a" => 'E',    # E^
310    "\xc3\xaa" => 'e',    # e^
311    "\xc3\x8b" => 'E',    # E:
312    "\xc3\xab" => 'e',    # e:
313    "\xc4\x92" => 'E',    # latin capital letter e with macron
314    "\xc4\x93" => 'e',    # latin small letter e with macron
315    "\xc4\x98" => 'E',    # latin capital letter e with ogonek
316    "\xc4\x99" => 'e',    # latin small letter e with ogonek
317    "\xc4\x9a" => 'E',    # latin capital letter e with caron
318    "\xc4\x9b" => 'e',    # latin small letter e with caron
319    "\xc4\x94" => 'E',    # latin capital letter e with breve
320    "\xc4\x95" => 'e',    # latin small letter e with breve
321    "\xc4\x96" => 'E',    # latin capital letter e with dot above
322    "\xc4\x97" => 'e',    # latin small letter e with dot above
323    "\xc3\x8c" => 'I',    # I`
324    "\xc3\xac" => 'i',    # i`
325    "\xc3\x8d" => 'I',    # I'
326    "\xc3\xad" => 'i',    # i'
327    "\xc3\x8e" => 'I',    # I^
328    "\xc3\xae" => 'i',    # i^
329    "\xc3\x8f" => 'I',    # I:
330    "\xc3\xaf" => 'i',    # i:
331    "\xc4\xaa" => 'I',    # latin capital letter i with macron
332    "\xc4\xab" => 'i',    # latin small letter i with macron
333    "\xc4\xa8" => 'I',    # latin capital letter i with tilde
334    "\xc4\xa9" => 'i',    # latin small letter i with tilde
335    "\xc4\xac" => 'I',    # latin capital letter i with breve
336    "\xc4\xad" => 'i',    # latin small letter i with breve
337    "\xc4\xae" => 'I',    # latin capital letter i with ogonek
338    "\xc4\xaf" => 'i',    # latin small letter i with ogonek
339    "\xc4\xb0" => 'I',    # latin capital letter with dot above
340    "\xc4\xb1" => 'i',    # latin small letter dotless i
341    "\xc4\xb2" => 'IJ',   # latin capital ligature ij
342    "\xc4\xb3" => 'ij',   # latin small ligature ij
343    "\xc4\xb4" => 'J',    # latin capital letter j with circumflex
344    "\xc4\xb5" => 'j',    # latin small letter j with circumflex
345    "\xc4\xb6" => 'K',    # latin capital letter k with cedilla
346    "\xc4\xb7" => 'k',    # latin small letter k with cedilla
347    "\xc4\xb8" => 'k',    # latin small letter kra
348    "\xc5\x81" => 'L',    # latin capital letter l with stroke
349    "\xc5\x82" => 'l',    # latin small letter l with stroke
350    "\xc4\xbd" => 'L',    # latin capital letter l with caron
351    "\xc4\xbe" => 'l',    # latin small letter l with caron
352    "\xc4\xb9" => 'L',    # latin capital letter l with acute
353    "\xc4\xba" => 'l',    # latin small letter l with acute
354    "\xc4\xbb" => 'L',    # latin capital letter l with cedilla
355    "\xc4\xbc" => 'l',    # latin small letter l with cedilla
356    "\xc4\xbf" => 'l',    # latin capital letter l with middle dot
357    "\xc5\x80" => 'l',    # latin small letter l with middle dot
358    "\xc3\x92" => 'O',    # O`
359    "\xc3\xb2" => 'o',    # o`
360    "\xc3\x93" => 'O',    # O'
361    "\xc3\xb3" => 'o',    # o'
362    "\xc3\x94" => 'O',    # O^
363    "\xc3\xb4" => 'o',    # o^
364    "\xc3\x96" => 'O',    # O:
365    "\xc3\xb6" => 'o',    # o:
366    "\xc3\x95" => 'O',    # O~
367    "\xc3\xb5" => 'o',    # o~
368    "\xc3\x98" => 'O',    # O/
369    "\xc3\xb8" => 'o',    # o/
370    "\xc5\x8c" => 'O',    # latin capital letter o with macron
371    "\xc5\x8d" => 'o',    # latin small letter o with macron
372    "\xc5\x90" => 'O',    # latin capital letter o with double acute
373    "\xc5\x91" => 'o',    # latin small letter o with double acute
374    "\xc5\x8e" => 'O',    # latin capital letter o with breve
375    "\xc5\x8f" => 'o',    # latin small letter o with breve
376    "\xc5\x92" => 'OE',   # latin capital ligature oe
377    "\xc5\x93" => 'oe',   # latin small ligature oe
378    "\xc5\x94" => 'R',    # latin capital letter r with acute
379    "\xc5\x95" => 'r',    # latin small letter r with acute
380    "\xc5\x98" => 'R',    # latin capital letter r with caron
381    "\xc5\x99" => 'r',    # latin small letter r with caron
382    "\xc5\x96" => 'R',    # latin capital letter r with cedilla
383    "\xc5\x97" => 'r',    # latin small letter r with cedilla
384    "\xc3\x99" => 'U',    # U`
385    "\xc3\xb9" => 'u',    # u`
386    "\xc3\x9a" => 'U',    # U'
387    "\xc3\xba" => 'u',    # u'
388    "\xc3\x9b" => 'U',    # U^
389    "\xc3\xbb" => 'u',    # u^
390    "\xc3\x9c" => 'U',    # U:
391    "\xc3\xbc" => 'u',    # u:
392    "\xc5\xaa" => 'U',    # latin capital letter u with macron
393    "\xc5\xab" => 'u',    # latin small letter u with macron
394    "\xc5\xae" => 'U',    # latin capital letter u with ring above
395    "\xc5\xaf" => 'u',    # latin small letter u with ring above
396    "\xc5\xb0" => 'U',    # latin capital letter u with double acute
397    "\xc5\xb1" => 'u',    # latin small letter u with double acute
398    "\xc5\xac" => 'U',    # latin capital letter u with breve
399    "\xc5\xad" => 'u',    # latin small letter u with breve
400    "\xc5\xa8" => 'U',    # latin capital letter u with tilde
401    "\xc5\xa9" => 'u',    # latin small letter u with tilde
402    "\xc5\xb2" => 'U',    # latin capital letter u with ogonek
403    "\xc5\xb3" => 'u',    # latin small letter u with ogonek
404    "\xc3\x87" => 'C',    # ,C
405    "\xc3\xa7" => 'c',    # ,c
406    "\xc4\x86" => 'C',    # latin capital letter c with acute
407    "\xc4\x87" => 'c',    # latin small letter c with acute
408    "\xc4\x8c" => 'C',    # latin capital letter c with caron
409    "\xc4\x8d" => 'c',    # latin small letter c with caron
410    "\xc4\x88" => 'C',    # latin capital letter c with circumflex
411    "\xc4\x89" => 'c',    # latin small letter c with circumflex
412    "\xc4\x8a" => 'C',    # latin capital letter c with dot above
413    "\xc4\x8b" => 'c',    # latin small letter c with dot above
414    "\xc4\x8e" => 'D',    # latin capital letter d with caron
415    "\xc4\x8f" => 'd',    # latin small letter d with caron
416    "\xc4\x90" => 'D',    # latin capital letter d with stroke
417    "\xc4\x91" => 'd',    # latin small letter d with stroke
418    "\xc3\x91" => 'N',    # N~
419    "\xc3\xb1" => 'n',    # n~
420    "\xc5\x83" => 'N',    # latin capital letter n with acute
421    "\xc5\x84" => 'n',    # latin small letter n with acute
422    "\xc5\x87" => 'N',    # latin capital letter n with caron
423    "\xc5\x88" => 'n',    # latin small letter n with caron
424    "\xc5\x85" => 'N',    # latin capital letter n with cedilla
425    "\xc5\x86" => 'n',    # latin small letter n with cedilla
426    "\xc5\x89" => 'n',    # latin small letter n preceded by apostrophe
427    "\xc5\x8a" => 'N',    # latin capital letter eng
428    "\xc5\x8b" => 'n',    # latin small letter eng
429    "\xc3\x9f" => 'ss',   # double-s
430    "\xc5\x9a" => 'S',    # latin capital letter s with acute
431    "\xc5\x9b" => 's',    # latin small letter s with acute
432    "\xc5\xa0" => 'S',    # latin capital letter s with caron
433    "\xc5\xa1" => 's',    # latin small letter s with caron
434    "\xc5\x9e" => 'S',    # latin capital letter s with cedilla
435    "\xc5\x9f" => 's',    # latin small letter s with cedilla
436    "\xc5\x9c" => 'S',    # latin capital letter s with circumflex
437    "\xc5\x9d" => 's',    # latin small letter s with circumflex
438    "\xc8\x98" => 'S',    # latin capital letter s with comma below
439    "\xc8\x99" => 's',    # latin small letter s with comma below
440    "\xc5\xa4" => 'T',    # latin capital letter t with caron
441    "\xc5\xa5" => 't',    # latin small letter t with caron
442    "\xc5\xa2" => 'T',    # latin capital letter t with cedilla
443    "\xc5\xa3" => 't',    # latin small letter t with cedilla
444    "\xc5\xa6" => 'T',    # latin capital letter t with stroke
445    "\xc5\xa7" => 't',    # latin small letter t with stroke
446    "\xc8\x9a" => 'T',    # latin capital letter t with comma below
447    "\xc8\x9b" => 't',    # latin small letter t with comma below
448    "\xc6\x92" => 'f',    # latin small letter f with hook
449    "\xc4\x9c" => 'G',    # latin capital letter g with circumflex
450    "\xc4\x9d" => 'g',    # latin small letter g with circumflex
451    "\xc4\x9e" => 'G',    # latin capital letter g with breve
452    "\xc4\x9f" => 'g',    # latin small letter g with breve
453    "\xc4\xa0" => 'G',    # latin capital letter g with dot above
454    "\xc4\xa1" => 'g',    # latin small letter g with dot above
455    "\xc4\xa2" => 'G',    # latin capital letter g with cedilla
456    "\xc4\xa3" => 'g',    # latin small letter g with cedilla
457    "\xc4\xa4" => 'H',    # latin capital letter h with circumflex
458    "\xc4\xa5" => 'h',    # latin small letter h with circumflex
459    "\xc4\xa6" => 'H',    # latin capital letter h with stroke
460    "\xc4\xa7" => 'h',    # latin small letter h with stroke
461    "\xc5\xb4" => 'W',    # latin capital letter w with circumflex
462    "\xc5\xb5" => 'w',    # latin small letter w with circumflex
463    "\xc3\x9d" => 'Y',    # latin capital letter y with acute
464    "\xc3\xbd" => 'y',    # latin small letter y with acute
465    "\xc5\xb8" => 'Y',    # latin capital letter y with diaeresis
466    "\xc3\xbf" => 'y',    # latin small letter y with diaeresis
467    "\xc5\xb6" => 'Y',    # latin capital letter y with circumflex
468    "\xc5\xb7" => 'y',    # latin small letter y with circumflex
469    "\xc5\xbd" => 'Z',    # latin capital letter z with caron
470    "\xc5\xbe" => 'z',    # latin small letter z with caron
471    "\xc5\xbb" => 'Z',    # latin capital letter z with dot above
472    "\xc5\xbc" => 'z',    # latin small letter z with dot above
473    "\xc5\xb9" => 'Z',    # latin capital letter z with acute
474    "\xc5\xba" => 'z',    # latin small letter z with acute
475);
476function xliterate_utf8($s) {
477    global $Utf8_ASCII;
478    return strtr($s, $Utf8_ASCII);
479}
480
481function iso_dirify($s, $sep = '_') {
482    if ($sep == '1') $sep = '_';
483    $s = convert_high_ascii($s);  ## convert high-ASCII chars to 7bit.
484    $s = strtolower($s);                   ## lower-case.
485    $s = strip_tags($s);          ## remove HTML tags.
486    $s = preg_replace('!&[^;\s]+;!', '', $s); ## remove HTML entities.
487    $s = preg_replace('![^\w\s]!', '', $s);   ## remove non-word/space chars.
488    $s = preg_replace('/\s+/',$sep,$s);         ## change space chars to underscores.
489    return($s);
490}
491
492global $Latin1_ASCII;
493$Latin1_ASCII = array(
494    "\xc0" => 'A',    # A`
495    "\xe0" => 'a',    # a`
496    "\xc1" => 'A',    # A'
497    "\xe1" => 'a',    # a'
498    "\xc2" => 'A',    # A^
499    "\xe2" => 'a',    # a^
500    "\xc4" => 'A',    # A:
501    "\xe4" => 'a',    # a:
502    "\xc5" => 'A',    # Aring
503    "\xe5" => 'a',    # aring
504    "\xc6" => 'AE',   # AE
505    "\xe6" => 'ae',   # ae
506    "\xc3" => 'A',    # A~
507    "\xe3" => 'a',    # a~
508    "\xc8" => 'E',    # E`
509    "\xe8" => 'e',    # e`
510    "\xc9" => 'E',    # E'
511    "\xe9" => 'e',    # e'
512    "\xca" => 'E',    # E^
513    "\xea" => 'e',    # e^
514    "\xcb" => 'E',    # E:
515    "\xeb" => 'e',    # e:
516    "\xcc" => 'I',    # I`
517    "\xec" => 'i',    # i`
518    "\xcd" => 'I',    # I'
519    "\xed" => 'i',    # i'
520    "\xce" => 'I',    # I^
521    "\xee" => 'i',    # i^
522    "\xcf" => 'I',    # I:
523    "\xef" => 'i',    # i:
524    "\xd2" => 'O',    # O`
525    "\xf2" => 'o',    # o`
526    "\xd3" => 'O',    # O'
527    "\xf3" => 'o',    # o'
528    "\xd4" => 'O',    # O^
529    "\xf4" => 'o',    # o^
530    "\xd6" => 'O',    # O:
531    "\xf6" => 'o',    # o:
532    "\xd5" => 'O',    # O~
533    "\xf5" => 'o',    # o~
534    "\xd8" => 'O',    # O/
535    "\xf8" => 'o',    # o/
536    "\xd9" => 'U',    # U`
537    "\xf9" => 'u',    # u`
538    "\xda" => 'U',    # U'
539    "\xfa" => 'u',    # u'
540    "\xdb" => 'U',    # U^
541    "\xfb" => 'u',    # u^
542    "\xdc" => 'U',    # U:
543    "\xfc" => 'u',    # u:
544    "\xc7" => 'C',    # ,C
545    "\xe7" => 'c',    # ,c
546    "\xd1" => 'N',    # N~
547    "\xf1" => 'n',    # n~
548    "\xdd" => 'Y',    # Yacute
549    "\xfd" => 'y',    # yacute
550    "\xdf" => 'ss',   # szlig
551    "\xff" => 'y'     # yuml
552);
553function convert_high_ascii($s) {
554    global $mt;
555    $lang = $mt->config('DefaultLanguage');
556    if ($lang == 'ja') {
557        $s = mb_convert_encoding($s, 'UTF-8');
558        return $s;
559    }
560    global $Latin1_ASCII;
561    return strtr($s, $Latin1_ASCII);
562}
563
564global $Languages;
565$Languages = array(
566    'en' => array(
567            array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'),
568            array('January','February','March','April','May','June',
569                  'July','August','September','October','November','December'),
570            array('AM','PM'),
571          ),
572
573    'fr' => array(
574            array('dimanche','lundi','mardi','mercredi','jeudi','vendredi','samedi' ),
575            array('janvier', "f&#xe9;vrier", 'mars', 'avril', 'mai', 'juin',
576               'juillet', "ao&#xfb;t", 'septembre', 'octobre', 'novembre',
577               "d&#xe9;cembre"),
578            array('AM','PM'),
579          ),
580
581    'es' => array(
582            array('Domingo', 'Lunes', 'Martes', "Mi&#xe9;rcoles", 'Jueves',
583               'Viernes', "S&#xe1;bado"),
584            array('Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio','Agosto',
585                  'Septiembre','Octubre','Noviembre','Diciembre'),
586            array('AM','PM'),
587          ),
588
589    'pt' => array(
590            array('domingo', 'segunda-feira', "ter&#xe7;a-feira", 'quarta-feira',
591               'quinta-feira', 'sexta-feira', "s&#xe1;bado"),
592            array('janeiro', 'fevereiro', "mar&#xe7;o", 'abril', 'maio', 'junho',
593               'julho', 'agosto', 'setembro', 'outubro', 'novembro',
594               'dezembro' ),
595            array('AM','PM'),
596          ),
597
598    'nl' => array(
599            array('zondag','maandag','dinsdag','woensdag','donderdag','vrijdag',
600                  'zaterdag'),
601            array('januari','februari','maart','april','mei','juni','juli','augustus',
602                  'september','oktober','november','december'),
603            array('am','pm'),
604             "%d %B %Y %H:%M",
605             "%d %B %Y"
606          ),
607
608    'dk' => array(
609            array("s&#xf8;ndag", 'mandag', 'tirsdag', 'onsdag', 'torsdag',
610               'fredag', "l&#xf8;rdag"),
611            array('januar','februar','marts','april','maj','juni','juli','august',
612                  'september','oktober','november','december'),
613            array('am','pm'),
614            "%d.%m.%Y %H:%M",
615            "%d.%m.%Y",
616            "%H:%M",
617          ),
618
619    'se' => array(
620            array("s&#xf6;ndag", "m&#xe5;ndag", 'tisdag', 'onsdag', 'torsdag',
621               'fredag', "l&#xf6;rdag"),
622            array('januari','februari','mars','april','maj','juni','juli','augusti',
623                  'september','oktober','november','december'),
624            array('FM','EM'),
625          ),
626
627    'no' => array(
628            array("S&#xf8;ndag", "Mandag", 'Tirsdag', 'Onsdag', 'Torsdag',
629               'Fredag', "L&#xf8;rdag"),
630            array('Januar','Februar','Mars','April','Mai','Juni','Juli','August',
631                  'September','Oktober','November','Desember'),
632            array('FM','EM'),
633          ),
634
635    'de' => array(
636            array('Sonntag','Montag','Dienstag','Mittwoch','Donnerstag','Freitag',
637                  'Samstag'),
638            array('Januar', 'Februar', "M&#xe4;rz", 'April', 'Mai', 'Juni',
639               'Juli', 'August', 'September', 'Oktober', 'November',
640               'Dezember'),
641            array('FM','EM'),
642            "%d.%m.%y %H:%M",
643            "%d.%m.%y",
644            "%H:%M",
645          ),
646
647    'it' => array(
648            array('Domenica', "Luned&#xec;", "Marted&#xec;", "Mercoled&#xec;",
649               "Gioved&#xec;", "Venerd&#xec;", 'Sabato'),
650            array('Gennaio','Febbraio','Marzo','Aprile','Maggio','Giugno','Luglio',
651                  'Agosto','Settembre','Ottobre','Novembre','Dicembre'),
652            array('AM','PM'),
653            "%d.%m.%y %H:%M",
654            "%d.%m.%y",
655            "%H:%M",
656          ),
657
658    'pl' => array(
659            array('niedziela', "poniedzia&#322;ek", 'wtorek', "&#347;roda",
660               'czwartek', "pi&#261;tek", 'sobota'),
661            array('stycznia', 'lutego', 'marca', 'kwietnia', 'maja', 'czerwca',
662               'lipca', 'sierpnia', "wrze&#347;nia", "pa&#378;dziernika",
663               'listopada', 'grudnia'),
664            array('AM','PM'),
665            "%e %B %Y %k:%M",
666            "%e %B %Y",
667            "%k:%M",
668          ),
669           
670    'fi' => array(
671            array('sunnuntai','maanantai','tiistai','keskiviikko','torstai','perjantai',
672                  'lauantai'),
673            array('tammikuu', 'helmikuu', 'maaliskuu', 'huhtikuu', 'toukokuu',
674               "kes&#xe4;kuu", "hein&#xe4;kuu", 'elokuu', 'syyskuu', 'lokakuu',
675               'marraskuu', 'joulukuu'),
676            array('AM','PM'),
677            "%d.%m.%y %H:%M",
678          ),
679           
680    'is' => array(
681            array('Sunnudagur', "M&#xe1;nudagur", "&#xde;ri&#xf0;judagur",
682               "Mi&#xf0;vikudagur", 'Fimmtudagur', "F&#xf6;studagur",
683               'Laugardagur'),
684            array("jan&#xfa;ar", "febr&#xfa;ar", 'mars', "apr&#xed;l", "ma&#xed;",
685               "j&#xfa;n&#xed;", "j&#xfa;l&#xed;", "&#xe1;g&#xfa;st", 'september',             
686               "okt&#xf3;ber", "n&#xf3;vember", 'desember'),
687            array('FH','EH'),
688            "%d.%m.%y %H:%M",
689          ),
690           
691    'si' => array(
692            array('nedelja', 'ponedeljek', 'torek', 'sreda', "&#xe3;etrtek",
693               'petek', 'sobota'),
694            array('januar','februar','marec','april','maj','junij','julij','avgust',
695                  'september','oktober','november','december'),
696            array('AM','PM'),
697            "%d.%m.%y %H:%M",
698          ),
699           
700    'cz' => array(
701            array('Ned&#283;le', 'Pond&#283;l&#237;', '&#218;ter&#253;',
702               'St&#345;eda', '&#268;tvrtek', 'P&#225;tek', 'Sobota'),
703            array('Leden', '&#218;nor', 'B&#345;ezen', 'Duben', 'Kv&#283;ten',
704               '&#268;erven', '&#268;ervenec', 'Srpen', 'Z&#225;&#345;&#237;',
705               '&#216;&#237;jen', 'Listopad', 'Prosinec'),
706            array('AM','PM'),
707            "%e. %B %Y %k:%M",
708            "%e. %B %Y",
709            "%k:%M",
710          ),
711           
712    'sk' => array(
713            array('nede&#318;a', 'pondelok', 'utorok', 'streda',
714               '&#353;tvrtok', 'piatok', 'sobota'),
715            array('janu&#225;r', 'febru&#225;r', 'marec', 'apr&#237;l',
716               'm&#225;j', 'j&#250;n', 'j&#250;l', 'august', 'september',
717               'okt&#243;ber', 'november', 'december'),
718            array('AM','PM'),
719            "%e. %B %Y %k:%M",
720            "%e. %B %Y",
721            "%k:%M",
722          ),
723
724    'jp' => array(
725            array('&#26085;&#26332;&#26085;', '&#26376;&#26332;&#26085;',
726              '&#28779;&#26332;&#26085;', '&#27700;&#26332;&#26085;',
727              '&#26408;&#26332;&#26085;', '&#37329;&#26332;&#26085;',
728              '&#22303;&#26332;&#26085;'),
729            array('1','2','3','4','5','6','7','8','9','10','11','12'),
730            array('AM','PM'),
731            "%Y&#24180;%b&#26376;%e&#26085; %H:%M",
732            "%Y&#24180;%b&#26376;%e&#26085;",
733            "%H:%M",
734            "%Y&#24180;%b&#26376;",
735            "%b&#26376;%e&#26085;",
736          ),
737
738    'ja' => array(
739            array('&#26085;&#26332;&#26085;', '&#26376;&#26332;&#26085;',
740              '&#28779;&#26332;&#26085;', '&#27700;&#26332;&#26085;',
741              '&#26408;&#26332;&#26085;', '&#37329;&#26332;&#26085;',
742              '&#22303;&#26332;&#26085;'),
743            array('1','2','3','4','5','6','7','8','9','10','11','12'),
744            array('AM','PM'),
745            "%Y&#24180;%b&#26376;%e&#26085; %H:%M",
746            "%Y&#24180;%b&#26376;%e&#26085;",
747            "%H:%M",
748            "%Y&#24180;%b&#26376;",
749            "%b&#26376;%e&#26085;",
750          ),
751
752    'et' => array(
753            array('ip&uuml;hap&auml;ev','esmasp&auml;ev','teisip&auml;ev',
754                  'kolmap&auml;ev','neljap&auml;ev','reede','laup&auml;ev'),
755            array('jaanuar', 'veebruar', 'm&auml;rts', 'aprill', 'mai',
756               'juuni', 'juuli', 'august', 'september', 'oktoober',
757              'november', 'detsember'),
758            array('AM','PM'),
759            "%m.%d.%y %H:%M",
760            "%e. %B %Y",
761            "%H:%M",
762          ),
763);
764
765global $_encode_xml_Map;
766$_encode_xml_Map = array('&' => '&amp;', '"' => '&quot;',
767                         '<' => '&lt;', '>' => '&gt;',
768                         '\'' => '&apos;');
769
770function encode_xml($str, $nocdata = 0) {
771    global $mt, $_encode_xml_Map;
772    $cfg_nocdata = $mt->config('NoCDATA');
773    $nocdata or (isset($cfg_nocdata) and $nocdata = $cfg_nocdata);
774    if ((!$nocdata) && (preg_match('/
775        <[^>]+>  ## HTML markup
776        |        ## or
777        &(?:(?!(\#([0-9]+)|\#x([0-9a-fA-F]+))).*?);
778                 ## something that looks like an HTML entity.
779        /x', $str))) {
780        ## If ]]> exists in the string, encode the > to &gt;.
781        $str = preg_replace('/]]>/', ']]&gt;', $str);
782        $str = '<![CDATA[' . $str . ']]>';
783    } else {
784        $str = strtr($str, $_encode_xml_Map);
785        $str = preg_replace('/&amp;((\#([0-9]+)|\#x([0-9a-fA-F]+)).*?);/', "&$1;", $str);
786    }
787    return $str;
788}
789
790function decode_xml($str) {
791    if (preg_match('/<!\[CDATA\[(.*?)]]>/', $str)) {
792        $str = preg_replace('/<!\[CDATA\[(.*?)]]>/', '\1', $str);
793        ## Decode encoded ]]&gt;
794        $str = preg_replace('/]]&(gt|#62);/', ']]>', $str);
795    } else {
796        global $_encode_xml_Map;
797        $str = strtr($str, array_flip($_encode_xml_Map));
798    }
799    return $str;
800}
801
802function encode_js($str) {
803    if (!isset($str)) return '';
804    $str = preg_replace('!\\\\!', '\\\\', $str);
805    $str = preg_replace('!>!', '\\>', $str);
806    $str = preg_replace('!<!', '\\<', $str);
807    $str = preg_replace('!(s)(cript)!i', '$1\\\\$2', $str);
808    $str = preg_replace('!</!', '<\\/', $str); # </ is supposed to be the end of Javascript (</script in most UA)
809    $str = preg_replace('!\'!', '\\\'', $str);
810    $str = preg_replace('!"!', '\\"', $str);
811    $str = preg_replace('!\n!', '\\n', $str);
812    $str = preg_replace('!\f!', '\\f', $str);
813    $str = preg_replace('!\r!', '\\r', $str);
814    $str = preg_replace('!\t!', '\\t', $str);
815    return $str;
816}
817
818function gmtime($ts = null) {
819    if (!isset($ts)) {
820        $ts = time();
821    }
822    $offset = date('Z', $ts);
823    $ts -= $offset;
824    $tsa = localtime($ts);
825    $tsa[8] = 0;
826    return $tsa;
827}
828
829function is_hash($array) {
830    if ( is_array($array) ) {
831        if ( array_keys($array) === range(0, count($array) - 1) ) {
832            // 0,1,2,3... must be an array
833            return false;
834        }
835        return true;
836    }
837    return false;
838}
839
840function offset_time_list($ts, $blog = null, $dir = null) {
841    return gmtime(offset_time($ts, $blog, $dir));
842}
843
844function strip_hyphen($s) {
845    return preg_replace('/-+/', '-', $s);
846}
847
848function first_n_words($text, $n) {
849    $text = strip_tags($text);
850    $words = preg_split('/\s+/', $text);
851    $max = count($words) > $n ? $n : count($words);
852    return join(' ', array_slice($words, 0, $max));
853}
854
855function html_text_transform($str = '') {
856    $paras = preg_split('/\r?\n\r?\n/', $str);
857    if ($str == '') {
858        return '';
859    }
860    foreach ($paras as $k => $p) {
861        if (!preg_match('/^<\/?(?:h1|h2|h3|h4|h5|h6|table|ol|dl|ul|menu|dir|p|pre|center|form|select|fieldset|blockquote|address|div|hr)/', $p)) {
862            $p = preg_replace('/\r?\n/', "<br />\n", $p);
863            $p = "<p>$p</p>";
864            $paras[$k] = $p;
865        }
866    }
867    return implode("\n\n", $paras);
868}
869
870function encode_html($str, $quote_style = ENT_COMPAT) {
871    if (!isset($str)) return '';
872    $trans_table = get_html_translation_table(HTML_SPECIALCHARS, $quote_style);
873    if( $trans_table["'"] != '&#039;' ) { # some versions of PHP match single quotes to &#39;
874        $trans_table["'"] = '&#039;';
875    }
876    return (strtr($str, $trans_table));
877}
878
879function decode_html($str, $quote_style = ENT_COMPAT) {
880    if (!isset($str)) return '';
881    $trans_table = get_html_translation_table(HTML_SPECIALCHARS, $quote_style);
882    if( $trans_table["'"] != '&#039;' ) { # some versions of PHP match single quotes to &#39;
883        $trans_table["'"] = '&#039;';
884    }
885    return (strtr($str, array_flip($trans_table)));
886}
887
888function get_category_context(&$ctx, $class = 'category') {
889    # Get our hands on the category for the current context
890    # Either in MTCategories, a Category Archive Template
891    # Or the category for the current entry
892    $cat = $ctx->stash('category') or
893           $ctx->stash('archive_category');
894    if (!isset($cat)) {
895        # No category found so far, test the entry
896        if ($ctx->stash('entry')) {
897            $entry = $ctx->stash('entry');
898            if (empty($entry['placement_category_id']))
899                return null;
900            if ($class == 'folder')
901                $cat = $ctx->mt->db->fetch_folder($entry['placement_category_id']);
902            else
903                $cat = $ctx->mt->db->fetch_category($entry['placement_category_id']);
904 
905            # Return empty string if entry has no category
906            # as the tag has been used in the correct context
907            # but there is no category to work with
908            if (!isset($cat)) {
909                return null;
910            }
911        } else {
912            $tag = $ctx->this_tag();
913            return $ctx->error("$tag must be used in a category context");
914        }
915    }
916    return $cat;
917}
918
919function munge_comment($text, $blog) {
920    if (!$blog['blog_allow_comment_html']) {
921        $text = strip_tags($text);
922    }
923    if ($blog['blog_autolink_urls']) {
924        $text = preg_replace('!(^|\s|>)(https?://[^\s<]+)!s', '$1<a href="$2">$2</a>', $text);
925    }
926    return $text;
927}
928
929function length_text($text) {
930    if (!extension_loaded('mbstring')) {
931        $len = strlen($text);
932    } else {
933        $len = mb_strlen($text);
934    }
935    return $len;
936}
937
938function substr_text($text, $startpos, $length) {
939    if (!extension_loaded('mbstring')) {
940        $text = substr($text, $startpos, $length);
941    } else {
942        $text = mb_substr($text, $startpos, $length);
943    }
944    return $text;
945}
946
947function first_n_text($text, $n) {
948    if (!isset($lang) || empty($lang)) { 
949        global $mt;
950        $lang = ($blog && $blog['blog_language'] ? $blog['blog_language'] : 
951                     $mt->config('DefaultLanguage'));
952    }
953    if ($lang == 'jp') {
954        $lang = 'ja';
955    }
956
957    if ($lang == 'ja') {
958        $text = strip_tags($text);
959        $text = preg_replace('/\r?\n/', " ", $text);
960        return substr_text($text, 0, $n);
961    }else{
962        return first_n_words($text, $n);
963    }
964}
965
966function tag_split_delim($delim, $str) {
967    $delim = quotemeta($delim);
968    $tags = array();
969    $str = trim($str);
970    while (strlen($str) && (preg_match("/^(((['\"])(.*?)\3[^$delim]*?|.*?)($delim\s*|$))/s", $str, $match))) {
971        $str = substr($str, strlen($match[1]));
972        $tag = (isset($match[4]) && $match[4] != '') ? $match[4] : $match[2];
973        $tag = trim($tag);
974        $tag = preg_replace('/\s+/', ' ', $tag);
975        $n8d_tag = tag_normalize($tag);
976        if ($n8d_tag != '')
977            if ($tag != '') $tags[] = $tag;
978    }
979    return $tags;
980}
981
982function tag_split($str) {
983    return tag_split_delim(',', $str);
984}
985
986function catarray_path_length_sort($a, $b) {
987        $al = strlen($a['category_label_path']);
988        $bl = strlen($b['category_label_path']);
989        return $al == $bl ? 0 : $al < $bl ? 1 : -1;
990}
991
992# sorts by length of category label, from longest to shortest
993function catarray_length_sort($a, $b) {
994        $al = strlen($a['category_label']);
995        $bl = strlen($b['category_label']);
996        return $al == $bl ? 0 : $al < $bl ? 1 : -1;
997}
998
999function create_expr_exception($m) {
1000    if ($m[2])
1001        return '(0)';
1002    else
1003        return $m[1];
1004}
1005
1006function create_cat_expr_function($expr, &$cats, $param) {
1007    global $mt;
1008    $cats_used = array();
1009    $orig_expr = $expr;
1010
1011    $include_children = $param['children'] ? 1 : 0;
1012    $cats_used = array();
1013
1014    if (preg_match('/\//', $expr)) {
1015        foreach ($cats as $id => $cat) {
1016            $catp = category_label_path($cat);
1017            $cats[$id]['category_label_path'] = $catp;
1018        }
1019        $cols = array('category_label_path', 'category_label');
1020    } else {
1021        $cols = array('category_label');
1022    }
1023    foreach ($cols as $col) {
1024        if ($col == 'category_label_path') {
1025            usort($cats, 'catarray_path_length_sort');
1026        } else {
1027            usort($cats, 'catarray_length_sort');
1028        }
1029        $cats_replaced = array();
1030        foreach ($cats as $cat) {
1031            $catl = $cat[$col];
1032            $catid = $cat['category_id'];
1033            $catre = preg_quote($catl, "/");
1034            if (!preg_match("/(?:(?<![#\d])\[$catre\]|$catre)|(?:#$catid\b)/", $expr))
1035                continue;
1036            if ($include_children) {
1037                $kids = array($cat);
1038                $child_cats = array();
1039                while ($c = array_shift($kids)) {
1040                    $child_cats[$c['category_id']] = $c;
1041                    $children = $mt->db->fetch_categories(array('category_id' => $c['category_id'], 'children' => 1, 'show_empty' => 1, 'class' => $c['category_class']));
1042                    if ($children) {
1043                        foreach ($children as $child) {
1044                            $kids[] = $child;
1045                        }
1046                    }
1047                }
1048                $repl = '';
1049                foreach ($child_cats as $ccid => $cc) {
1050                    $repl .= '||#' . $ccid;
1051                }
1052                if (strlen($repl)) $repl = substr($repl, 2);
1053                $repl = '(' . $repl . ')';
1054            } else {
1055                $repl = "(#$catid)";
1056            }
1057            if (isset($cats_replaced[$catl])) {
1058                $last_catid = $cats_replaced[$catl];
1059                $expr = preg_replace("/(#$last_catid\b)/", '($1 || #' . $catid . ')', $expr);
1060            } else {
1061                    $expr = preg_replace("/(?:(?<!#)(?:\[$catre\]|$catre))|#$catid\b/", $repl,
1062                    $expr);
1063                $cats_replaced[$catl] = $catid;
1064            }
1065            if ($include_children) {
1066                foreach ($child_cats as $ccid => $cc)
1067                    $cats_used[$ccid] = $cc;
1068            } else {
1069                $cats_used[$catid] = $cat;
1070            }
1071        }
1072    }
1073
1074    $expr = preg_replace('/\bAND\b/i', '&&', $expr);
1075    $expr = preg_replace('/\bOR\b/i', '||', $expr);
1076    $expr = preg_replace('/\bNOT\b/i', '!', $expr);
1077    $expr = preg_replace_callback('/( |#\d+|&&|\|\||!|\(|\))|([^#0-9&|!()]+)/', 'create_expr_exception', $expr);
1078
1079    # strip out all the 'ok' stuff. if anything is left, we have
1080    # some invalid data in our expression:
1081    $test_expr = preg_replace('/!|&&|\|\||\(0\)|\(|\)|\s|#\d+/', '', $expr);
1082    if ($test_expr != '') {
1083        echo "Invalid category filter: $orig_expr";
1084        return;
1085    }
1086        if (!preg_match('/!/', $expr))
1087            $cats = array_values($cats_used);
1088
1089    $expr = preg_replace('/#(\d+)/', "array_key_exists('\\1', \$pm)", $expr);
1090    $expr = '$pm = array_key_exists($e["entry_id"], $c["p"]) ? $c["p"][$e["entry_id"]] : array(); return (' . $expr . ');';
1091    $fn = create_function('&$e,&$c', $expr);
1092    if ($fn === FALSE) {
1093        echo "Invalid category filter: $orig_expr";
1094        return;
1095    }
1096    return $fn;
1097}
1098
1099function category_label_path($cat) {
1100    global $mt;
1101    $mtdb =& $mt->db;
1102    if (isset($cat['__label_path']))
1103        return $cat['__label_path'];
1104    $result = preg_match('/\//', $cat['category_label']) ? '[' . $cat['category_label'] . ']' : $cat['category_label'];
1105    while ($cat) {
1106        $cat = $cat['category_parent'] ? $mtdb->fetch_category($cat['category_parent']) : null;
1107        if ($cat)
1108            $result = (preg_match('/\//', $cat['category_label']) ? '[' . $cat['category_label'] . ']' : $cat['category_label']) . '/' . $result;
1109    }
1110    # caching this information may be problematic IF
1111    # parent category labels are changed.
1112    $cat['__label_path'] = $result;
1113    return $result;
1114}
1115
1116function cat_path_to_category($path, $blogs = null, $class = 'category') {
1117    global $mt;
1118    if (!$blogs)
1119        $blogs = array('include_blogs' => $mt->blog_id);
1120    if (!is_array($blogs))
1121        $blogs = array($blogs);
1122    $mtdb =& $mt->db;
1123    if (preg_match_all('/(\[[^]]+?\]|[^]\/]+)/', $path, $matches)) {
1124        # split on slashes, fields quoted by []
1125        $cat_path = $matches[1];
1126        for ($i = 0; $i < count($cat_path); $i++) {
1127            $cat_path[$i] = preg_replace('/^\[(.*)\]$/', '\1', $cat_path[$i]);       # remove any []
1128        }
1129        $last_cat_ids = array(0);
1130        foreach ($cat_path as $label) {
1131            $cats = $mtdb->fetch_categories(array_merge($blogs, array('label' => $label, 'parent' => $last_cat_ids, 'show_empty' => 1, 'class' => $class)));
1132            if (!$cats)
1133                break;
1134            $last_cat_ids = array();
1135            foreach ($cats as $cat)
1136                $last_cat_ids[] = $cat['category_id'];
1137        }
1138    }
1139    if ($cats)
1140        return $cats;
1141    if (!$cats && $path) {
1142        $cats = $mtdb->fetch_categories(array_merge($blogs, array('label' => $path, 'show_empty' => 1, 'class' => $class)));
1143        if ($cats)
1144            return $cats;
1145    }
1146    return null;
1147}
1148
1149# sorts by length of tag name, from longest to shortest
1150function tagarray_name_sort($a, $b) {
1151    return strcmp($a['tag_name'], $b['tag_name']);
1152}
1153
1154function tagarray_length_sort($a, $b) {
1155        $al = strlen($a['tag_name']);
1156        $bl = strlen($b['tag_name']);
1157        return $al == $bl ? 0 : $al < $bl ? 1 : -1;
1158}
1159
1160function create_tag_expr_function($expr, &$tags, $datasource = 'entry') {
1161    $tags_used = array();
1162    $orig_expr = $expr;
1163   
1164    # Sort in descending order by length
1165        usort($tags, 'tagarray_length_sort');
1166
1167    # Modify the tag argument, replacing the tag name with '#TagID'
1168    # Create a ID-based hash of the tags that are used in the arg
1169    foreach ($tags as $tag) {
1170        $tagn = $tag['tag_name'];
1171        $tagid = $tag['tag_id'];
1172        $oldexpr = $expr;
1173            $expr = preg_replace("!(?:\Q[$tagn]\E|\Q$tagn\E)!", "#$tagid",
1174                $expr);
1175            if ($oldexpr != $expr)
1176                $tags_used[$tagid] = $tag;
1177        }
1178
1179    # Replace logical constructs with their PHP equivalents
1180    $expr = preg_replace('/\bAND\b/i', '&&', $expr);
1181    $expr = preg_replace('/\bOR\b/i', '||', $expr);
1182    $expr = preg_replace('/\bNOT\b/i', '!', $expr);
1183
1184    # The following is no more readable in PHP than it is in Perl
1185    $expr = preg_replace_callback('/( |#\d+|&&|\|\||!|\(|\))|([^#0-9&|!()]+)/', 'create_expr_exception', $expr);
1186
1187    # Syntax check on 'tag' argument
1188    # Strip out all the valid stuff. if anything is left, we have
1189    # some invalid data in our expression
1190    $test_expr = preg_replace('/!|&&|\|\||\(0\)|\(|\)|\s|#\d+/', '', $expr);
1191    if ($test_expr != '') {
1192        echo "Invalid tag filter: $orig_expr";
1193        return;
1194    }
1195
1196    # Populate array (passed in by reference) of used tags
1197    $tags = array_values($tags_used);
1198
1199    # Replace '#TagID' with a hash lookup function.
1200    # Function confirms/denies use of tag on entry (by IDs)
1201    $column_name = $datasource . '_id';
1202    $expr = preg_replace('/#(\d+)/', "array_key_exists('\\1', \$tm)", $expr);
1203
1204    # Create a PHP-blessed function of that code and return it
1205    # if all is well.  This function will be used later to
1206    # test for existence of specified tags in entries.
1207    $expr = '$tm = array_key_exists($e["'.$datasource.'_id"], $c["t"]) ? $c["t"][$e["'.$datasource.'_id"]] : array(); return ' . $expr .
1208        ';';
1209    $fn = create_function('&$e,&$c', $expr);
1210    if ($fn === FALSE) {
1211        echo "Invalid tag filter: $orig_expr";
1212        return;
1213    }
1214    return $fn;
1215}
1216
1217function tag_normalize($str) {
1218    # FIXME: character set issues here...
1219    $private = preg_match('/^@/', $str) ? 1 : 0;
1220    $str = preg_replace('/[@!`\\<>\*&#\/~\?\'"\.\,=\(\)\${}\[\];:\ \+\-\r\n]+/', '', $str);
1221    $str = strtolower($str);
1222    if ($private) $str = '@' . $str;
1223    return $str;
1224}
1225
1226function static_path($host) {
1227    global $mt;
1228    $path = $mt->config('StaticWebPath');
1229    if (!$path) {
1230        $path = $mt->config('CGIPath');
1231        if (substr($path, 0, 1) == '/') {  # relative
1232            if (!preg_match('!/$!', $host))
1233                $host .= '/';
1234            if (preg_match('!^(https?://[^/:]+)(:\d+)?/!', $host, $matches)) {
1235                $path = $matches[1] . $path;
1236            }
1237        }
1238        if (substr($path, strlen($path) - 1, 1) != '/')
1239            $path .= '/';
1240        $path .= 'mt-static/';
1241    } elseif (substr($path, 0, 1) == '/') {
1242        if (!preg_match('!/$!', $host))
1243            $host .= '/';
1244        if (preg_match('!^(https?://[^/:]+)(:\d+)?/!', $host, $matches)) {
1245            $path = $matches[1] . $path;
1246        }       
1247    }
1248    if (substr($path, strlen($path) - 1, 1) != '/')
1249        $path .= '/';
1250
1251    return $path;
1252}
1253
1254function static_file_path() {
1255    global $mt;
1256    $path = $mt->config('StaticFilePath');
1257    if (!$path) {
1258        $path = dirname(dirname(dirname(__FILE__)));
1259        $path .= DIRECTORY_SEPARATOR . 'mt-static' . DIRECTORY_SEPARATOR;
1260    }
1261    if (substr($path, strlen($path) - 1, 1) != DIRECTORY_SEPARATOR)
1262        $path .= DIRECTORY_SEPARATOR;
1263
1264    return $path;
1265}
1266
1267function asset_path($path, $blog) {
1268    $site_path = $blog['blog_site_path'];
1269    $site_path = preg_replace('/\/$/', '', $site_path);
1270    $path = preg_replace('/^%r/', $site_path, $path);
1271
1272    $static_file_path = static_file_path();
1273    $static_file_path = preg_replace('/\/$/', '', $static_file_path);
1274    $path = preg_replace('/^%s/', $static_file_path, $path);
1275
1276    $archive_path = $blog['blog_archive_path'];
1277    if ($archive_path) {
1278        $archive_path = preg_replace('/\/$/', '', $archive_path);
1279        $path = preg_replace('/^%a/', $archive_path, $path);
1280    }
1281
1282    return $path;
1283}
1284
1285function userpic_url($asset, $blog, $author) {
1286    global $mt;
1287    $format = $mt->translate('userpic-[_1]-%wx%h%x', array($author['author_id']));
1288    $max_dim = $mt->config('UserpicThumbnailSize');
1289
1290    # generate thumbnail
1291    $src_file = asset_path($asset['asset_file_path'], $blog);
1292
1293    $cache_path = $mt->config('AssetCacheDir');
1294    $image_path = $cache_path . DIRECTORY_SEPARATOR . 'userpics';
1295    $static_file_path = static_file_path().'support';
1296
1297    require_once('thumbnail_lib.php');
1298    $thumb = new Thumbnail($src_file);
1299    $thumb_w = $max_dim;
1300    $thumb_h = $max_dim;
1301    $dest;
1302    $thumb_name = $static_file_path.DIRECTORY_SEPARATOR.$image_path.DIRECTORY_SEPARATOR.$format;
1303    if (!$thumb->get_thumbnail($dest, $thumb_w, $thumb_h, $scale, $thumb_name, 'png')) {
1304        return '';
1305    }
1306    $basename = basename($dest);
1307
1308    $static_path = $mt->config('StaticWebPath');
1309    $static_path = preg_replace('/\/$/', '', $static_path);
1310    $static_path .= '/support';
1311    $url = sprintf("%s/%s/%s", $static_path, $image_path, $basename);
1312    return $url;
1313}
1314
1315# for compatibility...
1316function make_thumbnail_file($src, $dest, $width, $height, $scale = 0, $dest_type = 'auto') {
1317    require_once('thumbnail_lib.php');
1318    $thumb = new Thumbnail($src);
1319
1320    $thumb_w = $width;
1321    $thumb_h = $height;
1322    $thumb->get_thumbnail($dest, $thumb_w, $thumb_h, $scale, null, $dest_type);
1323
1324    return array($thumb_w, $thumb_h);
1325}
1326
1327function get_thumbnail_file($asset, $blog, $width = 0, $height = 0, $scale = 0, $format = '%f-thumb-%wx%h%x') {
1328    # Get parameter
1329    $site_path = $blog['blog_site_path'];
1330    $site_path = preg_replace('/\/$/', '', $site_path);
1331    $filename = asset_path($asset['asset_file_path'], $blog);
1332
1333    # Retrieve thumbnail
1334    global $mt;
1335    $path_parts = pathinfo($filename);
1336    $cache_path = $mt->config('AssetCacheDir');
1337
1338    $ts = preg_replace('![^0-9]!', '', $asset['asset_created_on']);
1339    $date_stamp = format_ts('%Y/%m', $ts, $blog);
1340    $cache_dir = $site_path . DIRECTORY_SEPARATOR . $cache_path . DIRECTORY_SEPARATOR . $date_stamp . DIRECTORY_SEPARATOR;
1341    $thumb_name = $cache_dir . $format;
1342 
1343    # generate thumbnail
1344    require_once('thumbnail_lib.php');
1345    $thumb = new Thumbnail($filename);
1346    $thumb_w = $width;
1347    $thumb_h = $height;
1348    $dest;
1349    if (!$thumb->get_thumbnail($dest, $thumb_w, $thumb_h, $scale, $thumb_name)) {
1350        return '';
1351    }
1352
1353    # make url
1354    $basename = basename($dest);
1355    $site_url = $blog['blog_site_url'];
1356    if (!preg_match('!/$!', $site_url))
1357        $site_url .= '/';
1358
1359    $thumb_url = $site_url . $cache_path . '/' . $date_stamp . '/' . $basename;
1360
1361    return array($thumb_url, $thumb_w, $thumb_h, $thumb_name);
1362}
1363
1364function asset_cleanup($str) {
1365    $str = preg_replace_callback('/<(?:[Ff][Oo][Rr][Mm]|[Ss][Pp][Aa][Nn])([^>]*?)\smt:asset-id="\d+"([^>]+?>)(.*?)<\/(?:[Ff][Oo][Rr][Mm]|[Ss][Pp][Aa][Nn])>/s', 'asset_cleanup_cb', $str);
1366    return $str;
1367}
1368
1369function asset_cleanup_cb($matches) {
1370    $attr = $matches[1] . $matches[2];
1371    $inner = $matches[3];
1372    $attr = preg_replace('/\s[Cc][Oo][Nn][Tt][Ee][Nn][Tt][Ee][Dd][Ii][Tt][Aa][Bb][Ll][Ee]=([\'"][^\'"]*?[\'"]|[Ff][Aa][Ll][Ss][Ee])/', '', $attr);
1373    return '<span' . $attr . $inner . '</span>';
1374}
1375
1376function create_role_expr_function($expr, &$roles, $datasource = 'author') {
1377    $roles_used = array();
1378    $orig_expr = $expr;
1379
1380    $expr = preg_replace('/,/i', ' OR ', $expr);
1381
1382    foreach ($roles as $role) {
1383        $rolen = $role['role_name'];
1384        $roleid = $role['role_id'];
1385        $oldexpr = $expr;
1386        $expr = preg_replace("!(?:\Q[$rolen]\E|\Q$rolen\E)!", "#$roleid", $expr);
1387        if ($oldexpr != $expr)
1388            $roles_used[$roleid] = $role;
1389    }
1390
1391    $expr = preg_replace('/\bOR\b/i', '||', $expr);
1392    $expr = preg_replace('/\bAND\b/i', '&&', $expr);
1393    $expr = preg_replace('/\bNOT\b/i', '!', $expr);
1394    $expr = preg_replace_callback('/( |#\d+|&&|\|\||!|\(|\))|([^#0-9&|!()]+)/', 'create_expr_exception', $expr);
1395
1396    $test_expr = preg_replace('/!|&&|\|\||\(0\)|\(|\)|\s|#\d+/', '', $expr);
1397    if ($test_expr != '') {
1398        echo "Invalid role filter: $orig_expr";
1399        return;
1400    }
1401
1402    if (!preg_match('/!/', $expr))
1403        $roles = array_values($roles_used);
1404
1405    $column_name = $datasource . '_id';
1406    $expr = preg_replace('/#(\d+)/', "array_key_exists('\\1', \$tm)", $expr);
1407
1408    $expr = '$tm = array_key_exists($e["'.$datasource.'_id"], $c["r"]) ? $c["r"][$e["'.$datasource.'_id"]] : array(); return ' . $expr . ';';
1409    $fn = create_function('&$e,&$c', $expr);
1410    if ($fn === FALSE) {
1411        echo "Invalid role filter: $orig_expr";
1412        return;
1413    }
1414    return $fn;
1415}
1416
1417function create_status_expr_function($expr, &$status, $datasource = 'author') {
1418    $orig_expr = $expr;
1419
1420    foreach ($status as $s) {
1421        $statusn = $s['name'];
1422        $statusid = $s['id'];
1423        $oldexpr = $expr;
1424        $expr = preg_replace("!(?:\Q[$statusn]\E|\Q$statusn\E)!", "#$statusid", $expr);
1425    }
1426
1427    $expr = preg_replace('/\bOR\b/i', '||', $expr);
1428    $expr = preg_replace_callback('/( |#\d+|&&|\|\||!|\(|\))|([^#0-9&|!()]+)/', 'create_expr_exception', $expr);
1429
1430    $test_expr = preg_replace('/!|&&|\|\||\(0\)|\(|\)|\s|#\d+/', '', $expr);
1431    if ($test_expr != '') {
1432        echo "Invalid status filter: $orig_expr";
1433        return;
1434    }
1435
1436    $expr = preg_replace('/#(\d+)/', '$e["'.$datasource.'_status"] == \\1', $expr);
1437    $expr = 'return ' . $expr . ';';
1438
1439    $fn = create_function('&$e,&$c', $expr);
1440    if ($fn === FALSE) {
1441        echo "Invalid status filter: $orig_expr";
1442        return;
1443    }
1444    return $fn;
1445}
1446
1447function create_rating_expr_function($expr, $filter, $namespace, $datasource = 'entry') {
1448    $orig_expr = $expr;
1449
1450    require_once 'rating_lib.php';
1451    $expr = '$ctx = $c; if ($ctx == null) { global $mt; $ctx = $mt->context(); } $old = $ctx->mt->db->result; ';
1452    if ($filter == 'min_score') {
1453        $expr .= '$ret = score_for($ctx, $e["'.$datasource.'_id"], "'.$datasource.'", "'.$namespace.'")  >= '.$orig_expr.';';
1454    } elseif ($filter == 'max_score') {
1455        $expr .= '$ret = score_for($ctx, $e["'.$datasource.'_id"], "'.$datasource.'", "'.$namespace.'")  <= '.$orig_expr.';';
1456    } elseif ($filter == 'min_rate') {
1457        $expr .= '$ret = score_avg($ctx, $e["'.$datasource.'_id"], "'.$datasource.'", "'.$namespace.'")  >= '.$orig_expr.';';
1458    } elseif ($filter == 'max_rate') {
1459        $expr .= '$ret = score_avg($ctx, $e["'.$datasource.'_id"], "'.$datasource.'", "'.$namespace.'")  <= '.$orig_expr.';';
1460    } elseif ($filter == 'min_count') {
1461        $expr .= '$ret = score_count($ctx, $e["'.$datasource.'_id"], "'.$datasource.'", "'.$namespace.'")  >= '.$orig_expr.';';
1462    } elseif ($filter == 'max_count') {
1463        $expr .= '$ret = score_count($ctx, $e["'.$datasource.'_id"], "'.$datasource.'", "'.$namespace.'")  <= '.$orig_expr.';';
1464    } elseif ($filter == 'scored_by') {
1465        $expr .= '$ret = get_score($ctx, $e["'.$datasource.'_id"], "'.$datasource.'", "'.$namespace.'", '.$orig_expr.') > 0;';
1466    }
1467    $expr .= ' $ctx->mt->db->result = $old; return $ret;';
1468
1469    $fn = create_function('&$e,&$c', $expr);
1470    if ($fn === FALSE) {
1471        echo "Invalid rating filter: $orig_expr";
1472        return;
1473    }
1474    return $fn;
1475}
1476
1477function _math_operation($op, $lvalue, $rvalue) {
1478    if (!preg_match('/^\-?[\d\.]+$/', $lvalue))
1479        return;
1480    if ( isset($rvalue) && !preg_match('/^\-?[\d\.]+$/', $rvalue))
1481        return;
1482    if ( !isset($rvalue)
1483      && $op != 'inc' && $op != 'dec' && $op != '++' && $op != '--' )
1484        return;
1485    if ( ( '+' == $op ) || ( 'add' == $op ) ) {
1486        return $lvalue + $rvalue;
1487    }
1488    elseif ( ( '++' == $op ) || ( 'inc' == $op ) ) {
1489        return $lvalue + 1;
1490    }
1491    elseif ( ( '-' == $op ) || ( 'sub' == $op ) ) {
1492        return $lvalue - $rvalue;
1493    }
1494    elseif ( ( '--' == $op ) || ( 'dec' == $op ) ) {
1495        return $lvalue - 1;
1496    }
1497    elseif ( ( '*' == $op ) || ( 'mul' == $op ) ) {
1498        return $lvalue * $rvalue;
1499    }
1500    elseif ( ( '/' == $op ) || ( 'div' == $op ) ) {
1501        if ( $rvalue == 0 )
1502            return;
1503        return $lvalue / $rvalue;
1504    }
1505    elseif ( ( '%' == $op ) || ( 'mod' == $op ) ) {
1506        // to be in line with perl equivalent
1507        $lvalue = floor($lvalue);
1508        $rvalue = floor($rvalue);
1509        if ( $rvalue == 0 )
1510            return;
1511        return $lvalue % $rvalue;
1512    }
1513    return;
1514}
1515
1516function apply_text_filter ($ctx, $text, $filters) {
1517    if ($text == '' || $filters == '') return $text;
1518
1519    $f = preg_split('/\s*,\s*/', $filters);
1520    if (is_array($f) && count($f) > 0) {
1521        foreach ($f as $filter) {
1522            if ($filter == '__default__') {
1523                $filter = 'convert_breaks';
1524            }
1525            if ($filter == 'convert_breaks') {
1526                $text = html_text_transform($text);
1527            } elseif ($ctx->load_modifier($filter)) {
1528                $mod = 'smarty_modifier_'.$filter;
1529                $text = $mod($text);
1530            }
1531        }
1532    }
1533
1534    return $text;
1535}
1536
1537function get_page_column ($layout) {
1538    $columns = 0;
1539    if (empty($layout)) return $columns;
1540
1541    $columns_map = array(
1542        'layout-wt'  => 2,
1543        'layout-tw'  => 2,
1544        'layout-wm'  => 2,
1545        'layout-mw'  => 2,
1546        'layout-wtt' => 3,
1547        'layout-twt' => 3);
1548
1549    if(array_key_exists($layout, $columns_map))
1550         $columns = $columns_map[$layout];
1551
1552    return $columns;
1553}
1554
1555function mkpath($path, $mode = 0777) {
1556    // PHP5 supports recursive param
1557    if (version_compare(PHP_VERSION, '5.0.0', ">="))
1558        return mkdir($path, $mode, true);
1559
1560    // for php4
1561    is_dir(dirname($path)) || mkpath(dirname($path), $mode);
1562    return is_dir($path) || @mkdir($path, $mode);
1563}
1564
1565?>
Note: See TracBrowser for help on using the browser.