summaryrefslogtreecommitdiff
path: root/inc/format.php
blob: 68f15fe129dd60c0f0fd9959f999fb13e2f9e951 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
<?
require_once("conf/dokuwiki.php");
require_once("inc/common.php");


/**
 * Assembles all parts defined by the link formater below
 * Returns HTML for the link
 */
function format_link_build($link){
  //make sure the url is XHTML compliant
  $link['url'] = str_replace('&','&amp;',$link['url']);
  $link['url'] = str_replace('&amp;amp;','&amp;',$link['url']);

  $ret  = '';
  $ret .= $link['pre'];
  $ret .= '<a href="'.$link['url'].'"';
  if($link['class'])  $ret .= ' class="'.$link['class'].'"';
  if($link['target']) $ret .= ' target="'.$link['target'].'"';
  if($link['title'])  $ret .= ' title="'.$link['title'].'"';
  if($link['style'])  $ret .= ' style="'.$link['style'].'"';
  if($link['more'])   $ret .= ' '.$link['more'];
  $ret .= '>';
  $ret .= $link['name'];
  $ret .= '</a>';
  $ret .= $link['suf'];
  return $ret;
}

/**
 * Link Formaters
 *
 * Each of these functions need to set
 *
 * $link['url']    URL to use in href=""
 * $link['name']   HTML to enclose in <a> with proper special char encoding
 * $link['class']  CSS class to set on link
 * $link['target'] which target to use (blank) for current window
 * $link['style']  Additonal style attribute set with style=""
 * $link['title']  Title to set with title="" 
 * $link['pre']    HTML to prepend to link
 * $link['suf']    HTML to append to link
 * $link['more']   Additonal HTML to include into the anchortag
 *
 */

function format_link_wiki($link){
  global $conf;
  global $ID; //we use this to get the current namespace
  //obvious setup
  $link['target'] = $conf['target']['wiki'];
  $link['style']  = '';
  $link['pre']    = '';
  $link['suf']   = '';
  $link['more']   = 'onclick="return svchk()" onkeypress="return svchk()"';

  //if links starts with . add current namespace if any
  if(strpos($link['url'],'.')===0){
    $ns = substr($ID,0,strrpos($ID,':')); #get current ns
    $link['url'] = $ns.':'.substr($link['url'],1);
  }

  //if link contains no namespace. add current namespace (if any)
  if(strpos($ID,':')!==false  && strpos($link['url'],':') === false){
    $ns = substr($ID,0,strrpos($ID,':')); #get current ns
    $link['url'] = $ns.':'.$link['url'];
  }

  //keep hashlink if exists
  list($link['url'],$hash) = split('#',$link['url'],2);
  $hash = cleanID($hash);

  //use link without namespace as name
  if(empty($link['name'])) $link['name'] = preg_replace('/.*:/','',$link['url']);
  $link['name'] = htmlspecialchars($link['name']);

  $link['url'] = cleanID($link['url']);
  $link['title'] = $link['url'];

  //set class depending on existance
  $file = wikiFN($link['url']);
  if(@file_exists($file)){
    $link['class']="wikilink1";
  }else{
    if($conf['autoplural']){
      //try plural/nonplural
      if(substr($link['url'],-1) == 's'){
        $try = substr($link['url'],0,-1);
      }else{
        $try = $link['url'].'s';
      }
      $file = wikiFN($try);
      //check if the other form exists
      if(@file_exists($file)){
        $link['class']="wikilink1";
        $link['url'] = $try;
      }else{
        $link['class']="wikilink2";
      }
    }else{
      //no autoplural is wanted
      $link['class']="wikilink2";
    }
  }

  //construct the full link
  $link['url'] = wl($link['url']);

  //add hash if exists
  if($hash) $link['url'] .= '#'.$hash;

  return $link;
}

function format_link_externalurl($link){
  global $conf;
  //simple setup
  $link['class']  = 'urlextern';
  $link['target'] = $conf['target']['extern'];
  $link['pre']    = '';
  $link['suf']    = '';
  $link['style']  = '';
  $link['more']   = 'onclick="return svchk()" onkeypress="return svchk()"';
  $link['url']    = $link['url']; //keep it
  $link['title']  = htmlspecialchars($link['url']);
  if(!$link['name']) $link['name'] = htmlspecialchars($link['url']);
  //thats it :-)
  return $link;
}

