register_hook('PARSER_CACHE_USE', 'BEFORE', $this, 'handle_cache_use'); // Hook 2: Substitute source text content inside target namespace boundaries $controller->register_hook('PARSER_WIKITEXT_PREPROCESS', 'BEFORE', $this, 'handle_wikitext_preprocess'); } /** * Strict authentication state resolver */ private function is_user_logged_in() { if (!empty($_SERVER['REMOTE_USER'])) return true; if (function_exists('auth_is_loggedin') && auth_is_loggedin()) return true; global $_user; if (!empty($_user)) return true; return false; } /** * Strictly verifies target configurations against absolute namespace roots */ private function is_target($page_id) { $raw_namespaces = $this->getConf('target_namespaces'); if (empty($raw_namespaces)) return false; $requested_id = strtolower(cleanID($page_id)); // Split your list by commas $patterns = explode(',', $raw_namespaces); foreach ($patterns as $pattern) { // FIX: Explicitly trim leading/trailing spaces from config settings $pattern = trim($pattern); if (empty($pattern)) continue; // Handle Wildcard Sub-Namespaces strictly (e.g. "explore:earth:people:*") if (substr($pattern, -2) === ':*') { $base_ns = strtolower(cleanID(substr($pattern, 0, -2))); // Ensure strict start matching from index 0 if (strpos($requested_id, $base_ns . ':') === 0) { return true; } } else { // Handle Exact Page Matches strictly $pattern_clean = strtolower(cleanID($pattern)); if ($requested_id === $pattern_clean) { return true; } } } return false; } private function is_restriction_active() { $view_start = (int)$this->getConf('night_start_hour'); $view_end = (int)$this->getConf('night_end_hour'); if (isset($_COOKIE['user_tz_offset'])) { $offset_seconds = (int)$_COOKIE['user_tz_offset'] * -60; $user_hour = (int)gmdate('G', time() + $offset_seconds); } else { $user_hour = (int)gmdate('G'); } if ($view_start < $view_end) { $is_inside_view_window = ($user_hour >= $view_start && $user_hour < $view_end); } else { $is_inside_view_window = ($view_start === $view_end) || ($user_hour >= $view_start || $user_hour < $view_end); } return !$is_inside_view_window; } public function handle_cache_use(Doku_Event $event, $param) { global $ID; // ABSOLUTE ISOLATION EXIT: If page is not a targeted match, abort immediately and do nothing if (!$this->is_target($ID)) return; if (!page_exists($ID)) return; // Force dynamic re-rendering exclusively for targeted locked spaces $event->preventDefault(); $event->stopPropagation(); $event->result = false; } public function handle_wikitext_preprocess(Doku_Event $event, $param) { // BYPASS COMPLETELY: Do absolutely nothing if the user is authenticated/logged in if ($this->is_user_logged_in()) return; global $ID; // ABSOLUTE ISOLATION EXIT: If page is not a targeted match, abort immediately and do nothing if (!$this->is_target($ID)) return; if (!page_exists($ID)) return; if (!$this->is_restriction_active()) return; $notice_config = $this->getConf('notice_page'); $notice_page_id = strtolower(cleanID($notice_config)); if (strtolower(cleanID($ID)) === $notice_page_id) return; $notice_file = wikiFN($notice_page_id); if (@file_exists($notice_file)) { $event->data = io_readFile($notice_file); } else { $event->data = "====== Access Restricted ======\nThis workspace is locked and unavailable at this time.\n\nAccess is permitted exclusively between **" . $this->getConf('night_start_hour') . ":00** and **" . $this->getConf('night_end_hour') . ":00** local time."; } } }