You are here

kaltura.admin.inc in Kaltura 5

File

includes/kaltura.admin.inc
View source
<?php

/*
 * @file
 * contains functions for administration use of the kaltura core module
 *
 */

/*
 * callback function for admin/settings/kalturas
 *
 * returns HTML with links to all configuration pages of the module
 */
function kaltura_settings_main() {
  $items = array();
  $items[] = l('Server Integration settings', 'admin/settings/kaltura/general');
  if (module_exists('node_kaltura_entry')) {
    $items[] = l('Media Node Settings', 'admin/settings/kaltura/kaltura_entry_settings');
  }
  if (module_exists('node_kaltura_mix')) {
    $items[] = l('Media Mix Settings', 'admin/settings/kaltura/kaltura_mix_settings');
  }
  if (module_exists('kaltura_comments')) {
    $items[] = l('Video Comments Settings', 'admin/settings/kaltura/kaltura_comments_settings');
  }
  if (module_exists('kaltura_playlist')) {
    $items[] = l('Kaltura Playlist Settings', 'admin/settings/kaltura/kaltura_playlist_settings');
  }
  $items[] = l('Kaltura Credits Settings', 'admin/settings/kaltura/kaltura_credits_settings');
  $items[] = l('List of Kaltura Items', 'kaltura/entries');
  return theme_item_list($items);
}
function kaltura_credits_settings_form() {
  $form = array();
  $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/') . PHP_EOL . l('open source player', 'http://corp.kaltura.com/technology/video_publishing');
  $form['kaltura_credits_text'] = array(
    '#type' => 'item',
    '#value' => nl2br($text),
  );
  $form['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),
  );
  return system_settings_form($form);
}

/*
 * helper function that process the partner registration with kaltura
 *
 * if registration successful, the partner details are saved as system variables (drupal DB) and
 * the user is being redirected to the modules settings page where he should see the details inserted
 * and a configuration test which says that the configuraion works
 */
function register_kaltura_partner() {
  global $base_url;
  $partner = new KalturaPartner();
  $partner->name = $_REQUEST['partner_name'];
  $partner->url1 = $_REQUEST['partner_url1'];
  $partner->url2 = $_REQUEST['partner_url2'] . '/?q=kaltura/notification_handler';
  $partner->adminName = $_REQUEST['partner_adminName'];
  $partner->adminEmail = $_REQUEST['partner_adminEmail'];
  $partner->description = $_REQUEST['partner_description'];
  $partner->appearInSearch = '1';

  // update relevant field with $_REQUEST['kaltura_adult_content']
  $content_categories = new KalturaContentCategories();
  foreach ($_REQUEST['kaltura_content_categories'] as $selected) {
    $categories .= $content_categories->categories[$selected] . ',';
  }
  $categories .= $_REQUEST['kaltura_adult_content'] == 1 ? 'adult' : 'non-adult';
  $categories = rtrim($categories, ',');
  $partner->contentCategories = $categories;
  $session_user = KalturaHelpers::getSessionUser();
  $config = KalturaHelpers::getServiceConfiguration();
  $kaltura_client = new KalturaClient($config);
  watchdog('regis', print_r($partner, TRUE));
  $res = $kaltura_client
    ->registerPartner($session_user, $partner, $_REQUEST['cms_password']);
  $result = $res['result']['partner']['id'];
  if ($result) {
    variable_set('kaltura_partner_id', $res['result']['partner']['id']);
    variable_set('kaltura_subp_id', $res['result']['subp_id']);
    variable_set('kaltura_secret', $res['result']['partner']['secret']);
    variable_set('kaltura_admin_secret', $res['result']['partner']['adminSecret']);
    variable_set('kaltura_local_registration', 1);
    variable_set('kaltura_server_url', KalturaHelpers::getKalturaServerUrl());
    variable_set('kaltura_partner_cms_email', $partner->adminEmail);
    variable_set('kaltura_partner_cms_password', $_REQUEST['cms_password']);
    variable_set('kaltura_partner_url2', $_REQUEST['partner_url2']);
    variable_set('kaltura_notification_type', 0);
    variable_set('kaltura_content_categories', implode(',', $_REQUEST['kaltura_content_categories']));
    kaltura_update_kaltura_partner(2, $partner->url2);
    kaltura_create_widgets_for_partner($pid, $secret);
    drupal_set_message('Congratulations!  You have successfully installed the All in One Video Module and registered for the Kaltura Partner Program.');
    drupal_goto('admin/settings/kaltura/general');
  }
  else {

    //$msg = 'There was an error while trying to register new partner.<br />'. str_replace('_', ' ', $res['error'][0]['code']) .': '. $res['error'][0]['desc'];
    $msg = 'The email address you entered already belongs to a registered Kaltura partner.<br />' . 'Please register with another email or <a href="#" onclick="switch_to_exist_partner();return false;">click here</a> to import your existing partner details';
    drupal_set_message($msg, 'error');
  }
}

