| 1 |
<?php
|
| 2 |
// $Id: http_action.module,v 1.4 2007/07/31 22:06:43 eaton Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Exposes Drupal actions for sending HTTP requests.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implementation of hook_action_info().
|
| 11 |
*/
|
| 12 |
function http_action_action_info() {
|
| 13 |
return array(
|
| 14 |
'http_action_request_action' => array(
|
| 15 |
'type' => 'system',
|
| 16 |
'description' => t('Send an HTTP request'),
|
| 17 |
'configurable' => TRUE,
|
| 18 |
'hooks' => array(
|
| 19 |
'any' => TRUE,
|
| 20 |
),
|
| 21 |
),
|
| 22 |
);
|
| 23 |
}
|
| 24 |
|
| 25 |
/**
|
| 26 |
* Return a form definition so the Send email action can be configured.
|
| 27 |
*
|
| 28 |
* @param $context
|
| 29 |
* Default values (if we are editing an existing action instance).
|
| 30 |
* @return
|
| 31 |
* Form definition.
|
| 32 |
*/
|
| 33 |
function http_action_request_action_form($context = array()) {
|
| 34 |
// Set default values for form.
|
| 35 |
$context += array(
|
| 36 |
'uri' => '',
|
| 37 |
'headers' => '',
|
| 38 |
'method' => 'GET',
|
| 39 |
'data' => '',
|
| 40 |
);
|
| 41 |
|
| 42 |
$form['uri'] = array(
|
| 43 |
'#type' => 'textfield',
|
| 44 |
'#title' => t('Request URI'),
|
| 45 |
'#default_value' => $context['uri'],
|
| 46 |
'#size' => 80,
|
| 47 |
'#required' => TRUE,
|
| 48 |
);
|
| 49 |
|
| 50 |
$form['method'] = array(
|
| 51 |
'#type' => 'select',
|
| 52 |
'#title' => t('Request type'),
|
| 53 |
'#default_value' => $context['method'],
|
| 54 |
'#options' => array('GET' => 'GET', 'POST' => 'POST'),
|
| 55 |
'#required' => TRUE,
|
| 56 |
);
|
| 57 |
|
| 58 |
$form['headers'] = array(
|
| 59 |
'#title' => t('Request headers'),
|
| 60 |
'#type' => 'textarea',
|
| 61 |
'#default_value' => $context['headers'],
|
| 62 |
'#columns' => 80,
|
| 63 |
'#rows' => 3,
|
| 64 |
'#description' => t('Any additional headers that should be sent with the request. One line per header, with the header name and a colon at the beginning of each line.'),
|
| 65 |
'#required' => FALSE,
|
| 66 |
);
|
| 67 |
|
| 68 |
$form['data'] = array(
|
| 69 |
'#title' => t('Request data'),
|
| 70 |
'#type' => 'textarea',
|
| 71 |
'#default_value' => $context['data'],
|
| 72 |
'#columns' => 80,
|
| 73 |
'#rows' => 3,
|
| 74 |
'#description' => t('Optionally, any data that should be sent with the request.'),
|
| 75 |
'#required' => FALSE,
|
| 76 |
);
|
| 77 |
|
| 78 |
if (module_exists('token')) {
|
| 79 |
$form['help'] = array(
|
| 80 |
'#type' => 'fieldset',
|
| 81 |
'#collapsible' => TRUE,
|
| 82 |
'#collapsed' => TRUE,
|
| 83 |
'#title' => t('Placeholder tokens'),
|
| 84 |
'#description' => t("The following placeholder tokens can be used in any of the above fields. Some tokens may not be available, depending on the context in which the action is triggered."),
|
| 85 |
);
|
| 86 |
|
| 87 |
$form['help']['tokens'] = array(
|
| 88 |
'#value' => theme('token_help', 'all'),
|
| 89 |
);
|
| 90 |
}
|
| 91 |
|
| 92 |
return $form;
|
| 93 |
}
|
| 94 |
|
| 95 |
function http_action_request_action_submit($form, $form_state) {
|
| 96 |
$form_values = $form_state['values'];
|
| 97 |
// Process the HTML form to store configuration. The keyed array that
|
| 98 |
// we return will be serialized to the database.
|
| 99 |
$params = array(
|
| 100 |
'uri' => $form_values['uri'],
|
| 101 |
'method' => $form_values['method'],
|
| 102 |
'headers' => $form_values['headers'],
|
| 103 |
'data' => $form_values['data'],
|
| 104 |
);
|
| 105 |
return $params;
|
| 106 |
}
|
| 107 |
|
| 108 |
/**
|
| 109 |
* Implementation of a configurable Drupal action.
|
| 110 |
* Sends an email.
|
| 111 |
*/
|
| 112 |
function http_action_request_action($object, $context) {
|
| 113 |
$context['global'] = NULL;
|
| 114 |
if (!empty($context['node']) && empty($context['user'])) {
|
| 115 |
$context['user'] = user_load(array('uid' => $context['node']->uid));
|
| 116 |
}
|
| 117 |
|
| 118 |
if (module_exists('token')) {
|
| 119 |
$uri = token_replace_multiple($context['uri'], $context);
|
| 120 |
$data = token_replace_multiple($context['data'], $context);
|
| 121 |
$tmp = token_replace_multiple($context['headers'], $context);
|
| 122 |
}
|
| 123 |
else {
|
| 124 |
$uri = $context['uri'];
|
| 125 |
$data = $context['data'];
|
| 126 |
$tmp = $context['headers'];
|
| 127 |
}
|
| 128 |
|
| 129 |
$lines = explode("\n", $tmp);
|
| 130 |
$headers = array();
|
| 131 |
foreach ($lines as $line) {
|
| 132 |
$foo = explode(':', $line, 2);
|
| 133 |
$headers[$foo[0]] = trim($foo[1]);
|
| 134 |
}
|
| 135 |
drupal_http_request($uri, $headers, $context['method'], empty($data) ? NULL : $data);
|
| 136 |
}
|