plugins/admin/ThemeManager.php
author Dan
Mon, 10 Aug 2009 22:43:26 -0400
changeset 1079 fcc42560afe6
parent 976 50f8eb4f53e1
child 1081 745200a9cc2a
permissions -rw-r--r--
Added ability for authentication plugins to modify session keys (to allow invalidation when their own authentication data is changed) as well as the ability to disable the built-in password change facility
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
433
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
     1
<?php
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
     2
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
     3
/*
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
     4
 * Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
801
eb8b23f11744 Two big commits in one day I know, but redid password storage to use HMAC-SHA1. Consolidated much AES processing to three core methods in session that should handle everything automagically. Installation works; upgrades should. Rebranded as 1.1.6.
Dan
parents: 685
diff changeset
     5
 * Version 1.1.6 (Caoineag beta 1)
536
218a627eb53e Rebrand as 1.1.4 (Caoineag alpha 4)
Dan
parents: 504
diff changeset
     6
 * Copyright (C) 2006-2008 Dan Fuhry
433
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
     7
 *
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
     8
 * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
     9
 * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    10
 *
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    11
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    12
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    13
 */
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    14
465
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
    15
function page_Admin_ThemeManager($force_no_json = false)
433
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    16
{
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    17
  global $db, $session, $paths, $template, $plugins; // Common objects
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    18
  global $lang;
976
50f8eb4f53e1 Added cache for installed themes
Dan
parents: 801
diff changeset
    19
  global $cache;
50f8eb4f53e1 Added cache for installed themes
Dan
parents: 801
diff changeset
    20
  
433
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    21
  if ( $session->auth_level < USER_LEVEL_ADMIN || $session->user_level < USER_LEVEL_ADMIN )
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    22
  {
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    23
    $login_link = makeUrlNS('Special', 'Login/' . $paths->nslist['Special'] . 'Administration', 'level=' . USER_LEVEL_ADMIN, true);
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    24
    echo '<h3>' . $lang->get('adm_err_not_auth_title') . '</h3>';
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    25
    echo '<p>' . $lang->get('adm_err_not_auth_body', array( 'login_link' => $login_link )) . '</p>';
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    26
    return;
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    27
  }
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    28
  
471
7906fb190fc1 Implemented all security features on theme disabling and ACLs; added clean_key mode to login API to clean unused encryption keys
Dan
parents: 465
diff changeset
    29
  $system_themes =& $template->system_themes;
433
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    30
  
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    31
  // Obtain the list of themes (both available and already installed) and the styles available for each
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    32
  $dh = @opendir(ENANO_ROOT . '/themes');
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    33
  if ( !$dh )
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    34
    die('Couldn\'t open themes directory');
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    35
  $themes = array();
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    36
  while ( $dr = @readdir($dh) )
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    37
  {
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    38
    if ( $dr == '.' || $dr == '..' )
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    39
      continue;
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    40
    if ( !is_dir(ENANO_ROOT . "/themes/$dr") )
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    41
      continue;
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    42
    if ( !file_exists(ENANO_ROOT . "/themes/$dr/theme.cfg") || !is_dir(ENANO_ROOT . "/themes/$dr/css") )
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    43
      continue;
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    44
    $cdh = @opendir(ENANO_ROOT . "/themes/$dr/css");
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    45
    if ( !$cdh )
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    46
      continue;
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    47
    
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    48
    require(ENANO_ROOT . "/themes/$dr/theme.cfg");
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    49
    global $theme;
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    50
    
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    51
    $themes[$dr] = array(
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    52
        'css' => array(),
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    53
        'theme_name' => $theme['theme_name']
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    54
      );
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    55
    while ( $cdr = @readdir($cdh) )
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    56
    {
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    57
      if ( $cdr == '.' || $cdr == '..' )
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    58
        continue;
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    59
      if ( preg_match('/\.css$/i', $cdr) )
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    60
        $themes[$dr]['css'][] = substr($cdr, 0, -4);
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    61
    }
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    62
  }
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    63
  
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    64
  // Decide which themes are not installed
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    65
  $installable = array_flip(array_keys($themes));
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    66
  // FIXME: sanitize directory names or check with preg_match()
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    67
  $where_clause = 'theme_id = \'' . implode('\' OR theme_id = \'', array_flip($installable)) . '\'';
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    68
  $q = $db->sql_query('SELECT theme_id, theme_name, enabled FROM ' . table_prefix . "themes WHERE $where_clause;");
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    69
  if ( !$q )
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    70
    $db->_die();
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    71
  
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    72
  while ( $row = $db->fetchrow() )
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    73
  {
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    74
    $tid =& $row['theme_id'];
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    75
    unset($installable[$tid]);
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    76
    $themes[$tid]['theme_name'] = $row['theme_name'];
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    77
    $themes[$tid]['enabled'] = ( $row['enabled'] == 1 );
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    78
  }
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    79
  
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    80
  foreach ( $system_themes as $st )
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    81
  {
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    82
    unset($installable[$st]);
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    83
  }
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    84
  
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    85
  $installable = array_flip($installable);
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    86
  
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    87
  // AJAX code
465
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
    88
  if ( $paths->getParam(0) === 'action.json' && !$force_no_json )
433
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    89
  {
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    90
    return ajaxServlet_Admin_ThemeManager($themes);
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    91
  }
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    92
  
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    93
  // List installed themes
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    94
  ?>
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    95
  <div style="float: right;">
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    96
    <a href="#" id="systheme_toggler" onclick="ajaxToggleSystemThemes(); return false;"><?php echo $lang->get('acptm_btn_system_themes_show'); ?></a>
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    97
  </div>
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    98
  <?php
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
    99
  echo '<h3>' . $lang->get('acptm_heading_edit_themes') . '</h3>';
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   100
  echo '<div id="theme_list_edit">';
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   101
  foreach ( $themes as $theme_id => $theme_data )
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   102
  {
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   103
    if ( in_array($theme_id, $installable) )
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   104
      continue;
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   105
    if ( file_exists(ENANO_ROOT . "/themes/$theme_id/preview.png") )
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   106
    {
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   107
      $preview_path = scriptPath . "/themes/$theme_id/preview.png";
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   108
    }
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   109
    else
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   110
    {
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   111
      $preview_path = scriptPath . "/images/themepreview.png";
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   112
    }
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   113
    $d = ( @$theme_data['enabled'] ) ? '' : ' themebutton_theme_disabled';
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   114
    $st = ( in_array($theme_id, $system_themes) ) ? ' themebutton_theme_system' : '';
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   115
    echo '<div class="themebutton' . $st . '' . $d . '" id="themebtn_edit_' . $theme_id . '" style="background-image: url(' . $preview_path . ');">';
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   116
    if ( in_array($theme_id, $system_themes) )
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   117
    {
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   118
      echo   '<a class="tb-inner" href="#" onclick="return false;">
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   119
                ' . $lang->get('acptm_btn_theme_system') . '
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   120
                <span class="themename">' . htmlspecialchars($theme_data['theme_name']) . '</span>
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   121
              </a>';
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   122
    }
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   123
    else
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   124
    {
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   125
      echo   '<a class="tb-inner" href="#" onclick="ajaxEditTheme(\'' . $theme_id . '\'); return false;">
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   126
                ' . $lang->get('acptm_btn_theme_edit') . '
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   127
                <span class="themename">' . htmlspecialchars($theme_data['theme_name']) . '</span>
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   128
              </a>';
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   129
    }
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   130
    echo '</div>';
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   131
  }
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   132
  echo '</div>';
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   133
  echo '<span class="menuclear"></span>';
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   134
  
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   135
  if ( count($installable) > 0 )
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   136
  {
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   137
    echo '<h3>' . $lang->get('acptm_heading_install_themes') . '</h3>';
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   138
  
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   139
    echo '<div id="theme_list_install">';
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   140
    foreach ( $installable as $i => $theme_id )
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   141
    {
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   142
      if ( file_exists(ENANO_ROOT . "/themes/$theme_id/preview.png") )
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   143
      {
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   144
        $preview_path = scriptPath . "/themes/$theme_id/preview.png";
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   145
      }
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   146
      else
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   147
      {
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   148
        $preview_path = scriptPath . "/images/themepreview.png";
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   149
      }
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   150
      echo '<div class="themebutton" id="themebtn_install_' . $theme_id . '" enano:themename="' . htmlspecialchars($themes[$theme_id]['theme_name']) . '" style="background-image: url(' . $preview_path . ');">';
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   151
      echo   '<a class="tb-inner" href="#" onclick="ajaxInstallTheme(\'' . $theme_id . '\'); return false;">
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   152
                ' . $lang->get('acptm_btn_theme_install') . '
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   153
                <span class="themename">' . htmlspecialchars($themes[$theme_id]['theme_name']) . '</span>
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   154
              </a>';
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   155
      echo '</div>';
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   156
    }
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   157
    echo '</div>';
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   158
    echo '<span class="menuclear"></span>';
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   159
  }
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   160
}
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   161
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   162
function ajaxServlet_Admin_ThemeManager(&$themes)
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   163
{
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   164
  global $db, $session, $paths, $template, $plugins; // Common objects
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   165
  global $lang;
976
50f8eb4f53e1 Added cache for installed themes
Dan
parents: 801
diff changeset
   166
  global $cache;
50f8eb4f53e1 Added cache for installed themes
Dan
parents: 801
diff changeset
   167
  
433
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   168
  if ( $session->auth_level < USER_LEVEL_ADMIN || $session->user_level < USER_LEVEL_ADMIN )
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   169
  {
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   170
    $login_link = makeUrlNS('Special', 'Login/' . $paths->nslist['Special'] . 'Administration', 'level=' . USER_LEVEL_ADMIN, true);
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   171
    echo '<h3>' . $lang->get('adm_err_not_auth_title') . '</h3>';
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   172
    echo '<p>' . $lang->get('adm_err_not_auth_body', array( 'login_link' => $login_link )) . '</p>';
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   173
    return;
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   174
  }
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   175
  
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   176
  if ( !isset($_POST['r']) )
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   177
    return false;
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   178
  
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   179
  try
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   180
  {
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   181
    $request = enano_json_decode($_POST['r']);
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   182
  }
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   183
  catch ( Exception $e )
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   184
  {
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   185
    die('Exception in JSON parser, probably invalid input.');
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   186
  }
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   187
  
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   188
  if ( !isset($request['mode']) )
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   189
  {
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   190
    die('No mode specified in JSON request.');
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   191
  }
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   192
  
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   193
  switch ( $request['mode'] )
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   194
  {
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   195
    case 'fetch_theme':
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   196
      $theme_id = $db->escape($request['theme_id']);
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   197
      if ( empty($theme_id) )
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   198
        die('Invalid theme_id');
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   199
      
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   200
      $q = $db->sql_query("SELECT theme_id, theme_name, default_style, enabled, group_policy, group_list FROM " . table_prefix . "themes WHERE theme_id = '$theme_id';");
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   201
      if ( !$q )
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   202
        $db->die_json();
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   203
      
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   204
      if ( $db->numrows() < 1 )
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   205
        die('BUG: no theme with that theme_id installed.');
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   206
      
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   207
      $row = $db->fetchrow();
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   208
      $row['enabled'] = ( $row['enabled'] == 1 );
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   209
      $row['css'] = @$themes[$theme_id]['css'];
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   210
      $row['default_style'] = preg_replace('/\.css$/', '', $row['default_style']);
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   211
      $row['is_default'] = ( getConfig('theme_default') === $theme_id );
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   212
      $row['group_list'] = ( empty($row['group_list']) ) ? array() : enano_json_decode($row['group_list']);
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   213
      
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   214
      // Build a list of group names
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   215
      $row['group_names'] = array();
465
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   216
      $q = $db->sql_query('SELECT group_id, group_name FROM ' . table_prefix . 'groups;');
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   217
      if ( !$q )
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   218
        $db->die_json();
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   219
      while ( $gr = $db->fetchrow() )
433
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   220
      {
465
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   221
        $row['group_names'][ intval($gr['group_id']) ] = $gr['group_name'];
433
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   222
      }
465
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   223
      $db->free_result();
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   224
      
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   225
      // Build a list of usernames
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   226
      $row['usernames'] = array();
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   227
      foreach ( $row['group_list'] as $el )
433
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   228
      {
465
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   229
        if ( !preg_match('/^u:([0-9]+)$/', $el, $match) )
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   230
          continue;
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   231
        $uid =& $match[1];
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   232
        $q = $db->sql_query('SELECT username FROM ' . table_prefix . "users WHERE user_id = $uid;");
433
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   233
        if ( !$q )
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   234
          $db->die_json();
465
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   235
        if ( $db->numrows() < 1 )
433
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   236
        {
465
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   237
          $db->free_result();
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   238
          continue;
433
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   239
        }
465
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   240
        list($username) = $db->fetchrow_num();
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   241
        $row['usernames'][$uid] = $username;
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   242
        $db->free_result();
433
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   243
      }
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   244
      
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   245
      echo enano_json_encode($row);
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   246
      break;
465
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   247
    case 'uid_lookup':
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   248
      $username = @$request['username'];
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   249
      if ( empty($username) )
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   250
      {
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   251
        die(enano_json_encode(array(
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   252
            'mode' => 'error',
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   253
            'error' => $lang->get('acptm_err_invalid_username')
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   254
          )));
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   255
      }
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   256
      $username = $db->escape(strtolower($username));
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   257
      $q = $db->sql_query('SELECT user_id, username FROM ' . table_prefix . "users WHERE " . ENANO_SQLFUNC_LOWERCASE . "(username) = '$username';");
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   258
      if ( !$q )
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   259
        $db->die_json();
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   260
      
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   261
      if ( $db->numrows() < 1 )
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   262
      {
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   263
        die(enano_json_encode(array(
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   264
            'mode' => 'error',
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   265
            'error' => $lang->get('acptm_err_username_not_found')
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   266
          )));
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   267
      }
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   268
      
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   269
      list($uid, $username_real) = $db->fetchrow_num();
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   270
      $db->free_result();
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   271
      
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   272
      echo enano_json_encode(array(
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   273
          'uid' => $uid,
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   274
          'username' => $username_real
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   275
        ));
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   276
      break;
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   277
    case 'save_theme':
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   278
      if ( !isset($request['theme_data']) )
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   279
      {
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   280
        die(enano_json_encode(array(
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   281
            'mode' => 'error',
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   282
            'error' => 'No theme data in request'
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   283
          )));
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   284
      }
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   285
      $theme_data =& $request['theme_data'];
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   286
      // Perform integrity check on theme data
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   287
      $chk_theme_exists = isset($themes[@$theme_data['theme_id']]);
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   288
      $theme_data['theme_name'] = trim(@$theme_data['theme_name']);
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   289
      $chk_name_good = !empty($theme_data['theme_name']);
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   290
      $chk_policy_good = in_array(@$theme_data['group_policy'], array('allow_all', 'whitelist', 'blacklist'));
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   291
      $chk_grouplist_good = true;
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   292
      foreach ( $theme_data['group_list'] as $acl_entry )
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   293
      {
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   294
        if ( !preg_match('/^(u|g):[0-9]+$/', $acl_entry) )
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   295
        {
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   296
          $chk_grouplist_good = false;
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   297
          break;
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   298
        }
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   299
      }
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   300
      $chk_style_good = @in_array(@$theme_data['default_style'], @$themes[@$theme_data['theme_id']]['css']);
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   301
      if ( !$chk_theme_exists || !$chk_name_good || !$chk_policy_good || !$chk_grouplist_good || !$chk_style_good )
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   302
      {
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   303
        die(enano_json_encode(array(
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   304
            'mode' => 'error',
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   305
            'error' => $lang->get('acptm_err_save_validation_failed')
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   306
          )));
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   307
      }
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   308
      
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   309
      $enable = ( $theme_data['enabled'] ) ? '1' : '0';
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   310
      $theme_default = getConfig('theme_default');
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   311
      $warn_default = ( $theme_default === $theme_data['theme_id'] || $theme_data['make_default'] ) ?
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   312
                        ' ' . $lang->get('acptm_warn_access_with_default') . ' ' :
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   313
                        ' ';
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   314
      if ( $enable == 0 && ( $theme_default === $theme_data['theme_id'] || $theme_data['make_default'] ) )
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   315
      {
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   316
        $enable = '1';
471
7906fb190fc1 Implemented all security features on theme disabling and ACLs; added clean_key mode to login API to clean unused encryption keys
Dan
parents: 465
diff changeset
   317
        $warn_default .= '<b>' . $lang->get('acptm_warn_cant_disable_default') . '</b>';
465
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   318
      }
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   319
      
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   320
      // We're good. Update the theme...
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   321
      $q = $db->sql_query('UPDATE ' . table_prefix . 'themes SET
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   322
                               theme_name = \'' . $db->escape($theme_data['theme_name']) . '\',
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   323
                               default_style = \'' . $db->escape($theme_data['default_style']) . '\',
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   324
                               group_list = \'' . $db->escape(enano_json_encode($theme_data['group_list'])) . '\',
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   325
                               group_policy = \'' . $db->escape($theme_data['group_policy']) . '\',
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   326
                               enabled = ' . $enable . '
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   327
                             WHERE theme_id = \'' . $db->escape($theme_data['theme_id']) . '\';');
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   328
      if ( !$q )
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   329
        $db->die_json();
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   330
      
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   331
      if ( $theme_data['make_default'] )
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   332
      {
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   333
        setConfig('theme_default', $theme_data['theme_id']);
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   334
      }
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   335
      
976
50f8eb4f53e1 Added cache for installed themes
Dan
parents: 801
diff changeset
   336
      $cache->purge('themes');
50f8eb4f53e1 Added cache for installed themes
Dan
parents: 801
diff changeset
   337
      
465
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   338
      echo '<div class="info-box"><b>' . $lang->get('acptm_msg_save_success') . '</b>' . $warn_default . '</div>';
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   339
      
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   340
      page_Admin_ThemeManager(true);
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   341
      break;
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   342
    case 'install':
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   343
      $theme_id =& $request['theme_id'];
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   344
      if ( !isset($themes[$theme_id]) )
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   345
      {
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   346
        die(enano_json_encode(array(
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   347
            'mode' => 'error',
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   348
            'error' => 'Theme was deleted from themes/ directory or couldn\'t read theme metadata from filesystem'
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   349
          )));
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   350
      }
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   351
      if ( !isset($themes[$theme_id]['css'][0]) )
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   352
      {
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   353
        die(enano_json_encode(array(
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   354
            'mode' => 'error',
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   355
            'error' => 'Theme doesn\'t have any files in css/, thus it can\'t be installed. (translators: l10n?)'
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   356
          )));
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   357
      }
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   358
      // build dataset
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   359
      $theme_name = $db->escape($themes[$theme_id]['theme_name']);
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   360
      $default_style = $db->escape($themes[$theme_id]['css'][0]);
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   361
      $theme_id = $db->escape($theme_id);
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   362
      
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   363
      // insert it
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   364
      $q = $db->sql_query('INSERT INTO ' . table_prefix . "themes(theme_id, theme_name, default_style, enabled, group_list, group_policy)\n"
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   365
                        . "  VALUES( '$theme_id', '$theme_name', '$default_style', 1, '[]', 'allow_all' );");
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   366
      if ( !$q )
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   367
        $db->die_json();
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   368
      
976
50f8eb4f53e1 Added cache for installed themes
Dan
parents: 801
diff changeset
   369
      $cache->purge('themes');
50f8eb4f53e1 Added cache for installed themes
Dan
parents: 801
diff changeset
   370
      
465
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   371
      // The response isn't processed unless it's in JSON.
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   372
      echo 'Roger that, over and out.';
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   373
      
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   374
      break;
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   375
    case 'uninstall':
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   376
      $theme_id =& $request['theme_id'];
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   377
      $theme_default = getConfig('theme_default');
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   378
      
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   379
      // Validation
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   380
      if ( !isset($themes[$theme_id]) )
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   381
      {
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   382
        die(enano_json_encode(array(
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   383
            'mode' => 'error',
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   384
            'error' => 'Theme was deleted from themes/ directory or couldn\'t read theme metadata from filesystem'
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   385
          )));
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   386
      }
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   387
      
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   388
      if ( $theme_id == $theme_default )
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   389
      {
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   390
        die(enano_json_encode(array(
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   391
            'mode' => 'error',
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   392
            'error' => $lang->get('acptm_err_uninstalling_default')
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   393
          )));
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   394
      }
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   395
      
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   396
      if ( $theme_id == 'oxygen' )
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   397
      {
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   398
        die(enano_json_encode(array(
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   399
            'mode' => 'error',
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   400
            'error' => $lang->get('acptm_err_uninstalling_oxygen')
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   401
          )));
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   402
      }
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   403
      
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   404
      $theme_id = $db->escape($theme_id);
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   405
      
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   406
      $q = $db->sql_query('DELETE FROM ' . table_prefix . "themes WHERE theme_id = '$theme_id';");
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   407
      if ( !$q )
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   408
        $db->die_json();
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   409
      
976
50f8eb4f53e1 Added cache for installed themes
Dan
parents: 801
diff changeset
   410
      $cache->purge('themes');
50f8eb4f53e1 Added cache for installed themes
Dan
parents: 801
diff changeset
   411
      
465
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   412
      // Change all the users that were on that theme to the default
477
c4d6e9b3b964 Fixed bad style update during theme uninstall.
Dan
parents: 471
diff changeset
   413
      $default_style = $template->named_theme_list[$theme_default]['default_style'];
465
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   414
      $default_style = preg_replace('/\.css$/', '', $default_style);
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   415
      
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   416
      $theme_default = $db->escape($theme_default);
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   417
      $default_style = $db->escape($default_style);
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   418
      
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   419
      $q = $db->sql_query('UPDATE ' . table_prefix . "users SET theme = '$theme_default', style = '$default_style' WHERE theme = '$theme_id';");
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   420
      if ( !$q )
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   421
        $db->die_json();
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   422
      
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   423
      echo '<div class="info-box">' . $lang->get('acptm_msg_uninstall_success') . '</div>';
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   424
      
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   425
      page_Admin_ThemeManager(true);
fe8b8c9b54e8 Finished theme manager to the point where it's in a working state in Firefox and hopefully IE.
Dan
parents: 433
diff changeset
   426
      break;
433
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   427
  }
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   428
}
c892b2013d3e Can't believe I forgot to add ThemeManager.php.
Dan
parents:
diff changeset
   429