/*
 * helper function to retrieve partner's details from kaltura
 */
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);
  $result = $kaltura_client
    ->getPartner($session_user, $cms_email, $cms_password, $partner_id);
  if ($result['result']['partner']['id']) {
    return array(
      $result['result']['partner']['id'],
      $result['result']['subp_id'],
      $result['result']['partner']['url2'],
      $result['result']['partner']['secret'],
      $result['result']['partner']['adminSecret'],
    );
  }
  else {
    return array(
      FALSE,
      $result['error'],
    );
  }
}

/*
 * helper function that uses Kaltura's getDefaultWidget service to create some default widgets for
 * the partner, in case he does not have these yet
 * 
 */
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) {
      $result = $kaltura_client
        ->getDefaultWidget($session_user, $confs['view_uiconf']);
      $result = $kaltura_client
        ->getDefaultWidget($session_user, $confs['remix_uiconf']);
    }
  }
}

/*
 * determine how the general settings form will look like.
 *
 * returns one of 2 states of forms:
 * 1) registration form (if variables do not exist in the DB)
 * 2) notifications settings + configuration test + settings form
 * 
 */
function kaltura_settings_form() {
  global $base_url;
  if (variable_get('kaltura_partner_id', '') != '') {
    $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_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 = ' kaltura_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>',
    );
  }
  $register_visible = FALSE;
  if ($_REQUEST['partner_name'] && $_REQUEST['op'] == 'Complete Installation') {
    if ($_REQUEST['kaltura_agree_terms'] == 1) {
      register_kaltura_partner();
    }
    else {
      drupal_set_message('You must agree to the terms of use', 'error');
    }
  }
  if ($_REQUEST['op'] == 'Complete Installation' && $_REQUEST['kaltura_partner_id']) {
    list($pid, $subpid, $url2, $secret, $admin_secret) = kaltura_get_partner_details($_REQUEST['kaltura_partner_id'], $_REQUEST['cms_email'], $_REQUEST['cms_password']);
    if ($pid) {
      variable_set('kaltura_partner_id', $pid);
      variable_set('kaltura_subp_id', $subpid);
      variable_set('kaltura_secret', $secret);
      variable_set('kaltura_admin_secret', $admin_secret);
      variable_set('kaltura_partner_url2', str_replace('/?q=kaltura/notification_handler', '', $url2));
      variable_set('kaltura_partner_cms_email', $_REQUEST['cms_email']);
      variable_set('kaltura_partner_cms_password', $_REQUEST['cms_password']);
      variable_set('kaltura_server_url', KalturaHelpers::getKalturaServerUrl());
      drupal_set_message('Your partner details were imported successfully. please check the ' . l('notification settings', 'admin/settings/kaltura/general/notifications'));
      drupal_set_message('You can ' . l('import Kaltura Media from your existing partner', 'admin/settings/kaltura/import'));
      kaltura_create_widgets_for_partner($pid, $secret);
      kaltura_update_kaltura_partner(2, $url2);
      drupal_goto('admin/settings/kaltura/general');
    }
    else {
      drupal_set_message('Couldn\'t import partner details.<br />' . print_r($subpid, TRUE) . $subpid['code'] . ': ' . $subpid['desc'], 'error');
    }
  }
  if (variable_get('kaltura_partner_id', '') == '') {
    $register_visible = TRUE;
    if (!variable_get('kaltura_registration_first_load', FALSE)) {
      variable_set('kaltura_registration_first_load', TRUE);
    }
    global $user;
    $information_text = 'Once you complete the form below and click "Complete installation", ' . 'the All in One Video Module will be fully installed and ready to use.<br />' . PHP_EOL;
    $registered = '<br /><a href="#" onclick="switch_to_exist_partner();return false;">Click here if you already have a Partner ID</a>';
    $not_registered = 'If you are not registered as a Kaltura partner, and would like to register ' . '<input type="button" onclick="switch_to_register();" value="Click Here" />';
    $form['kaltura_settings_information'] = array(
      '#value' => t($information_text) . $registered,
    );
    if ($_GET['register'] !== 'no') {
      drupal_set_title('All in One Video Module by Kaltura Installation');

      // registration form
      $showing_reg = TRUE;
      $form['kaltura_registration'] = array(
        '#type' => 'fieldset',
        '#title' => t('Registration'),
        '#collapsible' => FALSE,
        '#collapsed' => FALSE,
      );
      $form['kaltura_registration']['partner_name'] = array(
        '#type' => 'textfield',
        '#title' => t('Website Name'),
        '#default_value' => variable_get('site_name', ''),
        '#size' => 100,
        '#maxlength' => 256,
      );
      $form['kaltura_registration']['partner_url1'] = array(
        '#type' => 'hidden',
        '#title' => t('Your Website Address'),
        '#default_value' => $base_url,
        '#size' => 100,
        '#maxlength' => 1024,
      );
      $form['kaltura_registration']['partner_url2'] = array(
        '#type' => 'hidden',
        '#title' => t('URL 2'),
        '#default_value' => $base_url,
        '#maxlength' => 1024,
      );
      $form['kaltura_registration']['partner_adminName'] = array(
        '#type' => 'textfield',
        '#title' => t('Administrator Name'),
        '#default_value' => $user->name,
        '#size' => 100,
        '#maxlength' => 50,
      );
      $form['kaltura_registration']['partner_adminEmail'] = array(
        '#type' => 'textfield',
        '#title' => t('Administrator Email'),
        '#default_value' => $user->mail,
        '#size' => 100,
        '#maxlength' => 100,
      );
      $categories = new KalturaContentCategories();
      $form['kaltura_registration']['kaltura_content_categories'] = array(
        '#type' => 'select',
        '#title' => t('Content Categories'),
        '#multiple' => TRUE,
        '#size' => 8,
        '#options' => $categories->categories,
      );
      $form['kaltura_registration']['kaltura_adult_content'] = array(
        '#type' => 'radios',
        '#title' => t('Do you plan to display adult content?'),
        '#options' => array(
          1 => 'Yes',
          0 => 'No',
        ),
        '#default_value' => variable_get('kaltura_adult_content', 0) == 0 ? 0 : 1,
      );
      $form['kaltura_registration']['partner_description'] = array(
        //'#type' => 'textarea',
        '#type' => 'hidden',
        '#title' => t('Partner Description'),
        '#default_value' => 'Drupal module|' . VERSION,
      );
      $form['kaltura_registration']['cms_password'] = array(
        '#type' => 'hidden',
        '#title' => t('CMS password'),
        '#default_value' => kaltura_generate_cms_password(),
        '#size' => 20,
        '#maxlength' => 60,
      );
      $form['kaltura_registration']['kaltura_agree_terms'] = array(
        '#type' => 'checkbox',
        '#title' => 'I agree to comply with the <a href="http://www.kaltura.com/index.php/corp/tandc" target="_blank">Kaltura Terms of Use</a>',
        '#options' => 'Agree',
        '#required' => TRUE,
      );
      $form_state['storage']['submit'] = 'Register';
      $form['kaltura_registration']['buttons']['register'] = array(
        '#type' => 'button',
        '#title' => 'Complete Installation',
        '#value' => 'Complete Installation',
        '#default_value' => 'Complete Installation',
        '#weight' => 40,
      );
    }
  }
  if ($_GET['register'] === 'no' || !$showing_reg && !isset($_GET['register'])) {

    // normal settings form
    if (variable_get('kaltura_partner_id', '') == '') {
      drupal_set_title('All in One Video Module by Kaltura Installation');
      $forgot_link_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,
    );

    /*
    $form['kaltura_partner_settings']['kaltura_exitss_switch'] = array(
      '#value' => $not_registered,
    );
    */
    $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,
    );
    $form['kaltura_partner_settings']['cms_email'] = array(
      '#type' => 'textfield',
      '#title' => t('Email'),
      '#default_value' => variable_get('kaltura_partner_cms_email', ''),
    );
    $form['kaltura_partner_settings']['cms_password'] = array(
      '#type' => 'textfield',
      '#title' => t('Password'),
      '#default_value' => variable_get('kaltura_partner_cms_password', ''),
    );
    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,
    );
  }
  $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(
      'target' => '_blank',
    )) . t(' with any questions.'),
  );
  return system_settings_form($form);
}

