<commit_before><?php

namespace Slim\Middleware;

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

/**
 * Session middleware
 *
 * This class is meant to provide a easy way to manage sessions with framework,
 * using the PHP built-in (native) sessions but also allowing to manipulate the
 * session variables via same app instance, by registering a container to the
 * helper class that ships with this package. As a plus, you can set a lifetime
 * for a session and it will be updated after each user activity or interaction
 * like an 'autorefresh' feature.
 *
 * Keep in mind this relies on PHP native sessions, so for this to work you
 * must have that enabled and correctly working.
 *
 * @package Slim\Middleware
 * @author  <NAME>
 */
class Session
{
    /**
     * @var array
     */
    protected $settings;

    /**
     * Constructor
     *
     * @param array $settings
     */
    public function __construct($settings = [])
    {
        $defaults = [
            'lifetime'    => '20 minutes',
            'path'        => '/',
            'domain'      => null,
            'secure'      => false,
            'httponly'    => false,
            'name'        => 'slim_session',
            'autorefresh' => false,
        ];
        $settings = array_merge($defaults, $settings);

        if (is_string($lifetime = $settings['lifetime'])) {
            $settings['lifetime'] = strtotime($lifetime) - time();
        }
        $this->settings = $settings;

        ini_set('session.gc_probability', 1);
        ini_set('session.gc_divisor', 1);
        ini_set('session.gc_maxlifetime', 30 * 24 * 60 * 60);
    }

    /**
     * Called when middleware needs to be executed.
     *
     * @param \Psr\Http\Message\ServerRequestInterface $request  PSR7 request
     * @param \Psr\Http\Message\ResponseInterface      $response PSR7 response
     * @param callable                                 $next     Next middleware
     *
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function __invoke(Request $request, Response $response, callable $next)
    {
        $this->startSession();

        return $next($request, $response);
    }

    /**
     * Start session
     */
    protected function startSession()
    {
        $settings = $this->settings;
        $name = $settings['name'];

        session_set_cookie_params(
            $settings['lifetime'],
            $settings['path'],
            $settings['domain'],
            $settings['secure'],
            $settings['httponly']
        );


	$inactive = session_status() === PHP_SESSION_NONE; // ignore PHP_SESSION_DISABLED 
	if ($inactive) {
		// when using $active = session_status() === PHP_SESSION_ACTIVE; 
		// this line never called, so the lifetime never ascending
    		if ($settings['autorefresh'] && isset($_COOKIE[$name])) {
	        	setcookie(
            			$name,
            			$_COOKIE[$name],
        	    		time() + $settings['lifetime'],
        	    		$settings['path'],
				$settings['domain'],
        	    		$settings['secure'],
		            	$settings['httponly']
        		);
    		}
	}
	session_name($name);
	session_cache_limiter(false);
	if ($inactive) {
	    session_start();
	}




    }
}
<commit_msg>fix session_status to inactive<commit_after><?php

namespace Slim\Middleware;

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

/**
 * Session middleware
 *
 * This class is meant to provide a easy way to manage sessions with framework,
 * using the PHP built-in (native) sessions but also allowing to manipulate the
 * session variables via same app instance, by registering a container to the
 * helper class that ships with this package. As a plus, you can set a lifetime
 * for a session and it will be updated after each user activity or interaction
 * like an 'autorefresh' feature.
 *
 * Keep in mind this relies on PHP native sessions, so for this to work you
 * must have that enabled and correctly working.
 *
 * @package Slim\Middleware
 * @author  <NAME>
 */
class Session
{
    /**
     * @var array
     */
    protected $settings;

    /**
     * Constructor
     *
     * @param array $settings
     */
    public function __construct($settings = [])
    {
        $defaults = [
            'lifetime'    => '20 minutes',
            'path'        => '/',
            'domain'      => null,
            'secure'      => false,
            'httponly'    => false,
            'name'        => 'slim_session',
            'autorefresh' => false,
        ];
        $settings = array_merge($defaults, $settings);

        if (is_string($lifetime = $settings['lifetime'])) {
            $settings['lifetime'] = strtotime($lifetime) - time();
        }
        $this->settings = $settings;

        ini_set('session.gc_probability', 1);
        ini_set('session.gc_divisor', 1);
        ini_set('session.gc_maxlifetime', 30 * 24 * 60 * 60);
    }

    /**
     * Called when middleware needs to be executed.
     *
     * @param \Psr\Http\Message\ServerRequestInterface $request  PSR7 request
     * @param \Psr\Http\Message\ResponseInterface      $response PSR7 response
     * @param callable                                 $next     Next middleware
     *
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function __invoke(Request $request, Response $response, callable $next)
    {
        $this->startSession();

        return $next($request, $response);
    }

    /**
     * Start session
     */
    protected function startSession()
    {
        $settings = $this->settings;
        $name = $settings['name'];

        session_set_cookie_params(
            $settings['lifetime'],
            $settings['path'],
            $settings['domain'],
            $settings['secure'],
            $settings['httponly']
        );

	$inactive = session_status() === PHP_SESSION_NONE; // ignore PHP_SESSION_DISABLED 
	if ($inactive) {
		// when using $active = session_status() === PHP_SESSION_ACTIVE; 
		// this line never called, so the lifetime never ascending
    		if ($settings['autorefresh'] && isset($_COOKIE[$name])) {
	        	setcookie(
            			$name,
            			$_COOKIE[$name],
        	    		time() + $settings['lifetime'],
        	    		$settings['path'],
				$settings['domain'],
        	    		$settings['secure'],
		            	$settings['httponly']
        		);
    		}
	}
	session_name($name);
	session_cache_limiter(false);
	if ($inactive) {
	    session_start();
	}
    }
}

{'fraction_non_alphanumeric': 0.12269009391093608, 'fraction_numerical': 0.004241139048773099, 'mean_word_length': 2.1638715860086246, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1859546', 'n_tokens_mistral': 1942, 'n_tokens_neox': 1857, 'n_words': 497}