annotate 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
rev   line source
eads@1 1 <?php
eads@1 2
eads@2 3 // A suffix for auto generated IDs
eads@2 4 define(DND_ID_SUFFIX, '-dnd-library');
eads@2 5
eads@1 6 /**
eads@1 7 * Implementation of hook_menu().
eads@1 8 */
eads@1 9 function dnd_menu() {
eads@1 10 $items = array();
eads@1 11 $items['admin/settings/dnd'] = array(
eads@1 12 'title' => 'Drag and Drop Library',
eads@1 13 'page callback' => 'dnd_admin',
eads@1 14 'description' => 'Configure drag-and-drop enabled textareas.',
eads@1 15 'access arguments' => array('administer dnd'),
eads@1 16 'file' => 'dnd.admin.inc',
eads@1 17 );
eads@1 18 return $items;
eads@1 19 }
eads@1 20
eads@1 21 /**
eads@1 22 * Implementation of hook_perm().
eads@1 23 */
eads@1 24 function dnd_perm() {
eads@1 25 return array('administer dnd');
eads@1 26 }
eads@2 27
eads@2 28 /**
eads@2 29 * Implementation of hook_theme().
eads@2 30 */
eads@2 31 function dnd_theme() {
eads@2 32 return array(
eads@2 33 'dnd_library' => array('arguments' => array('element' => NULL, 'settings' => NULL), 'template' => 'dnd-library'),
eads@2 34 );
eads@2 35 }
eads@2 36
eads@2 37
eads@2 38 /**
eads@2 39 * Implementation of hook_elements().
eads@2 40 *
eads@2 41 * Overload textareas.
eads@2 42 */
eads@2 43 function dnd_elements() {
eads@2 44 $type = array();
eads@2 45 $type['textarea'] = array(
eads@2 46 '#input' => TRUE,
eads@2 47 '#cols' => 60,
eads@2 48 '#rows' => 5,
eads@2 49 '#resizable' => TRUE,
eads@2 50 '#dnd-enabled' => FALSE,
eads@2 51 '#dnd-settings' => NULL,
eads@2 52 '#process' => array('form_expand_ahah', 'dnd_process_textarea'),
eads@2 53 );
eads@2 54 return $type;
eads@2 55 }
eads@2 56
eads@2 57 /**
eads@2 58 * Settings array:
eads@2 59 * What should it take, if anything? Probably a source...
eads@2 60 * maybe editor specific configuration shit?
eads@2 61 *
eads@2 62 * - source for library json/ajax shit
eads@2 63 * - target selector
eads@2 64 * - item selector
eads@2 65 *
eads@2 66 * perhaps like so:
eads@2 67 *
eads@2 68 * global =>
eads@2 69 * droppable targets
eads@2 70 * library source for textarea
eads@2 71 *
eads@2 72 * tinymce/othereditor =>
eads@2 73 * target selector logic
eads@2 74 * configuration options
eads@2 75 * callback should be smart about attachment and detachment
eads@2 76 */
eads@2 77 function dnd_process_textarea($element, $form_state) {
eads@2 78 if ($element['#dnd-enabled']) {
eads@2 79
eads@4 80 drupal_add_js(drupal_get_path('module', 'dnd') .'/js/dnd.js', 'footer');
eads@4 81 drupal_add_js(drupal_get_path('module', 'dnd') .'/js/dnd-library.js', 'footer');
eads@2 82
eads@2 83 $settings = array();
eads@2 84
eads@2 85 // We take a string or an
eads@2 86 if (is_string($element['#dnd-settings'])) {
eads@2 87 // @TODO load settings
eads@2 88 }
eads@2 89 else if (is_object($element['#dnd-settings'])) {
eads@2 90 $settings = (array) $element['#dnd-settings'];
eads@2 91 }
eads@2 92 else if (is_array($element['#dnd-settings'])) {
eads@2 93 $settings = $element['#dnd-settings'];
eads@2 94 }
eads@2 95
eads@2 96 // Set some important defaults
eads@2 97 $settings = array('library_id' => $element['#id'] . DND_ID_SUFFIX) + $settings;
eads@2 98
eads@2 99 // Add enabled libraries to settings for tracking
eads@2 100 drupal_add_js(array(
eads@2 101 'dndEnabledLibraries' => array($element['#id'] => $settings),
eads@2 102 ), 'setting');
eads@2 103
eads@2 104 $element['#prefix'] = theme('dnd_library', $element, $settings);
eads@2 105 }
eads@2 106 return $element;
eads@2 107 }
eads@2 108
eads@2 109 function template_preprocess_dnd_library(&$variables) {
eads@2 110 global $base_url;
eads@2 111 list($element, $settings) = array($variables['element'], $variables['settings']);
eads@2 112
eads@2 113 // Get library via a backdoor HTTP request. This is plenty fast for this
eads@2 114 // application and keeps things nice and consistent.
eads@2 115 if (!($url = parse_url($settings['url']))) {
eads@2 116 return t('This library is not available');
eads@2 117 }
eads@2 118
franck@8 119 $headers = array();
eads@2 120 // Handle both relative and absolute urls
eads@2 121 if (!isset($url['scheme'])) {
eads@2 122 $settings['url'] = $base_url .'/'. $settings['url'];
franck@8 123 $headers['Cookie'] = session_name() .'='. session_id();
eads@2 124 }
eads@2 125
franck@8 126 $request = drupal_http_request($settings['url'], $headers);
eads@2 127
eads@2 128 // We must remove some Drupal escaping
eads@2 129 $json = json_decode(str_replace(array('\x3c', '\x3e', '\x26'), array("<", ">", "&"), $request->data), TRUE);
eads@2 130
eads@4 131 // Generate an array of editor representations to add
eads@7 132 if (is_array($json['editor_representations'])) {
eads@7 133 foreach ($json['editor_representations'] as $editor_id=>$editor_item) {
eads@7 134 $editor_representations[$editor_id] = filter_xss_admin($editor_item);
eads@7 135 }
eads@2 136 }
eads@2 137
eads@4 138 // Store editor representations in Drupal setting
eads@4 139 drupal_add_js(array('dndEditorRepresentations' => $editor_representations,), 'setting');
eads@4 140
eads@2 141 $variables['library_id'] = $settings['library_id'];
eads@4 142 $variables['header'] = filter_xss_admin($json['header']);
eads@4 143 $variables['library'] = filter_xss_admin($json['library']);
eads@4 144 $variables['footer'] = filter_xss_admin($json['footer']);
eads@2 145 }