PageRenderTime 45ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/youtube-embed/includes/generate-embed-code.php

https://bitbucket.org/crypticrod/sr_wp_code
PHP | 489 lines | 303 code | 55 blank | 131 comment | 107 complexity | eab88fb5d4d6ab2c32845ab541987127 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1, GPL-3.0, LGPL-2.0, AGPL-3.0
  1. <?php
  2. /**
  3. * Generate embed code
  4. *
  5. * Functions calls to generate the required YouTube code
  6. *
  7. * @package YouTubeEmbed
  8. */
  9. /**
  10. * Generate embed code
  11. *
  12. * Generate XHTML compatible YouTube embed code
  13. *
  14. * @since 2.0
  15. *
  16. * @uses ye_convert_id Convert special characters in ID
  17. * @uses ye_error Display an error
  18. * @uses ye_extract_id Get the video ID
  19. * @uses ye_validate_list Get the requested listr
  20. * @uses ye_validate_id Validate the video ID
  21. * @uses ye_validate_profile Get the requested profile
  22. * @uses ye_set_general_defaults Get general options
  23. * @uses ye_set_profile_defaults Set default profile options
  24. *
  25. * @param string $id Video ID
  26. * @param string $type Embed type
  27. * @param string $width Video width
  28. * @param string $height Video height
  29. * @param string $fullscreen Fullscreen button
  30. * @param string $related Show related info.
  31. * @param string $autoplay Start video automatically
  32. * @param string $loop Loop video to start
  33. * @param string $start Start in seconds
  34. * @param string $info Show video info.
  35. * @param string $annotation Annotations
  36. * @param string $cc Closed captions
  37. * @param string $style Stylesheet information
  38. * @param string $link Link back to YouTube
  39. * @param string $react Show EmbedPlus reactions
  40. * @param string $stop Stop in seconds
  41. * @param string $sweetspot Show EmbedPlus sweetspots
  42. * @param string $disablekb Disable keyboard controls
  43. * @param string $ratio Video size ratio
  44. * @param string $autohide Autohide controls
  45. * @param string $controls Display controls
  46. * @param string $profile Which profile to use
  47. * @param string $list_style How to use a list, if used
  48. * @param string $audio Only show controls, for audio playback
  49. * @param string $template Display template
  50. * @param string $hd Use HD, if available
  51. * @param string $color Progress bar colour
  52. * @param string $theme Use dark or light theme
  53. * @param string $https Use HTTPS for links
  54. * @return string Code output
  55. */
  56. function ye_generate_youtube_code( $id = '', $type = '', $width = '', $height = '', $fullscreen = '', $related = '', $autoplay = '', $loop = '', $start = '', $info = '', $annotation = '', $cc = '', $style = '', $link = '', $react = '', $stop = '', $sweetspot = '', $disablekb = '', $ratio = '', $autohide = '', $controls = '', $profile = '', $list_style = '', $audio = '', $template = '', $hd = '', $color = '', $theme = '', $https = '' ) {
  57. // Ensure an ID is passed
  58. if ( $id == '' ) { return ye_error( 'No video/playlist ID has been supplied' ); }
  59. // Get general options
  60. $general = ye_set_general_defaults();
  61. // Find the profile, if one is specified
  62. $profile = ye_validate_profile( $profile, $general[ 'profile_no' ] );
  63. // Get default values if no values are supplied
  64. $options = ye_set_profile_defaults( $profile );
  65. // Check it's not a list
  66. $playlist_ids = '';
  67. $list = ye_validate_list( $id, $general[ 'list_no' ] );
  68. if ( !is_array( $list ) ) {
  69. // Check if certain parameters are included in the URL
  70. $width = ye_get_url_para( $id, 'w', $width );
  71. $height = ye_get_url_para( $id, 'h', $height );
  72. // Extract the ID if a full URL has been specified
  73. $id = ye_extract_id( $id );
  74. // Is it being previewed? In which case remove any cache
  75. if ( ( preg_match( '/p=([0-9]*)&preview=true/', $_SERVER['QUERY_STRING'] ) == 1 ) && ( $general[ 'preview' ] == 1 ) ) {
  76. delete_transient( 'ye_type_' . $id );
  77. delete_transient( 'ye_title_' . $id );
  78. }
  79. // Check what type of video it is and whether it's valid
  80. $return = ye_validate_id( $id, true );
  81. $title = $return['title'];
  82. $embed_type = $return[ 'type' ];
  83. // If the video is invalid, output an appropriate error
  84. if ( ( $embed_type == '' ) or ( strlen ( $embed_type ) != 1 ) ) {
  85. if ( $embed_type == '' ) {
  86. $error = 'The YouTube ID of ' . $id . ' is invalid.';
  87. } else {
  88. $error = $embed_type;
  89. }
  90. $result = "\n<!-- YouTube Embed v" . youtube_embed_version . " | http://www.artiss.co.uk/artiss-youtube-embed -->\n";
  91. $result .= "<!-- " . $error . " -->\n" . ye_decode( $general[ 'error_message' ] ) . "\n<!-- End of YouTube Embed code -->\n";
  92. return $result;
  93. }
  94. } else {
  95. $embed_type = 'v';
  96. // Randomize the video
  97. if ( $list_style == 'random' ) { shuffle( $list ); }
  98. // Extract one video randomly
  99. if ( $list_style == 'single' ) {
  100. $id = $list[ array_rand( $list, 1 ) ];
  101. // Build the playlist
  102. } else {
  103. $id = $list [ 0 ];
  104. $title = '';
  105. // Build the playlist
  106. if ( count( $list ) > 1 ) {
  107. $loop = 1;
  108. while ( $loop < count( $list ) ) {
  109. if ( $playlist_ids != '' ) { $playlist_ids .= ','; }
  110. $list_id = ye_extract_id( $list[ $loop ] );
  111. $playlist_ids .= $list_id;
  112. $loop ++;
  113. }
  114. }
  115. }
  116. }
  117. // Generate a cache key for the above passed parameters
  118. $cache_key = 'ye_video_' . base64_encode( sha1( $id . $type . $width . $height . $fullscreen . $related . $autoplay . $loop . $start . $info . $annotation . $cc . $style . $link . $react . $stop . $sweetspot . $disablekb . $ratio . $autohide . $controls . $profile . $list_style . $audio . $template . $hd . $color . $theme . $https . print_r( $general, true ) . print_r( $options, true ) . print_r( $list, true ) ) );
  119. // Try and get the output from cache. If it exists, return the code
  120. if ( ( $general[ 'embed_cache' ] != 0 ) && ( !is_feed() ) ) {
  121. $result = get_transient( $cache_key );
  122. if ( $result !== false) { return $result; }
  123. }
  124. $metadata = $general[ 'metadata' ];
  125. // Work out correct protocol to use - HTTP or HTTPS
  126. if ( $https == '' ) { $https = $options[ 'https' ]; }
  127. if ( $https == 1 ) { $https = 's'; } else { $https = ''; }
  128. // If this is a feed then display a thumbnail and/or text link to the original video
  129. if ( is_feed () ) {
  130. $result = '';
  131. if ( $playlist_ids != '' ) {
  132. $result .= '<p>'.__('A video list cannot be viewed within this feed - please view the original content').".</p>\n";
  133. } else {
  134. $youtube_url = 'http' . $https . '://www.youtube.com/watch?' . $embed_type . '=' . $id;
  135. if ( ( $embed_type == 'v' ) && ( $general[ 'feed' ] != 't' ) ) { $result .= '<p><a href="' . $youtube_url . '"><img src="http://img.youtube.com/vi/' . $id . '/' . $general[ 'thumbnail' ] . ".jpg\"></a></p>\n"; }
  136. if ( ( $general ['feed'] != 'v' ) or ( $embed_type != 'v' ) ) { $result .= '<p><a href="' . $youtube_url . '">' . __( 'Click here</a> to view the video on YouTube' ) . ".</p>\n"; }
  137. }
  138. return $result;
  139. }
  140. // Only set width and height from defaults if both are missing
  141. if ( ( $width == '' ) && ( $height == '' ) ) {
  142. $width = $options[ 'width' ];
  143. $height = $options[ 'height' ];
  144. }
  145. // If values have not been pressed, use the default values
  146. if ( $fullscreen == '' ) { $fullscreen = $options[ 'fullscreen' ]; }
  147. if ( $related == '' ) { $related = $options[ 'related' ]; }
  148. if ( $autoplay == '' ) { $autoplay = $options[ 'autoplay' ]; }
  149. if ( $loop == '' ) { $loop = $options[ 'loop' ]; }
  150. if ( $info == '' ) { $info = $options[ 'info' ]; }
  151. if ( $annotation == '' ) { $annotation = $options[ 'annotation' ]; }
  152. if ( $cc == '' ) { $cc = $options[ 'cc' ]; }
  153. if ( $link == '' ) { $link = $options[ 'link' ]; }
  154. if ( $react == '' ) { $react = $options[ 'react' ]; }
  155. if ( $sweetspot == '' ) { $sweetspot = $options[ 'sweetspot' ]; }
  156. if ( $disablekb == '' ) { $disablekb = $options[ 'disablekb' ]; }
  157. if ( $autohide == '' ) { $autohide = $options[ 'autohide' ]; }
  158. if ( $controls == '' ) { $controls = $options[ 'controls' ]; }
  159. if ( $audio == '' ) { $audio = $options[ 'audio' ]; }
  160. if ( $hd == '' ) { $hd = $options[ 'hd' ]; }
  161. if ( $style == '' ) { $style = $options[ 'style' ]; }
  162. if ( $color == '' ) { $color = $options[ 'color' ]; }
  163. if ( $theme == '' ) { $theme = $options[ 'theme' ]; }
  164. $wmode = $options[ 'wmode' ];
  165. $shadow = $options[ 'shadow' ];
  166. // Build the required template
  167. if ( $template == '' ) { $template = $options[ 'template' ]; } else { $template = ye_decode( $template ); }
  168. if ( strpos( $template, '%video%' ) === false ) { $template = '%video%'; }
  169. // If a multi-play list has been specified and EmbedPlus selected, use fallback embedding method instead
  170. if ( ( $playlist_ids != '' ) && ( $type == 'm' ) && ( $list_style != 'single' ) ) { $type = $options[ 'fallback' ]; }
  171. // If looping and no playlist has been generated, add the current ID
  172. // This is a workaround for the AS3 player which won't otherwise loop
  173. if ( ( $loop == 1 ) && ( $embed_type != 'p' ) && ( $playlist_ids == '' ) ) { $playlist_ids = $id; }
  174. // If no type was specified, depending on whether this is a video or playlist, set the specific default
  175. if ( $type == '' ) {
  176. if ( $embed_type == 'v' ) {
  177. $type = $options[ 'type' ];
  178. } else {
  179. $type = $options[ 'playlist' ];
  180. }
  181. }
  182. // Set parameters without default values
  183. if ( $start == '' ) { $start = '0'; }
  184. if ( $stop == '' ) { $stop = '0'; }
  185. // If height or width is missing, calculate missing parameter using ratio
  186. if ( ( ( $width == '' ) or ( $height == '' ) ) && ( ( $width != '' ) or ( $height != '' ) ) ) {
  187. $ratio_to_use = '';
  188. if ( $ratio != '' ) {
  189. // Extract the ratio from the provided string
  190. $pos = strpos( $ratio, ':', 0 );
  191. if ( $pos !== false ) {
  192. $ratio_l = substr( $ratio, 0, $pos );
  193. $ratio_r = substr( $ratio, $pos + 1 );
  194. if ( ( is_numeric( $ratio_l ) ) && ( is_numeric( $ratio_r ) ) ) { $ratio_to_use = $ratio_l / $ratio_r; }
  195. }
  196. }
  197. // If no, or invalid, ratio supplied, calculate from the default video dimensions
  198. if ( $ratio_to_use == '' ) { $ratio_to_use = $options[ 'width' ] / $options[ 'height' ]; }
  199. // Complete the missing width or height using the ratio
  200. if ( $width == '' ) { $width = round( $height * $ratio_to_use, 0); }
  201. if ( $height == '' ) { $height = round( $width / $ratio_to_use, 0); }
  202. }
  203. // If audio playback option is set, restrict the height to just show the player toolbar
  204. if ( $audio == '1' ) { $height = 27; }
  205. // Set up embed types
  206. $tab = '';
  207. $class = 'youtube-player';
  208. $paras = 'version=3&amp;modestbranding=1';
  209. $embedplus = false;
  210. $swf = false;
  211. $iframe = false;
  212. $chromeless = false;
  213. if ( $type != 'v' ) {
  214. if ( $type == 'm' ) {
  215. $embedplus = true;
  216. $tab = "\t";
  217. $embedheight = $height + 32;
  218. $class = 'cantembedplus';
  219. $fallback = $options[ 'fallback' ];
  220. } else {
  221. if ( $type == "c" ) {
  222. $chromeless = true;
  223. } else {
  224. $swf = true;
  225. }
  226. }
  227. } else {
  228. $iframe = true;
  229. }
  230. // Generate parameters to add to URL
  231. if ( $fullscreen == 1 ) { $paras .= '&amp;fs=1'; } else { $paras .= '&amp;fs=0'; }
  232. if ( $related != 1 ) { $paras .= '&amp;rel=0'; }
  233. if ( $autoplay == 1 ) { $paras .= '&amp;autoplay=1'; $paras_ep .= '&amp;autoplay=1'; }
  234. if ( $loop == 1 ) { $paras .= '&amp;loop=1'; }
  235. if ( $info == 1 ) { $paras .= '&amp;showinfo=1'; }
  236. if ( $annotation != 1 ) { $paras .= '&amp;iv_load_policy=3'; }
  237. if ( $cc == 1 ) { $paras .= '&amp;cc_load_policy=1'; }
  238. if ( $disablekb == 1 ) { $paras .= '&amp;disablekb=1'; }
  239. if ( $autohide != 2 ) { $paras .= '&amp;autohide=' . $autohide; }
  240. if ( $controls != 1 ) { $paras .= '&amp;controls=0'; }
  241. if ( strtolower( $color ) != 'red' ) { $paras .= '&amp;color=' . strtolower( $color ); }
  242. if ( strtolower( $theme ) != 'dark' ) { $paras .= '&amp;theme=' . strtolower( $theme ); }
  243. // If not a playlist, add the playlist parameter
  244. if ( $playlist_ids != '' ) { $paras .= '&amp;playlist=' . $playlist_ids; }
  245. // Generate EmbedPlus parameters
  246. $paras_ep = '&amp;width=' . $width . '&amp;height=' . $height;
  247. if ( $react != 1 ) { $paras_ep .= '&amp;react=0'; }
  248. if ( $sweetspot != 1 ) { $paras_ep .= '&amp;sweetspot=0'; }
  249. if ( $hd == 1 ) { $paras_ep .= '&amp;hd=1'; }
  250. // Add start & stop parameters
  251. if ( $start != 0 ) { $paras .= '&amp;start=' . $start; $paras_ep .= '&amp;start=' . $start; }
  252. if ( $stop != 0 ) { $paras_ep .= '&amp;stop=' . $stop; }
  253. $result = '';
  254. // Add EmbedPlus code
  255. if ( $embedplus ) {
  256. $result .= "<object type=\"application/x-shockwave-flash\" width=\"" . $width . "\" height=\"" . $embedheight . "\" data=\"http://getembedplus.com/embedplus.swf\" style=\"" . $style . "\" id=\"" . uniqid( 'ep_', true ) . "\">\n";
  257. $result .= "\t<param value=\"http://getembedplus.com/embedplus.swf\" name=\"movie\" />\n";
  258. $result .= "\t<param value=\"high\" name=\"quality\" />\n";
  259. $result .= "\t<param value=\"" . $wmode . "\" name=\"wmode\" />\n";
  260. $result .= "\t<param value=\"always\" name=\"allowscriptaccess\" />\n";
  261. if ( $fullscreen == 1 ) { $result .= "\t<param name=\"allowFullScreen\" value=\"true\" />\n"; }
  262. $result .= "\t<param name=\"flashvars\" value=\"ytid=" . $id . $paras_ep . "\" />\n";
  263. }
  264. // Add AS3 YouTube embed code
  265. if ( ( $swf ) or ( $chromeless ) or ( ( $embedplus ) && ( ( $fallback == 'o' ) or ( $fallback == 'p' ) ) ) ) {
  266. if ( $chromeless ) {
  267. $url_start = 'http' . $https . '://www.youtube.com/apiplayer?video_id=' . $id . '&';
  268. } else {
  269. $url_start = 'http' . $https . '://www.youtube.com/' . $embed_type . '/' . $id . '?';
  270. }
  271. $result .= $tab . "<object class=\"" . $class . "\" type=\"application/x-shockwave-flash\" data=\"" . $url_start . $paras . "\" width=\"" . $width . "\" height=\"" . $height . "\" style=\"" . $style . "\"";
  272. if ( $metadata != 0 ) { $result .= " rel=\"media:video\" resource=\"http" . $https . "://www.youtube.com/" . $embed_type . "/" . $id ."\" xmlns:media=\"http://search.yahoo.com/searchmonkey/media/\""; }
  273. $result .= ">\n";
  274. if ( $metadata != 0 ) { $result .= $tab . "\t<a rel=\"media:thumbnail\" href=\"http://img.youtube.com/vi/" . $id . "/default.jpg\" />\n"; }
  275. $result .= $tab . "\t<param name=\"movie\" value=\"http" . $https . "://www.youtube.com/" . $embed_type . "/" . $id . "?" . $paras . "\" />\n";
  276. $result .= $tab . "\t<param name=\"wmode\" value=\"" . $wmode . "\" />\n";
  277. if ( $fullscreen == 1 ) { $result .= $tab . "\t<param name=\"allowFullScreen\" value=\"true\" />\n"; }
  278. if ( ( $link != 1 ) && ( $link != '' ) ) { $result .= $tab . "\t<param name=\"allowNetworking\" value=\"internal\" />\n"; }
  279. if ( ( $metadata != 0 ) && ( $title != '' ) ) { $result .= $tab . "\t<span property=\"media:title\" content=\"" . htmlentities( $title ) . "\" />\n"; }
  280. $result .= $tab . "</object>\n";
  281. }
  282. // Add IFRAME embed code
  283. if ( ( $iframe ) or ( ( $embedplus ) && ( $fallback == "v" ) ) ) {
  284. if ( $embed_type == "p" ) { $playlist_para = "p/"; } else { $playlist_para = ''; }
  285. $result .= $tab . '<iframe style="border: 0;' . $style . '" class="' . $class . '" width="' . $width . '" height="' . $height . '" src="http' . $https . '://www.youtube.com/embed/' . $playlist_para . $id . '?' . $paras . '&amp;wmode=' . $wmode . '"';
  286. if ( $fullscreen == 1 ) { $result .= ' allowfullscreen="allowfullscreen"'; }
  287. $result .= "></iframe>\n";
  288. }
  289. // If using EmbedPlus, add the OBJECT closure tag
  290. if ( $embedplus ) { $result .= "</object>\n<!--[if lte IE 6]> <style type=\"text/css\">.cantembedplus{display:none;}</style><![endif]-->\n"; }
  291. // Add drop shadow
  292. if ( ( is_numeric( $shadow ) ) && ( $shadow != 0 ) ) { $result .= "<br/><img src=\"" . plugins_url() . "/youtube-embed/images/shadow.png\" alt=\"\" title=\"\" height=\"" . $shadow . "\" width=\"" . $width . "\" />\n"; }
  293. // Now apply the template to the result
  294. $result = str_replace( '%video%', $result, $template );
  295. // Now add a commented header and trailer
  296. $result = "\n<!-- YouTube Embed v" . youtube_embed_version . " | http://www.artiss.co.uk/artiss-youtube-embed -->\n" . $result;
  297. $result .= "<!-- End of YouTube Embed code -->\n";
  298. // Cache the output
  299. if ( $general[ 'embed_cache' ] != 0 ) { set_transient( $cache_key, $result, 3600 * $general[ 'embed_cache' ] ); }
  300. return $result;
  301. }
  302. /**
  303. * Validate a supplied profile name
  304. *
  305. * Returns a profile number for a supplied name
  306. *
  307. * @since 2.0
  308. *
  309. * @param string $name The name of the profile to find
  310. * @param string $number The number of profiles available
  311. * @return string The profile number (defaults to 0)
  312. */
  313. function ye_validate_profile( $name, $number ) {
  314. $profile = 0;
  315. $name = strtolower( $name );
  316. if ( ( $name != '' ) && ( $name != 'default' ) ) {
  317. // Loop around, fetching in profile names
  318. $loop = 1;
  319. while ( ( $loop <= $number ) && ( $profile == 0 ) ) {
  320. if ( ( $name == $loop ) or ( $name == 'Profile ' . $loop ) ) {
  321. $profile = $loop;
  322. } else {
  323. $profiles = get_option( 'youtube_embed_profile' . $loop );
  324. $profname = strtolower( $profiles[ 'name' ] );
  325. if ( $profname == $name ) { $profile = $loop; }
  326. }
  327. $loop ++;
  328. }
  329. }
  330. return $profile;
  331. }
  332. /**
  333. * Validate a supplied list name
  334. *
  335. * Returns a list for a supplied list number or name name - blank if not a valid list
  336. *
  337. * @since 2.0
  338. *
  339. * @param string $name The name of the list to find
  340. * @param string $number The number of lists available
  341. * @return string The list (defaults to blank)
  342. */
  343. function ye_validate_list( $name, $number ) {
  344. $list = '';
  345. // If the parameter contains commas, assume to be a comma seperated list and move into an array
  346. if ( strpos( $name, ',' ) !== false ) {
  347. $list = explode( ',', $name );
  348. } else {
  349. // No comma, so check if this is a named list
  350. $name = strtolower( $name );
  351. if ( $name != '' ) {
  352. // Loop around, fetching in profile names
  353. $loop = 1;
  354. while ( ( $loop <= $number ) && ( $list == '' ) ) {
  355. $listfiles = get_option( 'youtube_embed_list' . $loop );
  356. if ( ( $name == strval( $loop ) ) or ( $name == 'List ' . $loop ) ) {
  357. $list = $listfiles[ 'list' ];
  358. } else {
  359. $listname = strtolower( $listfiles[ 'name' ] );
  360. if ( $listname == $name ) { $list = $listfiles[ 'list' ]; }
  361. }
  362. $loop ++;
  363. }
  364. }
  365. if ( $list != '' ) { $list = explode( "\n", $list ); }
  366. }
  367. return $list;
  368. }
  369. /**
  370. * Get URL parameters
  371. *
  372. * Extract a requested parameter from a URL
  373. *
  374. * @since 2.0
  375. *
  376. * @param string $id The ID of the video
  377. * @param string $para The parameter to extract
  378. * @param string $current The current parameter value
  379. * @return string The parameter value
  380. */
  381. function ye_get_url_para( $id, $para, $current ) {
  382. // Look for an ampersand
  383. $start_pos = false;
  384. if ( strpos( $id, '&amp;' . $para . '=' ) !== false ) { $start_pos = strpos( $id, '&amp;' . $para . '=' ) + 6 + strlen( $para ); }
  385. // If a parameter was found, look for the end of it
  386. if ( $start_pos !== false ) {
  387. $end_pos = strpos( $id, '&amp;', $start_pos + 1 );
  388. if ( !$end_pos ) { $end_pos = strlen( $id ); }
  389. // Extract the parameter and return it
  390. $current = substr( $id, $start_pos, $end_pos - $start_pos );
  391. }
  392. return $current;
  393. }
  394. /**
  395. * Decode a string
  396. *
  397. * Decode an HTML encoded string. I'm not using htmlspecialchars_decode to maintain
  398. * PHP 4 compatibility.
  399. *
  400. * @since 2.0.3
  401. *
  402. * @param string $encoded The encoded string
  403. * @return string The decoded string
  404. */
  405. function ye_decode( $encoded ) {
  406. $find = array( '&amp;', '&quot;', '&#039;', '&lt;', '&gt;' );
  407. $replace = array( '&', '"', "'", '<', '>' );
  408. $decoded = str_replace($find,$replace,$encoded);
  409. return $decoded;
  410. }
  411. ?>