//this only works in IE :-(
function format_link_windows($link){
  global $conf;
  global $lang;
  //simple setup
  $link['class']  = 'windows';
  $link['target'] = $conf['target']['windows'];
  $link['pre']    = '';
  $link['suf']   = '';
  $link['style']  = '';
  //Display error on browsers other than IE
  $link['more'] = 'onclick="if(document.all == null){alert(\''.htmlspecialchars($lang['nosmblinks'],ENT_QUOTES).'\');}" onkeypress="if(document.all == null){alert(\''.htmlspecialchars($lang['nosmblinks'],ENT_QUOTES).'\');}"';

  if(!$link['name']) $link['name'] = htmlspecialchars($link['url']);
  $link['title'] = htmlspecialchars($link['url']);
  $link['url']   = str_replace('\\','/',$link['url']);
  $link['url']   = 'file:///'.$link['url'];

  return $link;
}

function format_link_email($link){
  global $conf;
  //simple setup
  $link['class']  = 'mail';
  $link['target'] = '';
  $link['pre']    = '';
  $link['suf']   = '';
  $link['style']  = '';
  $link['more']   = '';

  $link['name']   = htmlspecialchars($link['name']);
  
  //shields up
  if($conf['mailguard']=='visible'){
    //the mail name gets some visible encoding
    $link['url'] = str_replace('@',' [at] ',$link['url']);
    $link['url'] = str_replace('.',' [dot] ',$link['url']);
    $link['url'] = str_replace('-',' [dash] ',$link['url']);
  }elseif($conf['mailguard']=='hex'){
    for ($x=0; $x < strlen($link['url']); $x++) {
      $encode .= '&#x' . bin2hex($link['url'][$x]).';';
    }
    $link['url'] = $encode;
  }
  
  $link['title'] = $link['url'];
  if(!$link['name']) $link['name'] = $link['url'];
  $link['url']   = 'mailto:'.$link['url'];

  return $link;
}

function format_link_interwiki($link){
  global $conf;

  //obvious ones
  $link['class']  = 'interwiki';
  $link['target'] = $conf['target']['interwiki'];
  $link['pre']    = '';
  $link['suf']    = '';
  $link['more']   = 'onclick="return svchk()" onkeypress="return svchk()"';

  //get interwiki short name
  list($wiki,$link['url']) = split('>',$link['url'],2);
  $wiki   = strtolower(trim($wiki)); //always use lowercase
  $link['url']   = trim($link['url']);
  if(!$link['name']) $link['name'] = $link['url'];

  //encode special chars
  $link['name']   = htmlspecialchars($link['name']);

  //set default to google
  $url = 'http://www.google.com/search?q=';
  $ico = 'google';

  //load interwikilinks
  //FIXME: loading this once may enhance speed a little bit
  $iwlinks = file('conf/interwiki.conf');

  //add special case 'this'
  $iwlinks[] = 'this '.getBaseURL(true).'{NAME}'; 
  
  //go through iwlinks and find URL for wiki
  foreach ($iwlinks as $line){
    $line = preg_replace('/#.*/','',$line); //skip comments
    $line = trim($line);
    list($iw,$iwurl) = preg_split('/\s+/',$line);
    if(!$iw or !$iwurl) continue; //skip broken or empty lines
    //check for match 
    if(strtolower($iw) == $wiki){
      $ico = $wiki;
      $url = $iwurl;
      break;
    }
  }

  //if ico exists set additonal style
  if(@file_exists('interwiki/'.$ico.'.png')){
    $link['style']='background: transparent url('.getBaseURL().'interwiki/'.$ico.'.png) 0px 1px no-repeat;';
  }elseif(@file_exists('interwiki/'.$ico.'.gif')){
    $link['style']='background: transparent url('.getBaseURL().'interwiki/'.$ico.'.gif) 0px 1px no-repeat;';
  }

  //do we stay at the same server? Use local target
  if( strpos($url,getBaseURL(true)) === 0 ){
    $link['target'] = $conf['target']['wiki'];
  }

  //replace placeholder
  if(strstr($url,'{URL}') !== false){
    $link['url'] = str_replace('{URL}',urlencode($link['url']),$url);
  }elseif(strstr($url,'{NAME}') !== false){
    $link['url'] = str_replace('{NAME}',$link['url'],$url);
  }else{
    $link['url'] = $url.urlencode($link['url']);
  }

  $link['title'] = htmlspecialchars($link['url']);

  //done :-)
  return $link;
}


