Commit | Line | Data |
---|---|---|
b62f8858 | 1 | <?php |
2 | /*************************************************************************** | |
2ab75571 | 3 | * Copyright (C) 2003-2010 Polytechnique.org * |
b62f8858 | 4 | * http://opensource.polytechnique.org/ * |
5 | * * | |
6 | * This program is free software; you can redistribute it and/or modify * | |
7 | * it under the terms of the GNU General Public License as published by * | |
8 | * the Free Software Foundation; either version 2 of the License, or * | |
9 | * (at your option) any later version. * | |
10 | * * | |
11 | * This program is distributed in the hope that it will be useful, * | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | |
14 | * GNU General Public License for more details. * | |
15 | * * | |
16 | * You should have received a copy of the GNU General Public License * | |
17 | * along with this program; if not, write to the Free Software * | |
18 | * Foundation, Inc., * | |
19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * | |
20 | ***************************************************************************/ | |
21 | ||
88a4c51b FB |
22 | class S |
23 | { | |
24 | /** Set a constructor because this is called prior to S::s(), so we can | |
25 | * define S::s() for other usages. | |
26 | */ | |
27 | private function __construct() | |
28 | { | |
29 | assert(false); | |
30 | } | |
31 | ||
32 | public static function has($key) | |
33 | { | |
34 | return isset($_SESSION[$key]); | |
35 | } | |
36 | ||
37 | public static function kill($key) | |
38 | { | |
39 | unset($_SESSION[$key]); | |
40 | } | |
41 | ||
42 | public static function v($key, $default = null) | |
43 | { | |
44 | return isset($_SESSION[$key]) ? $_SESSION[$key] : $default; | |
45 | } | |
46 | ||
47 | public static function s($key, $default = '') | |
48 | { | |
49 | return (string)S::v($key, $default); | |
50 | } | |
51 | ||
7adcbe0e FB |
52 | public static function b($key, $default = false) |
53 | { | |
54 | return (bool)S::v($key, $default); | |
55 | } | |
56 | ||
88a4c51b FB |
57 | public static function i($key, $default = 0) |
58 | { | |
59 | $i = S::v($key, $default); | |
60 | return is_numeric($i) ? intval($i) : $default; | |
61 | } | |
62 | ||
f1c8bb75 FB |
63 | public static function t($key, $default = '') |
64 | { | |
65 | return trim(S::s($key, $default)); | |
66 | } | |
67 | ||
68 | public static function blank($key, $strict = false) | |
69 | { | |
70 | if (!S::has($key)) { | |
71 | return true; | |
72 | } | |
73 | $var = $strict ? S::s($key) : S::t($key); | |
74 | return empty($var); | |
75 | } | |
76 | ||
88a4c51b FB |
77 | public static function l(array $keys) |
78 | { | |
79 | return array_map(array('S', 'v'), $keys); | |
80 | } | |
81 | ||
c0799142 | 82 | public static function set($key, $value) |
88a4c51b FB |
83 | { |
84 | $_SESSION[$key] =& $value; | |
85 | } | |
86 | ||
c0799142 | 87 | public static function bootstrap($key, $value) |
88a4c51b FB |
88 | { |
89 | if (!S::has($key)) { | |
90 | S::set($key, $value); | |
91 | } | |
92 | } | |
93 | ||
47fa97fe | 94 | public static function logger($uid = null) |
c0799142 | 95 | { |
7c8d7022 FB |
96 | $uid = S::i('uid', $uid); |
97 | if (!S::has('log') || !S::v('log')->isValid($uid)) { | |
faeb823b | 98 | global $platal; |
c0799142 FB |
99 | if (S::has('suid')) { |
100 | $suid = S::v('suid'); | |
faeb823b | 101 | S::set('log', $platal->buildLogger(S::i('uid', $uid), $suid['uid'])); |
732e5855 | 102 | } else if (S::has('uid') || $uid) { |
faeb823b | 103 | S::set('log', $platal->buildLogger(S::i('uid', $uid))); |
7c8d7022 FB |
104 | } else { |
105 | S::set('log', PlLogger::dummy($uid)); | |
c0799142 FB |
106 | } |
107 | } | |
108 | return S::v('log'); | |
109 | } | |
110 | ||
c12e9211 VZ |
111 | /** User object storage and accessor. The user object (an instance of the |
112 | * local subclass of PlUser) is currently stored as a S class variable, and | |
113 | * not as a session variable, so as to avoid bloating the global on-disk | |
114 | * session. | |
115 | * TODO: When all the codebase will use S::user() as the only source for | |
116 | * user ids, fullname/displayname, and forlife/bestalias, S::$user should | |
117 | * move into the php session (and data it helds should be removed from | |
118 | * the php session). */ | |
119 | private static $user = null; | |
120 | public static function &user() | |
121 | { | |
be2abdd1 | 122 | if (self::$user == null && class_exists('User')) { |
324d1fa0 FB |
123 | if (S::has('user') && S::v('user') instanceof User) { |
124 | self::$user = S::v('user'); | |
125 | } else { | |
126 | self::$user = User::getSilentWithValues(S::i('uid'), $_SESSION); | |
127 | } | |
c12e9211 VZ |
128 | } |
129 | return self::$user; | |
130 | } | |
131 | ||
88a4c51b FB |
132 | public static function logged() |
133 | { | |
8cd8f58b | 134 | return S::i('auth', AUTH_PUBLIC) >= Platal::session()->loggedLevel(); |
88a4c51b FB |
135 | } |
136 | ||
137 | public static function identified() | |
138 | { | |
8cd8f58b | 139 | return S::i('auth', AUTH_PUBLIC) >= Platal::session()->sureLevel(); |
88a4c51b FB |
140 | } |
141 | ||
f1c8bb75 FB |
142 | public static function admin() |
143 | { | |
144 | return Platal::session()->checkPerms(PERMS_ADMIN); | |
145 | } | |
146 | ||
147 | public static function suid($field = null, $default = null) | |
148 | { | |
149 | if (is_null($field)) { | |
150 | return !S::blank('suid'); | |
151 | } else { | |
152 | $suid = S::v('suid', array()); | |
153 | if (!empty($suid) && isset($suid[$field])) { | |
154 | return $suid[$field]; | |
155 | } else { | |
156 | return $default; | |
157 | } | |
158 | } | |
159 | } | |
160 | ||
88a4c51b FB |
161 | // Anti-XSRF protections. |
162 | public static function has_xsrf_token() | |
163 | { | |
164 | return S::has('xsrf_token') && S::v('xsrf_token') == Env::v('token'); | |
165 | } | |
166 | ||
167 | public static function assert_xsrf_token() | |
168 | { | |
169 | if (!S::has_xsrf_token()) { | |
170 | Platal::page()->kill('L\'opération n\'a pas pu aboutir, merci de réessayer.'); | |
171 | } | |
172 | } | |
173 | ||
f1c8bb75 FB |
174 | public static function hasAuthToken() |
175 | { | |
176 | return !S::blank('token'); | |
177 | } | |
178 | ||
88a4c51b FB |
179 | public static function rssActivated() |
180 | { | |
f1c8bb75 | 181 | // XXX: Deprecated, to be replaced by S::hasToken() |
88a4c51b FB |
182 | return S::has('core_rss_hash') && S::v('core_rss_hash'); |
183 | } | |
6995a9b9 | 184 | } |
b62f8858 | 185 | |
a7de4ef7 | 186 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
b62f8858 | 187 | ?> |