Misc.php 51 KB

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