<?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; 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 new
            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

        uses: 328footer-plus-end.html
        last modified: 2024-04-14
    -----*/

    function hum_conn_sess()
    {
        // get part of the username from where this is installed

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

        // but the Oracle account you can log into is your username plus
        //    _php

        $ora_php_username = "{$os_username}_php";

        // but to ask to use blah_php's password to log in as blah,
        // we need to express it in the form BLAH_PHP[BLAH]

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

        // grab the password from this user's .oraauth

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

        // oci_connect expects a username,
        //    then a password,
        //    then a connection string (can be null in this particular approach,
        //                              so PHP can build it from env variables),
        //    then the desired character encoding (we'll use "AL32UTF8"),
        //    then the desired session mode (we'll use the default, the constant
        //                                   OCI_DEFAULT)

        $connectn = oci_connect($conn_username, $ora_php_password, null,
                               "AL32UTF8", OCI_DEFAULT);

        if (! $connectn)
        {
            ?>
            <p> Could not log into Oracle, sorry! </p>
            <p> <a 
                href="<?= htmlentities($_SERVER['PHP_SELF'], ENT_QUOTES) ?>">
                Try again </a> 
            </p>
            <?php
            require_once("328footer-plus-end.html");
            session_destroy();
            exit;
        }
    
        // if reach here, I connected!

        return $connectn;
    }
?>