function format_link_media($link){
  global $conf;

  $link['class']  = 'media';
  $link['style']  = '';
  $link['pre']    = '';
  $link['suf']    = '';
  $link['more']   = 'onclick="return svchk()" onkeypress="return svchk()"';
  $class          = 'media';

  list($link['name'],$title) = split('\|',$link['name'],2);
  $t = htmlspecialchars($title);

  //set alignment from spaces
  if(substr($link['name'],0,1)==' ' && substr($link['name'],-1,1)==' '){
    $link['pre'] = "</p>\n<div align=\"center\">";
    $link['suf'] = "</div>\n<p>";
  }elseif(substr($link['name'],0,1)==' '){
    #$a = ' align="right"';
    $class = 'mediaright';
  }elseif(substr($link['name'],-1,1)==' '){
    #$a = ' align="left"';
    $class = 'medialeft';
  }else{
    $a = ' align="middle"';
  }
  $link['name'] = trim($link['name']);
  
  //split into src and parameters
  list($src,$param) = split('\?',$link['name'],2);
  //parse width and height
  if(preg_match('#(\d*)(x(\d*))?#i',$param,$size)){
    if($size[1]) $w = $size[1];
    if($size[3]) $h = $size[3];
  }

  //check for nocache param
  $nocache = preg_match('/nocache/i',$param);
  //do image caching, resizing and src rewriting
  $cache = $src;
  $isimg = img_cache($cache,$src,$w,$h,$nocache);

  //set link to src if none given 
  if(!$link['url']){
    $link['url'] = $src;
    $link['target'] = $conf['target']['media'];
  }

  //prepare name
  if($isimg){
           $link['name'] = '<img src="'.getBaseURL().$cache.'"';
    if($w) $link['name'] .= ' width="'.$w.'"';
    if($h) $link['name'] .= ' height="'.$h.'"';
    if($t) $link['name'] .= ' title="'.$t.'"';
    if($a) $link['name'] .= $a;
           $link['name'] .= ' class="'.$class.'" border="0" alt="'.$t.'" />';
  }else{
    if($t){
      $link['name'] = $t;
    }else{
      $link['name'] = basename($src);
    }
  }

  return $link;
}

/**
 * Builds an URL list from a RSS feed
 */
function format_rss($url){
  global $lang;
  define('MAGPIE_CACHE_ON', false); //we do our own caching
  define('MAGPIE_DIR', 'inc/magpie/');
  require_once(MAGPIE_DIR.'/rss_fetch.inc');

  //disable warning while fetching
  $elvl = error_reporting(E_ERROR);
  $rss  = fetch_rss($url);
  error_reporting($elvl);

  $ret = '<ul class="rss">';
  if($rss){
    foreach ($rss->items as $item ) {
      $link         = array();
      $link['url']  = $item['link'];
      $link['name'] = $item['title'];
      $link         = format_link_externalurl($link);
      $ret         .= '<li>'.format_link_build($link).'</li>';
    }
  }else{
    $link['url']  = $url;
    $link         = format_link_externalurl($link);
    $ret         .= '<li>';
    $ret         .= '<em>'.$lang['rssfailed'].'</em>';
    $ret         .= format_link_build($link);
    $ret         .= '</li>';
  }
  $ret .= '</ul>';
  return $ret;
}


