xref: /dokuwiki/inc/common.php (revision d818621633e2c687264682f0504869858f9c780d)
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  $match = array();
499  for($i=0; $i<$cnt; $i++){
500    if(preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/',$ip[$i],$match)) {
501      $ip[$i] = $match[0];
502    } else {
503      $ip[$i] = '';
504    }
505    if(empty($ip[$i])) unset($ip[$i]);
506  }
507  $ip = array_values(array_unique($ip));
508  if(!$ip[0]) $ip[0] = '0.0.0.0'; // for some strange reason we don't have a IP
509
510  if(!$single) return join(',',$ip);
511
512  // decide which IP to use, trying to avoid local addresses
513  $ip = array_reverse($ip);
514  foreach($ip as $i){
515    if(preg_match('/^(127\.|10\.|192\.168\.|172\.((1[6-9])|(2[0-9])|(3[0-1]))\.)/',$i)){
516      continue;
517    }else{
518      return $i;
519    }
520  }
521  // still here? just use the first (last) address
522  return $ip[0];
523}
524
525/**
526 * Checks if a given page is currently locked.
527 *
528 * removes stale lockfiles
529 *
530 * @author Andreas Gohr <andi@splitbrain.org>
531 */
532function checklock($id){
533  global $conf;
534  $lock = wikiLockFN($id);
535
536  //no lockfile
537  if(!@file_exists($lock)) return false;
538
539  //lockfile expired
540  if((time() - filemtime($lock)) > $conf['locktime']){
541    @unlink($lock);
542    return false;
543  }
544
545  //my own lock
546  $ip = io_readFile($lock);
547  if( ($ip == clientIP()) || ($ip == $_SERVER['REMOTE_USER']) ){
548    return false;
549  }
550
551  return $ip;
552}
553
554/**
555 * Lock a page for editing
556 *
557 * @author Andreas Gohr <andi@splitbrain.org>
558 */
559function lock($id){
560  $lock = wikiLockFN($id);
561  if($_SERVER['REMOTE_USER']){
562    io_saveFile($lock,$_SERVER['REMOTE_USER']);
563  }else{
564    io_saveFile($lock,clientIP());
565  }
566}
567
568/**
569 * Unlock a page if it was locked by the user
570 *
571 * @author Andreas Gohr <andi@splitbrain.org>
572 * @return bool true if a lock was removed
573 */
574function unlock($id){
575  $lock = wikiLockFN($id);
576  if(@file_exists($lock)){
577    $ip = io_readFile($lock);
578    if( ($ip == clientIP()) || ($ip == $_SERVER['REMOTE_USER']) ){
579      @unlink($lock);
580      return true;
581    }
582  }
583  return false;
584}
585
586/**
587 * convert line ending to unix format
588 *
589 * @see    formText() for 2crlf conversion
590 * @author Andreas Gohr <andi@splitbrain.org>
591 */
592function cleanText($text){
593  $text = preg_replace("/(\015\012)|(\015)/","\012",$text);
594  return $text;
595}
596
597/**
598 * Prepares text for print in Webforms by encoding special chars.
599 * It also converts line endings to Windows format which is
600 * pseudo standard for webforms.
601 *
602 * @see    cleanText() for 2unix conversion
603 * @author Andreas Gohr <andi@splitbrain.org>
604 */
605function formText($text){
606  $text = preg_replace("/\012/","\015\012",$text);
607  return htmlspecialchars($text);
608}
609
610/**
611 * Returns the specified local text in raw format
612 *
613 * @author Andreas Gohr <andi@splitbrain.org>
614 */
615function rawLocale($id){
616  return io_readFile(localeFN($id));
617}
618
619/**
620 * Returns the raw WikiText
621 *
622 * @author Andreas Gohr <andi@splitbrain.org>
623 */
624function rawWiki($id,$rev=''){
625  return io_readWikiPage(wikiFN($id, $rev), $id, $rev);
626}
627
628/**
629 * Returns the pagetemplate contents for the ID's namespace
630 *
631 * @author Andreas Gohr <andi@splitbrain.org>
632 */
633function pageTemplate($id){
634  global $conf;
635  global $INFO;
636  $tpl = io_readFile(dirname(wikiFN($id)).'/_template.txt');
637  $tpl = str_replace('@ID@',$id,$tpl);
638  $tpl = str_replace('@NS@',getNS($id),$tpl);
639  $tpl = str_replace('@PAGE@',strtr(noNS($id),'_',' '),$tpl);
640  $tpl = str_replace('@USER@',$_SERVER['REMOTE_USER'],$tpl);
641  $tpl = str_replace('@NAME@',$INFO['userinfo']['name'],$tpl);
642  $tpl = str_replace('@MAIL@',$INFO['userinfo']['mail'],$tpl);
643  $tpl = str_replace('@DATE@',date($conf['dformat']),$tpl);
644  return $tpl;
645}
646
647
648/**
649 * Returns the raw Wiki Text in three slices.
650 *
651 * The range parameter needs to have the form "from-to"
652 * and gives the range of the section in bytes - no
653 * UTF-8 awareness is needed.
654 * The returned order is prefix, section and suffix.
655 *
656 * @author Andreas Gohr <andi@splitbrain.org>
657 */
658function rawWikiSlices($range,$id,$rev=''){
659  list($from,$to) = split('-',$range,2);
660  $text = io_readWikiPage(wikiFN($id, $rev), $id, $rev);
661  if(!$from) $from = 0;
662  if(!$to)   $to   = strlen($text)+1;
663
664  $slices[0] = substr($text,0,$from-1);
665  $slices[1] = substr($text,$from-1,$to-$from);
666  $slices[2] = substr($text,$to);
667
668  return $slices;
669}
670
671/**
672 * Joins wiki text slices
673 *
674 * function to join the text slices with correct lineendings again.
675 * When the pretty parameter is set to true it adds additional empty
676 * lines between sections if needed (used on saving).
677 *
678 * @author Andreas Gohr <andi@splitbrain.org>
679 */
680function con($pre,$text,$suf,$pretty=false){
681
682  if($pretty){
683    if($pre && substr($pre,-1) != "\n") $pre .= "\n";
684    if($suf && substr($text,-1) != "\n") $text .= "\n";
685  }
686
687  if($pre) $pre .= "\n";
688  if($suf) $text .= "\n";
689  return $pre.$text.$suf;
690}
691
692/**
693 * print debug messages
694 *
695 * little function to print the content of a var
696 *
697 * @author Andreas Gohr <andi@splitbrain.org>
698 */
699function dbg($msg,$hidden=false){
700  (!$hidden) ? print '<pre class="dbg">' : print "<!--\n";
701  print_r($msg);
702  (!$hidden) ? print '</pre>' : print "\n-->";
703}
704
705/**
706 * Print info to a log file
707 *
708 * @author Andreas Gohr <andi@splitbrain.org>
709 */
710function dbglog($msg){
711  global $conf;
712  $file = $conf['cachedir'].'/debug.log';
713  $fh = fopen($file,'a');
714  if($fh){
715    fwrite($fh,date('H:i:s ').$_SERVER['REMOTE_ADDR'].': '.$msg."\n");
716    fclose($fh);
717  }
718}
719
720/**
721 * Add's an entry to the changelog and saves the metadata for the page
722 *
723 * @author Andreas Gohr <andi@splitbrain.org>
724 * @author Esther Brunner <wikidesign@gmail.com>
725 * @author Ben Coburn <btcoburn@silicodon.net>
726 */
727function addLogEntry($date, $id, $type='E', $summary='', $extra=''){
728  global $conf, $INFO;
729
730  $id = cleanid($id);
731  $file = wikiFN($id);
732  $created = @filectime($file);
733  $minor = ($type==='e');
734  $wasRemoved = ($type==='D');
735
736  if(!$date) $date = time(); //use current time if none supplied
737  $remote = $_SERVER['REMOTE_ADDR'];
738  $user   = $_SERVER['REMOTE_USER'];
739
740  $logline = array(
741    'date'  => $date,
742    'ip'    => $remote,
743    'type'  => $type,
744    'id'    => $id,
745    'user'  => $user,
746    'sum'   => $summary,
747    'extra' => $extra
748  );
749
750  // update metadata
751  if (!$wasRemoved) {
752    $meta = array();
753    if (!$INFO['exists']){ // newly created
754      $meta['date']['created'] = $created;
755      if ($user) $meta['creator'] = $INFO['userinfo']['name'];
756    } elseif (!$minor) {   // non-minor modification
757      $meta['date']['modified'] = $date;
758      if ($user) $meta['contributor'][$user] = $INFO['userinfo']['name'];
759    }
760    $meta['last_change'] = $logline;
761    p_set_metadata($id, $meta, true);
762  }
763
764  // add changelog lines
765  $logline = implode("\t", $logline)."\n";
766  io_saveFile(metaFN($id,'.changes'),$logline,true); //page changelog
767  io_saveFile($conf['changelog'],$logline,true); //global changelog cache
768}
769
770/**
771 * Internal function used by getRecents
772 *
773 * don't call directly
774 *
775 * @see getRecents()
776 * @author Andreas Gohr <andi@splitbrain.org>
777 * @author Ben Coburn <btcoburn@silicodon.net>
778 */
779function _handleRecent($line,$ns,$flags){
780  static $seen  = array();         //caches seen pages and skip them
781  if(empty($line)) return false;   //skip empty lines
782
783  // split the line into parts
784  $recent = parseChangelogLine($line);
785  if ($recent===false) { return false; }
786
787  // skip seen ones
788  if(isset($seen[$recent['id']])) return false;
789
790  // skip minors
791  if($recent['type']==='e' && ($flags & RECENTS_SKIP_MINORS)) return false;
792
793  // remember in seen to skip additional sights
794  $seen[$recent['id']] = 1;
795
796  // check if it's a hidden page
797  if(isHiddenPage($recent['id'])) return false;
798
799  // filter namespace
800  if (($ns) && (strpos($recent['id'],$ns.':') !== 0)) return false;
801
802  // exclude subnamespaces
803  if (($flags & RECENTS_SKIP_SUBSPACES) && (getNS($recent['id']) != $ns)) return false;
804
805  // check ACL
806  if (auth_quickaclcheck($recent['id']) < AUTH_READ) return false;
807
808  // check existance
809  if((!@file_exists(wikiFN($recent['id']))) && ($flags & RECENTS_SKIP_DELETED)) return false;
810
811  return $recent;
812}
813
814
815/**
816 * returns an array of recently changed files using the
817 * changelog
818 *
819 * The following constants can be used to control which changes are
820 * included. Add them together as needed.
821 *
822 * RECENTS_SKIP_DELETED   - don't include deleted pages
823 * RECENTS_SKIP_MINORS    - don't include minor changes
824 * RECENTS_SKIP_SUBSPACES - don't include subspaces
825 *
826 * @param int    $first   number of first entry returned (for paginating
827 * @param int    $num     return $num entries
828 * @param string $ns      restrict to given namespace
829 * @param bool   $flags   see above
830 *
831 * @author Ben Coburn <btcoburn@silicodon.net>
832 */
833function getRecents($first,$num,$ns='',$flags=0){
834  global $conf;
835  $recent = array();
836  $count  = 0;
837
838  if(!$num)
839    return $recent;
840
841  // read all recent changes. (kept short)
842  $lines = file($conf['changelog']);
843
844  // handle lines
845  for($i = count($lines)-1; $i >= 0; $i--){
846    $rec = _handleRecent($lines[$i], $ns, $flags);
847    if($rec !== false) {
848      if(--$first >= 0) continue; // skip first entries
849      $recent[] = $rec;
850      $count++;
851      // break when we have enough entries
852      if($count >= $num){ break; }
853    }
854  }
855
856  return $recent;
857}
858
859/**
860 * parses a changelog line into it's components
861 *
862 * @author Ben Coburn <btcoburn@silicodon.net>
863 */
864function parseChangelogLine($line) {
865  $tmp = explode("\t", $line);
866    if ($tmp!==false && count($tmp)>1) {
867      $info = array();
868      $info['date']  = $tmp[0]; // unix timestamp
869      $info['ip']    = $tmp[1]; // IPv4 address (127.0.0.1)
870      $info['type']  = $tmp[2]; // log line type
871      $info['id']    = $tmp[3]; // page id
872      $info['user']  = $tmp[4]; // user name
873      $info['sum']   = $tmp[5]; // edit summary (or action reason)
874      $info['extra'] = rtrim($tmp[6], "\n"); // extra data (varies by line type)
875      return $info;
876  } else { return false; }
877}
878
879/**
880 * Get the changelog information for a specific page id
881 * and revision (timestamp). Adjacent changelog lines
882 * are optimistically parsed and cached to speed up
883 * consecutive calls to getRevisionInfo. For large
884 * changelog files, only the chunk containing the
885 * requested changelog line is read.
886 *
887 * @author Ben Coburn <btcoburn@silicodon.net>
888 */
889function getRevisionInfo($id, $rev, $chunk_size=8192) {
890  global $cache_revinfo;
891  $cache =& $cache_revinfo;
892  if (!isset($cache[$id])) { $cache[$id] = array(); }
893  $rev = max($rev, 0);
894
895  // check if it's already in the memory cache
896  if (isset($cache[$id]) && isset($cache[$id][$rev])) {
897    return $cache[$id][$rev];
898  }
899
900  $file = metaFN($id, '.changes');
901  if (!@file_exists($file)) { return false; }
902  if (filesize($file)<$chunk_size || $chunk_size==0) {
903    // read whole file
904    $lines = file($file);
905    if ($lines===false) { return false; }
906  } else {
907    // read by chunk
908    $fp = fopen($file, 'rb'); // "file pointer"
909    if ($fp===false) { return false; }
910    $head = 0;
911    fseek($fp, 0, SEEK_END);
912    $tail = ftell($fp);
913    $finger = 0;
914    $finger_rev = 0;
915
916    // find chunk
917    while ($tail-$head>$chunk_size) {
918      $finger = $head+floor(($tail-$head)/2.0);
919      fseek($fp, $finger);
920      fgets($fp); // slip the finger forward to a new line
921      $finger = ftell($fp);
922      $tmp = fgets($fp); // then read at that location
923      $tmp = parseChangelogLine($tmp);
924      $finger_rev = $tmp['date'];
925      if ($finger==$head || $finger==$tail) { break; }
926      if ($finger_rev>$rev) {
927        $tail = $finger;
928      } else {
929        $head = $finger;
930      }
931    }
932
933    if ($tail-$head<1) {
934      // cound not find chunk, assume requested rev is missing
935      fclose($fp);
936      return false;
937    }
938
939    // read chunk
940    $chunk = '';
941    $chunk_size = max($tail-$head, 0); // found chunk size
942    $got = 0;
943    fseek($fp, $head);
944    while ($got<$chunk_size && !feof($fp)) {
945      $tmp = fread($fp, max($chunk_size-$got, 0));
946      if ($tmp===false) { break; } //error state
947      $got += strlen($tmp);
948      $chunk .= $tmp;
949    }
950    $lines = explode("\n", $chunk);
951    array_pop($lines); // remove trailing newline
952    fclose($fp);
953  }
954
955  // parse and cache changelog lines
956  foreach ($lines as $value) {
957    $tmp = parseChangelogLine($value);
958    if ($tmp!==false) {
959      $cache[$id][$tmp['date']] = $tmp;
960    }
961  }
962  if (!isset($cache[$id][$rev])) { return false; }
963  return $cache[$id][$rev];
964}
965
966/**
967 * Return a list of page revisions numbers
968 * Does not guarantee that the revision exists in the attic,
969 * only that a line with the date exists in the changelog.
970 * By default the current revision is skipped.
971 *
972 * id:    the page of interest
973 * first: skip the first n changelog lines
974 * num:   number of revisions to return
975 *
976 * The current revision is automatically skipped when the page exists.
977 * See $INFO['meta']['last_change'] for the current revision.
978 *
979 * For efficiency, the log lines are parsed and cached for later
980 * calls to getRevisionInfo. Large changelog files are read
981 * backwards in chunks untill the requested number of changelog
982 * lines are recieved.
983 *
984 * @author Ben Coburn <btcoburn@silicodon.net>
985 */
986function getRevisions($id, $first, $num, $chunk_size=8192) {
987  global $cache_revinfo;
988  $cache =& $cache_revinfo;
989  if (!isset($cache[$id])) { $cache[$id] = array(); }
990
991  $revs = array();
992  $lines = array();
993  $count  = 0;
994  $file = metaFN($id, '.changes');
995  $num = max($num, 0);
996  $chunk_size = max($chunk_size, 0);
997  if ($first<0) { $first = 0; }
998  else if (@file_exists(wikiFN($id))) {
999     // skip current revision if the page exists
1000    $first = max($first+1, 0);
1001  }
1002
1003  if (!@file_exists($file)) { return $revs; }
1004  if (filesize($file)<$chunk_size || $chunk_size==0) {
1005    // read whole file
1006    $lines = file($file);
1007    if ($lines===false) { return $revs; }
1008  } else {
1009    // read chunks backwards
1010    $fp = fopen($file, 'rb'); // "file pointer"
1011    if ($fp===false) { return $revs; }
1012    fseek($fp, 0, SEEK_END);
1013    $tail = ftell($fp);
1014
1015    // chunk backwards
1016    $finger = max($tail-$chunk_size, 0);
1017    while ($count<$num+$first) {
1018      fseek($fp, $finger);
1019      if ($finger>0) {
1020        fgets($fp); // slip the finger forward to a new line
1021        $finger = ftell($fp);
1022      }
1023
1024      // read chunk
1025      if ($tail<=$finger) { break; }
1026      $chunk = '';
1027      $read_size = max($tail-$finger, 0); // found chunk size
1028      $got = 0;
1029      while ($got<$read_size && !feof($fp)) {
1030        $tmp = fread($fp, max($read_size-$got, 0));
1031        if ($tmp===false) { break; } //error state
1032        $got += strlen($tmp);
1033        $chunk .= $tmp;
1034      }
1035      $tmp = explode("\n", $chunk);
1036      array_pop($tmp); // remove trailing newline
1037
1038      // combine with previous chunk
1039      $count += count($tmp);
1040      $lines = array_merge($tmp, $lines);
1041
1042      // next chunk
1043      if ($finger==0) { break; } // already read all the lines
1044      else {
1045        $tail = $finger;
1046        $finger = max($tail-$chunk_size, 0);
1047      }
1048    }
1049    fclose($fp);
1050  }
1051
1052  // skip parsing extra lines
1053  $num = max(min(count($lines)-$first, $num), 0);
1054  if      ($first>0 && $num>0)  { $lines = array_slice($lines, max(count($lines)-$first-$num, 0), $num); }
1055  else if ($first>0 && $num==0) { $lines = array_slice($lines, 0, max(count($lines)-$first, 0)); }
1056  else if ($first==0 && $num>0) { $lines = array_slice($lines, max(count($lines)-$num, 0)); }
1057
1058  // handle lines in reverse order
1059  for ($i = count($lines)-1; $i >= 0; $i--) {
1060    $tmp = parseChangelogLine($lines[$i]);
1061    if ($tmp!==false) {
1062      $cache[$id][$tmp['date']] = $tmp;
1063      $revs[] = $tmp['date'];
1064    }
1065  }
1066
1067  return $revs;
1068}
1069
1070/**
1071 * Saves a wikitext by calling io_writeWikiPage
1072 *
1073 * @author Andreas Gohr <andi@splitbrain.org>
1074 * @author Ben Coburn <btcoburn@silicodon.net>
1075 */
1076function saveWikiText($id,$text,$summary,$minor=false){
1077  global $conf;
1078  global $lang;
1079  global $REV;
1080  // ignore if no changes were made
1081  if($text == rawWiki($id,'')){
1082    return;
1083  }
1084
1085  $file = wikiFN($id);
1086  $old  = saveOldRevision($id);
1087  $wasRemoved = empty($text);
1088  $wasCreated = !@file_exists($file);
1089  $wasReverted = ($REV==true);
1090
1091  if ($wasRemoved){
1092    // remove empty file
1093    @unlink($file);
1094    // remove old meta info...
1095    $mfiles = metaFiles($id);
1096    $changelog = metaFN($id, '.changes');
1097    foreach ($mfiles as $mfile) {
1098      // but keep per-page changelog to preserve page history
1099      if (@file_exists($mfile) && $mfile!==$changelog) { @unlink($mfile); }
1100    }
1101    $del = true;
1102    // autoset summary on deletion
1103    if(empty($summary)) $summary = $lang['deleted'];
1104    // remove empty namespaces
1105    io_sweepNS($id, 'datadir');
1106    io_sweepNS($id, 'mediadir');
1107  }else{
1108    // save file (namespace dir is created in io_writeWikiPage)
1109    io_writeWikiPage($file, $text, $id);
1110    $del = false;
1111  }
1112
1113  // select changelog line type
1114  $extra = '';
1115  $type = 'E';
1116  if ($wasReverted) {
1117    $type = 'R';
1118    $extra = $REV;
1119  }
1120  else if ($wasCreated) { $type = 'C'; }
1121  else if ($wasRemoved) { $type = 'D'; }
1122  else if ($minor && $conf['useacl'] && $_SERVER['REMOTE_USER']) { $type = 'e'; } //minor edits only for logged in users
1123
1124  addLogEntry(@filemtime($file), $id, $type, $summary, $extra);
1125  // send notify mails
1126  notify($id,'admin',$old,$summary,$minor);
1127  notify($id,'subscribers',$old,$summary,$minor);
1128
1129  //purge cache on add by updating the purgefile
1130  if($conf['purgeonadd'] && (!$old || $del)){
1131    io_saveFile($conf['cachedir'].'/purgefile',time());
1132  }
1133}
1134
1135/**
1136 * moves the current version to the attic and returns its
1137 * revision date
1138 *
1139 * @author Andreas Gohr <andi@splitbrain.org>
1140 */
1141function saveOldRevision($id){
1142  global $conf;
1143  $oldf = wikiFN($id);
1144  if(!@file_exists($oldf)) return '';
1145  $date = filemtime($oldf);
1146  $newf = wikiFN($id,$date);
1147  io_writeWikiPage($newf, rawWiki($id), $id, $date);
1148  return $date;
1149}
1150
1151/**
1152 * Sends a notify mail on page change
1153 *
1154 * @param  string  $id       The changed page
1155 * @param  string  $who      Who to notify (admin|subscribers)
1156 * @param  int     $rev      Old page revision
1157 * @param  string  $summary  What changed
1158 * @param  boolean $minor    Is this a minor edit?
1159 * @param  array   $replace  Additional string substitutions, @KEY@ to be replaced by value
1160 *
1161 * @author Andreas Gohr <andi@splitbrain.org>
1162 */
1163function notify($id,$who,$rev='',$summary='',$minor=false,$replace=array()){
1164  global $lang;
1165  global $conf;
1166
1167  // decide if there is something to do
1168  if($who == 'admin'){
1169    if(empty($conf['notify'])) return; //notify enabled?
1170    $text = rawLocale('mailtext');
1171    $to   = $conf['notify'];
1172    $bcc  = '';
1173  }elseif($who == 'subscribers'){
1174    if(!$conf['subscribers']) return; //subscribers enabled?
1175    if($conf['useacl'] && $_SERVER['REMOTE_USER'] && $minor) return; //skip minors
1176    $bcc  = subscriber_addresslist($id);
1177    if(empty($bcc)) return;
1178    $to   = '';
1179    $text = rawLocale('subscribermail');
1180  }elseif($who == 'register'){
1181    if(empty($conf['registernotify'])) return;
1182    $text = rawLocale('registermail');
1183    $to   = $conf['registernotify'];
1184    $bcc  = '';
1185  }else{
1186    return; //just to be safe
1187  }
1188
1189  $text = str_replace('@DATE@',date($conf['dformat']),$text);
1190  $text = str_replace('@BROWSER@',$_SERVER['HTTP_USER_AGENT'],$text);
1191  $text = str_replace('@IPADDRESS@',$_SERVER['REMOTE_ADDR'],$text);
1192  $text = str_replace('@HOSTNAME@',gethostbyaddr($_SERVER['REMOTE_ADDR']),$text);
1193  $text = str_replace('@NEWPAGE@',wl($id,'',true),$text);
1194  $text = str_replace('@PAGE@',$id,$text);
1195  $text = str_replace('@TITLE@',$conf['title'],$text);
1196  $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text);
1197  $text = str_replace('@SUMMARY@',$summary,$text);
1198  $text = str_replace('@USER@',$_SERVER['REMOTE_USER'],$text);
1199
1200  foreach ($replace as $key => $substitution) {
1201    $text = str_replace('@'.strtoupper($key).'@',$substitution, $text);
1202  }
1203
1204  if($who == 'register'){
1205    $subject = $lang['mail_new_user'].' '.$summary;
1206  }elseif($rev){
1207    $subject = $lang['mail_changed'].' '.$id;
1208    $text = str_replace('@OLDPAGE@',wl($id,"rev=$rev",true),$text);
1209    require_once(DOKU_INC.'inc/DifferenceEngine.php');
1210    $df  = new Diff(split("\n",rawWiki($id,$rev)),
1211                    split("\n",rawWiki($id)));
1212    $dformat = new UnifiedDiffFormatter();
1213    $diff    = $dformat->format($df);
1214  }else{
1215    $subject=$lang['mail_newpage'].' '.$id;
1216    $text = str_replace('@OLDPAGE@','none',$text);
1217    $diff = rawWiki($id);
1218  }
1219  $text = str_replace('@DIFF@',$diff,$text);
1220  $subject = '['.$conf['title'].'] '.$subject;
1221
1222  mail_send($to,$subject,$text,$conf['mailfrom'],'',$bcc);
1223}
1224
1225/**
1226 * extracts the query from a google referer
1227 *
1228 * @todo   should be more generic and support yahoo et al
1229 * @author Andreas Gohr <andi@splitbrain.org>
1230 */
1231function getGoogleQuery(){
1232  $url = parse_url($_SERVER['HTTP_REFERER']);
1233  if(!$url) return '';
1234
1235  if(!preg_match("#google\.#i",$url['host'])) return '';
1236  $query = array();
1237  parse_str($url['query'],$query);
1238
1239  return $query['q'];
1240}
1241
1242/**
1243 * Try to set correct locale
1244 *
1245 * @deprecated No longer used
1246 * @author     Andreas Gohr <andi@splitbrain.org>
1247 */
1248function setCorrectLocale(){
1249  global $conf;
1250  global $lang;
1251
1252  $enc = strtoupper($lang['encoding']);
1253  foreach ($lang['locales'] as $loc){
1254    //try locale
1255    if(@setlocale(LC_ALL,$loc)) return;
1256    //try loceale with encoding
1257    if(@setlocale(LC_ALL,"$loc.$enc")) return;
1258  }
1259  //still here? try to set from environment
1260  @setlocale(LC_ALL,"");
1261}
1262
1263/**
1264 * Return the human readable size of a file
1265 *
1266 * @param       int    $size   A file size
1267 * @param       int    $dec    A number of decimal places
1268 * @author      Martin Benjamin <b.martin@cybernet.ch>
1269 * @author      Aidan Lister <aidan@php.net>
1270 * @version     1.0.0
1271 */
1272function filesize_h($size, $dec = 1){
1273  $sizes = array('B', 'KB', 'MB', 'GB');
1274  $count = count($sizes);
1275  $i = 0;
1276
1277  while ($size >= 1024 && ($i < $count - 1)) {
1278    $size /= 1024;
1279    $i++;
1280  }
1281
1282  return round($size, $dec) . ' ' . $sizes[$i];
1283}
1284
1285/**
1286 * return an obfuscated email address in line with $conf['mailguard'] setting
1287 *
1288 * @author Harry Fuecks <hfuecks@gmail.com>
1289 * @author Christopher Smith <chris@jalakai.co.uk>
1290 */
1291function obfuscate($email) {
1292  global $conf;
1293
1294  switch ($conf['mailguard']) {
1295    case 'visible' :
1296      $obfuscate = array('@' => ' [at] ', '.' => ' [dot] ', '-' => ' [dash] ');
1297      return strtr($email, $obfuscate);
1298
1299    case 'hex' :
1300      $encode = '';
1301      for ($x=0; $x < strlen($email); $x++) $encode .= '&#x' . bin2hex($email{$x}).';';
1302      return $encode;
1303
1304    case 'none' :
1305    default :
1306      return $email;
1307  }
1308}
1309
1310/**
1311 * Return DokuWikis version
1312 *
1313 * @author Andreas Gohr <andi@splitbrain.org>
1314 */
1315function getVersion(){
1316  //import version string
1317  if(@file_exists('VERSION')){
1318    //official release
1319    return 'Release '.trim(io_readfile(DOKU_INC.'/VERSION'));
1320  }elseif(is_dir('_darcs')){
1321    //darcs checkout
1322    $inv = file('_darcs/inventory');
1323    $inv = preg_grep('#\*\*\d{14}[\]$]#',$inv);
1324    $cur = array_pop($inv);
1325    preg_match('#\*\*(\d{4})(\d{2})(\d{2})#',$cur,$matches);
1326    return 'Darcs '.$matches[1].'-'.$matches[2].'-'.$matches[3];
1327  }else{
1328    return 'snapshot?';
1329  }
1330}
1331
1332/**
1333 * Run a few sanity checks
1334 *
1335 * @author Andreas Gohr <andi@splitbrain.org>
1336 */
1337function check(){
1338  global $conf;
1339  global $INFO;
1340
1341  msg('DokuWiki version: '.getVersion(),1);
1342
1343  if(version_compare(phpversion(),'4.3.0','<')){
1344    msg('Your PHP version is too old ('.phpversion().' vs. 4.3.+ recommended)',-1);
1345  }elseif(version_compare(phpversion(),'4.3.10','<')){
1346    msg('Consider upgrading PHP to 4.3.10 or higher for security reasons (your version: '.phpversion().')',0);
1347  }else{
1348    msg('PHP version '.phpversion(),1);
1349  }
1350
1351  if(is_writable($conf['changelog'])){
1352    msg('Changelog is writable',1);
1353  }else{
1354    if (@file_exists($conf['changelog'])) {
1355      msg('Changelog is not writable',-1);
1356    }
1357  }
1358
1359  if (isset($conf['changelog_old']) && @file_exists($conf['changelog_old'])) {
1360    msg('Old changelog exists.', 0);
1361  }
1362
1363  if (@file_exists($conf['changelog'].'_failed')) {
1364    msg('Importing old changelog failed.', -1);
1365  } else if (@file_exists($conf['changelog'].'_importing')) {
1366    msg('Importing old changelog now.', 0);
1367  } else if (@file_exists($conf['changelog'].'_import_ok')) {
1368    msg('Old changelog imported.', 1);
1369  }
1370
1371  if(is_writable($conf['datadir'])){
1372    msg('Datadir is writable',1);
1373  }else{
1374    msg('Datadir is not writable',-1);
1375  }
1376
1377  if(is_writable($conf['olddir'])){
1378    msg('Attic is writable',1);
1379  }else{
1380    msg('Attic is not writable',-1);
1381  }
1382
1383  if(is_writable($conf['mediadir'])){
1384    msg('Mediadir is writable',1);
1385  }else{
1386    msg('Mediadir is not writable',-1);
1387  }
1388
1389  if(is_writable($conf['cachedir'])){
1390    msg('Cachedir is writable',1);
1391  }else{
1392    msg('Cachedir is not writable',-1);
1393  }
1394
1395  if(is_writable($conf['lockdir'])){
1396    msg('Lockdir is writable',1);
1397  }else{
1398    msg('Lockdir is not writable',-1);
1399  }
1400
1401  if(is_writable(DOKU_CONF.'users.auth.php')){
1402    msg('conf/users.auth.php is writable',1);
1403  }else{
1404    msg('conf/users.auth.php is not writable',0);
1405  }
1406
1407  if(function_exists('mb_strpos')){
1408    if(defined('UTF8_NOMBSTRING')){
1409      msg('mb_string extension is available but will not be used',0);
1410    }else{
1411      msg('mb_string extension is available and will be used',1);
1412    }
1413  }else{
1414    msg('mb_string extension not available - PHP only replacements will be used',0);
1415  }
1416
1417  if($conf['allowdebug']){
1418    msg('Debugging support is enabled. If you don\'t need it you should set $conf[\'allowdebug\'] = 0',-1);
1419  }else{
1420    msg('Debugging support is disabled',1);
1421  }
1422
1423  msg('Your current permission for this page is '.$INFO['perm'],0);
1424
1425  if(is_writable($INFO['filepath'])){
1426    msg('The current page is writable by the webserver',0);
1427  }else{
1428    msg('The current page is not writable by the webserver',0);
1429  }
1430
1431  if($INFO['writable']){
1432    msg('The current page is writable by you',0);
1433  }else{
1434    msg('The current page is not writable you',0);
1435  }
1436}
1437
1438/**
1439 * Let us know if a user is tracking a page
1440 *
1441 * @author Andreas Gohr <andi@splitbrain.org>
1442 */
1443function is_subscribed($id,$uid){
1444  $file=metaFN($id,'.mlist');
1445  if (@file_exists($file)) {
1446    $mlist = file($file);
1447    $pos = array_search($uid."\n",$mlist);
1448    return is_int($pos);
1449  }
1450
1451  return false;
1452}
1453
1454/**
1455 * Return a string with the email addresses of all the
1456 * users subscribed to a page
1457 *
1458 * @author Steven Danz <steven-danz@kc.rr.com>
1459 */
1460function subscriber_addresslist($id){
1461  global $conf;
1462  global $auth;
1463
1464  $emails = '';
1465
1466  if (!$conf['subscribers']) return;
1467
1468  $mlist = array();
1469  $file=metaFN($id,'.mlist');
1470  if (@file_exists($file)) {
1471    $mlist = file($file);
1472  }
1473  if(count($mlist) > 0) {
1474    foreach ($mlist as $who) {
1475      $who = rtrim($who);
1476      $info = $auth->getUserData($who);
1477      $level = auth_aclcheck($id,$who,$info['grps']);
1478      if ($level >= AUTH_READ) {
1479        if (strcasecmp($info['mail'],$conf['notify']) != 0) {
1480          if (empty($emails)) {
1481            $emails = $info['mail'];
1482          } else {
1483            $emails = "$emails,".$info['mail'];
1484          }
1485        }
1486      }
1487    }
1488  }
1489
1490  return $emails;
1491}
1492
1493/**
1494 * Removes quoting backslashes
1495 *
1496 * @author Andreas Gohr <andi@splitbrain.org>
1497 */
1498function unslash($string,$char="'"){
1499  return str_replace('\\'.$char,$char,$string);
1500}
1501
1502//Setup VIM: ex: et ts=2 enc=utf-8 :
1503