/[drupal]/contributions/modules/iframe/iframe.module
ViewVC logotype

Contents of /contributions/modules/iframe/iframe.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.12 - (show annotations) (download) (as text)
Sun May 10 00:04:27 2009 UTC (6 months, 2 weeks ago) by neffets
Branch: MAIN
CVS Tags: HEAD
Changes since 1.11: +34 -11 lines
File MIME type: text/x-php
issue#458104 will now provide 'scrolling' no/yes and the old attribute 'frameborder' will now work
1 <?php
2 // $Id: iframe.module,v 1.11 2009/05/09 23:33:42 neffets Exp $
3
4 /**
5 * @file
6 * Defines simple iframe field types.
7 * based on the cck-module "iframe" by quicksketch
8 */
9
10 define('iframe_EXTERNAL', 'external');
11 define('iframe_INTERNAL', 'internal');
12 define('iframe_FRONT', 'front');
13 define('iframe_EMAIL', 'email');
14 define('iframe_DOMAINS', 'aero|arpa|biz|com|cat|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel|mobi|local');
15
16 if (!defined('DEBUG_LEVEL')) define('DEBUG_LEVEL',0);
17 if (!function_exists('dmsg')) {
18 function dmsg ($level,$text) {
19 if ($level <= DEBUG_LEVEL) error_log('iframe('.$level.'): '.$text);
20 }
21 }
22 /**
23 * Implementation of hook_field_info().
24 */
25 function iframe_field_info() {
26 dmsg(3,'func iframe_field_info');
27 return array(
28 'iframe' => array(
29 'label' => t('IFrame'),
30 'description' => t('Store a title, src, and attributes in the database to assemble an iframe.'),
31 ),
32 );
33 }
34
35 /**
36 * Implementation of hook_field_settings().
37 * shown: at general setup of this field type on a content type
38 */
39 function iframe_field_settings($op, $field) {
40 dmsg(3,'func iframe_field_settings op='.$op);
41 switch ($op) {
42 case 'form':
43 $form = array(
44 '#theme' => 'iframe_field_settings',
45 );
46
47 $form['url'] = array(
48 '#type' => 'checkbox',
49 '#title' => t('Optional url'),
50 '#default_value' => $field['url'],
51 '#return_value' => 'optional',
52 '#description' => t('If checked, the url field is optional. If the url is ommitted, nothing will be displayed.'),
53 );
54
55 $title_options = array(
56 'optional' => t('Optional Title'),
57 'required' => t('Required Title'),
58 'value' => t('Static Title: '),
59 'none' => t('No Title'),
60 );
61
62 $form['title'] = array(
63 '#type' => 'radios',
64 '#title' => t('IFrame Title'),
65 '#default_value' => isset($field['title']) ? $field['title'] : 'optional',
66 '#options' => $title_options,
67 '#description' => t('If the iframe title is optional or required, a field will be displayed to the end user. If the iframe title is static, the iframe will always use the same title. If <a href="http://drupal.org/project/token">token module</a> is installed, the static title value may use any other node field as its value. Static and token-based titles may include most inline XHTML tags such as <em>strong</em>, <em>em</em>, <em>img</em>, <em>span</em>, etc.'),
68 );
69
70 $form['title_value'] = array(
71 '#type' => 'textfield',
72 '#default_value' => $field['title_value'],
73 '#size' => '46',
74 );
75
76 // Add token module replacements if available
77 if (module_exists('token')) {
78 $form['tokens'] = array(
79 '#type' => 'fieldset',
80 '#collapsible' => TRUE,
81 '#collapsed' => TRUE,
82 '#title' => t('Placeholder tokens'),
83 '#description' => t("The following placeholder tokens can be used in both paths and titles. When used in a path or title, they will be replaced with the appropriate values."),
84 );
85 $form['tokens']['help'] = array(
86 '#value' => theme('token_help', 'node'),
87 );
88
89 $form['enable_tokens'] = array(
90 '#type' => 'checkbox',
91 '#title' => t('Allow user-entered tokens'),
92 '#default_value' => isset($field['enable_tokens']) ? $field['enable_tokens'] : 1,
93 '#description' => t('Checking will allow users to enter tokens in URLs and Titles on the node edit form. This does not affect the field settings on this page.'),
94 );
95 }
96
97 $form['display'] = array(
98 '#tree' => true,
99 );
100
101 $form['attributes'] = array(
102 '#tree' => true,
103 );
104 $form['attributes']['class'] = array(
105 '#type' => 'textfield',
106 '#title' => t('Additional CSS Class'),
107 '#description' => t('When output, this iframe will have this class attribute. Multiple classes should be separated by spaces.'),
108 '#default_value' => empty($field['attributes']['class']) ? '' : $field['attributes']['class'],
109 );
110 $form['attributes']['width'] = array(
111 '#type' => 'textfield',
112 '#title' => t('width of the iframe'),
113 '#description' => t('iframes need fix width and height, only numbers are allowed.'),
114 '#default_value' => empty($field['attributes']['width']) ? '100%' : $field['attributes']['width'],
115 '#maxlength' => 4,
116 '#size' => 4,
117 );
118 $form['attributes']['height'] = array(
119 '#type' => 'textfield',
120 '#title' => t('height of the iframe'),
121 '#description' => t('iframes need fix width and height, only numbers are allowed.'),
122 '#default_value' => empty($field['attributes']['height']) ? '700' : $field['attributes']['height'],
123 '#maxlength' => 4,
124 '#size' => 4,
125 );
126 $form['attributes']['frameborder'] = array(
127 '#type' => 'select',
128 '#title' => t('Frameborder'),
129 '#options' => array( '0' => t('no frameborder'), 'yes' => t('show frameborder') ),
130 '#default_value' => empty($field['attributes']['frameborder']) ? '0' : $field['attributes']['frameborder'],
131 '#description' => t('Frameborder is the border arround the iframe. Mostly people want it silent, so the default value for frameborder is 0 = no.'),
132 );
133 $form['attributes']['scrolling'] = array(
134 '#type' => 'select',
135 '#title' => t('Scrolling'),
136 '#options' => array( 'auto' => t('Scrolling automatic'), 'no' => t('Scrolling disabled'), 'yes' => t('Scrolling enabled') ),
137 '#default_value' => empty($field['attributes']['scrolling']) ? 'auto' : $field['attributes']['scrolling'],
138 '#description' => t('Scrollbars help the user to reach all iframe content despite the real height of the iframe content. Please disable it only if You know what You are doing.'),
139 );
140 return $form;
141
142 case 'validate':
143 if ($field['title'] == 'value' && empty($field['title_value'])) {
144 form_set_error('title_value', t('A default title must be provided if the title is a static value'));
145 }
146 if (empty($field['attributes']['width']) || (int)$field['attributes']['width']<1) {
147 form_set_error('width_value', t('A default width and height must be provided.'));
148 }
149 if (empty($field['attributes']['height']) || (int)$field['attributes']['height']<1) {
150 form_set_error('height_value', t('A default width and height must be provided.'));
151 }
152 break;
153
154 case 'save':
155 return array('attributes', 'display', 'url', 'title', 'title_value', 'enable_tokens');
156
157 case 'database columns':
158 return array(
159 'url' => array('type' => 'varchar', 'length' => 1024, 'not null' => FALSE, 'sortable' => TRUE),
160 'title' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE, 'sortable' => TRUE),
161 'attributes' => array('type' => 'text', 'size' => 'medium', 'not null' => FALSE),
162 );
163
164 case 'views data':
165 if (FALSE && module_exists('views')) {
166 module_load_include('inc', 'iframe', 'views/iframe.views');
167 return iframe_views_content_field_data($field);
168 }
169 }
170 }
171
172 /**
173 * Theme the settings form for the iframe field.
174 */
175 function theme_iframe_field_settings($form) {
176 dmsg(3,'func theme_iframe_field_settings');
177 $title_value = drupal_render($form['title_value']);
178 $title_checkbox = drupal_render($form['title']['value']);
179
180 // Set Static Title radio option to include the title_value textfield.
181 $form['title']['value'] = array('#value' => '<div class="container-inline">'. $title_checkbox . $title_value .'</div>');
182
183 // Reprint the title radio options with the included textfield.
184 return drupal_render($form);
185 }
186
187 /**
188 * Implementation of hook_content_is_empty().
189 */
190 function iframe_content_is_empty($item, $field) {
191 dmsg(3,'func iframe_content_is_empty');
192 if (empty($item['url'])) {
193 return TRUE;
194 }
195 return FALSE;
196 }
197
198 /**
199 * Implementation of hook_field().
200 */
201 function iframe_field($op, &$node, $field, &$items, $teaser, $page) {
202 dmsg(3,'func iframe_field op='.$op.' field='.$field);
203 switch ($op) {
204 case 'load':
205 foreach ($items as $delta => $item) {
206 _iframe_load($items[$delta], $delta);
207 }
208 break;
209
210 case 'validate':
211 $optional_field_found = FALSE;
212 foreach($items as $delta => $value) {
213 _iframe_validate($items[$delta],$delta, $field, $node, $optional_field_found);
214 }
215
216 if ($field['url'] == 'optional' && $field['title'] == 'optional' && $field['required'] && !$optional_field_found) {
217 form_set_error($field['field_name'] .'][0][title', t('At least one title or URL must be entered.'));
218 }
219 break;
220
221 case 'presave':
222 foreach($items as $delta => $value) {
223 _iframe_process($items[$delta], $delta, $field, $node);
224 }
225 break;
226
227 case 'sanitize':
228 foreach ($items as $delta => $value) {
229 _iframe_sanitize($items[$delta], $delta, $field, $node);
230 }
231 break;
232 }
233 }
234
235 /**
236 * Implementation of hook_widget_info().
237 */
238 function iframe_widget_info() {
239 dmsg(3,'func iframe_widget_info');
240 return array(
241 'iframe' => array(
242 'label' => 'Text Fields for Title and IFrame-url',
243 'field types' => array('iframe'),
244 'multiple values' => CONTENT_HANDLE_CORE,
245 ),
246 );
247 }
248
249 /**
250 * Implementation of hook_widget().
251 */
252 function iframe_widget(&$form, &$form_state, $field, $items, $delta = 0) {
253 dmsg(3,'func iframe_widget');
254 $element = array(
255 '#type' => $field['widget']['type'],
256 '#default_value' => isset($items[$delta]) ? $items[$delta] : '',
257 '#title' => $field['widget']['label'],
258 '#weight' => $field['widget']['weight'],
259 '#description' => $field['widget']['description'],
260 '#required' => $field['required'],
261 '#field' => $field,
262 );
263 return $element;
264 }
265
266 function _iframe_load(&$item, $delta = 0) {
267 dmsg(3,'func _iframe_load');
268 // Unserialize the attributes array.
269 if (!is_array($item['attributes'])) $item['attributes'] = unserialize($item['attributes']);
270 }
271
272 function _iframe_process(&$item, $delta = 0, $field, $node) {
273 dmsg(3,'func _iframe_process');
274 // Trim whitespace from url.
275 $item['url'] = trim($item['url']);
276 // numbers should be numbers
277 $item['attributes']['width'] = iframe_validate_size($item['attributes']['width'], $field['widget']['default_value'][$delta]['width']);
278 $item['attributes']['height'] = iframe_validate_size($item['attributes']['height'], $field['widget']['default_value'][$delta]['height']);
279
280 // Serialize the attributes array.
281 $item['attributes'] = serialize($item['attributes']);
282
283 // Don't save an invalid default value (e.g. 'http://').
284 if ((isset($field['widget']['default_value'][$delta]['url']) && $item['url'] == $field['widget']['default_value'][$delta]['url']) && is_object($node)) {
285 if (!iframe_validate_url($item['url'])) {
286 unset($item['url']);
287 }
288 }
289 }
290
291 function _iframe_validate(&$item, $delta, $field, $node, &$optional_field_found) {
292 dmsg(3,'func _iframe_validate');
293 if ($item['url'] && !(isset($field['widget']['default_value'][$delta]['url']) && $item['url'] == $field['widget']['default_value'][$delta]['url'] && !$field['required'])) {
294 // Validate the iframe.
295 if (iframe_validate_url(trim($item['url'])) == FALSE) {
296 form_set_error($field['field_name'] .']['. $delta. '][url', t('Not a valid iframe-url.'));
297 }
298 // Require a title for the iframe if necessary.
299 if ($field['title'] == 'required' && strlen(trim($item['title'])) == 0) {
300 form_set_error($field['field_name'] .']['. $delta. '][title', t('Titles are required for all iframes.'));
301 }
302 }
303 // Require a iframe if we have a title.
304 if ($field['url'] !== 'optional' && strlen($item['title']) > 0 && strlen(trim($item['url'])) == 0) {
305 form_set_error($field['field_name'] .']['. $delta. '][url', t('You cannot enter a title without a iframe url.'));
306 }
307 // In a totally bizzaro case, where URLs and titles are optional but the field is required, ensure there is at least one iframe.
308 if ($field['url'] == 'optional' && $field['title'] == 'optional' && (strlen(trim($item['url'])) != 0 || strlen(trim($item['title'])) != 0)) {
309 $optional_field_found = TRUE;
310 }
311 }
312
313 /**
314 * Cleanup user-entered values for a iframe field according to field settings.
315 *
316 * @param $item
317 * A single iframe item, usually containing url, title, and attributes.
318 * @param $delta
319 * The delta value if this field is one of multiple fields.
320 * @param $field
321 * The CCK field definition.
322 * @param $node
323 * The node containing this iframe.
324 */
325 function _iframe_sanitize(&$item, $delta, &$field, &$node) {
326 dmsg(3,'func _iframe_sanitize');
327 // Don't try to process empty iframes.
328 if (empty($item['url']) && empty($item['title'])) {
329 return;
330 }
331
332 // Replace URL tokens.
333 if (module_exists('token') && $field['enable_tokens']) {
334 // Load the node if necessary for nodes in views.
335 $token_node = isset($node->nid) ? node_load($node->nid) : $node;
336 $item['url'] = token_replace($item['url'], 'node', $token_node);
337 }
338
339 $type = iframe_validate_url($item['url']);
340 $url = iframe_cleanup_url($item['url']);
341
342 // Separate out the anchor if any.
343 if (strpos($url, '#') !== FALSE) {
344 $item['fragment'] = substr($url, strpos($url, '#') + 1);
345 $url = substr($url, 0, strpos($url, '#'));
346 }
347 // Separate out the query string if any.
348 if (strpos($url, '?') !== FALSE) {
349 $item['query'] = substr($url, strpos($url, '?') + 1);
350 $url = substr($url, 0, strpos($url, '?'));
351 }
352 // Save the new URL without the anchor or query.
353 $item['url'] = $url;
354
355 // Create a shortened URL for display.
356 $display_url = $type == iframe_EMAIL ? str_replace('mailto:', '', $url) : url($url, array('query' => isset($item['query']) ? $item['query'] : NULL, 'fragment' => isset($item['fragment']) ? $item['fragment'] : NULL, 'absolute' => TRUE));
357 if (strlen($display_url) > 72) {
358 $display_url = substr($display_url, 0, 72) . "...";
359 }
360 $item['display_url'] = $display_url;
361
362 // Use the title defined at the field level.
363 if ($field['title'] == 'value' && strlen(trim($field['title_value']))) {
364 $title = $field['title_value'];
365 }
366 // Use the title defined by the user at the widget level.
367 else {
368 $title = $item['title'];
369 }
370 // Replace tokens.
371 if (module_exists('token') && ($field['title'] == 'value' || $field['enable_tokens'])) {
372 // Load the node if necessary for nodes in views.
373 $token_node = isset($node->nid) ? node_load($node->nid) : $node;
374 $title = filter_xss(token_replace($title, 'node', $token_node), array('b', 'br', 'code', 'em', 'i', 'img', 'span', 'strong', 'sub', 'sup', 'tt', 'u'));
375 $item['html'] = TRUE;
376 }
377 $item['display_title'] = empty($title) ? '' : $title;
378
379 // load attributes by node
380 _iframe_load($item);
381
382 // Add attributes defined at the widget level
383 $attributes = array();
384 if (!empty($item['attributes']) && is_array($item['attributes'])) {
385 foreach($item['attributes'] as $attribute => $attbvalue) {
386 if (isset($attbvalue) && ($field['attributes'][$attribute] == 'user' || $attribute=='width' || $attribute=='height' || $attribute=='frameborder' || $attribute=='scrolling')) {
387 $attributes[$attribute] = $attbvalue;
388 }
389 }
390 }
391 // Add attributes defined at the field level, if not set
392 if (is_array($field['attributes'])) {
393 foreach($field['attributes'] as $attribute => $attbvalue) {
394 if (!empty($attbvalue) && $attbvalue != 'default' && $attbvalue != 'user'
395 && !isset($attributes[$attribute]) ) {
396 $attributes[$attribute] = $attbvalue;
397 }
398 }
399 }
400 // Remove the rel=nofollow for internal iframes.
401 if ($type != iframe_EXTERNAL && isset($attributes['rel']) && strpos($attributes['rel'], 'nofollow') !== FALSE) {
402 $attributes['rel'] = str_replace('nofollow', '', $attributes['rel']);
403 if (empty($attributes['rel'])) {
404 unset($attributes['rel']);
405 }
406 }
407 $item['attributes'] = $attributes;
408
409 // Add the widget label.
410 $item['label'] = $field['widget']['label'];
411
412 // Ad identifier for the iframe html-code
413 $item['html-id'] = 'iframe-'.$delta;
414 }
415
416 /**
417 * Implementation of hook_theme().
418 */
419 function iframe_theme() {
420 dmsg(3,'func iframe_theme');
421 return array(
422 'iframe_field_settings' => array(
423 'arguments' => array('element' => NULL),
424 ),
425 'iframe_formatter_default' => array(
426 'arguments' => array('element' => NULL),
427 ),
428 'iframe_formatter_iframeonly' => array(
429 'arguments' => array('element' => NULL),
430 ),
431 'iframe_formatter_asurl' => array(
432 'arguments' => array('element' => NULL),
433 ),
434 'iframe' => array(
435 'arguments' => array('element' => NULL),
436 ),
437 );
438 }
439
440 /**
441 * FAPI theme for an individual text elements.
442 */
443 function theme_iframe($element) {
444 dmsg(3,'func theme_iframe');
445 drupal_add_css(drupal_get_path('module', 'iframe') .'/iframe.css');
446
447 // Prefix single value iframe fields with the name of the field.
448 if (empty($element['#field']['multiple'])) {
449 if (isset($element['url']) && isset($element['title'])) {
450 $element['url']['#title'] = $element['#title'] .' '. $element['url']['#title'];
451 $element['title']['#title'] = $element['#title'] .' '. $element['title']['#title'];
452 }
453 elseif($element['url']) {
454 $element['url']['#title'] = $element['#title'];
455 }
456 }
457
458 $output = '';
459 $output .= '<div class="iframe-field-subrow clear-block">';
460
461 if (isset($element['title'])) {
462 $output .= '<div class="iframe-field-title iframe-field-column">' . theme('textfield', $element['title']) . '</div>';
463 }
464
465 $output .= '<div class="iframe-field-url' . (isset($element['title']) ? ' iframe-field-column' : '') . '">' . theme('textfield', $element['url']) . '</div>';
466
467 $output .= '<div>'.t('Width and Height of the IFrame').'</div>';
468
469
470 if (!empty($element['attributes'])) {
471 foreach($element['attributes'] as $attr) {
472 $output .= '<div class="iframe-attributes iframe-field-column">' . theme($attr['#type'], $attr, $attr['#value']) . '</div>';
473 }
474 }
475 $output .= '</div>';
476
477 return $output;
478 }
479
480 /**
481 * Implementation of hook_elements().
482 */
483 function iframe_elements() {
484 dmsg(3,'func iframe_elements');
485 $elements = array();
486 $elements['iframe'] = array(
487 '#input' => TRUE,
488 '#columns' => array('url', 'title'),
489 '#process' => array('iframe_process'),
490 );
491 return $elements;
492 }
493
494 /**
495 * Process the iframe type element before displaying the field.
496 *
497 * Build the form element. When creating a form using FAPI #process,
498 * note that $element['#value'] is already set.
499 *
500 * The $fields array is in $form['#field_info'][$element['#field_name']].
501 */
502 function iframe_process($element, $edit, $form_state, $form) {
503 dmsg(3,'func iframe_process');
504 $field = $form['#field_info'][$element['#field_name']];
505 $delta = $element['#delta'];
506 // load attributes by node
507 _iframe_load($element['#value']);
508 dmsg(4,'func iframe_process after _iframe_load');
509
510 $element['url'] = array(
511 '#type' => 'textfield',
512 '#maxlength' => '1024',
513 '#title' => t('URL'),
514 '#description' => $element['#description'],
515 '#required' => ($delta == 0 && $field['url'] !== 'optional') ? $element['#required'] : FALSE,
516 '#default_value' => isset($element['#value']['url']) ? $element['#value']['url'] : NULL,
517 );
518 if ($field['title'] != 'none' && $field['title'] != 'value') {
519 $element['title'] = array(
520 '#type' => 'textfield',
521 '#maxlength' => '255',
522 '#title' => t('Title'),
523 '#required' => ($delta == 0 && $field['title'] == 'required') ? $field['required'] : FALSE,
524 '#default_value' => isset($element['#value']['title']) ? $element['#value']['title'] : NULL,
525 );
526 }
527 $element['attributes']['width'] = array(
528 '#type' => 'textfield',
529 '#maxlength' => '4',
530 '#size' => '4',
531 '#title' => t('Width'),
532 '#required' => ($delta == 0 && $field['width'] == 'required') ? $field['required'] : FALSE,
533 '#default_value' => !empty($element['#value']['attributes']['width'])? $element['#value']['attributes']['width'] : $field['attributes']['width'],
534 );
535 $element['attributes']['height'] = array(
536 '#type' => 'textfield',
537 '#maxlength' => '4',
538 '#size' => '4',
539 '#title' => t('Height'),
540 '#required' => ($delta == 0 && $field['height'] == 'required') ? $field['required'] : FALSE,
541 '#default_value' => !empty($element['#value']['attributes']['height'])? $element['#value']['attributes']['height'] : $field['attributes']['height'],
542 );
543 $element['attributes']['frameborder'] = array(
544 '#type' => 'select',
545 '#title' => t('Frameborder'),
546 '#options' => array( '0' => t('no frameborder'), 'yes' => t('show frameborder') ),
547 '#description' => t('Frameborder is the border arround the iframe. Mostly people want it silent, so the default value for frameborder is 0.'),
548 '#required' => ($delta == 0 && $field['frameborder'] == 'required') ? $field['required'] : FALSE,
549 '#default_value' => !empty($element['#value']['attributes']['frameborder'])? $element['#value']['attributes']['frameborder'] : $field['attributes']['frameborder'],
550 );
551 $element['attributes']['scrolling'] = array(
552 '#type' => 'select',
553 '#title' => t('Scrolling'),
554 '#options' => array( 'auto' => t('Scrolling automatic'), 'no' => t('Scrolling disabled'), 'yes' => t('Scrolling enabled') ),
555 '#description' => t('Scrollbars help the user to reach all iframe content despite the real height of the iframe content. Please disable it only if You know what You are doing.'),
556 '#required' => ($delta == 0 && $field['scrolling'] == 'required') ? $field['required'] : FALSE,
557 '#default_value' => !empty($element['#value']['attributes']['scrolling'])? $element['#value']['attributes']['scrolling'] : $field['attributes']['scrolling'],
558 );
559 return $element;
560 }
561
562 /**
563 * Implementation of hook_field_formatter_info().
564 */
565 function iframe_field_formatter_info() {
566 dmsg(3,'func iframe_field_formatter_info');
567 return array(
568 'default' => array(
569 'label' => t('Title, over iframe (default)'),
570 'field types' => array('iframe'),
571 'multiple values' => CONTENT_HANDLE_CORE,
572 ),
573 'iframeonly' => array(
574 'label' => t('IFrame without title'),
575 'field types' => array('iframe'),
576 'multiple values' => CONTENT_HANDLE_CORE,
577 ),
578 'asurl' => array(
579 'label' => t('IFrame url as plain url'),
580 'field types' => array('iframe'),
581 'multiple values' => CONTENT_HANDLE_CORE,
582 ),
583 );
584 }
585
586 /**
587 * Theme function for 'default' text field formatter.
588 */
589 function theme_iframe_formatter_default($element) {
590 dmsg(3,'func theme_iframe_formatter_default');
591 // If no url given display nothing.
592 if (empty($element['#item']['url'])) return '';
593 // Display all
594 return iframe_iframe($element['#item']['display_title'], $element['#item']['url'], $element['#item']);
595 }
596
597 /**
598 * Theme function for 'iframeonly' text field formatter.
599 */
600 function theme_iframe_formatter_iframeonly($element) {
601 dmsg(3,'func theme_iframe_formatter_iframeonly');
602 // If no url given display nothing.
603 if (empty($element['#item']['url'])) return '';
604 // Display all
605 return iframe_iframe('', $element['#item']['url'], $element['#item']);
606 }
607
608 /**
609 * Theme function for 'asurl' text field formatter.
610 */
611 function theme_iframe_formatter_asurl($element) {
612 dmsg(3,'func theme_iframe_formatter_asurl');
613 // If no url given display nothing.
614 if (empty($element['#item']['url'])) return '';
615 // Display all
616 return l($element['#item']['display_title'], $element['#item']['url'], $element['#item']);
617 }
618
619 function iframe_token_list($type = 'all') {
620 dmsg(3,'func iframe_token_list');
621 if ($type == 'field' || $type == 'all') {
622 $tokens = array();
623
624 $tokens['iframe']['url'] = t("iframe URL");
625 $tokens['iframe']['title'] = t("iframe title");
626 $tokens['iframe']['view'] = t("Formatted html iframe");
627
628 return $tokens;
629 }
630 }
631
632 function iframe_token_values($type, $object = NULL) {
633 dmsg(3,'func iframe_token_values');
634 if ($type == 'field') {
635 $item = $object[0];
636
637 $tokens['url'] = $item['url'];
638 $tokens['title'] = $item['title'];
639 $tokens['view'] = isset($item['view']) ? $item['view'] : '';
640
641 return $tokens;
642 }
643 }
644
645 /**
646 * Implementation of hook_views_api().
647 */
648 function iframe_views_api() {
649 return array(
650 'api' => 2,
651 'path' => drupal_get_path('module', 'views') . '/views',
652 );
653 }
654
655 /**
656 * Forms a valid URL if possible from an entered address.
657 * Trims whitespace and automatically adds an http:// to addresses without a protocol specified
658 *
659 * @param string $url
660 * @param string $protocol The protocol to be prepended to the url if one is not specified
661 */
662 function iframe_cleanup_url($url, $protocol = "http") {
663 dmsg(3,'func iframe_cleanup_url');
664 $url = trim($url);
665 $type = iframe_validate_url($url);
666
667 if ($type == iframe_EXTERNAL) {
668 // Check if there is no protocol specified.
669 $protocol_match = preg_match("/^([a-z0-9][a-z0-9\.\-_äöü]*:\/\/)/i",$url);
670 if (empty($protocol_match)) {
671 // But should there be? Add an automatic http:// if it starts with a domain name.
672 $domain_match = preg_match('/^(([a-z0-9]([a-z0-9\-_äöü]*\.)+)('. iframe_DOMAINS .'|[a-z]{2}))/i',$url);
673 if (!empty($domain_match)) {
674 $url = $protocol."://".$url;
675 }
676 }
677 }
678
679 return $url;
680 }
681
682 /**
683 * A lenient verification for URLs. Accepts all URLs following RFC 1738 standard
684 * for URL formation and all email addresses following the RFC 2368 standard for
685 * mailto address formation.
686 *
687 * @param string $text
688 * @return mixed Returns boolean FALSE if the URL is not valid. On success, returns an object with
689 * the following attributes: protocol, hostname, ip, and port.
690 */
691 function iframe_validate_url($text) {
692 dmsg(3,'func iframe_validate_url');
693
694 $allowed_protocols = variable_get('filter_allowed_protocols', array('http', 'https', 'ftp', 'news', 'nntp', 'telnet', 'mailto', 'irc', 'ssh', 'sftp', 'webcal'));
695
696 $protocol = '((' . implode("|", $allowed_protocols) . '):\/\/)';
697 $authentication = '([a-z0-9]+(:[a-z0-9]+)?@)';
698 $domain = '(([a-z0-9]([a-z0-9\-_\[\]äöü])*)(\.(([a-z0-9\-_\[\]äöü])+\.)*('. iframe_DOMAINS .'|[a-z]{2}))?)';
699 $ipv4 = '([0-9]{1,3}(\.[0-9]{1,3}){3})';
700 $ipv6 = '([0-9a-fA-F]{1,4}(\:[0-9a-fA-F]{1,4}){7})';
701 $port = '(:([0-9]{1,5}))';
702
703 // Pattern specific to external iframes.
704 $external_pattern = '/^' . $protocol . '?' . $authentication . '?' . '(' . $domain . '|' . $ipv4 . '|' . $ipv6 . ' |localhost)' . $port . '?';
705
706 // Pattern specific to internal iframes.
707 $internal_pattern = "/^([a-z0-9_\-+\[\]]+)";
708
709 $directories = "(\/[a-z0-9_\-\.~+%=&,$'():;*@\[\]]*)*";
710 // Yes, four backslashes == a single backslash.
711 $query = "(\/?\?([?a-z0-9+_|\-\.\/\\\\%=&,$'():;*@\[\]]*))";
712 $anchor = "(#[a-z0-9_\-\.~+%=&,$'():;*@\[\]]*)";
713
714 // The rest of the path for a standard URL.
715 $end = $directories . '?' . $query . '?' . $anchor . '?' . '$/i';
716
717 $user = '[a-zA-Z0-9_\-\.\+\^!#\$%&*+\/\=\?\`\|\{\}~\'\[\]]+';
718 $email_pattern = '/^mailto:' . $user . '@' . '(' . $domain . '|' . $ipv4 .'|'. $ipv6 . '|localhost)' . $query . '?$/';
719
720 if (strpos($text, '<front>') === 0) {
721 return iframe_FRONT;
722 }
723 if (in_array('mailto', $allowed_protocols) && preg_match($email_pattern, $text)) {
724 return iframe_EMAIL;
725 }
726 if (preg_match($internal_pattern . $end, $text)) {
727 return iframe_INTERNAL;
728 }
729 if (preg_match($external_pattern . $end, $text)) {
730 return iframe_EXTERNAL;
731 }
732
733 return FALSE;
734 }
735
736 /*
737 * validate size: width and height of an iframe
738 */
739 function iframe_validate_size($size,$default="100") {
740 dmsg(3,'func iframe_validate_size (size='.$size.', default='.$default);
741 if (is_numeric($size))
742 return $size>0? $size : $default;
743 if (preg_match('/^\d+\%$/',$size)) return $size;
744 return $default;
745 }
746
747 /*
748 * like central function
749 * form the iframe code
750 */
751 function iframe_iframe($text, $path, $options=FALSE) {
752 dmsg(3,'func iframe_iframe');
753 if (!$options) $options=array();
754 // Merge in defaults.
755 //error_log('IFIF options='.implode(" ++ ",explode("\n",print_r($options,TRUE))));
756 $options += array(
757 'attributes' => array(),
758 'html' => FALSE,
759 );
760 if (!isset($options['attributes']['width'])) $options['attributes']['width']='100%';
761 if (!isset($options['attributes']['height'])) $options['attributes']['height']='701';
762 if (!isset($options['attributes']['frameborder']) || empty($options['attributes']['frameborder'])) $options['attributes']['frameborder']='0';
763 if (!isset($options['attributes']['scrolling']) || empty($options['attributes']['scrolling'])) $options['attributes']['scrolling']='auto';
764
765 $htmlid='';
766 if (isset($options['html-id']) && !empty($options['html-id'])) $htmlid=' id="'.$options['html-id'].'" name="'.$options['html-id'].'"';
767
768 // Append active class.
769 if ($path == $_GET['q'] || ($path == '<front>' && drupal_is_front_page())) {
770 if (isset($options['attributes']['class'])) {
771 $options['attributes']['class'] .= ' active';
772 }
773 else {
774 $options['attributes']['class'] = 'active';
775 }
776 }
777
778 // Remove all HTML and PHP tags from a tooltip. For best performance, we act only
779 // if a quick strpos() pre-check gave a suspicion (because strip_tags() is expensive).
780 if (isset($options['attributes']['title']) && strpos($options['attributes']['title'], '<') !== FALSE) {
781 $options['attributes']['title'] = strip_tags($options['attributes']['title']);
782 }
783 $optionsLink==array(); $optionsLink['attributes']=array();
784 $optionsLink['attributes']['title'] = $options['attributes']['title'];
785
786 include_once(drupal_get_path('module', 'content') .'/includes/content.crud.inc');
787
788 return (empty($text)? '' : '<div class="iframe_title">'.($options['html'] ? $text : check_plain($text)).'</div>').
789 '<iframe src="'. check_url(url($path, $options)) .'"'. drupal_attributes($options['attributes']) .
790 $htmlid.
791 '>'.
792 t('Your browser does not support iframes. But You can use the following link.').' '.l('Link',check_url(url($path,$options)),$optionsLink).'</iframe>';
793 }
794

  ViewVC Help
Powered by ViewVC 1.1.2