Enclosure.php 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309
  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, Sam 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, Sam Sneddon, Ryan McCue
  37. * @author Ryan Parman
  38. * @author Sam 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. return null;
  255. }
  256. /**
  257. * Get a single caption
  258. *
  259. * @param int $key
  260. * @return SimplePie_Caption|null
  261. */
  262. public function get_caption($key = 0)
  263. {
  264. $captions = $this->get_captions();
  265. if (isset($captions[$key]))
  266. {
  267. return $captions[$key];
  268. }
  269. return null;
  270. }
  271. /**
  272. * Get all captions
  273. *
  274. * @return array|null Array of {@see SimplePie_Caption} objects
  275. */
  276. public function get_captions()
  277. {
  278. if ($this->captions !== null)
  279. {
  280. return $this->captions;
  281. }
  282. return null;
  283. }
  284. /**
  285. * Get a single category
  286. *
  287. * @param int $key
  288. * @return SimplePie_Category|null
  289. */
  290. public function get_category($key = 0)
  291. {
  292. $categories = $this->get_categories();
  293. if (isset($categories[$key]))
  294. {
  295. return $categories[$key];
  296. }
  297. return null;
  298. }
  299. /**
  300. * Get all categories
  301. *
  302. * @return array|null Array of {@see SimplePie_Category} objects
  303. */
  304. public function get_categories()
  305. {
  306. if ($this->categories !== null)
  307. {
  308. return $this->categories;
  309. }
  310. return null;
  311. }
  312. /**
  313. * Get the number of audio channels
  314. *
  315. * @return int|null
  316. */
  317. public function get_channels()
  318. {
  319. if ($this->channels !== null)
  320. {
  321. return $this->channels;
  322. }
  323. return null;
  324. }
  325. /**
  326. * Get the copyright information
  327. *
  328. * @return SimplePie_Copyright|null
  329. */
  330. public function get_copyright()
  331. {
  332. if ($this->copyright !== null)
  333. {
  334. return $this->copyright;
  335. }
  336. return null;
  337. }
  338. /**
  339. * Get a single credit
  340. *
  341. * @param int $key
  342. * @return SimplePie_Credit|null
  343. */
  344. public function get_credit($key = 0)
  345. {
  346. $credits = $this->get_credits();
  347. if (isset($credits[$key]))
  348. {
  349. return $credits[$key];
  350. }
  351. return null;
  352. }
  353. /**
  354. * Get all credits
  355. *
  356. * @return array|null Array of {@see SimplePie_Credit} objects
  357. */
  358. public function get_credits()
  359. {
  360. if ($this->credits !== null)
  361. {
  362. return $this->credits;
  363. }
  364. return null;
  365. }
  366. /**
  367. * Get the description of the enclosure
  368. *
  369. * @return string|null
  370. */
  371. public function get_description()
  372. {
  373. if ($this->description !== null)
  374. {
  375. return $this->description;
  376. }
  377. return null;
  378. }
  379. /**
  380. * Get the duration of the enclosure
  381. *
  382. * @param bool $convert Convert seconds into hh:mm:ss
  383. * @return string|int|null 'hh:mm:ss' string if `$convert` was specified, otherwise integer (or null if none found)
  384. */
  385. public function get_duration($convert = false)
  386. {
  387. if ($this->duration !== null)
  388. {
  389. if ($convert)
  390. {
  391. $time = SimplePie_Misc::time_hms($this->duration);
  392. return $time;
  393. }
  394. return $this->duration;
  395. }
  396. return null;
  397. }
  398. /**
  399. * Get the expression
  400. *
  401. * @return string Probably one of 'sample', 'full', 'nonstop', 'clip'. Defaults to 'full'
  402. */
  403. public function get_expression()
  404. {
  405. if ($this->expression !== null)
  406. {
  407. return $this->expression;
  408. }
  409. return 'full';
  410. }
  411. /**
  412. * Get the file extension
  413. *
  414. * @return string|null
  415. */
  416. public function get_extension()
  417. {
  418. if ($this->link !== null)
  419. {
  420. $url = SimplePie_Misc::parse_url($this->link);
  421. if ($url['path'] !== '')
  422. {
  423. return pathinfo($url['path'], PATHINFO_EXTENSION);
  424. }
  425. }
  426. return null;
  427. }
  428. /**
  429. * Get the framerate (in frames-per-second)
  430. *
  431. * @return string|null
  432. */
  433. public function get_framerate()
  434. {
  435. if ($this->framerate !== null)
  436. {
  437. return $this->framerate;
  438. }
  439. return null;
  440. }
  441. /**
  442. * Get the preferred handler
  443. *
  444. * @return string|null One of 'flash', 'fmedia', 'quicktime', 'wmedia', 'mp3'
  445. */
  446. public function get_handler()
  447. {
  448. return $this->get_real_type(true);
  449. }
  450. /**
  451. * Get a single hash
  452. *
  453. * @link http://www.rssboard.org/media-rss#media-hash
  454. * @param int $key
  455. * @return string|null Hash as per `media:hash`, prefixed with "$algo:"
  456. */
  457. public function get_hash($key = 0)
  458. {
  459. $hashes = $this->get_hashes();
  460. if (isset($hashes[$key]))
  461. {
  462. return $hashes[$key];
  463. }
  464. return null;
  465. }
  466. /**
  467. * Get all credits
  468. *
  469. * @return array|null Array of strings, see {@see get_hash()}
  470. */
  471. public function get_hashes()
  472. {
  473. if ($this->hashes !== null)
  474. {
  475. return $this->hashes;
  476. }
  477. return null;
  478. }
  479. /**
  480. * Get the height
  481. *
  482. * @return string|null
  483. */
  484. public function get_height()
  485. {
  486. if ($this->height !== null)
  487. {
  488. return $this->height;
  489. }
  490. return null;
  491. }
  492. /**
  493. * Get the language
  494. *
  495. * @link http://tools.ietf.org/html/rfc3066
  496. * @return string|null Language code as per RFC 3066
  497. */
  498. public function get_language()
  499. {
  500. if ($this->lang !== null)
  501. {
  502. return $this->lang;
  503. }
  504. return null;
  505. }
  506. /**
  507. * Get a single keyword
  508. *
  509. * @param int $key
  510. * @return string|null
  511. */
  512. public function get_keyword($key = 0)
  513. {
  514. $keywords = $this->get_keywords();
  515. if (isset($keywords[$key]))
  516. {
  517. return $keywords[$key];
  518. }
  519. return null;
  520. }
  521. /**
  522. * Get all keywords
  523. *
  524. * @return array|null Array of strings
  525. */
  526. public function get_keywords()
  527. {
  528. if ($this->keywords !== null)
  529. {
  530. return $this->keywords;
  531. }
  532. return null;
  533. }
  534. /**
  535. * Get length
  536. *
  537. * @return float Length in bytes
  538. */
  539. public function get_length()
  540. {
  541. if ($this->length !== null)
  542. {
  543. return $this->length;
  544. }
  545. return null;
  546. }
  547. /**
  548. * Get the URL
  549. *
  550. * @return string|null
  551. */
  552. public function get_link()
  553. {
  554. if ($this->link !== null)
  555. {
  556. return urldecode($this->link);
  557. }
  558. return null;
  559. }
  560. /**
  561. * Get the medium
  562. *
  563. * @link http://www.rssboard.org/media-rss#media-content
  564. * @return string|null Should be one of 'image', 'audio', 'video', 'document', 'executable'
  565. */
  566. public function get_medium()
  567. {
  568. if ($this->medium !== null)
  569. {
  570. return $this->medium;
  571. }
  572. return null;
  573. }
  574. /**
  575. * Get the player URL
  576. *
  577. * Typically the same as {@see get_permalink()}
  578. * @return string|null Player URL
  579. */
  580. public function get_player()
  581. {
  582. if ($this->player !== null)
  583. {
  584. return $this->player;
  585. }
  586. return null;
  587. }
  588. /**
  589. * Get a single rating
  590. *
  591. * @param int $key
  592. * @return SimplePie_Rating|null
  593. */
  594. public function get_rating($key = 0)
  595. {
  596. $ratings = $this->get_ratings();
  597. if (isset($ratings[$key]))
  598. {
  599. return $ratings[$key];
  600. }
  601. return null;
  602. }
  603. /**
  604. * Get all ratings
  605. *
  606. * @return array|null Array of {@see SimplePie_Rating} objects
  607. */
  608. public function get_ratings()
  609. {
  610. if ($this->ratings !== null)
  611. {
  612. return $this->ratings;
  613. }
  614. return null;
  615. }
  616. /**
  617. * Get a single restriction
  618. *
  619. * @param int $key
  620. * @return SimplePie_Restriction|null
  621. */
  622. public function get_restriction($key = 0)
  623. {
  624. $restrictions = $this->get_restrictions();
  625. if (isset($restrictions[$key]))
  626. {
  627. return $restrictions[$key];
  628. }
  629. return null;
  630. }
  631. /**
  632. * Get all restrictions
  633. *
  634. * @return array|null Array of {@see SimplePie_Restriction} objects
  635. */
  636. public function get_restrictions()
  637. {
  638. if ($this->restrictions !== null)
  639. {
  640. return $this->restrictions;
  641. }
  642. return null;
  643. }
  644. /**
  645. * Get the sampling rate (in kHz)
  646. *
  647. * @return string|null
  648. */
  649. public function get_sampling_rate()
  650. {
  651. if ($this->samplingrate !== null)
  652. {
  653. return $this->samplingrate;
  654. }
  655. return null;
  656. }
  657. /**
  658. * Get the file size (in MiB)
  659. *
  660. * @return float|null File size in mebibytes (1048 bytes)
  661. */
  662. public function get_size()
  663. {
  664. $length = $this->get_length();
  665. if ($length !== null)
  666. {
  667. return round($length/1048576, 2);
  668. }
  669. return null;
  670. }
  671. /**
  672. * Get a single thumbnail
  673. *
  674. * @param int $key
  675. * @return string|null Thumbnail URL
  676. */
  677. public function get_thumbnail($key = 0)
  678. {
  679. $thumbnails = $this->get_thumbnails();
  680. if (isset($thumbnails[$key]))
  681. {
  682. return $thumbnails[$key];
  683. }
  684. return null;
  685. }
  686. /**
  687. * Get all thumbnails
  688. *
  689. * @return array|null Array of thumbnail URLs
  690. */
  691. public function get_thumbnails()
  692. {
  693. if ($this->thumbnails !== null)
  694. {
  695. return $this->thumbnails;
  696. }
  697. return null;
  698. }
  699. /**
  700. * Get the title
  701. *
  702. * @return string|null
  703. */
  704. public function get_title()
  705. {
  706. if ($this->title !== null)
  707. {
  708. return $this->title;
  709. }
  710. return null;
  711. }
  712. /**
  713. * Get mimetype of the enclosure
  714. *
  715. * @see get_real_type()
  716. * @return string|null MIME type
  717. */
  718. public function get_type()
  719. {
  720. if ($this->type !== null)
  721. {
  722. return $this->type;
  723. }
  724. return null;
  725. }
  726. /**
  727. * Get the width
  728. *
  729. * @return string|null
  730. */
  731. public function get_width()
  732. {
  733. if ($this->width !== null)
  734. {
  735. return $this->width;
  736. }
  737. return null;
  738. }
  739. /**
  740. * Embed the enclosure using `<embed>`
  741. *
  742. * @deprecated Use the second parameter to {@see embed} instead
  743. *
  744. * @param array|string $options See first paramter to {@see embed}
  745. * @return string HTML string to output
  746. */
  747. public function native_embed($options='')
  748. {
  749. return $this->embed($options, true);
  750. }
  751. /**
  752. * Embed the enclosure using Javascript
  753. *
  754. * `$options` is an array or comma-separated key:value string, with the
  755. * following properties:
  756. *
  757. * - `alt` (string): Alternate content for when an end-user does not have
  758. * the appropriate handler installed or when a file type is
  759. * unsupported. Can be any text or HTML. Defaults to blank.
  760. * - `altclass` (string): If a file type is unsupported, the end-user will
  761. * see the alt text (above) linked directly to the content. That link
  762. * will have this value as its class name. Defaults to blank.
  763. * - `audio` (string): This is an image that should be used as a
  764. * placeholder for audio files before they're loaded (QuickTime-only).
  765. * Can be any relative or absolute URL. Defaults to blank.
  766. * - `bgcolor` (string): The background color for the media, if not
  767. * already transparent. Defaults to `#ffffff`.
  768. * - `height` (integer): The height of the embedded media. Accepts any
  769. * numeric pixel value (such as `360`) or `auto`. Defaults to `auto`,
  770. * and it is recommended that you use this default.
  771. * - `loop` (boolean): Do you want the media to loop when it's done?
  772. * Defaults to `false`.
  773. * - `mediaplayer` (string): The location of the included
  774. * `mediaplayer.swf` file. This allows for the playback of Flash Video
  775. * (`.flv`) files, and is the default handler for non-Odeo MP3's.
  776. * Defaults to blank.
  777. * - `video` (string): This is an image that should be used as a
  778. * placeholder for video files before they're loaded (QuickTime-only).
  779. * Can be any relative or absolute URL. Defaults to blank.
  780. * - `width` (integer): The width of the embedded media. Accepts any
  781. * numeric pixel value (such as `480`) or `auto`. Defaults to `auto`,
  782. * and it is recommended that you use this default.
  783. * - `widescreen` (boolean): Is the enclosure widescreen or standard?
  784. * This applies only to video enclosures, and will automatically resize
  785. * the content appropriately. Defaults to `false`, implying 4:3 mode.
  786. *
  787. * Note: Non-widescreen (4:3) mode with `width` and `height` set to `auto`
  788. * will default to 480x360 video resolution. Widescreen (16:9) mode with
  789. * `width` and `height` set to `auto` will default to 480x270 video resolution.
  790. *
  791. * @todo If the dimensions for media:content are defined, use them when width/height are set to 'auto'.
  792. * @param array|string $options Comma-separated key:value list, or array
  793. * @param bool $native Use `<embed>`
  794. * @return string HTML string to output
  795. */
  796. public function embed($options = '', $native = false)
  797. {
  798. // Set up defaults
  799. $audio = '';
  800. $video = '';
  801. $alt = '';
  802. $altclass = '';
  803. $loop = 'false';
  804. $width = 'auto';
  805. $height = 'auto';
  806. $bgcolor = '#ffffff';
  807. $mediaplayer = '';
  808. $widescreen = false;
  809. $handler = $this->get_handler();
  810. $type = $this->get_real_type();
  811. // Process options and reassign values as necessary
  812. if (is_array($options))
  813. {
  814. extract($options);
  815. }
  816. else
  817. {
  818. $options = explode(',', $options);
  819. foreach($options as $option)
  820. {
  821. $opt = explode(':', $option, 2);
  822. if (isset($opt[0], $opt[1]))
  823. {
  824. $opt[0] = trim($opt[0]);
  825. $opt[1] = trim($opt[1]);
  826. switch ($opt[0])
  827. {
  828. case 'audio':
  829. $audio = $opt[1];
  830. break;
  831. case 'video':
  832. $video = $opt[1];
  833. break;
  834. case 'alt':
  835. $alt = $opt[1];
  836. break;
  837. case 'altclass':
  838. $altclass = $opt[1];
  839. break;
  840. case 'loop':
  841. $loop = $opt[1];
  842. break;
  843. case 'width':
  844. $width = $opt[1];
  845. break;
  846. case 'height':
  847. $height = $opt[1];
  848. break;
  849. case 'bgcolor':
  850. $bgcolor = $opt[1];
  851. break;
  852. case 'mediaplayer':
  853. $mediaplayer = $opt[1];
  854. break;
  855. case 'widescreen':
  856. $widescreen = $opt[1];
  857. break;
  858. }
  859. }
  860. }
  861. }
  862. $mime = explode('/', $type, 2);
  863. $mime = $mime[0];
  864. // Process values for 'auto'
  865. if ($width === 'auto')
  866. {
  867. if ($mime === 'video')
  868. {
  869. if ($height === 'auto')
  870. {
  871. $width = 480;
  872. }
  873. elseif ($widescreen)
  874. {
  875. $width = round((intval($height)/9)*16);
  876. }
  877. else
  878. {
  879. $width = round((intval($height)/3)*4);
  880. }
  881. }
  882. else
  883. {
  884. $width = '100%';
  885. }
  886. }
  887. if ($height === 'auto')
  888. {
  889. if ($mime === 'audio')
  890. {
  891. $height = 0;
  892. }
  893. elseif ($mime === 'video')
  894. {
  895. if ($width === 'auto')
  896. {
  897. if ($widescreen)
  898. {
  899. $height = 270;
  900. }
  901. else
  902. {
  903. $height = 360;
  904. }
  905. }
  906. elseif ($widescreen)
  907. {
  908. $height = round((intval($width)/16)*9);
  909. }
  910. else
  911. {
  912. $height = round((intval($width)/4)*3);
  913. }
  914. }
  915. else
  916. {
  917. $height = 376;
  918. }
  919. }
  920. elseif ($mime === 'audio')
  921. {
  922. $height = 0;
  923. }
  924. // Set proper placeholder value
  925. if ($mime === 'audio')
  926. {
  927. $placeholder = $audio;
  928. }
  929. elseif ($mime === 'video')
  930. {
  931. $placeholder = $video;
  932. }
  933. $embed = '';
  934. // Flash
  935. if ($handler === 'flash')
  936. {
  937. if ($native)
  938. {
  939. $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>";
  940. }
  941. else
  942. {
  943. $embed .= "<script type='text/javascript'>embed_flash('$bgcolor', '$width', '$height', '" . $this->get_link() . "', '$loop', '$type');</script>";
  944. }
  945. }
  946. // Flash Media Player file types.
  947. // Preferred handler for MP3 file types.
  948. elseif ($handler === 'fmedia' || ($handler === 'mp3' && $mediaplayer !== ''))
  949. {
  950. $height += 20;
  951. if ($native)
  952. {
  953. $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>";
  954. }
  955. else
  956. {
  957. $embed .= "<script type='text/javascript'>embed_flv('$width', '$height', '" . rawurlencode($this->get_link().'?file_extension=.'.$this->get_extension()) . "', '$placeholder', '$loop', '$mediaplayer');</script>";
  958. }
  959. }
  960. // QuickTime 7 file types. Need to test with QuickTime 6.
  961. // Only handle MP3's if the Flash Media Player is not present.
  962. elseif ($handler === 'quicktime' || ($handler === 'mp3' && $mediaplayer === ''))
  963. {
  964. $height += 16;
  965. if ($native)
  966. {
  967. if ($placeholder !== '')
  968. {
  969. $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>";
  970. }
  971. else
  972. {
  973. $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>";
  974. }
  975. }
  976. else
  977. {
  978. $embed .= "<script type='text/javascript'>embed_quicktime('$type', '$bgcolor', '$width', '$height', '" . $this->get_link() . "', '$placeholder', '$loop');</script>";
  979. }
  980. }
  981. // Windows Media
  982. elseif ($handler === 'wmedia')
  983. {
  984. $height += 45;
  985. if ($native)
  986. {
  987. $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>";
  988. }
  989. else
  990. {
  991. $embed .= "<script type='text/javascript'>embed_wmedia('$width', '$height', '" . $this->get_link() . "');</script>";
  992. }
  993. }
  994. // Everything else
  995. else $embed .= '<a href="' . $this->get_link() . '" class="' . $altclass . '">' . $alt . '</a>';
  996. return $embed;
  997. }
  998. /**
  999. * Get the real media type
  1000. *
  1001. * Often, feeds lie to us, necessitating a bit of deeper inspection. This
  1002. * converts types to their canonical representations based on the file
  1003. * extension
  1004. *
  1005. * @see get_type()
  1006. * @param bool $find_handler Internal use only, use {@see get_handler()} instead
  1007. * @return string MIME type
  1008. */
  1009. public function get_real_type($find_handler = false)
  1010. {
  1011. // Mime-types by handler.
  1012. $types_flash = array('application/x-shockwave-flash', 'application/futuresplash'); // Flash
  1013. $types_fmedia = array('video/flv', 'video/x-flv','flv-application/octet-stream'); // Flash Media Player
  1014. $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
  1015. $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
  1016. $types_mp3 = array('audio/mp3', 'audio/x-mp3', 'audio/mpeg', 'audio/x-mpeg'); // MP3
  1017. if ($this->get_type() !== null)
  1018. {
  1019. $type = strtolower($this->type);
  1020. }
  1021. else
  1022. {
  1023. $type = null;
  1024. }
  1025. // If we encounter an unsupported mime-type, check the file extension and guess intelligently.
  1026. if (!in_array($type, array_merge($types_flash, $types_fmedia, $types_quicktime, $types_wmedia, $types_mp3)))
  1027. {
  1028. $extension = $this->get_extension();
  1029. if ($extension === null) {
  1030. return null;
  1031. }
  1032. switch (strtolower($extension))
  1033. {
  1034. // Audio mime-types
  1035. case 'aac':
  1036. case 'adts':
  1037. $type = 'audio/acc';
  1038. break;
  1039. case 'aif':
  1040. case 'aifc':
  1041. case 'aiff':
  1042. case 'cdda':
  1043. $type = 'audio/aiff';
  1044. break;
  1045. case 'bwf':
  1046. $type = 'audio/wav';
  1047. break;
  1048. case 'kar':
  1049. case 'mid':
  1050. case 'midi':
  1051. case 'smf':
  1052. $type = 'audio/midi';
  1053. break;
  1054. case 'm4a':
  1055. $type = 'audio/x-m4a';
  1056. break;
  1057. case 'mp3':
  1058. case 'swa':
  1059. $type = 'audio/mp3';
  1060. break;
  1061. case 'wav':
  1062. $type = 'audio/wav';
  1063. break;
  1064. case 'wax':
  1065. $type = 'audio/x-ms-wax';
  1066. break;
  1067. case 'wma':
  1068. $type = 'audio/x-ms-wma';
  1069. break;
  1070. // Video mime-types
  1071. case '3gp':
  1072. case '3gpp':
  1073. $type = 'video/3gpp';
  1074. break;
  1075. case '3g2':
  1076. case '3gp2':
  1077. $type = 'video/3gpp2';
  1078. break;
  1079. case 'asf':
  1080. $type = 'video/x-ms-asf';
  1081. break;
  1082. case 'flv':
  1083. $type = 'video/x-flv';
  1084. break;
  1085. case 'm1a':
  1086. case 'm1s':
  1087. case 'm1v':
  1088. case 'm15':
  1089. case 'm75':
  1090. case 'mp2':
  1091. case 'mpa':
  1092. case 'mpeg':
  1093. case 'mpg':
  1094. case 'mpm':
  1095. case 'mpv':
  1096. $type = 'video/mpeg';
  1097. break;
  1098. case 'm4v':
  1099. $type = 'video/x-m4v';
  1100. break;
  1101. case 'mov':
  1102. case 'qt':
  1103. $type = 'video/quicktime';
  1104. break;
  1105. case 'mp4':
  1106. case 'mpg4':
  1107. $type = 'video/mp4';
  1108. break;
  1109. case 'sdv':
  1110. $type = 'video/sd-video';
  1111. break;
  1112. case 'wm':
  1113. $type = 'video/x-ms-wm';
  1114. break;
  1115. case 'wmv':
  1116. $type = 'video/x-ms-wmv';
  1117. break;
  1118. case 'wvx':
  1119. $type = 'video/x-ms-wvx';
  1120. break;
  1121. // Flash mime-types
  1122. case 'spl':
  1123. $type = 'application/futuresplash';
  1124. break;
  1125. case 'swf':
  1126. $type = 'application/x-shockwave-flash';
  1127. break;
  1128. }
  1129. }
  1130. if ($find_handler)
  1131. {
  1132. if (in_array($type, $types_flash))
  1133. {
  1134. return 'flash';
  1135. }
  1136. elseif (in_array($type, $types_fmedia))
  1137. {
  1138. return 'fmedia';
  1139. }
  1140. elseif (in_array($type, $types_quicktime))
  1141. {
  1142. return 'quicktime';
  1143. }
  1144. elseif (in_array($type, $types_wmedia))
  1145. {
  1146. return 'wmedia';
  1147. }
  1148. elseif (in_array($type, $types_mp3))
  1149. {
  1150. return 'mp3';
  1151. }
  1152. return null;
  1153. }
  1154. return $type;
  1155. }
  1156. }