Misc.php 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263
  1. <?php
  2. /**
  3. * SimplePie
  4. *
  5. * A PHP-Based RSS and Atom Feed Framework.
  6. * Takes the hard work out of managing a complete RSS/Atom solution.
  7. *
  8. * Copyright (c) 2004-2012, Ryan Parman, Geoffrey Sneddon, Ryan McCue, and contributors
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without modification, are
  12. * permitted provided that the following conditions are met:
  13. *
  14. * * Redistributions of source code must retain the above copyright notice, this list of
  15. * conditions and the following disclaimer.
  16. *
  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.
  20. *
  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
  23. * written permission.
  24. *
  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.
  34. *
  35. * @package SimplePie
  36. * @version 1.4-dev
  37. * @copyright 2004-2012 Ryan Parman, Geoffrey Sneddon, Ryan McCue
  38. * @author Ryan Parman
  39. * @author Geoffrey Sneddon
  40. * @author Ryan McCue
  41. * @link http://simplepie.org/ SimplePie
  42. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  43. */
  44. /**
  45. * Miscellanous utilities
  46. *
  47. * @package SimplePie
  48. */
  49. class SimplePie_Misc
  50. {
  51. public static function time_hms($seconds)
  52. {
  53. $time = '';
  54. $hours = floor($seconds / 3600);
  55. $remainder = $seconds % 3600;
  56. if ($hours > 0)
  57. {
  58. $time .= $hours.':';
  59. }
  60. $minutes = floor($remainder / 60);
  61. $seconds = $remainder % 60;
  62. if ($minutes < 10 && $hours > 0)
  63. {
  64. $minutes = '0' . $minutes;
  65. }
  66. if ($seconds < 10)
  67. {
  68. $seconds = '0' . $seconds;
  69. }
  70. $time .= $minutes.':';
  71. $time .= $seconds;
  72. return $time;
  73. }
  74. /**
  75. * Force HTTPS for selected Web sites
  76. * FreshRSS
  77. */
  78. public static function https_url($url)
  79. {
  80. return preg_replace('%^http://((?:[^/]*?\.)?(?:youtube|dailymotion|tumblr)\.com/)%i', 'https://$1', $url);
  81. }
  82. public static function absolutize_url($relative, $base)
  83. {
  84. if (substr($relative, 0, 2) === '//')
  85. {//Protocol-relative URLs "//www.example.net"
  86. return 'https:' . $relative;
  87. }
  88. $iri = SimplePie_IRI::absolutize(new SimplePie_IRI($base), $relative);
  89. if ($iri === false)
  90. {
  91. return false;
  92. }
  93. return $iri->get_uri();
  94. }
  95. /**
  96. * Get a HTML/XML element from a HTML string
  97. *
  98. * @deprecated Use DOMDocument instead (parsing HTML with regex is bad!)
  99. * @param string $realname Element name (including namespace prefix if applicable)
  100. * @param string $string HTML document
  101. * @return array
  102. */
  103. public static function get_element($realname, $string)
  104. {
  105. $return = array();
  106. $name = preg_quote($realname, '/');
  107. if (preg_match_all("/<($name)" . SIMPLEPIE_PCRE_HTML_ATTRIBUTE . "(>(.*)<\/$name>|(\/)?>)/siU", $string, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE))
  108. {
  109. for ($i = 0, $total_matches = count($matches); $i < $total_matches; $i++)
  110. {
  111. $return[$i]['tag'] = $realname;
  112. $return[$i]['full'] = $matches[$i][0][0];
  113. $return[$i]['offset'] = $matches[$i][0][1];
  114. if (strlen($matches[$i][3][0]) <= 2)
  115. {
  116. $return[$i]['self_closing'] = true;
  117. }
  118. else
  119. {
  120. $return[$i]['self_closing'] = false;
  121. $return[$i]['content'] = $matches[$i][4][0];
  122. }
  123. $return[$i]['attribs'] = array();
  124. 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))
  125. {
  126. for ($j = 0, $total_attribs = count($attribs); $j < $total_attribs; $j++)
  127. {
  128. if (count($attribs[$j]) === 2)
  129. {
  130. $attribs[$j][2] = $attribs[$j][1];
  131. }
  132. $return[$i]['attribs'][strtolower($attribs[$j][1])]['data'] = SimplePie_Misc::entities_decode(end($attribs[$j]), 'UTF-8');
  133. }
  134. }
  135. }
  136. }
  137. return $return;
  138. }
  139. public static function element_implode($element)
  140. {
  141. $full = "<$element[tag]";
  142. foreach ($element['attribs'] as $key => $value)
  143. {
  144. $key = strtolower($key);
  145. $full .= " $key=\"" . htmlspecialchars($value['data'], ENT_COMPAT, 'UTF-8') . '"';
  146. }
  147. if ($element['self_closing'])
  148. {
  149. $full .= ' />';
  150. }
  151. else
  152. {
  153. $full .= ">$element[content]</$element[tag]>";
  154. }
  155. return $full;
  156. }
  157. public static function error($message, $level, $file, $line)
  158. {
  159. if ((ini_get('error_reporting') & $level) > 0)
  160. {
  161. switch ($level)
  162. {
  163. case E_USER_ERROR:
  164. $note = 'PHP Error';
  165. break;
  166. case E_USER_WARNING:
  167. $note = 'PHP Warning';
  168. break;
  169. case E_USER_NOTICE:
  170. $note = 'PHP Notice';
  171. break;
  172. default:
  173. $note = 'Unknown Error';
  174. break;
  175. }
  176. $log_error = true;
  177. if (!function_exists('error_log'))
  178. {
  179. $log_error = false;
  180. }
  181. $log_file = @ini_get('error_log');
  182. if (!empty($log_file) && ('syslog' !== $log_file) && !@is_writable($log_file))
  183. {
  184. $log_error = false;
  185. }
  186. if ($log_error)
  187. {
  188. @error_log("$note: $message in $file on line $line", 0);
  189. }
  190. }
  191. return $message;
  192. }
  193. public static function fix_protocol($url, $http = 1)
  194. {
  195. $url = SimplePie_Misc::normalize_url($url);
  196. $parsed = SimplePie_Misc::parse_url($url);
  197. if ($parsed['scheme'] !== '' && $parsed['scheme'] !== 'http' && $parsed['scheme'] !== 'https')
  198. {
  199. return SimplePie_Misc::fix_protocol(SimplePie_Misc::compress_parse_url('http', $parsed['authority'], $parsed['path'], $parsed['query'], $parsed['fragment']), $http);
  200. }
  201. if ($parsed['scheme'] === '' && $parsed['authority'] === '' && !file_exists($url))
  202. {
  203. return SimplePie_Misc::fix_protocol(SimplePie_Misc::compress_parse_url('http', $parsed['path'], '', $parsed['query'], $parsed['fragment']), $http);
  204. }
  205. if ($http === 2 && $parsed['scheme'] !== '')
  206. {
  207. return "feed:$url";
  208. }
  209. elseif ($http === 3 && strtolower($parsed['scheme']) === 'http')
  210. {
  211. return substr_replace($url, 'podcast', 0, 4);
  212. }
  213. elseif ($http === 4 && strtolower($parsed['scheme']) === 'http')
  214. {
  215. return substr_replace($url, 'itpc', 0, 4);
  216. }
  217. else
  218. {
  219. return $url;
  220. }
  221. }
  222. public static function array_merge_recursive($array1, $array2)
  223. {
  224. foreach ($array2 as $key => $value)
  225. {
  226. if (is_array($value))
  227. {
  228. $array1[$key] = SimplePie_Misc::array_merge_recursive($array1[$key], $value);
  229. }
  230. else
  231. {
  232. $array1[$key] = $value;
  233. }
  234. }
  235. return $array1;
  236. }
  237. public static function parse_url($url)
  238. {
  239. $iri = new SimplePie_IRI($url);
  240. return array(
  241. 'scheme' => (string) $iri->scheme,
  242. 'authority' => (string) $iri->authority,
  243. 'path' => (string) $iri->path,
  244. 'query' => (string) $iri->query,
  245. 'fragment' => (string) $iri->fragment
  246. );
  247. }
  248. public static function compress_parse_url($scheme = '', $authority = '', $path = '', $query = '', $fragment = '')
  249. {
  250. $iri = new SimplePie_IRI('');
  251. $iri->scheme = $scheme;
  252. $iri->authority = $authority;
  253. $iri->path = $path;
  254. $iri->query = $query;
  255. $iri->fragment = $fragment;
  256. return $iri->get_uri();
  257. }
  258. public static function normalize_url($url)
  259. {
  260. $iri = new SimplePie_IRI($url);
  261. return $iri->get_uri();
  262. }
  263. public static function percent_encoding_normalization($match)
  264. {
  265. $integer = hexdec($match[1]);
  266. if ($integer >= 0x41 && $integer <= 0x5A || $integer >= 0x61 && $integer <= 0x7A || $integer >= 0x30 && $integer <= 0x39 || $integer === 0x2D || $integer === 0x2E || $integer === 0x5F || $integer === 0x7E)
  267. {
  268. return chr($integer);
  269. }
  270. else
  271. {
  272. return strtoupper($match[0]);
  273. }
  274. }
  275. /**
  276. * Converts a Windows-1252 encoded string to a UTF-8 encoded string
  277. *
  278. * @static
  279. * @param string $string Windows-1252 encoded string
  280. * @return string UTF-8 encoded string
  281. */
  282. public static function windows_1252_to_utf8($string)
  283. {
  284. 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");
  285. return strtr($string, $convert_table);
  286. }
  287. /**
  288. * Change a string from one encoding to another
  289. *
  290. * @param string $data Raw data in $input encoding
  291. * @param string $input Encoding of $data
  292. * @param string $output Encoding you want
  293. * @return string|boolean False if we can't convert it
  294. */
  295. public static function change_encoding($data, $input, $output)
  296. {
  297. $input = SimplePie_Misc::encoding($input);
  298. $output = SimplePie_Misc::encoding($output);
  299. // We fail to fail on non US-ASCII bytes
  300. if ($input === 'US-ASCII')
  301. {
  302. static $non_ascii_octects = '';
  303. if (!$non_ascii_octects)
  304. {
  305. for ($i = 0x80; $i <= 0xFF; $i++)
  306. {
  307. $non_ascii_octects .= chr($i);
  308. }
  309. }
  310. $data = substr($data, 0, strcspn($data, $non_ascii_octects));
  311. }
  312. // This is first, as behaviour of this is completely predictable
  313. if ($input === 'windows-1252' && $output === 'UTF-8')
  314. {
  315. return SimplePie_Misc::windows_1252_to_utf8($data);
  316. }
  317. // This is second, as behaviour of this varies only with PHP version (the middle part of this expression checks the encoding is supported).
  318. elseif (function_exists('mb_convert_encoding') && ($return = SimplePie_Misc::change_encoding_mbstring($data, $input, $output)))
  319. {
  320. return $return;
  321. }
  322. // This is last, as behaviour of this varies with OS userland and PHP version
  323. elseif (function_exists('iconv') && ($return = SimplePie_Misc::change_encoding_iconv($data, $input, $output)))
  324. {
  325. return $return;
  326. }
  327. // If we can't do anything, just fail
  328. else
  329. {
  330. return false;
  331. }
  332. }
  333. protected static function change_encoding_mbstring($data, $input, $output)
  334. {
  335. if ($input === 'windows-949')
  336. {
  337. $input = 'EUC-KR';
  338. }
  339. if ($output === 'windows-949')
  340. {
  341. $output = 'EUC-KR';
  342. }
  343. if ($input === 'Windows-31J')
  344. {
  345. $input = 'SJIS';
  346. }
  347. if ($output === 'Windows-31J')
  348. {
  349. $output = 'SJIS';
  350. }
  351. // Check that the encoding is supported
  352. if (@mb_convert_encoding("\x80", 'UTF-16BE', $input) === "\x00\x80")
  353. {
  354. return false;
  355. }
  356. if (!in_array($input, mb_list_encodings()))
  357. {
  358. return false;
  359. }
  360. // Let's do some conversion
  361. if ($return = @mb_convert_encoding($data, $output, $input))
  362. {
  363. return $return;
  364. }
  365. return false;
  366. }
  367. protected static function change_encoding_iconv($data, $input, $output)
  368. {
  369. return @iconv($input, $output, $data);
  370. }
  371. /**
  372. * Normalize an encoding name
  373. *
  374. * This is automatically generated by create.php
  375. *
  376. * To generate it, run `php create.php` on the command line, and copy the
  377. * output to replace this function.
  378. *
  379. * @param string $charset Character set to standardise
  380. * @return string Standardised name
  381. */
  382. public static function encoding($charset)
  383. {
  384. // Normalization from UTS #22
  385. switch (strtolower(preg_replace('/(?:[^a-zA-Z0-9]+|([^0-9])0+)/', '\1', $charset)))
  386. {
  387. case 'adobestandardencoding':
  388. case 'csadobestandardencoding':
  389. return 'Adobe-Standard-Encoding';
  390. case 'adobesymbolencoding':
  391. case 'cshppsmath':
  392. return 'Adobe-Symbol-Encoding';
  393. case 'ami1251':
  394. case 'amiga1251':
  395. return 'Amiga-1251';
  396. case 'ansix31101983':
  397. case 'csat5001983':
  398. case 'csiso99naplps':
  399. case 'isoir99':
  400. case 'naplps':
  401. return 'ANSI_X3.110-1983';
  402. case 'arabic7':
  403. case 'asmo449':
  404. case 'csiso89asmo449':
  405. case 'iso9036':
  406. case 'isoir89':
  407. return 'ASMO_449';
  408. case 'big5':
  409. case 'csbig5':
  410. return 'Big5';
  411. case 'big5hkscs':
  412. return 'Big5-HKSCS';
  413. case 'bocu1':
  414. case 'csbocu1':
  415. return 'BOCU-1';
  416. case 'brf':
  417. case 'csbrf':
  418. return 'BRF';
  419. case 'bs4730':
  420. case 'csiso4unitedkingdom':
  421. case 'gb':
  422. case 'iso646gb':
  423. case 'isoir4':
  424. case 'uk':
  425. return 'BS_4730';
  426. case 'bsviewdata':
  427. case 'csiso47bsviewdata':
  428. case 'isoir47':
  429. return 'BS_viewdata';
  430. case 'cesu8':
  431. case 'cscesu8':
  432. return 'CESU-8';
  433. case 'ca':
  434. case 'csa71':
  435. case 'csaz243419851':
  436. case 'csiso121canadian1':
  437. case 'iso646ca':
  438. case 'isoir121':
  439. return 'CSA_Z243.4-1985-1';
  440. case 'csa72':
  441. case 'csaz243419852':
  442. case 'csiso122canadian2':
  443. case 'iso646ca2':
  444. case 'isoir122':
  445. return 'CSA_Z243.4-1985-2';
  446. case 'csaz24341985gr':
  447. case 'csiso123csaz24341985gr':
  448. case 'isoir123':
  449. return 'CSA_Z243.4-1985-gr';
  450. case 'csiso139csn369103':
  451. case 'csn369103':
  452. case 'isoir139':
  453. return 'CSN_369103';
  454. case 'csdecmcs':
  455. case 'dec':
  456. case 'decmcs':
  457. return 'DEC-MCS';
  458. case 'csiso21german':
  459. case 'de':
  460. case 'din66003':
  461. case 'iso646de':
  462. case 'isoir21':
  463. return 'DIN_66003';
  464. case 'csdkus':
  465. case 'dkus':
  466. return 'dk-us';
  467. case 'csiso646danish':
  468. case 'dk':
  469. case 'ds2089':
  470. case 'iso646dk':
  471. return 'DS_2089';
  472. case 'csibmebcdicatde':
  473. case 'ebcdicatde':
  474. return 'EBCDIC-AT-DE';
  475. case 'csebcdicatdea':
  476. case 'ebcdicatdea':
  477. return 'EBCDIC-AT-DE-A';
  478. case 'csebcdiccafr':
  479. case 'ebcdiccafr':
  480. return 'EBCDIC-CA-FR';
  481. case 'csebcdicdkno':
  482. case 'ebcdicdkno':
  483. return 'EBCDIC-DK-NO';
  484. case 'csebcdicdknoa':
  485. case 'ebcdicdknoa':
  486. return 'EBCDIC-DK-NO-A';
  487. case 'csebcdices':
  488. case 'ebcdices':
  489. return 'EBCDIC-ES';
  490. case 'csebcdicesa':
  491. case 'ebcdicesa':
  492. return 'EBCDIC-ES-A';
  493. case 'csebcdicess':
  494. case 'ebcdicess':
  495. return 'EBCDIC-ES-S';
  496. case 'csebcdicfise':
  497. case 'ebcdicfise':
  498. return 'EBCDIC-FI-SE';
  499. case 'csebcdicfisea':
  500. case 'ebcdicfisea':
  501. return 'EBCDIC-FI-SE-A';
  502. case 'csebcdicfr':
  503. case 'ebcdicfr':
  504. return 'EBCDIC-FR';
  505. case 'csebcdicit':
  506. case 'ebcdicit':
  507. return 'EBCDIC-IT';
  508. case 'csebcdicpt':
  509. case 'ebcdicpt':
  510. return 'EBCDIC-PT';
  511. case 'csebcdicuk':
  512. case 'ebcdicuk':
  513. return 'EBCDIC-UK';
  514. case 'csebcdicus':
  515. case 'ebcdicus':
  516. return 'EBCDIC-US';
  517. case 'csiso111ecmacyrillic':
  518. case 'ecmacyrillic':
  519. case 'isoir111':
  520. case 'koi8e':
  521. return 'ECMA-cyrillic';
  522. case 'csiso17spanish':
  523. case 'es':
  524. case 'iso646es':
  525. case 'isoir17':
  526. return 'ES';
  527. case 'csiso85spanish2':
  528. case 'es2':
  529. case 'iso646es2':
  530. case 'isoir85':
  531. return 'ES2';
  532. case 'cseucpkdfmtjapanese':
  533. case 'eucjp':
  534. case 'extendedunixcodepackedformatforjapanese':
  535. return 'EUC-JP';
  536. case 'cseucfixwidjapanese':
  537. case 'extendedunixcodefixedwidthforjapanese':
  538. return 'Extended_UNIX_Code_Fixed_Width_for_Japanese';
  539. case 'gb18030':
  540. return 'GB18030';
  541. case 'chinese':
  542. case 'cp936':
  543. case 'csgb2312':
  544. case 'csiso58gb231280':
  545. case 'gb2312':
  546. case 'gb231280':
  547. case 'gbk':
  548. case 'isoir58':
  549. case 'ms936':
  550. case 'windows936':
  551. return 'GBK';
  552. case 'cn':
  553. case 'csiso57gb1988':
  554. case 'gb198880':
  555. case 'iso646cn':
  556. case 'isoir57':
  557. return 'GB_1988-80';
  558. case 'csiso153gost1976874':
  559. case 'gost1976874':
  560. case 'isoir153':
  561. case 'stsev35888':
  562. return 'GOST_19768-74';
  563. case 'csiso150':
  564. case 'csiso150greekccitt':
  565. case 'greekccitt':
  566. case 'isoir150':
  567. return 'greek-ccitt';
  568. case 'csiso88greek7':
  569. case 'greek7':
  570. case 'isoir88':
  571. return 'greek7';
  572. case 'csiso18greek7old':
  573. case 'greek7old':
  574. case 'isoir18':
  575. return 'greek7-old';
  576. case 'cshpdesktop':
  577. case 'hpdesktop':
  578. return 'HP-DeskTop';
  579. case 'cshplegal':
  580. case 'hplegal':
  581. return 'HP-Legal';
  582. case 'cshpmath8':
  583. case 'hpmath8':
  584. return 'HP-Math8';
  585. case 'cshppifont':
  586. case 'hppifont':
  587. return 'HP-Pi-font';
  588. case 'cshproman8':
  589. case 'hproman8':
  590. case 'r8':
  591. case 'roman8':
  592. return 'hp-roman8';
  593. case 'hzgb2312':
  594. return 'HZ-GB-2312';
  595. case 'csibmsymbols':
  596. case 'ibmsymbols':
  597. return 'IBM-Symbols';
  598. case 'csibmthai':
  599. case 'ibmthai':
  600. return 'IBM-Thai';
  601. case 'cp37':
  602. case 'csibm37':
  603. case 'ebcdiccpca':
  604. case 'ebcdiccpnl':
  605. case 'ebcdiccpus':
  606. case 'ebcdiccpwt':
  607. case 'ibm37':
  608. return 'IBM037';
  609. case 'cp38':
  610. case 'csibm38':
  611. case 'ebcdicint':
  612. case 'ibm38':
  613. return 'IBM038';
  614. case 'cp273':
  615. case 'csibm273':
  616. case 'ibm273':
  617. return 'IBM273';
  618. case 'cp274':
  619. case 'csibm274':
  620. case 'ebcdicbe':
  621. case 'ibm274':
  622. return 'IBM274';
  623. case 'cp275':
  624. case 'csibm275':
  625. case 'ebcdicbr':
  626. case 'ibm275':
  627. return 'IBM275';
  628. case 'csibm277':
  629. case 'ebcdiccpdk':
  630. case 'ebcdiccpno':
  631. case 'ibm277':
  632. return 'IBM277';
  633. case 'cp278':
  634. case 'csibm278':
  635. case 'ebcdiccpfi':
  636. case 'ebcdiccpse':
  637. case 'ibm278':
  638. return 'IBM278';
  639. case 'cp280':
  640. case 'csibm280':
  641. case 'ebcdiccpit':
  642. case 'ibm280':
  643. return 'IBM280';
  644. case 'cp281':
  645. case 'csibm281':
  646. case 'ebcdicjpe':
  647. case 'ibm281':
  648. return 'IBM281';
  649. case 'cp284':
  650. case 'csibm284':
  651. case 'ebcdiccpes':
  652. case 'ibm284':
  653. return 'IBM284';
  654. case 'cp285':
  655. case 'csibm285':
  656. case 'ebcdiccpgb':
  657. case 'ibm285':
  658. return 'IBM285';
  659. case 'cp290':
  660. case 'csibm290':
  661. case 'ebcdicjpkana':
  662. case 'ibm290':
  663. return 'IBM290';
  664. case 'cp297':
  665. case 'csibm297':
  666. case 'ebcdiccpfr':
  667. case 'ibm297':
  668. return 'IBM297';
  669. case 'cp420':
  670. case 'csibm420':
  671. case 'ebcdiccpar1':
  672. case 'ibm420':
  673. return 'IBM420';
  674. case 'cp423':
  675. case 'csibm423':
  676. case 'ebcdiccpgr':
  677. case 'ibm423':
  678. return 'IBM423';
  679. case 'cp424':
  680. case 'csibm424':
  681. case 'ebcdiccphe':
  682. case 'ibm424':
  683. return 'IBM424';
  684. case '437':
  685. case 'cp437':
  686. case 'cspc8codepage437':
  687. case 'ibm437':
  688. return 'IBM437';
  689. case 'cp500':
  690. case 'csibm500':
  691. case 'ebcdiccpbe':
  692. case 'ebcdiccpch':
  693. case 'ibm500':
  694. return 'IBM500';
  695. case 'cp775':
  696. case 'cspc775baltic':
  697. case 'ibm775':
  698. return 'IBM775';
  699. case '850':
  700. case 'cp850':
  701. case 'cspc850multilingual':
  702. case 'ibm850':
  703. return 'IBM850';
  704. case '851':
  705. case 'cp851':
  706. case 'csibm851':
  707. case 'ibm851':
  708. return 'IBM851';
  709. case '852':
  710. case 'cp852':
  711. case 'cspcp852':
  712. case 'ibm852':
  713. return 'IBM852';
  714. case '855':
  715. case 'cp855':
  716. case 'csibm855':
  717. case 'ibm855':
  718. return 'IBM855';
  719. case '857':
  720. case 'cp857':
  721. case 'csibm857':
  722. case 'ibm857':
  723. return 'IBM857';
  724. case 'ccsid858':
  725. case 'cp858':
  726. case 'ibm858':
  727. case 'pcmultilingual850euro':
  728. return 'IBM00858';
  729. case '860':
  730. case 'cp860':
  731. case 'csibm860':
  732. case 'ibm860':
  733. return 'IBM860';
  734. case '861':
  735. case 'cp861':
  736. case 'cpis':
  737. case 'csibm861':
  738. case 'ibm861':
  739. return 'IBM861';
  740. case '862':
  741. case 'cp862':
  742. case 'cspc862latinhebrew':
  743. case 'ibm862':
  744. return 'IBM862';
  745. case '863':
  746. case 'cp863':
  747. case 'csibm863':
  748. case 'ibm863':
  749. return 'IBM863';
  750. case 'cp864':
  751. case 'csibm864':
  752. case 'ibm864':
  753. return 'IBM864';
  754. case '865':
  755. case 'cp865':
  756. case 'csibm865':
  757. case 'ibm865':
  758. return 'IBM865';
  759. case '866':
  760. case 'cp866':
  761. case 'csibm866':
  762. case 'ibm866':
  763. return 'IBM866';
  764. case 'cp868':
  765. case 'cpar':
  766. case 'csibm868':
  767. case 'ibm868':
  768. return 'IBM868';
  769. case '869':
  770. case 'cp869':
  771. case 'cpgr':
  772. case 'csibm869':
  773. case 'ibm869':
  774. return 'IBM869';
  775. case 'cp870':
  776. case 'csibm870':
  777. case 'ebcdiccproece':
  778. case 'ebcdiccpyu':
  779. case 'ibm870':
  780. return 'IBM870';
  781. case 'cp871':
  782. case 'csibm871':
  783. case 'ebcdiccpis':
  784. case 'ibm871':
  785. return 'IBM871';
  786. case 'cp880':
  787. case 'csibm880':
  788. case 'ebcdiccyrillic':
  789. case 'ibm880':
  790. return 'IBM880';
  791. case 'cp891':
  792. case 'csibm891':
  793. case 'ibm891':
  794. return 'IBM891';
  795. case 'cp903':
  796. case 'csibm903':
  797. case 'ibm903':
  798. return 'IBM903';
  799. case '904':
  800. case 'cp904':
  801. case 'csibbm904':
  802. case 'ibm904':
  803. return 'IBM904';
  804. case 'cp905':
  805. case 'csibm905':
  806. case 'ebcdiccptr':
  807. case 'ibm905':
  808. return 'IBM905';
  809. case 'cp918':
  810. case 'csibm918':
  811. case 'ebcdiccpar2':
  812. case 'ibm918':
  813. return 'IBM918';
  814. case 'ccsid924':
  815. case 'cp924':
  816. case 'ebcdiclatin9euro':
  817. case 'ibm924':
  818. return 'IBM00924';
  819. case 'cp1026':
  820. case 'csibm1026':
  821. case 'ibm1026':
  822. return 'IBM1026';
  823. case 'ibm1047':
  824. return 'IBM1047';
  825. case 'ccsid1140':
  826. case 'cp1140':
  827. case 'ebcdicus37euro':
  828. case 'ibm1140':
  829. return 'IBM01140';
  830. case 'ccsid1141':
  831. case 'cp1141':
  832. case 'ebcdicde273euro':
  833. case 'ibm1141':
  834. return 'IBM01141';
  835. case 'ccsid1142':
  836. case 'cp1142':
  837. case 'ebcdicdk277euro':
  838. case 'ebcdicno277euro':
  839. case 'ibm1142':
  840. return 'IBM01142';
  841. case 'ccsid1143':
  842. case 'cp1143':
  843. case 'ebcdicfi278euro':
  844. case 'ebcdicse278euro':
  845. case 'ibm1143':
  846. return 'IBM01143';
  847. case 'ccsid1144':
  848. case 'cp1144':
  849. case 'ebcdicit280euro':
  850. case 'ibm1144':
  851. return 'IBM01144';
  852. case 'ccsid1145':
  853. case 'cp1145':
  854. case 'ebcdices284euro':
  855. case 'ibm1145':
  856. return 'IBM01145';
  857. case 'ccsid1146':
  858. case 'cp1146':
  859. case 'ebcdicgb285euro':
  860. case 'ibm1146':
  861. return 'IBM01146';
  862. case 'ccsid1147':
  863. case 'cp1147':
  864. case 'ebcdicfr297euro':
  865. case 'ibm1147':
  866. return 'IBM01147';
  867. case 'ccsid1148':
  868. case 'cp1148':
  869. case 'ebcdicinternational500euro':
  870. case 'ibm1148':
  871. return 'IBM01148';
  872. case 'ccsid1149':
  873. case 'cp1149':
  874. case 'ebcdicis871euro':
  875. case 'ibm1149':
  876. return 'IBM01149';
  877. case 'csiso143iecp271':
  878. case 'iecp271':
  879. case 'isoir143':
  880. return 'IEC_P27-1';
  881. case 'csiso49inis':
  882. case 'inis':
  883. case 'isoir49':
  884. return 'INIS';
  885. case 'csiso50inis8':
  886. case 'inis8':
  887. case 'isoir50':
  888. return 'INIS-8';
  889. case 'csiso51iniscyrillic':
  890. case 'iniscyrillic':
  891. case 'isoir51':
  892. return 'INIS-cyrillic';
  893. case 'csinvariant':
  894. case 'invariant':
  895. return 'INVARIANT';
  896. case 'iso2022cn':
  897. return 'ISO-2022-CN';
  898. case 'iso2022cnext':
  899. return 'ISO-2022-CN-EXT';
  900. case 'csiso2022jp':
  901. case 'iso2022jp':
  902. return 'ISO-2022-JP';
  903. case 'csiso2022jp2':
  904. case 'iso2022jp2':
  905. return 'ISO-2022-JP-2';
  906. case 'csiso2022kr':
  907. case 'iso2022kr':
  908. return 'ISO-2022-KR';
  909. case 'cswindows30latin1':
  910. case 'iso88591windows30latin1':
  911. return 'ISO-8859-1-Windows-3.0-Latin-1';
  912. case 'cswindows31latin1':
  913. case 'iso88591windows31latin1':
  914. return 'ISO-8859-1-Windows-3.1-Latin-1';
  915. case 'csisolatin2':
  916. case 'iso88592':
  917. case 'iso885921987':
  918. case 'isoir101':
  919. case 'l2':
  920. case 'latin2':
  921. return 'ISO-8859-2';
  922. case 'cswindows31latin2':
  923. case 'iso88592windowslatin2':
  924. return 'ISO-8859-2-Windows-Latin-2';
  925. case 'csisolatin3':
  926. case 'iso88593':
  927. case 'iso885931988':
  928. case 'isoir109':
  929. case 'l3':
  930. case 'latin3':
  931. return 'ISO-8859-3';
  932. case 'csisolatin4':
  933. case 'iso88594':
  934. case 'iso885941988':
  935. case 'isoir110':
  936. case 'l4':
  937. case 'latin4':
  938. return 'ISO-8859-4';
  939. case 'csisolatincyrillic':
  940. case 'cyrillic':
  941. case 'iso88595':
  942. case 'iso885951988':
  943. case 'isoir144':
  944. return 'ISO-8859-5';
  945. case 'arabic':
  946. case 'asmo708':
  947. case 'csisolatinarabic':
  948. case 'ecma114':
  949. case 'iso88596':
  950. case 'iso885961987':
  951. case 'isoir127':
  952. return 'ISO-8859-6';
  953. case 'csiso88596e':
  954. case 'iso88596e':
  955. return 'ISO-8859-6-E';
  956. case 'csiso88596i':
  957. case 'iso88596i':
  958. return 'ISO-8859-6-I';
  959. case 'csisolatingreek':
  960. case 'ecma118':
  961. case 'elot928':
  962. case 'greek':
  963. case 'greek8':
  964. case 'iso88597':
  965. case 'iso885971987':
  966. case 'isoir126':
  967. return 'ISO-8859-7';
  968. case 'csisolatinhebrew':
  969. case 'hebrew':
  970. case 'iso88598':
  971. case 'iso885981988':
  972. case 'isoir138':
  973. return 'ISO-8859-8';
  974. case 'csiso88598e':
  975. case 'iso88598e':
  976. return 'ISO-8859-8-E';
  977. case 'csiso88598i':
  978. case 'iso88598i':
  979. return 'ISO-8859-8-I';
  980. case 'cswindows31latin5':
  981. case 'iso88599windowslatin5':
  982. return 'ISO-8859-9-Windows-Latin-5';
  983. case 'csisolatin6':
  984. case 'iso885910':
  985. case 'iso8859101992':
  986. case 'isoir157':
  987. case 'l6':
  988. case 'latin6':
  989. return 'ISO-8859-10';
  990. case 'iso885913':
  991. return 'ISO-8859-13';
  992. case 'iso885914':
  993. case 'iso8859141998':
  994. case 'isoceltic':
  995. case 'isoir199':
  996. case 'l8':
  997. case 'latin8':
  998. return 'ISO-8859-14';
  999. case 'iso885915':
  1000. case 'latin9':
  1001. return 'ISO-8859-15';
  1002. case 'iso885916':
  1003. case 'iso8859162001':
  1004. case 'isoir226':
  1005. case 'l10':
  1006. case 'latin10':
  1007. return 'ISO-8859-16';
  1008. case 'iso10646j1':
  1009. return 'ISO-10646-J-1';
  1010. case 'csunicode':
  1011. case 'iso10646ucs2':
  1012. return 'ISO-10646-UCS-2';
  1013. case 'csucs4':
  1014. case 'iso10646ucs4':
  1015. return 'ISO-10646-UCS-4';
  1016. case 'csunicodeascii':
  1017. case 'iso10646ucsbasic':
  1018. return 'ISO-10646-UCS-Basic';
  1019. case 'csunicodelatin1':
  1020. case 'iso10646':
  1021. case 'iso10646unicodelatin1':
  1022. return 'ISO-10646-Unicode-Latin1';
  1023. case 'csiso10646utf1':
  1024. case 'iso10646utf1':
  1025. return 'ISO-10646-UTF-1';
  1026. case 'csiso115481':
  1027. case 'iso115481':
  1028. case 'isotr115481':
  1029. return 'ISO-11548-1';
  1030. case 'csiso90':
  1031. case 'isoir90':
  1032. return 'iso-ir-90';
  1033. case 'csunicodeibm1261':
  1034. case 'isounicodeibm1261':
  1035. return 'ISO-Unicode-IBM-1261';
  1036. case 'csunicodeibm1264':
  1037. case 'isounicodeibm1264':
  1038. return 'ISO-Unicode-IBM-1264';
  1039. case 'csunicodeibm1265':
  1040. case 'isounicodeibm1265':
  1041. return 'ISO-Unicode-IBM-1265';
  1042. case 'csunicodeibm1268':
  1043. case 'isounicodeibm1268':
  1044. return 'ISO-Unicode-IBM-1268';
  1045. case 'csunicodeibm1276':
  1046. case 'isounicodeibm1276':
  1047. return 'ISO-Unicode-IBM-1276';
  1048. case 'csiso646basic1983':
  1049. case 'iso646basic1983':
  1050. case 'ref':
  1051. return 'ISO_646.basic:1983';
  1052. case 'csiso2intlrefversion':
  1053. case 'irv':
  1054. case 'iso646irv1983':
  1055. case 'isoir2':
  1056. return 'ISO_646.irv:1983';
  1057. case 'csiso2033':
  1058. case 'e13b':
  1059. case 'iso20331983':
  1060. case 'isoir98':
  1061. return 'ISO_2033-1983';
  1062. case 'csiso5427cyrillic':
  1063. case 'iso5427':
  1064. case 'isoir37':
  1065. return 'ISO_5427';
  1066. case 'iso5427cyrillic1981':
  1067. case 'iso54271981':
  1068. case 'isoir54':
  1069. return 'ISO_5427:1981';
  1070. case 'csiso5428greek':
  1071. case 'iso54281980':
  1072. case 'isoir55':
  1073. return 'ISO_5428:1980';
  1074. case 'csiso6937add':
  1075. case 'iso6937225':
  1076. case 'isoir152':
  1077. return 'ISO_6937-2-25';
  1078. case 'csisotextcomm':
  1079. case 'iso69372add':
  1080. case 'isoir142':
  1081. return 'ISO_6937-2-add';
  1082. case 'csiso8859supp':
  1083. case 'iso8859supp':
  1084. case 'isoir154':
  1085. case 'latin125':
  1086. return 'ISO_8859-supp';
  1087. case 'csiso10367box':
  1088. case 'iso10367box':
  1089. case 'isoir155':
  1090. return 'ISO_10367-box';
  1091. case 'csiso15italian':
  1092. case 'iso646it':
  1093. case 'isoir15':
  1094. case 'it':
  1095. return 'IT';
  1096. case 'csiso13jisc6220jp':
  1097. case 'isoir13':
  1098. case 'jisc62201969':
  1099. case 'jisc62201969jp':
  1100. case 'katakana':
  1101. case 'x2017':
  1102. return 'JIS_C6220-1969-jp';
  1103. case 'csiso14jisc6220ro':
  1104. case 'iso646jp':
  1105. case 'isoir14':
  1106. case 'jisc62201969ro':
  1107. case 'jp':
  1108. return 'JIS_C6220-1969-ro';
  1109. case 'csiso42jisc62261978':
  1110. case 'isoir42':
  1111. case 'jisc62261978':
  1112. return 'JIS_C6226-1978';
  1113. case 'csiso87jisx208':
  1114. case 'isoir87':
  1115. case 'jisc62261983':
  1116. case 'jisx2081983':
  1117. case 'x208':
  1118. return 'JIS_C6226-1983';
  1119. case 'csiso91jisc62291984a':
  1120. case 'isoir91':
  1121. case 'jisc62291984a':
  1122. case 'jpocra':
  1123. return 'JIS_C6229-1984-a';
  1124. case 'csiso92jisc62991984b':
  1125. case 'iso646jpocrb':
  1126. case 'isoir92':
  1127. case 'jisc62291984b':
  1128. case 'jpocrb':
  1129. return 'JIS_C6229-1984-b';
  1130. case 'csiso93jis62291984badd':
  1131. case 'isoir93':
  1132. case 'jisc62291984badd':
  1133. case 'jpocrbadd':
  1134. return 'JIS_C6229-1984-b-add';
  1135. case 'csiso94jis62291984hand':
  1136. case 'isoir94':
  1137. case 'jisc62291984hand':
  1138. case 'jpocrhand':
  1139. return 'JIS_C6229-1984-hand';
  1140. case 'csiso95jis62291984handadd':
  1141. case 'isoir95':
  1142. case 'jisc62291984handadd':
  1143. case 'jpocrhandadd':
  1144. return 'JIS_C6229-1984-hand-add';
  1145. case 'csiso96jisc62291984kana':
  1146. case 'isoir96':
  1147. case 'jisc62291984kana':
  1148. return 'JIS_C6229-1984-kana';
  1149. case 'csjisencoding':
  1150. case 'jisencoding':
  1151. return 'JIS_Encoding';
  1152. case 'cshalfwidthkatakana':
  1153. case 'jisx201':
  1154. case 'x201':
  1155. return 'JIS_X0201';
  1156. case 'csiso159jisx2121990':
  1157. case 'isoir159':
  1158. case 'jisx2121990':
  1159. case 'x212':
  1160. return 'JIS_X0212-1990';
  1161. case 'csiso141jusib1002':
  1162. case 'iso646yu':
  1163. case 'isoir141':
  1164. case 'js':
  1165. case 'jusib1002':
  1166. case 'yu':
  1167. return 'JUS_I.B1.002';
  1168. case 'csiso147macedonian':
  1169. case 'isoir147':
  1170. case 'jusib1003mac':
  1171. case 'macedonian':
  1172. return 'JUS_I.B1.003-mac';
  1173. case 'csiso146serbian':
  1174. case 'isoir146':
  1175. case 'jusib1003serb':
  1176. case 'serbian':
  1177. return 'JUS_I.B1.003-serb';
  1178. case 'koi7switched':
  1179. return 'KOI7-switched';
  1180. case 'cskoi8r':
  1181. case 'koi8r':
  1182. return 'KOI8-R';
  1183. case 'koi8u':
  1184. return 'KOI8-U';
  1185. case 'csksc5636':
  1186. case 'iso646kr':
  1187. case 'ksc5636':
  1188. return 'KSC5636';
  1189. case 'cskz1048':
  1190. case 'kz1048':
  1191. case 'rk1048':
  1192. case 'strk10482002':
  1193. return 'KZ-1048';
  1194. case 'csiso19latingreek':
  1195. case 'isoir19':
  1196. case 'latingreek':
  1197. return 'latin-greek';
  1198. case 'csiso27latingreek1':
  1199. case 'isoir27':
  1200. case 'latingreek1':
  1201. return 'Latin-greek-1';
  1202. case 'csiso158lap':
  1203. case 'isoir158':
  1204. case 'lap':
  1205. case 'latinlap':
  1206. return 'latin-lap';
  1207. case 'csmacintosh':
  1208. case 'mac':
  1209. case 'macintosh':
  1210. return 'macintosh';
  1211. case 'csmicrosoftpublishing':
  1212. case 'microsoftpublishing':
  1213. return 'Microsoft-Publishing';
  1214. case 'csmnem':
  1215. case 'mnem':
  1216. return 'MNEM';
  1217. case 'csmnemonic':
  1218. case 'mnemonic':
  1219. return 'MNEMONIC';
  1220. case 'csiso86hungarian':
  1221. case 'hu':
  1222. case 'iso646hu':
  1223. case 'isoir86':
  1224. case 'msz77953':
  1225. return 'MSZ_7795.3';
  1226. case 'csnatsdano':
  1227. case 'isoir91':
  1228. case 'natsdano':
  1229. return 'NATS-DANO';
  1230. case 'csnatsdanoadd':
  1231. case 'isoir92':
  1232. case 'natsdanoadd':
  1233. return 'NATS-DANO-ADD';
  1234. case 'csnatssefi':
  1235. case 'isoir81':
  1236. case 'natssefi':
  1237. return 'NATS-SEFI';
  1238. case 'csnatssefiadd':
  1239. case 'isoir82':
  1240. case 'natssefiadd':
  1241. return 'NATS-SEFI-ADD';
  1242. case 'csiso151cuba':
  1243. case 'cuba':
  1244. case 'iso646cu':
  1245. case 'isoir151':
  1246. case 'ncnc1081':
  1247. return 'NC_NC00-10:81';
  1248. case 'csiso69french':
  1249. case 'fr':
  1250. case 'iso646fr':
  1251. case 'isoir69':
  1252. case 'nfz62010':
  1253. return 'NF_Z_62-010';
  1254. case 'csiso25french':
  1255. case 'iso646fr1':
  1256. case 'isoir25':
  1257. case 'nfz620101973':
  1258. return 'NF_Z_62-010_(1973)';
  1259. case 'csiso60danishnorwegian':
  1260. case 'csiso60norwegian1':
  1261. case 'iso646no':
  1262. case 'isoir60':
  1263. case 'no':
  1264. case 'ns45511':
  1265. return 'NS_4551-1';
  1266. case 'csiso61norwegian2':
  1267. case 'iso646no2':
  1268. case 'isoir61':
  1269. case 'no2':
  1270. case 'ns45512':
  1271. return 'NS_4551-2';
  1272. case 'osdebcdicdf3irv':
  1273. return 'OSD_EBCDIC_DF03_IRV';
  1274. case 'osdebcdicdf41':
  1275. return 'OSD_EBCDIC_DF04_1';
  1276. case 'osdebcdicdf415':
  1277. return 'OSD_EBCDIC_DF04_15';
  1278. case 'cspc8danishnorwegian':
  1279. case 'pc8danishnorwegian':
  1280. return 'PC8-Danish-Norwegian';
  1281. case 'cspc8turkish':
  1282. case 'pc8turkish':
  1283. return 'PC8-Turkish';
  1284. case 'csiso16portuguese':
  1285. case 'iso646pt':
  1286. case 'isoir16':
  1287. case 'pt':
  1288. return 'PT';
  1289. case 'csiso84portuguese2':
  1290. case 'iso646pt2':
  1291. case 'isoir84':
  1292. case 'pt2':
  1293. return 'PT2';
  1294. case 'cp154':
  1295. case 'csptcp154':
  1296. case 'cyrillicasian':
  1297. case 'pt154':
  1298. case 'ptcp154':
  1299. return 'PTCP154';
  1300. case 'scsu':
  1301. return 'SCSU';
  1302. case 'csiso10swedish':
  1303. case 'fi':
  1304. case 'iso646fi':
  1305. case 'iso646se':
  1306. case 'isoir10':
  1307. case 'se':
  1308. case 'sen850200b':
  1309. return 'SEN_850200_B';
  1310. case 'csiso11swedishfornames':
  1311. case 'iso646se2':
  1312. case 'isoir11':
  1313. case 'se2':
  1314. case 'sen850200c':
  1315. return 'SEN_850200_C';
  1316. case 'csiso102t617bit':
  1317. case 'isoir102':
  1318. case 't617bit':
  1319. return 'T.61-7bit';
  1320. case 'csiso103t618bit':
  1321. case 'isoir103':
  1322. case 't61':
  1323. case 't618bit':
  1324. return 'T.61-8bit';
  1325. case 'csiso128t101g2':
  1326. case 'isoir128':
  1327. case 't101g2':
  1328. return 'T.101-G2';
  1329. case 'cstscii':
  1330. case 'tscii':
  1331. return 'TSCII';
  1332. case 'csunicode11':
  1333. case 'unicode11':
  1334. return 'UNICODE-1-1';
  1335. case 'csunicode11utf7':
  1336. case 'unicode11utf7':
  1337. return 'UNICODE-1-1-UTF-7';
  1338. case 'csunknown8bit':
  1339. case 'unknown8bit':
  1340. return 'UNKNOWN-8BIT';
  1341. case 'ansix341968':
  1342. case 'ansix341986':
  1343. case 'ascii':
  1344. case 'cp367':
  1345. case 'csascii':
  1346. case 'ibm367':
  1347. case 'iso646irv1991':
  1348. case 'iso646us':
  1349. case 'isoir6':
  1350. case 'us':
  1351. case 'usascii':
  1352. return 'US-ASCII';
  1353. case 'csusdk':
  1354. case 'usdk':
  1355. return 'us-dk';
  1356. case 'utf7':
  1357. return 'UTF-7';
  1358. case 'utf8':
  1359. return 'UTF-8';
  1360. case 'utf16':
  1361. return 'UTF-16';
  1362. case 'utf16be':
  1363. return 'UTF-16BE';
  1364. case 'utf16le':
  1365. return 'UTF-16LE';
  1366. case 'utf32':
  1367. return 'UTF-32';
  1368. case 'utf32be':
  1369. return 'UTF-32BE';
  1370. case 'utf32le':
  1371. return 'UTF-32LE';
  1372. case 'csventurainternational':
  1373. case 'venturainternational':
  1374. return 'Ventura-International';
  1375. case 'csventuramath':
  1376. case 'venturamath':
  1377. return 'Ventura-Math';
  1378. case 'csventuraus':
  1379. case 'venturaus':
  1380. return 'Ventura-US';
  1381. case 'csiso70videotexsupp1':
  1382. case 'isoir70':
  1383. case 'videotexsuppl':
  1384. return 'videotex-suppl';
  1385. case 'csviqr':
  1386. case 'viqr':
  1387. return 'VIQR';
  1388. case 'csviscii':
  1389. case 'viscii':
  1390. return 'VISCII';
  1391. case 'csshiftjis':
  1392. case 'cswindows31j':
  1393. case 'mskanji':
  1394. case 'shiftjis':
  1395. case 'windows31j':
  1396. return 'Windows-31J';
  1397. case 'iso885911':
  1398. case 'tis620':
  1399. return 'windows-874';
  1400. case 'cseuckr':
  1401. case 'csksc56011987':
  1402. case 'euckr':
  1403. case 'isoir149':
  1404. case 'korean':
  1405. case 'ksc5601':
  1406. case 'ksc56011987':
  1407. case 'ksc56011989':
  1408. case 'windows949':
  1409. return 'windows-949';
  1410. case 'windows1250':
  1411. return 'windows-1250';
  1412. case 'windows1251':
  1413. return 'windows-1251';
  1414. case 'cp819':
  1415. case 'csisolatin1':
  1416. case 'ibm819':
  1417. case 'iso88591':
  1418. case 'iso885911987':
  1419. case 'isoir100':
  1420. case 'l1':
  1421. case 'latin1':
  1422. case 'windows1252':
  1423. return 'windows-1252';
  1424. case 'windows1253':
  1425. return 'windows-1253';
  1426. case 'csisolatin5':
  1427. case 'iso88599':
  1428. case 'iso885991989':
  1429. case 'isoir148':
  1430. case 'l5':
  1431. case 'latin5':
  1432. case 'windows1254':
  1433. return 'windows-1254';
  1434. case 'windows1255':
  1435. return 'windows-1255';
  1436. case 'windows1256':
  1437. return 'windows-1256';
  1438. case 'windows1257':
  1439. return 'windows-1257';
  1440. case 'windows1258':
  1441. return 'windows-1258';
  1442. default:
  1443. return $charset;
  1444. }
  1445. }
  1446. public static function get_curl_version()
  1447. {
  1448. if (is_array($curl = curl_version()))
  1449. {
  1450. $curl = $curl['version'];
  1451. }
  1452. elseif (substr($curl, 0, 5) === 'curl/')
  1453. {
  1454. $curl = substr($curl, 5, strcspn($curl, "\x09\x0A\x0B\x0C\x0D", 5));
  1455. }
  1456. elseif (substr($curl, 0, 8) === 'libcurl/')
  1457. {
  1458. $curl = substr($curl, 8, strcspn($curl, "\x09\x0A\x0B\x0C\x0D", 8));
  1459. }
  1460. else
  1461. {
  1462. $curl = 0;
  1463. }
  1464. return $curl;
  1465. }
  1466. /**
  1467. * Strip HTML comments
  1468. *
  1469. * @param string $data Data to strip comments from
  1470. * @return string Comment stripped string
  1471. */
  1472. public static function strip_comments($data)
  1473. {
  1474. $output = '';
  1475. while (($start = strpos($data, '<!--')) !== false)
  1476. {
  1477. $output .= substr($data, 0, $start);
  1478. if (($end = strpos($data, '-->', $start)) !== false)
  1479. {
  1480. $data = substr_replace($data, '', 0, $end + 3);
  1481. }
  1482. else
  1483. {
  1484. $data = '';
  1485. }
  1486. }
  1487. return $output . $data;
  1488. }
  1489. public static function parse_date($dt)
  1490. {
  1491. $parser = SimplePie_Parse_Date::get();
  1492. return $parser->parse($dt);
  1493. }
  1494. /**
  1495. * Decode HTML entities
  1496. *
  1497. * @deprecated Use DOMDocument instead
  1498. * @param string $data Input data
  1499. * @return string Output data
  1500. */
  1501. public static function entities_decode($data)
  1502. {
  1503. $decoder = new SimplePie_Decode_HTML_Entities($data);
  1504. return $decoder->parse();
  1505. }
  1506. /**
  1507. * Remove RFC822 comments
  1508. *
  1509. * @param string $data Data to strip comments from
  1510. * @return string Comment stripped string
  1511. */
  1512. public static function uncomment_rfc822($string)
  1513. {
  1514. $string = (string) $string;
  1515. $position = 0;
  1516. $length = strlen($string);
  1517. $depth = 0;
  1518. $output = '';
  1519. while ($position < $length && ($pos = strpos($string, '(', $position)) !== false)
  1520. {
  1521. $output .= substr($string, $position, $pos - $position);
  1522. $position = $pos + 1;
  1523. if ($string[$pos - 1] !== '\\')
  1524. {
  1525. $depth++;
  1526. while ($depth && $position < $length)
  1527. {
  1528. $position += strcspn($string, '()', $position);
  1529. if ($string[$position - 1] === '\\')
  1530. {
  1531. $position++;
  1532. continue;
  1533. }
  1534. elseif (isset($string[$position]))
  1535. {
  1536. switch ($string[$position])
  1537. {
  1538. case '(':
  1539. $depth++;
  1540. break;
  1541. case ')':
  1542. $depth--;
  1543. break;
  1544. }
  1545. $position++;
  1546. }
  1547. else
  1548. {
  1549. break;
  1550. }
  1551. }
  1552. }
  1553. else
  1554. {
  1555. $output .= '(';
  1556. }
  1557. }
  1558. $output .= substr($string, $position);
  1559. return $output;
  1560. }
  1561. public static function parse_mime($mime)
  1562. {
  1563. if (($pos = strpos($mime, ';')) === false)
  1564. {
  1565. return trim($mime);
  1566. }
  1567. else
  1568. {
  1569. return trim(substr($mime, 0, $pos));
  1570. }
  1571. }
  1572. public static function atom_03_construct_type($attribs)
  1573. {
  1574. if (isset($attribs['']['mode']) && strtolower(trim($attribs['']['mode']) === 'base64'))
  1575. {
  1576. $mode = SIMPLEPIE_CONSTRUCT_BASE64;
  1577. }
  1578. else
  1579. {
  1580. $mode = SIMPLEPIE_CONSTRUCT_NONE;
  1581. }
  1582. if (isset($attribs['']['type']))
  1583. {
  1584. switch (strtolower(trim($attribs['']['type'])))
  1585. {
  1586. case 'text':
  1587. case 'text/plain':
  1588. return SIMPLEPIE_CONSTRUCT_TEXT | $mode;
  1589. case 'html':
  1590. case 'text/html':
  1591. return SIMPLEPIE_CONSTRUCT_HTML | $mode;
  1592. case 'xhtml':
  1593. case 'application/xhtml+xml':
  1594. return SIMPLEPIE_CONSTRUCT_XHTML | $mode;
  1595. default:
  1596. return SIMPLEPIE_CONSTRUCT_NONE | $mode;
  1597. }
  1598. }
  1599. else
  1600. {
  1601. return SIMPLEPIE_CONSTRUCT_TEXT | $mode;
  1602. }
  1603. }
  1604. public static function atom_10_construct_type($attribs)
  1605. {
  1606. if (isset($attribs['']['type']))
  1607. {
  1608. switch (strtolower(trim($attribs['']['type'])))
  1609. {
  1610. case 'text':
  1611. return SIMPLEPIE_CONSTRUCT_TEXT;
  1612. case 'html':
  1613. return SIMPLEPIE_CONSTRUCT_HTML;
  1614. case 'xhtml':
  1615. return SIMPLEPIE_CONSTRUCT_XHTML;
  1616. default:
  1617. return SIMPLEPIE_CONSTRUCT_NONE;
  1618. }
  1619. }
  1620. return SIMPLEPIE_CONSTRUCT_TEXT;
  1621. }
  1622. public static function atom_10_content_construct_type($attribs)
  1623. {
  1624. if (isset($attribs['']['type']))
  1625. {
  1626. $type = strtolower(trim($attribs['']['type']));
  1627. switch ($type)
  1628. {
  1629. case 'text':
  1630. return SIMPLEPIE_CONSTRUCT_TEXT;
  1631. case 'html':
  1632. return SIMPLEPIE_CONSTRUCT_HTML;
  1633. case 'xhtml':
  1634. return SIMPLEPIE_CONSTRUCT_XHTML;
  1635. }
  1636. if (in_array(substr($type, -4), array('+xml', '/xml')) || substr($type, 0, 5) === 'text/')
  1637. {
  1638. return SIMPLEPIE_CONSTRUCT_NONE;
  1639. }
  1640. else
  1641. {
  1642. return SIMPLEPIE_CONSTRUCT_BASE64;
  1643. }
  1644. }
  1645. else
  1646. {
  1647. return SIMPLEPIE_CONSTRUCT_TEXT;
  1648. }
  1649. }
  1650. public static function is_isegment_nz_nc($string)
  1651. {
  1652. 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);
  1653. }
  1654. public static function space_seperated_tokens($string)
  1655. {
  1656. $space_characters = "\x20\x09\x0A\x0B\x0C\x0D";
  1657. $string_length = strlen($string);
  1658. $position = strspn($string, $space_characters);
  1659. $tokens = array();
  1660. while ($position < $string_length)
  1661. {
  1662. $len = strcspn($string, $space_characters, $position);
  1663. $tokens[] = substr($string, $position, $len);
  1664. $position += $len;
  1665. $position += strspn($string, $space_characters, $position);
  1666. }
  1667. return $tokens;
  1668. }
  1669. /**
  1670. * Converts a unicode codepoint to a UTF-8 character
  1671. *
  1672. * @static
  1673. * @param int $codepoint Unicode codepoint
  1674. * @return string UTF-8 character
  1675. */
  1676. public static function codepoint_to_utf8($codepoint)
  1677. {
  1678. $codepoint = (int) $codepoint;
  1679. if ($codepoint < 0)
  1680. {
  1681. return false;
  1682. }
  1683. else if ($codepoint <= 0x7f)
  1684. {
  1685. return chr($codepoint);
  1686. }
  1687. else if ($codepoint <= 0x7ff)
  1688. {
  1689. return chr(0xc0 | ($codepoint >> 6)) . chr(0x80 | ($codepoint & 0x3f));
  1690. }
  1691. else if ($codepoint <= 0xffff)
  1692. {
  1693. return chr(0xe0 | ($codepoint >> 12)) . chr(0x80 | (($codepoint >> 6) & 0x3f)) . chr(0x80 | ($codepoint & 0x3f));
  1694. }
  1695. else if ($codepoint <= 0x10ffff)
  1696. {
  1697. return chr(0xf0 | ($codepoint >> 18)) . chr(0x80 | (($codepoint >> 12) & 0x3f)) . chr(0x80 | (($codepoint >> 6) & 0x3f)) . chr(0x80 | ($codepoint & 0x3f));
  1698. }
  1699. else
  1700. {
  1701. // U+FFFD REPLACEMENT CHARACTER
  1702. return "\xEF\xBF\xBD";
  1703. }
  1704. }
  1705. /**
  1706. * Similar to parse_str()
  1707. *
  1708. * Returns an associative array of name/value pairs, where the value is an
  1709. * array of values that have used the same name
  1710. *
  1711. * @static
  1712. * @param string $str The input string.
  1713. * @return array
  1714. */
  1715. public static function parse_str($str)
  1716. {
  1717. $return = array();
  1718. $str = explode('&', $str);
  1719. foreach ($str as $section)
  1720. {
  1721. if (strpos($section, '=') !== false)
  1722. {
  1723. list($name, $value) = explode('=', $section, 2);
  1724. $return[urldecode($name)][] = urldecode($value);
  1725. }
  1726. else
  1727. {
  1728. $return[urldecode($section)][] = null;
  1729. }
  1730. }
  1731. return $return;
  1732. }
  1733. /**
  1734. * Detect XML encoding, as per XML 1.0 Appendix F.1
  1735. *
  1736. * @todo Add support for EBCDIC
  1737. * @param string $data XML data
  1738. * @param SimplePie_Registry $registry Class registry
  1739. * @return array Possible encodings
  1740. */
  1741. public static function xml_encoding($data, $registry)
  1742. {
  1743. // UTF-32 Big Endian BOM
  1744. if (substr($data, 0, 4) === "\x00\x00\xFE\xFF")
  1745. {
  1746. $encoding[] = 'UTF-32BE';
  1747. }
  1748. // UTF-32 Little Endian BOM
  1749. elseif (substr($data, 0, 4) === "\xFF\xFE\x00\x00")
  1750. {
  1751. $encoding[] = 'UTF-32LE';
  1752. }
  1753. // UTF-16 Big Endian BOM
  1754. elseif (substr($data, 0, 2) === "\xFE\xFF")
  1755. {
  1756. $encoding[] = 'UTF-16BE';
  1757. }
  1758. // UTF-16 Little Endian BOM
  1759. elseif (substr($data, 0, 2) === "\xFF\xFE")
  1760. {
  1761. $encoding[] = 'UTF-16LE';
  1762. }
  1763. // UTF-8 BOM
  1764. elseif (substr($data, 0, 3) === "\xEF\xBB\xBF")
  1765. {
  1766. $encoding[] = 'UTF-8';
  1767. }
  1768. // UTF-32 Big Endian Without BOM
  1769. 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")
  1770. {
  1771. if ($pos = strpos($data, "\x00\x00\x00\x3F\x00\x00\x00\x3E"))
  1772. {
  1773. $parser = $registry->create('XML_Declaration_Parser', array(SimplePie_Misc::change_encoding(substr($data, 20, $pos - 20), 'UTF-32BE', 'UTF-8')));
  1774. if ($parser->parse())
  1775. {
  1776. $encoding[] = $parser->encoding;
  1777. }
  1778. }
  1779. $encoding[] = 'UTF-32BE';
  1780. }
  1781. // UTF-32 Little Endian Without BOM
  1782. 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")
  1783. {
  1784. if ($pos = strpos($data, "\x3F\x00\x00\x00\x3E\x00\x00\x00"))
  1785. {
  1786. $parser = $registry->create('XML_Declaration_Parser', array(SimplePie_Misc::change_encoding(substr($data, 20, $pos - 20), 'UTF-32LE', 'UTF-8')));
  1787. if ($parser->parse())
  1788. {
  1789. $encoding[] = $parser->encoding;
  1790. }
  1791. }
  1792. $encoding[] = 'UTF-32LE';
  1793. }
  1794. // UTF-16 Big Endian Without BOM
  1795. elseif (substr($data, 0, 10) === "\x00\x3C\x00\x3F\x00\x78\x00\x6D\x00\x6C")
  1796. {
  1797. if ($pos = strpos($data, "\x00\x3F\x00\x3E"))
  1798. {
  1799. $parser = $registry->create('XML_Declaration_Parser', array(SimplePie_Misc::change_encoding(substr($data, 20, $pos - 10), 'UTF-16BE', 'UTF-8')));
  1800. if ($parser->parse())
  1801. {
  1802. $encoding[] = $parser->encoding;
  1803. }
  1804. }
  1805. $encoding[] = 'UTF-16BE';
  1806. }
  1807. // UTF-16 Little Endian Without BOM
  1808. elseif (substr($data, 0, 10) === "\x3C\x00\x3F\x00\x78\x00\x6D\x00\x6C\x00")
  1809. {
  1810. if ($pos = strpos($data, "\x3F\x00\x3E\x00"))
  1811. {
  1812. $parser = $registry->create('XML_Declaration_Parser', array(SimplePie_Misc::change_encoding(substr($data, 20, $pos - 10), 'UTF-16LE', 'UTF-8')));
  1813. if ($parser->parse())
  1814. {
  1815. $encoding[] = $parser->encoding;
  1816. }
  1817. }
  1818. $encoding[] = 'UTF-16LE';
  1819. }
  1820. // US-ASCII (or superset)
  1821. elseif (substr($data, 0, 5) === "\x3C\x3F\x78\x6D\x6C")
  1822. {
  1823. if ($pos = strpos($data, "\x3F\x3E"))
  1824. {
  1825. $parser = $registry->create('XML_Declaration_Parser', array(substr($data, 5, $pos - 5)));
  1826. if ($parser->parse())
  1827. {
  1828. $encoding[] = $parser->encoding;
  1829. }
  1830. }
  1831. $encoding[] = 'UTF-8';
  1832. }
  1833. // Fallback to UTF-8
  1834. else
  1835. {
  1836. $encoding[] = 'UTF-8';
  1837. }
  1838. return $encoding;
  1839. }
  1840. public static function output_javascript()
  1841. {
  1842. if (function_exists('ob_gzhandler'))
  1843. {
  1844. ob_start('ob_gzhandler');
  1845. }
  1846. header('Content-type: text/javascript; charset: UTF-8');
  1847. header('Cache-Control: must-revalidate');
  1848. header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 604800) . ' GMT'); // 7 days
  1849. ?>
  1850. function embed_quicktime(type, bgcolor, width, height, link, placeholder, loop) {
  1851. if (placeholder != '') {
  1852. 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>');
  1853. }
  1854. else {
  1855. 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>');
  1856. }
  1857. }
  1858. function embed_flash(bgcolor, width, height, link, loop, type) {
  1859. 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>');
  1860. }
  1861. function embed_flv(width, height, link, placeholder, loop, player) {
  1862. 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>');
  1863. }
  1864. function embed_wmedia(width, height, link) {
  1865. document.writeln('<embed type="application/x-mplayer2" src="'+link+'" autosize="1" width="'+width+'" height="'+height+'" showcontrols="1" showstatusbar="0" showdisplay="0" autostart="0"></embed>');
  1866. }
  1867. <?php
  1868. }
  1869. /**
  1870. * Get the SimplePie build timestamp
  1871. *
  1872. * Return SimplePie.php modification time.
  1873. */
  1874. public static function get_build()
  1875. {
  1876. $mtime = @filemtime(dirname(dirname(__FILE__)) . '/SimplePie.php'); //FreshRSS
  1877. return $mtime ? $mtime : filemtime(__FILE__);
  1878. }
  1879. /**
  1880. * Format debugging information
  1881. */
  1882. public static function debug(&$sp)
  1883. {
  1884. $info = 'SimplePie ' . SIMPLEPIE_VERSION . ' Build ' . SIMPLEPIE_BUILD . "\n";
  1885. $info .= 'PHP ' . PHP_VERSION . "\n";
  1886. if ($sp->error() !== null)
  1887. {
  1888. $info .= 'Error occurred: ' . $sp->error() . "\n";
  1889. }
  1890. else
  1891. {
  1892. $info .= "No error found.\n";
  1893. }
  1894. $info .= "Extensions:\n";
  1895. $extensions = array('pcre', 'curl', 'zlib', 'mbstring', 'iconv', 'xmlreader', 'xml');
  1896. foreach ($extensions as $ext)
  1897. {
  1898. if (extension_loaded($ext))
  1899. {
  1900. $info .= " $ext loaded\n";
  1901. switch ($ext)
  1902. {
  1903. case 'pcre':
  1904. $info .= ' Version ' . PCRE_VERSION . "\n";
  1905. break;
  1906. case 'curl':
  1907. $version = curl_version();
  1908. $info .= ' Version ' . $version['version'] . "\n";
  1909. break;
  1910. case 'mbstring':
  1911. $info .= ' Overloading: ' . mb_get_info('func_overload') . "\n";
  1912. break;
  1913. case 'iconv':
  1914. $info .= ' Version ' . ICONV_VERSION . "\n";
  1915. break;
  1916. case 'xml':
  1917. $info .= ' Version ' . LIBXML_DOTTED_VERSION . "\n";
  1918. break;
  1919. }
  1920. }
  1921. else
  1922. {
  1923. $info .= " $ext not loaded\n";
  1924. }
  1925. }
  1926. return $info;
  1927. }
  1928. public static function silence_errors($num, $str)
  1929. {
  1930. // No-op
  1931. }
  1932. /**
  1933. * Sanitize a URL by removing HTTP credentials.
  1934. * @param $url the URL to sanitize.
  1935. * @return the same URL without HTTP credentials.
  1936. */
  1937. public static function url_remove_credentials($url) //FreshRSS
  1938. {
  1939. return preg_replace('#^(https?://)[^/:@]+:[^/:@]+@#i', '$1', $url);
  1940. }
  1941. }