/php/lib/MTUtil.php
PHP | 1689 lines | 1506 code | 127 blank | 56 comment | 232 complexity | a7b9f66679f1db91a2042b0e633ff907 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, LGPL-2.1
Large files files are truncated, but you can click here to view the full file
- <?php
- # Movable Type (r) Open Source (C) 2001-2010 Six Apart, Ltd.
- # This program is distributed under the terms of the
- # GNU General Public License, version 2.
- #
- # $Id: MTUtil.php 5151 2010-01-06 07:51:27Z takayama $
- function datetime_to_timestamp($dt) {
- $dt = preg_replace('/[^0-9]/', '', $dt);
- $ts = mktime(substr($dt, 8, 2), substr($dt, 10, 2), substr($dt, 12, 2), substr($dt, 4, 2), substr($dt, 6, 2), substr($dt, 0, 4));
- return $ts;
- }
- function start_end_ts($ts) {
- if ($ts) {
- if (strlen($ts) == 4) {
- $ts_start = $ts . '0101';
- $ts_end = $ts . '1231';
- } elseif (strlen($ts) == 6) {
- $ts_start = $ts . '01';
- $ts_end = $ts . sprintf("%02d", days_in(substr($ts, 4, 2), substr($ts, 0, 4)));
- } else {
- $ts_start = $ts;
- $ts_end = $ts;
- }
- }
- return array($ts_start . '000000', $ts_end . '235959');
- }
- function start_end_month($ts) {
- $y = substr($ts, 0, 4);
- $mo = substr($ts, 4, 2);
- $start = sprintf("%04d%02d01000000", $y, $mo);
- $end = sprintf("%04d%02d%02d235959", $y, $mo, days_in($mo, $y));
- return array($start, $end);
- }
- function days_in($m, $y) {
- return date('t', mktime(0, 0, 0, $m, 1, $y));
- }
- function start_end_day($ts) {
- $day = substr($ts, 0, 8);
- return array($day . "000000", $day . "235959");
- }
- function start_end_year($ts) {
- $year = substr($ts, 0, 4);
- return array($year . "0101000000", $year . "1231235959");
- }
- function start_end_week($ts) {
- $y = substr($ts, 0, 4);
- $mo = substr($ts, 4, 2);
- $d = substr($ts, 6, 2);
- $h = substr($ts, 8, 2);
- $s = substr($ts, 10, 2);
- $wday = wday_from_ts($y, $mo, $d);
- list($sd, $sm, $sy) = array($d - $wday, $mo, $y);
- if ($sd < 1) {
- $sm--;
- if ($sm < 1) {
- $sm = 12; $sy--;
- }
- $sd += days_in($sm, $sy);
- }
- $start = sprintf("%04d%02d%02d%s", $sy, $sm, $sd, "000000");
- list($ed, $em, $ey) = array($d + 6 - $wday, $mo, $y);
- if ($ed > days_in($em, $ey)) {
- $ed -= days_in($em, $ey);
- $em++;
- if ($em > 12) {
- $em = 1; $ey++;
- }
- }
- $end = sprintf("%04d%02d%02d%s", $ey, $em, $ed, "235959");
- return array($start, $end);
- }
- global $In_Year;
- $In_Year = array(
- array( 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 ),
- array( 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 ),
- );
- function week2ymd($y, $week) {
- $jan_one_dow_m1 = (ymd2rd($y, 1, 1) + 6) % 7;
- if ($jan_one_dow_m1 < 4) $week--;
- $day_of_year = $week * 7 - $jan_one_dow_m1;
- $leap_year = is_leap_year($y);
- if ($day_of_year < 1) {
- $y--;
- $day_of_year = ($leap_year ? 366 : 365) + $day_of_year;
- }
- if ($leap_year) {
- $ref = array(0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335);
- } else {
- $ref = array(0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334);
- }
- $m = 0;
- for ($i = count($ref); $i > 0; $i--) {
- if ($day_of_year > $ref[$i-1]) {
- $m = $i;
- break;
- }
- }
- return array($y, $m, $day_of_year - $ref[$m-1]);
- }
- function is_leap_year($y) {
- return (!($y % 4) && ($y % 100)) || !($y % 400) ? true : false;
- }
- function ymd2rd($y,$m,$d) {
- # make month in range 3..14 (treat Jan & Feb as months 13..14 of
- # prev year)
- if ( $m <= 2 ) {
- $adj = (int)(( 14 - $m ) / 12);
- $y -= $adj;
- $m += 12 * $adj;
- }
- elseif ( $m > 14 )
- {
- $adj = (int)(( $m - 3 ) / 12);
- $y += $adj;
- $m -= 12 * $adj;
- }
- # make year positive (oh, for a use integer 'sane_div'!)
- if ( $y < 0 )
- {
- $adj = (int)(( 399 - $y ) / 400);
- $d -= 146097 * $adj;
- $y += 400 * $adj;
- }
- # add: day of month, days of previous 0-11 month period that began
- # w/March, days of previous 0-399 year period that began w/March
- # of a 400-multiple year), days of any 400-year periods before
- # that, and 306 days to adjust from Mar 1, year 0-relative to Jan
- # 1, year 1-relative (whew)
- $d += (int)(( $m * 367 - 1094 ) / 12) + (int)((($y % 100) * 1461) / 4) +
- ( (int)($y / 100) * 36524 + (int)($y / 400) ) - 306;
- return $d;
- }
- function wday_from_ts($y, $m, $d) {
- global $In_Year;
- $leap = $y % 4 == 0 && ($y % 100 != 0 || $y % 400 == 0) ? 1 : 0;
- $y--;
- ## Copied from Date::Calc.
- $days = $y * 365;
- $days += $y >>= 2;
- $days -= intval($y /= 25);
- $days += $y >> 2;
- $days += $In_Year[$leap][$m-1] + $d;
- return ($days-1) % 7; // Patch Monday
- }
- function yday_from_ts($y, $m, $d) {
- global $In_Year;
- $leap = $y % 4 == 0 && ($y % 100 != 0 || $y % 400 == 0) ? 1 : 0;
- return $In_Year[$leap][$m-1] + $d;
- }
- function substr_wref($str, $start, $length) {
- if (preg_match_all('/(&[^;]*;|.)/', $str, $character_entities)) {
- return implode('', array_slice($character_entities[0], $start, $length));
- } else {
- return '';
- }
- }
- function format_ts($format, $ts, $blog, $lang = null) {
- global $Languages;
- if (!isset($lang) || empty($lang)) {
- $mt = MT::get_instance();
- $lang = ($blog && $blog->blog_language ? $blog->blog_language :
- $mt->config('DefaultLanguage'));
- }
- if ($lang == 'jp') {
- $lang = 'ja';
- }
- $lang = strtolower(substr($lang, 0, 2));
- if (!isset($format) || empty($format)) {
- if (count($Languages[$lang]) >= 4)
- $format = $Languages[$lang][3];
- $format or $format = "%B %e, %Y %l:%M %p";
- }
- global $_format_ts_cache;
- if (!isset($_format_ts_cache)) {
- $_format_ts_cache = array();
- }
- if (isset($_format_ts_cache[$ts.$lang])) {
- $f = $_format_ts_cache[$ts.$lang];
- } else {
- $L = $Languages[$lang];
- $tsa = array(substr($ts, 0, 4), substr($ts, 4, 2), substr($ts, 6, 2),
- substr($ts, 8, 2), substr($ts, 10, 2), substr($ts, 12, 2));
- list($f['Y'], $f['m'], $f['d'], $f['H'], $f['M'], $f['S']) = $tsa;
- $f['w'] = wday_from_ts($tsa[0],$tsa[1],$tsa[2]);
- $f['j'] = yday_from_ts($tsa[0],$tsa[1],$tsa[2]);
- $f['y'] = substr($f['Y'], 2);
- $f['b'] = substr_wref($L[1][$f['m']-1], 0, 3);
- $f['B'] = $L[1][$f['m']-1];
- if ($lang == 'ja') {
- $f['a'] = substr($L[0][$f['w']], 0, 8);
- } else {
- $f['a'] = substr_wref($L[0][$f['w']], 0, 3);
- }
- $f['A'] = $L[0][$f['w']];
- $f['e'] = $f['d'];
- $f['e'] = preg_replace('!^0!', ' ', $f['e']);
- $f['I'] = $f['H'];
- if ($f['I'] > 12) {
- $f['I'] -= 12;
- $f['p'] = $L[2][1];
- } elseif ($f['I'] == 0) {
- $f['I'] = 12;
- $f['p'] = $L[2][0];
- } elseif ($f['I'] == 12) {
- $f['p'] = $L[2][1];
- } else {
- $f['p'] = $L[2][0];
- }
- $f['I'] = sprintf("%02d", $f['I']);
- $f['k'] = $f['H'];
- $f['k'] = preg_replace('!^0!', ' ', $f['k']);
- $f['l'] = $f['I'];
- $f['l'] = preg_replace('!^0!', ' ', $f['l']);
- $f['j'] = sprintf("%03d", $f['j']);
- $f['Z'] = '';
- $_format_ts_cache[$ts . $lang] = $f;
- }
- $date_format = null;
- if (count($Languages[$lang]) >= 5)
- $date_format = $Languages[$lang][4];
- $date_format or $date_format = "%B %e, %Y";
- $time_format = null;
- if (count($Languages[$lang]) >= 6)
- $time_format = $Languages[$lang][5];
- $time_format or $time_format = "%l:%M %p";
- $format = preg_replace('!%x!', $date_format, $format);
- $format = preg_replace('!%X!', $time_format, $format);
- ## This is a dreadful hack. I can't think of a good format specifier
- ## for "%B %Y" (which is used for monthly archives, for example) so
- ## I'll just hardcode this, for Japanese dates.
- if ($lang == 'ja') {
- if (count($Languages[$lang]) >= 8) {
- $format = preg_replace('!%B %Y!', $Languages[$lang][6], $format);
- $format = preg_replace('!%B %E,? %Y!i', $Languages[$lang][4], $format);
- $format = preg_replace('!%B %E!', $Languages[$lang][7], $format);
- }
- }
- elseif ( $lang == 'it' ) {
- ## Hack for the Italian dates
- ## In Italian, the date always come before the month.
- $format = preg_replace('!%b %e!', '%e %b', $format);
- }
- if (isset($format)) {
- $format = preg_replace('!%(\w)!e', '\$f[\'\1\']', $format);
- }
- return $format;
- }
- function dirify($s, $sep = '_') {
- $mt = MT::get_instance();
- $charset = $mt->config('PublishCharset');
- $charset or $charset = 'utf-8';
- if (preg_match('/utf-?8/i', $charset)) {
- return utf8_dirify($s, $sep);
- } else {
- return iso_dirify($s, $sep);
- }
- }
- function utf8_dirify($s, $sep = '_') {
- if ($sep == '1') $sep = '_';
- $s = xliterate_utf8($s); ## convert high-ASCII chars to 7bit.
- $s = strtolower($s); ## lower-case.
- $s = strip_tags($s); ## remove HTML tags.
- $s = preg_replace('!&[^;\s]+;!', '', $s); ## remove HTML entities.
- $s = preg_replace('![^\w\s]!', '', $s); ## remove non-word/space chars.
- $s = preg_replace('/\s+/',$sep,$s); ## change space chars to underscores.
- return($s);
- }
- global $Utf8_ASCII;
- $Utf8_ASCII = array(
- "\xc3\x80" => 'A', # A`
- "\xc3\xa0" => 'a', # a`
- "\xc3\x81" => 'A', # A'
- "\xc3\xa1" => 'a', # a'
- "\xc3\x82" => 'A', # A^
- "\xc3\xa2" => 'a', # a^
- "\xc4\x82" => 'A', # latin capital letter a with breve
- "\xc4\x83" => 'a', # latin small letter a with breve
- "\xc3\x86" => 'AE', # latin capital letter AE
- "\xc3\xa6" => 'ae', # latin small letter ae
- "\xc3\x85" => 'A', # latin capital letter a with ring above
- "\xc3\xa5" => 'a', # latin small letter a with ring above
- "\xc4\x80" => 'A', # latin capital letter a with macron
- "\xc4\x81" => 'a', # latin small letter a with macron
- "\xc4\x84" => 'A', # latin capital letter a with ogonek
- "\xc4\x85" => 'a', # latin small letter a with ogonek
- "\xc3\x84" => 'A', # A:
- "\xc3\xa4" => 'a', # a:
- "\xc3\x83" => 'A', # A~
- "\xc3\xa3" => 'a', # a~
- "\xc3\x88" => 'E', # E`
- "\xc3\xa8" => 'e', # e`
- "\xc3\x89" => 'E', # E'
- "\xc3\xa9" => 'e', # e'
- "\xc3\x8a" => 'E', # E^
- "\xc3\xaa" => 'e', # e^
- "\xc3\x8b" => 'E', # E:
- "\xc3\xab" => 'e', # e:
- "\xc4\x92" => 'E', # latin capital letter e with macron
- "\xc4\x93" => 'e', # latin small letter e with macron
- "\xc4\x98" => 'E', # latin capital letter e with ogonek
- "\xc4\x99" => 'e', # latin small letter e with ogonek
- "\xc4\x9a" => 'E', # latin capital letter e with caron
- "\xc4\x9b" => 'e', # latin small letter e with caron
- "\xc4\x94" => 'E', # latin capital letter e with breve
- "\xc4\x95" => 'e', # latin small letter e with breve
- "\xc4\x96" => 'E', # latin capital letter e with dot above
- "\xc4\x97" => 'e', # latin small letter e with dot above
- "\xc3\x8c" => 'I', # I`
- "\xc3\xac" => 'i', # i`
- "\xc3\x8d" => 'I', # I'
- "\xc3\xad" => 'i', # i'
- "\xc3\x8e" => 'I', # I^
- "\xc3\xae" => 'i', # i^
- "\xc3\x8f" => 'I', # I:
- "\xc3\xaf" => 'i', # i:
- "\xc4\xaa" => 'I', # latin capital letter i with macron
- "\xc4\xab" => 'i', # latin small letter i with macron
- "\xc4\xa8" => 'I', # latin capital letter i with tilde
- "\xc4\xa9" => 'i', # latin small letter i with tilde
- "\xc4\xac" => 'I', # latin capital letter i with breve
- "\xc4\xad" => 'i', # latin small letter i with breve
- "\xc4\xae" => 'I', # latin capital letter i with ogonek
- "\xc4\xaf" => 'i', # latin small letter i with ogonek
- "\xc4\xb0" => 'I', # latin capital letter with dot above
- "\xc4\xb1" => 'i', # latin small letter dotless i
- "\xc4\xb2" => 'IJ', # latin capital ligature ij
- "\xc4\xb3" => 'ij', # latin small ligature ij
- "\xc4\xb4" => 'J', # latin capital letter j with circumflex
- "\xc4\xb5" => 'j', # latin small letter j with circumflex
- "\xc4\xb6" => 'K', # latin capital letter k with cedilla
- "\xc4\xb7" => 'k', # latin small letter k with cedilla
- "\xc4\xb8" => 'k', # latin small letter kra
- "\xc5\x81" => 'L', # latin capital letter l with stroke
- "\xc5\x82" => 'l', # latin small letter l with stroke
- "\xc4\xbd" => 'L', # latin capital letter l with caron
- "\xc4\xbe" => 'l', # latin small letter l with caron
- "\xc4\xb9" => 'L', # latin capital letter l with acute
- "\xc4\xba" => 'l', # latin small letter l with acute
- "\xc4\xbb" => 'L', # latin capital letter l with cedilla
- "\xc4\xbc" => 'l', # latin small letter l with cedilla
- "\xc4\xbf" => 'l', # latin capital letter l with middle dot
- "\xc5\x80" => 'l', # latin small letter l with middle dot
- "\xc3\x92" => 'O', # O`
- "\xc3\xb2" => 'o', # o`
- "\xc3\x93" => 'O', # O'
- "\xc3\xb3" => 'o', # o'
- "\xc3\x94" => 'O', # O^
- "\xc3\xb4" => 'o', # o^
- "\xc3\x96" => 'O', # O:
- "\xc3\xb6" => 'o', # o:
- "\xc3\x95" => 'O', # O~
- "\xc3\xb5" => 'o', # o~
- "\xc3\x98" => 'O', # O/
- "\xc3\xb8" => 'o', # o/
- "\xc5\x8c" => 'O', # latin capital letter o with macron
- "\xc5\x8d" => 'o', # latin small letter o with macron
- "\xc5\x90" => 'O', # latin capital letter o with double acute
- "\xc5\x91" => 'o', # latin small letter o with double acute
- "\xc5\x8e" => 'O', # latin capital letter o with breve
- "\xc5\x8f" => 'o', # latin small letter o with breve
- "\xc5\x92" => 'OE', # latin capital ligature oe
- "\xc5\x93" => 'oe', # latin small ligature oe
- "\xc5\x94" => 'R', # latin capital letter r with acute
- "\xc5\x95" => 'r', # latin small letter r with acute
- "\xc5\x98" => 'R', # latin capital letter r with caron
- "\xc5\x99" => 'r', # latin small letter r with caron
- "\xc5\x96" => 'R', # latin capital letter r with cedilla
- "\xc5\x97" => 'r', # latin small letter r with cedilla
- "\xc3\x99" => 'U', # U`
- "\xc3\xb9" => 'u', # u`
- "\xc3\x9a" => 'U', # U'
- "\xc3\xba" => 'u', # u'
- "\xc3\x9b" => 'U', # U^
- "\xc3\xbb" => 'u', # u^
- "\xc3\x9c" => 'U', # U:
- "\xc3\xbc" => 'u', # u:
- "\xc5\xaa" => 'U', # latin capital letter u with macron
- "\xc5\xab" => 'u', # latin small letter u with macron
- "\xc5\xae" => 'U', # latin capital letter u with ring above
- "\xc5\xaf" => 'u', # latin small letter u with ring above
- "\xc5\xb0" => 'U', # latin capital letter u with double acute
- "\xc5\xb1" => 'u', # latin small letter u with double acute
- "\xc5\xac" => 'U', # latin capital letter u with breve
- "\xc5\xad" => 'u', # latin small letter u with breve
- "\xc5\xa8" => 'U', # latin capital letter u with tilde
- "\xc5\xa9" => 'u', # latin small letter u with tilde
- "\xc5\xb2" => 'U', # latin capital letter u with ogonek
- "\xc5\xb3" => 'u', # latin small letter u with ogonek
- "\xc3\x87" => 'C', # ,C
- "\xc3\xa7" => 'c', # ,c
- "\xc4\x86" => 'C', # latin capital letter c with acute
- "\xc4\x87" => 'c', # latin small letter c with acute
- "\xc4\x8c" => 'C', # latin capital letter c with caron
- "\xc4\x8d" => 'c', # latin small letter c with caron
- "\xc4\x88" => 'C', # latin capital letter c with circumflex
- "\xc4\x89" => 'c', # latin small letter c with circumflex
- "\xc4\x8a" => 'C', # latin capital letter c with dot above
- "\xc4\x8b" => 'c', # latin small letter c with dot above
- "\xc4\x8e" => 'D', # latin capital letter d with caron
- "\xc4\x8f" => 'd', # latin small letter d with caron
- "\xc4\x90" => 'D', # latin capital letter d with stroke
- "\xc4\x91" => 'd', # latin small letter d with stroke
- "\xc3\x91" => 'N', # N~
- "\xc3\xb1" => 'n', # n~
- "\xc5\x83" => 'N', # latin capital letter n with acute
- "\xc5\x84" => 'n', # latin small letter n with acute
- "\xc5\x87" => 'N', # latin capital letter n with caron
- "\xc5\x88" => 'n', # latin small letter n with caron
- "\xc5\x85" => 'N', # latin capital letter n with cedilla
- "\xc5\x86" => 'n', # latin small letter n with cedilla
- "\xc5\x89" => 'n', # latin small letter n preceded by apostrophe
- "\xc5\x8a" => 'N', # latin capital letter eng
- "\xc5\x8b" => 'n', # latin small letter eng
- "\xc3\x9f" => 'ss', # double-s
- "\xc5\x9a" => 'S', # latin capital letter s with acute
- "\xc5\x9b" => 's', # latin small letter s with acute
- "\xc5\xa0" => 'S', # latin capital letter s with caron
- "\xc5\xa1" => 's', # latin small letter s with caron
- "\xc5\x9e" => 'S', # latin capital letter s with cedilla
- "\xc5\x9f" => 's', # latin small letter s with cedilla
- "\xc5\x9c" => 'S', # latin capital letter s with circumflex
- "\xc5\x9d" => 's', # latin small letter s with circumflex
- "\xc8\x98" => 'S', # latin capital letter s with comma below
- "\xc8\x99" => 's', # latin small letter s with comma below
- "\xc5\xa4" => 'T', # latin capital letter t with caron
- "\xc5\xa5" => 't', # latin small letter t with caron
- "\xc5\xa2" => 'T', # latin capital letter t with cedilla
- "\xc5\xa3" => 't', # latin small letter t with cedilla
- "\xc5\xa6" => 'T', # latin capital letter t with stroke
- "\xc5\xa7" => 't', # latin small letter t with stroke
- "\xc8\x9a" => 'T', # latin capital letter t with comma below
- "\xc8\x9b" => 't', # latin small letter t with comma below
- "\xc6\x92" => 'f', # latin small letter f with hook
- "\xc4\x9c" => 'G', # latin capital letter g with circumflex
- "\xc4\x9d" => 'g', # latin small letter g with circumflex
- "\xc4\x9e" => 'G', # latin capital letter g with breve
- "\xc4\x9f" => 'g', # latin small letter g with breve
- "\xc4\xa0" => 'G', # latin capital letter g with dot above
- "\xc4\xa1" => 'g', # latin small letter g with dot above
- "\xc4\xa2" => 'G', # latin capital letter g with cedilla
- "\xc4\xa3" => 'g', # latin small letter g with cedilla
- "\xc4\xa4" => 'H', # latin capital letter h with circumflex
- "\xc4\xa5" => 'h', # latin small letter h with circumflex
- "\xc4\xa6" => 'H', # latin capital letter h with stroke
- "\xc4\xa7" => 'h', # latin small letter h with stroke
- "\xc5\xb4" => 'W', # latin capital letter w with circumflex
- "\xc5\xb5" => 'w', # latin small letter w with circumflex
- "\xc3\x9d" => 'Y', # latin capital letter y with acute
- "\xc3\xbd" => 'y', # latin small letter y with acute
- "\xc5\xb8" => 'Y', # latin capital letter y with diaeresis
- "\xc3\xbf" => 'y', # latin small letter y with diaeresis
- "\xc5\xb6" => 'Y', # latin capital letter y with circumflex
- "\xc5\xb7" => 'y', # latin small letter y with circumflex
- "\xc5\xbd" => 'Z', # latin capital letter z with caron
- "\xc5\xbe" => 'z', # latin small letter z with caron
- "\xc5\xbb" => 'Z', # latin capital letter z with dot above
- "\xc5\xbc" => 'z', # latin small letter z with dot above
- "\xc5\xb9" => 'Z', # latin capital letter z with acute
- "\xc5\xba" => 'z', # latin small letter z with acute
- "\xd0\x81" => 'E', # cyrillic capital letter IO
- "\xd0\x90" => 'A', # cyrillic capital letter A
- "\xd0\x91" => 'B', # cyrillic capital letter BE
- "\xd0\x92" => 'V', # cyrillic capital letter VE
- "\xd0\x93" => 'G', # cyrillic capital letter GHE
- "\xd0\x94" => 'D', # cyrillic capital letter DE
- "\xd0\x95" => 'E', # cyrillic capital letter IE
- "\xd0\x96" => 'Zh', # cyrillic capital letter ZHE
- "\xd0\x97" => 'Z', # cyrillic capital letter ZE
- "\xd0\x98" => 'I', # cyrillic capital letter I
- "\xd0\x99" => 'J', # cyrillic capital letter SHORT I
- "\xd0\x9a" => 'K', # cyrillic capital letter KA
- "\xd0\x9b" => 'L', # cyrillic capital letter EL
- "\xd0\x9c" => 'M', # cyrillic capital letter EM
- "\xd0\x9d" => 'N', # cyrillic capital letter EN
- "\xd0\x9e" => 'O', # cyrillic capital letter O
- "\xd0\x9f" => 'P', # cyrillic capital letter PE
- "\xd0\xa0" => 'R', # cyrillic capital letter ER
- "\xd0\xa1" => 'S', # cyrillic capital letter ES
- "\xd0\xa2" => 'T', # cyrillic capital letter TE
- "\xd0\xa3" => 'U', # cyrillic capital letter U
- "\xd0\xa4" => 'F', # cyrillic capital letter EF
- "\xd0\xa5" => 'X', # cyrillic capital letter HA
- "\xd0\xa6" => 'C', # cyrillic capital letter TSE
- "\xd0\xa7" => 'Ch', # cyrillic capital letter CHE
- "\xd0\xa8" => 'Sh', # cyrillic capital letter SHA
- "\xd0\xa9" => 'Sch', # cyrillic capital letter SHCHA
- "\xd0\xaa" => '_', # cyrillic capital letter HARD SIGN
- "\xd0\xab" => 'I', # cyrillic capital letter YERU
- "\xd0\xac" => '_', # cyrillic capital letter SOFT SIGN
- "\xd0\xad" => 'E', # cyrillic capital letter E
- "\xd0\xae" => 'Ju', # cyrillic captial letter JU
- "\xd0\xaf" => 'Ja', # cyrillic capital letter YA
- "\xd0\xb0" => 'a', # cyrillic small letter A
- "\xd0\xb1" => 'b', # cyrillic small letter BE
- "\xd0\xb2" => 'v', # cyrillic small letter VE
- "\xd0\xb3" => 'g', # cyrillic small letter GHE
- "\xd0\xb4" => 'd', # cyrillic small letter DE
- "\xd0\xb5" => 'e', # cyrillic small letter IE
- "\xd0\xb6" => 'zh', # cyrillic small letter ZHE
- "\xd0\xb7" => 'z', # cyrillic small letter ZE
- "\xd0\xb8" => 'i', # cyrillic small letter I
- "\xd0\xb9" => 'j', # cyrillic small letter SHORT I
- "\xd0\xba" => 'k', # cyrillic small letter KA
- "\xd0\xbb" => 'l', # cyrillic small letter EL
- "\xd0\xbc" => 'm', # cyrillic small letter EM
- "\xd0\xbd" => 'n', # cyrillic small letter EN
- "\xd0\xbe" => 'o', # cyrillic small letter O
- "\xd0\xbf" => 'p', # cyrillic small letter PE
- "\xd1\x80" => 'r', # cyrillic small letter ER
- "\xd1\x81" => 's', # cyrillic small letter ES
- "\xd1\x82" => 't', # cyrillic small letter TE
- "\xd1\x83" => 'u', # cyrillic small letter U
- "\xd1\x84" => 'f', # cyrillic small letter EF
- "\xd1\x85" => 'x', # cyrillic small letter HA
- "\xd1\x86" => 'c', # cyrillic small letter TSE
- "\xd1\x87" => 'ch', # cyrillic small letter CHE
- "\xd1\x88" => 'sh', # cyrillic small letter SHA
- "\xd1\x89" => 'sch', # cyrillic small letter SHCHA
- "\xd1\x8a" => '_', # cyrillic small letter HARD SIGN
- "\xd1\x8b" => 'i', # cyrillic small letter YERU
- "\xd1\x8c" => '_', # cyrillic small letter SOFT SIGN
- "\xd1\x8d" => 'e', # cyrillic small letter E
- "\xd1\x8e" => 'ju', # cyrillic small letter YU
- "\xd1\x8f" => 'ja', # cyrillic small letter YA
- "\xd1\x91" => 'e', # cyrillic small letter IO
- );
- function xliterate_utf8($s) {
- global $Utf8_ASCII;
- return strtr($s, $Utf8_ASCII);
- }
- function iso_dirify($s, $sep = '_') {
- if ($sep == '1') $sep = '_';
- $s = convert_high_ascii($s); ## convert high-ASCII chars to 7bit.
- $s = strtolower($s); ## lower-case.
- $s = strip_tags($s); ## remove HTML tags.
- $s = preg_replace('!&[^;\s]+;!', '', $s); ## remove HTML entities.
- $s = preg_replace('![^\w\s]!', '', $s); ## remove non-word/space chars.
- $s = preg_replace('/\s+/',$sep,$s); ## change space chars to underscores.
- return($s);
- }
- global $Latin1_ASCII;
- $Latin1_ASCII = array(
- "\xc0" => 'A', # A`
- "\xe0" => 'a', # a`
- "\xc1" => 'A', # A'
- "\xe1" => 'a', # a'
- "\xc2" => 'A', # A^
- "\xe2" => 'a', # a^
- "\xc4" => 'A', # A:
- "\xe4" => 'a', # a:
- "\xc5" => 'A', # Aring
- "\xe5" => 'a', # aring
- "\xc6" => 'AE', # AE
- "\xe6" => 'ae', # ae
- "\xc3" => 'A', # A~
- "\xe3" => 'a', # a~
- "\xc8" => 'E', # E`
- "\xe8" => 'e', # e`
- "\xc9" => 'E', # E'
- "\xe9" => 'e', # e'
- "\xca" => 'E', # E^
- "\xea" => 'e', # e^
- "\xcb" => 'E', # E:
- "\xeb" => 'e', # e:
- "\xcc" => 'I', # I`
- "\xec" => 'i', # i`
- "\xcd" => 'I', # I'
- "\xed" => 'i', # i'
- "\xce" => 'I', # I^
- "\xee" => 'i', # i^
- "\xcf" => 'I', # I:
- "\xef" => 'i', # i:
- "\xd2" => 'O', # O`
- "\xf2" => 'o', # o`
- "\xd3" => 'O', # O'
- "\xf3" => 'o', # o'
- "\xd4" => 'O', # O^
- "\xf4" => 'o', # o^
- "\xd6" => 'O', # O:
- "\xf6" => 'o', # o:
- "\xd5" => 'O', # O~
- "\xf5" => 'o', # o~
- "\xd8" => 'O', # O/
- "\xf8" => 'o', # o/
- "\xd9" => 'U', # U`
- "\xf9" => 'u', # u`
- "\xda" => 'U', # U'
- "\xfa" => 'u', # u'
- "\xdb" => 'U', # U^
- "\xfb" => 'u', # u^
- "\xdc" => 'U', # U:
- "\xfc" => 'u', # u:
- "\xc7" => 'C', # ,C
- "\xe7" => 'c', # ,c
- "\xd1" => 'N', # N~
- "\xf1" => 'n', # n~
- "\xdd" => 'Y', # Yacute
- "\xfd" => 'y', # yacute
- "\xdf" => 'ss', # szlig
- "\xff" => 'y' # yuml
- );
- function convert_high_ascii($s) {
- $mt = MT::get_instance();
- $lang = $mt->config('DefaultLanguage');
- if ($lang == 'ja') {
- $s = mb_convert_encoding($s, 'UTF-8');
- return $s;
- }
- global $Latin1_ASCII;
- return strtr($s, $Latin1_ASCII);
- }
- global $Languages;
- $Languages = array(
- 'en' => array(
- array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'),
- array('January','February','March','April','May','June',
- 'July','August','September','October','November','December'),
- array('AM','PM'),
- ),
- 'fr' => array(
- array('dimanche','lundi','mardi','mercredi','jeudi','vendredi','samedi' ),
- array('janvier', "février", 'mars', 'avril', 'mai', 'juin',
- 'juillet', "août", 'septembre', 'octobre', 'novembre',
- "décembre"),
- array('AM','PM'),
- ),
- 'es' => array(
- array('Domingo', 'Lunes', 'Martes', "Miércoles", 'Jueves',
- 'Viernes', "Sábado"),
- array('Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio','Agosto',
- 'Septiembre','Octubre','Noviembre','Diciembre'),
- array('AM','PM'),
- ),
- 'pt' => array(
- array('domingo', 'segunda-feira', "terça-feira", 'quarta-feira',
- 'quinta-feira', 'sexta-feira', "sábado"),
- array('janeiro', 'fevereiro', "março", 'abril', 'maio', 'junho',
- 'julho', 'agosto', 'setembro', 'outubro', 'novembro',
- 'dezembro' ),
- array('AM','PM'),
- ),
- 'nl' => array(
- array('zondag','maandag','dinsdag','woensdag','donderdag','vrijdag',
- 'zaterdag'),
- array('januari','februari','maart','april','mei','juni','juli','augustus',
- 'september','oktober','november','december'),
- array('am','pm'),
- "%d %B %Y %H:%M",
- "%d %B %Y"
- ),
- 'dk' => array(
- array("søndag", 'mandag', 'tirsdag', 'onsdag', 'torsdag',
- 'fredag', "lørdag"),
- array('januar','februar','marts','april','maj','juni','juli','august',
- 'september','oktober','november','december'),
- array('am','pm'),
- "%d.%m.%Y %H:%M",
- "%d.%m.%Y",
- "%H:%M",
- ),
- 'se' => array(
- array("söndag", "måndag", 'tisdag', 'onsdag', 'torsdag',
- 'fredag', "lördag"),
- array('januari','februari','mars','april','maj','juni','juli','augusti',
- 'september','oktober','november','december'),
- array('FM','EM'),
- ),
- 'no' => array(
- array("Søndag", "Mandag", 'Tirsdag', 'Onsdag', 'Torsdag',
- 'Fredag', "Lørdag"),
- array('Januar','Februar','Mars','April','Mai','Juni','Juli','August',
- 'September','Oktober','November','Desember'),
- array('FM','EM'),
- ),
- 'de' => array(
- array('Sonntag','Montag','Dienstag','Mittwoch','Donnerstag','Freitag',
- 'Samstag'),
- array('Januar', 'Februar', "März", 'April', 'Mai', 'Juni',
- 'Juli', 'August', 'September', 'Oktober', 'November',
- 'Dezember'),
- array('FM','EM'),
- "%d.%m.%y %H:%M",
- "%d.%m.%y",
- "%H:%M",
- ),
- 'it' => array(
- array('Domenica', "Lunedì", "Martedì", "Mercoledì",
- "Giovedì", "Venerdì", 'Sabato'),
- array('Gennaio','Febbraio','Marzo','Aprile','Maggio','Giugno','Luglio',
- 'Agosto','Settembre','Ottobre','Novembre','Dicembre'),
- array('AM','PM'),
- "%d.%m.%y %H:%M",
- "%d.%m.%y",
- "%H:%M",
- ),
- 'pl' => array(
- array('niedziela', "poniedziałek", 'wtorek', "środa",
- 'czwartek', "piątek", 'sobota'),
- array('stycznia', 'lutego', 'marca', 'kwietnia', 'maja', 'czerwca',
- 'lipca', 'sierpnia', "września", "października",
- 'listopada', 'grudnia'),
- array('AM','PM'),
- "%e %B %Y %k:%M",
- "%e %B %Y",
- "%k:%M",
- ),
-
- 'fi' => array(
- array('sunnuntai','maanantai','tiistai','keskiviikko','torstai','perjantai',
- 'lauantai'),
- array('tammikuu', 'helmikuu', 'maaliskuu', 'huhtikuu', 'toukokuu',
- "kesäkuu", "heinäkuu", 'elokuu', 'syyskuu', 'lokakuu',
- 'marraskuu', 'joulukuu'),
- array('AM','PM'),
- "%d.%m.%y %H:%M",
- ),
-
- 'is' => array(
- array('Sunnudagur', "Mánudagur", "Þriðjudagur",
- "Miðvikudagur", 'Fimmtudagur', "Föstudagur",
- 'Laugardagur'),
- array("janúar", "febrúar", 'mars', "apríl", "maí",
- "júní", "júlí", "ágúst", 'september',
- "október", "nóvember", 'desember'),
- array('FH','EH'),
- "%d.%m.%y %H:%M",
- ),
-
- 'si' => array(
- array('nedelja', 'ponedeljek', 'torek', 'sreda', "ãetrtek",
- 'petek', 'sobota'),
- array('januar','februar','marec','april','maj','junij','julij','avgust',
- 'september','oktober','november','december'),
- array('AM','PM'),
- "%d.%m.%y %H:%M",
- ),
-
- 'cz' => array(
- array('Neděle', 'Pondělí', 'Úterý',
- 'Středa', 'Čtvrtek', 'Pátek', 'Sobota'),
- array('Leden', 'Únor', 'Březen', 'Duben', 'Květen',
- 'Červen', 'Červenec', 'Srpen', 'Září',
- 'Øíjen', 'Listopad', 'Prosinec'),
- array('AM','PM'),
- "%e. %B %Y %k:%M",
- "%e. %B %Y",
- "%k:%M",
- ),
-
- 'sk' => array(
- array('nedeľa', 'pondelok', 'utorok', 'streda',
- 'štvrtok', 'piatok', 'sobota'),
- array('január', 'február', 'marec', 'apríl',
- 'máj', 'jún', 'júl', 'august', 'september',
- 'október', 'november', 'december'),
- array('AM','PM'),
- "%e. %B %Y %k:%M",
- "%e. %B %Y",
- "%k:%M",
- ),
- 'jp' => array(
- array('日曜日', '月曜日',
- '火曜日', '水曜日',
- '木曜日', '金曜日',
- '土曜日'),
- array('1','2','3','4','5','6','7','8','9','10','11','12'),
- array('AM','PM'),
- "%Y年%b月%e日 %H:%M",
- "%Y年%b月%e日",
- "%H:%M",
- "%Y年%b月",
- "%b月%e日",
- ),
- 'ja' => array(
- array('日曜日', '月曜日',
- '火曜日', '水曜日',
- '木曜日', '金曜日',
- '土曜日'),
- array('1','2','3','4','5','6','7','8','9','10','11','12'),
- array('AM','PM'),
- "%Y年%b月%e日 %H:%M",
- "%Y年%b月%e日",
- "%H:%M",
- "%Y年%b月",
- "%b月%e日",
- ),
- 'et' => array(
- array('ipühapäev','esmaspäev','teisipäev',
- 'kolmapäev','neljapäev','reede','laupäev'),
- array('jaanuar', 'veebruar', 'märts', 'aprill', 'mai',
- 'juuni', 'juuli', 'august', 'september', 'oktoober',
- 'november', 'detsember'),
- array('AM','PM'),
- "%m.%d.%y %H:%M",
- "%e. %B %Y",
- "%H:%M",
- ),
- 'ru' => array(
- array('???????????','???????????','???????','?????','???????','???????','???????' ),
- array('??????', '???????', '????', '??????', '???', '????',
- '????', '??????', '????????', '???????', '??????',
- '???????'),
- array('AM','PM'),
- "%d.%m.%Y %H:%M",
- "%d.%m.%Y",
- "%H:%M",
- ),
- );
- global $_encode_xml_Map;
- $_encode_xml_Map = array('&' => '&', '"' => '"',
- '<' => '<', '>' => '>',
- '\'' => ''');
- function encode_xml($str, $nocdata = 0) {
- $mt = MT::get_instance();
- global $_encode_xml_Map;
- $cfg_nocdata = $mt->config('NoCDATA');
- $nocdata or (isset($cfg_nocdata) and $nocdata = $cfg_nocdata);
- if ((!$nocdata) && (preg_match('/
- <[^>]+> ## HTML markup
- | ## or
- &(?:(?!(\#([0-9]+)|\#x([0-9a-fA-F]+))).*?);
- ## something that looks like an HTML entity.
- /x', $str))) {
- ## If ]]> exists in the string, encode the > to >.
- $str = preg_replace('/]]>/', ']]>', $str);
- $str = '<![CDATA[' . $str . ']]>';
- } else {
- $str = strtr($str, $_encode_xml_Map);
- $str = preg_replace('/&((\#([0-9]+)|\#x([0-9a-fA-F]+)).*?);/', "&$1;", $str);
- }
- return $str;
- }
- function decode_xml($str) {
- if (preg_match('/<!\[CDATA\[(.*?)]]>/', $str)) {
- $str = preg_replace('/<!\[CDATA\[(.*?)]]>/', '\1', $str);
- ## Decode encoded ]]>
- $str = preg_replace('/]]&(gt|#62);/', ']]>', $str);
- } else {
- global $_encode_xml_Map;
- $str = strtr($str, array_flip($_encode_xml_Map));
- }
- return $str;
- }
- function encode_js($str) {
- if (!isset($str)) return '';
- $str = preg_replace('!\\\\!', '\\\\', $str);
- $str = preg_replace('!>!', '\\>', $str);
- $str = preg_replace('!<!', '\\<', $str);
- $str = preg_replace('!(s)(cript)!i', '$1\\\\$2', $str);
- $str = preg_replace('!</!', '<\\/', $str); # </ is supposed to be the end of Javascript (</script in most UA)
- $str = preg_replace('!\'!', '\\\'', $str);
- $str = preg_replace('!"!', '\\"', $str);
- $str = preg_replace('!\n!', '\\n', $str);
- $str = preg_replace('!\f!', '\\f', $str);
- $str = preg_replace('!\r!', '\\r', $str);
- $str = preg_replace('!\t!', '\\t', $str);
- return $str;
- }
- function gmtime($ts = null) {
- if (!isset($ts)) {
- $ts = time();
- }
- $offset = date('Z', $ts);
- $ts -= $offset;
- $tsa = localtime($ts);
- $tsa[8] = 0;
- return $tsa;
- }
- function is_hash($array) {
- if ( is_array($array) ) {
- if ( array_keys($array) === range(0, count($array) - 1) ) {
- // 0,1,2,3... must be an array
- return false;
- }
- return true;
- }
- return false;
- }
- function offset_time_list($ts, $blog = null, $dir = null) {
- return gmtime(offset_time($ts, $blog, $dir));
- }
- function strip_hyphen($s) {
- return preg_replace('/-+/', '-', $s);
- }
- function first_n_words($text, $n) {
- $text = strip_tags($text);
- $words = preg_split('/\s+/', $text);
- $max = count($words) > $n ? $n : count($words);
- return join(' ', array_slice($words, 0, $max));
- }
- function html_text_transform($str = '') {
- $paras = preg_split('/\r?\n\r?\n/', $str);
- if ($str == '') {
- return '';
- }
- foreach ($paras as $k => $p) {
- 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)) {
- $p = preg_replace('/\r?\n/', "<br />\n", $p);
- $p = "<p>$p</p>";
- $paras[$k] = $p;
- }
- }
- return implode("\n\n", $paras);
- }
- function encode_html($str, $quote_style = ENT_COMPAT) {
- if (!isset($str)) return '';
- $trans_table = get_html_translation_table(HTML_SPECIALCHARS, $quote_style);
- if( $trans_table["'"] != ''' ) { # some versions of PHP match single quotes to '
- $trans_table["'"] = ''';
- }
- return (strtr($str, $trans_table));
- }
- function decode_html($str, $quote_style = ENT_COMPAT) {
- if (!isset($str)) return '';
- $trans_table = get_html_translation_table(HTML_SPECIALCHARS, $quote_style);
- if( $trans_table["'"] != ''' ) { # some versions of PHP match single quotes to '
- $trans_table["'"] = ''';
- }
- return (strtr($str, array_flip($trans_table)));
- }
- function get_category_context(&$ctx, $class = 'category') {
- # Get our hands on the category for the current context
- # Either in MTCategories, a Category Archive Template
- # Or the category for the current entry
- $cat = $ctx->stash('category') or
- $ctx->stash('archive_category');
- if (!isset($cat)) {
- # No category found so far, test the entry
- if ($ctx->stash('entry')) {
- $entry = $ctx->stash('entry');
- $place = $entry->placement();
- if (empty($place))
- return null;
- $cat = $place[0]->category();
- # Return empty string if entry has no category
- # as the tag has been used in the correct context
- # but there is no category to work with
- if (!isset($cat)) {
- return null;
- }
- } else {
- $tag = $ctx->this_tag();
- return $ctx->error("$tag must be used in a category context");
- }
- }
- return $cat;
- }
- function munge_comment($text, $blog) {
- if (!$blog->blog_allow_comment_html) {
- $text = strip_tags($text);
- }
- if ($blog->blog_autolink_urls) {
- $text = preg_replace('!(^|\s|>)(https?://[^\s<]+)!s', '$1<a href="$2">$2</a>', $text);
- }
- return $text;
- }
- function length_text($text) {
- if (!extension_loaded('mbstring')) {
- $len = strlen($text);
- } else {
- $len = mb_strlen($text);
- }
- return $len;
- }
- function substr_text($text, $startpos, $length) {
- if (!extension_loaded('mbstring')) {
- $text = substr($text, $startpos, $length);
- } else {
- $text = mb_substr($text, $startpos, $length);
- }
- return $text;
- }
- function first_n_text($text, $n) {
- if (!isset($lang) || empty($lang)) {
- $mt = MT::get_instance();
- $lang = ($blog && $blog->blog_language ? $blog->blog_language :
- $mt->config('DefaultLanguage'));
- }
- if ($lang == 'jp') {
- $lang = 'ja';
- }
- if ($lang == 'ja') {
- $text = strip_tags($text);
- $text = preg_replace('/\r?\n/', " ", $text);
- return substr_text($text, 0, $n);
- }else{
- return first_n_words($text, $n);
- }
- }
- function tag_split_delim($delim, $str) {
- $delim = quotemeta($delim);
- $tags = array();
- $str = trim($str);
- while (strlen($str) && (preg_match("/^(((['\"])(.*?)\3[^$delim]*?|.*?)($delim\s*|$))/s", $str, $match))) {
- $str = substr($str, strlen($match[1]));
- $tag = (isset($match[4]) && $match[4] != '') ? $match[4] : $match[2];
- $tag = trim($tag);
- $tag = preg_replace('/\s+/', ' ', $tag);
- $n8d_tag = tag_normalize($tag);
- if ($n8d_tag != '')
- if ($tag != '') $tags[] = $tag;
- }
- return $tags;
- }
- function tag_split($str) {
- return tag_split_delim(',', $str);
- }
- function catarray_path_length_sort($a, $b) {
- $al = strlen($a->category_label_path);
- $bl = strlen($bcategory_label_path);
- return $al == $bl ? 0 : $al < $bl ? 1 : -1;
- }
- # sorts by length of category label, from longest to shortest
- function catarray_length_sort($a, $b) {
- $al = strlen($a->category_label);
- $bl = strlen($b->category_label);
- return $al == $bl ? 0 : $al < $bl ? 1 : -1;
- }
- function create_expr_exception($m) {
- if ($m[2])
- return '(0)';
- else
- return $m[1];
- }
- function create_cat_expr_function($expr, &$cats, $param) {
- $mt = MT::get_instance();
- $cats_used = array();
- $orig_expr = $expr;
- $include_children = $param['children'] ? 1 : 0;
- $cats_used = array();
- if (preg_match('/\//', $expr)) {
- foreach ($cats as $id => $cat) {
- $catp = category_label_path($cat);
- $cats[$id]->category_label_path = $catp;
- }
- $cols = array('category_label_path', 'category_label');
- } else {
- $cols = array('category_label');
- }
- foreach ($cols as $col) {
- if ($col == 'category_label_path') {
- usort($cats, 'catarray_path_length_sort');
- } else {
- usort($cats, 'catarray_length_sort');
- }
- $cats_replaced = array();
- foreach ($cats as $cat) {
- $catl = $cat->$col;
- $catid = $cat->category_id;
- $catre = preg_quote($catl, "/");
- if (!preg_match("/(?:(?<![#\d])\[$catre\]|$catre)|(?:#$catid\b)/", $expr))
- continue;
- if ($include_children) {
- $kids = array($cat);
- $child_cats = array();
- while ($c = array_shift($kids)) {
- $child_cats[$c->category_id] = $c;
- $children = $mt->db()->fetch_categories(array('category_id' => $c->category_id, 'children' => 1, 'show_empty' => 1, 'class' => $c->category_class));
- if ($children) {
- foreach ($children as $child) {
- $kids[] = $child;
- }
- }
- }
- $repl = '';
- foreach ($child_cats as $ccid => $cc) {
- $repl .= '||#' . $ccid;
- }
- if (strlen($repl)) $repl = substr($repl, 2);
- $repl = '(' . $repl . ')';
- } else {
- $repl = "(#$catid)";
- }
- if (isset($cats_replaced[$catl])) {
- $last_catid = $cats_replaced[$catl];
- $expr = preg_replace("/(#$last_catid\b)/", '($1 || #' . $catid . ')', $expr);
- } else {
- $expr = preg_replace("/(?:(?<!#)(?:\[$catre\]|$catre))|#$catid\b/", $repl,
- $expr);
- $cats_replaced[$catl] = $catid;
- }
- if ($include_children) {
- foreach ($child_cats as $ccid => $cc)
- $cats_used[$ccid] = $cc;
- } else {
- $cats_used[$catid] = $cat;
- }
- }
- }
- $expr = preg_replace('/\bAND\b/i', '&&', $expr);
- $expr = preg_replace('/\bOR\b/i', '||', $expr);
- $expr = preg_replace('/\bNOT\b/i', '!', $expr);
- $expr = preg_replace_callback('/( |#\d+|&&|\|\||!|\(|\))|([^#0-9&|!()]+)/', 'create_expr_exception', $expr);
- # strip out all the 'ok' stuff. if anything is left, we have
- # some invalid data in our expression:
- $test_expr = preg_replace('/!|&&|\|\||\(0\)|\(|\)|\s|#\d+/', '', $expr);
- if ($test_expr != '') {
- echo "Invalid category filter: $orig_expr";
- return;
- }
- if (!preg_match('/!/', $expr))
- $cats = array_values($cats_used);
- $expr = preg_replace('/#(\d+)/', "array_key_exists('\\1', \$pm)", $expr);
- $expr = '$pm = array_key_exists($e->entry_id, $c["p"]) ? $c["p"][$e->entry_id] : array(); return (' . $expr . ');';
- $fn = create_function('&$e,&$c', $expr);
- if ($fn === FALSE) {
- echo "Invalid category filter: $orig_expr";
- return;
- }
- return $fn;
- }
- function category_label_path($cat) {
- $mt = MT::get_instance();
- $mtdb =& $mt->db;
- if (isset($cat->__label_path))
- return $cat->__label_path;
- $result = preg_match('/\//', $cat->category_label) ? '[' . $cat->category_label . ']' : $cat->category_label;
- while ($cat) {
- $cat = $cat->category_parent ? $mtdb->fetch_category($cat->category_parent) : null;
- if ($cat)
- $result = (preg_match('/\//', $cat->category_label) ? '[' . $cat->category_label . ']' : $cat->category_label) . '/' . $result;
- }
- # caching this information may be problematic IF
- # parent category labels are changed.
- $cat->__label_path = $result;
- return $result;
- }
- function cat_path_to_category($path, $blogs = null, $class = 'category') {
- $mt = MT::get_instance();
- if (!$blogs)
- $blogs = array('include_blogs' => $mt->blog_id());
- if (!is_array($blogs))
- $blogs = array($blogs);
- $mtdb =& $mt->db();
- if (preg_match_all('/(\[[^]]+?\]|[^]\/]+)/', $path, $matches)) {
- # split on slashes, fields quoted by []
- $cat_path = $matches[1];
- for ($i = 0; $i < count($cat_path); $i++) {
- $cat_path[$i] = preg_replace('/^\[(.*)\]$/', '\1', $cat_path[$i]); # remove any []
- }
- $last_cat_ids = array(0);
- foreach ($cat_path as $label) {
- $cats = $mtdb->fetch_categories(array_merge($blogs, array('label' => $label, 'parent' => $last_cat_ids, 'show_empty' => 1, 'class' => $class)));
- if (!$cats)
- break;
- $last_cat_ids = array();
- foreach ($cats as $cat)
- $last_cat_ids[] = $cat->category_id;
- }
- }
- if ($cats)
- return $cats;
- if (!$cats && $path) {
- $cats = $mtdb->fetch_categories(array_merge($blogs, array('label' => $path, 'show_empty' => 1, 'class' => $class)));
- if ($cats)
- return $cats;
- }
- return null;
- }
- # sorts by length of tag name, from longest to shortest
- function tagarray_name_sort($a, $b) {
- return strcmp(strtolower($a->tag_name), strtolower($b->tag_name));
- }
- function tagarray_length_sort($a, $b) {
- $al = strlen($a->tag_name);
- $bl = strlen($b->tag_name);
- return $al == $bl ? 0 : $al < $bl ? 1 : -1;
- }
- function create_tag_expr_function($expr, &$tags, $datasource = 'entry') {
- $tags_used = array();
- $orig_expr = $expr;
-
- # Sort in descending order by length
- usort($tags, 'tagarray_length_sort');
- # Modify the tag argument, replacing the tag name with '#TagID'
- # Create a ID-based hash of the tags that are used in the arg
- foreach ($tags as $tag) {
- $tagn = $tag->tag_name;
- $tagid = $tag->tag_id;
- $oldexpr = $expr;
- $expr = preg_replace("!(?:\Q[$tagn]\E|\Q$tagn\E)!", "#$tagid",
- $expr);
- if ($oldexpr != $expr)
- $tags_used[$tagid] = $tag;
- }
- # Replace logical constructs with their PHP equivalents
- $expr = preg_replace('/\bAND\b/i', '&&', $expr);
- $expr = preg_replace('/\bOR\b/i', '||', $expr);
- $expr = preg_replace('/\bNOT\b/i', '!', $expr);
- # The following is no more readable in PHP than it is in Perl
- $expr = preg_replace_callback('/( |\#\d+|&&|\|\||!|\(|\))|([^#&|!()]+)/', 'create_expr_exception', $expr);
- # Syntax check on 'tag' argument
- # Strip out all the valid stuff. if anything is left, we have
- # some invalid data in our expression
- $test_expr = preg_replace('/!|&&|\|\||\(0\)|\(|\)|\s|#\d+/', '', $expr);
- if ($test_expr != '') {
- echo "Invalid tag filter: $orig_expr";
- return;
- }
- # Populate array (passed in by reference) of used tags
- $tags = array_values($tags_used);
- # Replace '#TagID' with a hash lookup function.
- # Function confirms/denies use of tag on entry (by IDs)
- $column_name = $datasource . '_id';
- $expr = preg_replace('/#(\d+)/', "array_key_exists('\\1', \$tm)", $expr);
- # Create a PHP-blessed function of that code and return it
- # if all is well. This function will be used later to
- # test for existence of specified tags in entries.
- $expr = '$tm = array_key_exists($e->'.$datasource.'_id, $c["t"]) ? $c["t"][$e->'.$datasource.'_id] : array(); return ' . $expr .
- ';';
- $fn = create_function('&$e,&$c', $expr);
- if ($fn === FALSE) {
- echo "Invalid tag filter: $orig_expr";
- return;
- }
- return $fn;
- }
- function tag_normalize($str) {
- # FIXME: character set issues here...
- $private = preg_match('/^@/', $str) ? 1 : 0;
- $str = preg_replace('/[@!`\\<>\*&#\/~\?\'"\.\,=\(\)\${}\[\];:\ \+\-\r\n]+/', '', $str);
- $str = strtolower($str);
- if ($private) $str = '@' . $str;
- return $str;
- }
- function static_path($host) {
- $mt = MT::get_instance();
- $path = $mt->config('StaticWebP…
Large files files are truncated, but you can click here to view the full file