Nessuna descrizione

Enclosure.php 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305
  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. switch (strtolower($this->get_extension()))
  1029. {
  1030. // Audio mime-types
  1031. case 'aac':
  1032. case 'adts':
  1033. $type = 'audio/acc';
  1034. break;
  1035. case 'aif':
  1036. case 'aifc':
  1037. case 'aiff':
  1038. case 'cdda':
  1039. $type = 'audio/aiff';
  1040. break;
  1041. case 'bwf':
  1042. $type = 'audio/wav';
  1043. break;
  1044. case 'kar':
  1045. case 'mid':
  1046. case 'midi':
  1047. case 'smf':
  1048. $type = 'audio/midi';
  1049. break;
  1050. case 'm4a':
  1051. $type = 'audio/x-m4a';
  1052. break;
  1053. case 'mp3':
  1054. case 'swa':
  1055. $type = 'audio/mp3';
  1056. break;
  1057. case 'wav':
  1058. $type = 'audio/wav';
  1059. break;
  1060. case 'wax':
  1061. $type = 'audio/x-ms-wax';
  1062. break;
  1063. case 'wma':
  1064. $type = 'audio/x-ms-wma';
  1065. break;
  1066. // Video mime-types
  1067. case '3gp':
  1068. case '3gpp':
  1069. $type = 'video/3gpp';
  1070. break;
  1071. case '3g2':
  1072. case '3gp2':
  1073. $type = 'video/3gpp2';
  1074. break;
  1075. case 'asf':
  1076. $type = 'video/x-ms-asf';
  1077. break;
  1078. case 'flv':
  1079. $type = 'video/x-flv';
  1080. break;
  1081. case 'm1a':
  1082. case 'm1s':
  1083. case 'm1v':
  1084. case 'm15':
  1085. case 'm75':
  1086. case 'mp2':
  1087. case 'mpa':
  1088. case 'mpeg':
  1089. case 'mpg':
  1090. case 'mpm':
  1091. case 'mpv':
  1092. $type = 'video/mpeg';
  1093. break;
  1094. case 'm4v':
  1095. $type = 'video/x-m4v';
  1096. break;
  1097. case 'mov':
  1098. case 'qt':
  1099. $type = 'video/quicktime';
  1100. break;
  1101. case 'mp4':
  1102. case 'mpg4':
  1103. $type = 'video/mp4';
  1104. break;
  1105. case 'sdv':
  1106. $type = 'video/sd-video';
  1107. break;
  1108. case 'wm':
  1109. $type = 'video/x-ms-wm';
  1110. break;
  1111. case 'wmv':
  1112. $type = 'video/x-ms-wmv';
  1113. break;
  1114. case 'wvx':
  1115. $type = 'video/x-ms-wvx';
  1116. break;
  1117. // Flash mime-types
  1118. case 'spl':
  1119. $type = 'application/futuresplash';
  1120. break;
  1121. case 'swf':
  1122. $type = 'application/x-shockwave-flash';
  1123. break;
  1124. }
  1125. }
  1126. if ($find_handler)
  1127. {
  1128. if (in_array($type, $types_flash))
  1129. {
  1130. return 'flash';
  1131. }
  1132. elseif (in_array($type, $types_fmedia))
  1133. {
  1134. return 'fmedia';
  1135. }
  1136. elseif (in_array($type, $types_quicktime))
  1137. {
  1138. return 'quicktime';
  1139. }
  1140. elseif (in_array($type, $types_wmedia))
  1141. {
  1142. return 'wmedia';
  1143. }
  1144. elseif (in_array($type, $types_mp3))
  1145. {
  1146. return 'mp3';
  1147. }
  1148. return null;
  1149. }
  1150. return $type;
  1151. }
  1152. }