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 global $auth; 41 if(!$auth) return AUTH_DELETE; 42 43 static $aclcache = []; 44 45 if (isset($aclcache[$pageid])) { 46 return $aclcache[$pageid]; 47 } 48 49 if (isHiddenPage($pageid)) { 50 $acl = AUTH_NONE; 51 } else { 52 $acl = auth_quickaclcheck($pageid); 53 } 54 $aclcache[$pageid] = $acl; 55 return $acl; 56 } 57 58 /** 59 * Wrapper around page_exists() with static caching 60 * 61 * This function is registered as a SQL function named PAGEEXISTS 62 * 63 * @param string $pageid 64 * @return int 0|1 65 */ 66 public static function pageExists($pageid) 67 { 68 static $cache = []; 69 if (!isset($cache[$pageid])) { 70 $cache[$pageid] = page_exists($pageid); 71 72 } 73 return (int)$cache[$pageid]; 74 } 75 76 /** 77 * Match a regular expression against a value 78 * 79 * This function is registered as a SQL function named REGEXP 80 * 81 * @param string $regexp 82 * @param string $value 83 * @return bool 84 */ 85 public static function regExp($regexp, $value) 86 { 87 $regexp = addcslashes($regexp, '/'); 88 return (bool)preg_match('/' . $regexp . '/u', $value); 89 } 90 91 /** 92 * Resolves a page ID (relative namespaces, plurals etc) 93 * 94 * This function is registered as a SQL function named RESOLVEPAGE 95 * 96 * @param string $page The page ID to resolve 97 * @param string $context The page ID (not namespace!) to resolve the page with 98 * @return null|string 99 */ 100 public static function resolvePage($page, $context) 101 { 102 if (is_null($page)) return null; 103 if (is_null($context)) return cleanID($page); 104 105 $ns = getNS($context); 106 resolve_pageid($ns, $page, $exists); 107 return $page; 108 } 109 110} 111