aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/AdminTools/include/MultiView.class.php
blob: 889ba457ff1112d2fe3a6e006eaa0974407fde3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
<?php
defined('ADMINTOOLS_PATH') or die('Hacking attempt!');

/**
 * Class managing multi views system
 */
class MultiView
{
  /** @var bool $is_admin */
  private $is_admin = false;

  /** @var array $data */
  private $data = array();
  private $data_url_params = array();

  /** @var array $user */
  private $user = array();

  /**
   * Constructor, load $data from session
   */
  function __construct()
  {
    global $conf;

    $this->data = array_merge(
      array(
        'view_as' => 0,
        'theme' => '',
        'lang' => '',
        'show_queries' => $conf['show_queries'],
        'debug_l10n' => $conf['debug_l10n'],
        'debug_template' => $conf['debug_template'],
        'template_combine_files' => $conf['template_combine_files'],
        'no_history' => false,
        ),
      pwg_get_session_var('multiview', array())
      );

    $this->data_url_params = array_keys($this->data);
    $this->data_url_params = array_map(create_function('$d', 'return "ato_".$d;'), $this->data_url_params);
  }

  /**
   * @return bool
   */
  public function is_admin()
  {
    return $this->is_admin;
  }

  /**
   * @return array
   */
  public function get_data()
  {
    return $this->data;
  }

  /**
   * @return array
   */
  public function get_user()
  {
    return $this->user;
  }

  /**
   * Save $data in session
   */
  private function save()
  {
    pwg_set_session_var('multiview', $this->data);
  }

  /**
   * Returns the current url minus MultiView params
   *
   * @param bool $with_amp - adds ? or & at the end of the url
   * @return string
   */
  public function get_clean_url($with_amp=false)
  {
    if (script_basename() == 'picture')
    {
      $url = duplicate_picture_url(array(), $this->data_url_params);
    }
    else if (script_basename() == 'index')
    {
      $url = duplicate_index_url(array(), $this->data_url_params);
    }
    else
    {
      $url = get_query_string_diff($this->data_url_params);
    }

    if ($with_amp)
    {
      $url.= strpos($url, '?')!==false ? '&' : '?';
    }

    return $url;
  }

  /**
   * Triggered on "user_init", change current view depending of URL params.
   */
  public function user_init()
  {
    global $user, $conf;

    $this->is_admin = is_admin();

    $this->user = array(
      'id' => $user['id'],
      'username' => $user['username'],
      'language' => $user['language'],
      'theme' => $user['theme'],
      );

    // inactive on ws.php to allow AJAX admin tasks
    if ($this->is_admin && script_basename() != 'ws')
    {
      // show_queries
      if (isset($_GET['ato_show_queries']))
      {
        $this->data['show_queries'] = (bool)$_GET['ato_show_queries'];
      }
      $conf['show_queries'] = $this->data['show_queries'];

      if ($this->data['view_as'] == 0)
      {
        $this->data['view_as'] = $user['id'];
      }
      if (empty($this->data['lang']))
      {
        $this->data['lang'] = $user['language'];
      }
      if (empty($this->data['theme']))
      {
        $this->data['theme'] = $user['theme'];
      }

      // view_as
      if (!defined('IN_ADMIN'))
      {
        if (isset($_GET['ato_view_as']))
        {
          $this->data['view_as'] = (int)$_GET['ato_view_as'];
        }
        if ($this->data['view_as'] != $user['id'])
        {
          $user = build_user($this->data['view_as'], true);
          if (isset($_GET['ato_view_as']))
          {
            $this->data['theme'] = $user['theme'];
            $this->data['lang'] = $user['language'];
          }
        }
      }

      // theme
      if (isset($_GET['ato_theme']))
      {
        $this->data['theme'] = $_GET['ato_theme'];
      }
      $user['theme'] = $this->data['theme'];

      // lang
      if (isset($_GET['ato_lang']))
      {
        $this->data['lang'] = $_GET['ato_lang'];
      }
      $user['language'] = $this->data['lang'];

      // debug_l10n
      if (isset($_GET['ato_debug_l10n']))
      {
        $this->data['debug_l10n'] = (bool)$_GET['ato_debug_l10n'];
      }
      $conf['debug_l10n'] = $this->data['debug_l10n'];

      // debug_template
      if (isset($_GET['ato_debug_template']))
      {
        $this->data['debug_template'] = (bool)$_GET['ato_debug_template'];
      }
      $conf['debug_template'] = $this->data['debug_template'];

      // template_combine_files
      if (isset($_GET['ato_template_combine_files']))
      {
        $this->data['template_combine_files'] = (bool)$_GET['ato_template_combine_files'];
      }
      $conf['template_combine_files'] = $this->data['template_combine_files'];

      // no_history
      if (isset($_GET['ato_no_history']))
      {
        $this->data['no_history'] = (bool)$_GET['ato_no_history'];
      }
      if ($this->data['no_history'])
      {
        add_event_handler('pwg_log_allowed', create_function('', 'return false;'));
      }

      $this->save();
    }
  }

  /**
   * Returns the language of the current user if different from the current language
   * false otherwise
   */
  function get_user_language()
  {
    if (isset($this->user['language']) && isset($this->data['lang'])
        && $this->user['language'] != $this->data['lang']
      )
    {
      return $this->user['language'];
    }
    return false;
  }

  /**
   * Triggered on "init", in order to clean template files (not initialized on "user_init")
   */
  public function init()
  {
    if ($this->is_admin)
    {
      if (isset($_GET['ato_purge_template']))
      {
        global $template;
        $template->delete_compiled_templates();
        FileCombiner::clear_combined_files();
      }
    }
  }

  /**
   * Mark browser session cache for deletion
   */
  public static function invalidate_cache()
  {
    global $conf;
    conf_update_param('multiview_invalidate_cache', true, true);
  }

  /**
   * Register custom API methods
   */
  public static function register_ws($arr)
  {
    $service = &$arr[0];

    $service->addMethod(
      'multiView.getData',
      array('MultiView', 'ws_get_data'),
      array(),
      'AdminTools private method.',
      null,
      array('admin_only' => true, 'hidden' => true)
      );
  }

  /**
   * API method
   * Return full list of users, themes and languages
   */
  public static function ws_get_data($params)
  {
    global $conf;

    // get users
    $query = '
SELECT
  '.$conf['user_fields']['id'].' AS id,
  '.$conf['user_fields']['username'].' AS username
FROM '.USERS_TABLE.'
  ORDER BY CONVERT('.$conf['user_fields']['username'].', CHAR)
;';
    $out['users'] = array_from_query($query);

    // get themes
    include_once(PHPWG_ROOT_PATH.'admin/include/themes.class.php');
    $themes = new themes();
    foreach (array_keys($themes->db_themes_by_id) as $theme)
    {
      if (!empty($theme))
      {
        $out['themes'][] = $theme;
      }
    }

    // get languages
    foreach (get_languages() as $code => $name)
    {
      $out['languages'][] = array(
        'id' => $code,
        'name' => $name,
        );
    }

    conf_delete_param('multiview_invalidate_cache');

    return $out;
  }
}