| 1 |
<?php
|
| 2 |
// $Id: inline.module,v 1.2 2006/02/02 09:03:48 frjo Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Original by matteo http://drupal.org/project/inline
|
| 7 |
* Modified by Fredrik Jonsson <fredrik at combonet dot se>
|
| 8 |
* More information at http://xdeb.org/drupaldev
|
| 9 |
*/
|
| 10 |
|
| 11 |
|
| 12 |
function inline_help($section = 'admin/help#inline') {
|
| 13 |
switch ($section) {
|
| 14 |
case 'admin/help#inline':
|
| 15 |
return t('<p>Sometimes a user may want to add an image or a file inside the body of a node. This can be done with special tags that are replaced by links to the corresponding uploaded file. If the file is an image, it will be displayed inline, otherwise a link to the file will be inserted. To enable this feature and learn the proper syntax, visit the <a href="%filters">filters configuration screen</a>.</p>', array('%filters' => url('admin/filters')));
|
| 16 |
case 'admin/modules#description':
|
| 17 |
return t('Allows users to insert uploaded files inline. Requires upload.module to work');
|
| 18 |
case 'filter#short-tip':
|
| 19 |
return t('You may add links to files uploaded with this node <a href="%explanation-url">using special tags</a>', array('%explanation-url' => url('filter/tips', NULL, 'image')));
|
| 20 |
case 'filter#long-tip':
|
| 21 |
return t('<p>You may link to files uploaded with the current node using special tags. The tags will be replaced by the corresponding files. Syntax: <code>[inline:file_id]</code>. Parameter: file_id represents the file uploaded with the node in which to link, assuming that the first uploaded file is labeled as 1 and so on.</p>
|
| 22 |
<p>If the file is an image, it will be displayed inline, otherwise a link to the file will be inserted.</p> ');
|
| 23 |
}
|
| 24 |
}
|
| 25 |
|
| 26 |
function inline_settings() {
|
| 27 |
$output = form_textfield(t('Maximum width and height for the displayed inline images (format: x,y)'), 'inline_img_dim', variable_get('inline_img_dim', '150,150'), 10, 10, t('This setting will affect the dimensions of displayed images. They will not be resized.'));
|
| 28 |
$output .= form_radios(t('Print link to images'), 'inline_link_img', variable_get('inline_link_img', 1), array('0' => t('Print only <img> tag'),'1' => t('Print <img> tag and make it a link to the image')));
|
| 29 |
$output .= form_textfield(t('Extra style 1 attributes to inline images'), 'inline_extra_style_1', variable_get('inline_extra_style_1', ''), 60, 150, t('With this setting you can add css style attributes to inline images, e.g. float: right; will make the images float to the right.'));
|
| 30 |
$output .= form_textfield(t('Extra style 2 attributes to inline images'), 'inline_extra_style_2', variable_get('inline_extra_style_2', ''), 60, 150, t('With this setting you can add css style attributes to inline images, e.g. float: left; will make the images float to the left.'));
|
| 31 |
$output .= form_textfield(t('Extra style 3 attributes to inline images'), 'inline_extra_style_3', variable_get('inline_extra_style_3', ''), 60, 150, t('With this setting you can add css style attributes to inline images, e.g. border: 1px solid #000; will put a black border on the images.'));
|
| 32 |
$output .= form_radios(t('Default extra style'), 'inline_extra_style_default', variable_get('inline_extra_style_default', 1), array('1' => t('Extra style 1'),'2' => t('Extra style 2'),'3' => t('Extra style 3')));
|
| 33 |
return $output;
|
| 34 |
}
|
| 35 |
|
| 36 |
function inline_filter($op, $delta = 0, $format = -1, $text = '') {
|
| 37 |
// The "list" operation provides the module an opportunity to declare both how
|
| 38 |
// many filters it defines and a human-readable name for each filter. Note that
|
| 39 |
// the returned name should be passed through t() for translation.
|
| 40 |
if ($op == 'list') {
|
| 41 |
return array(
|
| 42 |
0 => t('Inline file filter'));
|
| 43 |
}
|
| 44 |
|
| 45 |
// All operations besides "list" provide a $delta argument so we know which
|
| 46 |
// filter they refer to. We'll switch on that argument now so that we can
|
| 47 |
// discuss each filter in turn.
|
| 48 |
switch ($op) {
|
| 49 |
case 'description':
|
| 50 |
return t('Substitutes [inline:n] tags with the n:th file uploaded with the node.');
|
| 51 |
case 'prepare':
|
| 52 |
return $text;
|
| 53 |
case 'process':
|
| 54 |
return $text;
|
| 55 |
}
|
| 56 |
}
|
| 57 |
|
| 58 |
function inline_filter_tips($delta, $format, $long = false) {
|
| 59 |
if (user_access('upload files')) {
|
| 60 |
if ($long) {
|
| 61 |
return t('<p>You may link to files uploaded with the current node using special tags. The tags will be replaced by the corresponding files.<p>
|
| 62 |
|
| 63 |
<p>Syntax: <code>[inline|file|attachment:file#:style#=Title]</code></p>
|
| 64 |
|
| 65 |
<p>You can also link to files uploaded to other nodes.</p>
|
| 66 |
|
| 67 |
<p>Syntax: <code>[node:nid#.file#:style#=Title]</code></p>
|
| 68 |
|
| 69 |
<p>If no extra style is choosen the default style will be applied. If no title is choosen the file name is used.</p>
|
| 70 |
|
| 71 |
<p>Suppose you uploaded three files (in this order):</p>
|
| 72 |
<ul>
|
| 73 |
<li>imag1.png (referred as file #1)</li>
|
| 74 |
<li>file1.pdf (referred as file #2)</li>
|
| 75 |
<li>imag2.png (referred as file #3)</li>
|
| 76 |
</ul>
|
| 77 |
|
| 78 |
<p><code>[inline:1=Test] or [inline:imag1.png=Test]</code><br />
|
| 79 |
will be replaced by <code><img src="imag1.png" alt="Test" title="Test" /></code></p>
|
| 80 |
|
| 81 |
<p><code>[file:1=Test] or [file:imag1.png=Test]</code><br />
|
| 82 |
will be replaced by <code><a href="imag1.png">Test</a></code></p>
|
| 83 |
|
| 84 |
<p><code>[attachment:2=Test] or [attachment:file1.pdf=Test]</code><br />
|
| 85 |
will be replaced by <code><a href="file1.pdf">Test</a></code></p>
|
| 86 |
');
|
| 87 |
}
|
| 88 |
else {
|
| 89 |
return t('You may use <a href="%inline_help">[inline:x] tags</a> to display uploaded files or images inline.', array("%inline_help" => url("filter/tips/$format")));
|
| 90 |
}
|
| 91 |
}
|
| 92 |
else {
|
| 93 |
return '';
|
| 94 |
}
|
| 95 |
}
|
| 96 |
|
| 97 |
function inline_nodeapi(&$node, $op, $arg) {
|
| 98 |
if(is_array($node->files) && $op == 'view') {
|
| 99 |
$node->teaser = _inline_substitute_tags($node, 'teaser');
|
| 100 |
$node->body = _inline_substitute_tags($node, 'body');
|
| 101 |
}
|
| 102 |
}
|
| 103 |
|
| 104 |
function _inline_filename(&$node, $id) {
|
| 105 |
if (is_numeric($id)) {
|
| 106 |
$n = 1;
|
| 107 |
foreach ($node->files as $file) {
|
| 108 |
if ($n == $id) {
|
| 109 |
return array($file->filename, $file->filepath);
|
| 110 |
}
|
| 111 |
++$n;
|
| 112 |
}
|
| 113 |
return NULL;
|
| 114 |
}
|
| 115 |
else {
|
| 116 |
foreach ($node->files as $file) {
|
| 117 |
if ($file->filename == $id) {
|
| 118 |
return array($file->filename, $file->filepath);
|
| 119 |
}
|
| 120 |
}
|
| 121 |
return NULL;
|
| 122 |
}
|
| 123 |
}
|
| 124 |
|
| 125 |
function theme_inline_html($filename, $filepath, $title, $allow_inline_image, $style) {
|
| 126 |
|
| 127 |
$f_size = format_size(filesize($filepath));
|
| 128 |
|
| 129 |
//make a list with allowed image-tags
|
| 130 |
$extensions = 'jpg jpeg gif png';
|
| 131 |
$regex = '/\.('. ereg_replace(' +', '|', preg_quote($extensions)) .')$/i';
|
| 132 |
//make a list with allowed audio-tags
|
| 133 |
$extensions_audio = 'mp3';
|
| 134 |
$regex_audio = '/\.('. ereg_replace(' +', '|', preg_quote($extensions_audio)) .')$/i';
|
| 135 |
|
| 136 |
if (preg_match($regex, $filepath) && $allow_inline_image) {
|
| 137 |
$f_dim = getimagesize($filepath);
|
| 138 |
$f_width = $f_dim[0];
|
| 139 |
$f_height = $f_dim[1];
|
| 140 |
// read settings
|
| 141 |
$dim = explode(',',variable_get('inline_img_dim', '150,150'));
|
| 142 |
$width = $dim[0];
|
| 143 |
$height = $dim[1];
|
| 144 |
$extra_style = variable_get('inline_extra_style_'. ($style ? $style : variable_get('inline_extra_style_default', '1')), '');
|
| 145 |
// maintain aspect ration
|
| 146 |
if ($f_width > $width or $f_height > $height) {
|
| 147 |
// scale the image maintaining it's aspect ratio
|
| 148 |
if ($f_width / $width > $f_height / $height) {
|
| 149 |
$f_height = round($f_height * $width / $f_width);
|
| 150 |
$f_width = $width;
|
| 151 |
}
|
| 152 |
else {
|
| 153 |
$f_width = round($f_width * $height / $f_height);
|
| 154 |
$f_height = $height;
|
| 155 |
}
|
| 156 |
}
|
| 157 |
|
| 158 |
$html = '';
|
| 159 |
if (variable_get('inline_link_img', '1') == '1') {
|
| 160 |
$html = '<a href="'. file_create_url($filepath) . '" title="'.t("View").': '. $filename .'">';
|
| 161 |
}
|
| 162 |
if ($title != '') {
|
| 163 |
$html .= '<img style="width: '. $f_width .'px; height: '. $f_height .'px; '. $extra_style .'" src="'. file_create_url($filepath) .'" class="inline" alt="'. $title .'" title="'. $title .'" />';
|
| 164 |
}
|
| 165 |
else {
|
| 166 |
$html .= '<img style="width: '. $f_width .'px; height: '. $f_height .'px; '. $extra_style .'" src="'. file_create_url($filepath) .'" class="inline" alt="'. $filename .'" />';
|
| 167 |
}
|
| 168 |
if (variable_get('inline_link_img', '1') == '1') {
|
| 169 |
$html .= '</a>';
|
| 170 |
}
|
| 171 |
}
|
| 172 |
else if (preg_match($regex_audio, $filepath)) {
|
| 173 |
$module_path = drupal_get_path('module', 'inline');
|
| 174 |
$html = '<object type="application/x-shockwave-flash" data="'. $module_path .'/xspf_player_button.swf?&song_url='. file_create_url($filepath) .'" width="17" height="17">';
|
| 175 |
$html.= '<param name="allowScriptAccess" value="sameDomain" />';
|
| 176 |
$html.= '<param name="quality" value="high" />';
|
| 177 |
$html.= '<param name="movie" value="'. $module_path .'/xspf_player_button.swf?&song_url='. file_create_url($filepath) .'" />';
|
| 178 |
$html.= '<img src="'. $module_path .'/noflash.gif" width="17" height="17" alt="No Flash" /></object> ';
|
| 179 |
|
| 180 |
if ($title != '') {
|
| 181 |
$html.= '<a href="'. file_create_url($filepath) . '" title="'. t('Download') .': '. $filename .' | '. $f_size .'">'. $title .'</a>';
|
| 182 |
}
|
| 183 |
else {
|
| 184 |
$html.= '<a href="'. file_create_url($filepath) . '" title="'. t('Download') .': '. $filename .' | '. $f_size .'">'. $filename .'</a>';
|
| 185 |
}
|
| 186 |
}
|
| 187 |
else {
|
| 188 |
if ($title != '') {
|
| 189 |
$html = '<a href="'. file_create_url($filepath) . '" title="'. t('Download') .': '. $filename .' | '. $f_size .'">'. $title .'</a>';
|
| 190 |
}
|
| 191 |
else {
|
| 192 |
$html = '<a href="'. file_create_url($filepath) . '" title="'. t('Download') .': '. $filename .' | '. $f_size .'">'. $filename .'</a>';
|
| 193 |
}
|
| 194 |
}
|
| 195 |
|
| 196 |
return $html;
|
| 197 |
}
|
| 198 |
|
| 199 |
function _inline_substitute_tags(&$node, $field) {
|
| 200 |
if (preg_match_all("/\[(inline|file|attachment|node):([^=:\\]\\.]+)\\.?([0-9+])?:?(1|2|3)?=?([^\\]]*)?\]/i", $node->$field, $match)) {
|
| 201 |
foreach ($match[2] as $key => $value) {
|
| 202 |
$map[$value] = $key;
|
| 203 |
$mystyle = $match[4][$key];
|
| 204 |
$title = check_plain($match[5][$key]);
|
| 205 |
$mytype = $match[1][$key];
|
| 206 |
if ($mytype == 'node') {
|
| 207 |
$fromnode = node_load(array("nid" => $value));
|
| 208 |
$inline_file = _inline_filename($fromnode, $match[3][$key]);
|
| 209 |
$mytype = 'inline';
|
| 210 |
}
|
| 211 |
else {
|
| 212 |
$inline_file = _inline_filename($node, $value);
|
| 213 |
}
|
| 214 |
if ($inline_file != NULL) {
|
| 215 |
$replace = theme('inline_html', $inline_file[0], $inline_file[1], $title, $mytype == 'inline', $mystyle);
|
| 216 |
}
|
| 217 |
else {
|
| 218 |
$replace = '<span style="color:red; font-weight:bold">NOT FOUND: '. $value .'</span>';
|
| 219 |
}
|
| 220 |
$mtch[] = $match[0][$key];
|
| 221 |
$repl[] = $replace;
|
| 222 |
}
|
| 223 |
return str_replace($mtch, $repl, $node->$field);
|
| 224 |
}
|
| 225 |
|
| 226 |
return $node->$field;
|
| 227 |
}
|
| 228 |
|
| 229 |
?>
|