1 | <?php |
---|
2 | defined('ADMINTOOLS_ID') 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; |
---|
11 | |
---|
12 | /** @var array $data */ |
---|
13 | private $data = array(); |
---|
14 | |
---|
15 | /** |
---|
16 | * Constructor, load $data from session |
---|
17 | */ |
---|
18 | function __construct() |
---|
19 | { |
---|
20 | global $conf; |
---|
21 | |
---|
22 | $this->data = array_merge( |
---|
23 | array( |
---|
24 | 'view_as' => 0, |
---|
25 | 'theme' => '', |
---|
26 | 'lang' => '', |
---|
27 | 'show_queries' => $conf['show_queries'], |
---|
28 | 'debug_l10n' => $conf['debug_l10n'], |
---|
29 | 'debug_template' => $conf['debug_template'], |
---|
30 | 'template_combine_files' => $conf['template_combine_files'], |
---|
31 | 'no_history' => false, |
---|
32 | ), |
---|
33 | pwg_get_session_var('multiview', array()) |
---|
34 | ); |
---|
35 | } |
---|
36 | |
---|
37 | /** |
---|
38 | * @return bool |
---|
39 | */ |
---|
40 | public function is_admin() |
---|
41 | { |
---|
42 | return $this->is_admin; |
---|
43 | } |
---|
44 | |
---|
45 | /** |
---|
46 | * @return array |
---|
47 | */ |
---|
48 | public function get_data() |
---|
49 | { |
---|
50 | return $this->data; |
---|
51 | } |
---|
52 | |
---|
53 | /** |
---|
54 | * Save $data in session |
---|
55 | */ |
---|
56 | private function save() |
---|
57 | { |
---|
58 | pwg_set_session_var('multiview', $this->data); |
---|
59 | } |
---|
60 | |
---|
61 | /** |
---|
62 | * Returns the current url minus MultiView params |
---|
63 | * |
---|
64 | * @param bool $with_amp - adds ? or & at the end of the url |
---|
65 | * @return string |
---|
66 | */ |
---|
67 | public function get_clean_url($with_amp=false) |
---|
68 | { |
---|
69 | if (script_basename() == 'picture') |
---|
70 | { |
---|
71 | $url = duplicate_picture_url(array(), array_keys($this->data)); |
---|
72 | } |
---|
73 | else if (script_basename() == 'index') |
---|
74 | { |
---|
75 | $url = duplicate_index_url(array(), array_keys($this->data)); |
---|
76 | } |
---|
77 | else |
---|
78 | { |
---|
79 | $url = get_query_string_diff(array_keys($this->data)); |
---|
80 | } |
---|
81 | |
---|
82 | if ($with_amp) |
---|
83 | { |
---|
84 | $url.= strpos($url, '?')!==false ? '&' : '?'; |
---|
85 | } |
---|
86 | |
---|
87 | return $url; |
---|
88 | } |
---|
89 | |
---|
90 | /** |
---|
91 | * Triggered on "user_init", change current view depending of URL params. |
---|
92 | */ |
---|
93 | public function user_init() |
---|
94 | { |
---|
95 | $this->is_admin = is_admin(); |
---|
96 | |
---|
97 | if ($this->is_admin) |
---|
98 | { |
---|
99 | global $user, $conf; |
---|
100 | |
---|
101 | // view_as |
---|
102 | if (isset($_GET['view_as'])) |
---|
103 | { |
---|
104 | $this->data['view_as'] = (int)$_GET['view_as']; |
---|
105 | } |
---|
106 | if ($this->data['view_as'] != $user['id'] and $this->data['view_as'] != 0) |
---|
107 | { |
---|
108 | $user = build_user($this->data['view_as'], true); |
---|
109 | $this->data['theme'] = $user['theme']; |
---|
110 | $this->data['lang'] = $user['language']; |
---|
111 | } |
---|
112 | |
---|
113 | // theme |
---|
114 | if (isset($_GET['theme'])) |
---|
115 | { |
---|
116 | $this->data['theme'] = $_GET['theme']; |
---|
117 | } |
---|
118 | if ($this->data['theme'] != '') |
---|
119 | { |
---|
120 | $user['theme'] = $this->data['theme']; |
---|
121 | } |
---|
122 | |
---|
123 | // lang |
---|
124 | if (isset($_GET['lang'])) |
---|
125 | { |
---|
126 | $this->data['lang'] = $_GET['lang']; |
---|
127 | } |
---|
128 | if ($this->data['lang'] != '') |
---|
129 | { |
---|
130 | $user['language'] = $this->data['lang']; |
---|
131 | } |
---|
132 | |
---|
133 | // show_queries |
---|
134 | if (isset($_GET['show_queries'])) |
---|
135 | { |
---|
136 | $this->data['show_queries'] = (bool)$_GET['show_queries']; |
---|
137 | } |
---|
138 | $conf['show_queries'] = $this->data['show_queries']; |
---|
139 | |
---|
140 | // debug_l10n |
---|
141 | if (isset($_GET['debug_l10n'])) |
---|
142 | { |
---|
143 | $this->data['debug_l10n'] = (bool)$_GET['debug_l10n']; |
---|
144 | } |
---|
145 | $conf['debug_l10n'] = $this->data['debug_l10n']; |
---|
146 | |
---|
147 | // debug_template |
---|
148 | if (isset($_GET['debug_template'])) |
---|
149 | { |
---|
150 | $this->data['debug_template'] = (bool)$_GET['debug_template']; |
---|
151 | } |
---|
152 | $conf['debug_template'] = $this->data['debug_template']; |
---|
153 | |
---|
154 | // template_combine_files |
---|
155 | if (isset($_GET['template_combine_files'])) |
---|
156 | { |
---|
157 | $this->data['template_combine_files'] = (bool)$_GET['template_combine_files']; |
---|
158 | } |
---|
159 | $conf['template_combine_files'] = $this->data['template_combine_files']; |
---|
160 | |
---|
161 | // no_history |
---|
162 | if (isset($_GET['no_history'])) |
---|
163 | { |
---|
164 | $this->data['no_history'] = (bool)$_GET['no_history']; |
---|
165 | } |
---|
166 | if ($this->data['no_history']) |
---|
167 | { |
---|
168 | add_event_handler('pwg_log_allowed', create_function('', 'return false;')); |
---|
169 | } |
---|
170 | |
---|
171 | $this->save(); |
---|
172 | } |
---|
173 | } |
---|
174 | |
---|
175 | /** |
---|
176 | * Triggered on "init", in order to clean template files (not initialized on "user_init") |
---|
177 | */ |
---|
178 | public function init() |
---|
179 | { |
---|
180 | if (isset($_GET['purge_template'])) |
---|
181 | { |
---|
182 | global $template; |
---|
183 | $template->delete_compiled_templates(); |
---|
184 | FileCombiner::clear_combined_files(); |
---|
185 | } |
---|
186 | } |
---|
187 | } |
---|