ICanBoogie/ICanBoogie v2.3.1
  • Namespace
  • Class

Namespaces

  • ICanBoogie
    • Autoconfig
    • Core
    • HTTP
      • Dispatcher
    • Session

Classes

  • Core
  • Debug
  • Helpers
  • Logger
  • LogLevel
  • Session

Interfaces

  • LoggerInterface

Traits

  • LoggerTrait

Exceptions

  • AlreadyAuthenticated
  • AuthenticationRequired
  • CoreAlreadyBooted
  • CoreAlreadyInstantiated
  • CoreNotInstantiated
  • PermissionRequired
  • SecurityException

Constants

  • TOKEN_ALPHA
  • TOKEN_ALPHA_UPCASE
  • TOKEN_NUMERIC
  • TOKEN_SYMBOL
  • TOKEN_SYMBOL_WIDE

Functions

  • app
  • boot
  • excerpt
  • generate_token
  • generate_token_wide
  • get_autoconfig
  • log
  • log_error
  • log_info
  • log_success
  • log_time
  • normalize_namespace_part
  • pbkdf2
  • resolve_app_paths
  • strip_root
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 
<?php

/*
 * This file is part of the ICanBoogie package.
 *
 * (c) Olivier Laviale <olivier.laviale@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace ICanBoogie;

/**
 * Session.
 *
 * @property string $remote_agent_hash The remote user agent hash of the request that created the
 * session.
 * @property Logger $icanboogie_logger
 * @property string $token A token that can be used to prevent cross-site request forgeries.
 */
class Session
{
    static public $defaults = [

        'id' => null,
        'name' => 'ICanBoogie',
        'domain' => null,
        'use_cookies' => true,
        'use_only_cookies' => true,
        'use_trans_sid' => false,
        'cache_limiter' => null,
        'module_name' => 'files'

    ];

    /**
     * Checks if a session identifier can be found to retrieve a session.
     *
     * @return bool true if the session identifier exists in the cookie, false otherwise.
     */
    static public function exists()
    {
        return !empty($_COOKIE[app()->config['session']['name']]);
    }

    /**
     * Returns a Session instance.
     *
     * The session is initialized when the session object is created.
     *
     * Once the session is created the `start` event is fired with the session as sender.
     *
     * @param Core $app
     *
     * @return Session
     */
    static function get_session(Core $app)
    {
        $options = $app->config['session'];

        unset($options['id']);

        return new static($options);
    }

    /**
     * Constructor.
     *
     * In order to circumvent session fixation and session hijacking, the user agent hash is
     * attached to the session. A previous session can only be restored if the
     * user agent hash match.
     *
     * The session is destroyed when the values don't match and the "location" header is set to
     * request a reload.
     *
     * @param array $options
     */
    public function __construct(array $options = [])
    {
        if (session_id())
        {
            return;
        }

        $options = $this->prepare_options($options);

        $this->apply_options($options);

        if (PHP_SAPI != 'cli')
        {
            session_start();
            $this->check_fixation($options);
        }

        new Session\StartEvent($this);
    }

    /**
     * Prepare session options.
     *
     * @param array $options
     *
     * @return array
     */
    protected function prepare_options(array $options)
    {
        return $options + self::$defaults + session_get_cookie_params();
    }

    /**
     * Applies options.
     *
     * @param array $options
     */
    protected function apply_options(array $options)
    {
        $id = $options['id'];

        if ($id)
        {
            session_id($id);
        }

        session_name($options['name']);
        session_set_cookie_params($options['lifetime'], $options['path'], $options['domain'], $options['secure'], $options['httponly']);

        if ($options['cache_limiter'] !== null)
        {
            session_cache_limiter($options['cache_limiter']);
        }

        if ($options['module_name'] != session_module_name())
        {
            session_module_name($options['module_name']);
        }

        $this->apply_use_trans_id($options['use_trans_sid']);
    }

    /**
     * Applies `session.use_trans_sid`.
     *
     * @param $use_trans_sid
     */
    protected function apply_use_trans_id($use_trans_sid)
    {
        if (ini_get('session.use_trans_sid') == $use_trans_sid)
        {
            return;
        }

        ini_set('session.use_trans_sid', $use_trans_sid);

        if ($use_trans_sid)
        {
            output_add_rewrite_var(session_name(), session_id());
        }
        else
        {
            output_reset_rewrite_vars();
        }
    }

    /**
     * We do what we can to prevent session fixation.
     *
     * @param array $options
     */
    private function check_fixation(array $options)
    {
        $remote_agent_hash = isset($_SERVER['HTTP_USER_AGENT']) ? md5($_SERVER['HTTP_USER_AGENT']) : null;

        if (empty($this->remote_agent_hash))
        {
            $this->remote_agent_hash = $remote_agent_hash;
            $this->regenerate_token();
        }
        else if ($this->remote_agent_hash != $remote_agent_hash)
        {
            session_destroy();

            header('Location: ' . $_SERVER['REQUEST_URI']);

            if ($options['use_cookies'])
            {
                setcookie(session_name(), '', time() - 42000, $options['path'], $options['domain'], $options['secure'], $options['httponly']);
            }

            exit;
        }
    }

    /**
     * Regenerates the id of the session.
     *
     * @param bool $delete_old_session
     *
     * @return bool|null `true` when the id is regenerated, `false` when it is not, `null` when
     * the application is running from CLI.
     */
    public function regenerate_id($delete_old_session=false)
    {
        if (PHP_SAPI == 'cli')
        {
            return null;
        }

        return session_regenerate_id($delete_old_session);
    }

    /**
     * Regenerates the session token.
     *
     * The `token_time` property is updated to the current time.
     *
     * @return string The new session token.
     */
    public function regenerate_token()
    {
        $_SESSION['token'] = $token = md5(uniqid());
        $_SESSION['token_time'] = microtime(true);

        return $token;
    }

    public function &__get($property)
    {
        return $_SESSION[$property];
    }

    public function __set($property, $value)
    {
        $_SESSION[$property] = $value;
    }

    public function __isset($property)
    {
        return isset($_SESSION, $property);
    }

    public function __unset($property)
    {
        unset($_SESSION[$property]);
    }
}
ICanBoogie/ICanBoogie v2.3.1 API documentation generated by ApiGen