2 // Original from http://www.daniweb.com/code/snippet43.html
4 require_once "config.php";
5 require_once "classes/db.php";
6 require_once "autoload.php";
7 require_once "errorhandler.php";
8 require_once "lib/accept-to-gettext.php";
9 require_once "lib/gettext/gettext.inc";
10 require_once "version.php";
12 $session_expire = max(SESSION_COOKIE_LIFETIME, 86400);
13 $session_name = (!defined('TTRSS_SESSION_NAME')) ? "ttrss_sid" : TTRSS_SESSION_NAME;
15 if (@$_SERVER['HTTPS'] == "on") {
16 $session_name .= "_ssl";
17 ini_set("session.cookie_secure", true);
20 ini_set("session.gc_probability", 75);
21 ini_set("session.name", $session_name);
22 ini_set("session.use_only_cookies", true);
23 ini_set("session.gc_maxlifetime", $session_expire);
24 ini_set("session.cookie_lifetime", min(0, SESSION_COOKIE_LIFETIME));
26 function session_get_schema_version($nocache = false) {
27 global $schema_version;
29 if (!$schema_version) {
30 $result = Db::get()->query("SELECT schema_version FROM ttrss_version");
31 $version = Db::get()->fetch_result($result, 0, "schema_version");
32 $schema_version = $version;
35 return $schema_version;
39 function validate_session() {
40 if (SINGLE_USER_MODE) return true;
42 if (VERSION_STATIC != $_SESSION["version"]) return false;
44 $check_ip = $_SESSION['ip_address'];
46 switch (SESSION_CHECK_ADDRESS) {
51 $check_ip = substr($check_ip, 0, strrpos($check_ip, '.')+1);
54 $check_ip = substr($check_ip, 0, strrpos($check_ip, '.'));
55 $check_ip = substr($check_ip, 0, strrpos($check_ip, '.')+1);
59 if ($check_ip && strpos($_SERVER['REMOTE_ADDR'], $check_ip) !== 0) {
60 $_SESSION["login_error_msg"] =
61 __("Session failed to validate (incorrect IP)");
65 if ($_SESSION["ref_schema_version"] != session_get_schema_version(true)) {
66 $_SESSION["login_error_msg"] =
67 __("Session failed to validate (schema version changed)");
71 if (sha1($_SERVER['HTTP_USER_AGENT']) != $_SESSION["user_agent"]) {
72 $_SESSION["login_error_msg"] =
73 __("Session failed to validate (user agent changed)");
77 if ($_SESSION["uid"]) {
78 $result = Db::get()->query(
79 "SELECT pwd_hash FROM ttrss_users WHERE id = '".$_SESSION["uid"]."'");
82 if (Db::get()->num_rows($result) == 0) {
84 $_SESSION["login_error_msg"] =
85 __("Session failed to validate (user not found)");
89 $pwd_hash = Db::get()->fetch_result($result, 0, "pwd_hash");
91 if ($pwd_hash != $_SESSION["pwd_hash"]) {
93 $_SESSION["login_error_msg"] =
94 __("Session failed to validate (password changed)");
105 function ttrss_open ($s, $n) {
109 function ttrss_read ($id){
110 global $session_expire;
112 $res = Db::get()->query("SELECT data FROM ttrss_sessions WHERE id='$id'");
114 if (Db::get()->num_rows($res) != 1) {
116 $expire = time() + $session_expire;
118 Db::get()->query("INSERT INTO ttrss_sessions (id, data, expire)
119 VALUES ('$id', '', '$expire')");
123 return base64_decode(Db::get()->fetch_result($res, 0, "data"));
128 function ttrss_write ($id, $data) {
129 global $session_expire;
131 $data = base64_encode($data);
132 $expire = time() + $session_expire;
134 Db::get()->query("UPDATE ttrss_sessions SET data='$data', expire='$expire' WHERE id='$id'");
139 function ttrss_close () {
143 function ttrss_destroy($id) {
144 Db::get()->query("DELETE FROM ttrss_sessions WHERE id = '$id'");
149 function ttrss_gc ($expire) {
150 Db::get()->query("DELETE FROM ttrss_sessions WHERE expire < " . time());
153 if (!SINGLE_USER_MODE /* && DB_TYPE == "pgsql" */) {
154 session_set_save_handler("ttrss_open",
155 "ttrss_close", "ttrss_read", "ttrss_write",
156 "ttrss_destroy", "ttrss_gc");
157 register_shutdown_function('session_write_close');
160 if (!defined('NO_SESSION_AUTOSTART')) {
161 if (isset($_COOKIE[session_name()])) {