]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | // Original from http://www.daniweb.com/code/snippet43.html | |
3 | ||
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"; | |
11 | ||
12 | $session_expire = min(2147483647 - time() - 1, max(SESSION_COOKIE_LIFETIME, 86400)); | |
13 | $session_name = (!defined('TTRSS_SESSION_NAME')) ? "ttrss_sid" : TTRSS_SESSION_NAME; | |
14 | ||
15 | if (is_server_https()) { | |
16 | $session_name .= "_ssl"; | |
17 | ini_set("session.cookie_secure", true); | |
18 | } | |
19 | ||
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)); | |
25 | ||
26 | function session_get_schema_version() { | |
27 | global $schema_version; | |
28 | ||
29 | if (!$schema_version) { | |
30 | $row = Db::pdo()->query("SELECT schema_version FROM ttrss_version")->fetch(); | |
31 | ||
32 | $version = $row["schema_version"]; | |
33 | ||
34 | $schema_version = $version; | |
35 | return $version; | |
36 | } else { | |
37 | return $schema_version; | |
38 | } | |
39 | } | |
40 | ||
41 | function validate_session() { | |
42 | if (SINGLE_USER_MODE) return true; | |
43 | ||
44 | if (isset($_SESSION["ref_schema_version"]) && $_SESSION["ref_schema_version"] != session_get_schema_version()) { | |
45 | $_SESSION["login_error_msg"] = | |
46 | __("Session failed to validate (schema version changed)"); | |
47 | return false; | |
48 | } | |
49 | $pdo = Db::pdo(); | |
50 | ||
51 | if ($_SESSION["uid"]) { | |
52 | $sth = $pdo->prepare("SELECT pwd_hash FROM ttrss_users WHERE id = ?"); | |
53 | $sth->execute([$_SESSION['uid']]); | |
54 | ||
55 | // user not found | |
56 | if ($row = $sth->fetch()) { | |
57 | $pwd_hash = $row["pwd_hash"]; | |
58 | ||
59 | if ($pwd_hash != $_SESSION["pwd_hash"]) { | |
60 | ||
61 | $_SESSION["login_error_msg"] = | |
62 | __("Session failed to validate (password changed)"); | |
63 | ||
64 | return false; | |
65 | } | |
66 | } else { | |
67 | ||
68 | $_SESSION["login_error_msg"] = | |
69 | __("Session failed to validate (user not found)"); | |
70 | ||
71 | return false; | |
72 | ||
73 | } | |
74 | } | |
75 | ||
76 | return true; | |
77 | } | |
78 | ||
79 | /** | |
80 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) | |
81 | */ | |
82 | function ttrss_open ($s, $n) { | |
83 | return true; | |
84 | } | |
85 | ||
86 | function ttrss_read ($id){ | |
87 | global $session_expire; | |
88 | ||
89 | $sth = Db::pdo()->prepare("SELECT data FROM ttrss_sessions WHERE id=?"); | |
90 | $sth->execute([$id]); | |
91 | ||
92 | if ($row = $sth->fetch()) { | |
93 | return base64_decode($row["data"]); | |
94 | ||
95 | } else { | |
96 | $expire = time() + $session_expire; | |
97 | ||
98 | $sth = Db::pdo()->prepare("INSERT INTO ttrss_sessions (id, data, expire) | |
99 | VALUES (?, '', ?)"); | |
100 | $sth->execute([$id, $expire]); | |
101 | ||
102 | return ""; | |
103 | ||
104 | } | |
105 | ||
106 | } | |
107 | ||
108 | function ttrss_write ($id, $data) { | |
109 | global $session_expire; | |
110 | ||
111 | $data = base64_encode($data); | |
112 | $expire = time() + $session_expire; | |
113 | ||
114 | $sth = Db::pdo()->prepare("UPDATE ttrss_sessions SET data=?, expire=? WHERE id=?"); | |
115 | $sth->execute([$data, $expire, $id]); | |
116 | ||
117 | return true; | |
118 | } | |
119 | ||
120 | function ttrss_close () { | |
121 | return true; | |
122 | } | |
123 | ||
124 | function ttrss_destroy($id) { | |
125 | $sth = Db::pdo()->prepare("DELETE FROM ttrss_sessions WHERE id = ?"); | |
126 | $sth->execute([$id]); | |
127 | ||
128 | return true; | |
129 | } | |
130 | ||
131 | /** | |
132 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) | |
133 | */ | |
134 | function ttrss_gc ($expire) { | |
135 | Db::pdo()->query("DELETE FROM ttrss_sessions WHERE expire < " . time()); | |
136 | ||
137 | return true; | |
138 | } | |
139 | ||
140 | if (!SINGLE_USER_MODE /* && DB_TYPE == "pgsql" */) { | |
141 | session_set_save_handler("ttrss_open", | |
142 | "ttrss_close", "ttrss_read", "ttrss_write", | |
143 | "ttrss_destroy", "ttrss_gc"); | |
144 | register_shutdown_function('session_write_close'); | |
145 | } | |
146 | ||
147 | if (!defined('NO_SESSION_AUTOSTART')) { | |
148 | if (isset($_COOKIE[session_name()])) { | |
149 | @session_start(); | |
150 | } | |
151 | } |