View source
<?php
function kaltura_get_partner_details($partner_id, $cms_email, $cms_password) {
$session_user = KalturaHelpers::getSessionUser();
$config = KalturaHelpers::getServiceConfiguration();
$config->partnerId = $partner_id;
$kaltura_client = new KalturaClient($config);
try {
$result = $kaltura_client->partner
->getsecrets($partner_id, $cms_email, $cms_password);
} catch (Exception $ex) {
drupal_set_message(t('Kaltura client exception: @message', array(
'@message' => $ex
->getMessage(),
)), 'error');
$result['error'] = $ex
->getMessage();
}
if ($result->id) {
return array(
$result->id,
$result->id . "00",
$result->notificationUrl,
$result->secret,
$result->adminSecret,
);
}
else {
return array(
FALSE,
$result['error'],
);
}
}
function kaltura_create_widgets_for_partner($partner_id, $secret) {
$session_user = KalturaHelpers::getSessionUser();
$kaltura_client = KalturaHelpers::getKalturaClient(1);
$widgets = new KalturaSettings();
foreach ($widgets->kdp_widgets as $type => $uiconfs) {
foreach ($uiconfs as $skin => $confs) {
}
}
}
function kaltura_settings_form() {
global $base_url;
if (variable_get('kaltura_partner_id', '') == '') {
drupal_goto('admin/settings/kaltura/install');
}
if (!module_exists('advanced_help')) {
$form['kaltura_help'] = array(
'#value' => t('For more detailed help enable the advanced help module.'),
);
}
$form['kaltura_server_status'] = array(
'#type' => 'fieldset',
'#title' => t('Server Status'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
list($admin_session_status, $admin_session_error) = kaltura_check_server_status($admin = 1);
list($normal_session_status, $normal_session_error) = kaltura_check_server_status($admin = 0);
$status_class = $admin_session_status && $normal_session_status ? 'ok' : 'bad';
$session_status = $admin_session_status && $normal_session_status ? 'OK' : 'ERROR';
$error = '';
if ($admin_session_error) {
$error .= '<br />Admin Session: ' . $admin_session_error;
$error_status = TRUE;
}
if ($normal_session_error) {
$error .= '<br />Normal Session: ' . $normal_session_error;
$error_status = TRUE;
}
$form['kaltura_server_status']['kaltura_Admin_test_server'] = array(
'#type' => 'item',
'#title' => t('Drupal to Kaltura Session Test'),
'#prefix' => '<div class="kaltura-status-' . $status_class . '">',
'#value' => 'Session status: ' . $session_status . ($error_status ? $error : ''),
'#suffix' => '</div>',
);
if (kaltura_variable_get_real('kaltura_notification_type', 0) == 1) {
list($status, $text) = kaltura_test_notification_config();
$prefix = '<div class="kaltura-status-' . ($status ? 'ok' : 'bad') . '">';
$form['kaltura_server_status']['kaltura_notification_status'] = array(
'#type' => 'item',
'#title' => t('Kaltura to Drupal Notification Status'),
'#prefix' => $prefix,
'#value' => $text,
'#suffix' => '</div>',
);
}
list($status, $text) = kaltura_test_cron_job_status();
$prefix = '<div class="kaltura-status-' . ($status ? 'ok' : 'bad') . '">';
$form['kaltura_server_status']['kaltura_cron_job_status'] = array(
'#type' => 'item',
'#title' => t('Cron-Job Status'),
'#prefix' => $prefix,
'#value' => $text,
'#suffix' => '</div>',
);
list($status, $text) = kaltura_test_crossdomain();
$prefix = '<div class="kaltura-status-' . ($status ? 'ok' : 'bad') . '">';
$form['kaltura_server_status']['kaltura_crossdomain_status'] = array(
'#type' => 'item',
'#title' => t('crossdomain.xml Status'),
'#prefix' => $prefix,
'#value' => $text,
'#suffix' => '</div>',
);
if (variable_get('kaltura_partner_id', '') == '') {
drupal_set_title('All in One Video Module by Kaltura Installation');
$forgot_link_attributes = array(
'attributes' => array(
'target' => '_blank',
),
);
$information_text = 'Please enter your CMS email & password';
$information_text .= '<br />' . l('forgot password', 'http://www.kaltura.com/index.php/cms/login', $forgot_link_attributes);
$form['kaltura_settings_information']['#value'] = $information_text;
}
$form['kaltura_partner_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Partner Info'),
'#collapsible' => $_GET['register'] === 'no' ? FALSE : TRUE,
'#collapsed' => $_GET['register'] === 'no' ? FALSE : TRUE,
);
$partner_exist = variable_get('kaltura_partner_id', '');
$readonly_attr = array();
if (!empty($partner_exist)) {
$readonly_attr = array(
'readonly' => 'readonly',
);
}
$form['kaltura_partner_settings']['kaltura_partner_id'] = array(
'#type' => 'textfield',
'#title' => t('Partner ID'),
'#default_value' => variable_get('kaltura_partner_id', ''),
'#size' => 20,
'#maxlength' => 10,
'#attributes' => $readonly_attr,
);
$form['kaltura_partner_settings']['kaltura_partner_cms_email'] = array(
'#type' => 'textfield',
'#title' => t('Email'),
'#default_value' => variable_get('kaltura_partner_cms_email', ''),
'#description' => t('This value must match your Kaltura server settings.'),
);
$form['kaltura_partner_settings']['kaltura_partner_cms_password'] = array(
'#type' => 'password',
'#title' => t('Password'),
'#default_value' => variable_get('kaltura_partner_cms_password', ''),
'#description' => t('This value must match your Kaltura server settings. Leave blank unless changing your password.'),
);
if (!empty($partner_exist)) {
$kmc_url = trim(variable_get('kaltura_server_url', 'http://www.kaltura.com'), '/') . '/kmc';
$form['kaltura_partner_settings']['kaltura_upd_data'] = array(
'#type' => 'item',
'#value' => t('You can update your email and password via Kaltura Management Console here: ') . l($kmc_url, $kmc_url, array(
'attributes' => array(
'target' => '_blank',
),
)),
);
}
if (variable_get('kaltura_partner_id', '') == '') {
$form_state['storage']['submit'] = 'ImportPartner';
$form['kaltura_registration']['buttons']['import_partner'] = array(
'#type' => 'button',
'#title' => 'Complete Installation',
'#value' => 'Complete Installation',
'#default_value' => 'Complete Installation',
'#weight' => 40,
);
}
$form['kaltura_partner_settings']['kaltura_subp_id'] = array(
'#type' => 'hidden',
'#title' => t('Kaltura Sub-Partner Id'),
'#default_value' => variable_get('kaltura_subp_id', ''),
'#size' => 20,
'#maxlength' => 10,
);
if (variable_get('kaltura_local_registration', 0) != 1 || 1) {
$form['kaltura_partner_settings']['kaltura_secret'] = array(
'#type' => 'hidden',
'#title' => t('Kaltura Secret'),
'#default_value' => variable_get('kaltura_secret', ''),
'#size' => 60,
'#maxlength' => 64,
);
$form['kaltura_partner_settings']['kaltura_admin_secret'] = array(
'#type' => 'hidden',
'#title' => t('Kaltura Admin Secret'),
'#default_value' => variable_get('kaltura_admin_secret', ''),
'#size' => 60,
'#maxlength' => 64,
);
}
$form['kaltura_partner_settings']['kaltura_server_url'] = array(
'#type' => 'hidden',
'#title' => t('Kaltura Server URL'),
'#default_value' => variable_get('kaltura_server_url', KalturaHelpers::getKalturaServerUrl()),
'#size' => 100,
'#maxlength' => 64,
);
$notification_settings = kaltura_notifications_settings_form();
$form['kaltura_notification_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Kaltura to Drupal Notification Settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
foreach ($notification_settings as $name => $form_element) {
$form['kaltura_notification_settings'][$name] = $form_element;
}
$form['kaltura_credits'] = array(
'#type' => 'fieldset',
'#title' => 'Credits',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$text = '<b>Open Source Video Links</b>' . PHP_EOL;
$text .= 'Kaltura invests a great deal of effort in the open source community, and specifically in the open source video movement.';
$text .= 'The following two links will be included within the video player in order to further promote Kaltura and its efforts in the open video space. ';
$text .= 'We hope that you will join us by including these links in the player.' . PHP_EOL;
$text .= l('open source video', 'http://corp.kaltura.com/download') . PHP_EOL . l('video platform', 'http://corp.kaltura.com/technology/');
$form['kaltura_credits']['kaltura_credits_text'] = array(
'#type' => 'item',
'#value' => nl2br($text),
);
$form['kaltura_credits']['kaltura_display_credits'] = array(
'#type' => 'checkbox',
'#title' => t('Include links in player'),
'#option' => t('Include links in player'),
'#default_value' => variable_get('kaltura_display_credits', 1),
);
$form['kaltura_help_message'] = array(
'#type' => 'item',
'#value' => t('Please feel free to ') . l('contact us', 'http://corp.kaltura.com/support/form/project/16', array(
'attributes' => array(
'target' => '_blank',
),
)) . t(' with any questions.'),
);
$form['#submit'][] = 'kaltura_settings_form_submit';
return system_settings_form($form);
}
function kaltura_settings_form_validate($form, &$form_state) {
if (empty($form_state['values']['kaltura_partner_cms_password'])) {
$form_state['values']['kaltura_partner_cms_password'] = variable_get('kaltura_partner_cms_password', '');
}
if (!valid_url($form_state['values']['kaltura_partner_url2'], TRUE)) {
form_set_error('kaltura_partner_url2', t('Notification URL should be a valid URL, but %partner_url is not.', array(
'%partner_url' => $form_state['values']['kaltura_partner_url2'],
)));
}
try {
$config = Kalturahelpers::getServiceConfiguration();
$config->partnerId = variable_get('kaltura_partner_id', '');
$config->serviceUrl = variable_get('kaltura_server_url', '');
$client = new KalturaClient($config);
$result = $client->partner
->getSecrets($config->partnerId, $form_state['values']['kaltura_partner_cms_email'], $form_state['values']['kaltura_partner_cms_password']);
drupal_set_message(t('Kaltura credentials verified against !kaltura_url.', array(
'!kaltura_url' => $config->serviceUrl,
)));
} catch (Exception $ex) {
$targs = array(
'%message' => $ex
->getMessage(),
'%file' => $ex
->getFile(),
'%line' => $ex
->getLine(),
'%code' => $ex
->getCode(),
);
form_set_error('', t('Kaltura credentials failed login: %message', $targs));
watchdog('kaltura', 'Unable to verify Kaltura credentials: %message', $targs, WATCHDOG_ERROR);
}
}
function kaltura_settings_form_submit($form, &$form_state) {
if ($form_state['values']['kaltura_notification_type']) {
}
if (isset($form_state['values']['kaltura_notification_type']) && $form_state['values']['kaltura_partner_url2']) {
$url2 = url('kaltura/notification_handler', array(
'absolute' => TRUE,
));
if ($form_state['values']['kaltura_notification_type'] == 1) {
$not_type = 3;
}
else {
$not_type = 2;
}
try {
$result = kaltura_update_kaltura_partner($not_type, $url2);
if (!$result->id) {
if (isset($result['error']['code'])) {
throw new Exception($result['error']['desc'], $result['error']['code']);
}
else {
throw new Exception('Updating notification settings on Kaltura server failed.');
}
}
if ($form_state['values']['last_notification_url'] != $form_state['values']['kaltura_partner_url2'] || $not_type == 3) {
variable_set('kaltura_notification_status', 0);
list($status, $text) = kaltura_test_notification_config();
if ($status == FALSE) {
drupal_set_message(t('Notification test failed. The URL you specified is not valid. Please make sure it is correct and save the settings again.'), 'error');
}
else {
}
}
} catch (Exception $ex) {
drupal_set_message(t('Error: %message when updating notification settings.', array(
'%message' => $ex
->getMessage(),
)), 'error');
watchdog('kaltura', 'Error @message updating notification settings.<br />Backtrace:<pre>!trace</pre>', array(
'@message' => $ex
->getMessage(),
'!trace' => $ex
->getTrace(),
));
}
}
}
function kaltura_form_alter(&$form, &$form_state, $form_id) {
if (variable_get('kaltura_partner_id', '') == '') {
if ($form_id == 'kaltura_settings_form') {
unset($form['buttons']['submit']);
unset($form['buttons']['preview']);
unset($form['buttons']['reset']);
}
}
}
function kaltura_import_entries_page() {
$foundFlag = TRUE;
$enabledFlag = FALSE;
$entries = array();
$mediaEntries = array();
$entries_need_import = array();
$each_page = 30;
$total = 0;
$mediaTotal = 0;
$finished = false;
$current_page = 1;
if ($_POST['op'] == 'import selected') {
kaltura_import_entries($_POST['entries_to_import']);
drupal_goto('admin/settings/kaltura/entries/import');
}
if (variable_get('kaltura_partner_id', '') == '') {
return t('You have not configured your partner details yet. Import cannot be performed until you setup your partner details');
}
$get_entries = module_exists('node_kaltura_entry');
$get_mixes = module_exists('node_kaltura_mix');
if (empty($_GET['p'])) {
$_GET['p'] = 1;
}
else {
$current_page = $_GET['p'];
}
while (!$finished) {
set_time_limit(30);
if ($get_mixes) {
$filter = new KalturaMixEntryFilter();
list($total, $entries) = kaltura_get_my_entries($filter, KalturaEntryType::MIX, $each_page, $current_page);
if ($total == 0) {
$foundFlag = FALSE;
}
$enabledFlag = TRUE;
}
if ($get_entries) {
$filter = new KalturaBaseEntryFilter();
list($mediaTotal, $mediaEntries) = kaltura_get_my_entries($filter, KalturaEntryType::MEDIA_CLIP, $each_page, $current_page);
if ($mediaTotal == 0) {
$foundFlag = FALSE;
}
$enabledFlag = TRUE;
}
if (!$enabledFlag) {
return t('Can\'t import entries, you did not enable the node_kaltura_entries module.');
}
if ($entries === NULL) {
return t('No entries found.');
}
if (is_array($mediaEntries)) {
$entries_need_import = array_merge($entries_need_import, $mediaEntries);
}
if (is_array($entries)) {
$entries_need_import = array_merge($entries_need_import, $entries);
}
$entries_need_import = kaltura_remove_existing_entries($entries_need_import);
if (count($entries_need_import) >= $each_page) {
$finished = TRUE;
}
$shown = $current_page * $each_page;
if ($mediaTotal < $shown && $total < $shown) {
$finished = TRUE;
}
$current_page++;
}
if (!count($entries_need_import)) {
return t('No entries needed to be imported.');
}
if (count($entries_need_import) > $each_page) {
$multi_dim = array_chunk($entries_need_import, $each_page, true);
$entries_need_import = $multi_dim[0];
}
if ($total + $mediaTotal > $each_page) {
$pages = ceil(($total + $mediaTotal) / $each_page);
for ($start = 1; $start <= $pages; $start++) {
if ($start == $_GET['p']) {
$pager .= '<a>' . $start . '</a>';
}
else {
$pager .= l($start, 'admin/settings/kaltura/entries/import', array(
'query' => 'p=' . $start,
));
}
$pager .= ' ';
}
}
if (is_array($entries_need_import) && count($entries_need_import)) {
foreach ($entries_need_import as $entry => $details) {
$entries_options[$details['id']] = $details['id'] . ': ' . $details['name'] . ' (' . ($details['type'] == 2 ? 'mix' : 'entry') . ')';
}
}
else {
$entries_options = array();
}
$output = drupal_get_form('kaltura_create_entries_form', array(
$entries_options,
));
return $pager . $output . $pager;
}
function kaltura_import_entries($entries) {
$entries_list = implode(',', $entries);
$my_entries = kaltura_get_entries($entries_list, count($entries), true);
foreach ($my_entries->objects as $entry) {
kaltura_import_entry($entry);
}
drupal_set_message('<b>Please Note: All imported nodes are not yet published (node status 0)</b>');
}
function kaltura_import_entry($entry_details) {
global $user;
$node = new stdClass();
if ($entry_details->type == 1) {
$type = 'entry';
}
elseif ($entry_details->type == 2) {
$type = 'mix';
}
else {
$type = '';
}
$node->type = 'kaltura_' . $type;
$node->title = $entry_details->name;
$node->kaltura_entryId = $entry_details->id;
$node->uid = $user->uid;
$node->body = '';
$node->status = 0;
$node->created = time();
$node->comment = FALSE;
$node->changed = time();
$node->promote = 0;
$node->sticky = 0;
$node->kstatus = $entry_details->status;
$node->kaltura_entry_data = serialize($entry_details);
$node->kaltura_tags = $entry_details->tags;
$node->kaltura_admin_tags = '';
$node->kaltura_media_type = $entry_details->mediaType;
$node->kaltura_duration = $entry_details->duration;
$node->kaltura_thumbnail_url = $entry_details->thumbnailUrl;
$node->kaltura_partner_data = 'imported ' . $type;
$node->kaltura_width = $entry_details->width;
$node->kaltura_height = $entry_details->height;
$node->kaltura_download_url = $entry_details->downloadUrl;
$node->kaltura_views = $entry_details->views;
$node->kaltura_plays = $entry_details->plays;
$node->kaltura_votes = $entry_details->votes;
$node->kaltura_rank = $entry_details->rank;
$node->kaltura_total_rank = $entry_details->totalRank;
node_save($node);
module_invoke('node_kaltura_' . $type, 'hook_insert', $node);
drupal_set_message('Kaltura item imported: ' . l($node->title, 'node/' . $node->nid) . ' (id: ' . $node->kaltura_entryId . ')');
}
function kaltura_create_entries_form($form) {
$args = func_get_args();
$entries_options = $args[1][0];
$form['entries_to_import'] = array(
'#type' => 'checkboxes',
'#title' => t('Choose the entries you want to import'),
'#options' => $entries_options,
);
$form['import_now'] = array(
'#type' => 'submit',
'#title' => t('import selected'),
'#value' => t('import selected'),
);
return $form;
}
function kaltura_get_my_entries($filter, $type, $page_size = 30, $page = 1) {
$isAdmin = 1;
$session_user = KalturaHelpers::getSessionUser();
$kaltura_client = KalturaHelpers::getKalturaClient($isAdmin);
$pager_filter = new KalturaFilterPager();
$pager_filter->pageIndex = $page;
$pager_filter->pageSize = $page_size;
switch ($type) {
case KalturaEntryType::MIX:
$result = $kaltura_client->mixing
->listAction($filter, $pager_filter);
break;
case KalturaEntryType::MEDIA_CLIP:
$filter->TypeEqual = KalturaEntryType::MEDIA_CLIP;
$result = $kaltura_client->baseEntry
->listAction($filter, $pager_filter);
break;
}
if ($result->totalCount > 0) {
$count = $result->totalCount;
if (is_array($result->objects) && count($result->objects)) {
foreach ($result->objects as $entry) {
$returned_entries[$entry->id] = array(
'id' => $entry->id,
'name' => $entry->name,
'type' => $entry->type,
);
}
}
return array(
$count,
$returned_entries,
);
}
else {
return array(
0,
array(),
);
}
}
function kaltura_remove_existing_entries($entries) {
$count = 0;
$new_entries = array();
if (is_array($entries) && count($entries)) {
foreach ($entries as $entry) {
$sel = 'SELECT COUNT(nid) n FROM {node_kaltura} WHERE kaltura_entryId = \'%s\'';
$result = db_fetch_object(db_query($sel, $entry['id']));
if ($result->n == 0) {
$new_entries[] = $entry;
}
else {
$count++;
}
}
}
return $new_entries;
}
function kaltura_test_cron_job_status() {
$cron_last = variable_get('cron_last', NULL);
if (!$cron_last) {
return array(
FALSE,
t('cron has not run.') . ' ' . t('If you do not configure cron to run regularly, statistics about kaltura items will not be updated.'),
);
}
else {
return array(
TRUE,
t('cron has run.') . ' ' . t('Verify that you have a cron job configured, so you don\'t need to run it manually.') . '<br />' . t('Statistics about kaltura items will only be updated at cron run.'),
);
}
}
function kaltura_test_notification_config() {
return array(
TRUE,
t('Test call not yet implemented; see !test_call_issue', array(
'!test_call_issue' => l('[#1025090]', 'http://drupal.org/node/' . 1025090),
)),
);
}
function kaltura_variable_get_real($var, $default) {
$sql = "SELECT * FROM {variable} WHERE name = '%s'";
$result = db_fetch_object(db_query($sql, $var));
if (!$result) {
return $default;
}
return unserialize($result->value);
}
function kaltura_alternate_xml_verification($filename) {
return array(
TRUE,
t('crossdomain.xml is in place and seems to be OK'),
);
}
function kaltura_test_crossdomain() {
global $base_url;
$cd_req = drupal_http_request($base_url . '/crossdomain.xml');
if ($cd_req->code == '200') {
return kaltura_parse_crossdomain($cd_req->data);
}
else {
$filename = $_SERVER['DOCUMENT_ROOT'] . '/crossdomain.xml';
if ($cd_xml = file_get_contents($filename)) {
return kaltura_parse_crossdomain($cd_xml);
}
else {
return array(
FALSE,
t('crossdomain.xml file could not be found in your site\'s root directory') . ' [' . $filename . ']' . t('Please read ') . l('here', 'admin/help/kaltura'),
);
}
}
}
function kaltura_parse_crossdomain($crossdomain_xml) {
if (!function_exists('simplexml_load_string')) {
return kaltura_alternate_xml_verification($filename);
}
$cd_xml_content = simplexml_load_string($crossdomain_xml);
foreach ($cd_xml_content
->children() as $child) {
$atts = $child
->attributes();
if ($atts['domain'] == '*' || $atts['domain'] == '*.kaltura.com') {
$kaltura_ok = TRUE;
}
if ($atts['headers'] == '*' && ($atts['domain'] == '*' || $atts['domain'] == '*.kaltura.com')) {
$headers_ok = TRUE;
}
}
if ($kaltura_ok && $headers_ok) {
return array(
TRUE,
t('crossdomain.xml is in place and seems to be OK'),
);
}
else {
return array(
FALSE,
t('crossdomain.xml is in place, but it is probably not configured properly') . '<br />' . t('Please read ') . l('here', 'admin/help/kaltura'),
);
}
}
function kaltura_notifications_settings_form() {
global $base_url;
$notification_types = array(
'2' => 'only client side notifications',
'3' => 'both server & client notifications',
);
$notification_type_description = t('Enabling server notifications allows the Kaltura video platform to update your Drupal server about the status of ' . 'the media content. This is especially useful when uploading content in various formats that needs to be transcoded. ' . 'In order to support server side notifications, your server must be publicly available on the internet.');
$form['kaltura_notification_type'] = array(
'#type' => 'checkbox',
'#options' => 'Enable server side notifications',
'#description' => $notification_type_description,
'#title' => t('Enable server side notifications'),
'#default_value' => variable_get('kaltura_notification_type', 0),
);
$form['kaltura_partner_url2'] = array(
'#type' => 'textfield',
'#title' => 'Notification URL',
'#description' => t('Your Drupal site base URL; path %notification_path will be appended.', array(
'%notification_path' => 'kaltura/notification_handler',
)),
'#default_value' => variable_get('kaltura_partner_url2', $base_url),
);
$form['last_notification_url'] = array(
'#type' => 'hidden',
'#value' => variable_get('kaltura_partner_url2', ''),
);
return $form;
return system_settings_form($form);
}
function kaltura_notifications_settings_form_submit() {
}
function kaltura_check_server_status($admin) {
try {
$result = KalturaHelpers::getKalturaClient($admin);
$kaltura_status = TRUE;
$kaltura_error = "";
} catch (Exception $ex) {
$kaltura_status = FALSE;
$kaltura_error = $ex
->getMessage();
}
return array(
$kaltura_status,
$kaltura_error,
);
}
function kaltura_generate_cms_password() {
$pass = '';
$pass = md5(time() . $_SERVER['REMOTE_ADDR']);
$pass = drupal_substr($pass, 0, 8);
return $pass;
}