function img_cache(&$csrc,&$src,&$w,&$h,$nocache){
  global $conf;
  
  //container for various paths
  $f['full']['web'] = $src;
  $f['resz']['web'] = $src;
  $f['full']['fs']  = $src;
  $f['resz']['fs']  = $src;

  //generate cachename
  $md5 = md5($src);

  //check if it is an image
  if(preg_match('#\.(jpe?g|gif|png)$#i',$src,$match)){
    $ext   = strtolower($match[1]);
    $isimg = true;
  }

  //check if it is external or a local mediafile
  if(preg_match('#^([a-z0-9]+?)://#i',$src)){
    $isurl = true;
  }else{
    $src = str_replace(':','/',$src);
    $f['full']['web'] = $conf['mediaweb'].'/'.$src;
    $f['resz']['web'] = $conf['mediaweb'].'/'.$src;
    $f['full']['fs']  = $conf['mediadir'].'/'.$src;
    $f['resz']['fs']  = $conf['mediadir'].'/'.$src;
  }

  //download external images if allowed
  if($isurl && $isimg && !$nocache){
    $cache = $conf['mediadir']."/.cache/$md5.$ext";
    if (@file_exists($cache) || download($src,$cache)){
      $f['full']['web'] = $conf['mediaweb']."/.cache/$md5.$ext";
      $f['resz']['web'] = $conf['mediaweb']."/.cache/$md5.$ext";
      $f['full']['fs']  = $conf['mediadir']."/.cache/$md5.$ext";
      $f['resz']['fs']  = $conf['mediadir']."/.cache/$md5.$ext";
      $isurl = false;
    }
  }

  //for local images (cached or media) do resizing
  if($isimg && (!$isurl) && $w){
    $info = getImageSize($f['full']['fs']);
    //if $h not given calcualte it with correct aspect ratio
    if(!$h){
      $h = round(($w * $info[1]) / $info[0]);
    }
    $cache = $conf['mediadir'].'/.cache/'.$md5.'.'.$w.'x'.$h.'.'.$ext;
    //delete outdated cachefile
    if(@file_exists($cache) && (filemtime($cache)<filemtime($f['full']['fs']))){
      unlink($cache);
    }
    //check if a resized cachecopy exists else create one
    if(@file_exists($cache) || img_resize($ext,$f['full']['fs'],$info[0],$info[1],$cache,$w,$h)){
      $f['resz']['web'] = $conf['mediaweb'].'/.cache/'.$md5.'.'.$w.'x'.$h.'.'.$ext;
      $f['resz']['fs']  = $conf['mediadir'].'/.cache/'.$md5.'.'.$w.'x'.$h.'.'.$ext;
    }
  }elseif($isimg && (!$isurl)){
    //if no new size was given just return the img size
    $info = getImageSize($f['full']['fs']);
    $w = $info[0];
    $h = $info[1];
  }

  //set srcs
  $src  = $f['full']['web'];
  $csrc = $f['resz']['web'];
  return $isimg;
}

function img_resize($ext,$from,$from_w,$from_h,$to,$to_w,$to_h){
  // create cachedir
  io_makeFileDir($to);

  // create an image of the given filetype
  if ($ext == 'jpg' || $ext == 'jpeg'){
    if(!function_exists("imagecreatefromjpeg")) return false;
    $image = @imagecreateFromjpeg($from);
  }elseif($ext == 'png') {
    if(!function_exists("imagecreatefrompng")) return false;
    $image = @imagecreatefrompng($from);
  }elseif($ext == 'gif') {
    if(!function_exists("imagecreatefromgif")) return false;
    $image = @imagecreatefromgif($from);
  }
  if(!$image) return false;

  if(function_exists("imagecreatetruecolor")){
    $newimg = @imagecreatetruecolor ($to_w, $to_h);
  }
  if(!$newimg) $newimg = @imagecreate($to_w, $to_h);
  if(!$newimg) return false;

  //try resampling first
  if(function_exists("imagecopyresampled")){
    if(!@imagecopyresampled($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h)) {
      imagecopyresized($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h);
    }
  }else{
    imagecopyresized($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h);
  }
  
  if ($ext == 'jpg' || $ext == 'jpeg'){
    if(!function_exists("imagejpeg")) return false;
    return imagejpeg($newimg, $to, 70);
  }elseif($ext == 'png') {
    if(!function_exists("imagepng")) return false;
    return imagepng($newimg, $to);
  }elseif($ext == 'gif') {
    if(!function_exists("imagegif")) return false;
    return imagegif($newimg, $to);
  }

  return false;
}

?>