/*
 * Implementation of hook_form_alter().
 */
function kaltura_form_alter($form_id, &$form) {
  if (variable_get('kaltura_partner_id', '') == '') {
    if ($form_id == 'kaltura_settings_form') {
      unset($form['buttons']['submit']);
      unset($form['buttons']['preview']);
      unset($form['buttons']['reset']);
    }
  }
}

/*
 * callback function for url admin/settings/kaltura/import
 */
function kaltura_import_entries_page() {
  if ($_POST['op'] == 'import selected') {
    kaltura_import_entries($_POST['entries_to_import']);
    drupal_goto('admin/settings/kaltura/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');
  $filter = new KalturaEntryFilter();
  if ($get_entries) {
    if ($get_mixes) {
      $filter->inType = '1,2';
    }
    else {
      $filter->inType = '1';
    }
  }
  else {
    return t('Can\'t import entries, you did not enable the node_kaltura_entries module.');
  }
  if (!$_GET['p']) {
    $_GET['p'] = 1;
  }
  list($total, $each_page, $entries) = kaltura_get_my_entries($filter, $each_page = 30, $_GET['p']);
  if ($entries === NULL) {
    return t('No entries found.');
  }
  $entries_need_import = kaltura_remove_existing_entries($entries);
  if (!count($entries_need_import)) {
    return t('No entries needed to be imported.');
  }
  if (count($entries_need_import) < $each_page) {
    list($t, $n, $second_page) = kaltura_get_my_entries($filter, $each_page, $_GET['p'] + 1);
    $new_entries_need_import = kaltura_remove_existing_entries($second_page);
    if (is_array($new_entries_need_import) && $second_page !== NULL) {
      foreach ($new_entries_need_import as $eid => $entry) {
        $entries_need_import[$eid] = $entry;
        if (count($entries_need_import) == $each_page) {
          break;
        }
      }
    }
  }
  if ($total > $each_page) {
    $pages = floor($total / $each_page);
    for ($start = 1; $start <= $pages; $start++) {
      if ($start == $_GET['p']) {
        $pager .= '<a>' . $start . '</a>';
      }
      else {
        $pager .= l($start, 'admin/settings/kaltura/import', array(), 'p=' . $start);
      }
      $pager .= '&nbsp;';
    }
  }
  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;
}

/*
 * helper function that uses kaltura_get_entries() to retreive a list of specific entries
 * belongs to the partner, and call kaltura_import_entry() for each of the retreived list
 */
function kaltura_import_entries($entries) {
  $entries_list = implode(',', $entries);
  $my_entries = kaltura_get_entries($entries_list);
  $my_entries = $my_entries['entries'];
  foreach ($my_entries as $entry => $details) {
    kaltura_import_entry($details);
  }
  drupal_set_message('<b>Please Note: All imported nodes are not yet published (node status 0)</b>');
}

/*
 * helper function that gets an entry retreived from Kaltura
 * builds a node object according to entry, and invokes node_insert to save the node
 */
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['length_in_msecs'] ? (int) ($entry_details['length_in_msecs'] / 1000) : $entry_details['duration'];
  $node->kaltura_thumbnail_url = $entry_details['thumbnailUrl'];
  $node->kaltura_partner_data = 'imported ' . $type;
  $node->kaltura_source = $entry_details['source'];
  $node->kaltura_source_id = $entry_details['sourceId'];
  $node->kaltura_source_link = $entry_details['sourceLink'];
  $node->kaltura_width = $entry_details['width'];
  $node->kaltura_height = $entry_details['height'];
  $node->kaltura_download_url = $entry_details['downloadUrl'];
  $node->kaltura_media_date = $entry_details['mediaDate'];
  $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['total_rank'];
  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 . ')');
}

