DOKU_SESSION_LIFETIME,
        'path' => DOKU_SESSION_PATH,
        'domain' => DOKU_SESSION_DOMAIN,
        'secure' => ($conf['securecookie'] && \dokuwiki\Ip::isSsl()),
        'httponly' => true,
        'samesite' => 'Lax',
    ]);
    // make sure the session cookie contains a valid session ID
    if (isset($_COOKIE[DOKU_SESSION_NAME]) && !preg_match('/^[-,a-zA-Z0-9]{22,256}$/', $_COOKIE[DOKU_SESSION_NAME])) {
        unset($_COOKIE[DOKU_SESSION_NAME]);
    }
    session_start();
}
/**
 * Checks paths from config file
 */
function init_paths()
{
    global $conf;
    $paths = [
        'datadir'   => 'pages',
        'olddir'    => 'attic',
        'mediadir'  => 'media',
        'mediaolddir' => 'media_attic',
        'metadir'   => 'meta',
        'mediametadir' => 'media_meta',
        'cachedir'  => 'cache',
        'indexdir'  => 'index',
        'lockdir'   => 'locks',
        'tmpdir'    => 'tmp',
        'logdir'    => 'log',
    ];
    foreach ($paths as $c => $p) {
        $path = empty($conf[$c]) ? $conf['savedir'] . '/' . $p : $conf[$c];
        $conf[$c] = init_path($path);
        if (empty($conf[$c])) {
            $path = fullpath($path);
            nice_die("The $c ('$p') at $path is not found, isn't accessible or writable.
                You should check your config and permission settings.
                Or maybe you want to run the
                installer?");
        }
    }
    // path to old changelog only needed for upgrading
    $conf['changelog_old'] = init_path(
        $conf['changelog'] ?? $conf['savedir'] . '/changes.log'
    );
    if ($conf['changelog_old'] == '') {
        unset($conf['changelog_old']);
    }
    // hardcoded changelog because it is now a cache that lives in meta
    $conf['changelog'] = $conf['metadir'] . '/_dokuwiki.changes';
    $conf['media_changelog'] = $conf['metadir'] . '/_media.changes';
}
/**
 * Load the language strings
 *
 * @param string $langCode language code, as passed by event handler
 */
function init_lang($langCode)
{
    //prepare language array
    global $lang, $config_cascade;
    $lang = [];
    //load the language files
    require(DOKU_INC . 'inc/lang/en/lang.php');
    foreach ($config_cascade['lang']['core'] as $config_file) {
        if (file_exists($config_file . 'en/lang.php')) {
            include($config_file . 'en/lang.php');
        }
    }
    if ($langCode && $langCode != 'en') {
        if (file_exists(DOKU_INC . "inc/lang/$langCode/lang.php")) {
            require(DOKU_INC . "inc/lang/$langCode/lang.php");
        }
        foreach ($config_cascade['lang']['core'] as $config_file) {
            if (file_exists($config_file . "$langCode/lang.php")) {
                include($config_file . "$langCode/lang.php");
            }
        }
    }
}
/**
 * Checks the existence of certain files and creates them if missing.
 */
function init_files()
{
    global $conf;
    $files = [$conf['indexdir'] . '/page.idx'];
    foreach ($files as $file) {
        if (!file_exists($file)) {
            $fh = @fopen($file, 'a');
            if ($fh) {
                fclose($fh);
                if ($conf['fperm']) chmod($file, $conf['fperm']);
            } else {
                nice_die("$file is not writable. Check your permissions settings!");
            }
        }
    }
}
/**
 * Returns absolute path
 *
 * This tries the given path first, then checks in DOKU_INC.
 * Check for accessibility on directories as well.
 *
 * @author Andreas Gohr 
 *
 * @param string $path
 *
 * @return bool|string
 */
function init_path($path)
{
    // check existence
    $p = fullpath($path);
    if (!file_exists($p)) {
        $p = fullpath(DOKU_INC . $path);
        if (!file_exists($p)) {
            return '';
        }
    }
    // check writability
    if (!@is_writable($p)) {
        return '';
    }
    // check accessability (execute bit) for directories
    if (@is_dir($p) && !file_exists("$p/.")) {
        return '';
    }
    return $p;
}
/**
 * Sets the internal config values fperm and dperm which, when set,
 * will be used to change the permission of a newly created dir or
 * file with chmod. Considers the influence of the system's umask
 * setting the values only if needed.
 */
function init_creationmodes()
{
    global $conf;
    // Legacy support for old umask/dmask scheme
    unset($conf['dmask']);
    unset($conf['fmask']);
    unset($conf['umask']);
    $conf['fperm'] = false;
    $conf['dperm'] = false;
    // get system umask, fallback to 0 if none available
    $umask = @umask();
    if (!$umask) $umask = 0000;
    // check what is set automatically by the system on file creation
    // and set the fperm param if it's not what we want
    $auto_fmode = 0666 & ~$umask;
    if ($auto_fmode != $conf['fmode']) $conf['fperm'] = $conf['fmode'];
    // check what is set automatically by the system on directory creation
    // and set the dperm param if it's not what we want.
    $auto_dmode = 0777 & ~$umask;
    if ($auto_dmode != $conf['dmode']) $conf['dperm'] = $conf['dmode'];
}
/**
 * Returns the full absolute URL to the directory where
 * DokuWiki is installed in (includes a trailing slash)
 *
 * !! Can not access $_SERVER values through $INPUT
 * !! here as this function is called before $INPUT is
 * !! initialized.
 *
 * @author Andreas Gohr 
 *
 * @param null|bool $abs Return an absolute URL? (null defaults to $conf['canonical'])
 *
 * @return string
 */
function getBaseURL($abs = null)
{
    global $conf;
    $abs ??= $conf['canonical'];
    if (!empty($conf['basedir'])) {
        $dir = $conf['basedir'];
    } elseif (substr($_SERVER['SCRIPT_NAME'], -4) == '.php') {
        $dir = dirname($_SERVER['SCRIPT_NAME']);
    } elseif (substr($_SERVER['PHP_SELF'], -4) == '.php') {
        $dir = dirname($_SERVER['PHP_SELF']);
    } elseif ($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']) {
        $dir = preg_replace(
            '/^' . preg_quote($_SERVER['DOCUMENT_ROOT'], '/') . '/',
            '',
            $_SERVER['SCRIPT_FILENAME']
        );
        $dir = dirname('/' . $dir);
    } else {
        $dir = ''; //probably wrong, but we assume it's in the root
    }
    $dir = str_replace('\\', '/', $dir);             // bugfix for weird WIN behaviour
    $dir = preg_replace('#//+#', '/', "/$dir/");     // ensure leading and trailing slashes
    //handle script in lib/exe dir
    $dir = preg_replace('!lib/exe/$!', '', $dir);
    //handle script in lib/plugins dir
    $dir = preg_replace('!lib/plugins/.*$!', '', $dir);
    //finish here for relative URLs
    if (!$abs) return $dir;
    //use config if available, trim any slash from end of baseurl to avoid multiple consecutive slashes in the path
    if (!empty($conf['baseurl'])) return rtrim($conf['baseurl'], '/') . $dir;
    //split hostheader into host and port
    $hostname = \dokuwiki\Ip::hostName();
    $parsed_host = parse_url('http://' . $hostname);
    $host = $parsed_host['host'] ?? '';
    $port = $parsed_host['port'] ?? '';
    if (!\dokuwiki\Ip::isSsl()) {
        $proto = 'http://';
        if ($port == '80') {
            $port = '';
        }
    } else {
        $proto = 'https://';
        if ($port == '443') {
            $port = '';
        }
    }
    if ($port !== '') $port = ':' . $port;
    return $proto . $host . $port . $dir;
}
/**
 * @deprecated 2025-06-03
 */
function is_ssl()
{
    dbg_deprecated('Ip::isSsl()');
    return \dokuwiki\Ip::isSsl();
}
/**
 * checks it is windows OS
 * @return bool
 */
function isWindows()
{
    return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
}
/**
 * print a nice message even if no styles are loaded yet.
 *
 * @param integer|string $msg
 */
function nice_die($msg)
{
    echo<<
DokuWiki Setup Error
    
        DokuWiki Setup Error
        $msg
     
EOT;
    if (defined('DOKU_UNITTEST')) {
        throw new RuntimeException('nice_die: ' . $msg);
    }
    exit(1);
}
/**
 * A realpath() replacement
 *
 * This function behaves similar to PHP's realpath() but does not resolve
 * symlinks or accesses upper directories
 *
 * @author Andreas Gohr 
 * @author 
 * @link   http://php.net/manual/en/function.realpath.php#75992
 *
 * @param string $path
 * @param bool $exists
 *
 * @return bool|string
 */
function fullpath($path, $exists = false)
{
    static $run = 0;
    $root  = '';
    $iswin = (isWindows() || !empty($GLOBALS['DOKU_UNITTEST_ASSUME_WINDOWS']));
    // find the (indestructable) root of the path - keeps windows stuff intact
    if ($path[0] == '/') {
        $root = '/';
    } elseif ($iswin) {
        // match drive letter and UNC paths
        if (preg_match('!^([a-zA-z]:)(.*)!', $path, $match)) {
            $root = $match[1] . '/';
            $path = $match[2];
        } elseif (preg_match('!^(\\\\\\\\[^\\\\/]+\\\\[^\\\\/]+[\\\\/])(.*)!', $path, $match)) {
            $root = $match[1];
            $path = $match[2];
        }
    }
    $path = str_replace('\\', '/', $path);
    // if the given path wasn't absolute already, prepend the script path and retry
    if (!$root) {
        $base = dirname($_SERVER['SCRIPT_FILENAME']);
        $path = $base . '/' . $path;
        if ($run == 0) { // avoid endless recursion when base isn't absolute for some reason
            $run++;
            return fullpath($path, $exists);
        }
    }
    $run = 0;
    // canonicalize
    $path = explode('/', $path);
    $newpath = [];
    foreach ($path as $p) {
        if ($p === '' || $p === '.') continue;
        if ($p === '..') {
            array_pop($newpath);
            continue;
        }
        $newpath[] = $p;
    }
    $finalpath = $root . implode('/', $newpath);
    // check for existence when needed (except when unit testing)
    if ($exists && !defined('DOKU_UNITTEST') && !file_exists($finalpath)) {
        return false;
    }
    return $finalpath;
}