| 1 |
<?php
|
| 2 |
/* $Id: media_actions.module,v 1.1 2007/11/14 21:29:49 pfournier Exp $ */
|
| 3 |
|
| 4 |
|
| 5 |
/**
|
| 6 |
* Action info hook
|
| 7 |
*
|
| 8 |
* @return unknown
|
| 9 |
*/
|
| 10 |
function media_actions_action_info() {
|
| 11 |
return array(
|
| 12 |
'media_actions_bliptv_store_action' => array(
|
| 13 |
'description' => t('Send media to blip.tv; keep it synced when node is edited or deleted.'),
|
| 14 |
'type' => 'node',
|
| 15 |
'configurable' => TRUE,
|
| 16 |
'hooks' => array(
|
| 17 |
'nodeapi' => array('insert', 'update', 'delete'),
|
| 18 |
)
|
| 19 |
)
|
| 20 |
);
|
| 21 |
}
|
| 22 |
|
| 23 |
|
| 24 |
/**
|
| 25 |
* Action form hook
|
| 26 |
*
|
| 27 |
* @param unknown_type $context: default values for the form
|
| 28 |
* @return the form description
|
| 29 |
*/
|
| 30 |
function media_actions_bliptv_store_action_form($context) {
|
| 31 |
// default values for form
|
| 32 |
if (!isset($context['username'])) $context['username'] = '';
|
| 33 |
if (!isset($context['password'])) $context['password'] = '';
|
| 34 |
if (!isset($context['timeout'])) $context['timeout'] = '300';
|
| 35 |
if (!isset($context['license'])) $context['license'] = '1';
|
| 36 |
if (!isset($context['signature'])) $context['signature'] = '';
|
| 37 |
if (!isset($context['srcfield'])) $context['srcfield'] = 'local_video';
|
| 38 |
if (!isset($context['dstfield'])) $context['dstfield'] = 'remote_video';
|
| 39 |
if (!isset($context['node_types'])) $context['node_types'] = array();
|
| 40 |
|
| 41 |
$form = array();
|
| 42 |
|
| 43 |
// add form components
|
| 44 |
$form['username'] = array(
|
| 45 |
'#type' => 'textfield',
|
| 46 |
'#title' => t('blip.tv User Name'),
|
| 47 |
'#description' => t("Name of the blip.tv user."),
|
| 48 |
'#default_value' => $context['username'],
|
| 49 |
'#size' => '20',
|
| 50 |
'#maxlength' => '254',
|
| 51 |
'#required' => TRUE,
|
| 52 |
);
|
| 53 |
$form['password'] = array(
|
| 54 |
'#type' => 'textfield',
|
| 55 |
'#title' => t('blip.tv Password'),
|
| 56 |
'#default_value' => $context['password'],
|
| 57 |
'#size' => '20',
|
| 58 |
'#maxlength' => '254',
|
| 59 |
'#description' => t('Password for the blip.tv user above.'),
|
| 60 |
'#required' => TRUE,
|
| 61 |
);
|
| 62 |
$form['timeout'] = array(
|
| 63 |
'#type' => 'textfield',
|
| 64 |
'#title' => t('Upload Timeout'),
|
| 65 |
'#default_value' => $context['timeout'],
|
| 66 |
'#field_suffix' => t('seconds'),
|
| 67 |
'#size' => '4',
|
| 68 |
'#maxlength' => '4',
|
| 69 |
'#description' => t('How long we should wait for the transfer from our server to blip.tv server to complete.'),
|
| 70 |
'#required' => TRUE,
|
| 71 |
);
|
| 72 |
$license['-1'] = 'No license (All rights reserved)';
|
| 73 |
$license['1'] = 'Creative Commons Attribution';
|
| 74 |
$license['2'] = 'Creative Commons Attribution-NoDerivs';
|
| 75 |
$license['3'] = 'Creative Commons Attribution-NonCommercial-NoDerivs';
|
| 76 |
$license['4'] = 'Creative Commons Attribution-NonCommercial';
|
| 77 |
$license['5'] = 'Creative Commons Attribution-NonCommercial-ShareAlike';
|
| 78 |
$license['6'] = 'Creative Commons Attribution-ShareAlike';
|
| 79 |
$license['7'] = 'Public Domain';
|
| 80 |
$form['license'] = array(
|
| 81 |
'#type' => 'select',
|
| 82 |
'#title' => t('Global License Type'),
|
| 83 |
'#options' => $license,
|
| 84 |
'#default_value' => $context['license'],
|
| 85 |
'#description' => t('All video will be uploaded and attached with the specified license. Description of licenses here <a href="http://creativecommons.org/licenses/">www.creativecommons.org/licenses</a>'),
|
| 86 |
'#required' => TRUE,
|
| 87 |
);
|
| 88 |
$form['signature'] = array(
|
| 89 |
'#type' => 'textfield',
|
| 90 |
'#title' => t('Global Signature'),
|
| 91 |
'#length' => 5,
|
| 92 |
'#default_value' => $context['signature'],
|
| 93 |
'#description' => t('This text will be appended to any video description uploaded to your blip.tv account e.g. a link back to your site from blip.tv')
|
| 94 |
);
|
| 95 |
$form['srcfield'] = array(
|
| 96 |
'#type' => 'textfield',
|
| 97 |
'#title' => t('Local Video Field Name'),
|
| 98 |
'#default_value' => $context['srcfield'],
|
| 99 |
'#description' => t("Name of the node field containing the video file path."),
|
| 100 |
'#required' => TRUE,
|
| 101 |
);
|
| 102 |
$form['dstfield'] = array(
|
| 103 |
'#type' => 'textfield',
|
| 104 |
'#title' => t('Remote Video Field Name'),
|
| 105 |
'#default_value' => $context['dstfield'],
|
| 106 |
'#description' => t("Name of the node field where the uploaded video URL will be stored."),
|
| 107 |
'#required' => TRUE,
|
| 108 |
);
|
| 109 |
|
| 110 |
$types = node_get_types('names');
|
| 111 |
$form['node_types'] = array(
|
| 112 |
'#type' => 'checkboxes',
|
| 113 |
'#title' => t('Node types'),
|
| 114 |
'#default_value' => $context['node_types'],
|
| 115 |
'#options' => $types,
|
| 116 |
'#description' => t('Action will be executed on the selected node types.'),
|
| 117 |
);
|
| 118 |
|
| 119 |
return $form;
|
| 120 |
}
|
| 121 |
|
| 122 |
|
| 123 |
/**
|
| 124 |
* Action form validate hook
|
| 125 |
*
|
| 126 |
* @param unknown_type $form
|
| 127 |
* @param unknown_type $form_values
|
| 128 |
*/
|
| 129 |
function media_actions_bliptv_store_action_validate($form, $form_values) {
|
| 130 |
if (!is_numeric($form_values['timeout']) || ((int)$form_values['timeout'] < 0)) {
|
| 131 |
form_set_error('timeout', t("The timeout must be a non-negative numeric value."));
|
| 132 |
}
|
| 133 |
}
|
| 134 |
|
| 135 |
|
| 136 |
/**
|
| 137 |
* Action form submit hook
|
| 138 |
*
|
| 139 |
* @param unknown_type $form
|
| 140 |
* @param unknown_type $form_values
|
| 141 |
* @return unknown
|
| 142 |
*/
|
| 143 |
function media_actions_bliptv_store_action_submit($form, $form_values) {
|
| 144 |
$params = array(
|
| 145 |
'username' => $form_values['username'],
|
| 146 |
'password' => $form_values['password'],
|
| 147 |
'timeout' => $form_values['timeout'],
|
| 148 |
'license' => $form_values['license'],
|
| 149 |
'signature' => $form_values['signature'],
|
| 150 |
'srcfield' => $form_values['srcfield'],
|
| 151 |
'dstfield' => $form_values['dstfield'],
|
| 152 |
'node_types'=> $form_values['node_types'],
|
| 153 |
);
|
| 154 |
return $params;
|
| 155 |
}
|
| 156 |
|
| 157 |
|
| 158 |
/**
|
| 159 |
* The action hook
|
| 160 |
*
|
| 161 |
* @param unknown_type $node
|
| 162 |
* @param unknown_type $context
|
| 163 |
*/
|
| 164 |
function media_actions_bliptv_store_action(&$node, $context = array()) {
|
| 165 |
if ($context['hook'] == 'nodeapi') {
|
| 166 |
if ($context['op'] == 'insert') {
|
| 167 |
if (_media_actions_bliptv_check_params($node, $context) && _media_actions_bliptv_send($node, $context)) {
|
| 168 |
watchdog('media_actions', t('Sent node id %id media to blip.tv', array('%id' => intval($node->nid))));
|
| 169 |
}
|
| 170 |
}
|
| 171 |
else if ($context['op'] == 'update') {
|
| 172 |
if (_media_actions_bliptv_check_params($node, $context) && _media_actions_bliptv_update($node, $context)) {
|
| 173 |
watchdog('media_actions', t('Updated node id %id media to blip.tv', array('%id' => intval($node->nid))));
|
| 174 |
}
|
| 175 |
}
|
| 176 |
else if ($context['op'] == 'delete') {
|
| 177 |
if (_media_actions_bliptv_check_params($node, $context) && _media_actions_bliptv_delete($node, $context)) {
|
| 178 |
watchdog('media_actions', t('Deleted node id %id media to blip.tv', array('%id' => intval($node->nid))));
|
| 179 |
}
|
| 180 |
}
|
| 181 |
}
|
| 182 |
}
|
| 183 |
|
| 184 |
/**
|
| 185 |
* Check all parameters are ok for te action to execute on the node.
|
| 186 |
*
|
| 187 |
* @param $node
|
| 188 |
* @param $params
|
| 189 |
* @return TRUE if all is ok
|
| 190 |
*/
|
| 191 |
function _media_actions_bliptv_check_params($node, $params) {
|
| 192 |
// check node type
|
| 193 |
$node_type = node_get_types('type', $node);
|
| 194 |
if ($params['node_types'][$node_type->type] === 0) {
|
| 195 |
// do not process this node type
|
| 196 |
return FALSE;
|
| 197 |
}
|
| 198 |
|
| 199 |
// check user name
|
| 200 |
if (!$params['username']) {
|
| 201 |
$message = t("blip.tv username not set");
|
| 202 |
watchdog('media_actions', $message, 'WATCHDOG_ERROR');
|
| 203 |
return FALSE;
|
| 204 |
}
|
| 205 |
|
| 206 |
// check password
|
| 207 |
if (!$params['password']) {
|
| 208 |
$message = t("blip.tv password not set");
|
| 209 |
watchdog('media_actions', $message, 'WATCHDOG_ERROR');
|
| 210 |
return FALSE;
|
| 211 |
}
|
| 212 |
|
| 213 |
// check srcfield
|
| 214 |
if ((!$params['srcfield'])) {
|
| 215 |
$message = t("Local video field not set");
|
| 216 |
watchdog('media_actions', $message, 'WATCHDOG_ERROR');
|
| 217 |
return FALSE;
|
| 218 |
}
|
| 219 |
if (!array_key_exists($params['srcfield'], get_object_vars($node))) {
|
| 220 |
$message = t("Node type !n does not have a field named !f.", array('!n' => $node_type, '!f' => $params['srcfield']));
|
| 221 |
watchdog('media_actions', $message, 'WATCHDOG_ERROR');
|
| 222 |
return FALSE;
|
| 223 |
}
|
| 224 |
|
| 225 |
// check dstfield
|
| 226 |
if ((!$params['dstfield'])) {
|
| 227 |
$message = t("Remote video field not set");
|
| 228 |
watchdog('media_actions', $message, 'WATCHDOG_ERROR');
|
| 229 |
return FALSE;
|
| 230 |
}
|
| 231 |
if (!array_key_exists($params['dstfield'], get_object_vars($node))) {
|
| 232 |
$message = t("Node type !n does not have a field named !f.", array('!n' => $node_type, '!f' => $params['dstfield']));
|
| 233 |
watchdog('media_actions', $message, 'WATCHDOG_ERROR');
|
| 234 |
return FALSE;
|
| 235 |
}
|
| 236 |
|
| 237 |
return TRUE;
|
| 238 |
}
|
| 239 |
|
| 240 |
/**
|
| 241 |
* Sends file to bliptv.
|
| 242 |
* @node Node containing file to send.
|
| 243 |
* @params Action configuration parameters.
|
| 244 |
* @blid_file_id If this is set, we will update the bliptv file instead of creating a new one.
|
| 245 |
* @return FALSE on error, TRUE on success.
|
| 246 |
*
|
| 247 |
*/
|
| 248 |
function _media_actions_bliptv_send($node, $params = array(), $blip_file_id = null) {
|
| 249 |
$username = $params['username'];
|
| 250 |
$password = $params['password'];
|
| 251 |
$local_video_field_name = $params['srcfield'];
|
| 252 |
$remote_video_field_name = $params['dstfield'];
|
| 253 |
|
| 254 |
$local_video_field = &$node->$local_video_field_name;
|
| 255 |
$local_video = array_shift($local_video_field);
|
| 256 |
|
| 257 |
if ($local_video['flags']['delete']) {
|
| 258 |
// video file is being deleted
|
| 259 |
return _media_actions_bliptv_delete($node, $params);
|
| 260 |
}
|
| 261 |
|
| 262 |
$file_path = $local_video['filepath'];
|
| 263 |
$file_name = $local_video['filename'];
|
| 264 |
|
| 265 |
// Upload file to blip.tv
|
| 266 |
$post_data['file'] = "@" . $file_path;
|
| 267 |
$post_data['description'] = $file_name . $edit['signature']; // Any valid text or HTML
|
| 268 |
$post_data['title'] = $node->title; // Maximum length 255. No HTML.
|
| 269 |
$post_data['post'] = 1; // Required: must set this to 1
|
| 270 |
$post_data['license'] = $edit['license']; // Defaults to "-1" or "No license"
|
| 271 |
$post_data['categories_id'] = -1; // "-1" means no category. Never use a value of "0".
|
| 272 |
$post_data['userlogin'] = $username; // valid username
|
| 273 |
$post_data['password'] = $password; // valid password
|
| 274 |
if (isset($blip_file_id)) {
|
| 275 |
$post_data['id'] = $blip_file_id; // update the file
|
| 276 |
}
|
| 277 |
|
| 278 |
$response = array();
|
| 279 |
|
| 280 |
$timeout = intval($edit['timeout']);
|
| 281 |
set_time_limit($timeout);
|
| 282 |
|
| 283 |
// @TODO use drupal_http_request()
|
| 284 |
// for encoding data, see http://api-drupal.pajunas.com/api/function/_openid_encode_message/DRUPAL-5
|
| 285 |
// and also: http://www.w3.org/TR/html4/interact/forms.html
|
| 286 |
$curl_session = curl_init();
|
| 287 |
curl_setopt($curl_session, CURLOPT_URL, "http://www.blip.tv/?section=file&cmd=post&skin=api");
|
| 288 |
|
| 289 |
curl_setopt($curl_session, CURLOPT_POST, 1 );
|
| 290 |
curl_setopt($curl_session, CURLOPT_POSTFIELDS, $post_data);
|
| 291 |
curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, TRUE);
|
| 292 |
curl_setopt($curl_session, CURLOPT_TIMEOUT, $timeout);
|
| 293 |
$response['data'] = curl_exec($curl_session);
|
| 294 |
|
| 295 |
if (curl_errno($curl_session)) {
|
| 296 |
$response['error'] = curl_error($curl_session);
|
| 297 |
}
|
| 298 |
curl_close($curl_session);
|
| 299 |
|
| 300 |
if ($response['error']) {
|
| 301 |
$message = "CURL error: ".$response['error'] . " for file " . $post_data['file'];
|
| 302 |
watchdog('media_actions', $message, 'WATCHDOG_ERROR');
|
| 303 |
return FALSE;
|
| 304 |
}
|
| 305 |
|
| 306 |
$data = trim($response['data']);
|
| 307 |
$vals = $index = $array = array();
|
| 308 |
$parser = xml_parser_create();
|
| 309 |
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
|
| 310 |
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
|
| 311 |
$xml_parse_result = xml_parse_into_struct($parser, $data, $vals, $index);
|
| 312 |
|
| 313 |
if ($xml_parse_result == 0) {
|
| 314 |
$file_id = 0;
|
| 315 |
$file_url = "";
|
| 316 |
$message = t("Error in blip.tv answer: ") . xml_error_string(xml_get_error_code($parser)) . t(" at line ") . xml_get_current_line_number($parser);
|
| 317 |
watchdog('media_actions', $message, 'WATCHDOG_ERROR');
|
| 318 |
}
|
| 319 |
else
|
| 320 |
{
|
| 321 |
$file_id = $vals[$index['item_id'][0]]['value'];
|
| 322 |
$file_url = $vals[$index['link'][0]]['attributes']['href'];
|
| 323 |
}
|
| 324 |
|
| 325 |
xml_parser_free($parser);
|
| 326 |
|
| 327 |
if ($file_id != 0) {
|
| 328 |
$remote_video_field = &$node->$remote_video_field_name;
|
| 329 |
if ($file_id != $remote_video_field[0]['value']) {
|
| 330 |
$remote_video_field[0]['embed'] = $file_url;
|
| 331 |
$remote_video_field[0]['value'] = $file_id;
|
| 332 |
$remote_video_field[0]['provider'] = 'bliptv';
|
| 333 |
node_validate($node);
|
| 334 |
}
|
| 335 |
|
| 336 |
$local_video['flags']['delete'] = TRUE;
|
| 337 |
|
| 338 |
return TRUE;
|
| 339 |
}
|
| 340 |
else {
|
| 341 |
return FALSE;
|
| 342 |
}
|
| 343 |
}
|
| 344 |
|
| 345 |
|
| 346 |
/**
|
| 347 |
* Update file on bliptv.
|
| 348 |
* @node Node containing file to update.
|
| 349 |
* @params Action configuration parameters.
|
| 350 |
* @return FALSE on error, TRUE on success.
|
| 351 |
*
|
| 352 |
*/
|
| 353 |
function _media_actions_bliptv_update($node, $params = array()) {
|
| 354 |
$remote_video_field_name = $params['dstfield'];
|
| 355 |
$remote_video_field = &$node->$remote_video_field_name;
|
| 356 |
$blip_id = $remote_video_field[0]['value'];
|
| 357 |
|
| 358 |
_media_actions_bliptv_send($node, $params, $blip_id);
|
| 359 |
}
|
| 360 |
|
| 361 |
/**
|
| 362 |
* Delete file from bliptv.
|
| 363 |
* @node Node containing file to delete.
|
| 364 |
* @params Action configuration parameters.
|
| 365 |
* @return FALSE on error, TRUE on success.
|
| 366 |
*
|
| 367 |
*/
|
| 368 |
function _media_actions_bliptv_delete($node, $params = array()) {
|
| 369 |
$username = $params['username'];
|
| 370 |
$password = $params['password'];
|
| 371 |
$remote_video_field_name = $params['dstfield'];
|
| 372 |
|
| 373 |
$remote_video_field = $node->$remote_video_field_name;
|
| 374 |
$bliptv_file_id = $remote_video_field[0]['value'];
|
| 375 |
|
| 376 |
$reason = urlencode(t("Source node deleted"));
|
| 377 |
$username = urlencode($username);
|
| 378 |
$password = urlencode($password);
|
| 379 |
$response = drupal_http_request("http://www.blip.tv/?section=file&cmd=delete&id=$bliptv_file_id&reason=$reason&userlogin=$username&password=$password&skin=api");
|
| 380 |
|
| 381 |
if (isset($response->error)) {
|
| 382 |
watchdog('media_actions', $response->error, 'WATCHDOG_ERROR');
|
| 383 |
return FALSE;
|
| 384 |
}
|
| 385 |
|
| 386 |
$data = trim($response->data);
|
| 387 |
$vals = $index = $array = array();
|
| 388 |
$parser = xml_parser_create();
|
| 389 |
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
|
| 390 |
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
|
| 391 |
$xml_parse_result = xml_parse_into_struct($parser, $data, $vals, $index);
|
| 392 |
|
| 393 |
if ($xml_parse_result == 0) {
|
| 394 |
$message = t("Error in blip.tv answer: ") . xml_error_string(xml_get_error_code($parser)) . t(" at line ") . xml_get_current_line_number($parser);
|
| 395 |
watchdog('media_actions', $message, 'WATCHDOG_ERROR');
|
| 396 |
}
|
| 397 |
else
|
| 398 |
{
|
| 399 |
if (!$vals[$index['deleted'][0]]['value']){
|
| 400 |
$message = t("Could not delete blip.tv file");
|
| 401 |
watchdog('media_actions', $message, 'WATCHDOG_ERROR');
|
| 402 |
}
|
| 403 |
}
|
| 404 |
|
| 405 |
xml_parser_free($parser);
|
| 406 |
|
| 407 |
$remote_video_field[0]['embed'] = '';
|
| 408 |
$remote_video_field[0]['value'] = '';
|
| 409 |
$remote_video_field[0]['provider'] = '';
|
| 410 |
node_validate($node);
|
| 411 |
|
| 412 |
return TRUE;
|
| 413 |
}
|