<commit_before>xquery version "1.0";

import module namespace security="http://exist-db.org/mods/security" at "security.xqm";

declare namespace request="http://exist-db.org/xquery/request";
declare namespace session="http://exist-db.org/xquery/session";

declare function local:logout() {
    let $logout := request:get-parameter("logout", ())
    return
        if ($logout) then
            session:clear()
        else
            ()
};

declare function local:set-user() {
    session:create(),
    local:logout(),
    let $user := request:get-parameter("user", ())
    let $password := request:get-parameter("password", ())
    let $session-user-credential := security:get-user-credential-from-session()
    return
        if ($user) then (
            security:store-user-credential-in-session($user, $password),
            <set-attribute name="xquery.user" value="{$user}"/>,
            <set-attribute name="xquery.password" value="{$password}"/>
        ) else if ($session-user-credential != '') then (
            <set-attribute name="xquery.user" value="{$session-user-credential[1]}"/>,
            <set-attribute name="xquery.password" value="{$session-user-credential[2]}"/>
        ) else (
            <set-attribute name="xquery.user" value="{$security:GUEST_CREDENTIALS[1]}"/>,
            <set-attribute name="xquery.password" value="{$security:GUEST_CREDENTIALS[2]}"/>
        )
};

if ($exist:path eq '/') then
    <dispatch xmlns="http://exist.sourceforge.net/NS/exist">
		<redirect url="index.xml"/>
	</dispatch>
(:  Main page: index.xml is a template, which is passed through
    search.xql and the db2xhtml stylesheet. search.xql will run
    the actual search and expand the index.xml template.
:)
else if ($exist:resource eq 'index.xml') then
    <dispatch xmlns="http://exist.sourceforge.net/NS/exist">
        { local:set-user() }
        <view>
            <forward url="search.xql">
                <!-- Errors should be passed through instead of terminating the request -->
        		<set-attribute name="xquery.report-errors" value="yes"/>
            </forward>
		</view>
	</dispatch>
(:  Retrieve an item from the query results stored in the HTTP session. The
	format of the URL will be /sandbox/results/X, where X is the number of the
	item in the result set :)
else if ($exist:resource eq 'retrieve') then
	<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
	   { local:set-user() }
		<forward url="{$exist:controller}/session.xql">
		</forward>
	</dispatch>
else if (matches($exist:path, '(resources/|styles/syntax|scripts/|logo.jpg|default-style2.css|curvycorners.js)')) then
    let $newPath := replace($exist:path, '^.*((styles/|scripts/|logo).*)$', '/$1')
    return
        <dispatch xmlns="http://exist.sourceforge.net/NS/exist">
    		<forward url="{$newPath}"/>
    		<cache-control cache="yes"/>
    	</dispatch>
else
    (: everything else is passed through :)
    <dispatch xmlns="http://exist.sourceforge.net/NS/exist">
        { local:set-user() }
        <cache-control cache="yes"/>
    </dispatch><commit_msg>At logout do a redirect back to the home page - otherwise the jquery tree does not seem to be correctly reloaded<commit_after>xquery version "1.0";

import module namespace security="http://exist-db.org/mods/security" at "security.xqm";

declare namespace request="http://exist-db.org/xquery/request";
declare namespace session="http://exist-db.org/xquery/session";

declare function local:set-user() {
    session:create(),
    let $user := request:get-parameter("user", ())
    let $password := request:get-parameter("password", ())
    let $session-user-credential := security:get-user-credential-from-session()
    return
        if ($user) then (
            security:store-user-credential-in-session($user, $password),
            <set-attribute name="xquery.user" value="{$user}"/>,
            <set-attribute name="xquery.password" value="{$password}"/>
        ) else if ($session-user-credential != '') then (
            <set-attribute name="xquery.user" value="{$session-user-credential[1]}"/>,
            <set-attribute name="xquery.password" value="{$session-user-credential[2]}"/>
        ) else (
            <set-attribute name="xquery.user" value="{$security:GUEST_CREDENTIALS[1]}"/>,
            <set-attribute name="xquery.password" value="{$security:GUEST_CREDENTIALS[2]}"/>
        )
};

if ($exist:path eq '/') then
    <dispatch xmlns="http://exist.sourceforge.net/NS/exist">
		<redirect url="index.xml"/>
	</dispatch>
(:  Main page: index.xml is a template, which is passed through
    search.xql and the db2xhtml stylesheet. search.xql will run
    the actual search and expand the index.xml template.
:)
else if ($exist:resource eq 'index.xml') then

    if(request:get-parameter("logout",()))then
    (
        session:clear(),
        <dispatch xmlns="http://exist.sourceforge.net/NS/exist">
            <redirect url="index.xml"/>
        </dispatch>
    )
    else
    (
        <dispatch xmlns="http://exist.sourceforge.net/NS/exist">
            { local:set-user() }
            <view>
                <forward url="search.xql">
                    <!-- Errors should be passed through instead of terminating the request -->
            		<set-attribute name="xquery.report-errors" value="yes"/>
                </forward>
    		</view>
    	</dispatch>
        (:  Retrieve an item from the query results stored in the HTTP session. The
    	format of the URL will be /sandbox/results/X, where X is the number of the
    	item in the result set :)
	)
else if ($exist:resource eq 'retrieve') then
	<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
	   { local:set-user() }
		<forward url="{$exist:controller}/session.xql">
		</forward>
	</dispatch>
else if (matches($exist:path, '(resources/|styles/syntax|scripts/|logo.jpg|default-style2.css|curvycorners.js)')) then
    let $newPath := replace($exist:path, '^.*((styles/|scripts/|logo).*)$', '/$1')
    return
        <dispatch xmlns="http://exist.sourceforge.net/NS/exist">
    		<forward url="{$newPath}"/>
    		<cache-control cache="yes"/>
    	</dispatch>
else
    (: everything else is passed through :)
    <dispatch xmlns="http://exist.sourceforge.net/NS/exist">
        { local:set-user() }
        <cache-control cache="yes"/>
    </dispatch>
{'fraction_non_alphanumeric': 0.1655849295551686, 'fraction_numerical': 0.0028494538546778533, 'mean_word_length': 3.042226487523992, 'pattern_counts': {'":': 0, '<': 62, '<?xml version=': 0, '>': 62, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 20}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4933831', 'n_tokens_mistral': 1954, 'n_tokens_neox': 1898, 'n_words': 430}