Enclosure.php 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379
  1. <?php
  2. /**
  3. * SimplePie
  4. *
  5. * A PHP-Based RSS and Atom Feed Framework.
  6. * Takes the hard work out of managing a complete RSS/Atom solution.
  7. *
  8. * Copyright (c) 2004-2016, Ryan Parman, Geoffrey Sneddon, Ryan McCue, and contributors
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without modification, are
  12. * permitted provided that the following conditions are met:
  13. *
  14. * * Redistributions of source code must retain the above copyright notice, this list of
  15. * conditions and the following disclaimer.
  16. *
  17. * * Redistributions in binary form must reproduce the above copyright notice, this list
  18. * of conditions and the following disclaimer in the documentation and/or other materials
  19. * provided with the distribution.
  20. *
  21. * * Neither the name of the SimplePie Team nor the names of its contributors may be used
  22. * to endorse or promote products derived from this software without specific prior
  23. * written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  26. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  27. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS
  28. * AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  30. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  31. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  32. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. * @package SimplePie
  36. * @copyright 2004-2016 Ryan Parman, Geoffrey Sneddon, Ryan McCue
  37. * @author Ryan Parman
  38. * @author Geoffrey Sneddon
  39. * @author Ryan McCue
  40. * @link http://simplepie.org/ SimplePie
  41. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  42. */
  43. /**
  44. * Handles everything related to enclosures (including Media RSS and iTunes RSS)
  45. *
  46. * Used by {@see SimplePie_Item::get_enclosure()} and {@see SimplePie_Item::get_enclosures()}
  47. *
  48. * This class can be overloaded with {@see SimplePie::set_enclosure_class()}
  49. *
  50. * @package SimplePie
  51. * @subpackage API
  52. */
  53. class SimplePie_Enclosure
  54. {
  55. /**
  56. * @var string
  57. * @see get_bitrate()
  58. */
  59. var $bitrate;
  60. /**
  61. * @var array
  62. * @see get_captions()
  63. */
  64. var $captions;
  65. /**
  66. * @var array
  67. * @see get_categories()
  68. */
  69. var $categories;
  70. /**
  71. * @var int
  72. * @see get_channels()
  73. */
  74. var $channels;
  75. /**
  76. * @var SimplePie_Copyright
  77. * @see get_copyright()
  78. */
  79. var $copyright;
  80. /**
  81. * @var array
  82. * @see get_credits()
  83. */
  84. var $credits;
  85. /**
  86. * @var string
  87. * @see get_description()
  88. */
  89. var $description;
  90. /**
  91. * @var int
  92. * @see get_duration()
  93. */
  94. var $duration;
  95. /**
  96. * @var string
  97. * @see get_expression()
  98. */
  99. var $expression;
  100. /**
  101. * @var string
  102. * @see get_framerate()
  103. */
  104. var $framerate;
  105. /**
  106. * @var string
  107. * @see get_handler()
  108. */
  109. var $handler;
  110. /**
  111. * @var array
  112. * @see get_hashes()
  113. */
  114. var $hashes;
  115. /**
  116. * @var string
  117. * @see get_height()
  118. */
  119. var $height;
  120. /**
  121. * @deprecated
  122. * @var null
  123. */
  124. var $javascript;
  125. /**
  126. * @var array
  127. * @see get_keywords()
  128. */
  129. var $keywords;
  130. /**
  131. * @var string
  132. * @see get_language()
  133. */
  134. var $lang;
  135. /**
  136. * @var string
  137. * @see get_length()
  138. */
  139. var $length;
  140. /**
  141. * @var string
  142. * @see get_link()
  143. */
  144. var $link;
  145. /**
  146. * @var string
  147. * @see get_medium()
  148. */
  149. var $medium;
  150. /**
  151. * @var string
  152. * @see get_player()
  153. */
  154. var $player;
  155. /**
  156. * @var array
  157. * @see get_ratings()
  158. */
  159. var $ratings;
  160. /**
  161. * @var array
  162. * @see get_restrictions()
  163. */
  164. var $restrictions;
  165. /**
  166. * @var string
  167. * @see get_sampling_rate()
  168. */
  169. var $samplingrate;
  170. /**
  171. * @var array
  172. * @see get_thumbnails()
  173. */
  174. var $thumbnails;
  175. /**
  176. * @var string
  177. * @see get_title()
  178. */
  179. var $title;
  180. /**
  181. * @var string
  182. * @see get_type()
  183. */
  184. var $type;
  185. /**
  186. * @var string
  187. * @see get_width()
  188. */
  189. var $width;
  190. /**
  191. * Constructor, used to input the data
  192. *
  193. * For documentation on all the parameters, see the corresponding
  194. * properties and their accessors
  195. *
  196. * @uses idna_convert If available, this will convert an IDN
  197. */
  198. public function __construct($link = null, $type = null, $length = null, $javascript = null, $bitrate = null, $captions = null, $categories = null, $channels = null, $copyright = null, $credits = null, $description = null, $duration = null, $expression = null, $framerate = null, $hashes = null, $height = null, $keywords = null, $lang = null, $medium = null, $player = null, $ratings = null, $restrictions = null, $samplingrate = null, $thumbnails = null, $title = null, $width = null)
  199. {
  200. $this->bitrate = $bitrate;
  201. $this->captions = $captions;
  202. $this->categories = $categories;
  203. $this->channels = $channels;
  204. $this->copyright = $copyright;
  205. $this->credits = $credits;
  206. $this->description = $description;
  207. $this->duration = $duration;
  208. $this->expression = $expression;
  209. $this->framerate = $framerate;
  210. $this->hashes = $hashes;
  211. $this->height = $height;
  212. $this->keywords = $keywords;
  213. $this->lang = $lang;
  214. $this->length = $length;
  215. $this->link = $link;
  216. $this->medium = $medium;
  217. $this->player = $player;
  218. $this->ratings = $ratings;
  219. $this->restrictions = $restrictions;
  220. $this->samplingrate = $samplingrate;
  221. $this->thumbnails = $thumbnails;
  222. $this->title = $title;
  223. $this->type = $type;
  224. $this->width = $width;
  225. if (class_exists('idna_convert'))
  226. {
  227. $idn = new idna_convert();
  228. $parsed = SimplePie_Misc::parse_url($link);
  229. $this->link = SimplePie_Misc::compress_parse_url($parsed['scheme'], $idn->encode($parsed['authority']), $parsed['path'], $parsed['query'], $parsed['fragment']);
  230. }
  231. $this->handler = $this->get_handler(); // Needs to load last
  232. }
  233. /**
  234. * String-ified version
  235. *
  236. * @return string
  237. */
  238. public function __toString()
  239. {
  240. // There is no $this->data here
  241. return md5(serialize($this));
  242. }
  243. /**
  244. * Get the bitrate
  245. *
  246. * @return string|null
  247. */
  248. public function get_bitrate()
  249. {
  250. if ($this->bitrate !== null)
  251. {
  252. return $this->bitrate;
  253. }
  254. else
  255. {
  256. return null;
  257. }
  258. }
  259. /**
  260. * Get a single caption
  261. *
  262. * @param int $key
  263. * @return SimplePie_Caption|null
  264. */
  265. public function get_caption($key = 0)
  266. {
  267. $captions = $this->get_captions();
  268. if (isset($captions[$key]))
  269. {
  270. return $captions[$key];
  271. }
  272. else
  273. {
  274. return null;
  275. }
  276. }
  277. /**
  278. * Get all captions
  279. *
  280. * @return array|null Array of {@see SimplePie_Caption} objects
  281. */
  282. public function get_captions()
  283. {
  284. if ($this->captions !== null)
  285. {
  286. return $this->captions;
  287. }
  288. else
  289. {
  290. return null;
  291. }
  292. }
  293. /**
  294. * Get a single category
  295. *
  296. * @param int $key
  297. * @return SimplePie_Category|null
  298. */
  299. public function get_category($key = 0)
  300. {
  301. $categories = $this->get_categories();
  302. if (isset($categories[$key]))
  303. {
  304. return $categories[$key];
  305. }
  306. else
  307. {
  308. return null;
  309. }
  310. }
  311. /**
  312. * Get all categories
  313. *
  314. * @return array|null Array of {@see SimplePie_Category} objects
  315. */
  316. public function get_categories()
  317. {
  318. if ($this->categories !== null)
  319. {
  320. return $this->categories;
  321. }
  322. else
  323. {
  324. return null;
  325. }
  326. }
  327. /**
  328. * Get the number of audio channels
  329. *
  330. * @return int|null
  331. */
  332. public function get_channels()
  333. {
  334. if ($this->channels !== null)
  335. {
  336. return $this->channels;
  337. }
  338. else
  339. {
  340. return null;
  341. }
  342. }
  343. /**
  344. * Get the copyright information
  345. *
  346. * @return SimplePie_Copyright|null
  347. */
  348. public function get_copyright()
  349. {
  350. if ($this->copyright !== null)
  351. {
  352. return $this->copyright;
  353. }
  354. else
  355. {
  356. return null;
  357. }
  358. }
  359. /**
  360. * Get a single credit
  361. *
  362. * @param int $key
  363. * @return SimplePie_Credit|null
  364. */
  365. public function get_credit($key = 0)
  366. {
  367. $credits = $this->get_credits();
  368. if (isset($credits[$key]))
  369. {
  370. return $credits[$key];
  371. }
  372. else
  373. {
  374. return null;
  375. }
  376. }
  377. /**
  378. * Get all credits
  379. *
  380. * @return array|null Array of {@see SimplePie_Credit} objects
  381. */
  382. public function get_credits()
  383. {
  384. if ($this->credits !== null)
  385. {
  386. return $this->credits;
  387. }
  388. else
  389. {
  390. return null;
  391. }
  392. }
  393. /**
  394. * Get the description of the enclosure
  395. *
  396. * @return string|null
  397. */
  398. public function get_description()
  399. {
  400. if ($this->description !== null)
  401. {
  402. return $this->description;
  403. }
  404. else
  405. {
  406. return null;
  407. }
  408. }
  409. /**
  410. * Get the duration of the enclosure
  411. *
  412. * @param bool $convert Convert seconds into hh:mm:ss
  413. * @return string|int|null 'hh:mm:ss' string if `$convert` was specified, otherwise integer (or null if none found)
  414. */
  415. public function get_duration($convert = false)
  416. {
  417. if ($this->duration !== null)
  418. {
  419. if ($convert)
  420. {
  421. $time = SimplePie_Misc::time_hms($this->duration);
  422. return $time;
  423. }
  424. else
  425. {
  426. return $this->duration;
  427. }
  428. }
  429. else
  430. {
  431. return null;
  432. }
  433. }
  434. /**
  435. * Get the expression
  436. *
  437. * @return string Probably one of 'sample', 'full', 'nonstop', 'clip'. Defaults to 'full'
  438. */
  439. public function get_expression()
  440. {
  441. if ($this->expression !== null)
  442. {
  443. return $this->expression;
  444. }
  445. else
  446. {
  447. return 'full';
  448. }
  449. }
  450. /**
  451. * Get the file extension
  452. *
  453. * @return string|null
  454. */
  455. public function get_extension()
  456. {
  457. if ($this->link !== null)
  458. {
  459. $url = SimplePie_Misc::parse_url($this->link);
  460. if ($url['path'] !== '')
  461. {
  462. return pathinfo($url['path'], PATHINFO_EXTENSION);
  463. }
  464. }
  465. return null;
  466. }
  467. /**
  468. * Get the framerate (in frames-per-second)
  469. *
  470. * @return string|null
  471. */
  472. public function get_framerate()
  473. {
  474. if ($this->framerate !== null)
  475. {
  476. return $this->framerate;
  477. }
  478. else
  479. {
  480. return null;
  481. }
  482. }
  483. /**
  484. * Get the preferred handler
  485. *
  486. * @return string|null One of 'flash', 'fmedia', 'quicktime', 'wmedia', 'mp3'
  487. */
  488. public function get_handler()
  489. {
  490. return $this->get_real_type(true);
  491. }
  492. /**
  493. * Get a single hash
  494. *
  495. * @link http://www.rssboard.org/media-rss#media-hash
  496. * @param int $key
  497. * @return string|null Hash as per `media:hash`, prefixed with "$algo:"
  498. */
  499. public function get_hash($key = 0)
  500. {
  501. $hashes = $this->get_hashes();
  502. if (isset($hashes[$key]))
  503. {
  504. return $hashes[$key];
  505. }
  506. else
  507. {
  508. return null;
  509. }
  510. }
  511. /**
  512. * Get all credits
  513. *
  514. * @return array|null Array of strings, see {@see get_hash()}
  515. */
  516. public function get_hashes()
  517. {
  518. if ($this->hashes !== null)
  519. {
  520. return $this->hashes;
  521. }
  522. else
  523. {
  524. return null;
  525. }
  526. }
  527. /**
  528. * Get the height
  529. *
  530. * @return string|null
  531. */
  532. public function get_height()
  533. {
  534. if ($this->height !== null)
  535. {
  536. return $this->height;
  537. }
  538. else
  539. {
  540. return null;
  541. }
  542. }
  543. /**
  544. * Get the language
  545. *
  546. * @link http://tools.ietf.org/html/rfc3066
  547. * @return string|null Language code as per RFC 3066
  548. */
  549. public function get_language()
  550. {
  551. if ($this->lang !== null)
  552. {
  553. return $this->lang;
  554. }
  555. else
  556. {
  557. return null;
  558. }
  559. }
  560. /**
  561. * Get a single keyword
  562. *
  563. * @param int $key
  564. * @return string|null
  565. */
  566. public function get_keyword($key = 0)
  567. {
  568. $keywords = $this->get_keywords();
  569. if (isset($keywords[$key]))
  570. {
  571. return $keywords[$key];
  572. }
  573. else
  574. {
  575. return null;
  576. }
  577. }
  578. /**
  579. * Get all keywords
  580. *
  581. * @return array|null Array of strings
  582. */
  583. public function get_keywords()
  584. {
  585. if ($this->keywords !== null)
  586. {
  587. return $this->keywords;
  588. }
  589. else
  590. {
  591. return null;
  592. }
  593. }
  594. /**
  595. * Get length
  596. *
  597. * @return float Length in bytes
  598. */
  599. public function get_length()
  600. {
  601. if ($this->length !== null)
  602. {
  603. return $this->length;
  604. }
  605. else
  606. {
  607. return null;
  608. }
  609. }
  610. /**
  611. * Get the URL
  612. *
  613. * @return string|null
  614. */
  615. public function get_link()
  616. {
  617. if ($this->link !== null)
  618. {
  619. return urldecode($this->link);
  620. }
  621. else
  622. {
  623. return null;
  624. }
  625. }
  626. /**
  627. * Get the medium
  628. *
  629. * @link http://www.rssboard.org/media-rss#media-content
  630. * @return string|null Should be one of 'image', 'audio', 'video', 'document', 'executable'
  631. */
  632. public function get_medium()
  633. {
  634. if ($this->medium !== null)
  635. {
  636. return $this->medium;
  637. }
  638. else
  639. {
  640. return null;
  641. }
  642. }
  643. /**
  644. * Get the player URL
  645. *
  646. * Typically the same as {@see get_permalink()}
  647. * @return string|null Player URL
  648. */
  649. public function get_player()
  650. {
  651. if ($this->player !== null)
  652. {
  653. return $this->player;
  654. }
  655. else
  656. {
  657. return null;
  658. }
  659. }
  660. /**
  661. * Get a single rating
  662. *
  663. * @param int $key
  664. * @return SimplePie_Rating|null
  665. */
  666. public function get_rating($key = 0)
  667. {
  668. $ratings = $this->get_ratings();
  669. if (isset($ratings[$key]))
  670. {
  671. return $ratings[$key];
  672. }
  673. else
  674. {
  675. return null;
  676. }
  677. }
  678. /**
  679. * Get all ratings
  680. *
  681. * @return array|null Array of {@see SimplePie_Rating} objects
  682. */
  683. public function get_ratings()
  684. {
  685. if ($this->ratings !== null)
  686. {
  687. return $this->ratings;
  688. }
  689. else
  690. {
  691. return null;
  692. }
  693. }
  694. /**
  695. * Get a single restriction
  696. *
  697. * @param int $key
  698. * @return SimplePie_Restriction|null
  699. */
  700. public function get_restriction($key = 0)
  701. {
  702. $restrictions = $this->get_restrictions();
  703. if (isset($restrictions[$key]))
  704. {
  705. return $restrictions[$key];
  706. }
  707. else
  708. {
  709. return null;
  710. }
  711. }
  712. /**
  713. * Get all restrictions
  714. *
  715. * @return array|null Array of {@see SimplePie_Restriction} objects
  716. */
  717. public function get_restrictions()
  718. {
  719. if ($this->restrictions !== null)
  720. {
  721. return $this->restrictions;
  722. }
  723. else
  724. {
  725. return null;
  726. }
  727. }
  728. /**
  729. * Get the sampling rate (in kHz)
  730. *
  731. * @return string|null
  732. */
  733. public function get_sampling_rate()
  734. {
  735. if ($this->samplingrate !== null)
  736. {
  737. return $this->samplingrate;
  738. }
  739. else
  740. {
  741. return null;
  742. }
  743. }
  744. /**
  745. * Get the file size (in MiB)
  746. *
  747. * @return float|null File size in mebibytes (1048 bytes)
  748. */
  749. public function get_size()
  750. {
  751. $length = $this->get_length();
  752. if ($length !== null)
  753. {
  754. return round($length/1048576, 2);
  755. }
  756. else
  757. {
  758. return null;
  759. }
  760. }
  761. /**
  762. * Get a single thumbnail
  763. *
  764. * @param int $key
  765. * @return string|null Thumbnail URL
  766. */
  767. public function get_thumbnail($key = 0)
  768. {
  769. $thumbnails = $this->get_thumbnails();
  770. if (isset($thumbnails[$key]))
  771. {
  772. return $thumbnails[$key];
  773. }
  774. else
  775. {
  776. return null;
  777. }
  778. }
  779. /**
  780. * Get all thumbnails
  781. *
  782. * @return array|null Array of thumbnail URLs
  783. */
  784. public function get_thumbnails()
  785. {
  786. if ($this->thumbnails !== null)
  787. {
  788. return $this->thumbnails;
  789. }
  790. else
  791. {
  792. return null;
  793. }
  794. }
  795. /**
  796. * Get the title
  797. *
  798. * @return string|null
  799. */
  800. public function get_title()
  801. {
  802. if ($this->title !== null)
  803. {
  804. return $this->title;
  805. }
  806. else
  807. {
  808. return null;
  809. }
  810. }
  811. /**
  812. * Get mimetype of the enclosure
  813. *
  814. * @see get_real_type()
  815. * @return string|null MIME type
  816. */
  817. public function get_type()
  818. {
  819. if ($this->type !== null)
  820. {
  821. return $this->type;
  822. }
  823. else
  824. {
  825. return null;
  826. }
  827. }
  828. /**
  829. * Get the width
  830. *
  831. * @return string|null
  832. */
  833. public function get_width()
  834. {
  835. if ($this->width !== null)
  836. {
  837. return $this->width;
  838. }
  839. else
  840. {
  841. return null;
  842. }
  843. }
  844. /**
  845. * Embed the enclosure using `<embed>`
  846. *
  847. * @deprecated Use the second parameter to {@see embed} instead
  848. *
  849. * @param array|string $options See first paramter to {@see embed}
  850. * @return string HTML string to output
  851. */
  852. public function native_embed($options='')
  853. {
  854. return $this->embed($options, true);
  855. }
  856. /**
  857. * Embed the enclosure using Javascript
  858. *
  859. * `$options` is an array or comma-separated key:value string, with the
  860. * following properties:
  861. *
  862. * - `alt` (string): Alternate content for when an end-user does not have
  863. * the appropriate handler installed or when a file type is
  864. * unsupported. Can be any text or HTML. Defaults to blank.
  865. * - `altclass` (string): If a file type is unsupported, the end-user will
  866. * see the alt text (above) linked directly to the content. That link
  867. * will have this value as its class name. Defaults to blank.
  868. * - `audio` (string): This is an image that should be used as a
  869. * placeholder for audio files before they're loaded (QuickTime-only).
  870. * Can be any relative or absolute URL. Defaults to blank.
  871. * - `bgcolor` (string): The background color for the media, if not
  872. * already transparent. Defaults to `#ffffff`.
  873. * - `height` (integer): The height of the embedded media. Accepts any
  874. * numeric pixel value (such as `360`) or `auto`. Defaults to `auto`,
  875. * and it is recommended that you use this default.
  876. * - `loop` (boolean): Do you want the media to loop when it's done?
  877. * Defaults to `false`.
  878. * - `mediaplayer` (string): The location of the included
  879. * `mediaplayer.swf` file. This allows for the playback of Flash Video
  880. * (`.flv`) files, and is the default handler for non-Odeo MP3's.
  881. * Defaults to blank.
  882. * - `video` (string): This is an image that should be used as a
  883. * placeholder for video files before they're loaded (QuickTime-only).
  884. * Can be any relative or absolute URL. Defaults to blank.
  885. * - `width` (integer): The width of the embedded media. Accepts any
  886. * numeric pixel value (such as `480`) or `auto`. Defaults to `auto`,
  887. * and it is recommended that you use this default.
  888. * - `widescreen` (boolean): Is the enclosure widescreen or standard?
  889. * This applies only to video enclosures, and will automatically resize
  890. * the content appropriately. Defaults to `false`, implying 4:3 mode.
  891. *
  892. * Note: Non-widescreen (4:3) mode with `width` and `height` set to `auto`
  893. * will default to 480x360 video resolution. Widescreen (16:9) mode with
  894. * `width` and `height` set to `auto` will default to 480x270 video resolution.
  895. *
  896. * @todo If the dimensions for media:content are defined, use them when width/height are set to 'auto'.
  897. * @param array|string $options Comma-separated key:value list, or array
  898. * @param bool $native Use `<embed>`
  899. * @return string HTML string to output
  900. */
  901. public function embed($options = '', $native = false)
  902. {
  903. // Set up defaults
  904. $audio = '';
  905. $video = '';
  906. $alt = '';
  907. $altclass = '';
  908. $loop = 'false';
  909. $width = 'auto';
  910. $height = 'auto';
  911. $bgcolor = '#ffffff';
  912. $mediaplayer = '';
  913. $widescreen = false;
  914. $handler = $this->get_handler();
  915. $type = $this->get_real_type();
  916. // Process options and reassign values as necessary
  917. if (is_array($options))
  918. {
  919. extract($options);
  920. }
  921. else
  922. {
  923. $options = explode(',', $options);
  924. foreach($options as $option)
  925. {
  926. $opt = explode(':', $option, 2);
  927. if (isset($opt[0], $opt[1]))
  928. {
  929. $opt[0] = trim($opt[0]);
  930. $opt[1] = trim($opt[1]);
  931. switch ($opt[0])
  932. {
  933. case 'audio':
  934. $audio = $opt[1];
  935. break;
  936. case 'video':
  937. $video = $opt[1];
  938. break;
  939. case 'alt':
  940. $alt = $opt[1];
  941. break;
  942. case 'altclass':
  943. $altclass = $opt[1];
  944. break;
  945. case 'loop':
  946. $loop = $opt[1];
  947. break;
  948. case 'width':
  949. $width = $opt[1];
  950. break;
  951. case 'height':
  952. $height = $opt[1];
  953. break;
  954. case 'bgcolor':
  955. $bgcolor = $opt[1];
  956. break;
  957. case 'mediaplayer':
  958. $mediaplayer = $opt[1];
  959. break;
  960. case 'widescreen':
  961. $widescreen = $opt[1];
  962. break;
  963. }
  964. }
  965. }
  966. }
  967. $mime = explode('/', $type, 2);
  968. $mime = $mime[0];
  969. // Process values for 'auto'
  970. if ($width === 'auto')
  971. {
  972. if ($mime === 'video')
  973. {
  974. if ($height === 'auto')
  975. {
  976. $width = 480;
  977. }
  978. elseif ($widescreen)
  979. {
  980. $width = round((intval($height)/9)*16);
  981. }
  982. else
  983. {
  984. $width = round((intval($height)/3)*4);
  985. }
  986. }
  987. else
  988. {
  989. $width = '100%';
  990. }
  991. }
  992. if ($height === 'auto')
  993. {
  994. if ($mime === 'audio')
  995. {
  996. $height = 0;
  997. }
  998. elseif ($mime === 'video')
  999. {
  1000. if ($width === 'auto')
  1001. {
  1002. if ($widescreen)
  1003. {
  1004. $height = 270;
  1005. }
  1006. else
  1007. {
  1008. $height = 360;
  1009. }
  1010. }
  1011. elseif ($widescreen)
  1012. {
  1013. $height = round((intval($width)/16)*9);
  1014. }
  1015. else
  1016. {
  1017. $height = round((intval($width)/4)*3);
  1018. }
  1019. }
  1020. else
  1021. {
  1022. $height = 376;
  1023. }
  1024. }
  1025. elseif ($mime === 'audio')
  1026. {
  1027. $height = 0;
  1028. }
  1029. // Set proper placeholder value
  1030. if ($mime === 'audio')
  1031. {
  1032. $placeholder = $audio;
  1033. }
  1034. elseif ($mime === 'video')
  1035. {
  1036. $placeholder = $video;
  1037. }
  1038. $embed = '';
  1039. // Flash
  1040. if ($handler === 'flash')
  1041. {
  1042. if ($native)
  1043. {
  1044. $embed .= "<embed src=\"" . $this->get_link() . "\" pluginspage=\"http://adobe.com/go/getflashplayer\" type=\"$type\" quality=\"high\" width=\"$width\" height=\"$height\" bgcolor=\"$bgcolor\" loop=\"$loop\"></embed>";
  1045. }
  1046. else
  1047. {
  1048. $embed .= "<script type='text/javascript'>embed_flash('$bgcolor', '$width', '$height', '" . $this->get_link() . "', '$loop', '$type');</script>";
  1049. }
  1050. }
  1051. // Flash Media Player file types.
  1052. // Preferred handler for MP3 file types.
  1053. elseif ($handler === 'fmedia' || ($handler === 'mp3' && $mediaplayer !== ''))
  1054. {
  1055. $height += 20;
  1056. if ($native)
  1057. {
  1058. $embed .= "<embed src=\"$mediaplayer\" pluginspage=\"http://adobe.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" quality=\"high\" width=\"$width\" height=\"$height\" wmode=\"transparent\" flashvars=\"file=" . rawurlencode($this->get_link().'?file_extension=.'.$this->get_extension()) . "&autostart=false&repeat=$loop&showdigits=true&showfsbutton=false\"></embed>";
  1059. }
  1060. else
  1061. {
  1062. $embed .= "<script type='text/javascript'>embed_flv('$width', '$height', '" . rawurlencode($this->get_link().'?file_extension=.'.$this->get_extension()) . "', '$placeholder', '$loop', '$mediaplayer');</script>";
  1063. }
  1064. }
  1065. // QuickTime 7 file types. Need to test with QuickTime 6.
  1066. // Only handle MP3's if the Flash Media Player is not present.
  1067. elseif ($handler === 'quicktime' || ($handler === 'mp3' && $mediaplayer === ''))
  1068. {
  1069. $height += 16;
  1070. if ($native)
  1071. {
  1072. if ($placeholder !== '')
  1073. {
  1074. $embed .= "<embed type=\"$type\" style=\"cursor:hand; cursor:pointer;\" href=\"" . $this->get_link() . "\" src=\"$placeholder\" width=\"$width\" height=\"$height\" autoplay=\"false\" target=\"myself\" controller=\"false\" loop=\"$loop\" scale=\"aspect\" bgcolor=\"$bgcolor\" pluginspage=\"http://apple.com/quicktime/download/\"></embed>";
  1075. }
  1076. else
  1077. {
  1078. $embed .= "<embed type=\"$type\" style=\"cursor:hand; cursor:pointer;\" src=\"" . $this->get_link() . "\" width=\"$width\" height=\"$height\" autoplay=\"false\" target=\"myself\" controller=\"true\" loop=\"$loop\" scale=\"aspect\" bgcolor=\"$bgcolor\" pluginspage=\"http://apple.com/quicktime/download/\"></embed>";
  1079. }
  1080. }
  1081. else
  1082. {
  1083. $embed .= "<script type='text/javascript'>embed_quicktime('$type', '$bgcolor', '$width', '$height', '" . $this->get_link() . "', '$placeholder', '$loop');</script>";
  1084. }
  1085. }
  1086. // Windows Media
  1087. elseif ($handler === 'wmedia')
  1088. {
  1089. $height += 45;
  1090. if ($native)
  1091. {
  1092. $embed .= "<embed type=\"application/x-mplayer2\" src=\"" . $this->get_link() . "\" autosize=\"1\" width=\"$width\" height=\"$height\" showcontrols=\"1\" showstatusbar=\"0\" showdisplay=\"0\" autostart=\"0\"></embed>";
  1093. }
  1094. else
  1095. {
  1096. $embed .= "<script type='text/javascript'>embed_wmedia('$width', '$height', '" . $this->get_link() . "');</script>";
  1097. }
  1098. }
  1099. // Everything else
  1100. else $embed .= '<a href="' . $this->get_link() . '" class="' . $altclass . '">' . $alt . '</a>';
  1101. return $embed;
  1102. }
  1103. /**
  1104. * Get the real media type
  1105. *
  1106. * Often, feeds lie to us, necessitating a bit of deeper inspection. This
  1107. * converts types to their canonical representations based on the file
  1108. * extension
  1109. *
  1110. * @see get_type()
  1111. * @param bool $find_handler Internal use only, use {@see get_handler()} instead
  1112. * @return string MIME type
  1113. */
  1114. public function get_real_type($find_handler = false)
  1115. {
  1116. // Mime-types by handler.
  1117. $types_flash = array('application/x-shockwave-flash', 'application/futuresplash'); // Flash
  1118. $types_fmedia = array('video/flv', 'video/x-flv','flv-application/octet-stream'); // Flash Media Player
  1119. $types_quicktime = array('audio/3gpp', 'audio/3gpp2', 'audio/aac', 'audio/x-aac', 'audio/aiff', 'audio/x-aiff', 'audio/mid', 'audio/midi', 'audio/x-midi', 'audio/mp4', 'audio/m4a', 'audio/x-m4a', 'audio/wav', 'audio/x-wav', 'video/3gpp', 'video/3gpp2', 'video/m4v', 'video/x-m4v', 'video/mp4', 'video/mpeg', 'video/x-mpeg', 'video/quicktime', 'video/sd-video'); // QuickTime
  1120. $types_wmedia = array('application/asx', 'application/x-mplayer2', 'audio/x-ms-wma', 'audio/x-ms-wax', 'video/x-ms-asf-plugin', 'video/x-ms-asf', 'video/x-ms-wm', 'video/x-ms-wmv', 'video/x-ms-wvx'); // Windows Media
  1121. $types_mp3 = array('audio/mp3', 'audio/x-mp3', 'audio/mpeg', 'audio/x-mpeg'); // MP3
  1122. if ($this->get_type() !== null)
  1123. {
  1124. $type = strtolower($this->type);
  1125. }
  1126. else
  1127. {
  1128. $type = null;
  1129. }
  1130. // If we encounter an unsupported mime-type, check the file extension and guess intelligently.
  1131. if (!in_array($type, array_merge($types_flash, $types_fmedia, $types_quicktime, $types_wmedia, $types_mp3)))
  1132. {
  1133. switch (strtolower($this->get_extension()))
  1134. {
  1135. // Audio mime-types
  1136. case 'aac':
  1137. case 'adts':
  1138. $type = 'audio/acc';
  1139. break;
  1140. case 'aif':
  1141. case 'aifc':
  1142. case 'aiff':
  1143. case 'cdda':
  1144. $type = 'audio/aiff';
  1145. break;
  1146. case 'bwf':
  1147. $type = 'audio/wav';
  1148. break;
  1149. case 'kar':
  1150. case 'mid':
  1151. case 'midi':
  1152. case 'smf':
  1153. $type = 'audio/midi';
  1154. break;
  1155. case 'm4a':
  1156. $type = 'audio/x-m4a';
  1157. break;
  1158. case 'mp3':
  1159. case 'swa':
  1160. $type = 'audio/mp3';
  1161. break;
  1162. case 'wav':
  1163. $type = 'audio/wav';
  1164. break;
  1165. case 'wax':
  1166. $type = 'audio/x-ms-wax';
  1167. break;
  1168. case 'wma':
  1169. $type = 'audio/x-ms-wma';
  1170. break;
  1171. // Video mime-types
  1172. case '3gp':
  1173. case '3gpp':
  1174. $type = 'video/3gpp';
  1175. break;
  1176. case '3g2':
  1177. case '3gp2':
  1178. $type = 'video/3gpp2';
  1179. break;
  1180. case 'asf':
  1181. $type = 'video/x-ms-asf';
  1182. break;
  1183. case 'flv':
  1184. $type = 'video/x-flv';
  1185. break;
  1186. case 'm1a':
  1187. case 'm1s':
  1188. case 'm1v':
  1189. case 'm15':
  1190. case 'm75':
  1191. case 'mp2':
  1192. case 'mpa':
  1193. case 'mpeg':
  1194. case 'mpg':
  1195. case 'mpm':
  1196. case 'mpv':
  1197. $type = 'video/mpeg';
  1198. break;
  1199. case 'm4v':
  1200. $type = 'video/x-m4v';
  1201. break;
  1202. case 'mov':
  1203. case 'qt':
  1204. $type = 'video/quicktime';
  1205. break;
  1206. case 'mp4':
  1207. case 'mpg4':
  1208. $type = 'video/mp4';
  1209. break;
  1210. case 'sdv':
  1211. $type = 'video/sd-video';
  1212. break;
  1213. case 'wm':
  1214. $type = 'video/x-ms-wm';
  1215. break;
  1216. case 'wmv':
  1217. $type = 'video/x-ms-wmv';
  1218. break;
  1219. case 'wvx':
  1220. $type = 'video/x-ms-wvx';
  1221. break;
  1222. // Flash mime-types
  1223. case 'spl':
  1224. $type = 'application/futuresplash';
  1225. break;
  1226. case 'swf':
  1227. $type = 'application/x-shockwave-flash';
  1228. break;
  1229. }
  1230. }
  1231. if ($find_handler)
  1232. {
  1233. if (in_array($type, $types_flash))
  1234. {
  1235. return 'flash';
  1236. }
  1237. elseif (in_array($type, $types_fmedia))
  1238. {
  1239. return 'fmedia';
  1240. }
  1241. elseif (in_array($type, $types_quicktime))
  1242. {
  1243. return 'quicktime';
  1244. }
  1245. elseif (in_array($type, $types_wmedia))
  1246. {
  1247. return 'wmedia';
  1248. }
  1249. elseif (in_array($type, $types_mp3))
  1250. {
  1251. return 'mp3';
  1252. }
  1253. else
  1254. {
  1255. return null;
  1256. }
  1257. }
  1258. else
  1259. {
  1260. return $type;
  1261. }
  1262. }
  1263. }