| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* The skelton module providing filtering of [l-macro|href|text|attribute] to appropriate
|
| 7 |
* rendering. Modules implementing hooks can extend by adding new implementations
|
| 8 |
*/
|
| 9 |
|
| 10 |
|
| 11 |
/**
|
| 12 |
* Default hook implementations provided in separate ulink_??.inc files
|
| 13 |
*/
|
| 14 |
require_once (drupal_get_path('module', 'ulink').'/ulink_node.inc');
|
| 15 |
require_once (drupal_get_path('module', 'ulink').'/ulink_image.inc');
|
| 16 |
require_once (drupal_get_path('module', 'ulink').'/ulink_user.inc');
|
| 17 |
require_once (drupal_get_path('module', 'ulink').'/ulink_comment.inc');
|
| 18 |
require_once (drupal_get_path('module', 'ulink').'/ulink_files.inc');
|
| 19 |
require_once (drupal_get_path('module', 'ulink').'/ulink_others.inc');
|
| 20 |
require_once (drupal_get_path('module', 'ulink').'/ulink_helper.inc');
|
| 21 |
|
| 22 |
/**
|
| 23 |
* Implementation of hook_menu().
|
| 24 |
*/
|
| 25 |
function ulink_menu($may_cache) {
|
| 26 |
$items = array();
|
| 27 |
$items[] = array(
|
| 28 |
'path' => 'admin/settings/ulink',
|
| 29 |
'title' => t('uLink'),
|
| 30 |
'description' => t('Manage universal filtering and auto-completion of links.'),
|
| 31 |
'callback' => 'drupal_get_form',
|
| 32 |
'callback arguments' => array('_ulink_settings'),
|
| 33 |
'access' => user_access('administer ulink settings'),
|
| 34 |
);
|
| 35 |
$items[] = array(
|
| 36 |
'path' => 'admin/settings/ulink/general',
|
| 37 |
'title' => t('General Settings'),
|
| 38 |
'description' => t('Manage universal filtering and auto-completion of links.'),
|
| 39 |
'access' => user_access('administer ulink settings'),
|
| 40 |
'type' => MENU_DEFAULT_LOCAL_TASK,
|
| 41 |
'weight' => 0,
|
| 42 |
);
|
| 43 |
// tab-menus for settings
|
| 44 |
$ls = array ('image','node','user','comment','files','others');
|
| 45 |
for ($i = 0; $i < 6; $i++) {
|
| 46 |
$items[] = array(
|
| 47 |
'path' => 'admin/settings/ulink/'.$ls[$i],
|
| 48 |
'title' => t($ls[$i]),
|
| 49 |
'description' => t('Settings related to how '.$ls[$i].' rendering is performed'),
|
| 50 |
'callback' => 'drupal_get_form',
|
| 51 |
'callback arguments' => array('_ulink_'.$ls[$i].'_settings'),
|
| 52 |
'access' => user_access('administer ulink settings'),
|
| 53 |
'type' => MENU_LOCAL_TASK,
|
| 54 |
'weight' => $i+1,
|
| 55 |
);
|
| 56 |
}
|
| 57 |
//registering menus required by the default implementation
|
| 58 |
for ($i = 0; $i < 6; $i++) {
|
| 59 |
for ($j = 1; $j < 4; $j++) {
|
| 60 |
$items[] = array(
|
| 61 |
'path' => 'admin/settings/ulink/'.$ls[$i].'/ulink_'.$j,
|
| 62 |
'title' => t($ls[$i].' rendering'),
|
| 63 |
'callback' => 'drupal_get_form',
|
| 64 |
'callback arguments' => array('_ulink_'.$ls[$i].'_settings_'.$j),
|
| 65 |
'access' => user_access('administer ulink settings'),
|
| 66 |
'type' => MENU_CALLBACK,
|
| 67 |
);
|
| 68 |
}
|
| 69 |
}
|
| 70 |
return $items;
|
| 71 |
}
|
| 72 |
|
| 73 |
/**
|
| 74 |
* Implementation of hook_perm().
|
| 75 |
*/
|
| 76 |
function ulink_perm() {
|
| 77 |
return array('administer ulink settings');
|
| 78 |
}
|
| 79 |
|
| 80 |
/**
|
| 81 |
* Implementation of hook_help().
|
| 82 |
*/
|
| 83 |
function ulink_help($section ) {
|
| 84 |
$output = '';
|
| 85 |
switch ($section) {
|
| 86 |
case 'admin/help#ulink':
|
| 87 |
return t(
|
| 88 |
'<p>uLink serves as both a universal filter.</p><p><strong>Universal Filter:</strong></p> <p>[l-macro|href|text|attribute] will be replaced by relevant HTML tags decided by the implementation. However it depends on the type of the target and the implementation selected in the <a href="!settings">settings page</a>. For instance, the html output may be different for image targets and file targets. Also for the same target, many implementatinos can be grouped by cascading or optionally adding (ie output this only when others return a null string). Implementation by ulink module (default) allows the admins to modify the rendering on the fly using PHPcode or Tokens. Check the <a href="!filters">filter tips </a> and <a href="!settings">settings page</a> for specific information provided by the implementing modules.</p><p><strong>Auto completion of Links :</strong></p><p>UI to implement autocompletion of the link in realtime.</p>',
|
| 89 |
array( '!settings' => url('admin/settings/ulink'), '!filters' => url('filter/tips'))
|
| 90 |
);
|
| 91 |
case 'filter#short-tip':
|
| 92 |
case 'filter#long-tip':
|
| 93 |
return t(
|
| 94 |
'The tag [l-macro|href|text|attribute] will be replaced with appropriate implementation. Refer to the <a href="!ulink_help">filter tips </a> for detailed information.', array("!ulink_help" => url("filter/tips/$format", NULL, 'filter-ulink'
|
| 95 |
)));
|
| 96 |
}
|
| 97 |
}
|
| 98 |
|
| 99 |
/**
|
| 100 |
* Implementation of hook_filter()
|
| 101 |
*/
|
| 102 |
function ulink_filter($op, $delta = 0, $format = -1, $text = '') {
|
| 103 |
|
| 104 |
if ($op == 'list') {
|
| 105 |
return array( 0 => t("ulink filter"));
|
| 106 |
}
|
| 107 |
switch ($op) {
|
| 108 |
// This description is shown in the administrative interface
|
| 109 |
case 'description':
|
| 110 |
return t('Substitutes HTML tags for [l-macro|href|text|attribute] .');
|
| 111 |
// We don't need the "prepare" operation for this filter.
|
| 112 |
case 'prepare':
|
| 113 |
return $text;
|
| 114 |
// The actual filtering is performed here.
|
| 115 |
case 'process':
|
| 116 |
return _ulink_substitute_tags($text);
|
| 117 |
case 'no cache':
|
| 118 |
return TRUE;
|
| 119 |
case 'settings':
|
| 120 |
$form['settings'] = array(
|
| 121 |
'#type' => 'fieldset',
|
| 122 |
'#title' => t('ulink filter settings'),
|
| 123 |
'#collapsible' => true,
|
| 124 |
'#collapsed' => false,
|
| 125 |
);
|
| 126 |
$form['settings']['div-tag'] = array(
|
| 127 |
'#type' => 'markup',
|
| 128 |
'#value' => 'goto: <a href="'.url("admin/settings/ulink").'"> uLink filter settings</a>'
|
| 129 |
);
|
| 130 |
return $form;
|
| 131 |
default:
|
| 132 |
return;
|
| 133 |
}
|
| 134 |
}
|
| 135 |
|
| 136 |
/**
|
| 137 |
* Implementation of hook_filter_tips()
|
| 138 |
*/
|
| 139 |
function ulink_filter_tips($delta, $format, $long = FALSE) {
|
| 140 |
if ($long) {
|
| 141 |
$output = t('
|
| 142 |
<a id="filter-ulink" name="filter-ulink"></a><p><strong>The tag [l|href|text|attribute] will be replaced with appropriate implementation.</strong> Rendering can be modified by choosing different <a href="!settings">modes of implementation</a>. Examples of rendering of the current mode is as following : <br/><em>Please be aware this is created dynamically so you can check with the mode, compare and configure properly.</em></p>
|
| 143 |
|
| 144 |
<table class="description"> <thead><tr><th>Tag</th><th>Rendered to</th></tr></thead>
|
| 145 |
<tbody>
|
| 146 |
<tr class="even"><td class="region" colspan="2"><a href="!image">Image</a></td></tr>
|
| 147 |
<tr class="odd"><td>[l|files/drupal.png]</td><td><code>'
|
| 148 |
.str_replace(array('<','>'),array('<','>'),_ulink_substitute_tags('[l|files/drupal.png]')).'</code></td></tr>
|
| 149 |
<tr class="odd"><td>[l|files/drupal.png|text]</td><td><code>'
|
| 150 |
.str_replace(array('<','>'),array('<','>'),_ulink_substitute_tags('[l|files/drupal.png|text]')).'</code></td></tr>
|
| 151 |
<tr class="even"><td>[l|files/drupal.png|text|no_imagecache=true]</td><td><code>'
|
| 152 |
.str_replace(array('<','>'),array('<','>'),_ulink_substitute_tags('[l|files/drupal.png|text|no_imagecache=true]')).'</code></td></tr>
|
| 153 |
<tr class="odd"><td>[l|files/drupal.png|text|title=hello,hi,width=400]</td><td><code>'
|
| 154 |
.str_replace(array('<','>'),array('<','>'),_ulink_substitute_tags('[l|files/drupal.png|text|title=hello,hi,width=400]')).'</code></td></tr>
|
| 155 |
<tr class="even"><td>[l|files/drupal.png|text|force_link=true]</td><td><code>'
|
| 156 |
.str_replace(array('<','>'),array('<','>'),_ulink_substitute_tags('[l|files/drupal.png|text|force_link=true]')).'</code></td> </tr>
|
| 157 |
|
| 158 |
<tr class="even"><td class="region" colspan="2"><a href="!node">Node</a></td></tr>
|
| 159 |
<tr class="odd"><td>[l|node/1]</td><td><code>'
|
| 160 |
.str_replace(array('<','>'),array('<','>'),_ulink_substitute_tags('[l|node/1]')).'</code></td></tr>
|
| 161 |
<tr class="odd"><td>[l|node/1|text]</td><td><code>'
|
| 162 |
.str_replace(array('<','>'),array('<','>'),_ulink_substitute_tags('[l|node/1|text]')).'</code></td></tr>
|
| 163 |
<tr class="odd"><td>[l|story/node|pathalias]</td><td><code>'
|
| 164 |
.str_replace(array('<','>'),array('<','>'),_ulink_substitute_tags('[l|story/node|pathalias]')).'</code></td></tr>
|
| 165 |
<tr class="even"><td>[l|node/6|text&title|title=title]</td><td><code>'.str_replace(array('<','>'),array('<','>'),_ulink_substitute_tags('[l|node/6|text&title|title=title]')).'</code></td></tr>
|
| 166 |
<tr class="odd"><td>[l|node/-60|broken]</td><td><code>'.str_replace(array('<','>'),array('<','>'),_ulink_substitute_tags('[l|node/-60|broken]')).'</code></td></tr>
|
| 167 |
|
| 168 |
<tr class="even"><td class="region" colspan="2"><a href="!user">User</a></td></tr>
|
| 169 |
<tr class="even"><td>[l|user/1]</td><td><code>'
|
| 170 |
.str_replace(array('<','>'),array('<','>'),_ulink_substitute_tags('[l|user/1]')).'</code></td></tr>
|
| 171 |
<tr class="even"><td>[l|user/1|root]</td><td><code>'
|
| 172 |
.str_replace(array('<','>'),array('<','>'),_ulink_substitute_tags('[l|user/1|root]')).'</code></td></tr>
|
| 173 |
<tr class="even"><td>[l|user/1|text|title=title]</td><td><code>'.str_replace(array('<','>'),array('<','>'),_ulink_substitute_tags('[l|user/1|text|title=title]')).'</code></td></tr>
|
| 174 |
<tr class="odd"><td>[l|user/-60|broken]</td><td><code>'.str_replace(array('<','>'),array('<','>'),_ulink_substitute_tags('[l|user/-60|broken]')).'</code></td></tr>
|
| 175 |
|
| 176 |
|
| 177 |
<tr class="even"><td class="region" colspan="2"><a href="!comment">Comment</a></td></tr>
|
| 178 |
<tr class="odd"><td>[l|node/4#comment-2]</td><td><code>'
|
| 179 |
.str_replace(array('<','>'),array('<','>'),_ulink_substitute_tags('[l|node/4#comment-2]')).'</code></td></tr>
|
| 180 |
<tr class="odd"><td>[l|node/4#comment-2|text]</td><td><code>'
|
| 181 |
.str_replace(array('<','>'),array('<','>'),_ulink_substitute_tags('[l|node/4#comment-2|text]')).'</code></td></tr>
|
| 182 |
|
| 183 |
<tr class="even"><td class="region" colspan="2"><a href="!files">Files</a></td></tr>
|
| 184 |
<tr class="even"><td>[l|files/CCNA1.rtf]</td><td><code>'.str_replace(array('<','>'),array('<','>'),_ulink_substitute_tags(' [l|files/CCNA1.rtf]')).'</code></td></tr>
|
| 185 |
<tr class="even"><td>[l|files/CCNA1.rtf|broken]</td><td><code>'.str_replace(array('<','>'),array('<','>'),_ulink_substitute_tags(' [l|files/CCNA1.rtf|broken]')).'</code></td></tr>
|
| 186 |
<tr class="even"><td>[l|files/CCNA1.rtf|broken|title=title]</td><td><code>'.str_replace(array('<','>'),array('<','>'),_ulink_substitute_tags(' [l|files/CCNA1.rtf|broken|title=title]')).'</code></td></tr>
|
| 187 |
|
| 188 |
<tr class="even"><td class="region" colspan="2"><a href="!others">Others</a></td></tr>
|
| 189 |
<tr class="odd"><td>[l|http://www.drupal.org] </td><td><code>'.str_replace(array('<','>'),array('<','>'),_ulink_substitute_tags('[l|http://www.drupal.org]')).'</code></td></tr>
|
| 190 |
<tr class="odd"><td>[l|http://mydomain.com/TheRule.pdf] </td><td><code>'.str_replace(array('<','>'),array('<','>'),_ulink_substitute_tags('[l|http://mydomain.com/TheRule.pdf]')).'</code></td></tr>
|
| 191 |
<tr class="odd"><td>[l|http://mydomain.com/TheRule.pdf|sample external file] </td><td><code>'.str_replace(array('<','>'),array('<','>'),_ulink_substitute_tags('[l|http://mydomain.com/TheRule.pdf|sample external file]')).'</code></td></tr>
|
| 192 |
<tr class="odd"><td>[l|http://mydomain.com/TheRule.pdf|sample external file|title=hello] </td><td><code>'.str_replace(array('<','>'),array('<','>'),_ulink_substitute_tags('[l|http://mydomain.com/TheRule.pdf|sample external file|title=hello]')).'</code></td></tr>
|
| 193 |
<tr class="even"><td>[l|misc/jquery.js|internal|title=title]</td><td><code>'
|
| 194 |
.str_replace(array('<','>'),array('<','>'),_ulink_substitute_tags('[l|misc/jquery.js|internal|title=title]')).'</code></td></tr>
|
| 195 |
</tbody></table>',
|
| 196 |
array (
|
| 197 |
'!settings' => url('admin/settings/ulink'),
|
| 198 |
'!image' => url('admin/settings/ulink/image'),
|
| 199 |
'!node' => url('admin/settings/ulink/node'),
|
| 200 |
'!user' => url('admin/settings/ulink/user'),
|
| 201 |
'!files' => url('admin/settings/ulink/files'),
|
| 202 |
'!comment' => url('admin/settings/ulink/comment'),
|
| 203 |
'!others' => url('admin/settings/ulink/others'),
|
| 204 |
));
|
| 205 |
return $output;
|
| 206 |
}
|
| 207 |
else {
|
| 208 |
return t('The tag [l|text|href|attribute] will be replaced with appropriate implementation. Refer to the <a href="!ulink_help">filter tips </a> for detailed information.', array("!ulink_help" => url("filter/tips/$format", NULL, 'filter-ulink')));
|
| 209 |
}
|
| 210 |
}
|
| 211 |
|
| 212 |
/**
|
| 213 |
* Implementation of hook_token_list()
|
| 214 |
*/
|
| 215 |
function ulink_token_list($type = 'all') {
|
| 216 |
if ($type == 'ulink') {
|
| 217 |
$tokens['ulink']['ulink-text'] = t("Text to be added link");
|
| 218 |
$tokens['ulink']['ulink-spath'] = t("Targets url as specified by the user");
|
| 219 |
$tokens['ulink']['ulink-path'] = t("Targets full url");
|
| 220 |
$tokens['ulink']['ulink-mime'] = t("MIME of the target if available, otherwise null ");
|
| 221 |
$tokens['ulink']['ulink-type'] = t("Type of the target if it is internal (eg: node), otherwise the protocol suite (eg: http:)");
|
| 222 |
$tokens['ulink']['ulink-attributes-string'] = t("Attributes delivered by the user in a string-format");
|
| 223 |
$tokens['ulink']['ulink-attributes-array'] = t("Attributes delivered by the user in a array-format");
|
| 224 |
}
|
| 225 |
if ($type == 'comment') {
|
| 226 |
$tokens['comment']['comment-cid'] = t("Comment ID");
|
| 227 |
$tokens['comment']['comment-nid'] = t("Node ID of the comment");
|
| 228 |
$tokens['comment']['comment-uid'] = t("User ID");
|
| 229 |
$tokens['comment']['comment-subject'] = t("Subject of the comment");
|
| 230 |
$tokens['comment']['comment-comment'] = t("Comment body");
|
| 231 |
$tokens['comment']['comment-hostname'] = t("Comment made from");
|
| 232 |
}
|
| 233 |
if ($type == 'files') {
|
| 234 |
$tokens['files']['files-fid'] = t("File ID");
|
| 235 |
$tokens['files']['files-nid'] = t("Node ID");
|
| 236 |
$tokens['files']['files-filename'] = t("File name eg:image1.jpg");
|
| 237 |
$tokens['files']['files-filepath'] = t("File name eg:files/image1.jpg");
|
| 238 |
$tokens['files']['files-mime'] = t("File mime eg:image/jpeg");
|
| 239 |
$tokens['files']['files-size'] = t("File mime eg:7500");
|
| 240 |
}
|
| 241 |
return $tokens;
|
| 242 |
}
|
| 243 |
/**
|
| 244 |
* Implementation of hook_token_values()
|
| 245 |
*/
|
| 246 |
function ulink_token_values($type, $object = NULL) {
|
| 247 |
switch ($type) {
|
| 248 |
case 'ulink':
|
| 249 |
if (isset($object)) {
|
| 250 |
$link = $object;
|
| 251 |
}
|
| 252 |
$values['ulink-path'] = url($link['path'], NULL);
|
| 253 |
$values['ulink-spath'] = $link['path'];
|
| 254 |
$values['ulink-text'] = $link['path'] ? $link['text'] : '';
|
| 255 |
$values['ulink-mime'] = $link['path'] ? $link['mime'] : '';
|
| 256 |
$values['ulink-type'] = $link['path'] ? $link['type'] : '';
|
| 257 |
$values['ulink-attributes-string'] = $link['path'] ? drupal_attributes($link['attributes']) : '';
|
| 258 |
$values['ulink-attributes-array'] = $link['path'] ? $link['attributes'] : '';
|
| 259 |
break;
|
| 260 |
case 'comment':
|
| 261 |
if (isset($object)) {
|
| 262 |
$c = $object;
|
| 263 |
}
|
| 264 |
$values['comment-cid'] = $c->cid;
|
| 265 |
$values['comment-nid'] = $c->cid ? $c->nid : '';
|
| 266 |
$values['comment-uid'] = $c->cid ? $c->uid : '';
|
| 267 |
$values['comment-subject'] = $c->cid ? $c->subject : '';
|
| 268 |
$values['comment-comment'] = $c->cid ? $c->comment : '';
|
| 269 |
$values['comment-hostname'] = $c->cid ? $c->hostname : '';
|
| 270 |
break;
|
| 271 |
case 'files':
|
| 272 |
if (isset($object)) {
|
| 273 |
$f = $object;
|
| 274 |
}
|
| 275 |
$values['files-fid'] = $f->fid;
|
| 276 |
$values['files-nid'] = $f->fid ? $f->nid : '';
|
| 277 |
$values['files-filename'] = $f->fid ? $f->filename : '';
|
| 278 |
$values['files-filepath'] = $f->fid ? $f->filepath : '';
|
| 279 |
$values['files-mime'] = $f->fid ? $f->filemime : '';
|
| 280 |
$values['files-size'] = $f->fid ? $f->filesize : '';
|
| 281 |
break;
|
| 282 |
default:
|
| 283 |
break;
|
| 284 |
}
|
| 285 |
return $values;
|
| 286 |
}
|
| 287 |
/*
|
| 288 |
* helper function to parse the attributes into an array, parsing is performed based on comma separaction and value is extracted using the equal sign
|
| 289 |
*
|
| 290 |
* @param $attr
|
| 291 |
* String to be parsed, assuming it is in the format key=value,keyvalue,...
|
| 292 |
* @return
|
| 293 |
* Array with parsed key value pairs in the format {key=>value, key=>value, ..}
|
| 294 |
*/
|
| 295 |
function ulink_create_attribute_array($attr) {
|
| 296 |
if (!strstr($attr,'='))
|
| 297 |
return array();
|
| 298 |
$attributes = explode(',',$attr);
|
| 299 |
$attributes_array = array();
|
| 300 |
foreach ($attributes as $at) {
|
| 301 |
if (strstr($at,'=')) {
|
| 302 |
$at1 = explode('=',$at);
|
| 303 |
$attributes_array = array_merge($attributes_array, array($at1[0] =>$at1[1]));
|
| 304 |
}
|
| 305 |
else {
|
| 306 |
$key = key($attributes_array);
|
| 307 |
$attributes_array[$key] = $attributes_array[$key].','.$at;
|
| 308 |
}
|
| 309 |
}
|
| 310 |
return $attributes_array;
|
| 311 |
}
|
| 312 |
|
| 313 |
/**
|
| 314 |
* Provides the general settings form
|
| 315 |
*
|
| 316 |
* This functions oupputs the settings form, for general settings and calls ulink_general_settings to populated the settings pages with
|
| 317 |
* implementing modules settings
|
| 318 |
*/
|
| 319 |
function _ulink_settings() {
|
| 320 |
$form = array();
|
| 321 |
// Check if uLink filter is enabled
|
| 322 |
$ulink_activated = false;
|
| 323 |
foreach (filter_formats() as $format) {
|
| 324 |
foreach (filter_list_format($format->format) as $filter) {
|
| 325 |
if ($filter->module == 'ulink') {
|
| 326 |
$ulink_activated = true;
|
| 327 |
break;
|
| 328 |
}
|
| 329 |
}
|
| 330 |
}
|
| 331 |
if ($ulink_activated == false) {
|
| 332 |
drupal_set_message(t('uLink filter is not yet enabled for at least one <a href="!formats">input format</a>.', array('
|
| 333 |
!formats' => url('admin/settings/filters'))), 'error');
|
| 334 |
}
|
| 335 |
|
| 336 |
foreach (module_implements('ulink_general_settings') as $module) {
|
| 337 |
$form[$module]= array(
|
| 338 |
'#type' => 'fieldset',
|
| 339 |
'#title' => t($module.' - settings'),
|
| 340 |
'#collapsible' => false,
|
| 341 |
'#description' => t('Settings generated by '.$module),
|
| 342 |
);
|
| 343 |
$form[$module]['#weight'] = ($module == 'ulink') ? -1 : 0;
|
| 344 |
$form[$module]['settings'] = module_invoke($module, 'ulink_general_settings');
|
| 345 |
}
|
| 346 |
return system_settings_form($form);
|
| 347 |
}
|
| 348 |
|
| 349 |
/**
|
| 350 |
* Search for the ulink tag, retrieve the target type, get the implementation for the type, casecade the outputs and substitute it in the string
|
| 351 |
*
|
| 352 |
* @param $text
|
| 353 |
* String to be searched for tags.
|
| 354 |
* @return
|
| 355 |
* String where [l-macro|href|text|attributes] substituted with relevant html code
|
| 356 |
*/
|
| 357 |
function _ulink_substitute_tags($text){
|
| 358 |
|
| 359 |
// get configured macros
|
| 360 |
$macroarray = array();
|
| 361 |
$macro = variable_get('ulink_i_macro', "");
|
| 362 |
if($macro)
|
| 363 |
eval('$macroarray = array'.$macro.";"); // user is expected to enter the list as key=>value pair with surrounding parenthesis
|
| 364 |
|
| 365 |
if(preg_match_all("/\[l-?([^|\[\]]*)\|([^|\]\[]+)\|?([^|\]\[]*)\|?([^|\]\[]*)\]/i",$text,$match)){
|
| 366 |
foreach($match[2] as $key=>$val){
|
| 367 |
// $match[1] = macro $match[2] = href $match[3]= text and $match[4]= attributes
|
| 368 |
|
| 369 |
$mtch[] = $match[0][$key];
|
| 370 |
$img = explode(' ', strtolower(variable_get('ulink_image_ext', "jpg png gif")));
|
| 371 |
$match[1][$key] = check_plain($match[1][$key]);
|
| 372 |
$path = check_url(drupal_get_normal_path($match[2][$key]));
|
| 373 |
$match[3][$key] = check_plain($match[3][$key]);
|
| 374 |
$match[4][$key] = check_plain($match[4][$key]);
|
| 375 |
if($match[1][$key]) {
|
| 376 |
$match[4][$key] = $match[4][$key]?
|
| 377 |
$macroarray[$match[1][$key]].",".$match[4][$key] :
|
| 378 |
$macroarray[$match[1][$key]];
|
| 379 |
}
|
| 380 |
$type = array_shift(explode('/', $path));
|
| 381 |
$path_parts = pathinfo($path);
|
| 382 |
$base = $path_parts["basename"];
|
| 383 |
|
| 384 |
$result = '';
|
| 385 |
$link = array(
|
| 386 |
'path' => $path, //complete path passed by the user
|
| 387 |
'text' => $match[3][$key],
|
| 388 |
'type' => $type, // http: - external, node/user/files - internal
|
| 389 |
'mime' => strtolower($path_parts["extension"]), // null if no extension
|
| 390 |
'attributes' => ulink_create_attribute_array($match[4][$key]),
|
| 391 |
'attr' => $match[4][$key],
|
| 392 |
);
|
| 393 |
$title = $link['text'] ? $link['text'] : $link['path'] ;
|
| 394 |
$link['attributes']['title'] = $link['attributes']['title'] ? $link['attributes']['title'] : $title;
|
| 395 |
$link['attributes']['alt'] = $link['attributes']['alt'] ? $link['attributes']['alt'] : $title;
|
| 396 |
|
| 397 |
// internal links
|
| 398 |
|
| 399 |
// to handle internal non-drupal internal links, ie if links starts with '/'
|
| 400 |
if ($link['path'][0] == "/") {
|
| 401 |
global $base_url;
|
| 402 |
$link['path'] = $base_url.$link['path'];
|
| 403 |
}
|
| 404 |
|
| 405 |
// internal file
|
| 406 |
if (file_exists($link['path'])) {
|
| 407 |
$file = db_fetch_object(db_query("SELECT * FROM {files} WHERE filepath LIKE '%s'", $link['path']));
|
| 408 |
if ($file == NULL) {
|
| 409 |
$file = new StdClass();
|
| 410 |
$file->fid = -1;
|
| 411 |
$file->nid = 0;
|
| 412 |
$file->filename = $base;
|
| 413 |
$file->filepath = $link['path'];
|
| 414 |
$file->filesize = filesize($link['path']);
|
| 415 |
$file->filemime = $link['mime'];
|
| 416 |
}
|
| 417 |
$link = array_merge($link, array('files' => $file));
|
| 418 |
if (in_array($link['mime'], $img) && strncasecmp($link['attributes']['force_link'], "TRUE", 4)) {
|
| 419 |
$result = _ulink_substitute_tags_helper('image', $link, $file);
|
| 420 |
}
|
| 421 |
else {
|
| 422 |
$result = _ulink_substitute_tags_helper('files', $link, $file);
|
| 423 |
}
|
| 424 |
}
|
| 425 |
|
| 426 |
//nodes and comments
|
| 427 |
else if (!strcasecmp($type, "node")){
|
| 428 |
if (preg_match("/(\d+)#comment-(\d+)/i", $base, $localmatch)) {
|
| 429 |
$comment = module_exists('comment') ? _comment_load($localmatch[2]) : NULL;
|
| 430 |
$node = node_load($localmatch[1]);
|
| 431 |
$link = array_merge($link, array(
|
| 432 |
'nid' => $localmatch[1],
|
| 433 |
'cid' => $localmatch[2],
|
| 434 |
'comment' => $comment,
|
| 435 |
'node' => $node
|
| 436 |
));
|
| 437 |
$result = _ulink_substitute_tags_helper('comment', $link, $comment);
|
| 438 |
|
| 439 |
if (variable_get('clean_url', '0')) {
|
| 440 |
$result = str_replace('%2523', '#', $result);
|
| 441 |
}
|
| 442 |
else {
|
| 443 |
$result = str_replace('%23', '#', $result);
|
| 444 |
}
|
| 445 |
}
|
| 446 |
else {
|
| 447 |
$node = $base ? node_load($base) : NULL;
|
| 448 |
$link = array_merge($link, array('nid' => $base, 'node' => $node));
|
| 449 |
$result = _ulink_substitute_tags_helper('node', $link, $node);
|
| 450 |
}
|
| 451 |
}
|
| 452 |
|
| 453 |
// type - users
|
| 454 |
else if (!strcasecmp($type, "user")) {
|
| 455 |
$user = $base ? user_load(array('uid' => (int)$base)) : NULL;
|
| 456 |
$link = array_merge($link, array('uid' => $uid, 'user' => $user));
|
| 457 |
$result = _ulink_substitute_tags_helper('user', $link, $user);
|
| 458 |
}
|
| 459 |
|
| 460 |
// external links
|
| 461 |
else {
|
| 462 |
$link = array_merge($link, array('external' => 'true'));
|
| 463 |
if (in_array($link['mime'], $img) && strncasecmp($link['attributes']['force_link'], "TRUE", 4)) {
|
| 464 |
$result = _ulink_substitute_tags_helper('image', $link);
|
| 465 |
}
|
| 466 |
else {
|
| 467 |
$result = _ulink_substitute_tags_helper('others', $link);
|
| 468 |
}
|
| 469 |
}
|
| 470 |
$rep[] = $result;
|
| 471 |
}
|
| 472 |
return str_replace($mtch, $rep, $text);
|
| 473 |
}
|
| 474 |
else {
|
| 475 |
return $text;
|
| 476 |
}
|
| 477 |
}
|
| 478 |
/**
|
| 479 |
* Implementation of hook_ulink_general_settings()
|
| 480 |
*/
|
| 481 |
function ulink_ulink_general_settings() {
|
| 482 |
$form['general']= array(
|
| 483 |
'#type' => 'fieldset',
|
| 484 |
'#title' => t('General settings'),
|
| 485 |
'#collapsible' => FALSE,
|
| 486 |
'#weight' => -2,
|
| 487 |
);
|
| 488 |
$form['general']['ulink_i_macro']= array(
|
| 489 |
'#type' => 'textarea',
|
| 490 |
'#title' => t('Macros'),
|
| 491 |
'#default_value' => variable_get('ulink_i_macro', ""),
|
| 492 |
'#description' => t('Enter the macros for the attributes. eg: <br />("imagelist"=>"class=imagelist", "photos"=>"width=200,height=100")<br />They can be called by [l-imagelist|... and [l-photos|.. respectively'),
|
| 493 |
);
|
| 494 |
return $form;
|
| 495 |
}
|
| 496 |
|
| 497 |
|
| 498 |
/**
|
| 499 |
* Called by ulink/image menu and provides specific settings for image type. Calls _ulink_settings_helper("image") to populate the settings page
|
| 500 |
**/
|
| 501 |
function _ulink_image_settings() {
|
| 502 |
$form = array();
|
| 503 |
$option_array=array();
|
| 504 |
|
| 505 |
$form['image']= array(
|
| 506 |
'#type' => 'fieldset',
|
| 507 |
'#title' => t('Image extensions and filtering'),
|
| 508 |
'#collapsible' => FALSE,
|
| 509 |
'#description' => t('Settings related to the rendering of IMG tag'),
|
| 510 |
);
|
| 511 |
$form['image']['ulink_image_ext'] = array(
|
| 512 |
'#type' => 'textfield',
|
| 513 |
'#title' => t('Extensions supported by IMG tag'),
|
| 514 |
'#size' => 60,
|
| 515 |
'#maxlength' => 64,
|
| 516 |
'#default_value' => variable_get('ulink_image_ext', "jpg png gif"),
|
| 517 |
'#description' => t("Enter the extensions separated by space. eg: jpg png gif"),
|
| 518 |
);
|
| 519 |
$form['modules'] = _ulink_settings_helper('image');
|
| 520 |
return system_settings_form($form);
|
| 521 |
}
|
| 522 |
|
| 523 |
/**
|
| 524 |
* Called by ulink/node menu for node type. Calls _ulink_settings_helper("node") to populate the settings page
|
| 525 |
**/
|
| 526 |
function _ulink_node_settings() {
|
| 527 |
$form['modules'] = _ulink_settings_helper('node');
|
| 528 |
return system_settings_form($form);
|
| 529 |
}
|
| 530 |
|
| 531 |
/**
|
| 532 |
* Called by ulink/user menu for user type. Calls _ulink_settings_helper("user") to populate the settings page
|
| 533 |
**/
|
| 534 |
function _ulink_user_settings() {
|
| 535 |
$form['modules'] = _ulink_settings_helper('user');
|
| 536 |
return system_settings_form($form);
|
| 537 |
}
|
| 538 |
|
| 539 |
/**
|
| 540 |
* Called by ulink/comment menu for commment type. Calls _ulink_settings_helper("comment") to populate the settings page
|
| 541 |
**/
|
| 542 |
function _ulink_comment_settings() {
|
| 543 |
$form['modules'] = _ulink_settings_helper('comment');
|
| 544 |
return system_settings_form($form);
|
| 545 |
}
|
| 546 |
|
| 547 |
/**
|
| 548 |
* Called by ulink/files menu for files type. Calls _ulink_settings_helper("files") to populate the settings page
|
| 549 |
**/
|
| 550 |
function _ulink_files_settings() {
|
| 551 |
$form['modules'] = _ulink_settings_helper('files');
|
| 552 |
return system_settings_form($form);
|
| 553 |
}
|
| 554 |
|
| 555 |
/**
|
| 556 |
* Called by ulink/others menu for others type. Calls _ulink_settings_helper("others") to populate the settings page
|
| 557 |
**/
|
| 558 |
function _ulink_others_settings() {
|
| 559 |
$form['modules'] = _ulink_settings_helper('others');
|
| 560 |
return system_settings_form($form);
|
| 561 |
}
|
| 562 |
|
| 563 |
/**
|
| 564 |
* Helper function populates the settings page by calling the settings hook (eg ulink_node_settings)
|
| 565 |
*
|
| 566 |
* @param $type
|
| 567 |
* Type of the implementation - ulink_node or ulink_comment or ulink_files or ulink_user or ulink_others or ulink_image
|
| 568 |
*/
|
| 569 |
function _ulink_settings_helper($type) {
|
| 570 |
$modules = array();
|
| 571 |
foreach (module_implements('ulink_'.$type) as $module) {
|
| 572 |
$default = ($module == 'ulink') ? 1 : 0;
|
| 573 |
if (variable_get('ulink_'.$type.'_enable_'.$module, $default)) {
|
| 574 |
$modules[$module] = variable_get('ulink_'.$type.'_weight_'.$module, -10);
|
| 575 |
}
|
| 576 |
}
|
| 577 |
asort ($modules);
|
| 578 |
reset ($modules);
|
| 579 |
$module_list = '';
|
| 580 |
while (list ($key, $val) = each ($modules)) {
|
| 581 |
$default = ($key == 'ulink') ? 1 : 0;
|
| 582 |
if (variable_get('ulink_'.$type.'_enable_'.$key, $default) == 1) {
|
| 583 |
$module_list = $module_list ? $module_list.' . '.$key : $key;
|
| 584 |
}
|
| 585 |
else if (variable_get('ulink_'.$type.'_enable_'.$key, $default) == 2) {
|
| 586 |
$module_list = $module_list ? $module_list.' ? '.$key : $key;
|
| 587 |
}
|
| 588 |
}
|
| 589 |
|
| 590 |
foreach (module_implements('ulink_'.$type) as $module) {
|
| 591 |
$default = ($module == 'ulink') ? 1 : 0;
|
| 592 |
$form[$module]= array(
|
| 593 |
'#type' => 'fieldset',
|
| 594 |
'#title' => t($module.' - module imeplementation.'),
|
| 595 |
'#collapsible' => true,
|
| 596 |
'#collapsed' => !variable_get('ulink_'.$type.'_enable_'.$module, $default),
|
| 597 |
'#description' => t('Settings generated by '.$module.' module.'.module_invoke($module, 'ulink_'.$type.'_info')),
|
| 598 |
);
|
| 599 |
$form[$module]['#weight'] = (variable_get('ulink_'.$type.'_enable_'.$module, $default) == 0) ? 10 : variable_get('ulink_'.$type.'_weight_'.$module, -10);
|
| 600 |
|
| 601 |
$form[$module]['multi'] = array('#type' => 'fieldset');
|
| 602 |
$form[$module]['multi'] ['ulink_'.$type.'_enable_'.$module] = array(
|
| 603 |
'#type' => 'radios',
|
| 604 |
'#title' => t($module." - module's implementation"),
|
| 605 |
'#description' => t('Mulitple selections results in a cascaded output depends on the choice.'),
|
| 606 |
'#default_value' => variable_get('ulink_'.$type.'_enable_'.$module, $default),
|
| 607 |
'#options' => array( 0 => t('Disabled'), 1 => t("Cascade the module's rendering to the output"), 2 => t("Cascade only if the previous output is empty")),
|
| 608 |
);
|
| 609 |
$form[$module]['multi']['ulink_'.$type.'_weight_'.$module] = array(
|
| 610 |
'#type' => 'weight',
|
| 611 |
'#title' => t('Weight'),
|
| 612 |
'#default_value' => variable_get('ulink_'.$type.'_weight_'.$module, $default),
|
| 613 |
'#delta' => 10,
|
| 614 |
'#description' => t('Multiple renderings are cascaded in the order of their weights.'),
|
| 615 |
);
|
| 616 |
$form[$module]['settings'] = module_invoke($module, 'ulink_'.$type.'_settings');
|
| 617 |
}
|
| 618 |
if ($module_list) {
|
| 619 |
drupal_set_message("Following modules' implementations are selected for $type type : $module_list");
|
| 620 |
}
|
| 621 |
else {
|
| 622 |
drupal_set_message("No module's implementation is selected for $type type", 'error');
|
| 623 |
}
|
| 624 |
return $form;
|
| 625 |
}
|
| 626 |
|
| 627 |
/**
|
| 628 |
* Calls the modules giving implementations for ulink_xxx based on their availability and cascades the output dependent on the weight given by the user
|
| 629 |
*
|
| 630 |
* calls the modules iimplementing ulink_?? (eg ulink_node or ulink_image). Then based on whether they are enabled
|
| 631 |
* or not they are displayed in an extended or collapsed framset. And they are sort in the order of the preference set
|
| 632 |
* by the user in the settings page. User can select the implemenations as he wishes and he can arrange the order of cascading, if multiple
|
| 633 |
* implementations are selected and he could optionaly (ie this will be called only when all preceding modules return empty output) add implementations
|
| 634 |
* at the end
|
| 635 |
*
|
| 636 |
* @param $type
|
| 637 |
* Type of the implementation - ulink_node or ulink_comment or ulink_files or ulink_user or ulink_others or ulink_image
|
| 638 |
* @param $link array
|
| 639 |
* Contains the information about the target
|
| 640 |
* @param $object
|
| 641 |
* Contains the object if the target is internal and available(eg node)
|
| 642 |
* @return
|
| 643 |
* outputs by implementing modules in html code cascaded together.
|
| 644 |
*/
|
| 645 |
function _ulink_substitute_tags_helper($type, $link, $object = NULL) {
|
| 646 |
$modules = array();
|
| 647 |
foreach (module_implements('ulink_'.$type) as $module) {
|
| 648 |
$default = ($module == 'ulink') ? 1 : 0;
|
| 649 |
if (variable_get('ulink_'.$type.'_enable_'.$module, $default)) {
|
| 650 |
$modules[$module] = variable_get('ulink_'.$type.'_weight_'.$module, -10);
|
| 651 |
}
|
| 652 |
}
|
| 653 |
asort ($modules);
|
| 654 |
reset ($modules);
|
| 655 |
$result = '';
|
| 656 |
while (list ($key, $val) = each ($modules)) {
|
| 657 |
$default = ($key == 'ulink') ? 1 : 0;
|
| 658 |
if (variable_get('ulink_'.$type.'_enable_'.$key, $default) == 2) {
|
| 659 |
if (!$result && ($temp = module_invoke($key, 'ulink_'.$type, $link, $object))) {
|
| 660 |
return $temp;
|
| 661 |
}
|
| 662 |
else {
|
| 663 |
continue;
|
| 664 |
}
|
| 665 |
}
|
| 666 |
$result .= module_invoke($key, 'ulink_'.$type, $link, $object);
|
| 667 |
}
|
| 668 |
return $result;
|
| 669 |
}
|
| 670 |
|
| 671 |
/**
|
| 672 |
* Default hook implementations provided in separate ulink_??.inc files
|
| 673 |
*/
|
| 674 |
|