1 | <?php |
---|
2 | defined('ADMINTOOLS_PATH') or die('Hacking attempt!'); |
---|
3 | |
---|
4 | /** |
---|
5 | * Class managing multi views system |
---|
6 | */ |
---|
7 | class MultiView |
---|
8 | { |
---|
9 | /** @var bool $is_admin */ |
---|
10 | private $is_admin = false; |
---|
11 | |
---|
12 | /** @var array $data */ |
---|
13 | private $data = array(); |
---|
14 | private $data_url_params = array(); |
---|
15 | |
---|
16 | /** @var array $user */ |
---|
17 | private $user = array(); |
---|
18 | |
---|
19 | /** |
---|
20 | * Constructor, load $data from session |
---|
21 | */ |
---|
22 | function __construct() |
---|
23 | { |
---|
24 | global $conf; |
---|
25 | |
---|
26 | $this->data = array_merge( |
---|
27 | array( |
---|
28 | 'view_as' => 0, |
---|
29 | 'theme' => '', |
---|
30 | 'lang' => '', |
---|
31 | 'show_queries' => $conf['show_queries'], |
---|
32 | 'debug_l10n' => $conf['debug_l10n'], |
---|
33 | 'debug_template' => $conf['debug_template'], |
---|
34 | 'template_combine_files' => $conf['template_combine_files'], |
---|
35 | 'no_history' => false, |
---|
36 | ), |
---|
37 | pwg_get_session_var('multiview', array()) |
---|
38 | ); |
---|
39 | |
---|
40 | $this->data_url_params = array_keys($this->data); |
---|
41 | $this->data_url_params = array_map(create_function('$d', 'return "ato_".$d;'), $this->data_url_params); |
---|
42 | } |
---|
43 | |
---|
44 | /** |
---|
45 | * @return bool |
---|
46 | */ |
---|
47 | public function is_admin() |
---|
48 | { |
---|
49 | return $this->is_admin; |
---|
50 | } |
---|
51 | |
---|
52 | /** |
---|
53 | * @return array |
---|
54 | */ |
---|
55 | public function get_data() |
---|
56 | { |
---|
57 | return $this->data; |
---|
58 | } |
---|
59 | |
---|
60 | /** |
---|
61 | * @return array |
---|
62 | */ |
---|
63 | public function get_user() |
---|
64 | { |
---|
65 | return $this->user; |
---|
66 | } |
---|
67 | |
---|
68 | /** |
---|
69 | * Save $data in session |
---|
70 | */ |
---|
71 | private function save() |
---|
72 | { |
---|
73 | pwg_set_session_var('multiview', $this->data); |
---|
74 | } |
---|
75 | |
---|
76 | /** |
---|
77 | * Returns the current url minus MultiView params |
---|
78 | * |
---|
79 | * @param bool $with_amp - adds ? or & at the end of the url |
---|
80 | * @return string |
---|
81 | */ |
---|
82 | public function get_clean_url($with_amp=false) |
---|
83 | { |
---|
84 | if (script_basename() == 'picture') |
---|
85 | { |
---|
86 | $url = duplicate_picture_url(array(), $this->data_url_params); |
---|
87 | } |
---|
88 | else if (script_basename() == 'index') |
---|
89 | { |
---|
90 | $url = duplicate_index_url(array(), $this->data_url_params); |
---|
91 | } |
---|
92 | else |
---|
93 | { |
---|
94 | $url = get_query_string_diff($this->data_url_params); |
---|
95 | } |
---|
96 | |
---|
97 | if ($with_amp) |
---|
98 | { |
---|
99 | $url.= strpos($url, '?')!==false ? '&' : '?'; |
---|
100 | } |
---|
101 | |
---|
102 | return $url; |
---|
103 | } |
---|
104 | |
---|
105 | /** |
---|
106 | * Returns the current url minus MultiView params |
---|
107 | * |
---|
108 | * @param bool $with_amp - adds ? or & at the end of the url |
---|
109 | * @return string |
---|
110 | */ |
---|
111 | public function get_clean_admin_url($with_amp=false) |
---|
112 | { |
---|
113 | $url = PHPWG_ROOT_PATH.'admin.php'; |
---|
114 | |
---|
115 | $get = $_GET; |
---|
116 | unset($get['page'], $get['section'], $get['tag']); |
---|
117 | if (count($get) == 0 and !empty($_SERVER['QUERY_STRING'])) |
---|
118 | { |
---|
119 | $url.= '?' . str_replace('&', '&', $_SERVER['QUERY_STRING']); |
---|
120 | } |
---|
121 | |
---|
122 | if ($with_amp) |
---|
123 | { |
---|
124 | $url.= strpos($url, '?')!==false ? '&' : '?'; |
---|
125 | } |
---|
126 | |
---|
127 | return $url; |
---|
128 | } |
---|
129 | |
---|
130 | /** |
---|
131 | * Triggered on "user_init", change current view depending of URL params. |
---|
132 | */ |
---|
133 | public function user_init() |
---|
134 | { |
---|
135 | global $user, $conf; |
---|
136 | |
---|
137 | $this->is_admin = is_admin(); |
---|
138 | |
---|
139 | $this->user = array( |
---|
140 | 'id' => $user['id'], |
---|
141 | 'username' => $user['username'], |
---|
142 | 'language' => $user['language'], |
---|
143 | 'theme' => $user['theme'], |
---|
144 | ); |
---|
145 | |
---|
146 | // inactive on ws.php to allow AJAX admin tasks |
---|
147 | if ($this->is_admin && script_basename() != 'ws') |
---|
148 | { |
---|
149 | // show_queries |
---|
150 | if (isset($_GET['ato_show_queries'])) |
---|
151 | { |
---|
152 | $this->data['show_queries'] = (bool)$_GET['ato_show_queries']; |
---|
153 | } |
---|
154 | $conf['show_queries'] = $this->data['show_queries']; |
---|
155 | |
---|
156 | if ($this->data['view_as'] == 0) |
---|
157 | { |
---|
158 | $this->data['view_as'] = $user['id']; |
---|
159 | } |
---|
160 | if (empty($this->data['lang'])) |
---|
161 | { |
---|
162 | $this->data['lang'] = $user['language']; |
---|
163 | } |
---|
164 | if (empty($this->data['theme'])) |
---|
165 | { |
---|
166 | $this->data['theme'] = $user['theme']; |
---|
167 | } |
---|
168 | |
---|
169 | // view_as |
---|
170 | if (!defined('IN_ADMIN')) |
---|
171 | { |
---|
172 | if (isset($_GET['ato_view_as'])) |
---|
173 | { |
---|
174 | $this->data['view_as'] = (int)$_GET['ato_view_as']; |
---|
175 | } |
---|
176 | if ($this->data['view_as'] != $user['id']) |
---|
177 | { |
---|
178 | $user = build_user($this->data['view_as'], true); |
---|
179 | if (isset($_GET['ato_view_as'])) |
---|
180 | { |
---|
181 | $this->data['theme'] = $user['theme']; |
---|
182 | $this->data['lang'] = $user['language']; |
---|
183 | } |
---|
184 | } |
---|
185 | } |
---|
186 | |
---|
187 | // theme |
---|
188 | if (isset($_GET['ato_theme'])) |
---|
189 | { |
---|
190 | $this->data['theme'] = $_GET['ato_theme']; |
---|
191 | } |
---|
192 | $user['theme'] = $this->data['theme']; |
---|
193 | |
---|
194 | // lang |
---|
195 | if (isset($_GET['ato_lang'])) |
---|
196 | { |
---|
197 | $this->data['lang'] = $_GET['ato_lang']; |
---|
198 | } |
---|
199 | $user['language'] = $this->data['lang']; |
---|
200 | |
---|
201 | // debug_l10n |
---|
202 | if (isset($_GET['ato_debug_l10n'])) |
---|
203 | { |
---|
204 | $this->data['debug_l10n'] = (bool)$_GET['ato_debug_l10n']; |
---|
205 | } |
---|
206 | $conf['debug_l10n'] = $this->data['debug_l10n']; |
---|
207 | |
---|
208 | // debug_template |
---|
209 | if (isset($_GET['ato_debug_template'])) |
---|
210 | { |
---|
211 | $this->data['debug_template'] = (bool)$_GET['ato_debug_template']; |
---|
212 | } |
---|
213 | $conf['debug_template'] = $this->data['debug_template']; |
---|
214 | |
---|
215 | // template_combine_files |
---|
216 | if (isset($_GET['ato_template_combine_files'])) |
---|
217 | { |
---|
218 | $this->data['template_combine_files'] = (bool)$_GET['ato_template_combine_files']; |
---|
219 | } |
---|
220 | $conf['template_combine_files'] = $this->data['template_combine_files']; |
---|
221 | |
---|
222 | // no_history |
---|
223 | if (isset($_GET['ato_no_history'])) |
---|
224 | { |
---|
225 | $this->data['no_history'] = (bool)$_GET['ato_no_history']; |
---|
226 | } |
---|
227 | if ($this->data['no_history']) |
---|
228 | { |
---|
229 | add_event_handler('pwg_log_allowed', create_function('', 'return false;')); |
---|
230 | } |
---|
231 | |
---|
232 | $this->save(); |
---|
233 | } |
---|
234 | } |
---|
235 | |
---|
236 | /** |
---|
237 | * Returns the language of the current user if different from the current language |
---|
238 | * false otherwise |
---|
239 | */ |
---|
240 | function get_user_language() |
---|
241 | { |
---|
242 | if (isset($this->user['language']) && isset($this->data['lang']) |
---|
243 | && $this->user['language'] != $this->data['lang'] |
---|
244 | ) |
---|
245 | { |
---|
246 | return $this->user['language']; |
---|
247 | } |
---|
248 | return false; |
---|
249 | } |
---|
250 | |
---|
251 | /** |
---|
252 | * Triggered on "init", in order to clean template files (not initialized on "user_init") |
---|
253 | */ |
---|
254 | public function init() |
---|
255 | { |
---|
256 | if ($this->is_admin) |
---|
257 | { |
---|
258 | if (isset($_GET['ato_purge_template'])) |
---|
259 | { |
---|
260 | global $template; |
---|
261 | $template->delete_compiled_templates(); |
---|
262 | FileCombiner::clear_combined_files(); |
---|
263 | } |
---|
264 | } |
---|
265 | } |
---|
266 | |
---|
267 | /** |
---|
268 | * Mark browser session cache for deletion |
---|
269 | */ |
---|
270 | public static function invalidate_cache() |
---|
271 | { |
---|
272 | global $conf; |
---|
273 | conf_update_param('multiview_invalidate_cache', true, true); |
---|
274 | } |
---|
275 | |
---|
276 | /** |
---|
277 | * Register custom API methods |
---|
278 | */ |
---|
279 | public static function register_ws($arr) |
---|
280 | { |
---|
281 | $service = &$arr[0]; |
---|
282 | |
---|
283 | $service->addMethod( |
---|
284 | 'multiView.getData', |
---|
285 | array('MultiView', 'ws_get_data'), |
---|
286 | array(), |
---|
287 | 'AdminTools private method.', |
---|
288 | null, |
---|
289 | array('admin_only' => true, 'hidden' => true) |
---|
290 | ); |
---|
291 | } |
---|
292 | |
---|
293 | /** |
---|
294 | * API method |
---|
295 | * Return full list of users, themes and languages |
---|
296 | */ |
---|
297 | public static function ws_get_data($params) |
---|
298 | { |
---|
299 | global $conf; |
---|
300 | |
---|
301 | // get users |
---|
302 | $query = ' |
---|
303 | SELECT |
---|
304 | '.$conf['user_fields']['id'].' AS id, |
---|
305 | '.$conf['user_fields']['username'].' AS username, |
---|
306 | status |
---|
307 | FROM '.USERS_TABLE.' AS u |
---|
308 | INNER JOIN '.USER_INFOS_TABLE.' AS i |
---|
309 | ON '.$conf['user_fields']['id'].' = user_id |
---|
310 | ORDER BY CONVERT('.$conf['user_fields']['username'].', CHAR) |
---|
311 | ;'; |
---|
312 | $out['users'] = array_from_query($query); |
---|
313 | |
---|
314 | // get themes |
---|
315 | include_once(PHPWG_ROOT_PATH.'admin/include/themes.class.php'); |
---|
316 | $themes = new themes(); |
---|
317 | foreach (array_keys($themes->db_themes_by_id) as $theme) |
---|
318 | { |
---|
319 | if (!empty($theme)) |
---|
320 | { |
---|
321 | $out['themes'][] = $theme; |
---|
322 | } |
---|
323 | } |
---|
324 | |
---|
325 | // get languages |
---|
326 | foreach (get_languages() as $code => $name) |
---|
327 | { |
---|
328 | $out['languages'][] = array( |
---|
329 | 'id' => $code, |
---|
330 | 'name' => $name, |
---|
331 | ); |
---|
332 | } |
---|
333 | |
---|
334 | conf_delete_param('multiview_invalidate_cache'); |
---|
335 | |
---|
336 | return $out; |
---|
337 | } |
---|
338 | } |
---|