/*
 * helper function to provide form fields containing all entries for import
 * each entry as checkbox option
 */
function kaltura_create_entries_form($form) {
  $args = func_get_args();
  $form = array();
  $entries_options = $args[0][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;
}

/*
 * helper functuion to get all entries of partner from Kaltura's DB
 */
function kaltura_get_my_entries($filter, $page_size = 30, $page = 1) {
  if (!is_object($filter) && gettype($filter) == 'KalturaEntryFilter') {
    $filter = new KalturaEntryFilter();
  }
  $session_user = KalturaHelpers::getSessionUser();
  $kaltura_client = KalturaHelpers::getKalturaClient();
  $result = $kaltura_client
    ->listMyEntries($session_user, $filter, 0, $page_size, $page);
  if ($result['result']['count'] > 0) {
    $count = $result['result']['count'];
    $each_page = $result['result']['page_size'];
    if (is_array($result['result']['entries']) && count($result['result']['entries'])) {
      foreach ($result['result']['entries'] as $entry) {
        $returned_entries[$entry['id']] = array(
          'id' => $entry['id'],
          'name' => $entry['name'],
          'type' => $entry['type'],
        );
      }
    }
    return array(
      $count,
      $each_page,
      $returned_entries,
    );
  }
  else {
    return NULL;
  }
}

/*
 * helper function that recieves a list of entries, and returns a new list
 * which does not contain entries that are already represented as nodes
 */
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;
}

