1e39ccd63SJonny Dee<?php 2e39ccd63SJonny Dee/** 3*994617c9SJohann Duscher * Delete Page Guard for DokuWiki 4e39ccd63SJonny Dee * 5e39ccd63SJonny Dee * This action plugin prevents the deletion of pages by blocking "empty save" 6e39ccd63SJonny Dee * operations on pages whose IDs or file paths match a set of user‑defined 7e39ccd63SJonny Dee * regular expressions. Administrators (superusers) and optionally configured 8e39ccd63SJonny Dee * exempt groups are allowed to delete pages regardless of these patterns. 9e39ccd63SJonny Dee * 10e39ccd63SJonny Dee * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 11*994617c9SJohann Duscher * @author Johann Duscher <jonny.dee@posteo.net> 12e39ccd63SJonny Dee */ 13e39ccd63SJonny Dee 14e39ccd63SJonny Deeuse dokuwiki\Extension\ActionPlugin; 15e39ccd63SJonny Dee// Import the correct namespaced classes for event handling 16e39ccd63SJonny Deeuse dokuwiki\Extension\Event; 17e39ccd63SJonny Deeuse dokuwiki\Extension\EventHandler; 18e39ccd63SJonny Dee 19e39ccd63SJonny Dee// Protect against direct call 20e39ccd63SJonny Deeif (!defined('DOKU_INC')) die(); 21e39ccd63SJonny Dee 22e39ccd63SJonny Dee/** 23*994617c9SJohann Duscher * Class action_plugin_deletepageguard 24e39ccd63SJonny Dee * 25e39ccd63SJonny Dee * Registers a handler on COMMON_WIKIPAGE_SAVE to intercept page save 26e39ccd63SJonny Dee * operations. When a deletion (empty save) is attempted on a protected page 27e39ccd63SJonny Dee * by a non‑admin user, the save is prevented and an error message is shown. 28e39ccd63SJonny Dee */ 29*994617c9SJohann Duscherclass action_plugin_deletepageguard extends ActionPlugin { 30e39ccd63SJonny Dee 31e39ccd63SJonny Dee /** 32e39ccd63SJonny Dee * Register the plugin events 33e39ccd63SJonny Dee * 34e39ccd63SJonny Dee * @param EventHandler $controller 35e39ccd63SJonny Dee * @return void 36e39ccd63SJonny Dee */ 37e39ccd63SJonny Dee public function register(EventHandler $controller) { 38e39ccd63SJonny Dee // Run before the page is saved so we can abort the delete 39e39ccd63SJonny Dee $controller->register_hook('COMMON_WIKIPAGE_SAVE', 'BEFORE', $this, 'handle_common_wikipage_save'); 40e39ccd63SJonny Dee } 41e39ccd63SJonny Dee 42e39ccd63SJonny Dee /** 43e39ccd63SJonny Dee * Handler for the COMMON_WIKIPAGE_SAVE event 44e39ccd63SJonny Dee * 45e39ccd63SJonny Dee * This method checks whether the save operation represents a deletion 46e39ccd63SJonny Dee * (i.e. the new content is empty) and whether the page matches one of 47e39ccd63SJonny Dee * the configured regular expressions. If so, and the current user is 48e39ccd63SJonny Dee * neither an administrator nor in one of the exempt groups, the 49e39ccd63SJonny Dee * deletion is prevented. 50e39ccd63SJonny Dee * 51e39ccd63SJonny Dee * @param Event $event The event object 52e39ccd63SJonny Dee * @param mixed $param Additional parameters (unused) 53e39ccd63SJonny Dee * @return void 54e39ccd63SJonny Dee */ 55e39ccd63SJonny Dee public function handle_common_wikipage_save(Event $event, $param) { 56e39ccd63SJonny Dee global $USERINFO, $conf; 57e39ccd63SJonny Dee 58e39ccd63SJonny Dee // Only take action when the event is preventable 59e39ccd63SJonny Dee if (!$event->canPreventDefault) { 60e39ccd63SJonny Dee return; 61e39ccd63SJonny Dee } 62e39ccd63SJonny Dee 63e39ccd63SJonny Dee // Allow administrators to delete pages 64e39ccd63SJonny Dee if (function_exists('auth_isadmin') && auth_isadmin()) { 65e39ccd63SJonny Dee return; 66e39ccd63SJonny Dee } 67e39ccd63SJonny Dee 68e39ccd63SJonny Dee // Check for exempt groups configuration 69e39ccd63SJonny Dee $exemptSetting = (string)$this->getConf('exempt_groups'); 70e39ccd63SJonny Dee $exemptGroups = array_filter(array_map('trim', explode(',', $exemptSetting))); 71e39ccd63SJonny Dee 72e39ccd63SJonny Dee if (!empty($exemptGroups) && isset($USERINFO['grps']) && is_array($USERINFO['grps'])) { 73e39ccd63SJonny Dee foreach ($USERINFO['grps'] as $group) { 74e39ccd63SJonny Dee if (in_array($group, $exemptGroups, true)) { 75e39ccd63SJonny Dee // User is in an exempt group, allow deletion 76e39ccd63SJonny Dee return; 77e39ccd63SJonny Dee } 78e39ccd63SJonny Dee } 79e39ccd63SJonny Dee } 80e39ccd63SJonny Dee 81e39ccd63SJonny Dee // Determine if the save represents a deletion 82e39ccd63SJonny Dee $newContent = isset($event->data['newContent']) ? $event->data['newContent'] : ''; 83e39ccd63SJonny Dee $trimMode = (bool)$this->getConf('trim_mode'); 84e39ccd63SJonny Dee $isEmpty = $trimMode ? trim($newContent) === '' : $newContent === ''; 85e39ccd63SJonny Dee 86e39ccd63SJonny Dee if (!$isEmpty) { 87e39ccd63SJonny Dee // Not empty – normal edit, allow saving 88e39ccd63SJonny Dee return; 89e39ccd63SJonny Dee } 90e39ccd63SJonny Dee 91e39ccd63SJonny Dee // Determine the matching target: page ID or relative file path 92e39ccd63SJonny Dee $matchTarget = $this->getConf('match_target') === 'filepath' ? 93e39ccd63SJonny Dee $this->getRelativeFilePath($event->data['file'], $conf['datadir']) : 94e39ccd63SJonny Dee $event->data['id']; 95e39ccd63SJonny Dee 96e39ccd63SJonny Dee // Retrieve regex patterns from configuration 97e39ccd63SJonny Dee $patternsSetting = (string)$this->getConf('patterns'); 98e39ccd63SJonny Dee $patternLines = preg_split('/\R+/', $patternsSetting, -1, PREG_SPLIT_NO_EMPTY); 99e39ccd63SJonny Dee 100e39ccd63SJonny Dee foreach ($patternLines as $rawPattern) { 101e39ccd63SJonny Dee $pattern = trim($rawPattern); 102e39ccd63SJonny Dee if ($pattern === '') { 103e39ccd63SJonny Dee continue; 104e39ccd63SJonny Dee } 105e39ccd63SJonny Dee // Try to apply the regex; invalid patterns are ignored 106e39ccd63SJonny Dee if (@preg_match('/' . $pattern . '/u', '') === false) { 107e39ccd63SJonny Dee continue; 108e39ccd63SJonny Dee } 109e39ccd63SJonny Dee if (preg_match('/' . $pattern . '/u', $matchTarget)) { 110e39ccd63SJonny Dee // Match found – prevent deletion 111e39ccd63SJonny Dee $event->preventDefault(); 112e39ccd63SJonny Dee $event->stopPropagation(); 113e39ccd63SJonny Dee msg($this->getLang('deny_msg'), -1); 114e39ccd63SJonny Dee return; 115e39ccd63SJonny Dee } 116e39ccd63SJonny Dee } 117e39ccd63SJonny Dee } 118e39ccd63SJonny Dee 119e39ccd63SJonny Dee /** 120e39ccd63SJonny Dee * Convert an absolute file path into a relative one below the data directory 121e39ccd63SJonny Dee * 122e39ccd63SJonny Dee * The COMMON_WIKIPAGE_SAVE event provides the absolute file path. When 123e39ccd63SJonny Dee * matching against the file path, we want a path relative to the base 124e39ccd63SJonny Dee * pages directory so users can write concise regular expressions. 125e39ccd63SJonny Dee * 126e39ccd63SJonny Dee * @param string $fullPath Absolute path to the file 127e39ccd63SJonny Dee * @param string $dataDir Base data directory (usually $conf['datadir']) 128e39ccd63SJonny Dee * @return string Relative file path 129e39ccd63SJonny Dee */ 130e39ccd63SJonny Dee protected function getRelativeFilePath($fullPath, $dataDir) { 131e39ccd63SJonny Dee $base = rtrim($dataDir, '/'); 132e39ccd63SJonny Dee // DokuWiki stores pages in $datadir/pages 133e39ccd63SJonny Dee $pagesDir = $base . '/pages/'; 134e39ccd63SJonny Dee if (strpos($fullPath, $pagesDir) === 0) { 135e39ccd63SJonny Dee return substr($fullPath, strlen($pagesDir)); 136e39ccd63SJonny Dee } 137e39ccd63SJonny Dee // Fallback: attempt to strip base datadir 138e39ccd63SJonny Dee if (strpos($fullPath, $base . '/') === 0) { 139e39ccd63SJonny Dee return substr($fullPath, strlen($base) + 1); 140e39ccd63SJonny Dee } 141e39ccd63SJonny Dee return $fullPath; 142e39ccd63SJonny Dee } 143e39ccd63SJonny Dee}