xref: /dokuwiki/inc/common.php (revision 71726d7801bdcbf41dfdc79d244f09a0988529c0)
1<?php
2/**
3 * Common DokuWiki functions
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Andreas Gohr <andi@splitbrain.org>
7 */
8
9  if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
10  require_once(DOKU_CONF.'dokuwiki.php');
11  require_once(DOKU_INC.'inc/io.php');
12  require_once(DOKU_INC.'inc/utf8.php');
13  require_once(DOKU_INC.'inc/mail.php');
14  require_once(DOKU_INC.'inc/parserutils.php');
15
16/**
17 * These constants are used with the recents function
18 */
19define('RECENTS_SKIP_DELETED',2);
20define('RECENTS_SKIP_MINORS',4);
21define('RECENTS_SKIP_SUBSPACES',8);
22
23/**
24 * Wrapper around htmlspecialchars()
25 *
26 * @author Andreas Gohr <andi@splitbrain.org>
27 * @see    htmlspecialchars()
28 */
29function hsc($string){
30  return htmlspecialchars($string, ENT_QUOTES, 'UTF-8');
31}
32
33/**
34 * print a newline terminated string
35 *
36 * You can give an indention as optional parameter
37 *
38 * @author Andreas Gohr <andi@splitbrain.org>
39 */
40function ptln($string,$intend=0){
41  for($i=0; $i<$intend; $i++) print ' ';
42  print"$string\n";
43}
44
45/**
46 * Return info about the current document as associative
47 * array.
48 *
49 * @author Andreas Gohr <andi@splitbrain.org>
50 */
51function pageinfo(){
52  global $ID;
53  global $REV;
54  global $USERINFO;
55  global $conf;
56
57  if($_SERVER['REMOTE_USER']){
58    $info['userinfo']   = $USERINFO;
59    $info['perm']       = auth_quickaclcheck($ID);
60    $info['subscribed'] = is_subscribed($ID,$_SERVER['REMOTE_USER']);
61    $info['client']     = $_SERVER['REMOTE_USER'];
62
63    // if some outside auth were used only REMOTE_USER is set
64    if(!$info['userinfo']['name']){
65      $info['userinfo']['name'] = $_SERVER['REMOTE_USER'];
66    }
67
68  }else{
69    $info['perm']       = auth_aclcheck($ID,'',null);
70    $info['subscribed'] = false;
71    $info['client']     = clientIP(true);
72  }
73
74  $info['namespace'] = getNS($ID);
75  $info['locked']    = checklock($ID);
76  $info['filepath']  = realpath(wikiFN($ID,$REV));
77  $info['exists']    = @file_exists($info['filepath']);
78  if($REV && !$info['exists']){
79    //check if current revision was meant
80    $cur = wikiFN($ID);
81    if(@file_exists($cur) && (@filemtime($cur) == $REV)){
82      $info['filepath'] = realpath($cur);
83      $info['exists']   = true;
84      $REV = '';
85    }
86  }
87  $info['rev'] = $REV;
88  if($info['exists']){
89    $info['writable'] = (is_writable($info['filepath']) &&
90                         ($info['perm'] >= AUTH_EDIT));
91  }else{
92    $info['writable'] = ($info['perm'] >= AUTH_CREATE);
93  }
94  $info['editable']  = ($info['writable'] && empty($info['lock']));
95  $info['lastmod']   = @filemtime($info['filepath']);
96
97  //load page meta data
98  $info['meta'] = p_get_metadata($ID);
99
100  //who's the editor
101  if($REV){
102    $revinfo = getRevisionInfo($ID, $REV, 1024);
103  }else{
104    $revinfo = $info['meta']['last_change'];
105  }
106  $info['ip']     = $revinfo['ip'];
107  $info['user']   = $revinfo['user'];
108  $info['sum']    = $revinfo['sum'];
109  // See also $INFO['meta']['last_change'] which is the most recent log line for page $ID.
110  // Use $INFO['meta']['last_change']['type']==='e' in place of $info['minor'].
111
112  if($revinfo['user']){
113    $info['editor'] = $revinfo['user'];
114  }else{
115    $info['editor'] = $revinfo['ip'];
116  }
117
118  // draft
119  $draft = getCacheName($info['client'].$ID,'.draft');
120  if(@file_exists($draft)){
121    if(@filemtime($draft) < @filemtime(wikiFN($ID))){
122      // remove stale draft
123      @unlink($draft);
124    }else{
125      $info['draft'] = $draft;
126    }
127  }
128
129  return $info;
130}
131
132/**
133 * Build an string of URL parameters
134 *
135 * @author Andreas Gohr
136 */
137function buildURLparams($params, $sep='&amp;'){
138  $url = '';
139  $amp = false;
140  foreach($params as $key => $val){
141    if($amp) $url .= $sep;
142
143    $url .= $key.'=';
144    $url .= rawurlencode($val);
145    $amp = true;
146  }
147  return $url;
148}
149
150/**
151 * Build an string of html tag attributes
152 *
153 * @author Andreas Gohr
154 */
155function buildAttributes($params){
156  $url = '';
157  foreach($params as $key => $val){
158    $url .= $key.'="';
159    $url .= htmlspecialchars ($val);
160    $url .= '" ';
161  }
162  return $url;
163}
164
165
166/**
167 * print a message
168 *
169 * If HTTP headers were not sent yet the message is added
170 * to the global message array else it's printed directly
171 * using html_msgarea()
172 *
173 *
174 * Levels can be:
175 *
176 * -1 error
177 *  0 info
178 *  1 success
179 *
180 * @author Andreas Gohr <andi@splitbrain.org>
181 * @see    html_msgarea
182 */
183function msg($message,$lvl=0,$line='',$file=''){
184  global $MSG;
185  $errors[-1] = 'error';
186  $errors[0]  = 'info';
187  $errors[1]  = 'success';
188
189  if($line || $file) $message.=' ['.basename($file).':'.$line.']';
190
191  if(!headers_sent()){
192    if(!isset($MSG)) $MSG = array();
193    $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message);
194  }else{
195    $MSG = array();
196    $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message);
197    if(function_exists('html_msgarea')){
198      html_msgarea();
199    }else{
200      print "ERROR($lvl) $message";
201    }
202  }
203}
204
205/**
206 * This builds the breadcrumb trail and returns it as array
207 *
208 * @author Andreas Gohr <andi@splitbrain.org>
209 */
210function breadcrumbs(){
211  // we prepare the breadcrumbs early for quick session closing
212  static $crumbs = null;
213  if($crumbs != null) return $crumbs;
214
215  global $ID;
216  global $ACT;
217  global $conf;
218  $crumbs = $_SESSION[$conf['title']]['bc'];
219
220  //first visit?
221  if (!is_array($crumbs)){
222    $crumbs = array();
223  }
224  //we only save on show and existing wiki documents
225  $file = wikiFN($ID);
226  if($ACT != 'show' || !@file_exists($file)){
227    $_SESSION[$conf['title']]['bc'] = $crumbs;
228    return $crumbs;
229  }
230
231  // page names
232  $name = noNS($ID);
233  if ($conf['useheading']) {
234    // get page title
235    $title = p_get_first_heading($ID);
236    if ($title) {
237      $name = $title;
238    }
239  }
240
241  //remove ID from array
242  if (isset($crumbs[$ID])) {
243    unset($crumbs[$ID]);
244  }
245
246  //add to array
247  $crumbs[$ID] = $name;
248  //reduce size
249  while(count($crumbs) > $conf['breadcrumbs']){
250    array_shift($crumbs);
251  }
252  //save to session
253  $_SESSION[$conf['title']]['bc'] = $crumbs;
254  return $crumbs;
255}
256
257/**
258 * Filter for page IDs
259 *
260 * This is run on a ID before it is outputted somewhere
261 * currently used to replace the colon with something else
262 * on Windows systems and to have proper URL encoding
263 *
264 * Urlencoding is ommitted when the second parameter is false
265 *
266 * @author Andreas Gohr <andi@splitbrain.org>
267 */
268function idfilter($id,$ue=true){
269  global $conf;
270  if ($conf['useslash'] && $conf['userewrite']){
271    $id = strtr($id,':','/');
272  }elseif (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' &&
273      $conf['userewrite']) {
274    $id = strtr($id,':',';');
275  }
276  if($ue){
277    $id = rawurlencode($id);
278    $id = str_replace('%3A',':',$id); //keep as colon
279    $id = str_replace('%2F','/',$id); //keep as slash
280  }
281  return $id;
282}
283
284/**
285 * This builds a link to a wikipage
286 *
287 * It handles URL rewriting and adds additional parameter if
288 * given in $more
289 *
290 * @author Andreas Gohr <andi@splitbrain.org>
291 */
292function wl($id='',$more='',$abs=false,$sep='&amp;'){
293  global $conf;
294  if(is_array($more)){
295    $more = buildURLparams($more,$sep);
296  }else{
297    $more = str_replace(',',$sep,$more);
298  }
299
300  $id    = idfilter($id);
301  if($abs){
302    $xlink = DOKU_URL;
303  }else{
304    $xlink = DOKU_BASE;
305  }
306
307  if($conf['userewrite'] == 2){
308    $xlink .= DOKU_SCRIPT.'/'.$id;
309    if($more) $xlink .= '?'.$more;
310  }elseif($conf['userewrite']){
311    $xlink .= $id;
312    if($more) $xlink .= '?'.$more;
313  }else{
314    $xlink .= DOKU_SCRIPT.'?id='.$id;
315    if($more) $xlink .= $sep.$more;
316  }
317
318  return $xlink;
319}
320
321/**
322 * This builds a link to an alternate page format
323 *
324 * Handles URL rewriting if enabled. Follows the style of wl().
325 *
326 * @author Ben Coburn <btcoburn@silicodon.net>
327 */
328function exportlink($id='',$format='raw',$more='',$abs=false,$sep='&amp;'){
329  global $conf;
330  if(is_array($more)){
331    $more = buildURLparams($more,$sep);
332  }else{
333    $more = str_replace(',',$sep,$more);
334  }
335
336  $format = rawurlencode($format);
337  $id = idfilter($id);
338  if($abs){
339    $xlink = DOKU_URL;
340  }else{
341    $xlink = DOKU_BASE;
342  }
343
344  if($conf['userewrite'] == 2){
345    $xlink .= DOKU_SCRIPT.'/'.$id.'?do=export_'.$format;
346    if($more) $xlink .= $sep.$more;
347  }elseif($conf['userewrite'] == 1){
348    $xlink .= '_export/'.$format.'/'.$id;
349    if($more) $xlink .= '?'.$more;
350  }else{
351    $xlink .= DOKU_SCRIPT.'?do=export_'.$format.$sep.'id='.$id;
352    if($more) $xlink .= $sep.$more;
353  }
354
355  return $xlink;
356}
357
358/**
359 * Build a link to a media file
360 *
361 * Will return a link to the detail page if $direct is false
362 */
363function ml($id='',$more='',$direct=true,$sep='&amp;'){
364  global $conf;
365  if(is_array($more)){
366    $more = buildURLparams($more,$sep);
367  }else{
368    $more = str_replace(',',$sep,$more);
369  }
370
371  $xlink = DOKU_BASE;
372
373  // external URLs are always direct without rewriting
374  if(preg_match('#^(https?|ftp)://#i',$id)){
375    $xlink .= 'lib/exe/fetch.php';
376    if($more){
377      $xlink .= '?'.$more;
378      $xlink .= $sep.'media='.rawurlencode($id);
379    }else{
380      $xlink .= '?media='.rawurlencode($id);
381    }
382    return $xlink;
383  }
384
385  $id = idfilter($id);
386
387  // decide on scriptname
388  if($direct){
389    if($conf['userewrite'] == 1){
390      $script = '_media';
391    }else{
392      $script = 'lib/exe/fetch.php';
393    }
394  }else{
395    if($conf['userewrite'] == 1){
396      $script = '_detail';
397    }else{
398      $script = 'lib/exe/detail.php';
399    }
400  }
401
402  // build URL based on rewrite mode
403   if($conf['userewrite']){
404     $xlink .= $script.'/'.$id;
405     if($more) $xlink .= '?'.$more;
406   }else{
407     if($more){
408       $xlink .= $script.'?'.$more;
409       $xlink .= $sep.'media='.$id;
410     }else{
411       $xlink .= $script.'?media='.$id;
412     }
413   }
414
415  return $xlink;
416}
417
418
419
420/**
421 * Just builds a link to a script
422 *
423 * @todo   maybe obsolete
424 * @author Andreas Gohr <andi@splitbrain.org>
425 */
426function script($script='doku.php'){
427#  $link = getBaseURL();
428#  $link .= $script;
429#  return $link;
430  return DOKU_BASE.DOKU_SCRIPT;
431}
432
433/**
434 * Spamcheck against wordlist
435 *
436 * Checks the wikitext against a list of blocked expressions
437 * returns true if the text contains any bad words
438 *
439 * @author Andreas Gohr <andi@splitbrain.org>
440 */
441function checkwordblock(){
442  global $TEXT;
443  global $conf;
444
445  if(!$conf['usewordblock']) return false;
446
447  $wordblocks = getWordblocks();
448  //how many lines to read at once (to work around some PCRE limits)
449  if(version_compare(phpversion(),'4.3.0','<')){
450    //old versions of PCRE define a maximum of parenthesises even if no
451    //backreferences are used - the maximum is 99
452    //this is very bad performancewise and may even be too high still
453    $chunksize = 40;
454  }else{
455    //read file in chunks of 600 - this should work around the
456    //MAX_PATTERN_SIZE in modern PCRE
457    $chunksize = 400;
458  }
459  while($blocks = array_splice($wordblocks,0,$chunksize)){
460    $re = array();
461    #build regexp from blocks
462    foreach($blocks as $block){
463      $block = preg_replace('/#.*$/','',$block);
464      $block = trim($block);
465      if(empty($block)) continue;
466      $re[]  = $block;
467    }
468    if(preg_match('#('.join('|',$re).')#si',$TEXT, $match=array())) {
469      return true;
470    }
471  }
472  return false;
473}
474
475/**
476 * Return the IP of the client
477 *
478 * Honours X-Forwarded-For and X-Real-IP Proxy Headers
479 *
480 * It returns a comma separated list of IPs if the above mentioned
481 * headers are set. If the single parameter is set, it tries to return
482 * a routable public address, prefering the ones suplied in the X
483 * headers
484 *
485 * @param  boolean $single If set only a single IP is returned
486 * @author Andreas Gohr <andi@splitbrain.org>
487 */
488function clientIP($single=false){
489  $ip = array();
490  $ip[] = $_SERVER['REMOTE_ADDR'];
491  if($_SERVER['HTTP_X_FORWARDED_FOR'])
492    $ip = array_merge($ip,explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']));
493  if($_SERVER['HTTP_X_REAL_IP'])
494    $ip = array_merge($ip,explode(',',$_SERVER['HTTP_X_REAL_IP']));
495
496  // remove any non-IP stuff
497  $cnt = count($ip);
498  for($i=0; $i<$cnt; $i++){
499    $ip[$i] = preg_replace('/[^0-9\.]+/','',$ip[$i]);
500    if(!preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/',$ip[$i])) $ip[$i] = '';
501    if(empty($ip[$i])) unset($ip[$i]);
502  }
503  $ip = array_values(array_unique($ip));
504  if(!$ip[0]) $ip[0] = '0.0.0.0'; // for some strange reason we don't have a IP
505
506  if(!$single) return join(',',$ip);
507
508  // decide which IP to use, trying to avoid local addresses
509  $ip = array_reverse($ip);
510  foreach($ip as $i){
511    if(preg_match('/^(127\.|10\.|192\.168\.|172\.((1[6-9])|(2[0-9])|(3[0-1]))\.)/',$i)){
512      continue;
513    }else{
514      return $i;
515    }
516  }
517  // still here? just use the first (last) address
518  return $ip[0];
519}
520
521/**
522 * Checks if a given page is currently locked.
523 *
524 * removes stale lockfiles
525 *
526 * @author Andreas Gohr <andi@splitbrain.org>
527 */
528function checklock($id){
529  global $conf;
530  $lock = wikiLockFN($id);
531
532  //no lockfile
533  if(!@file_exists($lock)) return false;
534
535  //lockfile expired
536  if((time() - filemtime($lock)) > $conf['locktime']){
537    unlink($lock);
538    return false;
539  }
540
541  //my own lock
542  $ip = io_readFile($lock);
543  if( ($ip == clientIP()) || ($ip == $_SERVER['REMOTE_USER']) ){
544    return false;
545  }
546
547  return $ip;
548}
549
550/**
551 * Lock a page for editing
552 *
553 * @author Andreas Gohr <andi@splitbrain.org>
554 */
555function lock($id){
556  $lock = wikiLockFN($id);
557  if($_SERVER['REMOTE_USER']){
558    io_saveFile($lock,$_SERVER['REMOTE_USER']);
559  }else{
560    io_saveFile($lock,clientIP());
561  }
562}
563
564/**
565 * Unlock a page if it was locked by the user
566 *
567 * @author Andreas Gohr <andi@splitbrain.org>
568 * @return bool true if a lock was removed
569 */
570function unlock($id){
571  $lock = wikiLockFN($id);
572  if(@file_exists($lock)){
573    $ip = io_readFile($lock);
574    if( ($ip == clientIP()) || ($ip == $_SERVER['REMOTE_USER']) ){
575      @unlink($lock);
576      return true;
577    }
578  }
579  return false;
580}
581
582/**
583 * convert line ending to unix format
584 *
585 * @see    formText() for 2crlf conversion
586 * @author Andreas Gohr <andi@splitbrain.org>
587 */
588function cleanText($text){
589  $text = preg_replace("/(\015\012)|(\015)/","\012",$text);
590  return $text;
591}
592
593/**
594 * Prepares text for print in Webforms by encoding special chars.
595 * It also converts line endings to Windows format which is
596 * pseudo standard for webforms.
597 *
598 * @see    cleanText() for 2unix conversion
599 * @author Andreas Gohr <andi@splitbrain.org>
600 */
601function formText($text){
602  $text = preg_replace("/\012/","\015\012",$text);
603  return htmlspecialchars($text);
604}
605
606/**
607 * Returns the specified local text in raw format
608 *
609 * @author Andreas Gohr <andi@splitbrain.org>
610 */
611function rawLocale($id){
612  return io_readFile(localeFN($id));
613}
614
615/**
616 * Returns the raw WikiText
617 *
618 * @author Andreas Gohr <andi@splitbrain.org>
619 */
620function rawWiki($id,$rev=''){
621  return io_readWikiPage(wikiFN($id, $rev), $id, $rev);
622}
623
624/**
625 * Returns the pagetemplate contents for the ID's namespace
626 *
627 * @author Andreas Gohr <andi@splitbrain.org>
628 */
629function pageTemplate($id){
630  global $conf;
631  global $INFO;
632  $tpl = io_readFile(dirname(wikiFN($id)).'/_template.txt');
633  $tpl = str_replace('@ID@',$id,$tpl);
634  $tpl = str_replace('@NS@',getNS($id),$tpl);
635  $tpl = str_replace('@PAGE@',strtr(noNS($id),'_',' '),$tpl);
636  $tpl = str_replace('@USER@',$_SERVER['REMOTE_USER'],$tpl);
637  $tpl = str_replace('@NAME@',$INFO['userinfo']['name'],$tpl);
638  $tpl = str_replace('@MAIL@',$INFO['userinfo']['mail'],$tpl);
639  $tpl = str_replace('@DATE@',date($conf['dformat']),$tpl);
640  return $tpl;
641}
642
643
644/**
645 * Returns the raw Wiki Text in three slices.
646 *
647 * The range parameter needs to have the form "from-to"
648 * and gives the range of the section in bytes - no
649 * UTF-8 awareness is needed.
650 * The returned order is prefix, section and suffix.
651 *
652 * @author Andreas Gohr <andi@splitbrain.org>
653 */
654function rawWikiSlices($range,$id,$rev=''){
655  list($from,$to) = split('-',$range,2);
656  $text = io_readWikiPage(wikiFN($id, $rev), $id, $rev);
657  if(!$from) $from = 0;
658  if(!$to)   $to   = strlen($text)+1;
659
660  $slices[0] = substr($text,0,$from-1);
661  $slices[1] = substr($text,$from-1,$to-$from);
662  $slices[2] = substr($text,$to);
663
664  return $slices;
665}
666
667/**
668 * Joins wiki text slices
669 *
670 * function to join the text slices with correct lineendings again.
671 * When the pretty parameter is set to true it adds additional empty
672 * lines between sections if needed (used on saving).
673 *
674 * @author Andreas Gohr <andi@splitbrain.org>
675 */
676function con($pre,$text,$suf,$pretty=false){
677
678  if($pretty){
679    if($pre && substr($pre,-1) != "\n") $pre .= "\n";
680    if($suf && substr($text,-1) != "\n") $text .= "\n";
681  }
682
683  if($pre) $pre .= "\n";
684  if($suf) $text .= "\n";
685  return $pre.$text.$suf;
686}
687
688/**
689 * print debug messages
690 *
691 * little function to print the content of a var
692 *
693 * @author Andreas Gohr <andi@splitbrain.org>
694 */
695function dbg($msg,$hidden=false){
696  (!$hidden) ? print '<pre class="dbg">' : print "<!--\n";
697  print_r($msg);
698  (!$hidden) ? print '</pre>' : print "\n-->";
699}
700
701/**
702 * Print info to a log file
703 *
704 * @author Andreas Gohr <andi@splitbrain.org>
705 */
706function dbglog($msg){
707  global $conf;
708  $file = $conf['cachedir'].'/debug.log';
709  $fh = fopen($file,'a');
710  if($fh){
711    fwrite($fh,date('H:i:s ').$_SERVER['REMOTE_ADDR'].': '.$msg."\n");
712    fclose($fh);
713  }
714}
715
716/**
717 * Add's an entry to the changelog and saves the metadata for the page
718 *
719 * @author Andreas Gohr <andi@splitbrain.org>
720 * @author Esther Brunner <wikidesign@gmail.com>
721 * @author Ben Coburn <btcoburn@silicodon.net>
722 */
723function addLogEntry($date, $id, $type='E', $summary='', $extra=''){
724  global $conf, $INFO;
725
726  $id = cleanid($id);
727  $file = wikiFN($id);
728  $created = @filectime($file);
729  $minor = ($type==='e');
730  $wasRemoved = ($type==='D');
731
732  if(!$date) $date = time(); //use current time if none supplied
733  $remote = $_SERVER['REMOTE_ADDR'];
734  $user   = $_SERVER['REMOTE_USER'];
735
736  $logline = array(
737    'date'  => $date,
738    'ip'    => $remote,
739    'type'  => $type,
740    'id'    => $id,
741    'user'  => $user,
742    'sum'   => $summary,
743    'extra' => $extra
744  );
745
746  // update metadata
747  if (!$wasRemoved) {
748    $meta = array();
749    if (!$INFO['exists']){ // newly created
750      $meta['date']['created'] = $created;
751      if ($user) $meta['creator'] = $INFO['userinfo']['name'];
752    } elseif (!$minor) {   // non-minor modification
753      $meta['date']['modified'] = $date;
754      if ($user) $meta['contributor'][$user] = $INFO['userinfo']['name'];
755    }
756    $meta['last_change'] = $logline;
757    p_set_metadata($id, $meta, true);
758  }
759
760  // add changelog lines
761  $logline = implode("\t", $logline)."\n";
762  io_saveFile(metaFN($id,'.changes'),$logline,true); //page changelog
763  io_saveFile($conf['changelog'],$logline,true); //global changelog cache
764}
765
766/**
767 * Internal function used by getRecents
768 *
769 * don't call directly
770 *
771 * @see getRecents()
772 * @author Andreas Gohr <andi@splitbrain.org>
773 * @author Ben Coburn <btcoburn@silicodon.net>
774 */
775function _handleRecent($line,$ns,$flags){
776  static $seen  = array();         //caches seen pages and skip them
777  if(empty($line)) return false;   //skip empty lines
778
779  // split the line into parts
780  $recent = parseChangelogLine($line);
781  if ($recent===false) { return false; }
782
783  // skip seen ones
784  if(isset($seen[$recent['id']])) return false;
785
786  // skip minors
787  if($recent['type']==='e' && ($flags & RECENTS_SKIP_MINORS)) return false;
788
789  // remember in seen to skip additional sights
790  $seen[$recent['id']] = 1;
791
792  // check if it's a hidden page
793  if(isHiddenPage($recent['id'])) return false;
794
795  // filter namespace
796  if (($ns) && (strpos($recent['id'],$ns.':') !== 0)) return false;
797
798  // exclude subnamespaces
799  if (($flags & RECENTS_SKIP_SUBSPACES) && (getNS($recent['id']) != $ns)) return false;
800
801  // check ACL
802  if (auth_quickaclcheck($recent['id']) < AUTH_READ) return false;
803
804  // check existance
805  if((!@file_exists(wikiFN($recent['id']))) && ($flags & RECENTS_SKIP_DELETED)) return false;
806
807  return $recent;
808}
809
810
811/**
812 * returns an array of recently changed files using the
813 * changelog
814 *
815 * The following constants can be used to control which changes are
816 * included. Add them together as needed.
817 *
818 * RECENTS_SKIP_DELETED   - don't include deleted pages
819 * RECENTS_SKIP_MINORS    - don't include minor changes
820 * RECENTS_SKIP_SUBSPACES - don't include subspaces
821 *
822 * @param int    $first   number of first entry returned (for paginating
823 * @param int    $num     return $num entries
824 * @param string $ns      restrict to given namespace
825 * @param bool   $flags   see above
826 *
827 * @author Ben Coburn <btcoburn@silicodon.net>
828 */
829function getRecents($first,$num,$ns='',$flags=0){
830  global $conf;
831  $recent = array();
832  $count  = 0;
833
834  if(!$num)
835    return $recent;
836
837  // read all recent changes. (kept short)
838  $lines = file($conf['changelog']);
839
840  // handle lines
841  for($i = count($lines)-1; $i >= 0; $i--){
842    $rec = _handleRecent($lines[$i], $ns, $flags);
843    if($rec !== false) {
844      if(--$first >= 0) continue; // skip first entries
845      $recent[] = $rec;
846      $count++;
847      // break when we have enough entries
848      if($count >= $num){ break; }
849    }
850  }
851
852  return $recent;
853}
854
855/**
856 * parses a changelog line into it's components
857 *
858 * @author Ben Coburn <btcoburn@silicodon.net>
859 */
860function parseChangelogLine($line) {
861  $tmp = explode("\t", $line);
862    if ($tmp!==false && count($tmp)>1) {
863      $info = array();
864      $info['date']  = $tmp[0]; // unix timestamp
865      $info['ip']    = $tmp[1]; // IPv4 address (127.0.0.1)
866      $info['type']  = $tmp[2]; // log line type
867      $info['id']    = $tmp[3]; // page id
868      $info['user']  = $tmp[4]; // user name
869      $info['sum']   = $tmp[5]; // edit summary (or action reason)
870      $info['extra'] = rtrim($tmp[6], "\n"); // extra data (varies by line type)
871      return $info;
872  } else { return false; }
873}
874
875/**
876 * Get the changelog information for a specific page id
877 * and revision (timestamp). Adjacent changelog lines
878 * are optimistically parsed and cached to speed up
879 * consecutive calls to getRevisionInfo. For large
880 * changelog files, only the chunk containing the
881 * requested changelog line is read.
882 *
883 * @author Ben Coburn <btcoburn@silicodon.net>
884 */
885function getRevisionInfo($id, $rev, $chunk_size=8192) {
886  global $cache_revinfo;
887  $cache =& $cache_revinfo;
888  if (!isset($cache[$id])) { $cache[$id] = array(); }
889  $rev = max($rev, 0);
890
891  // check if it's already in the memory cache
892  if (isset($cache[$id]) && isset($cache[$id][$rev])) {
893    return $cache[$id][$rev];
894  }
895
896  $file = metaFN($id, '.changes');
897  if (!file_exists($file)) { return false; }
898  if (filesize($file)<$chunk_size || $chunk_size==0) {
899    // read whole file
900    $lines = file($file);
901    if ($lines===false) { return false; }
902  } else {
903    // read by chunk
904    $fp = fopen($file, 'rb'); // "file pointer"
905    if ($fp===false) { return false; }
906    $head = 0;
907    fseek($fp, 0, SEEK_END);
908    $tail = ftell($fp);
909    $finger = 0;
910    $finger_rev = 0;
911
912    // find chunk
913    while ($tail-$head>$chunk_size) {
914      $finger = $head+floor(($tail-$head)/2.0);
915      fseek($fp, $finger);
916      fgets($fp); // slip the finger forward to a new line
917      $finger = ftell($fp);
918      $tmp = fgets($fp); // then read at that location
919      $tmp = parseChangelogLine($tmp);
920      $finger_rev = $tmp['date'];
921      if ($finger==$head || $finger==$tail) { break; }
922      if ($finger_rev>$rev) {
923        $tail = $finger;
924      } else {
925        $head = $finger;
926      }
927    }
928
929    if ($tail-$head<1) {
930      // cound not find chunk, assume requested rev is missing
931      fclose($fp);
932      return false;
933    }
934
935    // read chunk
936    $chunk = '';
937    $chunk_size = max($tail-$head, 0); // found chunk size
938    $got = 0;
939    fseek($fp, $head);
940    while ($got<$chunk_size && !feof($fp)) {
941      $tmp = fread($fp, max($chunk_size-$got, 0));
942      if ($tmp===false) { break; } //error state
943      $got += strlen($tmp);
944      $chunk .= $tmp;
945    }
946    $lines = explode("\n", $chunk);
947    array_pop($lines); // remove trailing newline
948    fclose($fp);
949  }
950
951  // parse and cache changelog lines
952  foreach ($lines as $value) {
953    $tmp = parseChangelogLine($value);
954    if ($tmp!==false) {
955      $cache[$id][$tmp['date']] = $tmp;
956    }
957  }
958  if (!isset($cache[$id][$rev])) { return false; }
959  return $cache[$id][$rev];
960}
961
962/**
963 * Return a list of page revisions numbers
964 * Does not guarantee that the revision exists in the attic,
965 * only that a line with the date exists in the changelog.
966 * By default the current revision is skipped.
967 *
968 * id:    the page of interest
969 * first: skip the first n changelog lines
970 * num:   number of revisions to return
971 *
972 * The current revision is automatically skipped when the page exists.
973 * See $INFO['meta']['last_change'] for the current revision.
974 *
975 * For efficiency, the log lines are parsed and cached for later
976 * calls to getRevisionInfo. Large changelog files are read
977 * backwards in chunks untill the requested number of changelog
978 * lines are recieved.
979 *
980 * @author Ben Coburn <btcoburn@silicodon.net>
981 */
982function getRevisions($id, $first, $num, $chunk_size=8192) {
983  global $cache_revinfo;
984  $cache =& $cache_revinfo;
985  if (!isset($cache[$id])) { $cache[$id] = array(); }
986
987  $revs = array();
988  $lines = array();
989  $count  = 0;
990  $file = metaFN($id, '.changes');
991  $num = max($num, 0);
992  $chunk_size = max($chunk_size, 0);
993  if ($first<0) { $first = 0; }
994  else if (file_exists(wikiFN($id))) {
995     // skip current revision if the page exists
996    $first = max($first+1, 0);
997  }
998
999  if (!file_exists($file)) { return $revs; }
1000  if (filesize($file)<$chunk_size || $chunk_size==0) {
1001    // read whole file
1002    $lines = file($file);
1003    if ($lines===false) { return $revs; }
1004  } else {
1005    // read chunks backwards
1006    $fp = fopen($file, 'rb'); // "file pointer"
1007    if ($fp===false) { return $revs; }
1008    fseek($fp, 0, SEEK_END);
1009    $tail = ftell($fp);
1010
1011    // chunk backwards
1012    $finger = max($tail-$chunk_size, 0);
1013    while ($count<$num+$first) {
1014      fseek($fp, $finger);
1015      if ($finger>0) {
1016        fgets($fp); // slip the finger forward to a new line
1017        $finger = ftell($fp);
1018      }
1019
1020      // read chunk
1021      if ($tail<=$finger) { break; }
1022      $chunk = '';
1023      $read_size = max($tail-$finger, 0); // found chunk size
1024      $got = 0;
1025      while ($got<$read_size && !feof($fp)) {
1026        $tmp = fread($fp, max($read_size-$got, 0));
1027        if ($tmp===false) { break; } //error state
1028        $got += strlen($tmp);
1029        $chunk .= $tmp;
1030      }
1031      $tmp = explode("\n", $chunk);
1032      array_pop($tmp); // remove trailing newline
1033
1034      // combine with previous chunk
1035      $count += count($tmp);
1036      $lines = array_merge($tmp, $lines);
1037
1038      // next chunk
1039      if ($finger==0) { break; } // already read all the lines
1040      else {
1041        $tail = $finger;
1042        $finger = max($tail-$chunk_size, 0);
1043      }
1044    }
1045    fclose($fp);
1046  }
1047
1048  // skip parsing extra lines
1049  $num = max(min(count($lines)-$first, $num), 0);
1050  if      ($first>0 && $num>0)  { $lines = array_slice($lines, max(count($lines)-$first-$num, 0), $num); }
1051  else if ($first>0 && $num==0) { $lines = array_slice($lines, 0, max(count($lines)-$first, 0)); }
1052  else if ($first==0 && $num>0) { $lines = array_slice($lines, max(count($lines)-$num, 0)); }
1053
1054  // handle lines in reverse order
1055  for ($i = count($lines)-1; $i >= 0; $i--) {
1056    $tmp = parseChangelogLine($lines[$i]);
1057    if ($tmp!==false) {
1058      $cache[$id][$tmp['date']] = $tmp;
1059      $revs[] = $tmp['date'];
1060    }
1061  }
1062
1063  return $revs;
1064}
1065
1066/**
1067 * Saves a wikitext by calling io_writeWikiPage
1068 *
1069 * @author Andreas Gohr <andi@splitbrain.org>
1070 * @author Ben Coburn <btcoburn@silicodon.net>
1071 */
1072function saveWikiText($id,$text,$summary,$minor=false){
1073  global $conf;
1074  global $lang;
1075  global $REV;
1076  // ignore if no changes were made
1077  if($text == rawWiki($id,'')){
1078    return;
1079  }
1080
1081  $file = wikiFN($id);
1082  $old  = saveOldRevision($id);
1083  $wasRemoved = empty($text);
1084  $wasCreated = !file_exists($file);
1085  $wasReverted = ($REV==true);
1086
1087  if ($wasRemoved){
1088    // remove empty file
1089    @unlink($file);
1090    // remove old meta info...
1091    $mfiles = metaFiles($id);
1092    $changelog = metaFN($id, '.changes');
1093    foreach ($mfiles as $mfile) {
1094      // but keep per-page changelog to preserve page history
1095      if (file_exists($mfile) && $mfile!==$changelog) { @unlink($mfile); }
1096    }
1097    $del = true;
1098    // autoset summary on deletion
1099    if(empty($summary)) $summary = $lang['deleted'];
1100    // remove empty namespaces
1101    io_sweepNS($id, 'datadir');
1102    io_sweepNS($id, 'mediadir');
1103  }else{
1104    // save file (namespace dir is created in io_writeWikiPage)
1105    io_writeWikiPage($file, $text, $id);
1106    $del = false;
1107  }
1108
1109  // select changelog line type
1110  $extra = '';
1111  $type = 'E';
1112  if ($wasReverted) {
1113    $type = 'R';
1114    $extra = $REV;
1115  }
1116  else if ($wasCreated) { $type = 'C'; }
1117  else if ($wasRemoved) { $type = 'D'; }
1118  else if ($minor && $conf['useacl'] && $_SERVER['REMOTE_USER']) { $type = 'e'; } //minor edits only for logged in users
1119
1120  addLogEntry(@filemtime($file), $id, $type, $summary, $extra);
1121  // send notify mails
1122  notify($id,'admin',$old,$summary,$minor);
1123  notify($id,'subscribers',$old,$summary,$minor);
1124
1125  //purge cache on add by updating the purgefile
1126  if($conf['purgeonadd'] && (!$old || $del)){
1127    io_saveFile($conf['cachedir'].'/purgefile',time());
1128  }
1129}
1130
1131/**
1132 * moves the current version to the attic and returns its
1133 * revision date
1134 *
1135 * @author Andreas Gohr <andi@splitbrain.org>
1136 */
1137function saveOldRevision($id){
1138  global $conf;
1139  $oldf = wikiFN($id);
1140  if(!@file_exists($oldf)) return '';
1141  $date = filemtime($oldf);
1142  $newf = wikiFN($id,$date);
1143  io_writeWikiPage($newf, rawWiki($id), $id, $date);
1144  return $date;
1145}
1146
1147/**
1148 * Sends a notify mail on page change
1149 *
1150 * @param  string  $id       The changed page
1151 * @param  string  $who      Who to notify (admin|subscribers)
1152 * @param  int     $rev      Old page revision
1153 * @param  string  $summary  What changed
1154 * @param  boolean $minor    Is this a minor edit?
1155 * @param  array   $replace  Additional string substitutions, @KEY@ to be replaced by value
1156 *
1157 * @author Andreas Gohr <andi@splitbrain.org>
1158 */
1159function notify($id,$who,$rev='',$summary='',$minor=false,$replace=array()){
1160  global $lang;
1161  global $conf;
1162
1163  // decide if there is something to do
1164  if($who == 'admin'){
1165    if(empty($conf['notify'])) return; //notify enabled?
1166    $text = rawLocale('mailtext');
1167    $to   = $conf['notify'];
1168    $bcc  = '';
1169  }elseif($who == 'subscribers'){
1170    if(!$conf['subscribers']) return; //subscribers enabled?
1171    if($conf['useacl'] && $_SERVER['REMOTE_USER'] && $minor) return; //skip minors
1172    $bcc  = subscriber_addresslist($id);
1173    if(empty($bcc)) return;
1174    $to   = '';
1175    $text = rawLocale('subscribermail');
1176  }elseif($who == 'register'){
1177    if(empty($conf['registernotify'])) return;
1178    $text = rawLocale('registermail');
1179    $to   = $conf['registernotify'];
1180    $bcc  = '';
1181  }else{
1182    return; //just to be safe
1183  }
1184
1185  $text = str_replace('@DATE@',date($conf['dformat']),$text);
1186  $text = str_replace('@BROWSER@',$_SERVER['HTTP_USER_AGENT'],$text);
1187  $text = str_replace('@IPADDRESS@',$_SERVER['REMOTE_ADDR'],$text);
1188  $text = str_replace('@HOSTNAME@',gethostbyaddr($_SERVER['REMOTE_ADDR']),$text);
1189  $text = str_replace('@NEWPAGE@',wl($id,'',true),$text);
1190  $text = str_replace('@PAGE@',$id,$text);
1191  $text = str_replace('@TITLE@',$conf['title'],$text);
1192  $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text);
1193  $text = str_replace('@SUMMARY@',$summary,$text);
1194  $text = str_replace('@USER@',$_SERVER['REMOTE_USER'],$text);
1195
1196  foreach ($replace as $key => $substitution) {
1197    $text = str_replace('@'.strtoupper($key).'@',$substitution, $text);
1198  }
1199
1200  if($who == 'register'){
1201    $subject = $lang['mail_new_user'].' '.$summary;
1202  }elseif($rev){
1203    $subject = $lang['mail_changed'].' '.$id;
1204    $text = str_replace('@OLDPAGE@',wl($id,"rev=$rev",true),$text);
1205    require_once(DOKU_INC.'inc/DifferenceEngine.php');
1206    $df  = new Diff(split("\n",rawWiki($id,$rev)),
1207                    split("\n",rawWiki($id)));
1208    $dformat = new UnifiedDiffFormatter();
1209    $diff    = $dformat->format($df);
1210  }else{
1211    $subject=$lang['mail_newpage'].' '.$id;
1212    $text = str_replace('@OLDPAGE@','none',$text);
1213    $diff = rawWiki($id);
1214  }
1215  $text = str_replace('@DIFF@',$diff,$text);
1216  $subject = '['.$conf['title'].'] '.$subject;
1217
1218  mail_send($to,$subject,$text,$conf['mailfrom'],'',$bcc);
1219}
1220
1221/**
1222 * extracts the query from a google referer
1223 *
1224 * @todo   should be more generic and support yahoo et al
1225 * @author Andreas Gohr <andi@splitbrain.org>
1226 */
1227function getGoogleQuery(){
1228  $url = parse_url($_SERVER['HTTP_REFERER']);
1229  if(!$url) return '';
1230
1231  if(!preg_match("#google\.#i",$url['host'])) return '';
1232  $query = array();
1233  parse_str($url['query'],$query);
1234
1235  return $query['q'];
1236}
1237
1238/**
1239 * Try to set correct locale
1240 *
1241 * @deprecated No longer used
1242 * @author     Andreas Gohr <andi@splitbrain.org>
1243 */
1244function setCorrectLocale(){
1245  global $conf;
1246  global $lang;
1247
1248  $enc = strtoupper($lang['encoding']);
1249  foreach ($lang['locales'] as $loc){
1250    //try locale
1251    if(@setlocale(LC_ALL,$loc)) return;
1252    //try loceale with encoding
1253    if(@setlocale(LC_ALL,"$loc.$enc")) return;
1254  }
1255  //still here? try to set from environment
1256  @setlocale(LC_ALL,"");
1257}
1258
1259/**
1260 * Return the human readable size of a file
1261 *
1262 * @param       int    $size   A file size
1263 * @param       int    $dec    A number of decimal places
1264 * @author      Martin Benjamin <b.martin@cybernet.ch>
1265 * @author      Aidan Lister <aidan@php.net>
1266 * @version     1.0.0
1267 */
1268function filesize_h($size, $dec = 1){
1269  $sizes = array('B', 'KB', 'MB', 'GB');
1270  $count = count($sizes);
1271  $i = 0;
1272
1273  while ($size >= 1024 && ($i < $count - 1)) {
1274    $size /= 1024;
1275    $i++;
1276  }
1277
1278  return round($size, $dec) . ' ' . $sizes[$i];
1279}
1280
1281/**
1282 * return an obfuscated email address in line with $conf['mailguard'] setting
1283 *
1284 * @author Harry Fuecks <hfuecks@gmail.com>
1285 * @author Christopher Smith <chris@jalakai.co.uk>
1286 */
1287function obfuscate($email) {
1288  global $conf;
1289
1290  switch ($conf['mailguard']) {
1291    case 'visible' :
1292      $obfuscate = array('@' => ' [at] ', '.' => ' [dot] ', '-' => ' [dash] ');
1293      return strtr($email, $obfuscate);
1294
1295    case 'hex' :
1296      $encode = '';
1297      for ($x=0; $x < strlen($email); $x++) $encode .= '&#x' . bin2hex($email{$x}).';';
1298      return $encode;
1299
1300    case 'none' :
1301    default :
1302      return $email;
1303  }
1304}
1305
1306/**
1307 * Return DokuWikis version
1308 *
1309 * @author Andreas Gohr <andi@splitbrain.org>
1310 */
1311function getVersion(){
1312  //import version string
1313  if(@file_exists('VERSION')){
1314    //official release
1315    return 'Release '.trim(io_readfile(DOKU_INC.'/VERSION'));
1316  }elseif(is_dir('_darcs')){
1317    //darcs checkout
1318    $inv = file('_darcs/inventory');
1319    $inv = preg_grep('#\*\*\d{14}[\]$]#',$inv);
1320    $cur = array_pop($inv);
1321    preg_match('#\*\*(\d{4})(\d{2})(\d{2})#',$cur,$matches);
1322    return 'Darcs '.$matches[1].'-'.$matches[2].'-'.$matches[3];
1323  }else{
1324    return 'snapshot?';
1325  }
1326}
1327
1328/**
1329 * Run a few sanity checks
1330 *
1331 * @author Andreas Gohr <andi@splitbrain.org>
1332 */
1333function check(){
1334  global $conf;
1335  global $INFO;
1336
1337  msg('DokuWiki version: '.getVersion(),1);
1338
1339  if(version_compare(phpversion(),'4.3.0','<')){
1340    msg('Your PHP version is too old ('.phpversion().' vs. 4.3.+ recommended)',-1);
1341  }elseif(version_compare(phpversion(),'4.3.10','<')){
1342    msg('Consider upgrading PHP to 4.3.10 or higher for security reasons (your version: '.phpversion().')',0);
1343  }else{
1344    msg('PHP version '.phpversion(),1);
1345  }
1346
1347  if(is_writable($conf['changelog'])){
1348    msg('Changelog is writable',1);
1349  }else{
1350    if (file_exists($conf['changelog'])) {
1351      msg('Changelog is not writable',-1);
1352    }
1353  }
1354
1355  if (isset($conf['changelog_old']) && file_exists($conf['changelog_old'])) {
1356    msg('Old changelog exists.', 0);
1357  }
1358
1359  if (file_exists($conf['changelog'].'_failed')) {
1360    msg('Importing old changelog failed.', -1);
1361  } else if (file_exists($conf['changelog'].'_importing')) {
1362    msg('Importing old changelog now.', 0);
1363  } else if (file_exists($conf['changelog'].'_import_ok')) {
1364    msg('Old changelog imported.', 1);
1365  }
1366
1367  if(is_writable($conf['datadir'])){
1368    msg('Datadir is writable',1);
1369  }else{
1370    msg('Datadir is not writable',-1);
1371  }
1372
1373  if(is_writable($conf['olddir'])){
1374    msg('Attic is writable',1);
1375  }else{
1376    msg('Attic is not writable',-1);
1377  }
1378
1379  if(is_writable($conf['mediadir'])){
1380    msg('Mediadir is writable',1);
1381  }else{
1382    msg('Mediadir is not writable',-1);
1383  }
1384
1385  if(is_writable($conf['cachedir'])){
1386    msg('Cachedir is writable',1);
1387  }else{
1388    msg('Cachedir is not writable',-1);
1389  }
1390
1391  if(is_writable($conf['lockdir'])){
1392    msg('Lockdir is writable',1);
1393  }else{
1394    msg('Lockdir is not writable',-1);
1395  }
1396
1397  if(is_writable(DOKU_CONF.'users.auth.php')){
1398    msg('conf/users.auth.php is writable',1);
1399  }else{
1400    msg('conf/users.auth.php is not writable',0);
1401  }
1402
1403  if(function_exists('mb_strpos')){
1404    if(defined('UTF8_NOMBSTRING')){
1405      msg('mb_string extension is available but will not be used',0);
1406    }else{
1407      msg('mb_string extension is available and will be used',1);
1408    }
1409  }else{
1410    msg('mb_string extension not available - PHP only replacements will be used',0);
1411  }
1412
1413  if($conf['allowdebug']){
1414    msg('Debugging support is enabled. If you don\'t need it you should set $conf[\'allowdebug\'] = 0',-1);
1415  }else{
1416    msg('Debugging support is disabled',1);
1417  }
1418
1419  msg('Your current permission for this page is '.$INFO['perm'],0);
1420
1421  if(is_writable($INFO['filepath'])){
1422    msg('The current page is writable by the webserver',0);
1423  }else{
1424    msg('The current page is not writable by the webserver',0);
1425  }
1426
1427  if($INFO['writable']){
1428    msg('The current page is writable by you',0);
1429  }else{
1430    msg('The current page is not writable you',0);
1431  }
1432}
1433
1434/**
1435 * Let us know if a user is tracking a page
1436 *
1437 * @author Andreas Gohr <andi@splitbrain.org>
1438 */
1439function is_subscribed($id,$uid){
1440  $file=metaFN($id,'.mlist');
1441  if (@file_exists($file)) {
1442    $mlist = file($file);
1443    $pos = array_search($uid."\n",$mlist);
1444    return is_int($pos);
1445  }
1446
1447  return false;
1448}
1449
1450/**
1451 * Return a string with the email addresses of all the
1452 * users subscribed to a page
1453 *
1454 * @author Steven Danz <steven-danz@kc.rr.com>
1455 */
1456function subscriber_addresslist($id){
1457  global $conf;
1458  global $auth;
1459
1460  $emails = '';
1461
1462  if (!$conf['subscribers']) return;
1463
1464  $mlist = array();
1465  $file=metaFN($id,'.mlist');
1466  if (file_exists($file)) {
1467    $mlist = file($file);
1468  }
1469  if(count($mlist) > 0) {
1470    foreach ($mlist as $who) {
1471      $who = rtrim($who);
1472      $info = $auth->getUserData($who);
1473      $level = auth_aclcheck($id,$who,$info['grps']);
1474      if ($level >= AUTH_READ) {
1475        if (strcasecmp($info['mail'],$conf['notify']) != 0) {
1476          if (empty($emails)) {
1477            $emails = $info['mail'];
1478          } else {
1479            $emails = "$emails,".$info['mail'];
1480          }
1481        }
1482      }
1483    }
1484  }
1485
1486  return $emails;
1487}
1488
1489/**
1490 * Removes quoting backslashes
1491 *
1492 * @author Andreas Gohr <andi@splitbrain.org>
1493 */
1494function unslash($string,$char="'"){
1495  return str_replace('\\'.$char,$char,$string);
1496}
1497
1498//Setup VIM: ex: et ts=2 enc=utf-8 :
1499