<?php
    /*-----
        VARIATION of hum_conn_no_login for when SESSIONS
            are involved, AND when failure to connect
            implies that the current session should
            be DESTROYED;

        ALSO: since this is a variation anyway, think I
            will try assuming this is being used by a
            postback PHP and including
            a "Try again" link to the calling PHP if connection
            fails; IS this a good idea or not?
    -----*/

/*-----
    function: hum_conn_sess: void -> connection
    purpose: expects nothing,
        
        has the side-effect of trying to connect to
        Humboldt's Oracle student database using the current
        no-end-user-login approach (logging in based on where
        the PHP file resides),
            
        and, if successful, returns the resulting connection object,

        but, if NOT successful:
        *   complains, including a "try again" link to the
            calling document,
        *   ENDS the document
        *   destroys the current session, and
        *   exits the current PHP document

    original by: Peter Johnson, Troy Butolph (I think)
    adapted by:  Sharon Tuttle
    last modified: 2026-04-22
-----*/

function hum_conn_sess()
{
    /*===
        using the user directory context information
        to extract the owner-of-this-PHP's username
    ===*/

    $os_username = substr($_SERVER["CONTEXT_PREFIX"], 2);

    /*===
        get this user's Oracle account name, which ends in _php
    ===*/

    $ora_php_username = "{$os_username}_php";

    /*===
        to ask to use blah_php's password to log in as blah,
        it needs to be in the form BLAH_PHP[BLAH]
    ===*/

    $conn_username = "{$ora_php_username}[{$os_username}]";

    /*===
        how to get the needed password
    ===*/

    $ora_php_password =
        trim(file_get_contents("/home/{$os_username}/.oraauth"));

    /*===
        we are using a connection string of null
        to rely on admin-set-up environmental variables,

        an encoding of 'AL32UTF8' to "[specify] the character 
        set of the database to prevent any need for negotiation", 

        and a session mode whose value is the 
        PHP constant OCI_DEFAULT
     ===*/

    $connectn = oci_connect(username: $conn_username,
                            password: $ora_php_password,
                            connection_string: null,
                            encoding: 'AL32UTF8',
                            session_mode: OCI_DEFAULT);

    /*===
        complain and exit at least somewhat gracefully if
            if connection failed,
        now also including destroying the current session
            and adding a (hopefully-useful...?) "Try again" link
            to the calling PHP
    ===*/
    
    if (! $connectn)
    {
        ?>
        <p> Could not log into Oracle, sorry. </p>

        <p> <a href="<?= htmlentities($_SERVER['PHP_SELF'], ENT_QUOTES) ?>">
            Try again </a> </p>

    <footer>
    <hr />
    <p>
        Validate by pasting .xhtml copy's URL into<br />
	<a href="https://validator.w3.org/nu">
            https://validator.w3.org/nu
        </a>
	or  
        <a href="https://html5.validator.nu/">
            https://html5.validator.nu/
        </a>
    </p>
    </footer>
</body>
</html>
        <?php

        /*===
	    and in this connection-failed case,
            DESTROY the current session, and
	    EXIT this PHP document HERE and NOW,
	    reasonable when you hit an error such that there
	    is NO POINT in going any further
        ===*/
 
        session_destroy();
        exit;
    }

    /*===
        HOWEVER -- IF reach here, connection succeeded!
    ===*/

    return $connectn;   
}

?>