xref: /dokuwiki/inc/pageutils.php (revision bfcf8009182fabb89cbae83d9b5117faf0035ede)
1<?php
2/**
3 * Utilities for handling pagenames
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Andreas Gohr <andi@splitbrain.org>
7 * @todo       Combine similar functions like {wiki,media,meta}FN()
8 */
9
10use dokuwiki\ChangeLog\MediaChangeLog;
11use dokuwiki\ChangeLog\PageChangeLog;
12
13/**
14 * Fetch the an ID from request
15 *
16 * Uses either standard $_REQUEST variable or extracts it from
17 * the full request URI when userewrite is set to 2
18 *
19 * For $param='id' $conf['start'] is returned if no id was found.
20 * If the second parameter is true (default) the ID is cleaned.
21 *
22 * @author Andreas Gohr <andi@splitbrain.org>
23 *
24 * @param string $param  the $_REQUEST variable name, default 'id'
25 * @param bool   $clean  if true, ID is cleaned
26 * @return string
27 */
28function getID($param='id',$clean=true){
29    /** @var Input $INPUT */
30    global $INPUT;
31    global $conf;
32    global $ACT;
33
34    $id = $INPUT->str($param);
35
36    //construct page id from request URI
37    if(empty($id) && $conf['userewrite'] == 2){
38        $request = $INPUT->server->str('REQUEST_URI');
39        $script = '';
40
41        //get the script URL
42        if($conf['basedir']){
43            $relpath = '';
44            if($param != 'id') {
45                $relpath = 'lib/exe/';
46            }
47            $script = $conf['basedir'] . $relpath .
48                \dokuwiki\Utf8\PhpString::basename($INPUT->server->str('SCRIPT_FILENAME'));
49
50        }elseif($INPUT->server->str('PATH_INFO')){
51            $request = $INPUT->server->str('PATH_INFO');
52        }elseif($INPUT->server->str('SCRIPT_NAME')){
53            $script = $INPUT->server->str('SCRIPT_NAME');
54        }elseif($INPUT->server->str('DOCUMENT_ROOT') && $INPUT->server->str('SCRIPT_FILENAME')){
55            $script = preg_replace ('/^'.preg_quote($INPUT->server->str('DOCUMENT_ROOT'),'/').'/','',
56                    $INPUT->server->str('SCRIPT_FILENAME'));
57            $script = '/'.$script;
58        }
59
60        //clean script and request (fixes a windows problem)
61        $script  = preg_replace('/\/\/+/','/',$script);
62        $request = preg_replace('/\/\/+/','/',$request);
63
64        //remove script URL and Querystring to gain the id
65        if(preg_match('/^'.preg_quote($script,'/').'(.*)/',$request, $match)){
66            $id = preg_replace ('/\?.*/','',$match[1]);
67        }
68        $id = urldecode($id);
69        //strip leading slashes
70        $id = preg_replace('!^/+!','',$id);
71    }
72
73    // Namespace autolinking from URL
74    if(substr($id,-1) == ':' || ($conf['useslash'] && substr($id,-1) == '/')){
75        if(page_exists($id.$conf['start'])){
76            // start page inside namespace
77            $id = $id.$conf['start'];
78        }elseif(page_exists($id.noNS(cleanID($id)))){
79            // page named like the NS inside the NS
80            $id = $id.noNS(cleanID($id));
81        }elseif(page_exists($id)){
82            // page like namespace exists
83            $id = substr($id,0,-1);
84        }else{
85            // fall back to default
86            $id = $id.$conf['start'];
87        }
88        if (isset($ACT) && $ACT === 'show') {
89            $urlParameters = $_GET;
90            if (isset($urlParameters['id'])) {
91                unset($urlParameters['id']);
92            }
93            send_redirect(wl($id, $urlParameters, true, '&'));
94        }
95    }
96    if($clean) $id = cleanID($id);
97    if($id === '' && $param=='id') $id = $conf['start'];
98
99    return $id;
100}
101
102/**
103 * Remove unwanted chars from ID
104 *
105 * Cleans a given ID to only use allowed characters. Accented characters are
106 * converted to unaccented ones
107 *
108 * @author Andreas Gohr <andi@splitbrain.org>
109 *
110 * @param  string  $raw_id    The pageid to clean
111 * @param  boolean $ascii     Force ASCII
112 * @return string cleaned id
113 */
114function cleanID($raw_id,$ascii=false){
115    global $conf;
116    static $sepcharpat = null;
117
118    global $cache_cleanid;
119    $cache = & $cache_cleanid;
120
121    // check if it's already in the memory cache
122    if (!$ascii && isset($cache[(string)$raw_id])) {
123        return $cache[(string)$raw_id];
124    }
125
126    $sepchar = $conf['sepchar'];
127    if($sepcharpat == null) // build string only once to save clock cycles
128        $sepcharpat = '#\\'.$sepchar.'+#';
129
130    $id = trim((string)$raw_id);
131    $id = \dokuwiki\Utf8\PhpString::strtolower($id);
132
133    //alternative namespace seperator
134    if($conf['useslash']){
135        $id = strtr($id,';/','::');
136    }else{
137        $id = strtr($id,';/',':'.$sepchar);
138    }
139
140    if($conf['deaccent'] == 2 || $ascii) $id = \dokuwiki\Utf8\Clean::romanize($id);
141    if($conf['deaccent'] || $ascii) $id = \dokuwiki\Utf8\Clean::deaccent($id,-1);
142
143    //remove specials
144    $id = \dokuwiki\Utf8\Clean::stripspecials($id,$sepchar,'\*');
145
146    if($ascii) $id = \dokuwiki\Utf8\Clean::strip($id);
147
148    //clean up
149    $id = preg_replace($sepcharpat,$sepchar,$id);
150    $id = preg_replace('#:+#',':',$id);
151    $id = trim($id,':._-');
152    $id = preg_replace('#:[:\._\-]+#',':',$id);
153    $id = preg_replace('#[:\._\-]+:#',':',$id);
154
155    if (!$ascii) $cache[(string)$raw_id] = $id;
156    return($id);
157}
158
159/**
160 * Return namespacepart of a wiki ID
161 *
162 * @author Andreas Gohr <andi@splitbrain.org>
163 *
164 * @param string $id
165 * @return string|false the namespace part or false if the given ID has no namespace (root)
166 */
167function getNS($id){
168    $pos = strrpos((string)$id,':');
169    if($pos!==false){
170        return substr((string)$id,0,$pos);
171    }
172    return false;
173}
174
175/**
176 * Returns the ID without the namespace
177 *
178 * @author Andreas Gohr <andi@splitbrain.org>
179 *
180 * @param string $id
181 * @return string
182 */
183function noNS($id) {
184    $pos = strrpos($id, ':');
185    if ($pos!==false) {
186        return substr($id, $pos+1);
187    } else {
188        return $id;
189    }
190}
191
192/**
193 * Returns the current namespace
194 *
195 * @author Nathan Fritz <fritzn@crown.edu>
196 *
197 * @param string $id
198 * @return string
199 */
200function curNS($id) {
201    return noNS(getNS($id));
202}
203
204/**
205 * Returns the ID without the namespace or current namespace for 'start' pages
206 *
207 * @author Nathan Fritz <fritzn@crown.edu>
208 *
209 * @param string $id
210 * @return string
211 */
212function noNSorNS($id) {
213    global $conf;
214
215    $p = noNS($id);
216    if ($p === $conf['start'] || $p === false || $p === '') {
217        $p = curNS($id);
218        if ($p === false || $p === '') {
219            return $conf['start'];
220        }
221    }
222    return $p;
223}
224
225/**
226 * Creates a XHTML valid linkid from a given headline title
227 *
228 * @param string  $title   The headline title
229 * @param array|bool   $check   Existing IDs (title => number)
230 * @return string the title
231 *
232 * @author Andreas Gohr <andi@splitbrain.org>
233 */
234function sectionID($title,&$check) {
235    $title = str_replace(array(':','.'),'',cleanID($title));
236    $new = ltrim($title,'0123456789_-');
237    if(empty($new)){
238        $title = 'section'.preg_replace('/[^0-9]+/','',$title); //keep numbers from headline
239    }else{
240        $title = $new;
241    }
242
243    if(is_array($check)){
244        // make sure tiles are unique
245        if (!array_key_exists ($title,$check)) {
246            $check[$title] = 0;
247        } else {
248            $title .= ++ $check[$title];
249        }
250    }
251
252    return $title;
253}
254
255/**
256 * Wiki page existence check
257 *
258 * parameters as for wikiFN
259 *
260 * @author Chris Smith <chris@jalakai.co.uk>
261 *
262 * @param string $id page id
263 * @param string|int $rev empty or revision timestamp
264 * @param bool $clean flag indicating that $id should be cleaned (see wikiFN as well)
265 * @param bool $date_at
266 * @return bool exists?
267 */
268function page_exists($id,$rev='',$clean=true, $date_at=false) {
269    if($rev !== '' && $date_at) {
270        $pagelog = new PageChangeLog($id);
271        $pagelog_rev = $pagelog->getLastRevisionAt($rev);
272        if($pagelog_rev !== false)
273            $rev = $pagelog_rev;
274    }
275    return file_exists(wikiFN($id,$rev,$clean));
276}
277
278/**
279 * returns the full path to the datafile specified by ID and optional revision
280 *
281 * The filename is URL encoded to protect Unicode chars
282 *
283 * @param  $raw_id  string   id of wikipage
284 * @param  $rev     int|string   page revision, empty string for current
285 * @param  $clean   bool     flag indicating that $raw_id should be cleaned.  Only set to false
286 *                           when $id is guaranteed to have been cleaned already.
287 * @return string full path
288 *
289 * @author Andreas Gohr <andi@splitbrain.org>
290 */
291function wikiFN($raw_id,$rev='',$clean=true){
292    global $conf;
293
294    global $cache_wikifn;
295    $cache = & $cache_wikifn;
296
297    $id = $raw_id;
298
299    if ($clean) $id = cleanID($id);
300    $id = str_replace(':','/',$id);
301
302    if (isset($cache[$id]) && isset($cache[$id][$rev])) {
303        return $cache[$id][$rev];
304    }
305
306    if(empty($rev)){
307        $fn = $conf['datadir'].'/'.utf8_encodeFN($id).'.txt';
308    }else{
309        $fn = $conf['olddir'].'/'.utf8_encodeFN($id).'.'.$rev.'.txt';
310        if($conf['compression']){
311            //test for extensions here, we want to read both compressions
312            if (file_exists($fn . '.gz')){
313                $fn .= '.gz';
314            }else if(file_exists($fn . '.bz2')){
315                $fn .= '.bz2';
316            }else{
317                //file doesnt exist yet, so we take the configured extension
318                $fn .= '.' . $conf['compression'];
319            }
320        }
321    }
322
323    if (!isset($cache[$id])) { $cache[$id] = array(); }
324    $cache[$id][$rev] = $fn;
325    return $fn;
326}
327
328/**
329 * Returns the full path to the file for locking the page while editing.
330 *
331 * @author Ben Coburn <btcoburn@silicodon.net>
332 *
333 * @param string $id page id
334 * @return string full path
335 */
336function wikiLockFN($id) {
337    global $conf;
338    return $conf['lockdir'].'/'.md5(cleanID($id)).'.lock';
339}
340
341
342/**
343 * returns the full path to the meta file specified by ID and extension
344 *
345 * @author Steven Danz <steven-danz@kc.rr.com>
346 *
347 * @param string $id   page id
348 * @param string $ext  file extension
349 * @return string full path
350 */
351function metaFN($id,$ext){
352    global $conf;
353    $id = cleanID($id);
354    $id = str_replace(':','/',$id);
355    $fn = $conf['metadir'].'/'.utf8_encodeFN($id).$ext;
356    return $fn;
357}
358
359/**
360 * returns the full path to the media's meta file specified by ID and extension
361 *
362 * @author Kate Arzamastseva <pshns@ukr.net>
363 *
364 * @param string $id   media id
365 * @param string $ext  extension of media
366 * @return string
367 */
368function mediaMetaFN($id,$ext){
369    global $conf;
370    $id = cleanID($id);
371    $id = str_replace(':','/',$id);
372    $fn = $conf['mediametadir'].'/'.utf8_encodeFN($id).$ext;
373    return $fn;
374}
375
376/**
377 * returns an array of full paths to all metafiles of a given ID
378 *
379 * @author Esther Brunner <esther@kaffeehaus.ch>
380 * @author Michael Hamann <michael@content-space.de>
381 *
382 * @param string $id page id
383 * @return array
384 */
385function metaFiles($id){
386    $basename = metaFN($id, '');
387    $files    = glob($basename.'.*', GLOB_MARK);
388    // filter files like foo.bar.meta when $id == 'foo'
389    return    $files ? preg_grep('/^'.preg_quote($basename, '/').'\.[^.\/]*$/u', $files) : array();
390}
391
392/**
393 * returns the full path to the mediafile specified by ID
394 *
395 * The filename is URL encoded to protect Unicode chars
396 *
397 * @author Andreas Gohr <andi@splitbrain.org>
398 * @author Kate Arzamastseva <pshns@ukr.net>
399 *
400 * @param string     $id  media id
401 * @param string|int $rev empty string or revision timestamp
402 * @param bool $clean
403 *
404 * @return string full path
405 */
406function mediaFN($id, $rev='', $clean=true){
407    global $conf;
408    if ($clean) $id = cleanID($id);
409    $id = str_replace(':','/',$id);
410    if(empty($rev)){
411        $fn = $conf['mediadir'].'/'.utf8_encodeFN($id);
412    }else{
413        $ext = mimetype($id);
414        $name = substr($id,0, -1*strlen($ext[0])-1);
415        $fn = $conf['mediaolddir'].'/'.utf8_encodeFN($name .'.'.( (int) $rev ).'.'.$ext[0]);
416    }
417    return $fn;
418}
419
420/**
421 * Returns the full filepath to a localized file if local
422 * version isn't found the english one is returned
423 *
424 * @param  string $id  The id of the local file
425 * @param  string $ext The file extension (usually txt)
426 * @return string full filepath to localized file
427 *
428 * @author Andreas Gohr <andi@splitbrain.org>
429 */
430function localeFN($id,$ext='txt'){
431    global $conf;
432    $file = DOKU_CONF.'lang/'.$conf['lang'].'/'.$id.'.'.$ext;
433    if(!file_exists($file)){
434        $file = DOKU_INC.'inc/lang/'.$conf['lang'].'/'.$id.'.'.$ext;
435        if(!file_exists($file)){
436            //fall back to english
437            $file = DOKU_INC.'inc/lang/en/'.$id.'.'.$ext;
438        }
439    }
440    return $file;
441}
442
443/**
444 * Resolve relative paths in IDs
445 *
446 * Do not call directly use resolve_mediaid or resolve_pageid
447 * instead
448 *
449 * Partyly based on a cleanPath function found at
450 * http://php.net/manual/en/function.realpath.php#57016
451 *
452 * @deprecated 2020-09-30
453 * @param string $ns     namespace which is context of id
454 * @param string $id     relative id
455 * @param bool   $clean  flag indicating that id should be cleaned
456 * @return string
457 */
458function resolve_id($ns,$id,$clean=true){
459    global $conf;
460    dbg_deprecated(\dokuwiki\Utils\Resolver::class.' and its children');
461
462    // some pre cleaning for useslash:
463    if($conf['useslash']) $id = str_replace('/',':',$id);
464
465    // if the id starts with a dot we need to handle the
466    // relative stuff
467    if($id && $id[0] == '.'){
468        // normalize initial dots without a colon
469        $id = preg_replace('/^((\.+:)*)(\.+)(?=[^:\.])/','\1\3:',$id);
470        // prepend the current namespace
471        $id = $ns.':'.$id;
472
473        // cleanup relatives
474        $result = array();
475        $pathA  = explode(':', $id);
476        if (!$pathA[0]) $result[] = '';
477        foreach ($pathA AS $key => $dir) {
478            if ($dir == '..') {
479                if (end($result) == '..') {
480                    $result[] = '..';
481                } elseif (!array_pop($result)) {
482                    $result[] = '..';
483                }
484            } elseif ($dir && $dir != '.') {
485                $result[] = $dir;
486            }
487        }
488        if (!end($pathA)) $result[] = '';
489        $id = implode(':', $result);
490    }elseif($ns !== false && strpos($id,':') === false){
491        //if link contains no namespace. add current namespace (if any)
492        $id = $ns.':'.$id;
493    }
494
495    if($clean) $id = cleanID($id);
496    return $id;
497}
498
499/**
500 * Returns a full media id
501 *
502 * @deprecated 2020-09-30
503 * @param string $ns namespace which is context of id
504 * @param string &$page (reference) relative media id, updated to resolved id
505 * @param bool &$exists (reference) updated with existance of media
506 * @param int|string $rev
507 * @param bool $date_at
508 */
509function resolve_mediaid($ns,&$page,&$exists,$rev='',$date_at=false){
510    dbg_deprecated(\dokuwiki\Utils\MediaResolver::class);
511    $resolver = new \dokuwiki\Utils\MediaResolver("$ns:deprecated");
512    $page = $resolver->resolveId($page, $rev, $date_at);
513    $exists = page_exists($page, $rev, false, $date_at);
514}
515
516/**
517 * Returns a full page id
518 *
519 * @deprecated 2020-09-30
520 * @param string $ns namespace which is context of id
521 * @param string &$page (reference) relative page id, updated to resolved id
522 * @param bool &$exists (reference) updated with existance of media
523 * @param string $rev
524 * @param bool $date_at
525 */
526function resolve_pageid($ns,&$page,&$exists,$rev='',$date_at=false )
527{
528    dbg_deprecated(\dokuwiki\Utils\PageResolver::class);
529    $resolver = new \dokuwiki\Utils\PageResolver("$ns:deprecated");
530    $page = $resolver->resolveId($page, $rev, $date_at);
531    $exists = page_exists($page, $rev, false, $date_at);
532}
533
534/**
535 * Returns the name of a cachefile from given data
536 *
537 * The needed directory is created by this function!
538 *
539 * @author Andreas Gohr <andi@splitbrain.org>
540 *
541 * @param string $data  This data is used to create a unique md5 name
542 * @param string $ext   This is appended to the filename if given
543 * @return string       The filename of the cachefile
544 */
545function getCacheName($data,$ext=''){
546    global $conf;
547    $md5  = md5($data);
548    $file = $conf['cachedir'].'/'.$md5[0].'/'.$md5.$ext;
549    io_makeFileDir($file);
550    return $file;
551}
552
553/**
554 * Checks a pageid against $conf['hidepages']
555 *
556 * @author Andreas Gohr <gohr@cosmocode.de>
557 *
558 * @param string $id page id
559 * @return bool
560 */
561function isHiddenPage($id){
562    $data = array(
563        'id' => $id,
564        'hidden' => false
565    );
566    \dokuwiki\Extension\Event::createAndTrigger('PAGEUTILS_ID_HIDEPAGE', $data, '_isHiddenPage');
567    return $data['hidden'];
568}
569
570/**
571 * callback checks if page is hidden
572 *
573 * @param array $data event data    - see isHiddenPage()
574 */
575function _isHiddenPage(&$data) {
576    global $conf;
577    global $ACT;
578
579    if ($data['hidden']) return;
580    if(empty($conf['hidepages'])) return;
581    if($ACT == 'admin') return;
582
583    if(preg_match('/'.$conf['hidepages'].'/ui',':'.$data['id'])){
584        $data['hidden'] = true;
585    }
586}
587
588/**
589 * Reverse of isHiddenPage
590 *
591 * @author Andreas Gohr <gohr@cosmocode.de>
592 *
593 * @param string $id page id
594 * @return bool
595 */
596function isVisiblePage($id){
597    return !isHiddenPage($id);
598}
599
600/**
601 * Format an id for output to a user
602 *
603 * Namespaces are denoted by a trailing “:*”. The root namespace is
604 * “*”. Output is escaped.
605 *
606 * @author Adrian Lang <lang@cosmocode.de>
607 *
608 * @param string $id page id
609 * @return string
610 */
611function prettyprint_id($id) {
612    if (!$id || $id === ':') {
613        return '*';
614    }
615    if ((substr($id, -1, 1) === ':')) {
616        $id .= '*';
617    }
618    return hsc($id);
619}
620
621/**
622 * Encode a UTF-8 filename to use on any filesystem
623 *
624 * Uses the 'fnencode' option to determine encoding
625 *
626 * When the second parameter is true the string will
627 * be encoded only if non ASCII characters are detected -
628 * This makes it safe to run it multiple times on the
629 * same string (default is true)
630 *
631 * @author Andreas Gohr <andi@splitbrain.org>
632 * @see    urlencode
633 *
634 * @param string $file file name
635 * @param bool   $safe if true, only encoded when non ASCII characters detected
636 * @return string
637 */
638function utf8_encodeFN($file,$safe=true){
639    global $conf;
640    if($conf['fnencode'] == 'utf-8') return $file;
641
642    if($safe && preg_match('#^[a-zA-Z0-9/_\-\.%]+$#',$file)){
643        return $file;
644    }
645
646    if($conf['fnencode'] == 'safe'){
647        return SafeFN::encode($file);
648    }
649
650    $file = urlencode($file);
651    $file = str_replace('%2F','/',$file);
652    return $file;
653}
654
655/**
656 * Decode a filename back to UTF-8
657 *
658 * Uses the 'fnencode' option to determine encoding
659 *
660 * @author Andreas Gohr <andi@splitbrain.org>
661 * @see    urldecode
662 *
663 * @param string $file file name
664 * @return string
665 */
666function utf8_decodeFN($file){
667    global $conf;
668    if($conf['fnencode'] == 'utf-8') return $file;
669
670    if($conf['fnencode'] == 'safe'){
671        return SafeFN::decode($file);
672    }
673
674    return urldecode($file);
675}
676
677/**
678 * Find a page in the current namespace (determined from $ID) or any
679 * higher namespace that can be accessed by the current user,
680 * this condition can be overriden by an optional parameter.
681 *
682 * Used for sidebars, but can be used other stuff as well
683 *
684 * @todo   add event hook
685 *
686 * @param  string $page the pagename you're looking for
687 * @param bool $useacl only return pages readable by the current user, false to ignore ACLs
688 * @return false|string the full page id of the found page, false if any
689 */
690function page_findnearest($page, $useacl = true){
691    if ((string) $page === '') return false;
692    global $ID;
693
694    $ns = $ID;
695    do {
696        $ns = getNS($ns);
697        $pageid = cleanID("$ns:$page");
698        if(page_exists($pageid) && (!$useacl || auth_quickaclcheck($pageid) >= AUTH_READ)){
699            return $pageid;
700        }
701    } while($ns !== false);
702
703    return false;
704}
705