xref: /plugin/sqlite/Functions.php (revision da6f67b74ae3d238a0b055b983396f4a6ba03059)
18da7d805SAndreas Gohr<?php
28da7d805SAndreas Gohr/**
38da7d805SAndreas Gohr * @noinspection PhpUndefinedMethodInspection
48da7d805SAndreas Gohr * @noinspection PhpComposerExtensionStubsInspection
58da7d805SAndreas Gohr */
68da7d805SAndreas Gohr
78da7d805SAndreas Gohr
88da7d805SAndreas Gohrnamespace dokuwiki\plugin\sqlite;
98da7d805SAndreas Gohr
108da7d805SAndreas Gohr/**
118da7d805SAndreas Gohr * SQLite registered functions
128da7d805SAndreas Gohr */
138da7d805SAndreas Gohrclass Functions
148da7d805SAndreas Gohr{
158da7d805SAndreas Gohr
168da7d805SAndreas Gohr    /**
178da7d805SAndreas Gohr     * Register all standard functions
188da7d805SAndreas Gohr     *
198da7d805SAndreas Gohr     * @param \PDO $pdo
208da7d805SAndreas Gohr     */
218da7d805SAndreas Gohr    public static function register($pdo)
228da7d805SAndreas Gohr    {
238da7d805SAndreas Gohr        $pdo->sqliteCreateFunction('GETACCESSLEVEL', [Functions::class, 'getAccessLevel'], 1);
248da7d805SAndreas Gohr        $pdo->sqliteCreateFunction('PAGEEXISTS', [Functions::class, 'pageExists'], 1);
258da7d805SAndreas Gohr        $pdo->sqliteCreateFunction('REGEXP', [Functions::class, 'regExp'], 2);
268da7d805SAndreas Gohr        $pdo->sqliteCreateFunction('CLEANID', 'cleanID', 1);
278da7d805SAndreas Gohr        $pdo->sqliteCreateFunction('RESOLVEPAGE', [Functions::class, 'resolvePage'], 1);
288da7d805SAndreas Gohr    }
298da7d805SAndreas Gohr
308da7d805SAndreas Gohr    /**
318da7d805SAndreas Gohr     * Callback checks the permissions for the current user
328da7d805SAndreas Gohr     *
338da7d805SAndreas Gohr     * This function is registered as a SQL function named GETACCESSLEVEL
348da7d805SAndreas Gohr     *
358da7d805SAndreas Gohr     * @param string $pageid page ID (needs to be resolved and cleaned)
368da7d805SAndreas Gohr     * @return int permission level
378da7d805SAndreas Gohr     */
388da7d805SAndreas Gohr    public static function getAccessLevel($pageid)
398da7d805SAndreas Gohr    {
40*da6f67b7SAndreas Gohr        global $auth;
41*da6f67b7SAndreas Gohr        if(!$auth) return AUTH_DELETE;
42*da6f67b7SAndreas Gohr
438da7d805SAndreas Gohr        static $aclcache = [];
448da7d805SAndreas Gohr
458da7d805SAndreas Gohr        if (isset($aclcache[$pageid])) {
468da7d805SAndreas Gohr            return $aclcache[$pageid];
478da7d805SAndreas Gohr        }
488da7d805SAndreas Gohr
498da7d805SAndreas Gohr        if (isHiddenPage($pageid)) {
508da7d805SAndreas Gohr            $acl = AUTH_NONE;
518da7d805SAndreas Gohr        } else {
528da7d805SAndreas Gohr            $acl = auth_quickaclcheck($pageid);
538da7d805SAndreas Gohr        }
548da7d805SAndreas Gohr        $aclcache[$pageid] = $acl;
558da7d805SAndreas Gohr        return $acl;
568da7d805SAndreas Gohr    }
578da7d805SAndreas Gohr
588da7d805SAndreas Gohr    /**
598da7d805SAndreas Gohr     * Wrapper around page_exists() with static caching
608da7d805SAndreas Gohr     *
618da7d805SAndreas Gohr     * This function is registered as a SQL function named PAGEEXISTS
628da7d805SAndreas Gohr     *
638da7d805SAndreas Gohr     * @param string $pageid
648da7d805SAndreas Gohr     * @return int 0|1
658da7d805SAndreas Gohr     */
668da7d805SAndreas Gohr    public static function pageExists($pageid)
678da7d805SAndreas Gohr    {
688da7d805SAndreas Gohr        static $cache = [];
698da7d805SAndreas Gohr        if (!isset($cache[$pageid])) {
708da7d805SAndreas Gohr            $cache[$pageid] = page_exists($pageid);
718da7d805SAndreas Gohr
728da7d805SAndreas Gohr        }
738da7d805SAndreas Gohr        return (int)$cache[$pageid];
748da7d805SAndreas Gohr    }
758da7d805SAndreas Gohr
768da7d805SAndreas Gohr    /**
778da7d805SAndreas Gohr     * Match a regular expression against a value
788da7d805SAndreas Gohr     *
798da7d805SAndreas Gohr     * This function is registered as a SQL function named REGEXP
808da7d805SAndreas Gohr     *
818da7d805SAndreas Gohr     * @param string $regexp
828da7d805SAndreas Gohr     * @param string $value
838da7d805SAndreas Gohr     * @return bool
848da7d805SAndreas Gohr     */
858da7d805SAndreas Gohr    public static function regExp($regexp, $value)
868da7d805SAndreas Gohr    {
878da7d805SAndreas Gohr        $regexp = addcslashes($regexp, '/');
888da7d805SAndreas Gohr        return (bool)preg_match('/' . $regexp . '/u', $value);
898da7d805SAndreas Gohr    }
908da7d805SAndreas Gohr
918da7d805SAndreas Gohr    /**
928da7d805SAndreas Gohr     * Resolves a page ID (relative namespaces, plurals etc)
938da7d805SAndreas Gohr     *
948da7d805SAndreas Gohr     * This function is registered as a SQL function named RESOLVEPAGE
958da7d805SAndreas Gohr     *
968da7d805SAndreas Gohr     * @param string $page The page ID to resolve
978da7d805SAndreas Gohr     * @param string $context The page ID (not namespace!) to resolve the page with
988da7d805SAndreas Gohr     * @return null|string
998da7d805SAndreas Gohr     */
1008da7d805SAndreas Gohr    public static function resolvePage($page, $context)
1018da7d805SAndreas Gohr    {
1028da7d805SAndreas Gohr        if (is_null($page)) return null;
1038da7d805SAndreas Gohr        if (is_null($context)) return cleanID($page);
1048da7d805SAndreas Gohr
1058da7d805SAndreas Gohr        $ns = getNS($context);
1068da7d805SAndreas Gohr        resolve_pageid($ns, $page, $exists);
1078da7d805SAndreas Gohr        return $page;
1088da7d805SAndreas Gohr    }
1098da7d805SAndreas Gohr
1108da7d805SAndreas Gohr}
111