<?php

/*
 * Title                   : DOT Framework
 * File                    : framework/includes/class-session.php
 * Author                  : Dot on Paper
 * Copyright               : © 2016 Dot on Paper
 * Website                 : https://dotonpaper.net
 * Description             : Session PHP class.
 */

if (!class_exists('DOTSession')){
    class DOTSession{
        /*
         * Constructor
         *
         * @usage
         *      The constructor is called when a class instance is created.
         *
         * @params
         *      -
         *
         * @post
         *      -
         *
         * @get
         *      -
         *
         * @sessions
         *      -
         *
         * @cookies
         *      -
         *
         * @constants
         *      -
         *
         * @globals
         *      -
         *
         * @functions
         *      -
         *
         * @hooks
         *      -
         *
         * @layouts
         *      -
         *
         * @return
         *      -
         *
         * @return_details
         *      -
         *
         * @dv
         *      -
         *
         * @tests
         *      -
         */
        function __construct(){
        }

        /*
         * Secure the session using a cookie.
         *
         * @usage
         *      In FILE search for function call: $this->secure
         *      In FILE search for function call in hooks: array(&$this, 'secure')
         *      In PROJECT search for function call: $DOT->classes->session->secure
         *
         * @params
         *      -
         *
         * @post
         *      -
         *
         * @get
         *      -
         *
         * @sessions
         *      -
         *
         * @cookies
         *      -
         *
         * @constants
         *      DOT_SESSION_COOKIE (string): session cookie name
         *      DOT_SESSION_COOKIE_SECURE (boolean): session cookie security
         *      DOT_SESSION_COOKIE_HTTP (boolean): session cookie security
         *
         * @globals
         *      DOT (object): DOT framework main class variable
         *
         * @functions
         *      -
         *
         * @hooks
         *      -
         *
         * @layouts
         *      -
         *
         * @return
         *      A cookie for sessions is created.
         *
         * @return_details
         *      The lifetime of the cookie is 1 week (604800 seconds).
         *
         * @dv
         *      -
         *
         * @tests
         *      -
         */
        function secure(){
            global $DOT;

            /*
             * Set session parameters.
             */
            $session_name = DOT_SESSION_COOKIE;
            $secure = DOT_SESSION_COOKIE_SECURE;
            $http_only = DOT_SESSION_COOKIE_HTTP;

            if ($session_name != ''){
                /*
                 * Set session name.
                 */
                session_name($session_name);

                /*
                 * Configure to save the sessions in a cookie.
                 */
                if (ini_set('session.use_only_cookies',
                            1) === false){
                    exit;
                }

                /*
                 * Get cookie arguments from php.ini
                 */
                $cookie_args = session_get_cookie_params();

                /*
                 * Set session cookie.
                 */
                session_set_cookie_params($cookie_args['lifetime'],
                                          $cookie_args['path'],
                                          $cookie_args['domain'],
                                          $secure,
                                          $http_only);
            }

            /*
             * Start sessions.
             */
            session_start();

            /*
             * Don't regenerate session ID if an AJAX call is made.
             */
            if ($DOT->post(DOT_AJAX_VAR) === false
                    && $session_name != ''){
                session_regenerate_id(true);
            }
        }

        /*
         * Get a session variable.
         *
         * @usage
         *      In FILE search for function call: $this->get
         *      In FILE search for function call in hooks: array(&$this, 'get')
         *      In PROJECT search for function call: $DOT->classes->session->get
         *
         * @params
         *      name (string): session name
         *
         * @post
         *      -
         *
         * @get
         *      -
         *
         * @sessions
         *      -
         *
         * @cookies
         *      -
         *
         * @constants
         *      -
         *
         * @globals
         *      -
         *
         * @functions
         *      -
         *
         * @hooks
         *      -
         *
         * @layouts
         *      -
         *
         * @return
         *      Session value or "false" if the session does not exist.
         *
         * @return_details
         *      -
         *
         * @dv
         *      -
         *
         * @tests
         *      -
         */
        /**
         * @param string $name
         *
         * @return mixed|false
         */
        public function get($name){
            return $_SESSION[$name] ?? false;
        }

        /*
         * Set a session variable.
         *
         * @usage
         *      In FILE search for function call: $this->set
         *      In FILE search for function call in hooks: array(&$this, 'set')
         *      In PROJECT search for function call: $DOT->classes->session->set
         *
         * @params
         *      name (string): session name
         *      value (string): session value
         *
         * @post
         *      -
         *
         * @get
         *      -
         *
         * @sessions
         *      -
         *
         * @cookies
         *      -
         *
         * @constants
         *      -
         *
         * @globals
         *      -
         *
         * @functions
         *      -
         *
         * @hooks
         *      -
         *
         * @layouts
         *      -
         *
         * @return
         *      Return true after the session is set.
         *
         * @return_details
         * -
         *
         * @dv
         *      -
         *
         * @tests
         *      -
         */
        /**
         * @param string $name
         * @param string $value
         *
         * @return boolean
         */
        public function set($name,
                            $value){
            $_SESSION[$name] = $value;

            return true;
        }
    }
}