You are here

function radioactivity_http_port_edit in Radioactivity 5

1 string reference to 'radioactivity_http_port_edit'
radioactivity_http_menu in plugins/radioactivity_http.module

File

plugins/radioactivity_http.module, line 123

Code

function radioactivity_http_port_edit($port_id) {
  global $base_url;
  $form = array();
  if ($port_id == 'new') {

    // this is a new port
    $form[] = array(
      '#value' => '<h3>' . t('Create new port') . '</h3>',
    );
  }
  else {
    $form[] = array(
      '#value' => '<h3>' . t('Edit port %id', array(
        '%id' => $port_id,
      )) . '</h3>',
    );
  }
  $ports = _radioactivity_http_get_ports();
  $port = $ports[$port_id];
  $form['port_id'] = array(
    '#type' => 'hidden',
    '#default_value' => $port_id,
  );
  $form['path'] = array(
    '#type' => 'textfield',
    '#required' => TRUE,
    '#title' => t('Path of the access point'),
    '#description' => t('Define the path of the port access point. The exposed methods will be accessed by ' . '<code>' . $base_url . '/&lt;path&gt;/&lt;method_name&gt;</code>. ' . 'Do not add trailing slash.'),
    '#default_value' => $port['path'],
  );
  $form['security_scheme'] = array(
    '#type' => 'select',
    '#title' => t('Security scheme for the port'),
    '#description' => t('Choose security scheme for the port. The available schemes are:') . '<dl>' . '<dt>' . t('None') . '</dt>' . '<dd>' . t('No security. Choose this only if you have secured the access some other way.') . '</dd>' . '<dt>' . t('MD5 sign with private key') . '</dt>' . '<dd>' . t('MD5-based private key signing of method access. The signature is created by ' . 'the following formula: ') . "<code>md5('&lt;method_name&gt;,&lt;arg1_name=arg1_value&gt;,...,&lt;argN_name=argN_value&gt;,&lt;pkey&gt')</code>. " . t('The lowercase signature is the appended to the query as a query parameter <code>s</code>. Note that ' . 'the parameters are listed in <em>signature</em> order which may differ from the actual invocation order.') . '</dl>',
    '#default_value' => $port['security_scheme'],
    '#options' => array(
      'none' => t('None'),
      'pkey-md5' => t('MD5 sign with private key'),
    ),
  );
  $form['private_key'] = array(
    '#type' => 'textfield',
    '#title' => t('Private key'),
    '#description' => t('The private key is used by some security schemes.'),
    '#default_value' => $port['private_key'],
  );
  $form['access_method'] = array(
    '#type' => 'select',
    '#title' => t('Access method'),
    '#description' => t('Choose accessing method for the operations. The available access methods are:') . '<dl>' . '<dt>' . t('Method + query params') . '</dt>' . '<dd>' . t('Method is invoked by HTTP GET to <code>&lt;port_url&gt;/&lt;method_name&gt;?arg1_name=arg1_value&...argN_name=argN_value</code> .') . '</dd>' . '</dl>',
    '#default_value' => $port['access_method'],
    '#options' => array(
      'method-and-query' => t('Method + query params'),
    ),
  );
  $form['return'] = array(
    '#type' => 'select',
    '#title' => t('Return value encoding'),
    '#description' => t('Select return value encoding.'),
    '#default_value' => $port['return'],
    '#options' => array(
      'php-serialize' => t('PHP serialize()'),
    ),
  );
  $missing_functions = array();
  if (function_exists('json_encode')) {
    $form['return']['#options']['json'] = t('JSON');
  }
  else {
    $missing_functions[] = 'json_encode';
  }
  $options = array();
  foreach (_radioactivity_http_get_method_signatures() as $method => $signature) {
    $options[$method] = $method . '(' . implode(', ', $signature) . ')';
  }
  $form['exposed_methods'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Exposed method'),
    '#description' => t('Choose methods to expose by this port.'),
    '#options' => $options,
    '#default_value' => $port['exposed_methods'],
  );
  $form['missing_functions'] = array(
    '#type' => 'item',
    '#title' => t('Missing extension functions'),
    '#description' => t('Add extensions to provide missing functions if required. They ' . 'bring more configuration options.'),
  );
  if (!count($missing_functions)) {
    $form['missing_functions']['#value'] = t('None');
  }
  else {
    $form['missing_functions']['#value'] = implode(', ', $missing_functions);
  }
  $form['example'] = array(
    '#type' => 'item',
    '#title' => t("Example on how to invoke <code>radioactivity_get_energy(oid=2, oclass='node')</code>"),
    '#description' => t("The port translates the call to <code>radioactivity_get_energy(2, 'node')</code> as in PHP code. " . 'This retrieves radioactivity info on node/2. This example is not available before the port is saved. ' . 'Also, you must expose <code>radioactivity_get_energy</code> to make this example actually work.'),
  );
  if ($port_id == 'new') {
    $form['example']['#value'] = t('Example unavailable');
  }
  else {
    $query = array();
    switch ($port['access_method']) {
      case 'method-and-query':
        $base = $base_url . '/' . $port['path'] . '/radioactivity_get_energy';
        $query['oid'] = 2;
        $query['oclass'] = 'node';
        break;
    }
    switch ($port['security_scheme']) {
      case 'pkey-md5':
        $query['s'] = _radioactivity_http_create_pkey_md5('radioactivity_get_energy', array(
          'oid' => 2,
          'oclass' => 'node',
        ), $port['private_key']);
    }
    $url = $base;
    if (count($query)) {
      $url .= '?';
      $first = TRUE;
      foreach ($query as $name => $value) {
        if (!$first) {
          $url .= '&';
        }
        $url .= urlencode($name) . '=' . urlencode($value);
        $first = FALSE;
      }
    }
    $form['example']['#value'] = '<a href="' . $url . '">' . $url . '</a>';
  }
  $form['save'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  $form['save_and_edit'] = array(
    '#type' => 'submit',
    '#value' => t('Save and edit'),
  );
  $form['cancel'] = array(
    '#value' => l(t('Cancel'), 'admin/settings/radioactivity/http_ports'),
  );
  return $form;
}