/*
 * helper function that checks the drupal cron job status
 *
 * cron job is required to collect statictics about kaltura items
 */
function kaltura_test_cron_job_status() {
  $cron_last = variable_get('cron_last', NULL);
  if (!$cron_last) {
    return array(
      FALSE,
      'cron has not run. If you don\'t configure cron job, statistics about kaltura items will not be updated.',
    );
  }
  else {
    return array(
      TRUE,
      'cron has run. Be sure that you have a cron job configured, so you don\'t need to run it manually.<br />' . 'Statistics about kaltura items will only be updated at cron run.',
    );
  }
}

/*
 * helper function that tests the notification status of the module
 *
 * this function tries to close a loop with the kaltura sevrer in a "real-time" test.
 * The notification handler sets a variable in the DB before the this function run ends
 * therefore we need to use the kaltura_variable_get_real() function to check the variable status in the DB
 * and not in the $_GLOBALS which are set before this function even start
 */
function kaltura_test_notification_config() {
  if (kaltura_variable_get_real('kaltura_notification_status', 0) == 0) {
    $session_user = KalturaHelpers::getSessionUser();
    $kaltura_client = KalturaHelpers::getKalturaClient();
    $result = $kaltura_client
      ->testNotification($session_user);
    sleep(3);
  }
  if (kaltura_variable_get_real('kaltura_notification_status', 0) === 1) {
    return array(
      TRUE,
      'Drupal server receives notification from Kaltura server.',
    );
  }
  return array(
    FALSE,
    'Drupal server fails to receive notifications from Kaltura. Please fix your configuration or disable server notifications.',
  );
}

/*
 * replace default Drupal's variable_get() function with a "real-time" one
 *
 * This function checks a variable status in the DB and not in the $_GLOBALS
 *
 */
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 alternate_xml_verification($filename) {
  return array(
    TRUE,
    t('crossdomain.xml is in place and seems to be OK'),
  );
}

/*
 * helper function that checks if crossdomain.xml is in the right place
 */
function kaltura_test_crossdomain() {
  $filename = $_SERVER['DOCUMENT_ROOT'] . '/crossdomain.xml';
  $cd_xml = @fopen($filename, 'r');
  if ($cd_xml && filesize($filename) > 0 && file_exists($filename)) {
    if (!function_exists('simplexml_load_file')) {
      $result = alternate_xml_verification($filename);
      return $result;
    }
    $cd_xml_content = simplexml_load_file($filename);
    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 good') . '<br />' . t('Please read ') . l('here', 'admin/help/kaltura'),
      );
    }
  }
  else {
    return array(
      FALSE,
      t('crossdomain.xml file could not be found in your site\'s root directory') . ' [' . $filename . ']',
    );
  }
}

