5 * A PHP-Based RSS and Atom Feed Framework.
6 * Takes the hard work out of managing a complete RSS/Atom solution.
8 * Copyright (c) 2004-2012, Ryan Parman, Geoffrey Sneddon, Ryan McCue, and contributors
11 * Redistribution and use in source and binary forms, with or without modification, are
12 * permitted provided that the following conditions are met:
14 * * Redistributions of source code must retain the above copyright notice, this list of
15 * conditions and the following disclaimer.
17 * * Redistributions in binary form must reproduce the above copyright notice, this list
18 * of conditions and the following disclaimer in the documentation and/or other materials
19 * provided with the distribution.
21 * * Neither the name of the SimplePie Team nor the names of its contributors may be used
22 * to endorse or promote products derived from this software without specific prior
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
26 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
27 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS
28 * AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
37 * @copyright 2004-2012 Ryan Parman, Geoffrey Sneddon, Ryan McCue
39 * @author Geoffrey Sneddon
41 * @link http://simplepie.org/ SimplePie
42 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
46 * Miscellanous utilities
52 public static function time_hms($seconds)
56 $hours = floor($seconds / 3600);
57 $remainder = $seconds % 3600;
63 $minutes = floor($remainder / 60);
64 $seconds = $remainder % 60;
65 if ($minutes < 10 && $hours > 0)
67 $minutes = '0' . $minutes;
71 $seconds = '0' . $seconds;
74 $time .= $minutes.':';
80 public static function absolutize_url($relative, $base)
82 $iri = SimplePie_IRI::absolutize(new SimplePie_IRI($base), $relative);
87 return $iri->get_uri();
91 * Get a HTML/XML element from a HTML string
93 * @deprecated Use DOMDocument instead (parsing HTML with regex is bad!)
94 * @param string $realname Element name (including namespace prefix if applicable)
95 * @param string $string HTML document
98 public static function get_element($realname, $string)
101 $name = preg_quote($realname, '/');
102 if (preg_match_all("/<($name)" . SIMPLEPIE_PCRE_HTML_ATTRIBUTE . "(>(.*)<\/$name>|(\/)?>)/siU", $string, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE))
104 for ($i = 0, $total_matches = count($matches); $i < $total_matches; $i++)
106 $return[$i]['tag'] = $realname;
107 $return[$i]['full'] = $matches[$i][0][0];
108 $return[$i]['offset'] = $matches[$i][0][1];
109 if (strlen($matches[$i][3][0]) <= 2)
111 $return[$i]['self_closing'] = true;
115 $return[$i]['self_closing'] = false;
116 $return[$i]['content'] = $matches[$i][4][0];
118 $return[$i]['attribs'] = array();
119 if (isset($matches[$i][2][0]) && preg_match_all('/[\x09\x0A\x0B\x0C\x0D\x20]+([^\x09\x0A\x0B\x0C\x0D\x20\x2F\x3E][^\x09\x0A\x0B\x0C\x0D\x20\x2F\x3D\x3E]*)(?:[\x09\x0A\x0B\x0C\x0D\x20]*=[\x09\x0A\x0B\x0C\x0D\x20]*(?:"([^"]*)"|\'([^\']*)\'|([^\x09\x0A\x0B\x0C\x0D\x20\x22\x27\x3E][^\x09\x0A\x0B\x0C\x0D\x20\x3E]*)?))?/', ' ' . $matches[$i][2][0] . ' ', $attribs, PREG_SET_ORDER))
121 for ($j = 0, $total_attribs = count($attribs); $j < $total_attribs; $j++)
123 if (count($attribs[$j]) === 2)
125 $attribs[$j][2] = $attribs[$j][1];
127 $return[$i]['attribs'][strtolower($attribs[$j][1])]['data'] = SimplePie_Misc::entities_decode(end($attribs[$j]));
135 public static function element_implode($element)
137 $full = "<$element[tag]";
138 foreach ($element['attribs'] as $key => $value)
140 $key = strtolower($key);
141 $full .= " $key=\"" . htmlspecialchars($value['data']) . '"';
143 if ($element['self_closing'])
149 $full .= ">$element[content]</$element[tag]>";
154 public static function error($message, $level, $file, $line)
156 if ((ini_get('error_reporting') & $level) > 0)
164 $note = 'PHP Warning';
167 $note = 'PHP Notice';
170 $note = 'Unknown Error';
175 if (!function_exists('error_log'))
180 $log_file = @ini_get('error_log');
181 if (!empty($log_file) && ('syslog' !== $log_file) && !@is_writable($log_file))
188 @error_log("$note: $message in $file on line $line", 0);
195 public static function fix_protocol($url, $http = 1)
197 $url = SimplePie_Misc::normalize_url($url);
198 $parsed = SimplePie_Misc::parse_url($url);
199 if ($parsed['scheme'] !== '' && $parsed['scheme'] !== 'http' && $parsed['scheme'] !== 'https')
201 return SimplePie_Misc::fix_protocol(SimplePie_Misc::compress_parse_url('http', $parsed['authority'], $parsed['path'], $parsed['query'], $parsed['fragment']), $http);
204 if ($parsed['scheme'] === '' && $parsed['authority'] === '' && !file_exists($url))
206 return SimplePie_Misc::fix_protocol(SimplePie_Misc::compress_parse_url('http', $parsed['path'], '', $parsed['query'], $parsed['fragment']), $http);
209 if ($http === 2 && $parsed['scheme'] !== '')
213 elseif ($http === 3 && strtolower($parsed['scheme']) === 'http')
215 return substr_replace($url, 'podcast', 0, 4);
217 elseif ($http === 4 && strtolower($parsed['scheme']) === 'http')
219 return substr_replace($url, 'itpc', 0, 4);
227 public static function parse_url($url)
229 $iri = new SimplePie_IRI($url);
231 'scheme' => (string) $iri->scheme,
232 'authority' => (string) $iri->authority,
233 'path' => (string) $iri->path,
234 'query' => (string) $iri->query,
235 'fragment' => (string) $iri->fragment
239 public static function compress_parse_url($scheme = '', $authority = '', $path = '', $query = '', $fragment = '')
241 $iri = new SimplePie_IRI('');
242 $iri->scheme = $scheme;
243 $iri->authority = $authority;
245 $iri->query = $query;
246 $iri->fragment = $fragment;
247 return $iri->get_uri();
250 public static function normalize_url($url)
252 $iri = new SimplePie_IRI($url);
253 return $iri->get_uri();
256 public static function percent_encoding_normalization($match)
258 $integer = hexdec($match[1]);
259 if ($integer >= 0x41 && $integer <= 0x5A || $integer >= 0x61 && $integer <= 0x7A || $integer >= 0x30 && $integer <= 0x39 || $integer === 0x2D || $integer === 0x2E || $integer === 0x5F || $integer === 0x7E)
261 return chr($integer);
265 return strtoupper($match[0]);
270 * Converts a Windows-1252 encoded string to a UTF-8 encoded string
273 * @param string $string Windows-1252 encoded string
274 * @return string UTF-8 encoded string
276 public static function windows_1252_to_utf8($string)
278 static $convert_table = array("\x80" => "\xE2\x82\xAC", "\x81" => "\xEF\xBF\xBD", "\x82" => "\xE2\x80\x9A", "\x83" => "\xC6\x92", "\x84" => "\xE2\x80\x9E", "\x85" => "\xE2\x80\xA6", "\x86" => "\xE2\x80\xA0", "\x87" => "\xE2\x80\xA1", "\x88" => "\xCB\x86", "\x89" => "\xE2\x80\xB0", "\x8A" => "\xC5\xA0", "\x8B" => "\xE2\x80\xB9", "\x8C" => "\xC5\x92", "\x8D" => "\xEF\xBF\xBD", "\x8E" => "\xC5\xBD", "\x8F" => "\xEF\xBF\xBD", "\x90" => "\xEF\xBF\xBD", "\x91" => "\xE2\x80\x98", "\x92" => "\xE2\x80\x99", "\x93" => "\xE2\x80\x9C", "\x94" => "\xE2\x80\x9D", "\x95" => "\xE2\x80\xA2", "\x96" => "\xE2\x80\x93", "\x97" => "\xE2\x80\x94", "\x98" => "\xCB\x9C", "\x99" => "\xE2\x84\xA2", "\x9A" => "\xC5\xA1", "\x9B" => "\xE2\x80\xBA", "\x9C" => "\xC5\x93", "\x9D" => "\xEF\xBF\xBD", "\x9E" => "\xC5\xBE", "\x9F" => "\xC5\xB8", "\xA0" => "\xC2\xA0", "\xA1" => "\xC2\xA1", "\xA2" => "\xC2\xA2", "\xA3" => "\xC2\xA3", "\xA4" => "\xC2\xA4", "\xA5" => "\xC2\xA5", "\xA6" => "\xC2\xA6", "\xA7" => "\xC2\xA7", "\xA8" => "\xC2\xA8", "\xA9" => "\xC2\xA9", "\xAA" => "\xC2\xAA", "\xAB" => "\xC2\xAB", "\xAC" => "\xC2\xAC", "\xAD" => "\xC2\xAD", "\xAE" => "\xC2\xAE", "\xAF" => "\xC2\xAF", "\xB0" => "\xC2\xB0", "\xB1" => "\xC2\xB1", "\xB2" => "\xC2\xB2", "\xB3" => "\xC2\xB3", "\xB4" => "\xC2\xB4", "\xB5" => "\xC2\xB5", "\xB6" => "\xC2\xB6", "\xB7" => "\xC2\xB7", "\xB8" => "\xC2\xB8", "\xB9" => "\xC2\xB9", "\xBA" => "\xC2\xBA", "\xBB" => "\xC2\xBB", "\xBC" => "\xC2\xBC", "\xBD" => "\xC2\xBD", "\xBE" => "\xC2\xBE", "\xBF" => "\xC2\xBF", "\xC0" => "\xC3\x80", "\xC1" => "\xC3\x81", "\xC2" => "\xC3\x82", "\xC3" => "\xC3\x83", "\xC4" => "\xC3\x84", "\xC5" => "\xC3\x85", "\xC6" => "\xC3\x86", "\xC7" => "\xC3\x87", "\xC8" => "\xC3\x88", "\xC9" => "\xC3\x89", "\xCA" => "\xC3\x8A", "\xCB" => "\xC3\x8B", "\xCC" => "\xC3\x8C", "\xCD" => "\xC3\x8D", "\xCE" => "\xC3\x8E", "\xCF" => "\xC3\x8F", "\xD0" => "\xC3\x90", "\xD1" => "\xC3\x91", "\xD2" => "\xC3\x92", "\xD3" => "\xC3\x93", "\xD4" => "\xC3\x94", "\xD5" => "\xC3\x95", "\xD6" => "\xC3\x96", "\xD7" => "\xC3\x97", "\xD8" => "\xC3\x98", "\xD9" => "\xC3\x99", "\xDA" => "\xC3\x9A", "\xDB" => "\xC3\x9B", "\xDC" => "\xC3\x9C", "\xDD" => "\xC3\x9D", "\xDE" => "\xC3\x9E", "\xDF" => "\xC3\x9F", "\xE0" => "\xC3\xA0", "\xE1" => "\xC3\xA1", "\xE2" => "\xC3\xA2", "\xE3" => "\xC3\xA3", "\xE4" => "\xC3\xA4", "\xE5" => "\xC3\xA5", "\xE6" => "\xC3\xA6", "\xE7" => "\xC3\xA7", "\xE8" => "\xC3\xA8", "\xE9" => "\xC3\xA9", "\xEA" => "\xC3\xAA", "\xEB" => "\xC3\xAB", "\xEC" => "\xC3\xAC", "\xED" => "\xC3\xAD", "\xEE" => "\xC3\xAE", "\xEF" => "\xC3\xAF", "\xF0" => "\xC3\xB0", "\xF1" => "\xC3\xB1", "\xF2" => "\xC3\xB2", "\xF3" => "\xC3\xB3", "\xF4" => "\xC3\xB4", "\xF5" => "\xC3\xB5", "\xF6" => "\xC3\xB6", "\xF7" => "\xC3\xB7", "\xF8" => "\xC3\xB8", "\xF9" => "\xC3\xB9", "\xFA" => "\xC3\xBA", "\xFB" => "\xC3\xBB", "\xFC" => "\xC3\xBC", "\xFD" => "\xC3\xBD", "\xFE" => "\xC3\xBE", "\xFF" => "\xC3\xBF");
280 return strtr($string, $convert_table);
284 * Change a string from one encoding to another
286 * @param string $data Raw data in $input encoding
287 * @param string $input Encoding of $data
288 * @param string $output Encoding you want
289 * @return string|boolean False if we can't convert it
291 public static function change_encoding($data, $input, $output)
293 $input = SimplePie_Misc::encoding($input);
294 $output = SimplePie_Misc::encoding($output);
296 // We fail to fail on non US-ASCII bytes
297 if ($input === 'US-ASCII')
299 static $non_ascii_octects = '';
300 if (!$non_ascii_octects)
302 for ($i = 0x80; $i <= 0xFF; $i++)
304 $non_ascii_octects .= chr($i);
307 $data = substr($data, 0, strcspn($data, $non_ascii_octects));
310 // This is first, as behaviour of this is completely predictable
311 if ($input === 'windows-1252' && $output === 'UTF-8')
313 return SimplePie_Misc::windows_1252_to_utf8($data);
315 // This is second, as behaviour of this varies only with PHP version (the middle part of this expression checks the encoding is supported).
316 elseif (function_exists('mb_convert_encoding') && ($return = SimplePie_Misc::change_encoding_mbstring($data, $input, $output)))
320 // This is last, as behaviour of this varies with OS userland and PHP version
321 elseif (function_exists('iconv') && ($return = SimplePie_Misc::change_encoding_iconv($data, $input, $output)))
325 // If we can't do anything, just fail
332 protected static function change_encoding_mbstring($data, $input, $output)
334 if ($input === 'windows-949')
338 if ($output === 'windows-949')
342 if ($input === 'Windows-31J')
346 if ($output === 'Windows-31J')
351 // Check that the encoding is supported
352 if (@mb_convert_encoding("\x80", 'UTF-16BE', $input) === "\x00\x80")
356 if (!in_array($input, mb_list_encodings()))
361 // Let's do some conversion
362 if ($return = @mb_convert_encoding($data, $output, $input))
370 protected static function change_encoding_iconv($data, $input, $output)
372 return @iconv($input, $output, $data);
376 * Normalize an encoding name
378 * This is automatically generated by create.php
380 * To generate it, run `php create.php` on the command line, and copy the
381 * output to replace this function.
383 * @param string $charset Character set to standardise
384 * @return string Standardised name
386 public static function encoding($charset)
388 // Normalization from UTS #22
389 switch (strtolower(preg_replace('/(?:[^a-zA-Z0-9]+|([^0-9])0+)/', '\1', $charset)))
391 case 'adobestandardencoding':
392 case 'csadobestandardencoding':
393 return 'Adobe-Standard-Encoding';
395 case 'adobesymbolencoding':
397 return 'Adobe-Symbol-Encoding';
403 case 'ansix31101983':
405 case 'csiso99naplps':
408 return 'ANSI_X3.110-1983';
412 case 'csiso89asmo449':
433 case 'csiso4unitedkingdom':
441 case 'csiso47bsviewdata':
443 return 'BS_viewdata';
451 case 'csaz243419851':
452 case 'csiso121canadian1':
455 return 'CSA_Z243.4-1985-1';
458 case 'csaz243419852':
459 case 'csiso122canadian2':
462 return 'CSA_Z243.4-1985-2';
464 case 'csaz24341985gr':
465 case 'csiso123csaz24341985gr':
467 return 'CSA_Z243.4-1985-gr';
469 case 'csiso139csn369103':
479 case 'csiso21german':
490 case 'csiso646danish':
496 case 'csibmebcdicatde':
498 return 'EBCDIC-AT-DE';
500 case 'csebcdicatdea':
502 return 'EBCDIC-AT-DE-A';
506 return 'EBCDIC-CA-FR';
510 return 'EBCDIC-DK-NO';
512 case 'csebcdicdknoa':
514 return 'EBCDIC-DK-NO-A';
522 return 'EBCDIC-ES-A';
526 return 'EBCDIC-ES-S';
530 return 'EBCDIC-FI-SE';
532 case 'csebcdicfisea':
534 return 'EBCDIC-FI-SE-A';
556 case 'csiso111ecmacyrillic':
560 return 'ECMA-cyrillic';
562 case 'csiso17spanish':
568 case 'csiso85spanish2':
574 case 'cseucpkdfmtjapanese':
576 case 'extendedunixcodepackedformatforjapanese':
579 case 'cseucfixwidjapanese':
580 case 'extendedunixcodefixedwidthforjapanese':
581 return 'Extended_UNIX_Code_Fixed_Width_for_Japanese';
589 case 'csiso58gb231280':
599 case 'csiso57gb1988':
605 case 'csiso153gost1976874':
609 return 'GOST_19768-74';
612 case 'csiso150greekccitt':
615 return 'greek-ccitt';
617 case 'csiso88greek7':
622 case 'csiso18greek7old':
654 return 'IBM-Symbols';
761 case 'cspc8codepage437':
773 case 'cspc775baltic':
779 case 'cspc850multilingual':
810 case 'pcmultilingual850euro':
828 case 'cspc862latinhebrew':
870 case 'ebcdiccproece':
883 case 'ebcdiccyrillic':
917 case 'ebcdiclatin9euro':
931 case 'ebcdicus37euro':
937 case 'ebcdicde273euro':
943 case 'ebcdicdk277euro':
944 case 'ebcdicno277euro':
950 case 'ebcdicfi278euro':
951 case 'ebcdicse278euro':
957 case 'ebcdicit280euro':
963 case 'ebcdices284euro':
969 case 'ebcdicgb285euro':
975 case 'ebcdicfr297euro':
981 case 'ebcdicinternational500euro':
987 case 'ebcdicis871euro':
991 case 'csiso143iecp271':
1001 case 'csiso50inis8':
1006 case 'csiso51iniscyrillic':
1007 case 'iniscyrillic':
1009 return 'INIS-cyrillic';
1016 return 'ISO-2022-CN';
1018 case 'iso2022cnext':
1019 return 'ISO-2022-CN-EXT';
1023 return 'ISO-2022-JP';
1025 case 'csiso2022jp2':
1027 return 'ISO-2022-JP-2';
1031 return 'ISO-2022-KR';
1033 case 'cswindows30latin1':
1034 case 'iso88591windows30latin1':
1035 return 'ISO-8859-1-Windows-3.0-Latin-1';
1037 case 'cswindows31latin1':
1038 case 'iso88591windows31latin1':
1039 return 'ISO-8859-1-Windows-3.1-Latin-1';
1043 case 'iso885921987':
1047 return 'ISO-8859-2';
1049 case 'cswindows31latin2':
1050 case 'iso88592windowslatin2':
1051 return 'ISO-8859-2-Windows-Latin-2';
1055 case 'iso885931988':
1059 return 'ISO-8859-3';
1063 case 'iso885941988':
1067 return 'ISO-8859-4';
1069 case 'csisolatincyrillic':
1072 case 'iso885951988':
1074 return 'ISO-8859-5';
1078 case 'csisolatinarabic':
1081 case 'iso885961987':
1083 return 'ISO-8859-6';
1087 return 'ISO-8859-6-E';
1091 return 'ISO-8859-6-I';
1093 case 'csisolatingreek':
1099 case 'iso885971987':
1101 return 'ISO-8859-7';
1103 case 'csisolatinhebrew':
1106 case 'iso885981988':
1108 return 'ISO-8859-8';
1112 return 'ISO-8859-8-E';
1116 return 'ISO-8859-8-I';
1118 case 'cswindows31latin5':
1119 case 'iso88599windowslatin5':
1120 return 'ISO-8859-9-Windows-Latin-5';
1124 case 'iso8859101992':
1128 return 'ISO-8859-10';
1131 return 'ISO-8859-13';
1134 case 'iso8859141998':
1139 return 'ISO-8859-14';
1143 return 'ISO-8859-15';
1146 case 'iso8859162001':
1150 return 'ISO-8859-16';
1153 return 'ISO-10646-J-1';
1156 case 'iso10646ucs2':
1157 return 'ISO-10646-UCS-2';
1160 case 'iso10646ucs4':
1161 return 'ISO-10646-UCS-4';
1163 case 'csunicodeascii':
1164 case 'iso10646ucsbasic':
1165 return 'ISO-10646-UCS-Basic';
1167 case 'csunicodelatin1':
1169 case 'iso10646unicodelatin1':
1170 return 'ISO-10646-Unicode-Latin1';
1172 case 'csiso10646utf1':
1173 case 'iso10646utf1':
1174 return 'ISO-10646-UTF-1';
1179 return 'ISO-11548-1';
1185 case 'csunicodeibm1261':
1186 case 'isounicodeibm1261':
1187 return 'ISO-Unicode-IBM-1261';
1189 case 'csunicodeibm1264':
1190 case 'isounicodeibm1264':
1191 return 'ISO-Unicode-IBM-1264';
1193 case 'csunicodeibm1265':
1194 case 'isounicodeibm1265':
1195 return 'ISO-Unicode-IBM-1265';
1197 case 'csunicodeibm1268':
1198 case 'isounicodeibm1268':
1199 return 'ISO-Unicode-IBM-1268';
1201 case 'csunicodeibm1276':
1202 case 'isounicodeibm1276':
1203 return 'ISO-Unicode-IBM-1276';
1205 case 'csiso646basic1983':
1206 case 'iso646basic1983':
1208 return 'ISO_646.basic:1983';
1210 case 'csiso2intlrefversion':
1212 case 'iso646irv1983':
1214 return 'ISO_646.irv:1983';
1220 return 'ISO_2033-1983';
1222 case 'csiso5427cyrillic':
1227 case 'iso5427cyrillic1981':
1230 return 'ISO_5427:1981';
1232 case 'csiso5428greek':
1235 return 'ISO_5428:1980';
1237 case 'csiso6937add':
1240 return 'ISO_6937-2-25';
1242 case 'csisotextcomm':
1245 return 'ISO_6937-2-add';
1247 case 'csiso8859supp':
1251 return 'ISO_8859-supp';
1253 case 'csiso10367box':
1256 return 'ISO_10367-box';
1258 case 'csiso15italian':
1264 case 'csiso13jisc6220jp':
1266 case 'jisc62201969':
1267 case 'jisc62201969jp':
1270 return 'JIS_C6220-1969-jp';
1272 case 'csiso14jisc6220ro':
1275 case 'jisc62201969ro':
1277 return 'JIS_C6220-1969-ro';
1279 case 'csiso42jisc62261978':
1281 case 'jisc62261978':
1282 return 'JIS_C6226-1978';
1284 case 'csiso87jisx208':
1286 case 'jisc62261983':
1289 return 'JIS_C6226-1983';
1291 case 'csiso91jisc62291984a':
1293 case 'jisc62291984a':
1295 return 'JIS_C6229-1984-a';
1297 case 'csiso92jisc62991984b':
1298 case 'iso646jpocrb':
1300 case 'jisc62291984b':
1302 return 'JIS_C6229-1984-b';
1304 case 'csiso93jis62291984badd':
1306 case 'jisc62291984badd':
1308 return 'JIS_C6229-1984-b-add';
1310 case 'csiso94jis62291984hand':
1312 case 'jisc62291984hand':
1314 return 'JIS_C6229-1984-hand';
1316 case 'csiso95jis62291984handadd':
1318 case 'jisc62291984handadd':
1319 case 'jpocrhandadd':
1320 return 'JIS_C6229-1984-hand-add';
1322 case 'csiso96jisc62291984kana':
1324 case 'jisc62291984kana':
1325 return 'JIS_C6229-1984-kana';
1327 case 'csjisencoding':
1329 return 'JIS_Encoding';
1331 case 'cshalfwidthkatakana':
1336 case 'csiso159jisx2121990':
1340 return 'JIS_X0212-1990';
1342 case 'csiso141jusib1002':
1348 return 'JUS_I.B1.002';
1350 case 'csiso147macedonian':
1352 case 'jusib1003mac':
1354 return 'JUS_I.B1.003-mac';
1356 case 'csiso146serbian':
1358 case 'jusib1003serb':
1360 return 'JUS_I.B1.003-serb';
1362 case 'koi7switched':
1363 return 'KOI7-switched';
1380 case 'strk10482002':
1383 case 'csiso19latingreek':
1386 return 'latin-greek';
1388 case 'csiso27latingreek1':
1391 return 'Latin-greek-1';
1404 case 'csmicrosoftpublishing':
1405 case 'microsoftpublishing':
1406 return 'Microsoft-Publishing';
1416 case 'csiso86hungarian':
1421 return 'MSZ_7795.3';
1428 case 'csnatsdanoadd':
1431 return 'NATS-DANO-ADD';
1438 case 'csnatssefiadd':
1441 return 'NATS-SEFI-ADD';
1443 case 'csiso151cuba':
1448 return 'NC_NC00-10:81';
1450 case 'csiso69french':
1455 return 'NF_Z_62-010';
1457 case 'csiso25french':
1460 case 'nfz620101973':
1461 return 'NF_Z_62-010_(1973)';
1463 case 'csiso60danishnorwegian':
1464 case 'csiso60norwegian1':
1471 case 'csiso61norwegian2':
1478 case 'osdebcdicdf3irv':
1479 return 'OSD_EBCDIC_DF03_IRV';
1481 case 'osdebcdicdf41':
1482 return 'OSD_EBCDIC_DF04_1';
1484 case 'osdebcdicdf415':
1485 return 'OSD_EBCDIC_DF04_15';
1487 case 'cspc8danishnorwegian':
1488 case 'pc8danishnorwegian':
1489 return 'PC8-Danish-Norwegian';
1491 case 'cspc8turkish':
1493 return 'PC8-Turkish';
1495 case 'csiso16portuguese':
1501 case 'csiso84portuguese2':
1509 case 'cyrillicasian':
1517 case 'csiso10swedish':
1524 return 'SEN_850200_B';
1526 case 'csiso11swedishfornames':
1531 return 'SEN_850200_C';
1533 case 'csiso102t617bit':
1538 case 'csiso103t618bit':
1544 case 'csiso128t101g2':
1555 return 'UNICODE-1-1';
1557 case 'csunicode11utf7':
1558 case 'unicode11utf7':
1559 return 'UNICODE-1-1-UTF-7';
1561 case 'csunknown8bit':
1563 return 'UNKNOWN-8BIT';
1571 case 'iso646irv1991':
1606 case 'csventurainternational':
1607 case 'venturainternational':
1608 return 'Ventura-International';
1610 case 'csventuramath':
1612 return 'Ventura-Math';
1616 return 'Ventura-US';
1618 case 'csiso70videotexsupp1':
1620 case 'videotexsuppl':
1621 return 'videotex-suppl';
1632 case 'cswindows31j':
1636 return 'Windows-31J';
1640 return 'windows-874';
1643 case 'csksc56011987':
1651 return 'windows-949';
1654 return 'windows-1250';
1657 return 'windows-1251';
1663 case 'iso885911987':
1668 return 'windows-1252';
1671 return 'windows-1253';
1675 case 'iso885991989':
1680 return 'windows-1254';
1683 return 'windows-1255';
1686 return 'windows-1256';
1689 return 'windows-1257';
1692 return 'windows-1258';
1699 public static function get_curl_version()
1701 if (is_array($curl = curl_version()))
1703 $curl = $curl['version'];
1705 elseif (substr($curl, 0, 5) === 'curl/')
1707 $curl = substr($curl, 5, strcspn($curl, "\x09\x0A\x0B\x0C\x0D", 5));
1709 elseif (substr($curl, 0, 8) === 'libcurl/')
1711 $curl = substr($curl, 8, strcspn($curl, "\x09\x0A\x0B\x0C\x0D", 8));
1721 * Strip HTML comments
1723 * @param string $data Data to strip comments from
1724 * @return string Comment stripped string
1726 public static function strip_comments($data)
1729 while (($start = strpos($data, '<!--')) !== false)
1731 $output .= substr($data, 0, $start);
1732 if (($end = strpos($data, '-->', $start)) !== false)
1734 $data = substr_replace($data, '', 0, $end + 3);
1741 return $output . $data;
1744 public static function parse_date($dt)
1746 $parser = SimplePie_Parse_Date::get();
1747 return $parser->parse($dt);
1751 * Decode HTML entities
1753 * @deprecated Use DOMDocument instead
1754 * @param string $data Input data
1755 * @return string Output data
1757 public static function entities_decode($data)
1759 $decoder = new SimplePie_Decode_HTML_Entities($data);
1760 return $decoder->parse();
1764 * Remove RFC822 comments
1766 * @param string $data Data to strip comments from
1767 * @return string Comment stripped string
1769 public static function uncomment_rfc822($string)
1771 $string = (string) $string;
1773 $length = strlen($string);
1778 while ($position < $length && ($pos = strpos($string, '(', $position)) !== false)
1780 $output .= substr($string, $position, $pos - $position);
1781 $position = $pos + 1;
1782 if ($string[$pos - 1] !== '\\')
1785 while ($depth && $position < $length)
1787 $position += strcspn($string, '()', $position);
1788 if ($string[$position - 1] === '\\')
1793 elseif (isset($string[$position]))
1795 switch ($string[$position])
1818 $output .= substr($string, $position);
1823 public static function parse_mime($mime)
1825 if (($pos = strpos($mime, ';')) === false)
1831 return trim(substr($mime, 0, $pos));
1835 public static function atom_03_construct_type($attribs)
1837 if (isset($attribs['']['mode']) && strtolower(trim($attribs['']['mode']) === 'base64'))
1839 $mode = SIMPLEPIE_CONSTRUCT_BASE64;
1843 $mode = SIMPLEPIE_CONSTRUCT_NONE;
1845 if (isset($attribs['']['type']))
1847 switch (strtolower(trim($attribs['']['type'])))
1851 return SIMPLEPIE_CONSTRUCT_TEXT | $mode;
1855 return SIMPLEPIE_CONSTRUCT_HTML | $mode;
1858 case 'application/xhtml+xml':
1859 return SIMPLEPIE_CONSTRUCT_XHTML | $mode;
1862 return SIMPLEPIE_CONSTRUCT_NONE | $mode;
1867 return SIMPLEPIE_CONSTRUCT_TEXT | $mode;
1871 public static function atom_10_construct_type($attribs)
1873 if (isset($attribs['']['type']))
1875 switch (strtolower(trim($attribs['']['type'])))
1878 return SIMPLEPIE_CONSTRUCT_TEXT;
1881 return SIMPLEPIE_CONSTRUCT_HTML;
1884 return SIMPLEPIE_CONSTRUCT_XHTML;
1887 return SIMPLEPIE_CONSTRUCT_NONE;
1890 return SIMPLEPIE_CONSTRUCT_TEXT;
1893 public static function atom_10_content_construct_type($attribs)
1895 if (isset($attribs['']['type']))
1897 $type = strtolower(trim($attribs['']['type']));
1901 return SIMPLEPIE_CONSTRUCT_TEXT;
1904 return SIMPLEPIE_CONSTRUCT_HTML;
1907 return SIMPLEPIE_CONSTRUCT_XHTML;
1909 if (in_array(substr($type, -4), array('+xml', '/xml')) || substr($type, 0, 5) === 'text/')
1911 return SIMPLEPIE_CONSTRUCT_NONE;
1915 return SIMPLEPIE_CONSTRUCT_BASE64;
1920 return SIMPLEPIE_CONSTRUCT_TEXT;
1924 public static function is_isegment_nz_nc($string)
1926 return (bool) preg_match('/^([A-Za-z0-9\-._~\x{A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}\x{10000}-\x{1FFFD}\x{20000}-\x{2FFFD}\x{30000}-\x{3FFFD}\x{40000}-\x{4FFFD}\x{50000}-\x{5FFFD}\x{60000}-\x{6FFFD}\x{70000}-\x{7FFFD}\x{80000}-\x{8FFFD}\x{90000}-\x{9FFFD}\x{A0000}-\x{AFFFD}\x{B0000}-\x{BFFFD}\x{C0000}-\x{CFFFD}\x{D0000}-\x{DFFFD}\x{E1000}-\x{EFFFD}!$&\'()*+,;=@]|(%[0-9ABCDEF]{2}))+$/u', $string);
1929 public static function space_seperated_tokens($string)
1931 $space_characters = "\x20\x09\x0A\x0B\x0C\x0D";
1932 $string_length = strlen($string);
1934 $position = strspn($string, $space_characters);
1937 while ($position < $string_length)
1939 $len = strcspn($string, $space_characters, $position);
1940 $tokens[] = substr($string, $position, $len);
1942 $position += strspn($string, $space_characters, $position);
1949 * Converts a unicode codepoint to a UTF-8 character
1952 * @param int $codepoint Unicode codepoint
1953 * @return string UTF-8 character
1955 public static function codepoint_to_utf8($codepoint)
1957 $codepoint = (int) $codepoint;
1962 else if ($codepoint <= 0x7f)
1964 return chr($codepoint);
1966 else if ($codepoint <= 0x7ff)
1968 return chr(0xc0 | ($codepoint >> 6)) . chr(0x80 | ($codepoint & 0x3f));
1970 else if ($codepoint <= 0xffff)
1972 return chr(0xe0 | ($codepoint >> 12)) . chr(0x80 | (($codepoint >> 6) & 0x3f)) . chr(0x80 | ($codepoint & 0x3f));
1974 else if ($codepoint <= 0x10ffff)
1976 return chr(0xf0 | ($codepoint >> 18)) . chr(0x80 | (($codepoint >> 12) & 0x3f)) . chr(0x80 | (($codepoint >> 6) & 0x3f)) . chr(0x80 | ($codepoint & 0x3f));
1980 // U+FFFD REPLACEMENT CHARACTER
1981 return "\xEF\xBF\xBD";
1986 * Similar to parse_str()
1988 * Returns an associative array of name/value pairs, where the value is an
1989 * array of values that have used the same name
1992 * @param string $str The input string.
1995 public static function parse_str($str)
1998 $str = explode('&', $str);
2000 foreach ($str as $section)
2002 if (strpos($section, '=') !== false)
2004 list($name, $value) = explode('=', $section, 2);
2005 $return[urldecode($name)][] = urldecode($value);
2009 $return[urldecode($section)][] = null;
2017 * Detect XML encoding, as per XML 1.0 Appendix F.1
2019 * @todo Add support for EBCDIC
2020 * @param string $data XML data
2021 * @param SimplePie_Registry $registry Class registry
2022 * @return array Possible encodings
2024 public static function xml_encoding($data, $registry)
2026 // UTF-32 Big Endian BOM
2027 if (substr($data, 0, 4) === "\x00\x00\xFE\xFF")
2029 $encoding[] = 'UTF-32BE';
2031 // UTF-32 Little Endian BOM
2032 elseif (substr($data, 0, 4) === "\xFF\xFE\x00\x00")
2034 $encoding[] = 'UTF-32LE';
2036 // UTF-16 Big Endian BOM
2037 elseif (substr($data, 0, 2) === "\xFE\xFF")
2039 $encoding[] = 'UTF-16BE';
2041 // UTF-16 Little Endian BOM
2042 elseif (substr($data, 0, 2) === "\xFF\xFE")
2044 $encoding[] = 'UTF-16LE';
2047 elseif (substr($data, 0, 3) === "\xEF\xBB\xBF")
2049 $encoding[] = 'UTF-8';
2051 // UTF-32 Big Endian Without BOM
2052 elseif (substr($data, 0, 20) === "\x00\x00\x00\x3C\x00\x00\x00\x3F\x00\x00\x00\x78\x00\x00\x00\x6D\x00\x00\x00\x6C")
2054 if ($pos = strpos($data, "\x00\x00\x00\x3F\x00\x00\x00\x3E"))
2056 $parser = $registry->create('XML_Declaration_Parser', array(SimplePie_Misc::change_encoding(substr($data, 20, $pos - 20), 'UTF-32BE', 'UTF-8')));
2057 if ($parser->parse())
2059 $encoding[] = $parser->encoding;
2062 $encoding[] = 'UTF-32BE';
2064 // UTF-32 Little Endian Without BOM
2065 elseif (substr($data, 0, 20) === "\x3C\x00\x00\x00\x3F\x00\x00\x00\x78\x00\x00\x00\x6D\x00\x00\x00\x6C\x00\x00\x00")
2067 if ($pos = strpos($data, "\x3F\x00\x00\x00\x3E\x00\x00\x00"))
2069 $parser = $registry->create('XML_Declaration_Parser', array(SimplePie_Misc::change_encoding(substr($data, 20, $pos - 20), 'UTF-32LE', 'UTF-8')));
2070 if ($parser->parse())
2072 $encoding[] = $parser->encoding;
2075 $encoding[] = 'UTF-32LE';
2077 // UTF-16 Big Endian Without BOM
2078 elseif (substr($data, 0, 10) === "\x00\x3C\x00\x3F\x00\x78\x00\x6D\x00\x6C")
2080 if ($pos = strpos($data, "\x00\x3F\x00\x3E"))
2082 $parser = $registry->create('XML_Declaration_Parser', array(SimplePie_Misc::change_encoding(substr($data, 20, $pos - 10), 'UTF-16BE', 'UTF-8')));
2083 if ($parser->parse())
2085 $encoding[] = $parser->encoding;
2088 $encoding[] = 'UTF-16BE';
2090 // UTF-16 Little Endian Without BOM
2091 elseif (substr($data, 0, 10) === "\x3C\x00\x3F\x00\x78\x00\x6D\x00\x6C\x00")
2093 if ($pos = strpos($data, "\x3F\x00\x3E\x00"))
2095 $parser = $registry->create('XML_Declaration_Parser', array(SimplePie_Misc::change_encoding(substr($data, 20, $pos - 10), 'UTF-16LE', 'UTF-8')));
2096 if ($parser->parse())
2098 $encoding[] = $parser->encoding;
2101 $encoding[] = 'UTF-16LE';
2103 // US-ASCII (or superset)
2104 elseif (substr($data, 0, 5) === "\x3C\x3F\x78\x6D\x6C")
2106 if ($pos = strpos($data, "\x3F\x3E"))
2108 $parser = $registry->create('XML_Declaration_Parser', array(substr($data, 5, $pos - 5)));
2109 if ($parser->parse())
2111 $encoding[] = $parser->encoding;
2114 $encoding[] = 'UTF-8';
2116 // Fallback to UTF-8
2119 $encoding[] = 'UTF-8';
2124 public static function output_javascript()
2126 if (function_exists('ob_gzhandler'))
2128 ob_start('ob_gzhandler');
2130 header('Content-type: text/javascript; charset: UTF-8');
2131 header('Cache-Control: must-revalidate');
2132 header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 604800) . ' GMT'); // 7 days
2134 function embed_quicktime(type, bgcolor, width, height, link, placeholder, loop) {
2135 if (placeholder != '') {
2136 document.writeln('<embed type="'+type+'" style="cursor:hand; cursor:pointer;" href="'+link+'" src="'+placeholder+'" width="'+width+'" height="'+height+'" autoplay="false" target="myself" controller="false" loop="'+loop+'" scale="aspect" bgcolor="'+bgcolor+'" pluginspage="http://www.apple.com/quicktime/download/"></embed>');
2139 document.writeln('<embed type="'+type+'" style="cursor:hand; cursor:pointer;" src="'+link+'" width="'+width+'" height="'+height+'" autoplay="false" target="myself" controller="true" loop="'+loop+'" scale="aspect" bgcolor="'+bgcolor+'" pluginspage="http://www.apple.com/quicktime/download/"></embed>');
2143 function embed_flash(bgcolor, width, height, link, loop, type) {
2144 document.writeln('<embed src="'+link+'" pluginspage="http://www.macromedia.com/go/getflashplayer" type="'+type+'" quality="high" width="'+width+'" height="'+height+'" bgcolor="'+bgcolor+'" loop="'+loop+'"></embed>');
2147 function embed_flv(width, height, link, placeholder, loop, player) {
2148 document.writeln('<embed src="'+player+'" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" quality="high" width="'+width+'" height="'+height+'" wmode="transparent" flashvars="file='+link+'&autostart=false&repeat='+loop+'&showdigits=true&showfsbutton=false"></embed>');
2151 function embed_wmedia(width, height, link) {
2152 document.writeln('<embed type="application/x-mplayer2" src="'+link+'" autosize="1" width="'+width+'" height="'+height+'" showcontrols="1" showstatusbar="0" showdisplay="0" autostart="0"></embed>');
2158 * Get the SimplePie build timestamp
2160 * Uses the git index if it exists, otherwise uses the modification time
2161 * of the newest file.
2163 public static function get_build()
2165 $root = dirname(dirname(__FILE__));
2166 if (file_exists($root . '/.git/index'))
2168 return filemtime($root . '/.git/index');
2170 elseif (file_exists($root . '/SimplePie'))
2173 foreach (glob($root . '/SimplePie/*.php') as $file)
2175 if (($mtime = filemtime($file)) > $time)
2182 elseif (file_exists(dirname(__FILE__) . '/Core.php'))
2184 return filemtime(dirname(__FILE__) . '/Core.php');
2188 return filemtime(__FILE__);
2193 * Format debugging information
2195 public static function debug(&$sp)
2197 $info = 'SimplePie ' . SIMPLEPIE_VERSION . ' Build ' . SIMPLEPIE_BUILD . "\n";
2198 $info .= 'PHP ' . PHP_VERSION . "\n";
2199 if ($sp->error() !== null)
2201 $info .= 'Error occurred: ' . $sp->error() . "\n";
2205 $info .= "No error found.\n";
2207 $info .= "Extensions:\n";
2208 $extensions = array('pcre', 'curl', 'zlib', 'mbstring', 'iconv', 'xmlreader', 'xml');
2209 foreach ($extensions as $ext)
2211 if (extension_loaded($ext))
2213 $info .= " $ext loaded\n";
2217 $info .= ' Version ' . PCRE_VERSION . "\n";
2220 $version = curl_version();
2221 $info .= ' Version ' . $version['version'] . "\n";
2224 $info .= ' Overloading: ' . mb_get_info('func_overload') . "\n";
2227 $info .= ' Version ' . ICONV_VERSION . "\n";
2230 $info .= ' Version ' . LIBXML_DOTTED_VERSION . "\n";
2236 $info .= " $ext not loaded\n";
2242 public static function silence_errors($num, $str)