video_embed_field.handlers.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. <?php
  2. /**
  3. * Provide some handlers for video embed field
  4. * Other modules can implement the hook_video_embed_handler_info to provide more handlers
  5. */
  6. /**
  7. * Implementation of hook_video_embed_handler_info
  8. */
  9. function video_embed_field_video_embed_handler_info() {
  10. $handlers = array();
  11. $handlers['youtube'] = array(
  12. 'title' => 'Youtube',
  13. 'function' => 'video_embed_field_handle_youtube',
  14. 'thumbnail_function' => 'video_embed_field_handle_youtube_thumbnail',
  15. 'data_function' => 'video_embed_field_handle_youtube_data',
  16. 'form' => 'video_embed_field_handler_youtube_form',
  17. 'domains' => array(
  18. 'youtube.com',
  19. 'youtu.be'
  20. ),
  21. 'defaults' => array(
  22. 'width' => '640px',
  23. 'height' => '360px',
  24. 'autoplay' => 0,
  25. 'hd' => 1,
  26. 'rel' => 0,
  27. 'autohide' => 2,
  28. 'showinfo' => 1,
  29. 'modestbranding' => 0,
  30. 'theme' => 'dark',
  31. 'iv_load_policy' => 1,
  32. ),
  33. );
  34. $handlers['vimeo'] = array(
  35. 'title' => 'Vimeo',
  36. 'function' => 'video_embed_field_handle_vimeo',
  37. 'thumbnail_function' => 'video_embed_field_handle_vimeo_thumbnail',
  38. 'data_function' => 'video_embed_field_handle_vimeo_data',
  39. 'form' => 'video_embed_field_handler_vimeo_form',
  40. 'domains' => array(
  41. 'vimeo.com'
  42. ),
  43. 'defaults' => array(
  44. 'width' => '640px',
  45. 'height' => '360px',
  46. 'color' => '00adef',
  47. 'portrait' => 1,
  48. 'title' => 1,
  49. 'byline' => 1,
  50. 'autoplay' => 0,
  51. 'loop' => 0,
  52. ),
  53. );
  54. return $handlers;
  55. }
  56. /**
  57. * Helper function to get the youtube video's id
  58. * Returns false if it doesn't parse for wahtever reason
  59. */
  60. function _video_embed_field_get_youtube_id($url){
  61. // Find the ID of the video they want to play from the url
  62. if (stristr($url, 'http://')) {
  63. $url = substr($url, 7);
  64. }
  65. else if (stristr($url, 'https://')) {
  66. $url = substr($url, 8);
  67. }
  68. if(stristr($url, 'playlist')){
  69. //Playlists need the appended ampersand to take the options properly.
  70. $url = $url . '&';
  71. $pos = strripos($url, '?list=');
  72. if($pos!==FALSE){
  73. $pos2 = stripos($url, '&');
  74. $pos2++;
  75. }
  76. else{
  77. return FALSE;
  78. }
  79. }
  80. //Alternate playlist link
  81. else if(stristr($url, 'view_play_list')){
  82. $url = $url . '&';
  83. //All playlist ID's are prepended with PL. view_play_list links allow you to not have that, though.
  84. if(!stristr($url, '?p=PL')){
  85. $url = substr_replace($url, 'PL', strpos($url, '?p=') + 3, 0);
  86. }
  87. //Replace the links format with the embed format
  88. $url = str_ireplace('play_list?p=', 'videoseries?list=', $url);
  89. $pos = strripos($url, 'videoseries?list=');
  90. if($pos !== FALSE){
  91. $pos2 = stripos($url, '&');
  92. $pos2++;
  93. }
  94. else{
  95. return FALSE;
  96. }
  97. }
  98. else{
  99. $pos = strripos($url, 'v=');
  100. if ($pos!== FALSE) {
  101. $pos += 2;
  102. $pos2 = stripos($url, '&', $pos);
  103. $pos_hash = stripos($url, '#', $pos);
  104. $pos2 = _video_embed_get_min($pos2, $pos_hash);
  105. }
  106. else {
  107. $pos = strripos($url, '/');
  108. if ($pos !== FALSE) {
  109. $pos++;
  110. $pos2 = stripos($url, '?', $pos);
  111. $pos_hash = stripos($url, '#', $pos);
  112. $pos2 = _video_embed_get_min($pos2, $pos_hash);
  113. }
  114. }
  115. }
  116. if ($pos === FALSE) {
  117. return FALSE;
  118. }
  119. else {
  120. if ($pos2 > 0) {
  121. $id = substr($url, $pos, $pos2 - $pos);
  122. }
  123. else {
  124. $id = substr($url, $pos);
  125. }
  126. }
  127. return $id;
  128. }
  129. /**
  130. * Handler for Youtube videos.
  131. */
  132. function video_embed_field_handle_youtube($url, $settings) {
  133. $output = array();
  134. //Grab the minutes and seconds, and just convert it down to seconds
  135. preg_match('/#t=((?P<min>\d+)m)?((?P<sec>\d+)s)?/', $url, $matches);
  136. //Give it some default data in case there is no #t=...
  137. $matches += array(
  138. "min" => 0,
  139. "sec" => 0,
  140. );
  141. $time = ($matches["min"] * 60) + $matches["sec"];
  142. $settings['start'] = $time;
  143. $id = _video_embed_field_get_youtube_id($url);
  144. if (!$id) {
  145. // We can't decode the URL - just return the URL as a link
  146. $output['#markup'] = l($url, $url);
  147. return $output;
  148. }
  149. // Construct the embed code
  150. $settings['wmode'] = 'opaque';
  151. $settings_str = _video_embed_code_get_settings_str($settings);
  152. $output['#markup'] = '<iframe width="' . $settings['width'] . '" height="' . $settings['height'] . '" src="//www.youtube.com/embed/' . $id . '?' . $settings_str . '" frameborder="0" allowfullscreen></iframe>';
  153. return $output;
  154. }
  155. /**
  156. * Get the thumbnail url for youtube videos
  157. */
  158. function video_embed_field_handle_youtube_thumbnail($video_url){
  159. $info = array();
  160. $id = _video_embed_field_get_youtube_id($video_url);
  161. //Playlist
  162. if(stristr($id, '?list=')){
  163. //Strip out all but the ID, including the PL behind the ID.
  164. $start = strpos($id, '?list=PL') + 8;
  165. $length = strpos($id, '&') - $start;
  166. $id = substr($id, $start, $length);
  167. $info['id'] = $id;
  168. //Playlist info is stored in XML. The thumbnail is in there.
  169. $xml = drupal_http_request('http://gdata.youtube.com/feeds/api/playlists/' . $id);
  170. if(!isset($xml->error)){
  171. $xml = new SimpleXMLElement($xml->data);
  172. $media = $xml->children('http://search.yahoo.com/mrss/');
  173. if($media->group->thumbnail && $media->group->thumbnail[0]->attributes()){
  174. $attrs = $media->group->thumbnail[0]->attributes();
  175. $info['url'] = (string)$attrs['url'];
  176. }
  177. }
  178. }
  179. //Regular video
  180. else if($id) {
  181. $info['id'] = $id;
  182. $info['url'] = 'http://img.youtube.com/vi/'.$id.'/0.jpg';
  183. }
  184. return $info;
  185. }
  186. /**
  187. * Get video data for a YouTube video URL
  188. *
  189. * @param string $url
  190. * A YouTube video URL to get data for
  191. *
  192. * @return array|false $data
  193. * An array of video data, or FALSE if unable to fetch data
  194. */
  195. function video_embed_field_handle_youtube_data($url) {
  196. $data = array();
  197. // Get YouTube video ID from URL
  198. $id = _video_embed_field_get_youtube_id($url);
  199. if ($id) {
  200. $response = drupal_http_request('http://gdata.youtube.com/feeds/api/videos/' . $id . '?v=2&alt=json');
  201. if (!isset($response->error)) {
  202. $data = json_decode($response->data);
  203. $data = (array) $data->entry;
  204. return _video_embed_field_clean_up_youtube_data($data);
  205. }
  206. }
  207. return FALSE;
  208. }
  209. /**
  210. * Flatten out some unnecessary nesting in the youtube data
  211. */
  212. function _video_embed_field_clean_up_youtube_data($data) {
  213. //make things a bit nicer for people trying to use the data
  214. foreach ($data as $key => $value) {
  215. if (is_object($value)) {
  216. $temp = (array)$value;
  217. if (isset($temp['$t'])) {
  218. $data[$key] = $temp['$t'];
  219. } else {
  220. $data[$key] = _video_embed_field_clean_up_youtube_data($temp);
  221. }
  222. } else if (is_array($value)) {
  223. $data[$key] = _video_embed_field_clean_up_youtube_data($value);
  224. }
  225. if($key === 'category') {
  226. $terms = array();
  227. foreach ($data[$key] as $value) {
  228. if(isset($value['scheme']) && $value['scheme'] == 'http://schemas.google.com/g/2005#kind') {
  229. continue;
  230. }
  231. if(isset($value['term'])) {
  232. $terms[] = $value['term'];
  233. }
  234. }
  235. $data['terms'] = $terms;
  236. }
  237. }
  238. return $data;
  239. }
  240. /**
  241. * Defines the form elements for the Youtube configuration form.
  242. */
  243. function video_embed_field_handler_youtube_form($defaults) {
  244. $form = array();
  245. $form['width'] = array(
  246. '#type' => 'textfield',
  247. '#size' => '5',
  248. '#title' => t('Player Width'),
  249. '#description' => t('The width of the youtube player.'),
  250. '#default_value' => $defaults['width'],
  251. );
  252. $form['height'] = array(
  253. '#type' => 'textfield',
  254. '#size' => '5',
  255. '#title' => t('Player Height'),
  256. '#description' => t('The height of the youtube player.'),
  257. '#default_value' => $defaults['height'],
  258. );
  259. $form['theme'] = array(
  260. '#type' => 'select',
  261. '#options' => array(
  262. 'dark' => t('Dark'),
  263. 'light' => t('Light'),
  264. ),
  265. '#title' => t('Player theme'),
  266. '#default_value' => $defaults['theme'],
  267. );
  268. $form['autoplay'] = array(
  269. '#type' => 'checkbox',
  270. '#title' => t('Autoplay'),
  271. '#description' => t('Play the video immediately.'),
  272. '#default_value' => $defaults['autoplay'],
  273. );
  274. $form['hd'] = array(
  275. '#type' => 'checkbox',
  276. '#title' => t('Use HD'),
  277. '#description' => t('Attempt to play the video in HD if available.'),
  278. '#default_value' => $defaults['hd'],
  279. );
  280. $form['rel'] = array(
  281. '#type' => 'checkbox',
  282. '#title' => t('Show related videos'),
  283. '#description' => t('Show related videos after the video is finished playing.'),
  284. '#default_value' => $defaults['rel'],
  285. );
  286. $form['showinfo'] = array(
  287. '#type' => 'checkbox',
  288. '#title' => t('Show info'),
  289. '#description' => t('Display information like the video title and rating before the video starts playing.'),
  290. '#default_value' => $defaults['showinfo'],
  291. );
  292. $form['modestbranding'] = array(
  293. '#type' => 'checkbox',
  294. '#title' => t('Hide Youtube logo'),
  295. '#description' => t('Hide the Youtube logo button on the player'),
  296. '#default_value' => $defaults['modestbranding']
  297. );
  298. $form['iv_load_policy'] = array(
  299. '#type' => 'radios',
  300. '#options' => array(
  301. 1 => t('Show video annotations.'),
  302. 3 => t('Hide video annotations.'),
  303. ),
  304. '#title' => t('Display annotations'),
  305. '#description' => t('Controls the display of annotations over the video content. Only works when using the flash player.'),
  306. '#default_value' => $defaults['iv_load_policy'],
  307. );
  308. $form['autohide'] = array(
  309. '#type' => 'radios',
  310. '#options' => array(
  311. 0 => t('The video progress bar and player controls will be visible throughout the video.'),
  312. 1 => t('Automatically slide the video progress bar and the player controls out of view a couple of seconds after the video starts playing. They will only reappear if the user moves her mouse over the video player or presses a keyboard key.'),
  313. 2 => t('The video progress bar will fade out but the player controls (play button, volume control, etc.) remain visible.'),
  314. ),
  315. '#title' => t('Autohide progress bar and the player controls'),
  316. '#description' => t('Controls the autohide behavior of the youtube player controls.'),
  317. '#default_value' => $defaults['autohide'],
  318. );
  319. return $form;
  320. }
  321. /**
  322. * Helper function to get the Vimeo video's ID
  323. *
  324. * @param string $url
  325. * A Vimeo video URL to get the ID of
  326. *
  327. * @return integer|false $id
  328. * The video ID, or FALSE if unable to get the video ID
  329. */
  330. function _video_embed_field_get_vimeo_id($url){
  331. $pos = strripos($url, '/');
  332. if ($pos != FALSE) {
  333. $pos += 1;
  334. return (int) substr($url, $pos);
  335. }
  336. return FALSE;
  337. }
  338. /**
  339. * Handler for Vimeo videos.
  340. */
  341. function video_embed_field_handle_vimeo($url, $settings) {
  342. // Get ID of video from URL
  343. $id = _video_embed_field_get_vimeo_id($url);
  344. if (!$id) {
  345. return array(
  346. '#markup' => l($url, $url),
  347. );
  348. }
  349. // Construct the embed code
  350. $settings['portrait'] = 0;
  351. $settings_str = _video_embed_code_get_settings_str($settings);
  352. return array(
  353. '#markup' => '<iframe width="' . $settings['width'] . '" height="' . $settings['height'] . '" src="//player.vimeo.com/video/' . $id .
  354. '?' . $settings_str . '" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowfullscreen></iframe>',
  355. );
  356. }
  357. /**
  358. * Get the thumbnail url for youtube videos
  359. */
  360. function video_embed_field_handle_vimeo_thumbnail($url){
  361. // Get ID of video from URL
  362. $id = _video_embed_field_get_vimeo_id($url);
  363. $info = array(
  364. 'id' => $id,
  365. );
  366. $response = drupal_http_request('http://vimeo.com/api/v2/video/' . $id . '.php');
  367. if (!isset($response->error)) {
  368. $response = unserialize($response->data);
  369. $video = current($response);
  370. $image_url = $video['thumbnail_large'];
  371. $info['url'] = $image_url;
  372. }
  373. return $info;
  374. }
  375. /**
  376. * Get video data for a Vimeo video URL
  377. *
  378. * @param string $url
  379. * A Vimeo video URL to get data for
  380. *
  381. * @return array|false $data
  382. * An array of video data, or FALSE if unable to fetch data
  383. */
  384. function video_embed_field_handle_vimeo_data($url) {
  385. // Get ID of video from URL
  386. $id = _video_embed_field_get_vimeo_id($url);
  387. if ($id) {
  388. $response = drupal_http_request('http://vimeo.com/api/v2/video/' . $id . '.php');
  389. if (!isset($response->error)) {
  390. $response = unserialize($response->data);
  391. return (array) current($response);
  392. }
  393. }
  394. return FALSE;
  395. }
  396. /**
  397. * Defines the form elements for the Vimeo configuration form.
  398. */
  399. function video_embed_field_handler_vimeo_form($defaults) {
  400. $form = array();
  401. $form['width'] = array(
  402. '#type' => 'textfield',
  403. '#size' => '5',
  404. '#title' => t('Player Width'),
  405. '#description' => t('The width of the vimeo player.'),
  406. '#default_value' => $defaults['width'],
  407. );
  408. $form['height'] = array(
  409. '#type' => 'textfield',
  410. '#size' => '5',
  411. '#title' => t('Player Height'),
  412. '#description' => t('The height of the vimeo player.'),
  413. '#default_value' => $defaults['height'],
  414. );
  415. $form['color'] = array(
  416. '#type' => 'select',
  417. '#options' => array(
  418. '00adef' => t('Blue'),
  419. 'ff9933' => t('Orange'),
  420. 'c9ff23' => t('Lime'),
  421. 'ff0179' => t('Fuschia'),
  422. 'ffffff' => t('White')
  423. ),
  424. '#title' => t('Player Color'),
  425. '#description' => t('The color to use on the vimeo player.'),
  426. '#default_value' => $defaults['color'],
  427. );
  428. $form['portrait'] = array(
  429. '#type' => 'checkbox',
  430. '#title' => t('Overlay Author Thumbnail'),
  431. '#description' => t('Overlay the author\'s thumbnail before the video is played.'),
  432. '#default_value' => $defaults['portrait'],
  433. );
  434. $form['title'] = array(
  435. '#type' => 'checkbox',
  436. '#title' => t('Overlay Video\'s Title'),
  437. '#description' => t('Overlay the video\'s title before the video is played.'),
  438. '#default_value' => $defaults['title'],
  439. );
  440. $form['byline'] = array(
  441. '#type' => 'checkbox',
  442. '#title' => t('Overlay Video\'s Byline'),
  443. '#description' => t('Overlay the video\'s description before the video is played.'),
  444. '#default_value' => $defaults['byline'],
  445. );
  446. $form['overridable'] = array (
  447. '#prefix' => '<p class="note"><strong>'.t('Note').': </strong><em>',
  448. '#markup' => t('Color, portrait, title and byline can be restricted by Vimeo Plus videos.
  449. Such videos will ignore these settings.'),
  450. '#suffix' => '</em></p>',
  451. );
  452. $form['autoplay'] = array(
  453. '#type' => 'checkbox',
  454. '#title' => t('Autoplay'),
  455. '#description' => t('Play the video immediately.'),
  456. '#default_value' => $defaults['autoplay'],
  457. );
  458. $form['loop'] = array(
  459. '#type' => 'checkbox',
  460. '#title' => t('Loop'),
  461. '#description' => t('Loop the video\'s playback'),
  462. '#default_value' => $defaults['loop'],
  463. );
  464. return $form;
  465. }
  466. /**
  467. * Calculate the min index for use in finding the id of a youtube video
  468. */
  469. function _video_embed_get_min($pos1, $pos2) {
  470. if(!$pos1) {
  471. return $pos2;
  472. } else if(!$pos2) {
  473. return $pos1;
  474. } else {
  475. return min($pos1, $pos2);
  476. }
  477. }