xref: /plugin/sqlite/Functions.php (revision ff1cb7aeb9f435cab452a4ebd057f46f4dcfc245)
1<?php
2/**
3 * @noinspection PhpUndefinedMethodInspection
4 * @noinspection PhpComposerExtensionStubsInspection
5 */
6
7
8namespace dokuwiki\plugin\sqlite;
9
10/**
11 * SQLite registered functions
12 */
13class Functions
14{
15
16    /**
17     * Register all standard functions
18     *
19     * @param \PDO $pdo
20     */
21    public static function register($pdo)
22    {
23        $pdo->sqliteCreateFunction('GETACCESSLEVEL', [Functions::class, 'getAccessLevel'], 1);
24        $pdo->sqliteCreateFunction('PAGEEXISTS', [Functions::class, 'pageExists'], 1);
25        $pdo->sqliteCreateFunction('REGEXP', [Functions::class, 'regExp'], 2);
26        $pdo->sqliteCreateFunction('CLEANID', 'cleanID', 1);
27        $pdo->sqliteCreateFunction('RESOLVEPAGE', [Functions::class, 'resolvePage'], 1);
28    }
29
30    /**
31     * Callback checks the permissions for the current user
32     *
33     * This function is registered as a SQL function named GETACCESSLEVEL
34     *
35     * @param string $pageid page ID (needs to be resolved and cleaned)
36     * @return int permission level
37     */
38    public static function getAccessLevel($pageid)
39    {
40        static $aclcache = [];
41
42        if (isset($aclcache[$pageid])) {
43            return $aclcache[$pageid];
44        }
45
46        if (isHiddenPage($pageid)) {
47            $acl = AUTH_NONE;
48        } else {
49            $acl = auth_quickaclcheck($pageid);
50        }
51        $aclcache[$pageid] = $acl;
52        return $acl;
53    }
54
55    /**
56     * Wrapper around page_exists() with static caching
57     *
58     * This function is registered as a SQL function named PAGEEXISTS
59     *
60     * @param string $pageid
61     * @return int 0|1
62     */
63    public static function pageExists($pageid)
64    {
65        static $cache = [];
66        if (!isset($cache[$pageid])) {
67            $cache[$pageid] = page_exists($pageid);
68
69        }
70        return (int)$cache[$pageid];
71    }
72
73    /**
74     * Match a regular expression against a value
75     *
76     * This function is registered as a SQL function named REGEXP
77     *
78     * @param string $regexp
79     * @param string $value
80     * @return bool
81     */
82    public static function regExp($regexp, $value)
83    {
84        $regexp = addcslashes($regexp, '/');
85        return (bool)preg_match('/' . $regexp . '/u', $value);
86    }
87
88    /**
89     * Resolves a page ID (relative namespaces, plurals etc)
90     *
91     * This function is registered as a SQL function named RESOLVEPAGE
92     *
93     * @param string $page The page ID to resolve
94     * @param string $context The page ID (not namespace!) to resolve the page with
95     * @return null|string
96     */
97    public static function resolvePage($page, $context)
98    {
99        if (is_null($page)) return null;
100        if (is_null($context)) return cleanID($page);
101
102        $ns = getNS($context);
103        resolve_pageid($ns, $page, $exists);
104        return $page;
105    }
106
107}
108