View source
<?php
define('EMFIELD_STATUS_UNAVAILABLE', 0x0);
define('EMFIELD_STATUS_AVAILABLE', 0x1);
define('EMFIELD_STATUS_PRIVATE', 0x2);
function emfield_menu() {
$items['admin/content/emfield'] = array(
'file' => 'emfield.admin.inc',
'title' => 'Embedded Media Field configuration',
'description' => 'Configure Embedded Media Field: Allow content types to use various 3rd party providers, enter API keys, etc.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'emfield_settings',
),
'access arguments' => array(
'administer site configuration',
),
);
$items['admin/content/emfield/media'] = array(
'file' => 'emfield.admin.inc',
'title' => 'General',
'description' => 'Configure Embedded Media Field: Allow content types to use various 3rd party providers, enter API keys, etc.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'emfield_settings',
),
'access arguments' => array(
'administer site configuration',
),
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -3,
);
foreach (module_implements('emfield_info', TRUE) as $module) {
$emfield_info = module_invoke($module, 'emfield_info');
$items['admin/content/emfield/' . $module] = array(
'title' => $emfield_info['#settings_title'],
'description' => $emfield_info['#settings_description'],
'file' => 'emfield.admin.inc',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'emfield_module_settings',
3,
),
'access arguments' => array(
'administer site configuration',
),
'type' => MENU_LOCAL_TASK,
);
}
$items['admin/content/emfield/obsolete'] = array(
'file' => 'emfield.admin.inc',
'title' => 'Disable obsolete provider',
'page callback' => 'emfield_disable_obsolete_provider',
'access arguments' => array(
'administer site configuration',
),
'type' => MENU_CALLBACK,
);
if (module_exists('job_queue')) {
$items['admin/content/emfield/reload'] = array(
'file' => 'emfield.admin.inc',
'title' => 'Reload data',
'description' => 'Reload emfield fields in bulk',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'emfield_settings_jobqueue',
),
'access arguments' => array(
'administer site configuration',
),
'type' => MENU_LOCAL_TASK,
'weight' => 3,
);
}
return $items;
}
function emfield_field_columns($field) {
module_load_include('inc', 'emfield', 'emfield.cck');
return _emfield_field_columns($field);
}
function emfield_emfield_field($op, &$node, $field, &$items, $teaser, $page, $module) {
module_load_include('inc', 'emfield', 'emfield.cck');
return _emfield_emfield_field($op, $node, $field, $items, $teaser, $page, $module);
}
function emfield_emfield_content_is_empty($item, $field) {
return empty($item['value']);
}
function emfield_allowed_providers($field, $module) {
$allowed_providers = emfield_system_list($module);
$providers = isset($field['widget']['providers']) ? $field['widget']['providers'] : (isset($field['providers']) ? $field['providers'] : array());
$allow_all = TRUE;
foreach ($allowed_providers as $test) {
if (!variable_get('emfield_' . $module . '_allow_' . $test->name, TRUE)) {
unset($allowed_providers[$test->name]);
}
else {
$allow_all &= empty($providers[$test->name]);
}
}
if (!$allow_all) {
foreach ($allowed_providers as $test) {
if (empty($providers[$test->name])) {
unset($allowed_providers[$test->name]);
}
}
}
return $allowed_providers;
}
function emfield_implement_types($cached = TRUE) {
static $types;
if (!isset($types) || !$cached) {
if ($cached && ($cache = cache_get('emfield_implement_types', 'cache'))) {
$types = $cache->data;
}
else {
$system_types = _content_type_info();
$content_types = $system_types['content types'];
$field_types = $system_types['field types'];
$types = array();
$modules = array();
foreach (module_implements('emfield_info', TRUE) as $module) {
$modules[$module] = $module;
}
foreach ($content_types as $content_type => $type) {
foreach ($type['fields'] as $field_type => $field) {
if (!empty($modules[$field_types[$field['type']]['module']])) {
$module = $modules[$field_types[$field['type']]['module']];
$types[$module][$content_type][$field_type] = $field;
}
}
}
}
}
cache_set('emfield_implement_types', $types, 'cache', CACHE_PERMANENT);
return $types;
}
function emfield_parse_embed($field, $embed = '', $module) {
$providers = emfield_allowed_providers($field, $module);
foreach ($providers as $provider) {
$success = emfield_include_invoke($module, $provider->name, 'extract', trim($embed), $field);
if (is_array($success)) {
foreach ($success as $regex) {
$matches = NULL;
if (preg_match($regex, trim($embed), $matches)) {
return array(
'provider' => $provider->name,
'value' => $matches[1],
);
}
}
}
else {
if ($success) {
return array(
'provider' => $provider->name,
'value' => $success,
);
}
}
}
return array();
}
function _emfield_field_validate_id($field, $item, $error_field, $module, $delta = 0) {
$item = _emfield_field_submit_id($field, $item, $module, $error_field);
if ($item['embed'] && !$item['provider']) {
form_set_error($error_field, t('You have specified an invalid media URL or embed code.'));
}
else {
emfield_include_invoke($module, $item['provider'], 'validate', $item['value'], $error_field);
}
return $item;
}
function _emfield_field_submit_id($field, $item, $module) {
if (!isset($item['provider'])) {
$item['provider'] = '';
}
$item = array_merge($item, emfield_parse_embed($field, $item['embed'], $module));
$item['data'] = (array) emfield_include_invoke($module, $item['provider'], 'data', $field, $item);
$item['version'] = intval(emfield_include_invoke($module, $item['provider'], 'data_version', $field, $item));
$info = emfield_include_invoke($module, $item['provider'], 'info');
if ($info['module'] && module_hook($info['module'], 'emfield_status')) {
$item['status'] = module_invoke($info['module'], 'emfield_status', $item, $field, $module);
}
return $item;
}
function emfield_emfield_field_formatter($field, $item, $formatter, $node, $module, $options = array()) {
if (isset($node->in_preview)) {
$item = emfield_parse_embed($field, $item['embed'], $module);
}
if (!isset($item['value'])) {
return '';
}
if (!isset($node->type) && isset($field['type_name'])) {
$type = content_types($field['type_name']);
$field['widget'] = $type['fields'][$field['field_name']]['widget'];
}
if (!is_array($item['data'])) {
$item['data'] = (array) unserialize($item['data']);
}
$output = '';
$output .= theme($module . '_' . $formatter, $field, $item, $formatter, $node, $options);
return $output;
}
function emfield_emfield_widget_settings($op, $widget, $module) {
module_load_include('inc', 'emfield', 'emfield.cck');
return _emfield_emfield_widget_settings($op, $widget, $module);
}
function emfield_emfield_widget(&$form, &$form_state, $field, $items, $delta = 0, $module) {
module_load_include('inc', 'emfield', 'emfield.cck');
return _emfield_emfield_widget($form, $form_state, $field, $items, $delta, $module);
}
function emfield_node_operations() {
$operations = array(
'emfield_reload' => array(
'label' => t('Reload Embedded Media Data'),
'callback' => 'emfield_operations_reload',
),
);
return $operations;
}
function emfield_operations_reload($nids = array(), $show_message = TRUE) {
foreach ($nids as $nid) {
if ($node = node_load($nid)) {
$type = content_types($node->type);
foreach ($type['fields'] as $field) {
if (module_hook($field['type'], 'emfield_info')) {
$message = "Reloaded %node-title's %field.";
$message_variables = array(
'%node-title' => $node->title,
'%field' => $field['type_name'],
);
watchdog('emfield reload data', $message, $message_variables, WATCHDOG_NOTICE, l($node->title, 'node/' . $node->nid));
if ($show_message) {
drupal_set_message(t($message, $message_variables));
}
$items = $node->{$field['field_name']};
emfield_emfield_field('submit', $node, $field, $items, FALSE, FALSE, $field['type']);
$node->{$field['field_name']} = $items;
node_save($node);
}
}
}
}
}
function emfield_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
switch ($op) {
case 'rss item':
module_load_include('inc', 'emfield', 'emfield.rss');
return _emfield_nodeapi_rss($node, $op, $teaser, $page);
}
}
function emfield_request_xml($provider, $url, $args = array(), $cached = TRUE, $return_errors = FALSE, $hash_extra = FALSE, $serialized = FALSE, $json = FALSE) {
ksort($args);
$arghash = $provider . ':';
foreach ($args as $k => $v) {
$arghash .= $k . $v;
}
foreach ($args as $k => $v) {
$encoded_params[] = urlencode($k) . '=' . urlencode($v);
}
if (!empty($encoded_params)) {
$url .= '?' . implode('&', $encoded_params);
}
if (isset($hash_extra)) {
$arghash .= ':' . $hash_extra;
}
if ($cached && ($cache = cache_get($arghash, 'cache'))) {
return $cache->data;
}
$result = drupal_http_request($url);
if (!empty($result->error)) {
if ($return_errors) {
return array(
'stat' => 'error',
'code' => $result->code,
'message' => 'HTTP Error: ' . $result->error,
);
}
emfield_set_error(t("Could not connect to @provider to request XML: HTTP error @error.", array(
'@error' => $result->code,
'@provider' => $provider,
)));
return array();
}
if ($json) {
$response = (array) json_decode($result->data);
}
else {
if ($serialized) {
$response = unserialize($result->data);
if (!$response) {
if ($return_errors) {
return array(
'stat' => 'error',
'code' => '-1',
'message' => 'The response was corrupted, it could not be unserialized.',
);
}
emfield_set_error(t("The response from @provider was corrupted and could not be unserialized.", array(
'@provider' => $provider,
)));
return array();
}
}
else {
$parser = drupal_xml_parser_create($result->data);
$vals = array();
$index = array();
xml_parse_into_struct($parser, $result->data, $vals, $index);
xml_parser_free($parser);
$response = array();
$response['_emfield_arghash'] = $arghash;
$level = array();
$start_level = 1;
foreach ($vals as $xml_elem) {
if ($xml_elem['type'] == 'open') {
if (array_key_exists('attributes', $xml_elem)) {
list($level[$xml_elem['level']], $extra) = array_values($xml_elem['attributes']);
}
else {
$level[$xml_elem['level']] = $xml_elem['tag'];
}
}
if ($xml_elem['type'] == 'complete') {
$php_stmt = '$response';
while ($start_level < $xml_elem['level']) {
$php_stmt .= '[$level[' . $start_level . ']]';
$start_level++;
}
$php_stmt .= '[$xml_elem[\'tag\']][] = $xml_elem[\'value\'];' . $php_stmt . '[$xml_elem[\'tag\']][] = $xml_elem[\'attributes\'];';
eval($php_stmt);
$start_level--;
}
}
}
}
if ($cached) {
cache_set($arghash, $response, 'cache', time() + variable_get('emfield_cache_duration', 3600));
}
return $response;
}
function emfield_request_header($provider, $url, $cached = TRUE, $return_errors = TRUE) {
if ($cached && ($cache = cache_get('emfield:request-header:' . $url, 'cache_emfield_xml'))) {
return $cache->data;
}
$result = _emfield_http_request_header($url);
if (!empty($result->error)) {
if ($return_errors) {
return $result;
}
emfield_set_error(t("Could not connect to @provider: HTTP error @error.", array(
'@error' => $result->code,
'@provider' => $provider,
)));
return array();
}
cache_set('emfield:request-header:' . $url, $result, 'cache_emfield_xml', time() + variable_get('emfield_cache_duration', 3600));
return $result;
}
function _emfield_http_request_header($url, $retry = 4) {
$result = drupal_http_request($url, array(), 'HEAD', NULL, 0);
switch ($result->code) {
case 200:
case 304:
break;
case 301:
case 302:
case 303:
case 307:
$location = $result->headers['Location'];
if ($retry > 0) {
$result = _emfield_http_request_header($result->headers['Location'], --$retry);
$result->redirect_code = $result->code;
}
$result->redirect_url = $location;
break;
default:
}
return $result;
}
function emfield_set_error($error) {
watchdog('emfield', '!error', array(
'!error' => $error,
), WATCHDOG_WARNING);
}
function emfield_system_list($module, $provider = NULL, $options = array()) {
$files = $override_files = module_invoke_all('emfield_providers', $module, $provider);
ksort($files);
foreach ($files as $provider => $file) {
emfield_include_list($file);
if (!function_exists($module . '_' . $provider . '_info')) {
emfield_include_list($file, TRUE);
unset($files[$provider]);
if (function_exists($module . '_' . $provider . '_obsolete') && variable_get('emfield_' . $module . '_allow_' . $provider, TRUE)) {
if (isset($options['suppress_errors']) && $options['suppress_errors']) {
continue;
}
$error = t('Attempted to load obsolete provider: %provider at @file.', array(
'%provider' => $provider,
'@file' => $file->filename,
));
emfield_set_error($error);
if (user_access('administer site configuration')) {
drupal_set_message($error, error);
call_user_func($module . '_' . $provider . '_obsolete');
drupal_set_message(t('You may also choose to !turn_off this warning about the missing or obsolete Embedded Media Field %provider provider file.', array(
'%provider' => $provider,
'!turn_off' => l(t('turn off'), 'admin/content/emfield/obsolete/' . $module . '/' . $provider, array(
'query' => 'destination=' . $_GET['q'],
)),
)), 'error');
}
}
}
}
return $files;
}
function emfield_include_list($file = NULL, $remove = FALSE) {
static $list = array();
if ($file && $file->filename && !isset($list[$file->filename])) {
include_once './' . $file->filename;
$list[$file->filename] = $file->name;
}
if ($file && $remove) {
unset($list[$file->filename]);
}
return $list;
}
function emfield_include_hook($module, $provider, $hook) {
return function_exists($module . '_' . $provider . '_' . $hook);
}
function emfield_include_invoke() {
$args = func_get_args();
$module = array_shift($args);
$provider = array_shift($args);
$hook = array_shift($args);
$function = $module . '_' . $provider . '_' . $hook;
emfield_system_list($module, $provider);
return emfield_include_hook($module, $provider, $hook) ? call_user_func_array($function, $args) : NULL;
}
function emfield_views_handler_filter_is_not_null($op, $filter, $filterinfo, &$query) {
if ($op == 'handler') {
$query
->ensure_table($filterinfo['table']);
if ($filter['value']) {
$qs = "%s.%s <> '' AND %s.%s IS NOT NULL";
}
else {
$qs = "%s.%s = '' OR %s.%s IS NULL";
}
$query
->add_where($qs, $filterinfo['table'], $filterinfo['content_db_info']['columns']['provider']['column'], $filterinfo['table'], $filterinfo['content_db_info']['columns']['provider']['column']);
}
}
function emfield_views_handler_filter_provider_list($op) {
$providers = array();
foreach (emfield_system_list('emvideo') as $provider => $info) {
$providers[$provider] = $info->name;
}
return $providers;
}
function emfield_views_handler_filter_provider($op, $filter, $filterinfo, &$query) {
if ($op == 'handler') {
$query
->ensure_table($filterinfo['table']);
if ($filter['operator'] == 'OR') {
foreach ($filter['value'] as $provider) {
$items[] = "%s.%s = '{$provider}'";
$where[] = $filterinfo['table'];
$where[] = $filterinfo['content_db_info']['columns']['provider']['column'];
}
$qs = implode(' OR ', $items);
}
else {
foreach ($filter['value'] as $provider) {
$items[] = "%s.%s <> '{$provider}'";
$where[] = $filterinfo['table'];
$where[] = $filterinfo['content_db_info']['columns']['provider']['column'];
}
$qs = implode(' AND ', $items);
}
$query
->add_where($qs, $where);
}
}
function _emfield_handler_arg_provider($op, &$query, $argtype, $arg = '', $module = '') {
if ($op == 'filter') {
$field_name = drupal_substr($argtype['type'], 10);
}
else {
$field_name = drupal_substr($argtype, 10);
}
$field = content_fields($field_name);
$db_info = content_database_info($field);
$main_column = $db_info['columns']['provider'];
$table = 'node_data_' . $field['field_name'];
switch ($op) {
case 'summary':
$query
->ensure_table($table);
$query
->add_field($main_column['column'], $table);
$query
->add_groupby($table . '.' . $main_column['column']);
return array(
'field' => $table . '.' . $main_column['column'],
);
case 'sort':
$query
->ensure_table($table);
$query
->add_orderby($table, $main_column['column'], $argtype);
break;
case 'filter':
$query
->ensure_table($table);
$where = db_escape_string($arg);
$query
->add_where($table . '.' . $main_column['column'] . " = '%s'", $where);
break;
case 'link':
$provider = emfield_system_list($module, $query->{$main_column}['column']);
$info = emfield_include_invoke($module, $provider[$query]->name, 'info');
$title = $info['name'];
return l($title, $arg . '/' . $query->{$main_column}['column']);
case 'title':
$provider = emfield_system_list($module, $query);
$info = emfield_include_invoke($module, $provider[$query]->name, 'info');
$title = $info['name'];
return $title ? $title : check_plain($query);
}
}
function emfield_provider_themes($module, $provider = NULL) {
$themes = array();
if ($provider && ($subthemes = emfield_include_invoke($module, $provider, 'subtheme'))) {
$themes = $subthemes;
}
$providers = emfield_system_list($module);
foreach ($providers as $provider) {
if ($subthemes = emfield_include_invoke($module, $provider->name, 'emfield_subtheme')) {
$themes += $subthemes;
}
}
return $themes;
}
function emfield_provider_menus($module, $provider = NULL) {
$menus = array();
if ($provider && ($submenus = emfield_include_invoke($module, $provider, 'submenu'))) {
$menus = $submenus;
}
$providers = emfield_system_list($module);
foreach ($providers as $provider) {
if ($submenus = emfield_include_invoke($module, $provider->name, 'emfield_submenu')) {
$menus = array_merge($menus, (array) $submenus);
}
}
return $menus;
}
function emfield_flvmediaplayer_url($reset = FALSE) {
static $path;
if (!isset($path) || $reset) {
if (module_exists('flvmediaplayer')) {
$path = variable_get('flvmediaplayer_path', drupal_get_path('module', 'flvmediaplayer') . '/mediaplayer.swf');
}
else {
if ($reset || ($path = variable_get('emfield_flvmediaplayer_url', NULL) && !$path)) {
$path = _emfield_autodiscover_path('(^player-viral\\.swf$|^player\\.swf$|^mediaplayer\\.swf$)');
variable_set('emfield_flvmediaplayer_url', $path);
}
}
}
return $path;
}
function emfield_imagerotator_url($reset = FALSE) {
static $path;
if (is_null($path) || $reset) {
if (is_null($path = variable_get('emfield_imagerotator_url', NULL)) || !$path && $reset) {
$path = _emfield_autodiscover_path('(^imagerotator\\.swf$)');
variable_set('emfield_imagerotator_url', $path);
}
}
return $path;
}
function _emfield_autodiscover_path($pattern) {
foreach (array(
'libraries',
'plugins',
'modules',
) as $folder) {
$files = drupal_system_listing($pattern, $folder, 'basename', 0);
if (!empty($files)) {
$file = array_pop($files);
return $file->filename;
}
}
}
function emfield_content_generate($node, $field) {
$item = $urls = array();
$module = $field['module'];
foreach (emfield_allowed_providers($field, $module) as $provider) {
$provider_urls = emfield_include_invoke($module, $provider->name, 'content_generate', $node, $field);
if (is_array($provider_urls)) {
$urls[$provider->name] = $provider_urls;
}
}
if (!empty($urls)) {
$provider = array_rand($urls);
$item['provider'] = $provider;
$key = array_rand($urls[$provider]);
$item['embed'] = $urls[$provider][$key];
$item = _emfield_field_submit_id($field, $item, $module);
}
return $item;
}
function emfield_emfield_field_settings($op, $field) {
module_load_include('inc', 'emfield', 'emfield.cck');
return _emfield_emfield_field_settings($op, $field);
}
function emfield_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'emfield') . '/includes/views',
);
}
function emfield_theme() {
return array(
'emfield_handler_field_provider' => array(
'arguments' => array(
'item' => NULL,
'field' => NULL,
'node' => NULL,
'values' => NULL,
'format' => NULL,
),
),
'emfield_handler_field_status' => array(
'arguments' => array(
'item' => NULL,
'field' => NULL,
'node' => NULL,
'values' => NULL,
'format' => NULL,
),
),
'emfield_swfobject' => array(
'arguments' => array(
"url" => NULL,
'$div_id' => NULL,
'width' => NULL,
'height' => NULL,
'version' => "8",
),
'path' => drupal_get_path('module', 'emfield') . '/includes/themes',
'file' => 'emfield.themes.inc',
),
);
}
function emfield_retrieve_xml($url, $display_errors = FALSE, $refresh = FALSE) {
module_load_include('inc', 'emfield', 'includes/emfield.xml');
return _emfield_retrieve_xml($url, $display_errors, $refresh);
}
function emfield_audit() {
module_load_include('inc', 'emfield', 'includes/emfield.audit');
return _emfield_audit();
}
function emfield_init() {
global $emfield_registered_classes;
foreach (module_implements('emfield_register') as $module) {
foreach (module_invoke($module, 'emfield_register') as $class_name => $class) {
$emfield_registered_classes[$class_name] = is_array($class) ? $class : array();
if (!isset($emfield_registered_classes[$class_name]['class_name'])) {
$emfield_registered_classes[$class_name]['class_name'] = $class_name;
}
if (!isset($emfield_registered_classes[$class_name]['name'])) {
$emfield_registered_classes[$class_name]['name'] = $class_name;
}
if (!isset($emfield_registered_classes[$class_name]['path'])) {
$emfield_registered_classes[$class_name]['path'] = drupal_get_path('module', $module);
}
if (!isset($emfield_registered_classes[$class_name]['file'])) {
$emfield_registered_classes[$class_name]['file'] = $class_name . '.inc';
}
}
}
spl_autoload_register('emfield_autoload');
}
function emfield_autoload($class_name) {
global $emfield_registered_classes;
if (isset($emfield_registered_classes[$class_name])) {
include_once $emfield_registered_classes[$class_name]['path'] . '/' . $emfield_registered_classes[$class_name]['file'];
}
}
function emfield_media($url, $overrides = array()) {
global $emfield_registered_classes;
foreach ($emfield_registered_classes as $provider => $class) {
if ($class['parse callback'] && $class['class_name']) {
if ($parsed = $class['parse callback']($url, $overrides)) {
return new $class['class_name']($parsed['code'], $overrides);
}
}
}
}