diff --git a/plugins/link.inc b/plugins/link.inc index 1241367..c7fe8fd 100644 --- a/plugins/link.inc +++ b/plugins/link.inc @@ -120,38 +120,21 @@ function fbsmp_link_fbsmp_admin_settings_form(&$form_state) { '#description' => t('Select the maximum amount of time to take while attaching a link.'), ); - if (module_exists('emvideo')) { - module_load_include('inc', 'fbsmp', 'plugins/link.emvideo'); - $form['emvideo'] = array( - '#type' => 'fieldset', - '#title' => t('Emvideo integration'), - '#collapsible' => TRUE, - '#collapsed' => TRUE, - ); - $form['emvideo']['enable_emvideo'] = array( - '#type' => 'checkbox', - '#default_value' => $prev_settings['enable_emvideo'], - '#title' => t('Enable emvideo integration'), - '#description' => t('Enabling this will show the embedded video along with the status, if the shared link is from the list of the supported video providers.') - ); - $form['emvideo'] += _fbsmp_link_emvideo_settings_form($form_state, $prev_settings); - } - - if (module_exists('emaudio')) { - module_load_include('inc', 'fbsmp', 'plugins/link.emaudio'); - $form['emaudio'] = array( - '#type' => 'fieldset', - '#title' => t('Emaudio integration'), - '#collapsible' => TRUE, - '#collapsed' => TRUE, - ); - $form['emaudio']['enable_emaudio'] = array( - '#type' => 'checkbox', - '#default_value' => $prev_settings['enable_emaudio'], - '#title' => t('Enable emaudio integration'), - '#description' => t('Enabling this will show the embedded audio along with the status, if the shared link is from the list of the supported audio providers.') - ); - $form['emaudio'] += _fbsmp_link_emaudio_settings_form($form_state, $prev_settings); + if (module_exists('media')) { + module_load_include('inc', 'fbsmp', 'plugins/link.media'); + $form['media'] = array( + '#type' => 'fieldset', + '#title' => t('Media integration'), + '#collapsible' => TRUE, + '#collapsed' => TRUE, + ); + $form['media']['enable_media'] = array( + '#type' => 'checkbox', + '#default_value' => $prev_settings['enable_media'], + '#title' => t('Enable Media integration'), + '#description' => t('Enabling this will show the embedded video along with the status, if the shared link is from the list of the supported video providers.') + ); + $form['media'] += _fbsmp_link_media_settings_form($form_state, $prev_settings); } return $form; @@ -183,14 +166,9 @@ function fbsmp_link_fbsmp_admin_settings_form_validate(&$form, &$form_state) { form_set_error('min_imagesize', t('The "Minimum image size" option must contain a valid value. You can either leave the text field empty or enter a string like "512" (bytes), "80K" (kilobytes) or "50M" (megabytes).')); } - if (module_exists('emvideo')) { - module_load_include('inc', 'fbsmp', 'plugins/link.emvideo'); - _fbsmp_link_emvideo_settings_form_validate($form, $form_state); - } - - if (module_exists('emaudio')) { - module_load_include('inc', 'fbsmp', 'plugins/link.emaudio'); - _fbsmp_link_emaudio_settings_form_validate($form, $form_state); + if (module_exists('media')) { + module_load_include('inc', 'fbsmp', 'plugins/link.media'); + _fbsmp_link_media_settings_form_validate($form, $form_state); } } @@ -214,16 +192,10 @@ function fbsmp_link_fbsmp_admin_settings_form_submit(&$form, &$form_state) { $settings['cache_duration'] = $form_values['cache_duration']; $settings['timeout'] = $form_values['timeout']; - if (module_exists('emvideo')) { - $settings['enable_emvideo'] = $form_values['enable_emvideo']; - module_load_include('inc', 'fbsmp', 'plugins/link.emvideo'); - $settings += _fbsmp_link_emvideo_settings_form_submit($form, $form_state); - } - - if (module_exists('emaudio')) { - $settings['enable_emaudio'] = $form_values['enable_emaudio']; - module_load_include('inc', 'fbsmp', 'plugins/link.emaudio'); - $settings += _fbsmp_link_emaudio_settings_form_submit($form, $form_state); + if (module_exists('media')) { + $settings['enable_media'] = $form_values['enable_media']; + module_load_include('inc', 'fbsmp', 'plugins/link.media'); + $settings += _fbsmp_link_media_settings_form_submit($form, $form_state); } fbsmp_save_plugin_settings($settings, 'link'); @@ -351,9 +323,8 @@ function fbsmp_link_fbsmp_repost_form(&$form_state, $attachment) { 'images' => array($attachment->data['thumbnail'] => 0), 'title' => $attachment->data['title'], 'description' => $attachment->data['description'], - 'emvideo_valid' => $attachment->data['emvideo_valid'], - 'emaudio_valid' => $attachment->data['emaudio_valid'], - 'emfield_data' => $attachment->data['emfield_data'], + 'media_valid' => $attachment->data['media_valid'], + 'media_data' => $attachment->data['media_data'], ); $form_state['fbsmp']['fbsmp_link']['data'] = $data; @@ -408,9 +379,8 @@ function fbsmp_link_fbsmp_widget_form_save($form, $form_state) { $delta = $form_state['values']['fbsmp_link_current_thumbnail']; if (!$form_state['values']['fbsmp_link_no_thumbnail']) { $record['thumbnail'] = $form_state['values']['fbsmp_link_thumbnail_' . $delta]; - $record['emvideo_valid'] = $form_state['values']['fbsmp_link_data']['data']['emvideo_valid']; - $record['emaudio_valid'] = $form_state['values']['fbsmp_link_data']['data']['emaudio_valid']; - $record['emfield_data'] = $form_state['values']['fbsmp_link_data']['data']['emfield_data']; + $record['media_valid'] = $form_state['values']['fbsmp_link_data']['data']['media_valid']; + $record['media_data'] = $form_state['values']['fbsmp_link_data']['data']['media_data']; } $record['title'] = trim($form_state['values']['fbsmp_link_title']); $record['description'] = $form_state['values']['fbsmp_link_description'] ? trim($form_state['values']['fbsmp_link_description']) : ""; @@ -502,13 +472,9 @@ function fbsmp_link_fbsmp_subtheme() { 'variables' => array('attachment' => NULL, 'settings' => NULL), 'file' => 'plugins/link.inc', ), - 'fbsmp_link_emvideo' => array( + 'fbsmp_link_media' => array( 'variables' => array('attachment' => NULL, 'settings' => NULL), - 'file' => 'plugins/link.emvideo.inc', - ), - 'fbsmp_link_emaudio' => array( - 'variables' => array('attachment' => NULL, 'settings' => NULL), - 'file' => 'plugins/link.emaudio.inc', + 'file' => 'plugins/link.media.inc', ), ); } @@ -555,7 +521,7 @@ function fbsmp_link_fbsmp_themed_attachment($attachment) { function theme_fbsmp_link_attachment($variables) { $attachment = $variables['attachment']; $settings = $variables['settings']; - + $attachment_data = $variables['attachment']->data; $output = theme('fbsmp_link_url', array('attachment' => $attachment, 'settings' => $settings)); if (isset($attachment_data['description']) && $attachment_data['description']) { @@ -581,10 +547,10 @@ function theme_fbsmp_link_attachment($variables) { function theme_fbsmp_link_thumbnail($variables) { $attachment = $variables['attachment']; $settings = $variables['settings']; - + $attachment_data = $attachment->data; $output = ''; - + $attributes = array(); $attributes['path'] = $attachment_data['thumbnail']; $attributes['title'] = $attachment_data['title']; @@ -595,7 +561,7 @@ function theme_fbsmp_link_thumbnail($variables) { $attributes[$attr] = $settings[$attr]; } } - + $attributes['attributes'] = array('class' => 'fbsmp-link-thumbnail'); $output .= theme('image', $attributes); } @@ -635,23 +601,20 @@ function theme_fbsmp_link_thumbnail($variables) { function theme_fbsmp_link_url($variables) { $attachment = $variables['attachment']; $settings = $variables['settings']; - + $attachment_data = $attachment->data; $output = ''; $emfield_output = ''; - if ($attachment_data['emvideo_valid']) { - if ($settings['emvideo_inline_video_replacement']) { + if ($attachment_data['media_valid']) { + if ($settings['media_video_inline_replacement']) { $settings['class'] .= ' fbsmp-link-emvideo-thumbnail-replacement'; } else { - $emfield_output = theme('fbsmp_link_emvideo', array('attachment' => $attachment, 'settings' => $settings)); + $emfield_output = theme('fbsmp_link_media', array('attachment' => $attachment, 'settings' => $settings)); } } - elseif ($attachment_data['emaudio_valid']) { - $emfield_output = theme('fbsmp_link_emaudio', array('attachment' => $attachment, 'settings' => $settings)); - } - if (!$attachment_data['emvideo_valid'] || ($attachment_data['emvideo_valid'] && $settings['emvideo_inline_video_replacement'])) { + if (!$attachment_data['media_valid'] || ($attachment_data['media_valid'] && $settings['media_video_inline_replacement'])) { $output = theme('fbsmp_link_thumbnail', array('attachment' => $attachment, 'settings' => $settings)); } @@ -671,12 +634,12 @@ function theme_fbsmp_link_url($variables) { $options['attributes']['rel'] = 'nofollow'; } - if ($attachment_data['emvideo_valid'] && $settings['emvideo_inline_video_replacement']) { - $attachment_data['page_url'] = 'fbsmp/link/emvideo/' . $attachment->sid; + if ($attachment_data['media_valid'] && $settings['media_video_inline_replacement']) { + $attachment_data['page_url'] = 'fbsmp/link/media/' . $attachment->sid; static $added_js; if (!isset($added_js)) { - drupal_add_js(drupal_get_path('module', 'fbsmp') . '/plugins/link.emvideo.thumbnail-replacement.js'); - drupal_add_css(drupal_get_path('module', 'fbsmp') . '/plugins/link.emvideo.thumbnail-replacement.css'); + drupal_add_js(drupal_get_path('module', 'fbsmp') . '/plugins/link.media.thumbnail-replacement.js'); + drupal_add_css(drupal_get_path('module', 'fbsmp') . '/plugins/link.media.thumbnail-replacement.css'); $added_js = TRUE; } } @@ -698,11 +661,11 @@ function fbsmp_link_render_form_thumbnail($image_url, $settings = array()) { $variables[$attr] = $settings[$attr]; } } - + $variables['path'] = $image_url; $attributes['class'] = "fbsmp-link-thumbnail-select"; $variables['attributes'] = $attributes; - + $output .= theme('image', $variables); $output .= ''; return $output; @@ -850,42 +813,30 @@ function fbsmp_link_request_contents($url, $options = array()) { 'title' => $url, 'description' => '', ); - $emfield = array( - 'emvideo_valid' => FALSE, - 'emaudio_valid' => FALSE, - 'emfield_data' => array(), + $media = array( + 'media_valid' => FALSE, + 'media_data' => array(), 'images' => array(), ); //Check if the URL is from the supported video providers. We do not cache this content. - if (module_exists('emvideo') && $options['enable_emvideo']) { - module_load_include('inc', 'fbsmp', 'plugins/link.emvideo'); - $item = _fbsmp_link_emvideo_data($url, $options); - if (is_array($item) && $item) { - $emfield['emvideo_valid'] = TRUE; - $emfield['images'] = empty($item['image']) ? array() : array($item['image'] => 0); - $emfield['emfield_data'] = $item; - } - } - - //Check if the URL is from the supported audio providers. We do not cache this content. - if (!$emfield['emvideo_valid'] && module_exists('emaudio') && $options['enable_emaudio']) { - module_load_include('inc', 'fbsmp', 'plugins/link.emaudio'); - $item = _fbsmp_link_emaudio_data($url, $options); - if (is_array($item) && $item) { - $emfield['emaudio_valid'] = TRUE; - $emfield['images'] = empty($item['image']) ? array() : array($item['image'] => 0); - $emfield['emfield_data'] = $item; - } + if (module_exists('media') && $options['enable_media']) { + module_load_include('inc', 'fbsmp', 'plugins/link.media'); + $item = _fbsmp_link_media_data($url, $options); + if (is_array($item) && $item) { + $media['media_valid'] = TRUE; + $media['images'] = empty($item['image']) ? array() : array($item['image'] => 0); + $media['media_data'] = $item; + } } //If it is a cachable request, try to load a cached value. if ($options['cached'] && $cache = cache_get($arghash, 'cache')) { - return ($emfield['emvideo_valid'] || $emfield['emaudio_valid']) ? array_merge($cache->data, $emfield) : $cache->data; + return $media['media_valid'] ? array_merge($cache->data, $media) : $cache->data; } timer_start(__FUNCTION__); - + $result = drupal_http_request($url, array('timeout' => $options['timeout'])); if ($result->code != 200) { @@ -894,12 +845,12 @@ function fbsmp_link_request_contents($url, $options = array()) { } $page_url = $url; - if (in_array($result->redirect_code, array(301, 302, 307))) { + if (isset($result->redirect_code) && in_array($result->redirect_code, array(301, 302, 307))) { $page_url = $result->redirect_url; $data['title'] = $page_url; } - if (stripos($result->headers['Content-Type'], 'image') !== FALSE) { + if (stripos($result->headers['content-type'], 'image') !== FALSE) { $images = array(); if (($imagesize = _fbsmp_link_validate_filesize($page_url, $options['max_imagesize'], $options['min_imagesize'], ($options['timeout'] - timer_read(__FUNCTION__) / 1000))) != -1) { $images[$page_url] = $imagesize; @@ -925,8 +876,8 @@ function fbsmp_link_request_contents($url, $options = array()) { $descriptions = @$xpath->evaluate("//meta[@name='description']"); $data['description'] = trim($descriptions->length > 0 ? $descriptions->item(0)->getAttribute('content') : $data['description']); - if (!$emfield['emvideo_valid'] && !$emfield['emaudio_valid']) { - $hrefs = @$xpath->evaluate("/html/body//img"); + if (!$media['media_valid']) { + $hrefs = @$xpath->evaluate("/html/body//img"); $images = array(); if (!fbsmp_include_library('url_to_absolute.inc', 'url_to_absolute')) { @@ -959,7 +910,7 @@ function fbsmp_link_request_contents($url, $options = array()) { cache_set($arghash, $data, 'cache', REQUEST_TIME + $options['cache_duration']); } - return ($emfield['emvideo_valid'] || $emfield['emaudio_valid']) ? array_merge($data, $emfield) : $data; + return $media['media_valid'] ? array_merge($data, $media) : $data; } /** diff --git a/plugins/link.media.inc b/plugins/link.media.inc new file mode 100644 index 0000000..6ee4cda --- /dev/null +++ b/plugins/link.media.inc @@ -0,0 +1,229 @@ + FBEMP_LINK_MEDIA_DEFAULT_VIDEO_WIDTH, + 'media_video_height' => FBSMP_LINK_MEDIA_DEFAULT_VIDEO_HEIGHT, + 'media_video_autoplay' => 0, + 'media_video_providers' => array(), + ); + + $url = ' ' . $url . ' '; + + //Need to attach the variables to the callback after the regex. + $callback = _fbsmp_link_media_curry('_fbsmp_link_media_parse_links', 2); + + // Match absolute URLs. + $item = preg_replace_callback("`(

|

  • ||[ \n\r\t\(])((http://|https://)([a-zA-Z0-9@:%_+*~#?&=.,/;-]*[a-zA-Z0-9@:%_+*~#&=/;-]))([.,?!]*?)(?=(

    |
  • ||[ \n\r\t\)]))`i", $callback($settings), $url); + $item = unserialize($item); + if (is_array($item)) { + $item['image'] = $item['thumbnail_url']; + } + return $item; +} + +/** + * Helper function which invokes the media 'data' hook of the video provider. + */ +function _fbsmp_link_media_parse_links($settings, $match) { + //Get just the URL. + $match[2] = check_url(decode_entities($match[2])); + + try { + $provider = media_internet_get_provider($match[2]); + $file = $provider->getFileObject(); + $item = $provider->getOEmbed(); + } + catch(MediaInternetNoHandlerException $e) { + return ''; + } + + return serialize($item); +} + +/** + * @todo Please document this function. + * @see http://drupal.org/node/1354 + */ +function _fbsmp_link_media_curry($func, $arity) { + return create_function('', " + \$args = func_get_args(); + if(count(\$args) >= $arity) + return call_user_func_array('$func', \$args); + \$args = var_export(\$args, 1); + return create_function('',' + \$a = func_get_args(); + \$z = ' . \$args . '; + \$a = array_merge(\$z,\$a); + return call_user_func_array(\'$func\', \$a); + '); + "); +} + +/** + * Generates the embedded code for the shared video. + * + * @param $attachment + * The attachment object. It contains an array containing the raw data, + * provider and the data version. ($attachment->data['emfield_data']) + * @param $settings + * (optional) An array which can have one or more of following keys: + * - emvideo_providers + * An array containing the allowed video providers. Defaults to none. + * - emvideo_height + * An integer specifying the height of the embedded video. Defaults to 435. + * - emvideo_width + * An integer specifying the height of the embedded video. Defaults to 350. + * - emvideo_autoplay + * A boolean specifying whether to autoplay the embedded video. Defaults to 0. + * + * @return + * A string containing the embedded code. + */ +function theme_fbsmp_link_media($attachment, $settings = array()) { + // TODO: Should this theme fbsmp_link_emvideo be declared in hook_theme()? + $settings += array( + 'media_video_width' => FBEMP_LINK_MEDIA_DEFAULT_VIDEO_WIDTH, + 'media_video_height' => FBSMP_LINK_MEDIA_DEFAULT_VIDEO_HEIGHT, + 'media_video_autoplay' => 0, + 'media_video_providers' => array(), + ); + + $display_format = "video_video"; + + $emfield_item = $attachment['attachment']->data['media_data']; + $emfield_item['embed'] = TRUE; + + $content = $emfield_item['html']; + $output = ''; + $output .= ''; + return $output; +} + +/** + * Settings form for Link-Media integration. + */ +function _fbsmp_link_media_settings_form(&$form_state, $settings = array()) { + $form = array(); + + // Get Media internet providers + $providers = media_internet_get_providers(); + foreach ($providers as $name => $provider) { + $options[$name] = $provider['title']; + } + + $form['provider_list'] = array( + '#type' => 'fieldset', + '#title' => t('Providers Supported'), + '#description' => t('Select which third party providers you wish to allow from the list below. When a user submits new content, the URL they enter will be matched to the provider, assuming that provider is allowed here.'), + '#collapsible' => TRUE, + '#collapsed' => FALSE, + ); + $form['provider_list']['media_providers'] = array( + '#type' => 'checkboxes', + '#title' => t('Providers'), + '#default_value' => empty($settings['media_providers']) ? array() : $settings['media_providers'], + '#options' => $options, + ); + + $form['video'] = array( + '#type' => 'fieldset', + '#title' => t('Video Display Settings'), + '#description' => t('These settings control how this video is displayed in its full size'), + '#collapsible' => TRUE, + '#collapsed' => FALSE, + ); + $form['video']['media_video_width'] = array( + '#type' => 'textfield', + '#title' => t('Video display width'), + '#default_value' => empty($settings['media_video_width']) ? $settings['default_media_video_width'] : $settings['media_video_width'], + '#required' => TRUE, + '#description' => t('The width of the embedded video. It defaults to @width.', array('@width' => $settings['default_media_video_width'])), + ); + $form['video']['media_video_height'] = array( + '#type' => 'textfield', + '#title' => t('Video display height'), + '#default_value' => empty($settings['media_video_height']) ? $settings['default_media_video_height'] : $settings['media_video_height'], + '#required' => TRUE, + '#description' => t('The height of the video. It defaults to @height.', array('@height' => $settings['default_media_video_height'])), + ); + $form['video']['media_video_autoplay'] = array( + '#type' => 'checkbox', + '#title' => t('Autoplay'), + '#default_value' => empty($settings['media_video_autoplay']) ? '' : $settings['media_video_autoplay'], + '#description' => t('If supported by the provider, checking this box will cause the video to automatically begin after the video loads.'), + ); + $form['video']['media_video_inline_replacement'] = array( + '#type' => 'checkbox', + '#title' => t('Video inline replacement'), + '#default_value' => empty($settings['media_video_inline_replacement']) ? '' : $settings['media_video_inline_replacement'], + '#description' => t('Checking this box will cause the video to be delay loaded, only when the user clicks on the thumbnail.'), + ); + return $form; +} + +/** + * Validation for the settings form + */ +function _fbsmp_link_media_settings_form_validate(&$form, &$form_state) { + $form_values = $form_state['values']; + foreach (array('media_video_height' => 'video height', 'media_video_width' => 'video width') as $attr => $desc) { + if (!empty($form_values[$attr]) && (!is_numeric($form_values[$attr]) || $form_values[$attr] != round($form_values[$attr]) || $form_values[$attr] < 0)) { + form_set_error($attr, t('The @attribute must be a non-negative integer.', array('@attribute' => $desc))); + } + } +} + +/** + * Submit function for the settings form. + */ +function _fbsmp_link_media_settings_form_submit(&$form, &$form_state) { + $settings = array(); + $settings['media_providers'] = $form_state['values']['media_providers']; + $settings['media_video_width'] = $form_state['values']['media_video_width']; + $settings['media_video_height'] = $form_state['values']['media_video_height']; + $settings['media_video_autoplay'] = $form_state['values']['media_video_autoplay']; + $settings['media_video_inline_replacement'] = $form_state['values']['media_video_inline_replacement']; + + return $settings; +} + +/** + * Menu callback for inline video replacement. + */ +function fbsmp_link_media_video_replace($sid) { + $attachment = fbsmp_load($sid); + $settings = fbsmp_load_plugin_settings('link'); + + // TODO Please change this theme call to use an associative array for the $variables parameter. + print theme('fbsmp_link_media', $attachment, $settings); +}