/*
 * helper function that updates partner notification settings in kaltura's server
 */
function kaltura_update_kaltura_partner($notifications_config = 2, $notification_url = '') {
  $partner = new KalturaPartner();
  $partner->allowMultiNotification = '1';
  $partner->notify = 1;
  $partner->notificationsConfig = '*=' . $notifications_config;
  $url = url('kaltura/notification_handler', 'absolute=TRUE');
  $notification_url = $notification_url == '' ? $url : $notification_url;
  $partner->url2 = $notification_url;
  $session_user = KalturaHelpers::getSessionUser();
  $kaltura_client = KalturaHelpers::getKalturaClient(1);
  if ($kaltura_client) {
    $result = $kaltura_client
      ->updatePartner($session_user, $partner);
  }
  if ($result['result']) {
    return $result['result'];
  }
  return $result['error'];
}

/*
 * Settings form in URL admin/settings/kaltura/general/notifications
 */
function kaltura_notifications_settings_form() {
  global $base_url;
  $notification_types = array(
    //'1' => 'only server side notifications',
    '2' => 'only client side notifications',
    '3' => 'both server & client notifications',
  );
  if (isset($_REQUEST['kaltura_notification_type']) && $_REQUEST['kaltura_partner_url2']) {

    // TODO: update partner settings with notification type
    $url2 = $_REQUEST['kaltura_partner_url2'] . '/?q=kaltura/notification_handler';
    if ($_REQUEST['kaltura_notification_type'] == 1) {
      $not_type = 3;
    }
    else {
      $not_type = 2;
    }
    $result = kaltura_update_kaltura_partner($not_type, $url2);
    if ($_REQUEST['last_notification_url'] != $_REQUEST['kaltura_partner_url2']) {
      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 {
        drupal_set_message(t('Notification test was successful.'));
      }
    }
    if (!$result['partner']['id']) {
      drupal_set_message('Updating kaltura\'s server faild.<br />' . $result['error']['code'] . ': ' . $result['error']['desc'], 'error');
    }
    else {
      drupal_set_message('Updating kaltura\'s server was successful ' . l($url2, $url2));
    }
  }
  $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),
  );
  $not_url = variable_get('kaltura_partner_url2', '') != '' ? variable_get('kaltura_partner_url2', '') : $base_url;
  $form['kaltura_partner_url2'] = array(
    '#type' => 'textfield',
    '#title' => 'Notification URL',
    '#description' => t('Your drupal base URL.') . ' /?q=kaltura/notification_handler ' . t('will be added to this base URL.'),
    '#default_value' => $not_url,
  );
  $form['last_notification_url'] = array(
    '#type' => 'hidden',
    '#value' => variable_get('kaltura_partner_url2', ''),
  );
  return $form;
  return system_settings_form($form);
}

/*
 * helper function that tries to open session with the kaltura server
 *
 * used to verify partner details, and display errors if there are
 */
function kaltura_check_server_status($admin) {
  $session_user = KalturaHelpers::getSessionUser();
  $config = KalturaHelpers::getServiceConfiguration();
  $kaltura_client = new KalturaClient($config);
  if ($admin == 1) {
    $result = $kaltura_client
      ->startSession($session_user, variable_get('kaltura_secret', ''));
  }
  else {
    $result = $kaltura_client
      ->startSession($session_user, variable_get('kaltura_admin_secret', ''), 1);
  }
  if ($result['error']) {
    $kaltura_status = FALSE;
    $kaltura_error = $result['error'][0]["code"] . ' ' . $result['error'][0]["desc"];
  }
  else {
    $kaltura_status = TRUE;
    $kaltura_error = "";
  }
  return array(
    $kaltura_status,
    $kaltura_error,
  );
}

/*
 * helper function to generate a cms password
 *
 * cms is the "administration panel" for kaltura's partners in kaltura's website
 */
function kaltura_generate_cms_password() {
  $pass = '';
  $pass = md5(time() . $_SERVER['REMOTE_ADDR']);
  $pass = drupal_substr($pass, 0, 8);
  return $pass;
}