class-simplepie.php 96 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354
  1. <?php
  2. if ( ! class_exists( 'SimplePie', false ) ) :
  3. // Load classes we will need.
  4. require ABSPATH . WPINC . '/SimplePie/Misc.php';
  5. require ABSPATH . WPINC . '/SimplePie/Cache.php';
  6. require ABSPATH . WPINC . '/SimplePie/File.php';
  7. require ABSPATH . WPINC . '/SimplePie/Sanitize.php';
  8. require ABSPATH . WPINC . '/SimplePie/Registry.php';
  9. require ABSPATH . WPINC . '/SimplePie/IRI.php';
  10. require ABSPATH . WPINC . '/SimplePie/Locator.php';
  11. require ABSPATH . WPINC . '/SimplePie/Content/Type/Sniffer.php';
  12. require ABSPATH . WPINC . '/SimplePie/XML/Declaration/Parser.php';
  13. require ABSPATH . WPINC . '/SimplePie/Parser.php';
  14. require ABSPATH . WPINC . '/SimplePie/Item.php';
  15. require ABSPATH . WPINC . '/SimplePie/Parse/Date.php';
  16. require ABSPATH . WPINC . '/SimplePie/Author.php';
  17. /**
  18. * WordPress autoloader for SimplePie.
  19. *
  20. * @since 3.5.0
  21. */
  22. function wp_simplepie_autoload( $class ) {
  23. if ( 0 !== strpos( $class, 'SimplePie_' ) )
  24. return;
  25. $file = ABSPATH . WPINC . '/' . str_replace( '_', '/', $class ) . '.php';
  26. include $file;
  27. }
  28. /**
  29. * We autoload classes we may not need.
  30. */
  31. spl_autoload_register( 'wp_simplepie_autoload' );
  32. /**
  33. * SimplePie
  34. *
  35. * A PHP-Based RSS and Atom Feed Framework.
  36. * Takes the hard work out of managing a complete RSS/Atom solution.
  37. *
  38. * Copyright (c) 2004-2017, Ryan Parman, Sam Sneddon, Ryan McCue, and contributors
  39. * All rights reserved.
  40. *
  41. * Redistribution and use in source and binary forms, with or without modification, are
  42. * permitted provided that the following conditions are met:
  43. *
  44. * * Redistributions of source code must retain the above copyright notice, this list of
  45. * conditions and the following disclaimer.
  46. *
  47. * * Redistributions in binary form must reproduce the above copyright notice, this list
  48. * of conditions and the following disclaimer in the documentation and/or other materials
  49. * provided with the distribution.
  50. *
  51. * * Neither the name of the SimplePie Team nor the names of its contributors may be used
  52. * to endorse or promote products derived from this software without specific prior
  53. * written permission.
  54. *
  55. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  56. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  57. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS
  58. * AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  59. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  60. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  61. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  62. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  63. * POSSIBILITY OF SUCH DAMAGE.
  64. *
  65. * @package SimplePie
  66. * @version 1.5.8
  67. * @copyright 2004-2017 Ryan Parman, Sam Sneddon, Ryan McCue
  68. * @author Ryan Parman
  69. * @author Sam Sneddon
  70. * @author Ryan McCue
  71. * @link http://simplepie.org/ SimplePie
  72. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  73. */
  74. /**
  75. * SimplePie Name
  76. */
  77. define('SIMPLEPIE_NAME', 'SimplePie');
  78. /**
  79. * SimplePie Version
  80. */
  81. define('SIMPLEPIE_VERSION', '1.5.8');
  82. /**
  83. * SimplePie Build
  84. * @todo Hardcode for release (there's no need to have to call SimplePie_Misc::get_build() only every load of simplepie.inc)
  85. */
  86. define('SIMPLEPIE_BUILD', gmdate('YmdHis', SimplePie_Misc::get_build()));
  87. /**
  88. * SimplePie Website URL
  89. */
  90. define('SIMPLEPIE_URL', 'http://simplepie.org');
  91. /**
  92. * SimplePie Useragent
  93. * @see SimplePie::set_useragent()
  94. */
  95. define('SIMPLEPIE_USERAGENT', SIMPLEPIE_NAME . '/' . SIMPLEPIE_VERSION . ' (Feed Parser; ' . SIMPLEPIE_URL . '; Allow like Gecko) Build/' . SIMPLEPIE_BUILD);
  96. /**
  97. * SimplePie Linkback
  98. */
  99. define('SIMPLEPIE_LINKBACK', '<a href="' . SIMPLEPIE_URL . '" title="' . SIMPLEPIE_NAME . ' ' . SIMPLEPIE_VERSION . '">' . SIMPLEPIE_NAME . '</a>');
  100. /**
  101. * No Autodiscovery
  102. * @see SimplePie::set_autodiscovery_level()
  103. */
  104. define('SIMPLEPIE_LOCATOR_NONE', 0);
  105. /**
  106. * Feed Link Element Autodiscovery
  107. * @see SimplePie::set_autodiscovery_level()
  108. */
  109. define('SIMPLEPIE_LOCATOR_AUTODISCOVERY', 1);
  110. /**
  111. * Local Feed Extension Autodiscovery
  112. * @see SimplePie::set_autodiscovery_level()
  113. */
  114. define('SIMPLEPIE_LOCATOR_LOCAL_EXTENSION', 2);
  115. /**
  116. * Local Feed Body Autodiscovery
  117. * @see SimplePie::set_autodiscovery_level()
  118. */
  119. define('SIMPLEPIE_LOCATOR_LOCAL_BODY', 4);
  120. /**
  121. * Remote Feed Extension Autodiscovery
  122. * @see SimplePie::set_autodiscovery_level()
  123. */
  124. define('SIMPLEPIE_LOCATOR_REMOTE_EXTENSION', 8);
  125. /**
  126. * Remote Feed Body Autodiscovery
  127. * @see SimplePie::set_autodiscovery_level()
  128. */
  129. define('SIMPLEPIE_LOCATOR_REMOTE_BODY', 16);
  130. /**
  131. * All Feed Autodiscovery
  132. * @see SimplePie::set_autodiscovery_level()
  133. */
  134. define('SIMPLEPIE_LOCATOR_ALL', 31);
  135. /**
  136. * No known feed type
  137. */
  138. define('SIMPLEPIE_TYPE_NONE', 0);
  139. /**
  140. * RSS 0.90
  141. */
  142. define('SIMPLEPIE_TYPE_RSS_090', 1);
  143. /**
  144. * RSS 0.91 (Netscape)
  145. */
  146. define('SIMPLEPIE_TYPE_RSS_091_NETSCAPE', 2);
  147. /**
  148. * RSS 0.91 (Userland)
  149. */
  150. define('SIMPLEPIE_TYPE_RSS_091_USERLAND', 4);
  151. /**
  152. * RSS 0.91 (both Netscape and Userland)
  153. */
  154. define('SIMPLEPIE_TYPE_RSS_091', 6);
  155. /**
  156. * RSS 0.92
  157. */
  158. define('SIMPLEPIE_TYPE_RSS_092', 8);
  159. /**
  160. * RSS 0.93
  161. */
  162. define('SIMPLEPIE_TYPE_RSS_093', 16);
  163. /**
  164. * RSS 0.94
  165. */
  166. define('SIMPLEPIE_TYPE_RSS_094', 32);
  167. /**
  168. * RSS 1.0
  169. */
  170. define('SIMPLEPIE_TYPE_RSS_10', 64);
  171. /**
  172. * RSS 2.0
  173. */
  174. define('SIMPLEPIE_TYPE_RSS_20', 128);
  175. /**
  176. * RDF-based RSS
  177. */
  178. define('SIMPLEPIE_TYPE_RSS_RDF', 65);
  179. /**
  180. * Non-RDF-based RSS (truly intended as syndication format)
  181. */
  182. define('SIMPLEPIE_TYPE_RSS_SYNDICATION', 190);
  183. /**
  184. * All RSS
  185. */
  186. define('SIMPLEPIE_TYPE_RSS_ALL', 255);
  187. /**
  188. * Atom 0.3
  189. */
  190. define('SIMPLEPIE_TYPE_ATOM_03', 256);
  191. /**
  192. * Atom 1.0
  193. */
  194. define('SIMPLEPIE_TYPE_ATOM_10', 512);
  195. /**
  196. * All Atom
  197. */
  198. define('SIMPLEPIE_TYPE_ATOM_ALL', 768);
  199. /**
  200. * All feed types
  201. */
  202. define('SIMPLEPIE_TYPE_ALL', 1023);
  203. /**
  204. * No construct
  205. */
  206. define('SIMPLEPIE_CONSTRUCT_NONE', 0);
  207. /**
  208. * Text construct
  209. */
  210. define('SIMPLEPIE_CONSTRUCT_TEXT', 1);
  211. /**
  212. * HTML construct
  213. */
  214. define('SIMPLEPIE_CONSTRUCT_HTML', 2);
  215. /**
  216. * XHTML construct
  217. */
  218. define('SIMPLEPIE_CONSTRUCT_XHTML', 4);
  219. /**
  220. * base64-encoded construct
  221. */
  222. define('SIMPLEPIE_CONSTRUCT_BASE64', 8);
  223. /**
  224. * IRI construct
  225. */
  226. define('SIMPLEPIE_CONSTRUCT_IRI', 16);
  227. /**
  228. * A construct that might be HTML
  229. */
  230. define('SIMPLEPIE_CONSTRUCT_MAYBE_HTML', 32);
  231. /**
  232. * All constructs
  233. */
  234. define('SIMPLEPIE_CONSTRUCT_ALL', 63);
  235. /**
  236. * Don't change case
  237. */
  238. define('SIMPLEPIE_SAME_CASE', 1);
  239. /**
  240. * Change to lowercase
  241. */
  242. define('SIMPLEPIE_LOWERCASE', 2);
  243. /**
  244. * Change to uppercase
  245. */
  246. define('SIMPLEPIE_UPPERCASE', 4);
  247. /**
  248. * PCRE for HTML attributes
  249. */
  250. define('SIMPLEPIE_PCRE_HTML_ATTRIBUTE', '((?:[\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]*)?))?)*)[\x09\x0A\x0B\x0C\x0D\x20]*');
  251. /**
  252. * PCRE for XML attributes
  253. */
  254. define('SIMPLEPIE_PCRE_XML_ATTRIBUTE', '((?:\s+(?:(?:[^\s:]+:)?[^\s:]+)\s*=\s*(?:"(?:[^"]*)"|\'(?:[^\']*)\'))*)\s*');
  255. /**
  256. * XML Namespace
  257. */
  258. define('SIMPLEPIE_NAMESPACE_XML', 'http://www.w3.org/XML/1998/namespace');
  259. /**
  260. * Atom 1.0 Namespace
  261. */
  262. define('SIMPLEPIE_NAMESPACE_ATOM_10', 'http://www.w3.org/2005/Atom');
  263. /**
  264. * Atom 0.3 Namespace
  265. */
  266. define('SIMPLEPIE_NAMESPACE_ATOM_03', 'http://purl.org/atom/ns#');
  267. /**
  268. * RDF Namespace
  269. */
  270. define('SIMPLEPIE_NAMESPACE_RDF', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#');
  271. /**
  272. * RSS 0.90 Namespace
  273. */
  274. define('SIMPLEPIE_NAMESPACE_RSS_090', 'http://my.netscape.com/rdf/simple/0.9/');
  275. /**
  276. * RSS 1.0 Namespace
  277. */
  278. define('SIMPLEPIE_NAMESPACE_RSS_10', 'http://purl.org/rss/1.0/');
  279. /**
  280. * RSS 1.0 Content Module Namespace
  281. */
  282. define('SIMPLEPIE_NAMESPACE_RSS_10_MODULES_CONTENT', 'http://purl.org/rss/1.0/modules/content/');
  283. /**
  284. * RSS 2.0 Namespace
  285. * (Stupid, I know, but I'm certain it will confuse people less with support.)
  286. */
  287. define('SIMPLEPIE_NAMESPACE_RSS_20', '');
  288. /**
  289. * DC 1.0 Namespace
  290. */
  291. define('SIMPLEPIE_NAMESPACE_DC_10', 'http://purl.org/dc/elements/1.0/');
  292. /**
  293. * DC 1.1 Namespace
  294. */
  295. define('SIMPLEPIE_NAMESPACE_DC_11', 'http://purl.org/dc/elements/1.1/');
  296. /**
  297. * W3C Basic Geo (WGS84 lat/long) Vocabulary Namespace
  298. */
  299. define('SIMPLEPIE_NAMESPACE_W3C_BASIC_GEO', 'http://www.w3.org/2003/01/geo/wgs84_pos#');
  300. /**
  301. * GeoRSS Namespace
  302. */
  303. define('SIMPLEPIE_NAMESPACE_GEORSS', 'http://www.georss.org/georss');
  304. /**
  305. * Media RSS Namespace
  306. */
  307. define('SIMPLEPIE_NAMESPACE_MEDIARSS', 'http://search.yahoo.com/mrss/');
  308. /**
  309. * Wrong Media RSS Namespace. Caused by a long-standing typo in the spec.
  310. */
  311. define('SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG', 'http://search.yahoo.com/mrss');
  312. /**
  313. * Wrong Media RSS Namespace #2. New namespace introduced in Media RSS 1.5.
  314. */
  315. define('SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG2', 'http://video.search.yahoo.com/mrss');
  316. /**
  317. * Wrong Media RSS Namespace #3. A possible typo of the Media RSS 1.5 namespace.
  318. */
  319. define('SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG3', 'http://video.search.yahoo.com/mrss/');
  320. /**
  321. * Wrong Media RSS Namespace #4. New spec location after the RSS Advisory Board takes it over, but not a valid namespace.
  322. */
  323. define('SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG4', 'http://www.rssboard.org/media-rss');
  324. /**
  325. * Wrong Media RSS Namespace #5. A possible typo of the RSS Advisory Board URL.
  326. */
  327. define('SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG5', 'http://www.rssboard.org/media-rss/');
  328. /**
  329. * iTunes RSS Namespace
  330. */
  331. define('SIMPLEPIE_NAMESPACE_ITUNES', 'http://www.itunes.com/dtds/podcast-1.0.dtd');
  332. /**
  333. * XHTML Namespace
  334. */
  335. define('SIMPLEPIE_NAMESPACE_XHTML', 'http://www.w3.org/1999/xhtml');
  336. /**
  337. * IANA Link Relations Registry
  338. */
  339. define('SIMPLEPIE_IANA_LINK_RELATIONS_REGISTRY', 'http://www.iana.org/assignments/relation/');
  340. /**
  341. * No file source
  342. */
  343. define('SIMPLEPIE_FILE_SOURCE_NONE', 0);
  344. /**
  345. * Remote file source
  346. */
  347. define('SIMPLEPIE_FILE_SOURCE_REMOTE', 1);
  348. /**
  349. * Local file source
  350. */
  351. define('SIMPLEPIE_FILE_SOURCE_LOCAL', 2);
  352. /**
  353. * fsockopen() file source
  354. */
  355. define('SIMPLEPIE_FILE_SOURCE_FSOCKOPEN', 4);
  356. /**
  357. * cURL file source
  358. */
  359. define('SIMPLEPIE_FILE_SOURCE_CURL', 8);
  360. /**
  361. * file_get_contents() file source
  362. */
  363. define('SIMPLEPIE_FILE_SOURCE_FILE_GET_CONTENTS', 16);
  364. /**
  365. * SimplePie
  366. *
  367. * @package SimplePie
  368. * @subpackage API
  369. */
  370. class SimplePie
  371. {
  372. /**
  373. * @var array Raw data
  374. * @access private
  375. */
  376. public $data = array();
  377. /**
  378. * @var mixed Error string
  379. * @access private
  380. */
  381. public $error;
  382. /**
  383. * @var int HTTP status code
  384. * @see SimplePie::status_code()
  385. * @access private
  386. */
  387. public $status_code;
  388. /**
  389. * @var object Instance of SimplePie_Sanitize (or other class)
  390. * @see SimplePie::set_sanitize_class()
  391. * @access private
  392. */
  393. public $sanitize;
  394. /**
  395. * @var string SimplePie Useragent
  396. * @see SimplePie::set_useragent()
  397. * @access private
  398. */
  399. public $useragent = SIMPLEPIE_USERAGENT;
  400. /**
  401. * @var string Feed URL
  402. * @see SimplePie::set_feed_url()
  403. * @access private
  404. */
  405. public $feed_url;
  406. /**
  407. * @var string Original feed URL, or new feed URL iff HTTP 301 Moved Permanently
  408. * @see SimplePie::subscribe_url()
  409. * @access private
  410. */
  411. public $permanent_url = null;
  412. /**
  413. * @var object Instance of SimplePie_File to use as a feed
  414. * @see SimplePie::set_file()
  415. * @access private
  416. */
  417. public $file;
  418. /**
  419. * @var string Raw feed data
  420. * @see SimplePie::set_raw_data()
  421. * @access private
  422. */
  423. public $raw_data;
  424. /**
  425. * @var int Timeout for fetching remote files
  426. * @see SimplePie::set_timeout()
  427. * @access private
  428. */
  429. public $timeout = 10;
  430. /**
  431. * @var array Custom curl options
  432. * @see SimplePie::set_curl_options()
  433. * @access private
  434. */
  435. public $curl_options = array();
  436. /**
  437. * @var bool Forces fsockopen() to be used for remote files instead
  438. * of cURL, even if a new enough version is installed
  439. * @see SimplePie::force_fsockopen()
  440. * @access private
  441. */
  442. public $force_fsockopen = false;
  443. /**
  444. * @var bool Force the given data/URL to be treated as a feed no matter what
  445. * it appears like
  446. * @see SimplePie::force_feed()
  447. * @access private
  448. */
  449. public $force_feed = false;
  450. /**
  451. * @var bool Enable/Disable Caching
  452. * @see SimplePie::enable_cache()
  453. * @access private
  454. */
  455. public $cache = true;
  456. /**
  457. * @var bool Force SimplePie to fallback to expired cache, if enabled,
  458. * when feed is unavailable.
  459. * @see SimplePie::force_cache_fallback()
  460. * @access private
  461. */
  462. public $force_cache_fallback = false;
  463. /**
  464. * @var int Cache duration (in seconds)
  465. * @see SimplePie::set_cache_duration()
  466. * @access private
  467. */
  468. public $cache_duration = 3600;
  469. /**
  470. * @var int Auto-discovery cache duration (in seconds)
  471. * @see SimplePie::set_autodiscovery_cache_duration()
  472. * @access private
  473. */
  474. public $autodiscovery_cache_duration = 604800; // 7 Days.
  475. /**
  476. * @var string Cache location (relative to executing script)
  477. * @see SimplePie::set_cache_location()
  478. * @access private
  479. */
  480. public $cache_location = './cache';
  481. /**
  482. * @var string Function that creates the cache filename
  483. * @see SimplePie::set_cache_name_function()
  484. * @access private
  485. */
  486. public $cache_name_function = 'md5';
  487. /**
  488. * @var bool Reorder feed by date descending
  489. * @see SimplePie::enable_order_by_date()
  490. * @access private
  491. */
  492. public $order_by_date = true;
  493. /**
  494. * @var mixed Force input encoding to be set to the follow value
  495. * (false, or anything type-cast to false, disables this feature)
  496. * @see SimplePie::set_input_encoding()
  497. * @access private
  498. */
  499. public $input_encoding = false;
  500. /**
  501. * @var int Feed Autodiscovery Level
  502. * @see SimplePie::set_autodiscovery_level()
  503. * @access private
  504. */
  505. public $autodiscovery = SIMPLEPIE_LOCATOR_ALL;
  506. /**
  507. * Class registry object
  508. *
  509. * @var SimplePie_Registry
  510. */
  511. public $registry;
  512. /**
  513. * @var int Maximum number of feeds to check with autodiscovery
  514. * @see SimplePie::set_max_checked_feeds()
  515. * @access private
  516. */
  517. public $max_checked_feeds = 10;
  518. /**
  519. * @var array All the feeds found during the autodiscovery process
  520. * @see SimplePie::get_all_discovered_feeds()
  521. * @access private
  522. */
  523. public $all_discovered_feeds = array();
  524. /**
  525. * @var string Web-accessible path to the handler_image.php file.
  526. * @see SimplePie::set_image_handler()
  527. * @access private
  528. */
  529. public $image_handler = '';
  530. /**
  531. * @var array Stores the URLs when multiple feeds are being initialized.
  532. * @see SimplePie::set_feed_url()
  533. * @access private
  534. */
  535. public $multifeed_url = array();
  536. /**
  537. * @var array Stores SimplePie objects when multiple feeds initialized.
  538. * @access private
  539. */
  540. public $multifeed_objects = array();
  541. /**
  542. * @var array Stores the get_object_vars() array for use with multifeeds.
  543. * @see SimplePie::set_feed_url()
  544. * @access private
  545. */
  546. public $config_settings = null;
  547. /**
  548. * @var integer Stores the number of items to return per-feed with multifeeds.
  549. * @see SimplePie::set_item_limit()
  550. * @access private
  551. */
  552. public $item_limit = 0;
  553. /**
  554. * @var bool Stores if last-modified and/or etag headers were sent with the
  555. * request when checking a feed.
  556. */
  557. public $check_modified = false;
  558. /**
  559. * @var array Stores the default attributes to be stripped by strip_attributes().
  560. * @see SimplePie::strip_attributes()
  561. * @access private
  562. */
  563. public $strip_attributes = array('bgsound', 'class', 'expr', 'id', 'style', 'onclick', 'onerror', 'onfinish', 'onmouseover', 'onmouseout', 'onfocus', 'onblur', 'lowsrc', 'dynsrc');
  564. /**
  565. * @var array Stores the default attributes to add to different tags by add_attributes().
  566. * @see SimplePie::add_attributes()
  567. * @access private
  568. */
  569. public $add_attributes = array('audio' => array('preload' => 'none'), 'iframe' => array('sandbox' => 'allow-scripts allow-same-origin'), 'video' => array('preload' => 'none'));
  570. /**
  571. * @var array Stores the default tags to be stripped by strip_htmltags().
  572. * @see SimplePie::strip_htmltags()
  573. * @access private
  574. */
  575. public $strip_htmltags = array('base', 'blink', 'body', 'doctype', 'embed', 'font', 'form', 'frame', 'frameset', 'html', 'iframe', 'input', 'marquee', 'meta', 'noscript', 'object', 'param', 'script', 'style');
  576. /**
  577. * @var bool Should we throw exceptions, or use the old-style error property?
  578. * @access private
  579. */
  580. public $enable_exceptions = false;
  581. /**
  582. * The SimplePie class contains feed level data and options
  583. *
  584. * To use SimplePie, create the SimplePie object with no parameters. You can
  585. * then set configuration options using the provided methods. After setting
  586. * them, you must initialise the feed using $feed->init(). At that point the
  587. * object's methods and properties will be available to you.
  588. *
  589. * Previously, it was possible to pass in the feed URL along with cache
  590. * options directly into the constructor. This has been removed as of 1.3 as
  591. * it caused a lot of confusion.
  592. *
  593. * @since 1.0 Preview Release
  594. */
  595. public function __construct()
  596. {
  597. if (version_compare(PHP_VERSION, '5.6', '<'))
  598. {
  599. trigger_error('Please upgrade to PHP 5.6 or newer.');
  600. die();
  601. }
  602. // Other objects, instances created here so we can set options on them
  603. $this->sanitize = new SimplePie_Sanitize();
  604. $this->registry = new SimplePie_Registry();
  605. if (func_num_args() > 0)
  606. {
  607. $level = defined('E_USER_DEPRECATED') ? E_USER_DEPRECATED : E_USER_WARNING;
  608. trigger_error('Passing parameters to the constructor is no longer supported. Please use set_feed_url(), set_cache_location(), and set_cache_duration() directly.', $level);
  609. $args = func_get_args();
  610. switch (count($args)) {
  611. case 3:
  612. $this->set_cache_duration($args[2]);
  613. case 2:
  614. $this->set_cache_location($args[1]);
  615. case 1:
  616. $this->set_feed_url($args[0]);
  617. $this->init();
  618. }
  619. }
  620. }
  621. /**
  622. * Used for converting object to a string
  623. */
  624. public function __toString()
  625. {
  626. return md5(serialize($this->data));
  627. }
  628. /**
  629. * Remove items that link back to this before destroying this object
  630. */
  631. public function __destruct()
  632. {
  633. if (!gc_enabled())
  634. {
  635. if (!empty($this->data['items']))
  636. {
  637. foreach ($this->data['items'] as $item)
  638. {
  639. $item->__destruct();
  640. }
  641. unset($item, $this->data['items']);
  642. }
  643. if (!empty($this->data['ordered_items']))
  644. {
  645. foreach ($this->data['ordered_items'] as $item)
  646. {
  647. $item->__destruct();
  648. }
  649. unset($item, $this->data['ordered_items']);
  650. }
  651. }
  652. }
  653. /**
  654. * Force the given data/URL to be treated as a feed
  655. *
  656. * This tells SimplePie to ignore the content-type provided by the server.
  657. * Be careful when using this option, as it will also disable autodiscovery.
  658. *
  659. * @since 1.1
  660. * @param bool $enable Force the given data/URL to be treated as a feed
  661. */
  662. public function force_feed($enable = false)
  663. {
  664. $this->force_feed = (bool) $enable;
  665. }
  666. /**
  667. * Set the URL of the feed you want to parse
  668. *
  669. * This allows you to enter the URL of the feed you want to parse, or the
  670. * website you want to try to use auto-discovery on. This takes priority
  671. * over any set raw data.
  672. *
  673. * You can set multiple feeds to mash together by passing an array instead
  674. * of a string for the $url. Remember that with each additional feed comes
  675. * additional processing and resources.
  676. *
  677. * @since 1.0 Preview Release
  678. * @see set_raw_data()
  679. * @param string|array $url This is the URL (or array of URLs) that you want to parse.
  680. */
  681. public function set_feed_url($url)
  682. {
  683. $this->multifeed_url = array();
  684. if (is_array($url))
  685. {
  686. foreach ($url as $value)
  687. {
  688. $this->multifeed_url[] = $this->registry->call('Misc', 'fix_protocol', array($value, 1));
  689. }
  690. }
  691. else
  692. {
  693. $this->feed_url = $this->registry->call('Misc', 'fix_protocol', array($url, 1));
  694. $this->permanent_url = $this->feed_url;
  695. }
  696. }
  697. /**
  698. * Set an instance of {@see SimplePie_File} to use as a feed
  699. *
  700. * @param SimplePie_File &$file
  701. * @return bool True on success, false on failure
  702. */
  703. public function set_file(&$file)
  704. {
  705. if ($file instanceof SimplePie_File)
  706. {
  707. $this->feed_url = $file->url;
  708. $this->permanent_url = $this->feed_url;
  709. $this->file =& $file;
  710. return true;
  711. }
  712. return false;
  713. }
  714. /**
  715. * Set the raw XML data to parse
  716. *
  717. * Allows you to use a string of RSS/Atom data instead of a remote feed.
  718. *
  719. * If you have a feed available as a string in PHP, you can tell SimplePie
  720. * to parse that data string instead of a remote feed. Any set feed URL
  721. * takes precedence.
  722. *
  723. * @since 1.0 Beta 3
  724. * @param string $data RSS or Atom data as a string.
  725. * @see set_feed_url()
  726. */
  727. public function set_raw_data($data)
  728. {
  729. $this->raw_data = $data;
  730. }
  731. /**
  732. * Set the default timeout for fetching remote feeds
  733. *
  734. * This allows you to change the maximum time the feed's server to respond
  735. * and send the feed back.
  736. *
  737. * @since 1.0 Beta 3
  738. * @param int $timeout The maximum number of seconds to spend waiting to retrieve a feed.
  739. */
  740. public function set_timeout($timeout = 10)
  741. {
  742. $this->timeout = (int) $timeout;
  743. }
  744. /**
  745. * Set custom curl options
  746. *
  747. * This allows you to change default curl options
  748. *
  749. * @since 1.0 Beta 3
  750. * @param array $curl_options Curl options to add to default settings
  751. */
  752. public function set_curl_options(array $curl_options = array())
  753. {
  754. $this->curl_options = $curl_options;
  755. }
  756. /**
  757. * Force SimplePie to use fsockopen() instead of cURL
  758. *
  759. * @since 1.0 Beta 3
  760. * @param bool $enable Force fsockopen() to be used
  761. */
  762. public function force_fsockopen($enable = false)
  763. {
  764. $this->force_fsockopen = (bool) $enable;
  765. }
  766. /**
  767. * Enable/disable caching in SimplePie.
  768. *
  769. * This option allows you to disable caching all-together in SimplePie.
  770. * However, disabling the cache can lead to longer load times.
  771. *
  772. * @since 1.0 Preview Release
  773. * @param bool $enable Enable caching
  774. */
  775. public function enable_cache($enable = true)
  776. {
  777. $this->cache = (bool) $enable;
  778. }
  779. /**
  780. * SimplePie to continue to fall back to expired cache, if enabled, when
  781. * feed is unavailable.
  782. *
  783. * This tells SimplePie to ignore any file errors and fall back to cache
  784. * instead. This only works if caching is enabled and cached content
  785. * still exists.
  786. * @param bool $enable Force use of cache on fail.
  787. */
  788. public function force_cache_fallback($enable = false)
  789. {
  790. $this->force_cache_fallback= (bool) $enable;
  791. }
  792. /**
  793. * Set the length of time (in seconds) that the contents of a feed will be
  794. * cached
  795. *
  796. * @param int $seconds The feed content cache duration
  797. */
  798. public function set_cache_duration($seconds = 3600)
  799. {
  800. $this->cache_duration = (int) $seconds;
  801. }
  802. /**
  803. * Set the length of time (in seconds) that the autodiscovered feed URL will
  804. * be cached
  805. *
  806. * @param int $seconds The autodiscovered feed URL cache duration.
  807. */
  808. public function set_autodiscovery_cache_duration($seconds = 604800)
  809. {
  810. $this->autodiscovery_cache_duration = (int) $seconds;
  811. }
  812. /**
  813. * Set the file system location where the cached files should be stored
  814. *
  815. * @param string $location The file system location.
  816. */
  817. public function set_cache_location($location = './cache')
  818. {
  819. $this->cache_location = (string) $location;
  820. }
  821. /**
  822. * Return the filename (i.e. hash, without path and without extension) of the file to cache a given URL.
  823. * @param string $url The URL of the feed to be cached.
  824. * @return string A filename (i.e. hash, without path and without extension).
  825. */
  826. public function get_cache_filename($url)
  827. {
  828. // Append custom parameters to the URL to avoid cache pollution in case of multiple calls with different parameters.
  829. $url .= $this->force_feed ? '#force_feed' : '';
  830. $options = array();
  831. if ($this->timeout != 10)
  832. {
  833. $options[CURLOPT_TIMEOUT] = $this->timeout;
  834. }
  835. if ($this->useragent !== SIMPLEPIE_USERAGENT)
  836. {
  837. $options[CURLOPT_USERAGENT] = $this->useragent;
  838. }
  839. if (!empty($this->curl_options))
  840. {
  841. foreach ($this->curl_options as $k => $v)
  842. {
  843. $options[$k] = $v;
  844. }
  845. }
  846. if (!empty($options))
  847. {
  848. ksort($options);
  849. $url .= '#' . urlencode(var_export($options, true));
  850. }
  851. return call_user_func($this->cache_name_function, $url);
  852. }
  853. /**
  854. * Set whether feed items should be sorted into reverse chronological order
  855. *
  856. * @param bool $enable Sort as reverse chronological order.
  857. */
  858. public function enable_order_by_date($enable = true)
  859. {
  860. $this->order_by_date = (bool) $enable;
  861. }
  862. /**
  863. * Set the character encoding used to parse the feed
  864. *
  865. * This overrides the encoding reported by the feed, however it will fall
  866. * back to the normal encoding detection if the override fails
  867. *
  868. * @param string $encoding Character encoding
  869. */
  870. public function set_input_encoding($encoding = false)
  871. {
  872. if ($encoding)
  873. {
  874. $this->input_encoding = (string) $encoding;
  875. }
  876. else
  877. {
  878. $this->input_encoding = false;
  879. }
  880. }
  881. /**
  882. * Set how much feed autodiscovery to do
  883. *
  884. * @see SIMPLEPIE_LOCATOR_NONE
  885. * @see SIMPLEPIE_LOCATOR_AUTODISCOVERY
  886. * @see SIMPLEPIE_LOCATOR_LOCAL_EXTENSION
  887. * @see SIMPLEPIE_LOCATOR_LOCAL_BODY
  888. * @see SIMPLEPIE_LOCATOR_REMOTE_EXTENSION
  889. * @see SIMPLEPIE_LOCATOR_REMOTE_BODY
  890. * @see SIMPLEPIE_LOCATOR_ALL
  891. * @param int $level Feed Autodiscovery Level (level can be a combination of the above constants, see bitwise OR operator)
  892. */
  893. public function set_autodiscovery_level($level = SIMPLEPIE_LOCATOR_ALL)
  894. {
  895. $this->autodiscovery = (int) $level;
  896. }
  897. /**
  898. * Get the class registry
  899. *
  900. * Use this to override SimplePie's default classes
  901. * @see SimplePie_Registry
  902. * @return SimplePie_Registry
  903. */
  904. public function &get_registry()
  905. {
  906. return $this->registry;
  907. }
  908. /**#@+
  909. * Useful when you are overloading or extending SimplePie's default classes.
  910. *
  911. * @deprecated Use {@see get_registry()} instead
  912. * @link http://php.net/manual/en/language.oop5.basic.php#language.oop5.basic.extends PHP5 extends documentation
  913. * @param string $class Name of custom class
  914. * @return boolean True on success, false otherwise
  915. */
  916. /**
  917. * Set which class SimplePie uses for caching
  918. */
  919. public function set_cache_class($class = 'SimplePie_Cache')
  920. {
  921. return $this->registry->register('Cache', $class, true);
  922. }
  923. /**
  924. * Set which class SimplePie uses for auto-discovery
  925. */
  926. public function set_locator_class($class = 'SimplePie_Locator')
  927. {
  928. return $this->registry->register('Locator', $class, true);
  929. }
  930. /**
  931. * Set which class SimplePie uses for XML parsing
  932. */
  933. public function set_parser_class($class = 'SimplePie_Parser')
  934. {
  935. return $this->registry->register('Parser', $class, true);
  936. }
  937. /**
  938. * Set which class SimplePie uses for remote file fetching
  939. */
  940. public function set_file_class($class = 'SimplePie_File')
  941. {
  942. return $this->registry->register('File', $class, true);
  943. }
  944. /**
  945. * Set which class SimplePie uses for data sanitization
  946. */
  947. public function set_sanitize_class($class = 'SimplePie_Sanitize')
  948. {
  949. return $this->registry->register('Sanitize', $class, true);
  950. }
  951. /**
  952. * Set which class SimplePie uses for handling feed items
  953. */
  954. public function set_item_class($class = 'SimplePie_Item')
  955. {
  956. return $this->registry->register('Item', $class, true);
  957. }
  958. /**
  959. * Set which class SimplePie uses for handling author data
  960. */
  961. public function set_author_class($class = 'SimplePie_Author')
  962. {
  963. return $this->registry->register('Author', $class, true);
  964. }
  965. /**
  966. * Set which class SimplePie uses for handling category data
  967. */
  968. public function set_category_class($class = 'SimplePie_Category')
  969. {
  970. return $this->registry->register('Category', $class, true);
  971. }
  972. /**
  973. * Set which class SimplePie uses for feed enclosures
  974. */
  975. public function set_enclosure_class($class = 'SimplePie_Enclosure')
  976. {
  977. return $this->registry->register('Enclosure', $class, true);
  978. }
  979. /**
  980. * Set which class SimplePie uses for `<media:text>` captions
  981. */
  982. public function set_caption_class($class = 'SimplePie_Caption')
  983. {
  984. return $this->registry->register('Caption', $class, true);
  985. }
  986. /**
  987. * Set which class SimplePie uses for `<media:copyright>`
  988. */
  989. public function set_copyright_class($class = 'SimplePie_Copyright')
  990. {
  991. return $this->registry->register('Copyright', $class, true);
  992. }
  993. /**
  994. * Set which class SimplePie uses for `<media:credit>`
  995. */
  996. public function set_credit_class($class = 'SimplePie_Credit')
  997. {
  998. return $this->registry->register('Credit', $class, true);
  999. }
  1000. /**
  1001. * Set which class SimplePie uses for `<media:rating>`
  1002. */
  1003. public function set_rating_class($class = 'SimplePie_Rating')
  1004. {
  1005. return $this->registry->register('Rating', $class, true);
  1006. }
  1007. /**
  1008. * Set which class SimplePie uses for `<media:restriction>`
  1009. */
  1010. public function set_restriction_class($class = 'SimplePie_Restriction')
  1011. {
  1012. return $this->registry->register('Restriction', $class, true);
  1013. }
  1014. /**
  1015. * Set which class SimplePie uses for content-type sniffing
  1016. */
  1017. public function set_content_type_sniffer_class($class = 'SimplePie_Content_Type_Sniffer')
  1018. {
  1019. return $this->registry->register('Content_Type_Sniffer', $class, true);
  1020. }
  1021. /**
  1022. * Set which class SimplePie uses item sources
  1023. */
  1024. public function set_source_class($class = 'SimplePie_Source')
  1025. {
  1026. return $this->registry->register('Source', $class, true);
  1027. }
  1028. /**#@-*/
  1029. /**
  1030. * Set the user agent string
  1031. *
  1032. * @param string $ua New user agent string.
  1033. */
  1034. public function set_useragent($ua = SIMPLEPIE_USERAGENT)
  1035. {
  1036. $this->useragent = (string) $ua;
  1037. }
  1038. /**
  1039. * Set callback function to create cache filename with
  1040. *
  1041. * @param mixed $function Callback function
  1042. */
  1043. public function set_cache_name_function($function = 'md5')
  1044. {
  1045. if (is_callable($function))
  1046. {
  1047. $this->cache_name_function = $function;
  1048. }
  1049. }
  1050. /**
  1051. * Set options to make SP as fast as possible
  1052. *
  1053. * Forgoes a substantial amount of data sanitization in favor of speed. This
  1054. * turns SimplePie into a dumb parser of feeds.
  1055. *
  1056. * @param bool $set Whether to set them or not
  1057. */
  1058. public function set_stupidly_fast($set = false)
  1059. {
  1060. if ($set)
  1061. {
  1062. $this->enable_order_by_date(false);
  1063. $this->remove_div(false);
  1064. $this->strip_comments(false);
  1065. $this->strip_htmltags(false);
  1066. $this->strip_attributes(false);
  1067. $this->add_attributes(false);
  1068. $this->set_image_handler(false);
  1069. $this->set_https_domains(array());
  1070. }
  1071. }
  1072. /**
  1073. * Set maximum number of feeds to check with autodiscovery
  1074. *
  1075. * @param int $max Maximum number of feeds to check
  1076. */
  1077. public function set_max_checked_feeds($max = 10)
  1078. {
  1079. $this->max_checked_feeds = (int) $max;
  1080. }
  1081. public function remove_div($enable = true)
  1082. {
  1083. $this->sanitize->remove_div($enable);
  1084. }
  1085. public function strip_htmltags($tags = '', $encode = null)
  1086. {
  1087. if ($tags === '')
  1088. {
  1089. $tags = $this->strip_htmltags;
  1090. }
  1091. $this->sanitize->strip_htmltags($tags);
  1092. if ($encode !== null)
  1093. {
  1094. $this->sanitize->encode_instead_of_strip($tags);
  1095. }
  1096. }
  1097. public function encode_instead_of_strip($enable = true)
  1098. {
  1099. $this->sanitize->encode_instead_of_strip($enable);
  1100. }
  1101. public function strip_attributes($attribs = '')
  1102. {
  1103. if ($attribs === '')
  1104. {
  1105. $attribs = $this->strip_attributes;
  1106. }
  1107. $this->sanitize->strip_attributes($attribs);
  1108. }
  1109. public function add_attributes($attribs = '')
  1110. {
  1111. if ($attribs === '')
  1112. {
  1113. $attribs = $this->add_attributes;
  1114. }
  1115. $this->sanitize->add_attributes($attribs);
  1116. }
  1117. /**
  1118. * Set the output encoding
  1119. *
  1120. * Allows you to override SimplePie's output to match that of your webpage.
  1121. * This is useful for times when your webpages are not being served as
  1122. * UTF-8. This setting will be obeyed by {@see handle_content_type()}, and
  1123. * is similar to {@see set_input_encoding()}.
  1124. *
  1125. * It should be noted, however, that not all character encodings can support
  1126. * all characters. If your page is being served as ISO-8859-1 and you try
  1127. * to display a Japanese feed, you'll likely see garbled characters.
  1128. * Because of this, it is highly recommended to ensure that your webpages
  1129. * are served as UTF-8.
  1130. *
  1131. * The number of supported character encodings depends on whether your web
  1132. * host supports {@link http://php.net/mbstring mbstring},
  1133. * {@link http://php.net/iconv iconv}, or both. See
  1134. * {@link http://simplepie.org/wiki/faq/Supported_Character_Encodings} for
  1135. * more information.
  1136. *
  1137. * @param string $encoding
  1138. */
  1139. public function set_output_encoding($encoding = 'UTF-8')
  1140. {
  1141. $this->sanitize->set_output_encoding($encoding);
  1142. }
  1143. public function strip_comments($strip = false)
  1144. {
  1145. $this->sanitize->strip_comments($strip);
  1146. }
  1147. /**
  1148. * Set element/attribute key/value pairs of HTML attributes
  1149. * containing URLs that need to be resolved relative to the feed
  1150. *
  1151. * Defaults to |a|@href, |area|@href, |blockquote|@cite, |del|@cite,
  1152. * |form|@action, |img|@longdesc, |img|@src, |input|@src, |ins|@cite,
  1153. * |q|@cite
  1154. *
  1155. * @since 1.0
  1156. * @param array|null $element_attribute Element/attribute key/value pairs, null for default
  1157. */
  1158. public function set_url_replacements($element_attribute = null)
  1159. {
  1160. $this->sanitize->set_url_replacements($element_attribute);
  1161. }
  1162. /**
  1163. * Set the list of domains for which to force HTTPS.
  1164. * @see SimplePie_Sanitize::set_https_domains()
  1165. * @param array List of HTTPS domains. Example array('biz', 'example.com', 'example.org', 'www.example.net').
  1166. */
  1167. public function set_https_domains($domains = array())
  1168. {
  1169. if (is_array($domains))
  1170. {
  1171. $this->sanitize->set_https_domains($domains);
  1172. }
  1173. }
  1174. /**
  1175. * Set the handler to enable the display of cached images.
  1176. *
  1177. * @param string $page Web-accessible path to the handler_image.php file.
  1178. * @param string $qs The query string that the value should be passed to.
  1179. */
  1180. public function set_image_handler($page = false, $qs = 'i')
  1181. {
  1182. if ($page !== false)
  1183. {
  1184. $this->sanitize->set_image_handler($page . '?' . $qs . '=');
  1185. }
  1186. else
  1187. {
  1188. $this->image_handler = '';
  1189. }
  1190. }
  1191. /**
  1192. * Set the limit for items returned per-feed with multifeeds
  1193. *
  1194. * @param integer $limit The maximum number of items to return.
  1195. */
  1196. public function set_item_limit($limit = 0)
  1197. {
  1198. $this->item_limit = (int) $limit;
  1199. }
  1200. /**
  1201. * Enable throwing exceptions
  1202. *
  1203. * @param boolean $enable Should we throw exceptions, or use the old-style error property?
  1204. */
  1205. public function enable_exceptions($enable = true)
  1206. {
  1207. $this->enable_exceptions = $enable;
  1208. }
  1209. /**
  1210. * Initialize the feed object
  1211. *
  1212. * This is what makes everything happen. Period. This is where all of the
  1213. * configuration options get processed, feeds are fetched, cached, and
  1214. * parsed, and all of that other good stuff.
  1215. *
  1216. * @return boolean True if successful, false otherwise
  1217. */
  1218. public function init()
  1219. {
  1220. // Check absolute bare minimum requirements.
  1221. if (!extension_loaded('xml') || !extension_loaded('pcre'))
  1222. {
  1223. $this->error = 'XML or PCRE extensions not loaded!';
  1224. return false;
  1225. }
  1226. // Then check the xml extension is sane (i.e., libxml 2.7.x issue on PHP < 5.2.9 and libxml 2.7.0 to 2.7.2 on any version) if we don't have xmlreader.
  1227. elseif (!extension_loaded('xmlreader'))
  1228. {
  1229. static $xml_is_sane = null;
  1230. if ($xml_is_sane === null)
  1231. {
  1232. $parser_check = xml_parser_create();
  1233. xml_parse_into_struct($parser_check, '<foo>&amp;</foo>', $values);
  1234. xml_parser_free($parser_check);
  1235. $xml_is_sane = isset($values[0]['value']);
  1236. }
  1237. if (!$xml_is_sane)
  1238. {
  1239. return false;
  1240. }
  1241. }
  1242. // The default sanitize class gets set in the constructor, check if it has
  1243. // changed.
  1244. if ($this->registry->get_class('Sanitize') !== 'SimplePie_Sanitize') {
  1245. $this->sanitize = $this->registry->create('Sanitize');
  1246. }
  1247. if (method_exists($this->sanitize, 'set_registry'))
  1248. {
  1249. $this->sanitize->set_registry($this->registry);
  1250. }
  1251. // Pass whatever was set with config options over to the sanitizer.
  1252. // Pass the classes in for legacy support; new classes should use the registry instead
  1253. $this->sanitize->pass_cache_data($this->cache, $this->cache_location, $this->cache_name_function, $this->registry->get_class('Cache'));
  1254. $this->sanitize->pass_file_data($this->registry->get_class('File'), $this->timeout, $this->useragent, $this->force_fsockopen, $this->curl_options);
  1255. if (!empty($this->multifeed_url))
  1256. {
  1257. $i = 0;
  1258. $success = 0;
  1259. $this->multifeed_objects = array();
  1260. $this->error = array();
  1261. foreach ($this->multifeed_url as $url)
  1262. {
  1263. $this->multifeed_objects[$i] = clone $this;
  1264. $this->multifeed_objects[$i]->set_feed_url($url);
  1265. $single_success = $this->multifeed_objects[$i]->init();
  1266. $success |= $single_success;
  1267. if (!$single_success)
  1268. {
  1269. $this->error[$i] = $this->multifeed_objects[$i]->error();
  1270. }
  1271. $i++;
  1272. }
  1273. return (bool) $success;
  1274. }
  1275. elseif ($this->feed_url === null && $this->raw_data === null)
  1276. {
  1277. return false;
  1278. }
  1279. $this->error = null;
  1280. $this->data = array();
  1281. $this->check_modified = false;
  1282. $this->multifeed_objects = array();
  1283. $cache = false;
  1284. if ($this->feed_url !== null)
  1285. {
  1286. $parsed_feed_url = $this->registry->call('Misc', 'parse_url', array($this->feed_url));
  1287. // Decide whether to enable caching
  1288. if ($this->cache && $parsed_feed_url['scheme'] !== '')
  1289. {
  1290. $filename = $this->get_cache_filename($this->feed_url);
  1291. $cache = $this->registry->call('Cache', 'get_handler', array($this->cache_location, $filename, 'spc'));
  1292. }
  1293. // Fetch the data via SimplePie_File into $this->raw_data
  1294. if (($fetched = $this->fetch_data($cache)) === true)
  1295. {
  1296. return true;
  1297. }
  1298. elseif ($fetched === false) {
  1299. return false;
  1300. }
  1301. list($headers, $sniffed) = $fetched;
  1302. }
  1303. // Empty response check
  1304. if(empty($this->raw_data)){
  1305. $this->error = "A feed could not be found at `$this->feed_url`. Empty body.";
  1306. $this->registry->call('Misc', 'error', array($this->error, E_USER_NOTICE, __FILE__, __LINE__));
  1307. return false;
  1308. }
  1309. // Set up array of possible encodings
  1310. $encodings = array();
  1311. // First check to see if input has been overridden.
  1312. if ($this->input_encoding !== false)
  1313. {
  1314. $encodings[] = strtoupper($this->input_encoding);
  1315. }
  1316. $application_types = array('application/xml', 'application/xml-dtd', 'application/xml-external-parsed-entity');
  1317. $text_types = array('text/xml', 'text/xml-external-parsed-entity');
  1318. // RFC 3023 (only applies to sniffed content)
  1319. if (isset($sniffed))
  1320. {
  1321. if (in_array($sniffed, $application_types) || substr($sniffed, 0, 12) === 'application/' && substr($sniffed, -4) === '+xml')
  1322. {
  1323. if (isset($headers['content-type']) && preg_match('/;\x20?charset=([^;]*)/i', $headers['content-type'], $charset))
  1324. {
  1325. $encodings[] = strtoupper($charset[1]);
  1326. }
  1327. $encodings = array_merge($encodings, $this->registry->call('Misc', 'xml_encoding', array($this->raw_data, &$this->registry)));
  1328. $encodings[] = 'UTF-8';
  1329. }
  1330. elseif (in_array($sniffed, $text_types) || substr($sniffed, 0, 5) === 'text/' && substr($sniffed, -4) === '+xml')
  1331. {
  1332. if (isset($headers['content-type']) && preg_match('/;\x20?charset=([^;]*)/i', $headers['content-type'], $charset))
  1333. {
  1334. $encodings[] = strtoupper($charset[1]);
  1335. }
  1336. $encodings[] = 'US-ASCII';
  1337. }
  1338. // Text MIME-type default
  1339. elseif (substr($sniffed, 0, 5) === 'text/')
  1340. {
  1341. $encodings[] = 'UTF-8';
  1342. }
  1343. }
  1344. // Fallback to XML 1.0 Appendix F.1/UTF-8/ISO-8859-1
  1345. $encodings = array_merge($encodings, $this->registry->call('Misc', 'xml_encoding', array($this->raw_data, &$this->registry)));
  1346. $encodings[] = 'UTF-8';
  1347. $encodings[] = 'ISO-8859-1';
  1348. // There's no point in trying an encoding twice
  1349. $encodings = array_unique($encodings);
  1350. // Loop through each possible encoding, till we return something, or run out of possibilities
  1351. foreach ($encodings as $encoding)
  1352. {
  1353. // Change the encoding to UTF-8 (as we always use UTF-8 internally)
  1354. if ($utf8_data = $this->registry->call('Misc', 'change_encoding', array($this->raw_data, $encoding, 'UTF-8')))
  1355. {
  1356. // Create new parser
  1357. $parser = $this->registry->create('Parser');
  1358. // If it's parsed fine
  1359. if ($parser->parse($utf8_data, 'UTF-8', $this->permanent_url))
  1360. {
  1361. $this->data = $parser->get_data();
  1362. if (!($this->get_type() & ~SIMPLEPIE_TYPE_NONE))
  1363. {
  1364. $this->error = "A feed could not be found at `$this->feed_url`. This does not appear to be a valid RSS or Atom feed.";
  1365. $this->registry->call('Misc', 'error', array($this->error, E_USER_NOTICE, __FILE__, __LINE__));
  1366. return false;
  1367. }
  1368. if (isset($headers))
  1369. {
  1370. $this->data['headers'] = $headers;
  1371. }
  1372. $this->data['build'] = SIMPLEPIE_BUILD;
  1373. // Cache the file if caching is enabled
  1374. if ($cache && !$cache->save($this))
  1375. {
  1376. trigger_error("$this->cache_location is not writable. Make sure you've set the correct relative or absolute path, and that the location is server-writable.", E_USER_WARNING);
  1377. }
  1378. return true;
  1379. }
  1380. }
  1381. }
  1382. if (isset($parser))
  1383. {
  1384. // We have an error, just set SimplePie_Misc::error to it and quit
  1385. $this->error = $this->feed_url;
  1386. $this->error .= sprintf(' is invalid XML, likely due to invalid characters. XML error: %s at line %d, column %d', $parser->get_error_string(), $parser->get_current_line(), $parser->get_current_column());
  1387. }
  1388. else
  1389. {
  1390. $this->error = 'The data could not be converted to UTF-8.';
  1391. if (!extension_loaded('mbstring') && !extension_loaded('iconv') && !class_exists('\UConverter')) {
  1392. $this->error .= ' You MUST have either the iconv, mbstring or intl (PHP 5.5+) extension installed and enabled.';
  1393. } else {
  1394. $missingExtensions = array();
  1395. if (!extension_loaded('iconv')) {
  1396. $missingExtensions[] = 'iconv';
  1397. }
  1398. if (!extension_loaded('mbstring')) {
  1399. $missingExtensions[] = 'mbstring';
  1400. }
  1401. if (!class_exists('\UConverter')) {
  1402. $missingExtensions[] = 'intl (PHP 5.5+)';
  1403. }
  1404. $this->error .= ' Try installing/enabling the ' . implode(' or ', $missingExtensions) . ' extension.';
  1405. }
  1406. }
  1407. $this->registry->call('Misc', 'error', array($this->error, E_USER_NOTICE, __FILE__, __LINE__));
  1408. return false;
  1409. }
  1410. /**
  1411. * Fetch the data via SimplePie_File
  1412. *
  1413. * If the data is already cached, attempt to fetch it from there instead
  1414. * @param SimplePie_Cache_Base|false $cache Cache handler, or false to not load from the cache
  1415. * @return array|true Returns true if the data was loaded from the cache, or an array of HTTP headers and sniffed type
  1416. */
  1417. protected function fetch_data(&$cache)
  1418. {
  1419. // If it's enabled, use the cache
  1420. if ($cache)
  1421. {
  1422. // Load the Cache
  1423. $this->data = $cache->load();
  1424. if (!empty($this->data))
  1425. {
  1426. // If the cache is for an outdated build of SimplePie
  1427. if (!isset($this->data['build']) || $this->data['build'] !== SIMPLEPIE_BUILD)
  1428. {
  1429. $cache->unlink();
  1430. $this->data = array();
  1431. }
  1432. // If we've hit a collision just rerun it with caching disabled
  1433. elseif (isset($this->data['url']) && $this->data['url'] !== $this->feed_url)
  1434. {
  1435. $cache = false;
  1436. $this->data = array();
  1437. }
  1438. // If we've got a non feed_url stored (if the page isn't actually a feed, or is a redirect) use that URL.
  1439. elseif (isset($this->data['feed_url']))
  1440. {
  1441. // If the autodiscovery cache is still valid use it.
  1442. if ($cache->mtime() + $this->autodiscovery_cache_duration > time())
  1443. {
  1444. // Do not need to do feed autodiscovery yet.
  1445. if ($this->data['feed_url'] !== $this->data['url'])
  1446. {
  1447. $this->set_feed_url($this->data['feed_url']);
  1448. return $this->init();
  1449. }
  1450. $cache->unlink();
  1451. $this->data = array();
  1452. }
  1453. }
  1454. // Check if the cache has been updated
  1455. elseif ($cache->mtime() + $this->cache_duration < time())
  1456. {
  1457. // Want to know if we tried to send last-modified and/or etag headers
  1458. // when requesting this file. (Note that it's up to the file to
  1459. // support this, but we don't always send the headers either.)
  1460. $this->check_modified = true;
  1461. if (isset($this->data['headers']['last-modified']) || isset($this->data['headers']['etag']))
  1462. {
  1463. $headers = array(
  1464. 'Accept' => 'application/atom+xml, application/rss+xml, application/rdf+xml;q=0.9, application/xml;q=0.8, text/xml;q=0.8, text/html;q=0.7, unknown/unknown;q=0.1, application/unknown;q=0.1, */*;q=0.1',
  1465. );
  1466. if (isset($this->data['headers']['last-modified']))
  1467. {
  1468. $headers['if-modified-since'] = $this->data['headers']['last-modified'];
  1469. }
  1470. if (isset($this->data['headers']['etag']))
  1471. {
  1472. $headers['if-none-match'] = $this->data['headers']['etag'];
  1473. }
  1474. $file = $this->registry->create('File', array($this->feed_url, $this->timeout/10, 5, $headers, $this->useragent, $this->force_fsockopen, $this->curl_options));
  1475. $this->status_code = $file->status_code;
  1476. if ($file->success)
  1477. {
  1478. if ($file->status_code === 304)
  1479. {
  1480. // Set raw_data to false here too, to signify that the cache
  1481. // is still valid.
  1482. $this->raw_data = false;
  1483. $cache->touch();
  1484. return true;
  1485. }
  1486. }
  1487. else
  1488. {
  1489. $this->check_modified = false;
  1490. if($this->force_cache_fallback)
  1491. {
  1492. $cache->touch();
  1493. return true;
  1494. }
  1495. unset($file);
  1496. }
  1497. }
  1498. }
  1499. // If the cache is still valid, just return true
  1500. else
  1501. {
  1502. $this->raw_data = false;
  1503. return true;
  1504. }
  1505. }
  1506. // If the cache is empty, delete it
  1507. else
  1508. {
  1509. $cache->unlink();
  1510. $this->data = array();
  1511. }
  1512. }
  1513. // If we don't already have the file (it'll only exist if we've opened it to check if the cache has been modified), open it.
  1514. if (!isset($file))
  1515. {
  1516. if ($this->file instanceof SimplePie_File && $this->file->url === $this->feed_url)
  1517. {
  1518. $file =& $this->file;
  1519. }
  1520. else
  1521. {
  1522. $headers = array(
  1523. 'Accept' => 'application/atom+xml, application/rss+xml, application/rdf+xml;q=0.9, application/xml;q=0.8, text/xml;q=0.8, text/html;q=0.7, unknown/unknown;q=0.1, application/unknown;q=0.1, */*;q=0.1',
  1524. );
  1525. $file = $this->registry->create('File', array($this->feed_url, $this->timeout, 5, $headers, $this->useragent, $this->force_fsockopen, $this->curl_options));
  1526. }
  1527. }
  1528. $this->status_code = $file->status_code;
  1529. // If the file connection has an error, set SimplePie::error to that and quit
  1530. if (!$file->success && !($file->method & SIMPLEPIE_FILE_SOURCE_REMOTE === 0 || ($file->status_code === 200 || $file->status_code > 206 && $file->status_code < 300)))
  1531. {
  1532. $this->error = $file->error;
  1533. return !empty($this->data);
  1534. }
  1535. if (!$this->force_feed)
  1536. {
  1537. // Check if the supplied URL is a feed, if it isn't, look for it.
  1538. $locate = $this->registry->create('Locator', array(&$file, $this->timeout, $this->useragent, $this->max_checked_feeds, $this->force_fsockopen, $this->curl_options));
  1539. if (!$locate->is_feed($file))
  1540. {
  1541. $copyStatusCode = $file->status_code;
  1542. $copyContentType = $file->headers['content-type'];
  1543. try
  1544. {
  1545. $microformats = false;
  1546. if (class_exists('DOMXpath') && function_exists('Mf2\parse')) {
  1547. $doc = new DOMDocument();
  1548. @$doc->loadHTML($file->body);
  1549. $xpath = new DOMXpath($doc);
  1550. // Check for both h-feed and h-entry, as both a feed with no entries
  1551. // and a list of entries without an h-feed wrapper are both valid.
  1552. $query = '//*[contains(concat(" ", @class, " "), " h-feed ") or '.
  1553. 'contains(concat(" ", @class, " "), " h-entry ")]';
  1554. $result = $xpath->query($query);
  1555. $microformats = $result->length !== 0;
  1556. }
  1557. // Now also do feed discovery, but if microformats were found don't
  1558. // overwrite the current value of file.
  1559. $discovered = $locate->find($this->autodiscovery,
  1560. $this->all_discovered_feeds);
  1561. if ($microformats)
  1562. {
  1563. if ($hub = $locate->get_rel_link('hub'))
  1564. {
  1565. $self = $locate->get_rel_link('self');
  1566. $this->store_links($file, $hub, $self);
  1567. }
  1568. // Push the current file onto all_discovered feeds so the user can
  1569. // be shown this as one of the options.
  1570. if (isset($this->all_discovered_feeds)) {
  1571. $this->all_discovered_feeds[] = $file;
  1572. }
  1573. }
  1574. else
  1575. {
  1576. if ($discovered)
  1577. {
  1578. $file = $discovered;
  1579. }
  1580. else
  1581. {
  1582. // We need to unset this so that if SimplePie::set_file() has
  1583. // been called that object is untouched
  1584. unset($file);
  1585. $this->error = "A feed could not be found at `$this->feed_url`; the status code is `$copyStatusCode` and content-type is `$copyContentType`";
  1586. $this->registry->call('Misc', 'error', array($this->error, E_USER_NOTICE, __FILE__, __LINE__));
  1587. return false;
  1588. }
  1589. }
  1590. }
  1591. catch (SimplePie_Exception $e)
  1592. {
  1593. // We need to unset this so that if SimplePie::set_file() has been called that object is untouched
  1594. unset($file);
  1595. // This is usually because DOMDocument doesn't exist
  1596. $this->error = $e->getMessage();
  1597. $this->registry->call('Misc', 'error', array($this->error, E_USER_NOTICE, $e->getFile(), $e->getLine()));
  1598. return false;
  1599. }
  1600. if ($cache)
  1601. {
  1602. $this->data = array('url' => $this->feed_url, 'feed_url' => $file->url, 'build' => SIMPLEPIE_BUILD);
  1603. if (!$cache->save($this))
  1604. {
  1605. trigger_error("$this->cache_location is not writable. Make sure you've set the correct relative or absolute path, and that the location is server-writable.", E_USER_WARNING);
  1606. }
  1607. $cache = $this->registry->call('Cache', 'get_handler', array($this->cache_location, call_user_func($this->cache_name_function, $file->url), 'spc'));
  1608. }
  1609. }
  1610. $this->feed_url = $file->url;
  1611. $locate = null;
  1612. }
  1613. $this->raw_data = $file->body;
  1614. $this->permanent_url = $file->permanent_url;
  1615. $headers = $file->headers;
  1616. $sniffer = $this->registry->create('Content_Type_Sniffer', array(&$file));
  1617. $sniffed = $sniffer->get_type();
  1618. return array($headers, $sniffed);
  1619. }
  1620. /**
  1621. * Get the error message for the occurred error
  1622. *
  1623. * @return string|array Error message, or array of messages for multifeeds
  1624. */
  1625. public function error()
  1626. {
  1627. return $this->error;
  1628. }
  1629. /**
  1630. * Get the last HTTP status code
  1631. *
  1632. * @return int Status code
  1633. */
  1634. public function status_code()
  1635. {
  1636. return $this->status_code;
  1637. }
  1638. /**
  1639. * Get the raw XML
  1640. *
  1641. * This is the same as the old `$feed->enable_xml_dump(true)`, but returns
  1642. * the data instead of printing it.
  1643. *
  1644. * @return string|boolean Raw XML data, false if the cache is used
  1645. */
  1646. public function get_raw_data()
  1647. {
  1648. return $this->raw_data;
  1649. }
  1650. /**
  1651. * Get the character encoding used for output
  1652. *
  1653. * @since Preview Release
  1654. * @return string
  1655. */
  1656. public function get_encoding()
  1657. {
  1658. return $this->sanitize->output_encoding;
  1659. }
  1660. /**
  1661. * Send the content-type header with correct encoding
  1662. *
  1663. * This method ensures that the SimplePie-enabled page is being served with
  1664. * the correct {@link http://www.iana.org/assignments/media-types/ mime-type}
  1665. * and character encoding HTTP headers (character encoding determined by the
  1666. * {@see set_output_encoding} config option).
  1667. *
  1668. * This won't work properly if any content or whitespace has already been
  1669. * sent to the browser, because it relies on PHP's
  1670. * {@link http://php.net/header header()} function, and these are the
  1671. * circumstances under which the function works.
  1672. *
  1673. * Because it's setting these settings for the entire page (as is the nature
  1674. * of HTTP headers), this should only be used once per page (again, at the
  1675. * top).
  1676. *
  1677. * @param string $mime MIME type to serve the page as
  1678. */
  1679. public function handle_content_type($mime = 'text/html')
  1680. {
  1681. if (!headers_sent())
  1682. {
  1683. $header = "Content-type: $mime;";
  1684. if ($this->get_encoding())
  1685. {
  1686. $header .= ' charset=' . $this->get_encoding();
  1687. }
  1688. else
  1689. {
  1690. $header .= ' charset=UTF-8';
  1691. }
  1692. header($header);
  1693. }
  1694. }
  1695. /**
  1696. * Get the type of the feed
  1697. *
  1698. * This returns a SIMPLEPIE_TYPE_* constant, which can be tested against
  1699. * using {@link http://php.net/language.operators.bitwise bitwise operators}
  1700. *
  1701. * @since 0.8 (usage changed to using constants in 1.0)
  1702. * @see SIMPLEPIE_TYPE_NONE Unknown.
  1703. * @see SIMPLEPIE_TYPE_RSS_090 RSS 0.90.
  1704. * @see SIMPLEPIE_TYPE_RSS_091_NETSCAPE RSS 0.91 (Netscape).
  1705. * @see SIMPLEPIE_TYPE_RSS_091_USERLAND RSS 0.91 (Userland).
  1706. * @see SIMPLEPIE_TYPE_RSS_091 RSS 0.91.
  1707. * @see SIMPLEPIE_TYPE_RSS_092 RSS 0.92.
  1708. * @see SIMPLEPIE_TYPE_RSS_093 RSS 0.93.
  1709. * @see SIMPLEPIE_TYPE_RSS_094 RSS 0.94.
  1710. * @see SIMPLEPIE_TYPE_RSS_10 RSS 1.0.
  1711. * @see SIMPLEPIE_TYPE_RSS_20 RSS 2.0.x.
  1712. * @see SIMPLEPIE_TYPE_RSS_RDF RDF-based RSS.
  1713. * @see SIMPLEPIE_TYPE_RSS_SYNDICATION Non-RDF-based RSS (truly intended as syndication format).
  1714. * @see SIMPLEPIE_TYPE_RSS_ALL Any version of RSS.
  1715. * @see SIMPLEPIE_TYPE_ATOM_03 Atom 0.3.
  1716. * @see SIMPLEPIE_TYPE_ATOM_10 Atom 1.0.
  1717. * @see SIMPLEPIE_TYPE_ATOM_ALL Any version of Atom.
  1718. * @see SIMPLEPIE_TYPE_ALL Any known/supported feed type.
  1719. * @return int SIMPLEPIE_TYPE_* constant
  1720. */
  1721. public function get_type()
  1722. {
  1723. if (!isset($this->data['type']))
  1724. {
  1725. $this->data['type'] = SIMPLEPIE_TYPE_ALL;
  1726. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['feed']))
  1727. {
  1728. $this->data['type'] &= SIMPLEPIE_TYPE_ATOM_10;
  1729. }
  1730. elseif (isset($this->data['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['feed']))
  1731. {
  1732. $this->data['type'] &= SIMPLEPIE_TYPE_ATOM_03;
  1733. }
  1734. elseif (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF']))
  1735. {
  1736. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_10]['channel'])
  1737. || isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_10]['image'])
  1738. || isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_10]['item'])
  1739. || isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_10]['textinput']))
  1740. {
  1741. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_10;
  1742. }
  1743. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_090]['channel'])
  1744. || isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_090]['image'])
  1745. || isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_090]['item'])
  1746. || isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_090]['textinput']))
  1747. {
  1748. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_090;
  1749. }
  1750. }
  1751. elseif (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss']))
  1752. {
  1753. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_ALL;
  1754. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0]['attribs']['']['version']))
  1755. {
  1756. switch (trim($this->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0]['attribs']['']['version']))
  1757. {
  1758. case '0.91':
  1759. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_091;
  1760. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_20]['skiphours']['hour'][0]['data']))
  1761. {
  1762. switch (trim($this->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_20]['skiphours']['hour'][0]['data']))
  1763. {
  1764. case '0':
  1765. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_091_NETSCAPE;
  1766. break;
  1767. case '24':
  1768. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_091_USERLAND;
  1769. break;
  1770. }
  1771. }
  1772. break;
  1773. case '0.92':
  1774. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_092;
  1775. break;
  1776. case '0.93':
  1777. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_093;
  1778. break;
  1779. case '0.94':
  1780. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_094;
  1781. break;
  1782. case '2.0':
  1783. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_20;
  1784. break;
  1785. }
  1786. }
  1787. }
  1788. else
  1789. {
  1790. $this->data['type'] = SIMPLEPIE_TYPE_NONE;
  1791. }
  1792. }
  1793. return $this->data['type'];
  1794. }
  1795. /**
  1796. * Get the URL for the feed
  1797. *
  1798. * When the 'permanent' mode is enabled, returns the original feed URL,
  1799. * except in the case of an `HTTP 301 Moved Permanently` status response,
  1800. * in which case the location of the first redirection is returned.
  1801. *
  1802. * When the 'permanent' mode is disabled (default),
  1803. * may or may not be different from the URL passed to {@see set_feed_url()},
  1804. * depending on whether auto-discovery was used, and whether there were
  1805. * any redirects along the way.
  1806. *
  1807. * @since Preview Release (previously called `get_feed_url()` since SimplePie 0.8.)
  1808. * @todo Support <itunes:new-feed-url>
  1809. * @todo Also, |atom:link|@rel=self
  1810. * @param bool $permanent Permanent mode to return only the original URL or the first redirection
  1811. * iff it is a 301 redirection
  1812. * @return string|null
  1813. */
  1814. public function subscribe_url($permanent = false)
  1815. {
  1816. if ($permanent)
  1817. {
  1818. if ($this->permanent_url !== null)
  1819. {
  1820. // sanitize encodes ampersands which are required when used in a url.
  1821. return str_replace('&amp;', '&',
  1822. $this->sanitize($this->permanent_url,
  1823. SIMPLEPIE_CONSTRUCT_IRI));
  1824. }
  1825. }
  1826. else
  1827. {
  1828. if ($this->feed_url !== null)
  1829. {
  1830. return str_replace('&amp;', '&',
  1831. $this->sanitize($this->feed_url,
  1832. SIMPLEPIE_CONSTRUCT_IRI));
  1833. }
  1834. }
  1835. return null;
  1836. }
  1837. /**
  1838. * Get data for an feed-level element
  1839. *
  1840. * This method allows you to get access to ANY element/attribute that is a
  1841. * sub-element of the opening feed tag.
  1842. *
  1843. * The return value is an indexed array of elements matching the given
  1844. * namespace and tag name. Each element has `attribs`, `data` and `child`
  1845. * subkeys. For `attribs` and `child`, these contain namespace subkeys.
  1846. * `attribs` then has one level of associative name => value data (where
  1847. * `value` is a string) after the namespace. `child` has tag-indexed keys
  1848. * after the namespace, each member of which is an indexed array matching
  1849. * this same format.
  1850. *
  1851. * For example:
  1852. * <pre>
  1853. * // This is probably a bad example because we already support
  1854. * // <media:content> natively, but it shows you how to parse through
  1855. * // the nodes.
  1856. * $group = $item->get_item_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'group');
  1857. * $content = $group[0]['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['content'];
  1858. * $file = $content[0]['attribs']['']['url'];
  1859. * echo $file;
  1860. * </pre>
  1861. *
  1862. * @since 1.0
  1863. * @see http://simplepie.org/wiki/faq/supported_xml_namespaces
  1864. * @param string $namespace The URL of the XML namespace of the elements you're trying to access
  1865. * @param string $tag Tag name
  1866. * @return array
  1867. */
  1868. public function get_feed_tags($namespace, $tag)
  1869. {
  1870. $type = $this->get_type();
  1871. if ($type & SIMPLEPIE_TYPE_ATOM_10)
  1872. {
  1873. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['feed'][0]['child'][$namespace][$tag]))
  1874. {
  1875. return $this->data['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['feed'][0]['child'][$namespace][$tag];
  1876. }
  1877. }
  1878. if ($type & SIMPLEPIE_TYPE_ATOM_03)
  1879. {
  1880. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['feed'][0]['child'][$namespace][$tag]))
  1881. {
  1882. return $this->data['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['feed'][0]['child'][$namespace][$tag];
  1883. }
  1884. }
  1885. if ($type & SIMPLEPIE_TYPE_RSS_RDF)
  1886. {
  1887. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][$namespace][$tag]))
  1888. {
  1889. return $this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][$namespace][$tag];
  1890. }
  1891. }
  1892. if ($type & SIMPLEPIE_TYPE_RSS_SYNDICATION)
  1893. {
  1894. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0]['child'][$namespace][$tag]))
  1895. {
  1896. return $this->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0]['child'][$namespace][$tag];
  1897. }
  1898. }
  1899. return null;
  1900. }
  1901. /**
  1902. * Get data for an channel-level element
  1903. *
  1904. * This method allows you to get access to ANY element/attribute in the
  1905. * channel/header section of the feed.
  1906. *
  1907. * See {@see SimplePie::get_feed_tags()} for a description of the return value
  1908. *
  1909. * @since 1.0
  1910. * @see http://simplepie.org/wiki/faq/supported_xml_namespaces
  1911. * @param string $namespace The URL of the XML namespace of the elements you're trying to access
  1912. * @param string $tag Tag name
  1913. * @return array
  1914. */
  1915. public function get_channel_tags($namespace, $tag)
  1916. {
  1917. $type = $this->get_type();
  1918. if ($type & SIMPLEPIE_TYPE_ATOM_ALL)
  1919. {
  1920. if ($return = $this->get_feed_tags($namespace, $tag))
  1921. {
  1922. return $return;
  1923. }
  1924. }
  1925. if ($type & SIMPLEPIE_TYPE_RSS_10)
  1926. {
  1927. if ($channel = $this->get_feed_tags(SIMPLEPIE_NAMESPACE_RSS_10, 'channel'))
  1928. {
  1929. if (isset($channel[0]['child'][$namespace][$tag]))
  1930. {
  1931. return $channel[0]['child'][$namespace][$tag];
  1932. }
  1933. }
  1934. }
  1935. if ($type & SIMPLEPIE_TYPE_RSS_090)
  1936. {
  1937. if ($channel = $this->get_feed_tags(SIMPLEPIE_NAMESPACE_RSS_090, 'channel'))
  1938. {
  1939. if (isset($channel[0]['child'][$namespace][$tag]))
  1940. {
  1941. return $channel[0]['child'][$namespace][$tag];
  1942. }
  1943. }
  1944. }
  1945. if ($type & SIMPLEPIE_TYPE_RSS_SYNDICATION)
  1946. {
  1947. if ($channel = $this->get_feed_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'channel'))
  1948. {
  1949. if (isset($channel[0]['child'][$namespace][$tag]))
  1950. {
  1951. return $channel[0]['child'][$namespace][$tag];
  1952. }
  1953. }
  1954. }
  1955. return null;
  1956. }
  1957. /**
  1958. * Get data for an channel-level element
  1959. *
  1960. * This method allows you to get access to ANY element/attribute in the
  1961. * image/logo section of the feed.
  1962. *
  1963. * See {@see SimplePie::get_feed_tags()} for a description of the return value
  1964. *
  1965. * @since 1.0
  1966. * @see http://simplepie.org/wiki/faq/supported_xml_namespaces
  1967. * @param string $namespace The URL of the XML namespace of the elements you're trying to access
  1968. * @param string $tag Tag name
  1969. * @return array
  1970. */
  1971. public function get_image_tags($namespace, $tag)
  1972. {
  1973. $type = $this->get_type();
  1974. if ($type & SIMPLEPIE_TYPE_RSS_10)
  1975. {
  1976. if ($image = $this->get_feed_tags(SIMPLEPIE_NAMESPACE_RSS_10, 'image'))
  1977. {
  1978. if (isset($image[0]['child'][$namespace][$tag]))
  1979. {
  1980. return $image[0]['child'][$namespace][$tag];
  1981. }
  1982. }
  1983. }
  1984. if ($type & SIMPLEPIE_TYPE_RSS_090)
  1985. {
  1986. if ($image = $this->get_feed_tags(SIMPLEPIE_NAMESPACE_RSS_090, 'image'))
  1987. {
  1988. if (isset($image[0]['child'][$namespace][$tag]))
  1989. {
  1990. return $image[0]['child'][$namespace][$tag];
  1991. }
  1992. }
  1993. }
  1994. if ($type & SIMPLEPIE_TYPE_RSS_SYNDICATION)
  1995. {
  1996. if ($image = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'image'))
  1997. {
  1998. if (isset($image[0]['child'][$namespace][$tag]))
  1999. {
  2000. return $image[0]['child'][$namespace][$tag];
  2001. }
  2002. }
  2003. }
  2004. return null;
  2005. }
  2006. /**
  2007. * Get the base URL value from the feed
  2008. *
  2009. * Uses `<xml:base>` if available, otherwise uses the first link in the
  2010. * feed, or failing that, the URL of the feed itself.
  2011. *
  2012. * @see get_link
  2013. * @see subscribe_url
  2014. *
  2015. * @param array $element
  2016. * @return string
  2017. */
  2018. public function get_base($element = array())
  2019. {
  2020. if (!($this->get_type() & SIMPLEPIE_TYPE_RSS_SYNDICATION) && !empty($element['xml_base_explicit']) && isset($element['xml_base']))
  2021. {
  2022. return $element['xml_base'];
  2023. }
  2024. elseif ($this->get_link() !== null)
  2025. {
  2026. return $this->get_link();
  2027. }
  2028. return $this->subscribe_url();
  2029. }
  2030. /**
  2031. * Sanitize feed data
  2032. *
  2033. * @access private
  2034. * @see SimplePie_Sanitize::sanitize()
  2035. * @param string $data Data to sanitize
  2036. * @param int $type One of the SIMPLEPIE_CONSTRUCT_* constants
  2037. * @param string $base Base URL to resolve URLs against
  2038. * @return string Sanitized data
  2039. */
  2040. public function sanitize($data, $type, $base = '')
  2041. {
  2042. try
  2043. {
  2044. return $this->sanitize->sanitize($data, $type, $base);
  2045. }
  2046. catch (SimplePie_Exception $e)
  2047. {
  2048. if (!$this->enable_exceptions)
  2049. {
  2050. $this->error = $e->getMessage();
  2051. $this->registry->call('Misc', 'error', array($this->error, E_USER_WARNING, $e->getFile(), $e->getLine()));
  2052. return '';
  2053. }
  2054. throw $e;
  2055. }
  2056. }
  2057. /**
  2058. * Get the title of the feed
  2059. *
  2060. * Uses `<atom:title>`, `<title>` or `<dc:title>`
  2061. *
  2062. * @since 1.0 (previously called `get_feed_title` since 0.8)
  2063. * @return string|null
  2064. */
  2065. public function get_title()
  2066. {
  2067. if ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'title'))
  2068. {
  2069. return $this->sanitize($return[0]['data'], $this->registry->call('Misc', 'atom_10_construct_type', array($return[0]['attribs'])), $this->get_base($return[0]));
  2070. }
  2071. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'title'))
  2072. {
  2073. return $this->sanitize($return[0]['data'], $this->registry->call('Misc', 'atom_03_construct_type', array($return[0]['attribs'])), $this->get_base($return[0]));
  2074. }
  2075. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_RSS_10, 'title'))
  2076. {
  2077. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_MAYBE_HTML, $this->get_base($return[0]));
  2078. }
  2079. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_RSS_090, 'title'))
  2080. {
  2081. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_MAYBE_HTML, $this->get_base($return[0]));
  2082. }
  2083. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'title'))
  2084. {
  2085. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_MAYBE_HTML, $this->get_base($return[0]));
  2086. }
  2087. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_DC_11, 'title'))
  2088. {
  2089. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2090. }
  2091. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_DC_10, 'title'))
  2092. {
  2093. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2094. }
  2095. return null;
  2096. }
  2097. /**
  2098. * Get a category for the feed
  2099. *
  2100. * @since Unknown
  2101. * @param int $key The category that you want to return. Remember that arrays begin with 0, not 1
  2102. * @return SimplePie_Category|null
  2103. */
  2104. public function get_category($key = 0)
  2105. {
  2106. $categories = $this->get_categories();
  2107. if (isset($categories[$key]))
  2108. {
  2109. return $categories[$key];
  2110. }
  2111. return null;
  2112. }
  2113. /**
  2114. * Get all categories for the feed
  2115. *
  2116. * Uses `<atom:category>`, `<category>` or `<dc:subject>`
  2117. *
  2118. * @since Unknown
  2119. * @return array|null List of {@see SimplePie_Category} objects
  2120. */
  2121. public function get_categories()
  2122. {
  2123. $categories = array();
  2124. foreach ((array) $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'category') as $category)
  2125. {
  2126. $term = null;
  2127. $scheme = null;
  2128. $label = null;
  2129. if (isset($category['attribs']['']['term']))
  2130. {
  2131. $term = $this->sanitize($category['attribs']['']['term'], SIMPLEPIE_CONSTRUCT_TEXT);
  2132. }
  2133. if (isset($category['attribs']['']['scheme']))
  2134. {
  2135. $scheme = $this->sanitize($category['attribs']['']['scheme'], SIMPLEPIE_CONSTRUCT_TEXT);
  2136. }
  2137. if (isset($category['attribs']['']['label']))
  2138. {
  2139. $label = $this->sanitize($category['attribs']['']['label'], SIMPLEPIE_CONSTRUCT_TEXT);
  2140. }
  2141. $categories[] = $this->registry->create('Category', array($term, $scheme, $label));
  2142. }
  2143. foreach ((array) $this->get_channel_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'category') as $category)
  2144. {
  2145. // This is really the label, but keep this as the term also for BC.
  2146. // Label will also work on retrieving because that falls back to term.
  2147. $term = $this->sanitize($category['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2148. if (isset($category['attribs']['']['domain']))
  2149. {
  2150. $scheme = $this->sanitize($category['attribs']['']['domain'], SIMPLEPIE_CONSTRUCT_TEXT);
  2151. }
  2152. else
  2153. {
  2154. $scheme = null;
  2155. }
  2156. $categories[] = $this->registry->create('Category', array($term, $scheme, null));
  2157. }
  2158. foreach ((array) $this->get_channel_tags(SIMPLEPIE_NAMESPACE_DC_11, 'subject') as $category)
  2159. {
  2160. $categories[] = $this->registry->create('Category', array($this->sanitize($category['data'], SIMPLEPIE_CONSTRUCT_TEXT), null, null));
  2161. }
  2162. foreach ((array) $this->get_channel_tags(SIMPLEPIE_NAMESPACE_DC_10, 'subject') as $category)
  2163. {
  2164. $categories[] = $this->registry->create('Category', array($this->sanitize($category['data'], SIMPLEPIE_CONSTRUCT_TEXT), null, null));
  2165. }
  2166. if (!empty($categories))
  2167. {
  2168. return array_unique($categories);
  2169. }
  2170. return null;
  2171. }
  2172. /**
  2173. * Get an author for the feed
  2174. *
  2175. * @since 1.1
  2176. * @param int $key The author that you want to return. Remember that arrays begin with 0, not 1
  2177. * @return SimplePie_Author|null
  2178. */
  2179. public function get_author($key = 0)
  2180. {
  2181. $authors = $this->get_authors();
  2182. if (isset($authors[$key]))
  2183. {
  2184. return $authors[$key];
  2185. }
  2186. return null;
  2187. }
  2188. /**
  2189. * Get all authors for the feed
  2190. *
  2191. * Uses `<atom:author>`, `<author>`, `<dc:creator>` or `<itunes:author>`
  2192. *
  2193. * @since 1.1
  2194. * @return array|null List of {@see SimplePie_Author} objects
  2195. */
  2196. public function get_authors()
  2197. {
  2198. $authors = array();
  2199. foreach ((array) $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'author') as $author)
  2200. {
  2201. $name = null;
  2202. $uri = null;
  2203. $email = null;
  2204. if (isset($author['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data']))
  2205. {
  2206. $name = $this->sanitize($author['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2207. }
  2208. if (isset($author['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data']))
  2209. {
  2210. $uri = $this->sanitize($author['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($author['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]));
  2211. }
  2212. if (isset($author['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['email'][0]['data']))
  2213. {
  2214. $email = $this->sanitize($author['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['email'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2215. }
  2216. if ($name !== null || $email !== null || $uri !== null)
  2217. {
  2218. $authors[] = $this->registry->create('Author', array($name, $uri, $email));
  2219. }
  2220. }
  2221. if ($author = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'author'))
  2222. {
  2223. $name = null;
  2224. $url = null;
  2225. $email = null;
  2226. if (isset($author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['name'][0]['data']))
  2227. {
  2228. $name = $this->sanitize($author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['name'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2229. }
  2230. if (isset($author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['url'][0]['data']))
  2231. {
  2232. $url = $this->sanitize($author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['url'][0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['url'][0]));
  2233. }
  2234. if (isset($author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['email'][0]['data']))
  2235. {
  2236. $email = $this->sanitize($author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['email'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2237. }
  2238. if ($name !== null || $email !== null || $url !== null)
  2239. {
  2240. $authors[] = $this->registry->create('Author', array($name, $url, $email));
  2241. }
  2242. }
  2243. foreach ((array) $this->get_channel_tags(SIMPLEPIE_NAMESPACE_DC_11, 'creator') as $author)
  2244. {
  2245. $authors[] = $this->registry->create('Author', array($this->sanitize($author['data'], SIMPLEPIE_CONSTRUCT_TEXT), null, null));
  2246. }
  2247. foreach ((array) $this->get_channel_tags(SIMPLEPIE_NAMESPACE_DC_10, 'creator') as $author)
  2248. {
  2249. $authors[] = $this->registry->create('Author', array($this->sanitize($author['data'], SIMPLEPIE_CONSTRUCT_TEXT), null, null));
  2250. }
  2251. foreach ((array) $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ITUNES, 'author') as $author)
  2252. {
  2253. $authors[] = $this->registry->create('Author', array($this->sanitize($author['data'], SIMPLEPIE_CONSTRUCT_TEXT), null, null));
  2254. }
  2255. if (!empty($authors))
  2256. {
  2257. return array_unique($authors);
  2258. }
  2259. return null;
  2260. }
  2261. /**
  2262. * Get a contributor for the feed
  2263. *
  2264. * @since 1.1
  2265. * @param int $key The contrbutor that you want to return. Remember that arrays begin with 0, not 1
  2266. * @return SimplePie_Author|null
  2267. */
  2268. public function get_contributor($key = 0)
  2269. {
  2270. $contributors = $this->get_contributors();
  2271. if (isset($contributors[$key]))
  2272. {
  2273. return $contributors[$key];
  2274. }
  2275. return null;
  2276. }
  2277. /**
  2278. * Get all contributors for the feed
  2279. *
  2280. * Uses `<atom:contributor>`
  2281. *
  2282. * @since 1.1
  2283. * @return array|null List of {@see SimplePie_Author} objects
  2284. */
  2285. public function get_contributors()
  2286. {
  2287. $contributors = array();
  2288. foreach ((array) $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'contributor') as $contributor)
  2289. {
  2290. $name = null;
  2291. $uri = null;
  2292. $email = null;
  2293. if (isset($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data']))
  2294. {
  2295. $name = $this->sanitize($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2296. }
  2297. if (isset($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data']))
  2298. {
  2299. $uri = $this->sanitize($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]));
  2300. }
  2301. if (isset($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['email'][0]['data']))
  2302. {
  2303. $email = $this->sanitize($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['email'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2304. }
  2305. if ($name !== null || $email !== null || $uri !== null)
  2306. {
  2307. $contributors[] = $this->registry->create('Author', array($name, $uri, $email));
  2308. }
  2309. }
  2310. foreach ((array) $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'contributor') as $contributor)
  2311. {
  2312. $name = null;
  2313. $url = null;
  2314. $email = null;
  2315. if (isset($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['name'][0]['data']))
  2316. {
  2317. $name = $this->sanitize($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['name'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2318. }
  2319. if (isset($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['url'][0]['data']))
  2320. {
  2321. $url = $this->sanitize($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['url'][0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['url'][0]));
  2322. }
  2323. if (isset($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['email'][0]['data']))
  2324. {
  2325. $email = $this->sanitize($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['email'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2326. }
  2327. if ($name !== null || $email !== null || $url !== null)
  2328. {
  2329. $contributors[] = $this->registry->create('Author', array($name, $url, $email));
  2330. }
  2331. }
  2332. if (!empty($contributors))
  2333. {
  2334. return array_unique($contributors);
  2335. }
  2336. return null;
  2337. }
  2338. /**
  2339. * Get a single link for the feed
  2340. *
  2341. * @since 1.0 (previously called `get_feed_link` since Preview Release, `get_feed_permalink()` since 0.8)
  2342. * @param int $key The link that you want to return. Remember that arrays begin with 0, not 1
  2343. * @param string $rel The relationship of the link to return
  2344. * @return string|null Link URL
  2345. */
  2346. public function get_link($key = 0, $rel = 'alternate')
  2347. {
  2348. $links = $this->get_links($rel);
  2349. if (isset($links[$key]))
  2350. {
  2351. return $links[$key];
  2352. }
  2353. return null;
  2354. }
  2355. /**
  2356. * Get the permalink for the item
  2357. *
  2358. * Returns the first link available with a relationship of "alternate".
  2359. * Identical to {@see get_link()} with key 0
  2360. *
  2361. * @see get_link
  2362. * @since 1.0 (previously called `get_feed_link` since Preview Release, `get_feed_permalink()` since 0.8)
  2363. * @internal Added for parity between the parent-level and the item/entry-level.
  2364. * @return string|null Link URL
  2365. */
  2366. public function get_permalink()
  2367. {
  2368. return $this->get_link(0);
  2369. }
  2370. /**
  2371. * Get all links for the feed
  2372. *
  2373. * Uses `<atom:link>` or `<link>`
  2374. *
  2375. * @since Beta 2
  2376. * @param string $rel The relationship of links to return
  2377. * @return array|null Links found for the feed (strings)
  2378. */
  2379. public function get_links($rel = 'alternate')
  2380. {
  2381. if (!isset($this->data['links']))
  2382. {
  2383. $this->data['links'] = array();
  2384. if ($links = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'link'))
  2385. {
  2386. foreach ($links as $link)
  2387. {
  2388. if (isset($link['attribs']['']['href']))
  2389. {
  2390. $link_rel = (isset($link['attribs']['']['rel'])) ? $link['attribs']['']['rel'] : 'alternate';
  2391. $this->data['links'][$link_rel][] = $this->sanitize($link['attribs']['']['href'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($link));
  2392. }
  2393. }
  2394. }
  2395. if ($links = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'link'))
  2396. {
  2397. foreach ($links as $link)
  2398. {
  2399. if (isset($link['attribs']['']['href']))
  2400. {
  2401. $link_rel = (isset($link['attribs']['']['rel'])) ? $link['attribs']['']['rel'] : 'alternate';
  2402. $this->data['links'][$link_rel][] = $this->sanitize($link['attribs']['']['href'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($link));
  2403. }
  2404. }
  2405. }
  2406. if ($links = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_RSS_10, 'link'))
  2407. {
  2408. $this->data['links']['alternate'][] = $this->sanitize($links[0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($links[0]));
  2409. }
  2410. if ($links = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_RSS_090, 'link'))
  2411. {
  2412. $this->data['links']['alternate'][] = $this->sanitize($links[0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($links[0]));
  2413. }
  2414. if ($links = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'link'))
  2415. {
  2416. $this->data['links']['alternate'][] = $this->sanitize($links[0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($links[0]));
  2417. }
  2418. $keys = array_keys($this->data['links']);
  2419. foreach ($keys as $key)
  2420. {
  2421. if ($this->registry->call('Misc', 'is_isegment_nz_nc', array($key)))
  2422. {
  2423. if (isset($this->data['links'][SIMPLEPIE_IANA_LINK_RELATIONS_REGISTRY . $key]))
  2424. {
  2425. $this->data['links'][SIMPLEPIE_IANA_LINK_RELATIONS_REGISTRY . $key] = array_merge($this->data['links'][$key], $this->data['links'][SIMPLEPIE_IANA_LINK_RELATIONS_REGISTRY . $key]);
  2426. $this->data['links'][$key] =& $this->data['links'][SIMPLEPIE_IANA_LINK_RELATIONS_REGISTRY . $key];
  2427. }
  2428. else
  2429. {
  2430. $this->data['links'][SIMPLEPIE_IANA_LINK_RELATIONS_REGISTRY . $key] =& $this->data['links'][$key];
  2431. }
  2432. }
  2433. elseif (substr($key, 0, 41) === SIMPLEPIE_IANA_LINK_RELATIONS_REGISTRY)
  2434. {
  2435. $this->data['links'][substr($key, 41)] =& $this->data['links'][$key];
  2436. }
  2437. $this->data['links'][$key] = array_unique($this->data['links'][$key]);
  2438. }
  2439. }
  2440. if (isset($this->data['headers']['link']))
  2441. {
  2442. $link_headers = $this->data['headers']['link'];
  2443. if (is_string($link_headers)) {
  2444. $link_headers = array($link_headers);
  2445. }
  2446. $matches = preg_filter('/<([^>]+)>; rel='.preg_quote($rel).'/', '$1', $link_headers);
  2447. if (!empty($matches)) {
  2448. return $matches;
  2449. }
  2450. }
  2451. if (isset($this->data['links'][$rel]))
  2452. {
  2453. return $this->data['links'][$rel];
  2454. }
  2455. return null;
  2456. }
  2457. public function get_all_discovered_feeds()
  2458. {
  2459. return $this->all_discovered_feeds;
  2460. }
  2461. /**
  2462. * Get the content for the item
  2463. *
  2464. * Uses `<atom:subtitle>`, `<atom:tagline>`, `<description>`,
  2465. * `<dc:description>`, `<itunes:summary>` or `<itunes:subtitle>`
  2466. *
  2467. * @since 1.0 (previously called `get_feed_description()` since 0.8)
  2468. * @return string|null
  2469. */
  2470. public function get_description()
  2471. {
  2472. if ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'subtitle'))
  2473. {
  2474. return $this->sanitize($return[0]['data'], $this->registry->call('Misc', 'atom_10_construct_type', array($return[0]['attribs'])), $this->get_base($return[0]));
  2475. }
  2476. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'tagline'))
  2477. {
  2478. return $this->sanitize($return[0]['data'], $this->registry->call('Misc', 'atom_03_construct_type', array($return[0]['attribs'])), $this->get_base($return[0]));
  2479. }
  2480. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_RSS_10, 'description'))
  2481. {
  2482. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_MAYBE_HTML, $this->get_base($return[0]));
  2483. }
  2484. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_RSS_090, 'description'))
  2485. {
  2486. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_MAYBE_HTML, $this->get_base($return[0]));
  2487. }
  2488. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'description'))
  2489. {
  2490. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_HTML, $this->get_base($return[0]));
  2491. }
  2492. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_DC_11, 'description'))
  2493. {
  2494. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2495. }
  2496. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_DC_10, 'description'))
  2497. {
  2498. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2499. }
  2500. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ITUNES, 'summary'))
  2501. {
  2502. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_HTML, $this->get_base($return[0]));
  2503. }
  2504. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ITUNES, 'subtitle'))
  2505. {
  2506. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_HTML, $this->get_base($return[0]));
  2507. }
  2508. return null;
  2509. }
  2510. /**
  2511. * Get the copyright info for the feed
  2512. *
  2513. * Uses `<atom:rights>`, `<atom:copyright>` or `<dc:rights>`
  2514. *
  2515. * @since 1.0 (previously called `get_feed_copyright()` since 0.8)
  2516. * @return string|null
  2517. */
  2518. public function get_copyright()
  2519. {
  2520. if ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'rights'))
  2521. {
  2522. return $this->sanitize($return[0]['data'], $this->registry->call('Misc', 'atom_10_construct_type', array($return[0]['attribs'])), $this->get_base($return[0]));
  2523. }
  2524. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'copyright'))
  2525. {
  2526. return $this->sanitize($return[0]['data'], $this->registry->call('Misc', 'atom_03_construct_type', array($return[0]['attribs'])), $this->get_base($return[0]));
  2527. }
  2528. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'copyright'))
  2529. {
  2530. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2531. }
  2532. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_DC_11, 'rights'))
  2533. {
  2534. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2535. }
  2536. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_DC_10, 'rights'))
  2537. {
  2538. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2539. }
  2540. return null;
  2541. }
  2542. /**
  2543. * Get the language for the feed
  2544. *
  2545. * Uses `<language>`, `<dc:language>`, or @xml_lang
  2546. *
  2547. * @since 1.0 (previously called `get_feed_language()` since 0.8)
  2548. * @return string|null
  2549. */
  2550. public function get_language()
  2551. {
  2552. if ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'language'))
  2553. {
  2554. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2555. }
  2556. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_DC_11, 'language'))
  2557. {
  2558. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2559. }
  2560. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_DC_10, 'language'))
  2561. {
  2562. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2563. }
  2564. elseif (isset($this->data['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['feed'][0]['xml_lang']))
  2565. {
  2566. return $this->sanitize($this->data['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['feed'][0]['xml_lang'], SIMPLEPIE_CONSTRUCT_TEXT);
  2567. }
  2568. elseif (isset($this->data['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['feed'][0]['xml_lang']))
  2569. {
  2570. return $this->sanitize($this->data['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['feed'][0]['xml_lang'], SIMPLEPIE_CONSTRUCT_TEXT);
  2571. }
  2572. elseif (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['xml_lang']))
  2573. {
  2574. return $this->sanitize($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['xml_lang'], SIMPLEPIE_CONSTRUCT_TEXT);
  2575. }
  2576. elseif (isset($this->data['headers']['content-language']))
  2577. {
  2578. return $this->sanitize($this->data['headers']['content-language'], SIMPLEPIE_CONSTRUCT_TEXT);
  2579. }
  2580. return null;
  2581. }
  2582. /**
  2583. * Get the latitude coordinates for the item
  2584. *
  2585. * Compatible with the W3C WGS84 Basic Geo and GeoRSS specifications
  2586. *
  2587. * Uses `<geo:lat>` or `<georss:point>`
  2588. *
  2589. * @since 1.0
  2590. * @link http://www.w3.org/2003/01/geo/ W3C WGS84 Basic Geo
  2591. * @link http://www.georss.org/ GeoRSS
  2592. * @return string|null
  2593. */
  2594. public function get_latitude()
  2595. {
  2596. if ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_W3C_BASIC_GEO, 'lat'))
  2597. {
  2598. return (float) $return[0]['data'];
  2599. }
  2600. elseif (($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_GEORSS, 'point')) && preg_match('/^((?:-)?[0-9]+(?:\.[0-9]+)) ((?:-)?[0-9]+(?:\.[0-9]+))$/', trim($return[0]['data']), $match))
  2601. {
  2602. return (float) $match[1];
  2603. }
  2604. return null;
  2605. }
  2606. /**
  2607. * Get the longitude coordinates for the feed
  2608. *
  2609. * Compatible with the W3C WGS84 Basic Geo and GeoRSS specifications
  2610. *
  2611. * Uses `<geo:long>`, `<geo:lon>` or `<georss:point>`
  2612. *
  2613. * @since 1.0
  2614. * @link http://www.w3.org/2003/01/geo/ W3C WGS84 Basic Geo
  2615. * @link http://www.georss.org/ GeoRSS
  2616. * @return string|null
  2617. */
  2618. public function get_longitude()
  2619. {
  2620. if ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_W3C_BASIC_GEO, 'long'))
  2621. {
  2622. return (float) $return[0]['data'];
  2623. }
  2624. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_W3C_BASIC_GEO, 'lon'))
  2625. {
  2626. return (float) $return[0]['data'];
  2627. }
  2628. elseif (($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_GEORSS, 'point')) && preg_match('/^((?:-)?[0-9]+(?:\.[0-9]+)) ((?:-)?[0-9]+(?:\.[0-9]+))$/', trim($return[0]['data']), $match))
  2629. {
  2630. return (float) $match[2];
  2631. }
  2632. return null;
  2633. }
  2634. /**
  2635. * Get the feed logo's title
  2636. *
  2637. * RSS 0.9.0, 1.0 and 2.0 feeds are allowed to have a "feed logo" title.
  2638. *
  2639. * Uses `<image><title>` or `<image><dc:title>`
  2640. *
  2641. * @return string|null
  2642. */
  2643. public function get_image_title()
  2644. {
  2645. if ($return = $this->get_image_tags(SIMPLEPIE_NAMESPACE_RSS_10, 'title'))
  2646. {
  2647. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2648. }
  2649. elseif ($return = $this->get_image_tags(SIMPLEPIE_NAMESPACE_RSS_090, 'title'))
  2650. {
  2651. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2652. }
  2653. elseif ($return = $this->get_image_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'title'))
  2654. {
  2655. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2656. }
  2657. elseif ($return = $this->get_image_tags(SIMPLEPIE_NAMESPACE_DC_11, 'title'))
  2658. {
  2659. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2660. }
  2661. elseif ($return = $this->get_image_tags(SIMPLEPIE_NAMESPACE_DC_10, 'title'))
  2662. {
  2663. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2664. }
  2665. return null;
  2666. }
  2667. /**
  2668. * Get the feed logo's URL
  2669. *
  2670. * RSS 0.9.0, 2.0, Atom 1.0, and feeds with iTunes RSS tags are allowed to
  2671. * have a "feed logo" URL. This points directly to the image itself.
  2672. *
  2673. * Uses `<itunes:image>`, `<atom:logo>`, `<atom:icon>`,
  2674. * `<image><title>` or `<image><dc:title>`
  2675. *
  2676. * @return string|null
  2677. */
  2678. public function get_image_url()
  2679. {
  2680. if ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ITUNES, 'image'))
  2681. {
  2682. return $this->sanitize($return[0]['attribs']['']['href'], SIMPLEPIE_CONSTRUCT_IRI);
  2683. }
  2684. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'logo'))
  2685. {
  2686. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($return[0]));
  2687. }
  2688. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'icon'))
  2689. {
  2690. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($return[0]));
  2691. }
  2692. elseif ($return = $this->get_image_tags(SIMPLEPIE_NAMESPACE_RSS_10, 'url'))
  2693. {
  2694. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($return[0]));
  2695. }
  2696. elseif ($return = $this->get_image_tags(SIMPLEPIE_NAMESPACE_RSS_090, 'url'))
  2697. {
  2698. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($return[0]));
  2699. }
  2700. elseif ($return = $this->get_image_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'url'))
  2701. {
  2702. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($return[0]));
  2703. }
  2704. return null;
  2705. }
  2706. /**
  2707. * Get the feed logo's link
  2708. *
  2709. * RSS 0.9.0, 1.0 and 2.0 feeds are allowed to have a "feed logo" link. This
  2710. * points to a human-readable page that the image should link to.
  2711. *
  2712. * Uses `<itunes:image>`, `<atom:logo>`, `<atom:icon>`,
  2713. * `<image><title>` or `<image><dc:title>`
  2714. *
  2715. * @return string|null
  2716. */
  2717. public function get_image_link()
  2718. {
  2719. if ($return = $this->get_image_tags(SIMPLEPIE_NAMESPACE_RSS_10, 'link'))
  2720. {
  2721. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($return[0]));
  2722. }
  2723. elseif ($return = $this->get_image_tags(SIMPLEPIE_NAMESPACE_RSS_090, 'link'))
  2724. {
  2725. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($return[0]));
  2726. }
  2727. elseif ($return = $this->get_image_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'link'))
  2728. {
  2729. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($return[0]));
  2730. }
  2731. return null;
  2732. }
  2733. /**
  2734. * Get the feed logo's link
  2735. *
  2736. * RSS 2.0 feeds are allowed to have a "feed logo" width.
  2737. *
  2738. * Uses `<image><width>` or defaults to 88.0 if no width is specified and
  2739. * the feed is an RSS 2.0 feed.
  2740. *
  2741. * @return int|float|null
  2742. */
  2743. public function get_image_width()
  2744. {
  2745. if ($return = $this->get_image_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'width'))
  2746. {
  2747. return round($return[0]['data']);
  2748. }
  2749. elseif ($this->get_type() & SIMPLEPIE_TYPE_RSS_SYNDICATION && $this->get_image_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'url'))
  2750. {
  2751. return 88.0;
  2752. }
  2753. return null;
  2754. }
  2755. /**
  2756. * Get the feed logo's height
  2757. *
  2758. * RSS 2.0 feeds are allowed to have a "feed logo" height.
  2759. *
  2760. * Uses `<image><height>` or defaults to 31.0 if no height is specified and
  2761. * the feed is an RSS 2.0 feed.
  2762. *
  2763. * @return int|float|null
  2764. */
  2765. public function get_image_height()
  2766. {
  2767. if ($return = $this->get_image_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'height'))
  2768. {
  2769. return round($return[0]['data']);
  2770. }
  2771. elseif ($this->get_type() & SIMPLEPIE_TYPE_RSS_SYNDICATION && $this->get_image_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'url'))
  2772. {
  2773. return 31.0;
  2774. }
  2775. return null;
  2776. }
  2777. /**
  2778. * Get the number of items in the feed
  2779. *
  2780. * This is well-suited for {@link http://php.net/for for()} loops with
  2781. * {@see get_item()}
  2782. *
  2783. * @param int $max Maximum value to return. 0 for no limit
  2784. * @return int Number of items in the feed
  2785. */
  2786. public function get_item_quantity($max = 0)
  2787. {
  2788. $max = (int) $max;
  2789. $qty = count($this->get_items());
  2790. if ($max === 0)
  2791. {
  2792. return $qty;
  2793. }
  2794. return ($qty > $max) ? $max : $qty;
  2795. }
  2796. /**
  2797. * Get a single item from the feed
  2798. *
  2799. * This is better suited for {@link http://php.net/for for()} loops, whereas
  2800. * {@see get_items()} is better suited for
  2801. * {@link http://php.net/foreach foreach()} loops.
  2802. *
  2803. * @see get_item_quantity()
  2804. * @since Beta 2
  2805. * @param int $key The item that you want to return. Remember that arrays begin with 0, not 1
  2806. * @return SimplePie_Item|null
  2807. */
  2808. public function get_item($key = 0)
  2809. {
  2810. $items = $this->get_items();
  2811. if (isset($items[$key]))
  2812. {
  2813. return $items[$key];
  2814. }
  2815. return null;
  2816. }
  2817. /**
  2818. * Get all items from the feed
  2819. *
  2820. * This is better suited for {@link http://php.net/for for()} loops, whereas
  2821. * {@see get_items()} is better suited for
  2822. * {@link http://php.net/foreach foreach()} loops.
  2823. *
  2824. * @see get_item_quantity
  2825. * @since Beta 2
  2826. * @param int $start Index to start at
  2827. * @param int $end Number of items to return. 0 for all items after `$start`
  2828. * @return SimplePie_Item[]|null List of {@see SimplePie_Item} objects
  2829. */
  2830. public function get_items($start = 0, $end = 0)
  2831. {
  2832. if (!isset($this->data['items']))
  2833. {
  2834. if (!empty($this->multifeed_objects))
  2835. {
  2836. $this->data['items'] = SimplePie::merge_items($this->multifeed_objects, $start, $end, $this->item_limit);
  2837. if (empty($this->data['items']))
  2838. {
  2839. return array();
  2840. }
  2841. return $this->data['items'];
  2842. }
  2843. $this->data['items'] = array();
  2844. if ($items = $this->get_feed_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'entry'))
  2845. {
  2846. $keys = array_keys($items);
  2847. foreach ($keys as $key)
  2848. {
  2849. $this->data['items'][] = $this->registry->create('Item', array($this, $items[$key]));
  2850. }
  2851. }
  2852. if ($items = $this->get_feed_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'entry'))
  2853. {
  2854. $keys = array_keys($items);
  2855. foreach ($keys as $key)
  2856. {
  2857. $this->data['items'][] = $this->registry->create('Item', array($this, $items[$key]));
  2858. }
  2859. }
  2860. if ($items = $this->get_feed_tags(SIMPLEPIE_NAMESPACE_RSS_10, 'item'))
  2861. {
  2862. $keys = array_keys($items);
  2863. foreach ($keys as $key)
  2864. {
  2865. $this->data['items'][] = $this->registry->create('Item', array($this, $items[$key]));
  2866. }
  2867. }
  2868. if ($items = $this->get_feed_tags(SIMPLEPIE_NAMESPACE_RSS_090, 'item'))
  2869. {
  2870. $keys = array_keys($items);
  2871. foreach ($keys as $key)
  2872. {
  2873. $this->data['items'][] = $this->registry->create('Item', array($this, $items[$key]));
  2874. }
  2875. }
  2876. if ($items = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'item'))
  2877. {
  2878. $keys = array_keys($items);
  2879. foreach ($keys as $key)
  2880. {
  2881. $this->data['items'][] = $this->registry->create('Item', array($this, $items[$key]));
  2882. }
  2883. }
  2884. }
  2885. if (empty($this->data['items']))
  2886. {
  2887. return array();
  2888. }
  2889. if ($this->order_by_date)
  2890. {
  2891. if (!isset($this->data['ordered_items']))
  2892. {
  2893. $this->data['ordered_items'] = $this->data['items'];
  2894. usort($this->data['ordered_items'], array(get_class($this), 'sort_items'));
  2895. }
  2896. $items = $this->data['ordered_items'];
  2897. }
  2898. else
  2899. {
  2900. $items = $this->data['items'];
  2901. }
  2902. // Slice the data as desired
  2903. if ($end === 0)
  2904. {
  2905. return array_slice($items, $start);
  2906. }
  2907. return array_slice($items, $start, $end);
  2908. }
  2909. /**
  2910. * Set the favicon handler
  2911. *
  2912. * @deprecated Use your own favicon handling instead
  2913. */
  2914. public function set_favicon_handler($page = false, $qs = 'i')
  2915. {
  2916. $level = defined('E_USER_DEPRECATED') ? E_USER_DEPRECATED : E_USER_WARNING;
  2917. trigger_error('Favicon handling has been removed, please use your own handling', $level);
  2918. return false;
  2919. }
  2920. /**
  2921. * Get the favicon for the current feed
  2922. *
  2923. * @deprecated Use your own favicon handling instead
  2924. */
  2925. public function get_favicon()
  2926. {
  2927. $level = defined('E_USER_DEPRECATED') ? E_USER_DEPRECATED : E_USER_WARNING;
  2928. trigger_error('Favicon handling has been removed, please use your own handling', $level);
  2929. if (($url = $this->get_link()) !== null)
  2930. {
  2931. return 'https://www.google.com/s2/favicons?domain=' . urlencode($url);
  2932. }
  2933. return false;
  2934. }
  2935. /**
  2936. * Magic method handler
  2937. *
  2938. * @param string $method Method name
  2939. * @param array $args Arguments to the method
  2940. * @return mixed
  2941. */
  2942. public function __call($method, $args)
  2943. {
  2944. if (strpos($method, 'subscribe_') === 0)
  2945. {
  2946. $level = defined('E_USER_DEPRECATED') ? E_USER_DEPRECATED : E_USER_WARNING;
  2947. trigger_error('subscribe_*() has been deprecated, implement the callback yourself', $level);
  2948. return '';
  2949. }
  2950. if ($method === 'enable_xml_dump')
  2951. {
  2952. $level = defined('E_USER_DEPRECATED') ? E_USER_DEPRECATED : E_USER_WARNING;
  2953. trigger_error('enable_xml_dump() has been deprecated, use get_raw_data() instead', $level);
  2954. return false;
  2955. }
  2956. $class = get_class($this);
  2957. $trace = debug_backtrace(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection
  2958. $file = $trace[0]['file'];
  2959. $line = $trace[0]['line'];
  2960. trigger_error("Call to undefined method $class::$method() in $file on line $line", E_USER_ERROR);
  2961. }
  2962. /**
  2963. * Sorting callback for items
  2964. *
  2965. * @access private
  2966. * @param SimplePie $a
  2967. * @param SimplePie $b
  2968. * @return boolean
  2969. */
  2970. public static function sort_items($a, $b)
  2971. {
  2972. $a_date = $a->get_date('U');
  2973. $b_date = $b->get_date('U');
  2974. if ($a_date && $b_date) {
  2975. return $a_date > $b_date ? -1 : 1;
  2976. }
  2977. // Sort items without dates to the top.
  2978. if ($a_date) {
  2979. return 1;
  2980. }
  2981. if ($b_date) {
  2982. return -1;
  2983. }
  2984. return 0;
  2985. }
  2986. /**
  2987. * Merge items from several feeds into one
  2988. *
  2989. * If you're merging multiple feeds together, they need to all have dates
  2990. * for the items or else SimplePie will refuse to sort them.
  2991. *
  2992. * @link http://simplepie.org/wiki/tutorial/sort_multiple_feeds_by_time_and_date#if_feeds_require_separate_per-feed_settings
  2993. * @param array $urls List of SimplePie feed objects to merge
  2994. * @param int $start Starting item
  2995. * @param int $end Number of items to return
  2996. * @param int $limit Maximum number of items per feed
  2997. * @return array
  2998. */
  2999. public static function merge_items($urls, $start = 0, $end = 0, $limit = 0)
  3000. {
  3001. if (is_array($urls) && sizeof($urls) > 0)
  3002. {
  3003. $items = array();
  3004. foreach ($urls as $arg)
  3005. {
  3006. if ($arg instanceof SimplePie)
  3007. {
  3008. $items = array_merge($items, $arg->get_items(0, $limit));
  3009. }
  3010. else
  3011. {
  3012. trigger_error('Arguments must be SimplePie objects', E_USER_WARNING);
  3013. }
  3014. }
  3015. usort($items, array(get_class($urls[0]), 'sort_items'));
  3016. if ($end === 0)
  3017. {
  3018. return array_slice($items, $start);
  3019. }
  3020. return array_slice($items, $start, $end);
  3021. }
  3022. trigger_error('Cannot merge zero SimplePie objects', E_USER_WARNING);
  3023. return array();
  3024. }
  3025. /**
  3026. * Store PubSubHubbub links as headers
  3027. *
  3028. * There is no way to find PuSH links in the body of a microformats feed,
  3029. * so they are added to the headers when found, to be used later by get_links.
  3030. * @param SimplePie_File $file
  3031. * @param string $hub
  3032. * @param string $self
  3033. */
  3034. private function store_links(&$file, $hub, $self) {
  3035. if (isset($file->headers['link']['hub']) ||
  3036. (isset($file->headers['link']) &&
  3037. preg_match('/rel=hub/', $file->headers['link'])))
  3038. {
  3039. return;
  3040. }
  3041. if ($hub)
  3042. {
  3043. if (isset($file->headers['link']))
  3044. {
  3045. if ($file->headers['link'] !== '')
  3046. {
  3047. $file->headers['link'] = ', ';
  3048. }
  3049. }
  3050. else
  3051. {
  3052. $file->headers['link'] = '';
  3053. }
  3054. $file->headers['link'] .= '<'.$hub.'>; rel=hub';
  3055. if ($self)
  3056. {
  3057. $file->headers['link'] .= ', <'.$self.'>; rel=self';
  3058. }
  3059. }
  3060. }
  3061. }
  3062. endif;