view dnd.module @ 8:b9cd179a30a8

Use user session for the drupal_http_request requesting the library. By default, drupal_http_request runs in a sandbox environment, thus the request doesn't have any idea about the current user. This in turn means that the request on the library is performed as an anonymous user, who may not have appropriate credentials to access the library.
author Franck Deroche <franck@defr.org>
date Wed, 01 Apr 2009 15:49:44 +0200
parents e6378dbabe45
children a54d04d76554
line wrap: on
line source
<?php

// A suffix for auto generated IDs
define(DND_ID_SUFFIX, '-dnd-library');

/**
 * Implementation of hook_menu().
 */
function dnd_menu() {
  $items = array();
  $items['admin/settings/dnd'] = array(
    'title' => 'Drag and Drop Library',
    'page callback' => 'dnd_admin',
    'description' => 'Configure drag-and-drop enabled textareas.',
    'access arguments' => array('administer dnd'),
    'file' => 'dnd.admin.inc',
  );
  return $items;
}

/**
 * Implementation of hook_perm().
 */
function dnd_perm() {
  return array('administer dnd');
}

/**
 * Implementation of hook_theme().
 */
function dnd_theme() {
  return array(
    'dnd_library' => array('arguments' => array('element' => NULL, 'settings' => NULL), 'template' => 'dnd-library'),
  );
}


/**
 * Implementation of hook_elements().
 *
 * Overload textareas.
 */
function dnd_elements() {
  $type = array();
  $type['textarea'] = array(
    '#input' => TRUE, 
    '#cols' => 60, 
    '#rows' => 5, 
    '#resizable' => TRUE,
    '#dnd-enabled' => FALSE,
    '#dnd-settings' => NULL,
    '#process' => array('form_expand_ahah', 'dnd_process_textarea'),
  );
  return $type;
}

/**
 * Settings array:
 * What should it take, if anything?  Probably a source...
 * maybe editor specific configuration shit? 
 *
 * - source for library json/ajax shit
 * - target selector
 * - item selector
 *
 * perhaps like so:  
 *
 * global => 
 *   droppable targets
 *   library source for textarea
 * 
 * tinymce/othereditor =>
 *   target selector logic
 *   configuration options
 *   callback should be smart about attachment and detachment
 */
function dnd_process_textarea($element, $form_state) {
  if ($element['#dnd-enabled']) {

    drupal_add_js(drupal_get_path('module', 'dnd') .'/js/dnd.js', 'footer');
    drupal_add_js(drupal_get_path('module', 'dnd') .'/js/dnd-library.js', 'footer');

    $settings = array();

    // We take a string or an 
    if (is_string($element['#dnd-settings'])) {
      // @TODO load settings
    } 
    else if (is_object($element['#dnd-settings'])) {
      $settings = (array) $element['#dnd-settings'];
    }
    else if (is_array($element['#dnd-settings'])) {
      $settings = $element['#dnd-settings'];
    }

    // Set some important defaults
    $settings = array('library_id' => $element['#id'] . DND_ID_SUFFIX) + $settings;

    // Add enabled libraries to settings for tracking
    drupal_add_js(array(
      'dndEnabledLibraries' => array($element['#id'] => $settings),
    ), 'setting');

    $element['#prefix'] = theme('dnd_library', $element, $settings);
  }
  return $element;
}

function template_preprocess_dnd_library(&$variables) {
  global $base_url;
  list($element, $settings) = array($variables['element'], $variables['settings']);

  // Get library via a backdoor HTTP request.  This is plenty fast for this
  // application and keeps things nice and consistent.
  if (!($url = parse_url($settings['url']))) {
    return t('This library is not available');
  }

  $headers = array();
  // Handle both relative and absolute urls
  if (!isset($url['scheme'])) {
    $settings['url'] = $base_url .'/'. $settings['url'];
    $headers['Cookie'] = session_name() .'='. session_id();
  }

  $request = drupal_http_request($settings['url'], $headers);

  // We must remove some Drupal escaping
  $json = json_decode(str_replace(array('\x3c', '\x3e', '\x26'), array("<", ">", "&"), $request->data), TRUE);

  // Generate an array of editor representations to add
  if (is_array($json['editor_representations'])) {
    foreach ($json['editor_representations'] as $editor_id=>$editor_item) {
      $editor_representations[$editor_id] = filter_xss_admin($editor_item);
    }
  }

  // Store editor representations in Drupal setting
  drupal_add_js(array('dndEditorRepresentations' => $editor_representations,), 'setting');

  $variables['library_id'] = $settings['library_id'];
  $variables['header']     = filter_xss_admin($json['header']);
  $variables['library']    = filter_xss_admin($json['library']);
  $variables['footer']     = filter_xss_admin($json['footer']);
}