Project

General

Profile

Download (2.27 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * phpsessionmanager.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2016-2020 Rubicon Communications, LLC (Netgate)
7
 * All rights reserved.
8
 *
9
 * Licensed under the Apache License, Version 2.0 (the "License");
10
 * you may not use this file except in compliance with the License.
11
 * You may obtain a copy of the License at
12
 *
13
 * http://www.apache.org/licenses/LICENSE-2.0
14
 *
15
 * Unless required by applicable law or agreed to in writing, software
16
 * distributed under the License is distributed on an "AS IS" BASIS,
17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
 * See the License for the specific language governing permissions and
19
 * limitations under the License.
20
 */
21

    
22
$session_opencounter = 0;
23
$session_write = false;
24
$session_action_list = array();
25

    
26
function simplestacktrace() {
27
	$stack = debug_backtrace();
28
	$str = "";
29
	foreach($stack as $s) {
30
		// $s['args']
31
		$str .= "\n{$s['function']}(..) - {$s['file']}:{$s['line']}";
32
	}
33
	return $str;
34
}
35

    
36
function phpsession_begin() {
37
	global $session_opencounter, $session_action_list;
38
	$session_action_list[] = "#### phpsession_begin ####" . simplestacktrace();
39
	if ($session_opencounter == 0) {
40
		session_start();
41
	}
42
	$session_opencounter++;
43
}
44

    
45
function phpsession_destroy() {
46
	global $session_opencounter, $session_action_list;
47
	$session_action_list[] = "#### phpsession_destroy ####" . simplestacktrace();
48
	session_destroy();
49
	$session_opencounter = 0;
50
}
51

    
52
function phpsession_end($write = false) {
53
	global $session_opencounter, $session_write, $session_action_list;
54
	$session_action_list[] = "#### phpsession_end ####" . simplestacktrace();
55
	$session_write |= $write;
56
	$session_opencounter--;
57
	if ($session_opencounter == 0) {
58
		if ($session_write) {
59
			session_commit();
60
			$session_write = false;
61
		} else {
62
			session_abort();
63
		}
64
	}
65
	if ($session_opencounter < 0) {
66
		$session_opencounter = 0;
67
		syslog(LOG_WARNING, "PHPSESSION closed more often than opened!" . simplestacktrace());
68
	}
69
}
70

    
71
function phpsession_cleanupcheck() {
72
	global $session_opencounter, $session_action_list;
73
	if ($session_opencounter > 0) {
74
		syslog(LOG_WARNING, "PHPSESSION {$session_opencounter} open sessions left at shutdown script!".print_r($session_action_list, true));
75
	}
76
}
77

    
78
register_shutdown_function('phpsession_cleanupcheck');
(39-39/60)