[ Index ]

PHP Cross Reference of MyBB 1.8.38

title

Body

[close]

/admin/modules/style/ -> themes.php (source)

   1  <?php
   2  /**
   3   * MyBB 1.8
   4   * Copyright 2014 MyBB Group, All Rights Reserved
   5   *
   6   * Website: http://www.mybb.com
   7   * License: http://www.mybb.com/about/license
   8   *
   9   */
  10  
  11  // Disallow direct access to this file for security reasons
  12  if(!defined("IN_MYBB"))
  13  {
  14      die("Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.");
  15  }
  16  
  17  require_once MYBB_ADMIN_DIR."inc/functions_themes.php";
  18  
  19  $page->extra_header .= "
  20  <script type=\"text/javascript\">
  21  //<![CDATA[
  22  var save_changes_lang_string = '{$lang->save_changes_js}';
  23  var delete_lang_string = '{$lang->delete}';
  24  var file_lang_string = '{$lang->file}';
  25  var globally_lang_string = '{$lang->globally}';
  26  var specific_actions_lang_string = '{$lang->specific_actions}';
  27  var specific_actions_desc_lang_string = '{$lang->specific_actions_desc}';
  28  var delete_confirm_lang_string = '{$lang->delete_confirm_js}';
  29  
  30  lang.theme_info_fetch_error = \"{$lang->theme_info_fetch_error}\";
  31  lang.theme_info_save_error = \"{$lang->theme_info_save_error}\";
  32  //]]>
  33  </script>";
  34  
  35  if($mybb->input['action'] == "xmlhttp_stylesheet" && $mybb->request_method == "post")
  36  {
  37      // Fetch the theme we want to edit this stylesheet in
  38      $query = $db->simple_select("themes", "*", "tid='".$mybb->get_input('tid', MyBB::INPUT_INT)."'");
  39      $theme = $db->fetch_array($query);
  40  
  41      if(!$theme || $theme['tid'] == 1)
  42      {
  43          flash_message($lang->error_invalid_theme, 'error');
  44          admin_redirect("index.php?module=style-themes");
  45      }
  46  
  47      $parent_list = make_parent_theme_list($theme['tid']);
  48      $parent_list = implode(',', $parent_list);
  49      if(!$parent_list)
  50      {
  51          $parent_list = 1;
  52      }
  53  
  54      $query = $db->simple_select("themestylesheets", "*", "name='".$db->escape_string($mybb->input['file'])."' AND tid IN ({$parent_list})", array('order_by' => 'tid', 'order_dir' => 'desc', 'limit' => 1));
  55      $stylesheet = $db->fetch_array($query);
  56  
  57      // Does the theme not exist?
  58      if(!$stylesheet)
  59      {
  60          flash_message($lang->error_invalid_stylesheet, 'error');
  61          admin_redirect("index.php?module=style-themes");
  62      }
  63  
  64      $css_array = css_to_array($stylesheet['stylesheet']);
  65      $selector_list = get_selectors_as_options($css_array, $mybb->input['selector']);
  66      $editable_selector = $css_array[$mybb->input['selector']];
  67      $properties = parse_css_properties($editable_selector['values']);
  68  
  69      foreach(array('background', 'color', 'width', 'font-family', 'font-size', 'font-style', 'font-weight', 'text-decoration') as $_p)
  70      {
  71          if(!isset($properties[$_p]))
  72          {
  73              $properties[$_p] = '';
  74          }
  75      }
  76  
  77      $form = new Form("index.php?module=style-themes&amp;action=stylesheet_properties", "post", "selector_form", 0, "", true);
  78      echo $form->generate_hidden_field("tid", $mybb->input['tid'], array('id' => "tid"))."\n";
  79      echo $form->generate_hidden_field("file", htmlspecialchars_uni($mybb->input['file']), array('id' => "file"))."\n";
  80      echo $form->generate_hidden_field("selector", htmlspecialchars_uni($mybb->input['selector']), array('id' => 'hidden_selector'))."\n";
  81  
  82      $table = new Table;
  83      if($lang->settings['rtl'] === true)
  84      {
  85          $div_align = "left";
  86      }
  87      else
  88      {
  89          $div_align = "right";
  90      }
  91  
  92      $table->construct_cell("<div style=\"float: {$div_align};\">".$form->generate_text_box('css_bits[background]', $properties['background'], array('id' => 'css_bits[background]', 'style' => 'width: 260px;'))."</div><div><strong>{$lang->background}</strong></div>", array('style' => 'width: 20%;'));
  93      $table->construct_cell("<strong>{$lang->extra_css_atribs}</strong><br /><div style=\"align: center;\">".$form->generate_text_area('css_bits[extra]', $properties['extra'], array('id' => 'css_bits[extra]', 'style' => 'width: 98%;', 'rows' => '19'))."</div>", array('rowspan' => 8));
  94      $table->construct_row();
  95      $table->construct_cell("<div style=\"float: {$div_align};\">".$form->generate_text_box('css_bits[color]', $properties['color'], array('id' => 'css_bits[color]', 'style' => 'width: 260px;'))."</div><div><strong>{$lang->color}</strong></div>", array('style' => 'width: 40%;'));
  96      $table->construct_row();
  97      $table->construct_cell("<div style=\"float: {$div_align};\">".$form->generate_text_box('css_bits[width]', $properties['width'], array('id' => 'css_bits[width]', 'style' => 'width: 260px;'))."</div><div><strong>{$lang->width}</strong></div>", array('style' => 'width: 40%;'));
  98      $table->construct_row();
  99      $table->construct_cell("<div style=\"float: {$div_align};\">".$form->generate_text_box('css_bits[font_family]', $properties['font-family'], array('id' => 'css_bits[font_family]', 'style' => 'width: 260px;'))."</div><div><strong>{$lang->font_family}</strong></div>", array('style' => 'width: 40%;'));
 100      $table->construct_row();
 101      $table->construct_cell("<div style=\"float: {$div_align};\">".$form->generate_text_box('css_bits[font_size]', $properties['font-size'], array('id' => 'css_bits[font_size]', 'style' => 'width: 260px;'))."</div><div><strong>{$lang->font_size}</strong></div>", array('style' => 'width: 40%;'));
 102      $table->construct_row();
 103      $table->construct_cell("<div style=\"float: {$div_align};\">".$form->generate_text_box('css_bits[font_style]', $properties['font-style'], array('id' => 'css_bits[font_style]', 'style' => 'width: 260px;'))."</div><div><strong>{$lang->font_style}</strong></div>", array('style' => 'width: 40%;'));
 104      $table->construct_row();
 105      $table->construct_cell("<div style=\"float: {$div_align};\">".$form->generate_text_box('css_bits[font_weight]', $properties['font-weight'], array('id' => 'css_bits[font_weight]', 'style' => 'width: 260px;'))."</div><div><strong>{$lang->font_weight}</strong></div>", array('style' => 'width: 40%;'));
 106      $table->construct_row();
 107      $table->construct_cell("<div style=\"float: {$div_align};\">".$form->generate_text_box('css_bits[text_decoration]', $properties['text-decoration'], array('id' => 'css_bits[text_decoration]', 'style' => 'width: 260px;'))."</div><div><strong>{$lang->text_decoration}</strong></div>", array('style' => 'width: 40%;'));
 108      $table->construct_row();
 109  
 110      $table->output(htmlspecialchars_uni($editable_selector['class_name'])."<span id=\"saved\" style=\"color: #FEE0C6;\"></span>");
 111      exit;
 112  }
 113  
 114  $page->add_breadcrumb_item($lang->themes, "index.php?module=style-themes");
 115  
 116  if($mybb->input['action'] == "add" || $mybb->input['action'] == "import" || $mybb->input['action'] == "browse" || !$mybb->input['action'])
 117  {
 118      $sub_tabs['themes'] = array(
 119          'title' => $lang->themes,
 120          'link' => "index.php?module=style-themes",
 121          'description' => $lang->themes_desc
 122      );
 123  
 124      $sub_tabs['create_theme'] = array(
 125          'title' => $lang->create_new_theme,
 126          'link' => "index.php?module=style-themes&amp;action=add",
 127          'description' => $lang->create_new_theme_desc
 128      );
 129  
 130      $sub_tabs['import_theme'] = array(
 131          'title' => $lang->import_a_theme,
 132          'link' => "index.php?module=style-themes&amp;action=import",
 133          'description' => $lang->import_a_theme_desc
 134      );
 135  
 136      $sub_tabs['browse_themes'] = array(
 137          'title' => $lang->browse_themes,
 138          'link' => "index.php?module=style-themes&amp;action=browse",
 139          'description' => $lang->browse_themes_desc
 140      );
 141  }
 142  
 143  $plugins->run_hooks("admin_style_themes_begin");
 144  
 145  if($mybb->input['action'] == "browse")
 146  {
 147      $plugins->run_hooks("admin_style_themes_browse");
 148  
 149      $page->add_breadcrumb_item($lang->browse_themes);
 150  
 151      $page->output_header($lang->browse_themes);
 152  
 153      $page->output_nav_tabs($sub_tabs, 'browse_themes');
 154  
 155      // Process search requests
 156      $keywords = "";
 157      if(!empty($mybb->input['keywords']))
 158      {
 159          $keywords = "&keywords=".urlencode($mybb->input['keywords']);
 160      }
 161  
 162      if(!empty($mybb->input['page']))
 163      {
 164          $url_page = "&page=".$mybb->get_input('page', MyBB::INPUT_INT);
 165      }
 166      else
 167      {
 168          $mybb->input['page'] = 1;
 169          $url_page = "";
 170      }
 171  
 172      // Gets the major version code. i.e. 1410 -> 1400 or 121 -> 1200
 173      $major_version_code = round($mybb->version_code/100, 0)*100;
 174      // Convert to mods site version codes
 175      $search_version = ($major_version_code/100).'x';
 176  
 177      $contents = fetch_remote_file("https://community.mybb.com/xmlbrowse.php?api=2&type=themes&version={$search_version}{$keywords}{$url_page}");
 178  
 179      if(!$contents)
 180      {
 181          $page->output_inline_error($lang->error_communication_problem);
 182          $page->output_footer();
 183          exit;
 184      }
 185  
 186      $table = new Table;
 187      $table->construct_header($lang->themes, array('colspan' => 2));
 188      $table->construct_header($lang->controls, array("class" => "align_center", 'width' => 125));
 189  
 190      $parser = create_xml_parser($contents);
 191      $tree = $parser->get_tree();
 192  
 193      if(!is_array($tree) || !isset($tree['results']))
 194      {
 195          $page->output_inline_error($lang->error_communication_problem);
 196          $page->output_footer();
 197          exit;
 198      }
 199  
 200      if(!empty($tree['results']['result']))
 201      {
 202          if(array_key_exists("tag", $tree['results']['result']))
 203          {
 204              $only_theme = $tree['results']['result'];
 205              unset($tree['results']['result']);
 206              $tree['results']['result'][0] = $only_theme;
 207          }
 208  
 209          require_once  MYBB_ROOT . '/inc/class_parser.php';
 210          $post_parser = new postParser();
 211  
 212          foreach($tree['results']['result'] as $result)
 213          {
 214              $result['thumbnail']['value'] = htmlspecialchars_uni($result['thumbnail']['value']);
 215              $result['name']['value'] = htmlspecialchars_uni($result['name']['value']);
 216              $result['description']['value'] = htmlspecialchars_uni($result['description']['value']);
 217              $result['author']['url']['value'] = htmlspecialchars_uni($result['author']['url']['value']);
 218              $result['author']['name']['value'] = htmlspecialchars_uni($result['author']['name']['value']);
 219              $result['download_url']['value'] = htmlspecialchars_uni(html_entity_decode($result['download_url']['value']));
 220  
 221              $table->construct_cell("<img src=\"https://community.mybb.com/{$result['thumbnail']['value']}\" alt=\"{$lang->theme_thumbnail}\" title=\"{$lang->theme_thumbnail}\"/>", array("class" => "align_center", "width" => 100));
 222              $table->construct_cell("<strong>{$result['name']['value']}</strong><br /><small>{$result['description']['value']}</small><br /><i><small>{$lang->created_by} <a href=\"{$result['author']['url']['value']}\" target=\"_blank\" rel=\"noopener\">{$result['author']['name']['value']}</a></small></i>");
 223              $table->construct_cell("<strong><a href=\"https://community.mybb.com/{$result['download_url']['value']}\" target=\"_blank\" rel=\"noopener\">{$lang->download}</a></strong>", array("class" => "align_center"));
 224              $table->construct_row();
 225          }
 226      }
 227  
 228      if($table->num_rows() == 0)
 229      {
 230          $table->construct_cell($lang->error_no_results_found, array("colspan" => 3));
 231          $table->construct_row();
 232      }
 233  
 234      $search = new Form("index.php?module=style-themes&amp;action=browse", 'post', 'search_form');
 235      echo "<div style=\"padding-bottom: 3px; margin-top: -9px; text-align: right;\">";
 236      if(!empty($mybb->input['keywords']))
 237      {
 238          $default_class = '';
 239          $value = htmlspecialchars_uni($mybb->input['keywords']);
 240      }
 241      else
 242      {
 243          $default_class = "search_default";
 244          $value = $lang->search_for_themes;
 245      }
 246      echo $search->generate_text_box('keywords', $value, array('id' => 'search_keywords', 'class' => "{$default_class} field150 field_small"))."\n";
 247      echo "<input type=\"submit\" class=\"search_button\" value=\"{$lang->search}\" />\n";
 248      echo "<script type=\"text/javascript\">
 249          var form = $(\"#search_form\");
 250          form.on('submit', function()
 251          {
 252              var search = $('#search_keywords');
 253              if(search.val() == '' || search.val() == '{$lang->search_for_themes}')
 254              {
 255                  search.trigger('focus');
 256                  return false;
 257              }
 258          });
 259  
 260          var search = $('#search_keywords');
 261          search.on('focus', function()
 262          {
 263              var search_focus = $(this);
 264              if(search_focus.val() == '{$lang->search_for_themes}')
 265              {
 266                  search_focus.removeClass('search_default');
 267                  search_focus.val('');
 268              }
 269          }).on('blur', function()
 270          {
 271              var search_blur = $(this);
 272              if(search_blur.val() == '')
 273              {
 274                  search_blur.addClass('search_default');
 275                  search_blur.val('{$lang->search_for_themes}');
 276              }
 277          });
 278  
 279          // fix the styling used if we have a different default value
 280          if(search.val() != '{$lang->search_for_themes}')
 281          {
 282              search.removeClass('search_default');
 283          }
 284          </script>\n";
 285      echo "</div>\n";
 286      echo $search->end();
 287  
 288      // Recommended themes = Default; Otherwise search results & pagination
 289      if($mybb->request_method == "post")
 290      {
 291          $table->output("<span style=\"float: right;\"><small><a href=\"https://community.mybb.com/mods.php?action=browse&category=themes\" target=\"_blank\" rel=\"noopener\">{$lang->browse_all_themes}</a></small></span>".$lang->sprintf($lang->browse_results_for_mybb, $mybb->version));
 292      }
 293      else
 294      {
 295          $table->output("<span style=\"float: right;\"><small><a href=\"https://community.mybb.com/mods.php?action=browse&category=themes\" target=\"_blank\" rel=\"noopener\">{$lang->browse_all_themes}</a></small></span>".$lang->sprintf($lang->recommended_themes_for_mybb, $mybb->version));
 296      }
 297  
 298      if(!empty($tree['results']['attributes']['total']))
 299      {
 300          echo "<br />".draw_admin_pagination($mybb->input['page'], 15, $tree['results']['attributes']['total'], "index.php?module=style-themes&amp;action=browse{$keywords}&amp;page={page}");
 301      }
 302  
 303      $page->output_footer();
 304  }
 305  
 306  if($mybb->input['action'] == "import")
 307  {
 308      $plugins->run_hooks("admin_style_themes_import");
 309  
 310      if($mybb->request_method == "post")
 311      {
 312          if(!$_FILES['local_file'] && !$mybb->input['url'])
 313          {
 314              $errors[] = $lang->error_missing_url;
 315          }
 316  
 317          if(!$errors)
 318          {
 319              // Find out if there was an uploaded file
 320              if($_FILES['local_file']['error'] != 4)
 321              {
 322                  // Find out if there was an error with the uploaded file
 323                  if($_FILES['local_file']['error'] != 0)
 324                  {
 325                      $errors[] = $lang->error_uploadfailed.$lang->error_uploadfailed_detail;
 326                      switch($_FILES['local_file']['error'])
 327                      {
 328                          case 1: // UPLOAD_ERR_INI_SIZE
 329                              $errors[] = $lang->error_uploadfailed_php1;
 330                              break;
 331                          case 2: // UPLOAD_ERR_FORM_SIZE
 332                              $errors[] = $lang->error_uploadfailed_php2;
 333                              break;
 334                          case 3: // UPLOAD_ERR_PARTIAL
 335                              $errors[] = $lang->error_uploadfailed_php3;
 336                              break;
 337                          case 6: // UPLOAD_ERR_NO_TMP_DIR
 338                              $errors[] = $lang->error_uploadfailed_php6;
 339                              break;
 340                          case 7: // UPLOAD_ERR_CANT_WRITE
 341                              $errors[] = $lang->error_uploadfailed_php7;
 342                              break;
 343                          default:
 344                              $errors[] = $lang->sprintf($lang->error_uploadfailed_phpx, $_FILES['local_file']['error']);
 345                              break;
 346                      }
 347                  }
 348  
 349                  if(!$errors)
 350                  {
 351                      // Was the temporary file found?
 352                      if(!is_uploaded_file($_FILES['local_file']['tmp_name']))
 353                      {
 354                          $errors[] = $lang->error_uploadfailed_lost;
 355                      }
 356                      // Get the contents
 357                      $contents = @file_get_contents($_FILES['local_file']['tmp_name']);
 358                      // Delete the temporary file if possible
 359                      @unlink($_FILES['local_file']['tmp_name']);
 360                      // Are there contents?
 361                      if(!trim($contents))
 362                      {
 363                          $errors[] = $lang->error_uploadfailed_nocontents;
 364                      }
 365                  }
 366              }
 367              else if(!empty($mybb->input['url']))
 368              {
 369                  // Get the contents
 370                  $contents = @fetch_remote_file($mybb->input['url']);
 371                  if(!$contents)
 372                  {
 373                      $errors[] = $lang->error_local_file;
 374                  }
 375              }
 376              else
 377              {
 378                  // UPLOAD_ERR_NO_FILE
 379                  $errors[] = $lang->error_uploadfailed_php4;
 380              }
 381  
 382              if(!$errors)
 383              {
 384                  $options = array(
 385                      'no_stylesheets' => ($mybb->input['import_stylesheets'] ? 0 : 1),
 386                      'no_templates' => ($mybb->input['import_templates'] ? 0 : 1),
 387                      'version_compat' => $mybb->get_input('version_compat', MyBB::INPUT_INT),
 388                      'parent' => $mybb->get_input('tid', MyBB::INPUT_INT),
 389                      'force_name_check' => true,
 390                  );
 391                  $theme_id = import_theme_xml($contents, $options);
 392  
 393                  if($theme_id > -1)
 394                  {
 395                      $plugins->run_hooks("admin_style_themes_import_commit");
 396  
 397                      // Log admin action
 398                      log_admin_action($theme_id);
 399  
 400                      flash_message($lang->success_imported_theme, 'success');
 401                      admin_redirect("index.php?module=style-themes&action=edit&tid=".$theme_id);
 402                  }
 403                  else
 404                  {
 405                      switch($theme_id)
 406                      {
 407                          case -1:
 408                              $errors[] = $lang->error_uploadfailed_nocontents;
 409                              break;
 410                          case -2:
 411                              $errors[] = $lang->error_invalid_version;
 412                              break;
 413                          case -3:
 414                              $errors[] = $lang->error_theme_already_exists;
 415                              break;
 416                          case -4:
 417                              $errors[] = $lang->error_theme_security_problem;
 418                      }
 419                  }
 420              }
 421          }
 422      }
 423  
 424      $query = $db->simple_select("themes", "tid, name");
 425      while($theme = $db->fetch_array($query))
 426      {
 427          $themes[$theme['tid']] = $theme['name'];
 428      }
 429  
 430      $page->add_breadcrumb_item($lang->import_a_theme, "index.php?module=style-themes&amp;action=import");
 431  
 432      $page->output_header("{$lang->themes} - {$lang->import_a_theme}");
 433  
 434      $page->output_nav_tabs($sub_tabs, 'import_theme');
 435  
 436      if($errors)
 437      {
 438          $page->output_inline_error($errors);
 439  
 440          if($mybb->input['import'] == 1)
 441          {
 442              $import_checked[1] = "";
 443              $import_checked[2] = "checked=\"checked\"";
 444          }
 445          else
 446          {
 447              $import_checked[1] = "checked=\"checked\"";
 448              $import_checked[2] = "";
 449          }
 450      }
 451      else
 452      {
 453          $import_checked[1] = "checked=\"checked\"";
 454          $import_checked[2] = "";
 455  
 456          $mybb->input['import_stylesheets'] = true;
 457          $mybb->input['import_templates'] = true;
 458      }
 459  
 460      $form = new Form("index.php?module=style-themes&amp;action=import", "post", "", 1);
 461  
 462      $actions = '<script type="text/javascript">
 463  	function checkAction(id)
 464      {
 465          var checked = \'\';
 466  
 467          $(\'.\'+id+\'s_check\').each(function(e, val)
 468          {
 469              if($(this).prop(\'checked\') == true)
 470              {
 471                  checked = $(this).val();
 472              }
 473          });
 474          $(\'.\'+id+\'s\').each(function(e)
 475          {
 476              $(this).hide();
 477          });
 478          if($(\'#\'+id+\'_\'+checked))
 479          {
 480              $(\'#\'+id+\'_\'+checked).show();
 481          }
 482      }
 483  </script>
 484      <dl style="margin-top: 0; margin-bottom: 0; width: 35%;">
 485      <dt><label style="display: block;"><input type="radio" name="import" value="0" '.$import_checked[1].' class="imports_check" onclick="checkAction(\'import\');" style="vertical-align: middle;" /> '.$lang->local_file.'</label></dt>
 486          <dd style="margin-top: 0; margin-bottom: 0; width: 100%;" id="import_0" class="imports">
 487      <table cellpadding="4">
 488                  <tr>
 489                      <td>'.$form->generate_file_upload_box("local_file", array('style' => 'width: 230px;')).'</td>
 490                  </tr>
 491          </table>
 492          </dd>
 493          <dt><label style="display: block;"><input type="radio" name="import" value="1" '.$import_checked[2].' class="imports_check" onclick="checkAction(\'import\');" style="vertical-align: middle;" /> '.$lang->url.'</label></dt>
 494          <dd style="margin-top: 0; margin-bottom: 0; width: 100%;" id="import_1" class="imports">
 495          <table cellpadding="4">
 496                  <tr>
 497                      <td>'.$form->generate_text_box("url", $mybb->get_input('file')).'</td>
 498                  </tr>
 499          </table></dd>
 500      </dl>
 501      <script type="text/javascript">
 502      checkAction(\'import\');
 503      </script>';
 504  
 505      $form_container = new FormContainer($lang->import_a_theme);
 506      $form_container->output_row($lang->import_from, $lang->import_from_desc, $actions, 'file');
 507      $form_container->output_row($lang->parent_theme, $lang->parent_theme_desc, $form->generate_select_box('tid', $themes, $mybb->get_input('tid'), array('id' => 'tid')), 'tid');
 508      $form_container->output_row($lang->new_name, $lang->new_name_desc, $form->generate_text_box('name', $mybb->get_input('name'), array('id' => 'name')), 'name');
 509      $form_container->output_row($lang->advanced_options, "", $form->generate_check_box('version_compat', '1', $lang->ignore_version_compatibility, array('checked' => $mybb->get_input('version_compat'), 'id' => 'version_compat'))."<br /><small>{$lang->ignore_version_compat_desc}</small><br />".$form->generate_check_box('import_stylesheets', '1', $lang->import_stylesheets, array('checked' => $mybb->get_input('import_stylesheets'), 'id' => 'import_stylesheets'))."<br /><small>{$lang->import_stylesheets_desc}</small><br />".$form->generate_check_box('import_templates', '1', $lang->import_templates, array('checked' => $mybb->get_input('import_templates'), 'id' => 'import_templates'))."<br /><small>{$lang->import_templates_desc}</small>");
 510  
 511      $form_container->end();
 512  
 513      $buttons[] = $form->generate_submit_button($lang->import_theme);
 514  
 515      $form->output_submit_wrapper($buttons);
 516  
 517      $form->end();
 518  
 519      $page->output_footer();
 520  }
 521  
 522  if($mybb->input['action'] == "export")
 523  {
 524      $query = $db->simple_select("themes", "*", "tid='".$mybb->get_input('tid', MyBB::INPUT_INT)."'");
 525      $theme = $db->fetch_array($query);
 526  
 527      // Does the theme not exist?
 528      if(!$theme)
 529      {
 530          flash_message($lang->error_invalid_theme, 'error');
 531          admin_redirect("index.php?module=style-themes");
 532      }
 533  
 534      $plugins->run_hooks("admin_style_themes_export");
 535  
 536      if($mybb->request_method == "post")
 537      {
 538          $properties = my_unserialize($theme['properties']);
 539  
 540          $xml = "<?xml version=\"1.0\" encoding=\"{$lang->settings['charset']}\"?".">\r\n";
 541          $xml .= "<theme name=\"".htmlspecialchars_uni($theme['name'])."\" version=\"".$mybb->version_code."\">\r\n";
 542          $xml .= "\t<properties>\r\n";
 543          foreach($properties as $property => $value)
 544          {
 545              if($property == "inherited") continue;
 546  
 547              if(is_array($value))
 548              {
 549                  $value = my_serialize($value);
 550              }
 551  
 552              $value = str_replace(']]>', ']]]]><![CDATA[>', $value);
 553  
 554              $xml .= "\t\t<{$property}><![CDATA[{$value}]]></{$property}>\r\n";
 555          }
 556          $xml .= "\t</properties>\r\n";
 557  
 558          // Fetch list of all of the stylesheets for this theme
 559          $file_stylesheets = my_unserialize($theme['stylesheets']);
 560  
 561          $stylesheets = array();
 562          $inherited_load = array();
 563  
 564          // Now we loop through the list of stylesheets for each file
 565          foreach($file_stylesheets as $file => $action_stylesheet)
 566          {
 567              if($file == 'inherited' || !is_array($action_stylesheet))
 568              {
 569                  continue;
 570              }
 571  
 572              foreach($action_stylesheet as $action => $style)
 573              {
 574                  foreach($style as $stylesheet)
 575                  {
 576                      $stylesheets[$stylesheet]['applied_to'][$file][] = $action;
 577  
 578                      $array = &$file_stylesheets['inherited'][$file."_".$action];
 579  
 580                      if(is_array($array) && array_key_exists($stylesheet, $array))
 581                      {
 582                          $stylesheets[$stylesheet]['inherited'] = $file_stylesheets['inherited'][$file."_".$action];
 583                          foreach($file_stylesheets['inherited'][$file."_".$action] as $value)
 584                          {
 585                              $inherited_load[] = $value;
 586                          }
 587                      }
 588                  }
 589              }
 590          }
 591  
 592          $inherited_load[] = $mybb->input['tid'];
 593          $inherited_load = array_unique($inherited_load);
 594  
 595          $inherited_themes = $theme_stylesheets = array();
 596          if(count($inherited_load) > 0)
 597          {
 598              $query = $db->simple_select("themes", "tid, name", "tid IN (".implode(",", $inherited_load).")");
 599              while($inherited_theme = $db->fetch_array($query))
 600              {
 601                  $inherited_themes[$inherited_theme['tid']] = $inherited_theme['name'];
 602              }
 603  
 604              $query = $db->simple_select("themestylesheets", "*", "tid IN (".implode(",", $inherited_load).")", array('order_by' => 'tid', 'order_dir' => 'desc'));
 605              while($theme_stylesheet = $db->fetch_array($query))
 606              {
 607                  if(empty($theme_stylesheets[$theme_stylesheet['cachefile']]))
 608                  {
 609                      $theme_stylesheets[$theme_stylesheet['cachefile']] = $theme_stylesheet;
 610                      $theme_stylesheets[$theme_stylesheet['sid']] = $theme_stylesheet['cachefile'];
 611                  }
 612              }
 613          }
 614  
 615          $xml .= "\t<stylesheets>\r\n";
 616          foreach($stylesheets as $filename => $style)
 617          {
 618              if(strpos($filename, 'css.php?stylesheet=') !== false)
 619              {
 620                  $style['sid'] = (int)str_replace('css.php?stylesheet=', '', $filename);
 621                  $filename = $theme_stylesheets[$style['sid']];
 622              }
 623              else
 624              {
 625                  $filename = basename($filename);
 626  
 627                  if(isset($theme_stylesheets[$filename]['sid']))
 628                  {
 629                      $style['sid'] = $theme_stylesheets[$filename]['sid'];
 630                  }
 631                  else
 632                  {
 633                      $style['sid'] = null;
 634                  }
 635              }
 636  
 637              if(isset($theme_stylesheets[$filename]['tid']))
 638              {
 639                  $style['tid'] = $theme_stylesheets[$filename]['tid'];
 640              }
 641              else
 642              {
 643                  $style['tid'] = null;
 644              }
 645  
 646              if($mybb->input['custom_theme'] == 1 && $style['tid'] != $mybb->input['tid'])
 647              {
 648                  continue;
 649              }
 650  
 651              if(isset($theme_stylesheets[$filename]))
 652              {
 653                  // Has the file on the file system been modified?
 654                  resync_stylesheet($theme_stylesheets[$filename]);
 655  
 656                  $style['sid'] = $theme_stylesheets[$filename]['sid'];
 657  
 658                  $attachedto = $theme_stylesheets[$filename]['attachedto'];
 659                  $stylesheet = $theme_stylesheets[$filename]['stylesheet'];
 660                  $stylesheet = str_replace(']]>', ']]]]><![CDATA[>', $stylesheet);
 661  
 662                  if($attachedto)
 663                  {
 664                      $attachedto = "attachedto=\"{$attachedto}\" ";
 665                  }
 666  
 667                  $filename = $theme_stylesheets[$filename]['name'];
 668  
 669                  $xml .= "\t\t<stylesheet name=\"{$filename}\" {$attachedto}version=\"{$mybb->version_code}\"><![CDATA[{$stylesheet}]]>\r\n\t\t</stylesheet>\r\n";
 670              }
 671  
 672          }
 673          $xml .= "\t</stylesheets>\r\n";
 674  
 675          if($mybb->input['include_templates'] != 0)
 676          {
 677              $xml .= "\t<templates>\r\n";
 678              $query = $db->simple_select("templates", "*", "sid='".(int)$properties['templateset']."'");
 679              while($template = $db->fetch_array($query))
 680              {
 681                  $template['template'] = str_replace(']]>', ']]]]><![CDATA[>', $template['template']);
 682                  $xml .= "\t\t<template name=\"{$template['title']}\" version=\"{$template['version']}\"><![CDATA[{$template['template']}]]></template>\r\n";
 683              }
 684              $xml .= "\t</templates>\r\n";
 685          }
 686          $xml .= "</theme>";
 687  
 688          $plugins->run_hooks("admin_style_themes_export_commit");
 689  
 690          // Log admin action
 691          log_admin_action($theme['tid'], $theme['name']);
 692  
 693          $theme['name'] = rawurlencode($theme['name']);
 694          header("Content-disposition: attachment; filename=".$theme['name']."-theme.xml");
 695          header("Content-type: application/octet-stream");
 696          header("Content-Length: ".strlen($xml));
 697          header("Pragma: no-cache");
 698          header("Expires: 0");
 699          echo $xml;
 700          exit;
 701      }
 702  
 703      $page->add_breadcrumb_item(htmlspecialchars_uni($theme['name']), "index.php?module=style-themes&amp;action=edit&amp;tid={$mybb->input['tid']}");
 704  
 705      $page->add_breadcrumb_item($lang->export_theme, "index.php?module=style-themes&amp;action=export");
 706  
 707      $page->output_header("{$lang->themes} - {$lang->export_theme}");
 708  
 709      $sub_tabs['edit_stylesheets'] = array(
 710          'title' => $lang->edit_stylesheets,
 711          'link' => "index.php?module=style-themes&amp;action=edit&amp;tid={$mybb->input['tid']}",
 712      );
 713  
 714      $sub_tabs['add_stylesheet'] = array(
 715          'title' => $lang->add_stylesheet,
 716          'link' => "index.php?module=style-themes&amp;action=add_stylesheet&amp;tid={$mybb->input['tid']}",
 717      );
 718  
 719      $sub_tabs['export_theme'] = array(
 720          'title' => $lang->export_theme,
 721          'link' => "index.php?module=style-themes&amp;action=export&amp;tid={$mybb->input['tid']}",
 722          'description' => $lang->export_theme_desc
 723      );
 724  
 725      $sub_tabs['duplicate_theme'] = array(
 726          'title' => $lang->duplicate_theme,
 727          'link' => "index.php?module=style-themes&amp;action=duplicate&amp;tid={$mybb->input['tid']}",
 728          'description' => $lang->duplicate_theme_desc
 729      );
 730  
 731      $page->output_nav_tabs($sub_tabs, 'export_theme');
 732  
 733      if($errors)
 734      {
 735          $page->output_inline_error($errors);
 736      }
 737  
 738      $form = new Form("index.php?module=style-themes&amp;action=export", "post");
 739      echo $form->generate_hidden_field("tid", $theme['tid']);
 740  
 741      $form_container = new FormContainer($lang->export_theme.": ".htmlspecialchars_uni($theme['name']));
 742      $form_container->output_row($lang->include_custom_only, $lang->include_custom_only_desc, $form->generate_yes_no_radio('custom_theme', $mybb->get_input('custom_theme')), 'custom_theme');
 743      $form_container->output_row($lang->include_templates, $lang->include_templates_desc, $form->generate_yes_no_radio('include_templates', $mybb->get_input('include_templates')), 'include_templates');
 744  
 745      $form_container->end();
 746  
 747      $buttons[] = $form->generate_submit_button($lang->export_theme);
 748  
 749      $form->output_submit_wrapper($buttons);
 750  
 751      $form->end();
 752  
 753      $page->output_footer();
 754  }
 755  
 756  if($mybb->input['action'] == "duplicate")
 757  {
 758      $query = $db->simple_select("themes", "*", "tid='".$mybb->get_input('tid', MyBB::INPUT_INT)."'");
 759      $theme = $db->fetch_array($query);
 760  
 761      // Does the theme not exist?
 762      if(!$theme)
 763      {
 764          flash_message($lang->error_invalid_theme, 'error');
 765          admin_redirect("index.php?module=style-themes");
 766      }
 767  
 768      $plugins->run_hooks("admin_style_themes_duplicate");
 769  
 770      if($mybb->request_method == "post")
 771      {
 772          if($mybb->input['name'] == "")
 773          {
 774              $errors[] = $lang->error_missing_name;
 775          }
 776          else
 777          {
 778              $query = $db->simple_select("themes", "COUNT(tid) as numthemes", "name = '".$db->escape_string($mybb->get_input('name'))."'");
 779              $numthemes = $db->fetch_field($query, 'numthemes');
 780  
 781              if($numthemes)
 782              {
 783                  $errors[] = $lang->error_theme_already_exists;
 784              }
 785          }
 786  
 787          if(!$errors)
 788          {
 789              $properties = my_unserialize($theme['properties']);
 790              $sid = (int)$properties['templateset'];
 791              $nprops = null;
 792              if($mybb->get_input('duplicate_templates'))
 793              {
 794                  $nsid = $db->insert_query("templatesets", array('title' => $db->escape_string($mybb->get_input('name'))." Templates"));
 795  
 796                  // Copy all old Templates to our new templateset
 797                  $query = $db->simple_select("templates", "*", "sid='{$sid}'");
 798                  while($template = $db->fetch_array($query))
 799                  {
 800                      $insert = array(
 801                          "title" => $db->escape_string($template['title']),
 802                          "template" => $db->escape_string($template['template']),
 803                          "sid" => $nsid,
 804                          "version" => $db->escape_string($template['version']),
 805                          "dateline" => TIME_NOW
 806                      );
 807  
 808                      if($db->engine == "pgsql")
 809                      {
 810                          echo " ";
 811                          flush();
 812                      }
 813  
 814                      $db->insert_query("templates", $insert);
 815                  }
 816  
 817                  // We need to change the templateset so we need to work out the others properties too
 818                  foreach($properties as $property => $value)
 819                  {
 820                      if($property == "inherited")
 821                      {
 822                          continue;
 823                      }
 824  
 825                      $nprops[$property] = $value;
 826                      if(!empty($properties['inherited'][$property]))
 827                      {
 828                          $nprops['inherited'][$property] = $properties['inherited'][$property];
 829                      }
 830                      else
 831                      {
 832                          $nprops['inherited'][$property] = $theme['tid'];
 833                      }
 834                  }
 835                  $nprops['templateset'] = $nsid;
 836              }
 837              $tid = build_new_theme($mybb->get_input('name'), $nprops, $theme['tid']);
 838  
 839              update_theme_stylesheet_list($tid);
 840  
 841              $plugins->run_hooks("admin_style_themes_duplicate_commit");
 842  
 843              // Log admin action
 844              log_admin_action($tid, $theme['tid']);
 845  
 846              flash_message($lang->success_duplicated_theme, 'success');
 847              admin_redirect("index.php?module=style-themes&action=edit&tid=".$tid);
 848          }
 849      }
 850  
 851      $page->add_breadcrumb_item(htmlspecialchars_uni($theme['name']), "index.php?module=style-themes&amp;action=edit&amp;tid={$mybb->get_input('tid')}");
 852  
 853      $page->add_breadcrumb_item($lang->duplicate_theme, "index.php?module=style-themes&amp;action=duplicate&amp;tid={$theme['tid']}");
 854  
 855      $page->output_header("{$lang->themes} - {$lang->duplicate_theme}");
 856  
 857      $sub_tabs['edit_stylesheets'] = array(
 858          'title' => $lang->edit_stylesheets,
 859          'link' => "index.php?module=style-themes&amp;action=edit&amp;tid={$mybb->get_input('tid')}",
 860      );
 861  
 862      $sub_tabs['add_stylesheet'] = array(
 863          'title' => $lang->add_stylesheet,
 864          'link' => "index.php?module=style-themes&amp;action=add_stylesheet&amp;tid={$mybb->get_input('tid')}",
 865      );
 866  
 867      $sub_tabs['export_theme'] = array(
 868          'title' => $lang->export_theme,
 869          'link' => "index.php?module=style-themes&amp;action=export&amp;tid={$mybb->get_input('tid')}",
 870          'description' => $lang->export_theme_desc
 871      );
 872  
 873      $sub_tabs['duplicate_theme'] = array(
 874          'title' => $lang->duplicate_theme,
 875          'link' => "index.php?module=style-themes&amp;action=duplicate&amp;tid={$mybb->get_input('tid')}",
 876          'description' => $lang->duplicate_theme_desc
 877      );
 878  
 879      $page->output_nav_tabs($sub_tabs, 'duplicate_theme');
 880  
 881      if($errors)
 882      {
 883          $page->output_inline_error($errors);
 884      }
 885      else
 886      {
 887          $mybb->input['duplicate_templates'] = true;
 888      }
 889  
 890      $form = new Form("index.php?module=style-themes&amp;action=duplicate&amp;tid={$theme['tid']}", "post");
 891  
 892      $form_container = new FormContainer($lang->duplicate_theme);
 893      $form_container->output_row($lang->new_name, $lang->new_name_duplicate_desc, $form->generate_text_box('name', $mybb->get_input('name'), array('id' => 'name')), 'name');
 894      $form_container->output_row($lang->advanced_options, "", $form->generate_check_box('duplicate_templates', '1', $lang->duplicate_templates, array('checked' => $mybb->get_input('duplicate_templates'), 'id' => 'duplicate_templates'))."<br /><small>{$lang->duplicate_templates_desc}</small>");
 895  
 896      $form_container->end();
 897  
 898      $buttons[] = $form->generate_submit_button($lang->duplicate_theme);
 899  
 900      $form->output_submit_wrapper($buttons);
 901  
 902      $form->end();
 903  
 904      $page->output_footer();
 905  }
 906  
 907  if($mybb->input['action'] == "add")
 908  {
 909      $plugins->run_hooks("admin_style_themes_add");
 910  
 911      $query = $db->simple_select("themes", "tid, name");
 912      while($theme = $db->fetch_array($query))
 913      {
 914          $themes[$theme['tid']] = $theme['name'];
 915      }
 916  
 917      if($mybb->request_method == "post")
 918      {
 919          if(!$mybb->input['name'])
 920          {
 921              $errors[] = $lang->error_missing_name;
 922          }
 923          else if(in_array($mybb->input['name'], $themes))
 924          {
 925              $errors[] = $lang->error_theme_already_exists;
 926          }
 927  
 928          if(!$errors)
 929          {
 930              $tid = build_new_theme($mybb->input['name'], null, $mybb->input['tid']);
 931  
 932              $plugins->run_hooks("admin_style_themes_add_commit");
 933  
 934              // Log admin action
 935              log_admin_action($mybb->input['name'], $tid);
 936  
 937              flash_message($lang->success_theme_created, 'success');
 938              admin_redirect("index.php?module=style-themes&action=edit&tid=".$tid);
 939          }
 940      }
 941  
 942      $page->add_breadcrumb_item($lang->create_new_theme, "index.php?module=style-themes&amp;action=add");
 943  
 944      $page->output_header("{$lang->themes} - {$lang->create_new_theme}");
 945  
 946      $page->output_nav_tabs($sub_tabs, 'create_theme');
 947  
 948      if($errors)
 949      {
 950          $page->output_inline_error($errors);
 951      }
 952  
 953      $form = new Form("index.php?module=style-themes&amp;action=add", "post");
 954  
 955      $form_container = new FormContainer($lang->create_a_theme);
 956      $form_container->output_row($lang->name, $lang->name_desc, $form->generate_text_box('name', $mybb->get_input('name'), array('id' => 'name')), 'name');
 957      $form_container->output_row($lang->parent_theme, $lang->parent_theme_desc, $form->generate_select_box('tid', $themes, $mybb->get_input('tid'), array('id' => 'tid')), 'tid');
 958  
 959      $form_container->end();
 960  
 961      $buttons[] = $form->generate_submit_button($lang->create_new_theme);
 962  
 963      $form->output_submit_wrapper($buttons);
 964  
 965      $form->end();
 966  
 967      $page->output_footer();
 968  }
 969  
 970  if($mybb->input['action'] == "delete")
 971  {
 972      $query = $db->simple_select("themes", "*", "tid='".$mybb->get_input('tid', MyBB::INPUT_INT)."'");
 973      $theme = $db->fetch_array($query);
 974  
 975      // Does the theme not exist? or are we trying to delete the master?
 976      if(!$theme || $theme['tid'] == 1)
 977      {
 978          flash_message($lang->error_invalid_theme, 'error');
 979          admin_redirect("index.php?module=style-themes");
 980      }
 981  
 982      // User clicked no
 983      if($mybb->get_input('no'))
 984      {
 985          admin_redirect("index.php?module=style-themes");
 986      }
 987  
 988      $plugins->run_hooks("admin_style_themes_delete");
 989  
 990      if($mybb->request_method == "post")
 991      {
 992          $inherited_theme_cache = array();
 993  
 994          $query = $db->simple_select("themes", "tid,stylesheets", "tid != '{$theme['tid']}'", array('order_by' => "pid, name"));
 995          while($theme2 = $db->fetch_array($query))
 996          {
 997              $theme2['stylesheets'] = my_unserialize($theme2['stylesheets']);
 998  
 999              if(empty($theme2['stylesheets']['inherited']))
1000              {
1001                  continue;
1002              }
1003  
1004              $inherited_theme_cache[$theme2['tid']] = $theme2['stylesheets']['inherited'];
1005          }
1006  
1007          $inherited_stylesheets = false;
1008  
1009          // Are any other themes relying on stylesheets from this theme? Get a list and show an error
1010          foreach($inherited_theme_cache as $tid => $inherited)
1011          {
1012              foreach($inherited as $file => $value)
1013              {
1014                  foreach($value as $filepath => $val)
1015                  {
1016                      if(strpos($filepath, "cache/themes/theme{$theme['tid']}") !== false)
1017                      {
1018                          $inherited_stylesheets = true;
1019                      }
1020                  }
1021              }
1022          }
1023  
1024          if($inherited_stylesheets == true)
1025          {
1026              flash_message($lang->error_inheriting_stylesheets, 'error');
1027              admin_redirect("index.php?module=style-themes");
1028          }
1029  
1030          $query = $db->simple_select("themestylesheets", "cachefile", "tid='{$theme['tid']}'");
1031          while($cachefile = $db->fetch_array($query))
1032          {
1033              @unlink(MYBB_ROOT."cache/themes/theme{$theme['tid']}/{$cachefile['cachefile']}");
1034  
1035              $filename_min = str_replace('.css', '.min.css', $cachefile['cachefile']);
1036              @unlink(MYBB_ROOT."cache/themes/theme{$theme['tid']}/{$filename_min}");
1037          }
1038  
1039          $path = MYBB_ROOT."cache/themes/theme{$theme['tid']}/index.html";
1040          if(file_exists($path))
1041          {
1042              @unlink($path);
1043          }
1044  
1045          $db->delete_query("themestylesheets", "tid='{$theme['tid']}'");
1046  
1047          // Update the CSS file list for this theme
1048          update_theme_stylesheet_list($theme['tid'], $theme, true);
1049  
1050          $db->update_query("users", array('style' => 0), "style='{$theme['tid']}'");
1051  
1052          $path = MYBB_ROOT."cache/themes/theme{$theme['tid']}/";
1053          if(file_exists($path))
1054          {
1055              @rmdir($path);
1056          }
1057  
1058          $children = (array)make_child_theme_list($theme['tid']);
1059          $child_tids = array();
1060  
1061          foreach($children as $child_tid)
1062          {
1063              if($child_tid != 0)
1064              {
1065                  $child_tids[] = $child_tid;
1066              }
1067          }
1068  
1069          if(!empty($child_tids))
1070          {
1071              $db->update_query("themes", array('pid' => $theme['pid']), "tid IN (".implode(',', $child_tids).")");
1072          }
1073  
1074          $db->delete_query("themes", "tid='{$theme['tid']}'", 1);
1075  
1076          $plugins->run_hooks("admin_style_themes_delete_commit");
1077  
1078          // Log admin action
1079          log_admin_action($theme['tid'], $theme['name']);
1080  
1081          flash_message($lang->success_theme_deleted, 'success');
1082          admin_redirect("index.php?module=style-themes");
1083      }
1084      else
1085      {
1086          $page->output_confirm_action("index.php?module=style-themes&amp;action=delete&amp;tid={$theme['tid']}", $lang->confirm_theme_deletion);
1087      }
1088  }
1089  
1090  if($mybb->input['action'] == "edit")
1091  {
1092      $query = $db->simple_select("themes", "*", "tid='".$mybb->get_input('tid', MyBB::INPUT_INT)."'");
1093      $theme = $db->fetch_array($query);
1094  
1095      // Does the theme not exist?
1096      if(!$theme || $theme['tid'] == 1)
1097      {
1098          flash_message($lang->error_invalid_theme, 'error');
1099          admin_redirect("index.php?module=style-themes");
1100      }
1101  
1102      $plugins->run_hooks("admin_style_themes_edit");
1103  
1104      if($mybb->request_method == "post" && !$mybb->input['do'])
1105      {
1106          $properties = array(
1107              'templateset' => $mybb->get_input('templateset', MyBB::INPUT_INT),
1108              'editortheme' => $mybb->get_input('editortheme'),
1109              'imgdir' => $mybb->get_input('imgdir'),
1110              'logo' => $mybb->get_input('logo'),
1111              'tablespace' => $mybb->get_input('tablespace', MyBB::INPUT_INT),
1112              'borderwidth' => $mybb->get_input('borderwidth', MyBB::INPUT_INT),
1113              'color' => $mybb->get_input('color')
1114          );
1115  
1116          if($properties['color'] == 'none')
1117          {
1118              unset($properties['color']);
1119          }
1120  
1121          if($mybb->input['colors'])
1122          {
1123              $colors = explode("\n", $mybb->input['colors']);
1124  
1125              foreach($colors as $color)
1126              {
1127                  $color = trim($color);
1128                  if(preg_match('(^((\p{L}|\p{Nd}|_)+)={1}((\p{L}|\p{Nd}|_)+)$)u', $color))
1129                  {
1130                      $color = explode("=", $color);
1131                      $properties['colors'][$color[0]] = $color[1];
1132                  }
1133                  else
1134                  {
1135                      $errors[] = $lang->sprintf($lang->error_invalid_color, $color);
1136                  }
1137              }
1138          }
1139  
1140          if($properties['templateset'] <= 0)
1141          {
1142              $errors[] = $lang->error_invalid_templateset;
1143          }
1144  
1145          $theme_properties = my_unserialize($theme['properties']);
1146          if(is_array($theme_properties['disporder']))
1147          {
1148              $properties['disporder'] = $theme_properties['disporder'];
1149          }
1150          else
1151          {
1152              $errors[] = $lang->error_no_display_order;
1153          }
1154  
1155          $allowedgroups = array();
1156          if(is_array($mybb->input['allowedgroups']))
1157          {
1158              foreach($mybb->input['allowedgroups'] as $gid)
1159              {
1160                  if($gid == "all")
1161                  {
1162                      $allowedgroups = "all";
1163                      break;
1164                  }
1165                  $gid = (int)$gid;
1166                  $allowedgroups[$gid] = $gid;
1167              }
1168          }
1169          if(is_array($allowedgroups))
1170          {
1171              $allowedgroups = implode(",", $allowedgroups);
1172          }
1173  
1174          $update_array = array(
1175              'name' => $db->escape_string($mybb->input['name']),
1176              'pid' => $mybb->get_input('pid', MyBB::INPUT_INT),
1177              'allowedgroups' => $allowedgroups,
1178              'properties' => $db->escape_string(my_serialize($properties))
1179          );
1180  
1181          // Perform validation
1182          if(!$update_array['name'])
1183          {
1184              $errors[] = $lang->error_missing_name;
1185          }
1186          else
1187          {
1188              $query = $db->simple_select("themes", "COUNT(tid) as numthemes", "name = '".$db->escape_string($update_array['name'])."' AND tid != '{$theme['tid']}'");
1189              $numthemes = $db->fetch_field($query, 'numthemes');
1190  
1191              if($numthemes)
1192              {
1193                  $errors[] = $lang->error_theme_already_exists;
1194              }
1195          }
1196  
1197          if($update_array['pid'])
1198          {
1199              $query = $db->simple_select("themes", "tid", "tid='".$update_array['pid']."'");
1200              $parent_check = $db->fetch_field($query, "tid");
1201              if(!$parent_check)
1202              {
1203                  $errors[] = $lang->error_invalid_parent_theme;
1204              }
1205          }
1206          if($properties['templateset'])
1207          {
1208              $query = $db->simple_select("templatesets", "sid", "sid='".(int)$properties['templateset']."'");
1209              $ts_check = $db->fetch_field($query, "sid");
1210              if(!$ts_check)
1211              {
1212                  unset($properties['templateset']);
1213              }
1214          }
1215          if(!$properties['templateset'])
1216          {
1217              $errors[] = $lang->error_invalid_templateset;
1218          }
1219          if(!$properties['editortheme'] || !file_exists(MYBB_ROOT."jscripts/sceditor/themes/".$properties['editortheme']) || is_dir(MYBB_ROOT."jscripts/sceditor/themes/".$properties['editortheme']))
1220          {
1221              $errors[] = $lang->error_invalid_editortheme;
1222          }
1223  
1224          if(empty($errors))
1225          {
1226              $plugins->run_hooks("admin_style_themes_edit_commit");
1227  
1228              $db->update_query("themes", $update_array, "tid='{$theme['tid']}'");
1229              update_theme_stylesheet_list($theme['tid']);
1230  
1231              if($theme['def'] == 1)
1232              {
1233                  $cache->update_default_theme();
1234              }
1235  
1236              // Log admin action
1237              log_admin_action($theme['tid'], $theme['name']);
1238  
1239              flash_message($lang->success_theme_properties_updated, 'success');
1240              admin_redirect("index.php?module=style-themes&action=edit&tid={$theme['tid']}");
1241          }
1242      }
1243  
1244      // Fetch list of all of the stylesheets for this theme
1245      $file_stylesheets = my_unserialize($theme['stylesheets']);
1246  
1247      $stylesheets = array();
1248      $inherited_load = array();
1249  
1250      // Now we loop through the list of stylesheets for each file
1251      foreach($file_stylesheets as $file => $action_stylesheet)
1252      {
1253          if($file == 'inherited' || !is_array($action_stylesheet))
1254          {
1255              continue;
1256          }
1257  
1258          foreach($action_stylesheet as $action => $style)
1259          {
1260              foreach($style as $stylesheet)
1261              {
1262                  $stylesheets[$stylesheet]['applied_to'][$file][] = $action;
1263                  if(isset($file_stylesheets['inherited'][$file."_".$action]) && is_array($file_stylesheets['inherited'][$file."_".$action]) && in_array($stylesheet, array_keys($file_stylesheets['inherited'][$file."_".$action])))
1264                  {
1265                      $stylesheets[$stylesheet]['inherited'] = $file_stylesheets['inherited'][$file."_".$action];
1266                      foreach($file_stylesheets['inherited'][$file."_".$action] as $value)
1267                      {
1268                          $inherited_load[] = $value;
1269                      }
1270                  }
1271              }
1272          }
1273      }
1274  
1275      $inherited_load[] = $mybb->input['tid'];
1276      $inherited_load = array_unique($inherited_load);
1277  
1278      $inherited_themes = $theme_stylesheets = array();
1279      if(count($inherited_load) > 0)
1280      {
1281          $query = $db->simple_select("themes", "tid, name", "tid IN (".implode(",", $inherited_load).")");
1282          while($inherited_theme = $db->fetch_array($query))
1283          {
1284              $inherited_themes[$inherited_theme['tid']] = $inherited_theme['name'];
1285          }
1286  
1287          $query = $db->simple_select("themestylesheets", "*", "", array('order_by' => 'sid DESC, tid', 'order_dir' => 'desc'));
1288          while($theme_stylesheet = $db->fetch_array($query))
1289          {
1290              if(!isset($theme_stylesheets[$theme_stylesheet['name']]) && in_array($theme_stylesheet['tid'], $inherited_load))
1291              {
1292                  $theme_stylesheets[$theme_stylesheet['name']] = $theme_stylesheet;
1293              }
1294  
1295              $theme_stylesheets[$theme_stylesheet['sid']] = $theme_stylesheet['name'];
1296          }
1297      }
1298  
1299      // Save any stylesheet orders
1300      if($mybb->request_method == "post" && $mybb->input['do'] == "save_orders")
1301      {
1302          if(!is_array($mybb->input['disporder']))
1303          {
1304              // Error out
1305              flash_message($lang->error_no_display_order, 'error');
1306              admin_redirect("index.php?module=style-themes&action=edit&tid={$theme['tid']}");
1307          }
1308  
1309          $orders = array();
1310          foreach($theme_stylesheets as $stylesheet => $properties)
1311          {
1312              if(is_array($properties))
1313              {
1314                  $order = (int)$mybb->input['disporder'][$properties['sid']];
1315  
1316                  $orders[$properties['name']] = $order;
1317              }
1318          }
1319  
1320          asort($orders, SORT_NUMERIC);
1321  
1322          // Save the orders in the theme properties
1323          $properties = my_unserialize($theme['properties']);
1324          $properties['disporder'] = $orders;
1325  
1326          $update_array = array(
1327              "properties" => $db->escape_string(my_serialize($properties))
1328          );
1329  
1330          $db->update_query("themes", $update_array, "tid = '{$theme['tid']}'");
1331  
1332          if($theme['def'] == 1)
1333          {
1334              $cache->update_default_theme();
1335          }
1336  
1337          // Normalize for consistency
1338          update_theme_stylesheet_list($theme['tid'], false, true);
1339  
1340          flash_message($lang->success_stylesheet_order_updated, 'success');
1341          admin_redirect("index.php?module=style-themes&action=edit&tid={$theme['tid']}");
1342      }
1343  
1344      $page->add_breadcrumb_item(htmlspecialchars_uni($theme['name']), "index.php?module=style-themes&amp;action=edit&amp;tid={$mybb->input['tid']}");
1345  
1346      $page->output_header("{$lang->themes} - {$lang->stylesheets}");
1347  
1348      $sub_tabs['edit_stylesheets'] = array(
1349          'title' => $lang->edit_stylesheets,
1350          'link' => "index.php?module=style-themes&amp;action=edit&amp;tid={$mybb->input['tid']}",
1351          'description' => $lang->edit_stylesheets_desc
1352      );
1353  
1354      $sub_tabs['add_stylesheet'] = array(
1355          'title' => $lang->add_stylesheet,
1356          'link' => "index.php?module=style-themes&amp;action=add_stylesheet&amp;tid={$mybb->input['tid']}",
1357      );
1358  
1359      $sub_tabs['export_theme'] = array(
1360          'title' => $lang->export_theme,
1361          'link' => "index.php?module=style-themes&amp;action=export&amp;tid={$mybb->input['tid']}"
1362      );
1363  
1364      $sub_tabs['duplicate_theme'] = array(
1365          'title' => $lang->duplicate_theme,
1366          'link' => "index.php?module=style-themes&amp;action=duplicate&amp;tid={$mybb->input['tid']}",
1367          'description' => $lang->duplicate_theme_desc
1368      );
1369  
1370      $properties = my_unserialize($theme['properties']);
1371      $page->output_nav_tabs($sub_tabs, 'edit_stylesheets');
1372  
1373      $table = new Table;
1374      $table->construct_header($lang->stylesheets);
1375      $table->construct_header($lang->display_order, array("class" => "align_center", "width" => 50));
1376      $table->construct_header($lang->controls, array("class" => "align_center", "width" => 150));
1377  
1378      // Display Order form
1379      $form = new Form("index.php?module=style-themes&amp;action=edit", "post", "edit");
1380      echo $form->generate_hidden_field("tid", $theme['tid']);
1381      echo $form->generate_hidden_field("do", 'save_orders');
1382  
1383      // Order the stylesheets
1384      $ordered_stylesheets = array();
1385  
1386      foreach($properties['disporder'] as $style_name => $order)
1387      {
1388          foreach($stylesheets as $filename => $style)
1389          {
1390              if(strpos($filename, 'css.php?stylesheet=') !== false)
1391              {
1392                  $style['sid'] = (int)str_replace('css.php?stylesheet=', '', $filename);
1393                  $filename = $theme_stylesheets[$style['sid']];
1394              }
1395  
1396              if(basename($filename) != $style_name)
1397              {
1398                  continue;
1399              }
1400  
1401              $ordered_stylesheets[$filename] = $style;
1402          }
1403      }
1404  
1405      foreach($ordered_stylesheets as $filename => $style)
1406      {
1407          if(strpos($filename, 'css.php?stylesheet=') !== false)
1408          {
1409              $style['sid'] = (int)str_replace('css.php?stylesheet=', '', $filename);
1410              $filename = $theme_stylesheets[$style['sid']];
1411          }
1412          else
1413          {
1414              $filename = basename($filename);
1415              $style['sid'] = $theme_stylesheets[$filename]['sid'];
1416          }
1417  
1418          // Has the file on the file system been modified?
1419          resync_stylesheet($theme_stylesheets[$filename]);
1420  
1421          $filename = $theme_stylesheets[$filename]['name'];
1422  
1423          $inherited = "";
1424          $inherited_ary = array();
1425          if(isset($style['inherited']) && is_array($style['inherited']))
1426          {
1427              foreach($style['inherited'] as $tid)
1428              {
1429                  if($inherited_themes[$tid])
1430                  {
1431                      $inherited_ary[$tid] = $inherited_themes[$tid];
1432                  }
1433              }
1434          }
1435  
1436          if(!empty($inherited_ary))
1437          {
1438              $inherited = " <small>({$lang->inherited_from}";
1439              $sep = " ";
1440              $inherited_count = count($inherited_ary);
1441              $count = 0;
1442  
1443              foreach($inherited_ary as $tid => $file)
1444              {
1445                  if(isset($applied_to_count) && $count == $applied_to_count && $count != 0)
1446                  {
1447                      $sep = " {$lang->and} ";
1448                  }
1449  
1450                  $inherited .= $sep.htmlspecialchars_uni($file);
1451                  $sep = $lang->comma;
1452  
1453                  ++$count;
1454              }
1455              $inherited .= ")</small>";
1456          }
1457  
1458          if(is_array($style['applied_to']) && (!isset($style['applied_to']['global']) || $style['applied_to']['global'][0] != "global"))
1459          {
1460              $attached_to = '';
1461  
1462              $applied_to_count = count($style['applied_to']);
1463              $count = 0;
1464              $sep = " ";
1465              $name = "";
1466  
1467              $colors = array();
1468  
1469              if(!isset($properties['colors']) || !is_array($properties['colors']))
1470              {
1471                  $properties['colors'] = array();
1472              }
1473  
1474              foreach($style['applied_to'] as $name => $actions)
1475              {
1476                  if(!$name)
1477                  {
1478                      continue;
1479                  }
1480  
1481                  if(array_key_exists($name, $properties['colors']))
1482                  {
1483                      $colors[] = $properties['colors'][$name];
1484                  }
1485  
1486                  if(count($colors))
1487                  {
1488                      // Colors override files and are handled below.
1489                      continue;
1490                  }
1491  
1492                  // It's a file:
1493                  ++$count;
1494  
1495                  $name = htmlspecialchars_uni($name);
1496  
1497                  if($actions[0] != "global")
1498                  {
1499                      $actions = array_map('htmlspecialchars_uni', $actions);
1500  
1501                      $name = "{$name} ({$lang->actions}: ".implode(',', $actions).")";
1502                  }
1503  
1504                  if($count == $applied_to_count && $count > 1)
1505                  {
1506                      $sep = " {$lang->and} ";
1507                  }
1508                  $attached_to .= $sep.$name;
1509  
1510                  $sep = $lang->comma;
1511              }
1512  
1513              if($attached_to)
1514              {
1515                  $attached_to = "<small>{$lang->attached_to} {$attached_to}</small>";
1516              }
1517  
1518              if(count($colors))
1519              {
1520                  // Attached to color instead of files.
1521                  $count = 1;
1522                  $color_list = $sep = '';
1523  
1524                  foreach($colors as $color)
1525                  {
1526                      if($count == count($colors) && $count > 1)
1527                      {
1528                          $sep = " {$lang->and} ";
1529                      }
1530  
1531                      $color_list .= $sep.trim($color);
1532                      ++$count;
1533  
1534                      $sep = ', ';
1535                  }
1536  
1537                  $attached_to = "<small>{$lang->attached_to} ".$lang->sprintf($lang->colors_attached_to)." {$color_list}</small>";
1538              }
1539  
1540              if($attached_to == '')
1541              {
1542                  // Orphaned! :(
1543                  $attached_to = "<small>{$lang->attached_to_nothing}</small>";
1544              }
1545          }
1546          else
1547          {
1548              $attached_to = "<small>{$lang->attached_to_all_pages}</small>";
1549          }
1550  
1551          $popup = new PopupMenu("style_{$style['sid']}", $lang->options);
1552  
1553          $popup->add_item($lang->edit_style, "index.php?module=style-themes&amp;action=edit_stylesheet&amp;file=".htmlspecialchars_uni($filename)."&amp;tid={$theme['tid']}");
1554          $popup->add_item($lang->properties, "index.php?module=style-themes&amp;action=stylesheet_properties&amp;file=".htmlspecialchars_uni($filename)."&amp;tid={$theme['tid']}");
1555  
1556          if($inherited == "")
1557          {
1558              $popup->add_item($lang->delete_revert, "index.php?module=style-themes&amp;action=delete_stylesheet&amp;file=".htmlspecialchars_uni($filename)."&amp;tid={$theme['tid']}&amp;my_post_key={$mybb->post_code}", "return AdminCP.deleteConfirmation(this, '{$lang->confirm_stylesheet_deletion}')");
1559          }
1560  
1561          $table->construct_cell("<strong><a href=\"index.php?module=style-themes&amp;action=edit_stylesheet&amp;file=".htmlspecialchars_uni($filename)."&amp;tid={$theme['tid']}\">".htmlspecialchars_uni($filename)."</a></strong>{$inherited}<br />{$attached_to}");
1562          $table->construct_cell($form->generate_numeric_field("disporder[{$theme_stylesheets[$filename]['sid']}]", $properties['disporder'][$filename], array('style' => 'width: 80%; text-align: center;', 'min' => 0)), array("class" => "align_center"));
1563          $table->construct_cell($popup->fetch(), array("class" => "align_center"));
1564          $table->construct_row();
1565      }
1566  
1567      $table->output("{$lang->stylesheets_in} ".htmlspecialchars_uni($theme['name']));
1568  
1569      $buttons = array($form->generate_submit_button($lang->save_stylesheet_order));
1570      $form->output_submit_wrapper($buttons);
1571      $form->end();
1572  
1573      echo '<br />';
1574  
1575      // Theme Properties table
1576      if($errors)
1577      {
1578          $page->output_inline_error($errors);
1579      }
1580  
1581      $form = new Form("index.php?module=style-themes&amp;action=edit", "post", "edit");
1582      echo $form->generate_hidden_field("tid", $theme['tid']);
1583      $form_container = new FormContainer($lang->edit_theme_properties);
1584      $form_container->output_row($lang->name." <em>*</em>", $lang->name_desc_edit, $form->generate_text_box('name', $theme['name'], array('id' => 'name')), 'name');
1585  
1586      $options = build_theme_array($theme['tid']);
1587      $form_container->output_row($lang->parent_theme." <em>*</em>", $lang->parent_theme_desc, $form->generate_select_box('pid', $options, $theme['pid'], array('id' => 'pid')), 'pid');
1588  
1589      $options = array();
1590      $query = $db->simple_select("usergroups", "gid, title", "gid != '1'", array('order_by' => 'title'));
1591      $options['all'] = $lang->all_user_groups;
1592      while($usergroup = $db->fetch_array($query))
1593      {
1594          $options[(int)$usergroup['gid']] = $usergroup['title'];
1595      }
1596      $form_container->output_row($lang->allowed_user_groups, $lang->allowed_user_groups_desc, $form->generate_select_box('allowedgroups[]', $options, explode(",", $theme['allowedgroups']), array('id' => 'allowedgroups', 'multiple' => true, 'size' => 5)), 'allowedgroups');
1597  
1598      $options = array();
1599      $query = $db->simple_select("templatesets", "*", "", array('order_by' => 'title'));
1600      while($templateset = $db->fetch_array($query))
1601      {
1602          $options[(int)$templateset['sid']] = $templateset['title'];
1603      }
1604      $form_container->output_row($lang->template_set." <em>*</em>", $lang->template_set_desc, $form->generate_select_box('templateset', $options, $properties['templateset'], array('id' => 'templateset')), 'templateset');
1605  
1606      $options = array();
1607      $editor_theme_root = MYBB_ROOT."jscripts/sceditor/themes/";
1608      if($dh = @opendir($editor_theme_root))
1609      {
1610          while($dir = readdir($dh))
1611          {
1612              if($dir == ".svn" || $dir == "." || $dir == ".." || is_dir($editor_theme_root.$dir) || get_extension($editor_theme_root.$dir) != 'css')
1613              {
1614                  continue;
1615              }
1616              $options[$dir] = ucfirst(str_replace(array('_', '.css'), array(' ', ''), $dir));
1617          }
1618      }
1619  
1620      $form_container->output_row($lang->editor_theme." <em>*</em>", $lang->editor_theme_desc, $form->generate_select_box('editortheme', $options, $properties['editortheme'], array('id' => 'editortheme')), 'editortheme');
1621  
1622      $form_container->output_row($lang->img_directory, $lang->img_directory_desc, $form->generate_text_box('imgdir', $properties['imgdir'], array('id' => 'imgdir')), 'imgdir');
1623      $form_container->output_row($lang->logo, $lang->logo_desc, $form->generate_text_box('logo', $properties['logo'], array('id' => 'boardlogo')), 'logo');
1624      $form_container->output_row($lang->table_spacing, $lang->table_spacing_desc, $form->generate_numeric_field('tablespace', $properties['tablespace'], array('id' => 'tablespace', 'min' => 0)), 'tablespace');
1625      $form_container->output_row($lang->inner_border, $lang->inner_border_desc, $form->generate_numeric_field('borderwidth', $properties['borderwidth'], array('id' => 'borderwidth', 'min' => 0)), 'borderwidth');
1626  
1627      $form_container->end();
1628  
1629      $form_container = new FormContainer($lang->colors_manage);
1630  
1631      if(empty($properties['colors']) || !is_array($properties['colors']))
1632      {
1633          $color_setting = $lang->colors_no_color_setting;
1634      }
1635      else
1636      {
1637          $colors = array('none' => $lang->colors_please_select);
1638          $colors = array_merge($colors, $properties['colors']);
1639  
1640          if(!isset($properties['color']))
1641          {
1642              $properties['color'] = 'none';
1643          }
1644          $color_setting = $form->generate_select_box('color', $colors, $properties['color'], array('class' => "select\" style=\"width: 200px;"));
1645  
1646          $mybb->input['colors'] = '';
1647          foreach($properties['colors'] as $key => $color)
1648          {
1649              if($mybb->input['colors'])
1650              {
1651                  $mybb->input['colors'] .= "\n";
1652              }
1653  
1654              $mybb->input['colors'] .= "{$key}={$color}";
1655          }
1656      }
1657  
1658      $form_container->output_row($lang->colors_setting, $lang->colors_setting_desc, $color_setting, 'color');
1659      $form_container->output_row($lang->colors_add, $lang->colors_add_desc, $form->generate_text_area('colors', $mybb->get_input('colors'), array('style' => 'width: 200px;', 'rows' => '5')));
1660  
1661      $form_container->end();
1662  
1663      $buttons = array();
1664      $buttons[] = $form->generate_submit_button($lang->save_theme_properties);
1665      $form->output_submit_wrapper($buttons);
1666      $form->end();
1667  
1668      $page->output_footer();
1669  }
1670  
1671  if($mybb->input['action'] == "stylesheet_properties")
1672  {
1673      // Fetch the theme we want to edit this stylesheet in
1674      $query = $db->simple_select("themes", "*", "tid='".$mybb->get_input('tid', MyBB::INPUT_INT)."'");
1675      $theme = $db->fetch_array($query);
1676  
1677      if(!$theme || $theme['tid'] == 1)
1678      {
1679          flash_message($lang->error_invalid_theme, 'error');
1680          admin_redirect("index.php?module=style-themes");
1681      }
1682  
1683      $plugins->run_hooks("admin_style_themes_stylesheet_properties");
1684  
1685      $parent_list = make_parent_theme_list($theme['tid']);
1686      $parent_list = implode(',', $parent_list);
1687      if(!$parent_list)
1688      {
1689          $parent_list = 1;
1690      }
1691  
1692      $query = $db->simple_select("themestylesheets", "*", "name='".$db->escape_string($mybb->input['file'])."' AND tid IN ({$parent_list})", array('order_by' => 'tid', 'order_dir' => 'desc', 'limit' => 1));
1693      $stylesheet = $db->fetch_array($query);
1694  
1695      // Does the theme not exist?
1696      if(!$stylesheet)
1697      {
1698          flash_message($lang->error_invalid_stylesheet, 'error');
1699          admin_redirect("index.php?module=style-themes");
1700      }
1701  
1702      // Fetch list of all of the stylesheets for this theme
1703      $stylesheets = fetch_theme_stylesheets($theme);
1704  
1705      if(!array_key_exists($stylesheet['cachefile'], $stylesheets) && array_key_exists("css.php?stylesheet=".$stylesheet['tid'], $stylesheets))
1706      {
1707          $stylesheet['cachefile'] = "css.php?stylesheet=".$stylesheet['tid'];
1708      }
1709  
1710      $this_stylesheet = $stylesheets[$stylesheet['cachefile']];
1711      unset($stylesheets);
1712  
1713      if($mybb->request_method == "post")
1714      {
1715          // Do we not have a name, or is it just an extension?
1716          if(!$mybb->input['name'] || $mybb->input['name'] == ".css")
1717          {
1718              $errors[] = $lang->error_missing_stylesheet_name;
1719          }
1720  
1721          // Get 30 chars only because we don't want more than that
1722          $mybb->input['name'] = my_substr($mybb->input['name'], 0, 30);
1723          if(get_extension($mybb->input['name']) != "css")
1724          {
1725              // Does not end with '.css'
1726              $errors[] = $lang->sprintf(
1727                  $lang->error_missing_stylesheet_extension,
1728                  htmlspecialchars_uni($mybb->input['name'])
1729              );
1730          }
1731  
1732          if(!$errors)
1733          {
1734              // Theme & stylesheet theme ID do not match, editing inherited - we copy to local theme
1735              if($theme['tid'] != $stylesheet['tid'])
1736              {
1737                  $stylesheet['sid'] = copy_stylesheet_to_theme($stylesheet, $theme['tid']);
1738              }
1739  
1740              $attached = array();
1741              if($mybb->input['attach'] == 1)
1742              {
1743                  // Our stylesheet is attached to custom pages in MyBB
1744                  foreach($mybb->input as $id => $value)
1745                  {
1746                      $actions_list = "";
1747                      $attached_to = $value;
1748  
1749                      if(strpos($id, 'attached_') !== false)
1750                      {
1751                          // We have a custom attached file
1752                          $attached_id = (int)str_replace('attached_', '', $id);
1753  
1754                          if($mybb->input['action_'.$attached_id] == 1)
1755                          {
1756                              // We have custom actions for attached files
1757                              $actions_list = $mybb->input['action_list_'.$attached_id];
1758                          }
1759  
1760                          if($actions_list)
1761                          {
1762                              $attached_to .= "?".$actions_list;
1763                          }
1764  
1765                          $attached[] = $attached_to;
1766                      }
1767                  }
1768              }
1769              else if($mybb->input['attach'] == 2)
1770              {
1771                  if(!is_array($mybb->input['color']))
1772                  {
1773                      $errors[] = $lang->error_no_color_picked;
1774                  }
1775                  else
1776                  {
1777                      $attached = $mybb->input['color'];
1778                  }
1779              }
1780  
1781              // Update Stylesheet
1782              $update_array = array(
1783                  'name' => $db->escape_string($mybb->input['name']),
1784                  'attachedto' => $db->escape_string(implode('|', $attached))
1785              );
1786  
1787              if($stylesheet['name'] != $mybb->input['name'])
1788              {
1789                  $update_array['cachefile'] = $db->escape_string(str_replace('/', '', $mybb->input['name']));
1790              }
1791  
1792              $db->update_query("themestylesheets", $update_array, "sid='{$stylesheet['sid']}'", 1);
1793  
1794              // If the name changed, re-cache our stylesheet
1795              $theme_c = $update_d = false;
1796              if($stylesheet['name'] != $mybb->input['name'])
1797              {
1798                  // Update the theme stylesheet list if the name is changed
1799                  $theme_c = $theme;
1800                  $update_d = true;
1801  
1802                  $db->update_query("themestylesheets", array('lastmodified' => TIME_NOW), "sid='{$stylesheet['sid']}'", 1);
1803                  if(!cache_stylesheet($theme['tid'], str_replace('/', '', $mybb->input['name']), $stylesheet['stylesheet']))
1804                  {
1805                      $db->update_query("themestylesheets", array('cachefile' => "css.php?stylesheet={$stylesheet['sid']}"), "sid='{$stylesheet['sid']}'", 1);
1806                  }
1807                  @unlink(MYBB_ROOT."cache/themes/theme{$theme['tid']}/{$stylesheet['cachefile']}");
1808  
1809                  $filename_min = str_replace('.css', '.min.css', $stylesheet['cachefile']);
1810                  @unlink(MYBB_ROOT."cache/themes/theme{$theme['tid']}/{$filename_min}");
1811              }
1812  
1813              // Update the CSS file list for this theme
1814              update_theme_stylesheet_list($theme['tid'], $theme_c, $update_d);
1815  
1816              $plugins->run_hooks("admin_style_themes_stylesheet_properties_commit");
1817  
1818              // Log admin action
1819              log_admin_action($stylesheet['sid'], $mybb->input['name'], $theme['tid'], $theme['name']);
1820  
1821              flash_message($lang->success_stylesheet_properties_updated, 'success');
1822              admin_redirect("index.php?module=style-themes&action=edit&tid={$theme['tid']}");
1823          }
1824      }
1825  
1826      $properties = my_unserialize($theme['properties']);
1827      $page->add_breadcrumb_item(htmlspecialchars_uni($theme['name']), "index.php?module=style-themes&amp;action=edit&amp;tid={$mybb->input['tid']}");
1828      $page->add_breadcrumb_item(htmlspecialchars_uni($stylesheet['name'])." {$lang->properties}", "index.php?module=style-themes&amp;action=edit_properties&amp;tid={$mybb->input['tid']}");
1829  
1830      $page->output_header("{$lang->themes} - {$lang->stylesheet_properties}");
1831  
1832      // If the stylesheet and theme do not match, we must be editing something that is inherited
1833      if($this_stylesheet['inherited'][$stylesheet['name']])
1834      {
1835          $query = $db->simple_select("themes", "name", "tid='{$stylesheet['tid']}'");
1836          $stylesheet_parent = htmlspecialchars_uni($db->fetch_field($query, 'name'));
1837  
1838          // Show inherited warning
1839          if($stylesheet['tid'] == 1)
1840          {
1841              $page->output_alert($lang->sprintf($lang->stylesheet_inherited_default, $stylesheet_parent));
1842          }
1843          else
1844          {
1845              $page->output_alert($lang->sprintf($lang->stylesheet_inherited, $stylesheet_parent));
1846          }
1847      }
1848  
1849      $applied_to = $this_stylesheet['applied_to'];
1850      unset($this_stylesheet);
1851  
1852      if($errors)
1853      {
1854          $page->output_inline_error($errors);
1855  
1856          foreach($mybb->input as $name => $value)
1857          {
1858              if(strpos($name, "attached") !== false)
1859              {
1860                  list(, $id) = explode('_', $name);
1861                  $id = (int)$id;
1862  
1863                  $applied_to[$value] = array(0 => 'global');
1864  
1865                  if($mybb->input['action_'.$id] == 1)
1866                  {
1867                      $applied_to[$value] = explode(',', $mybb->input['action_list_'.$id]);
1868                  }
1869              }
1870          }
1871      }
1872      else
1873      {
1874          $mybb->input['name'] = $stylesheet['name'];
1875      }
1876  
1877      $global_checked[1] = "checked=\"checked\"";
1878      $global_checked[2] = "";
1879      $global_checked[3] = "";
1880  
1881      $form = new Form("index.php?module=style-themes&amp;action=stylesheet_properties", "post");
1882  
1883      $specific_files = "<div id=\"attach_1\" class=\"attachs\">";
1884      $count = 0;
1885      if(is_array($applied_to) && (!isset($applied_to['global']) || $applied_to['global'][0] != "global"))
1886      {
1887          $check_actions = "";
1888          $stylesheet['colors'] = array();
1889  
1890          if(!is_array($properties['colors']))
1891          {
1892              $properties['colors'] = array();
1893          }
1894  
1895          foreach($applied_to as $name => $actions)
1896          {
1897              // Verify this is a color for this theme
1898              if(array_key_exists($name, $properties['colors']))
1899              {
1900                  $stylesheet['colors'][] = $name;
1901              }
1902  
1903              if(count($stylesheet['colors']))
1904              {
1905                  // Colors override files and are handled below.
1906                  continue;
1907              }
1908  
1909              // It's a file:
1910              $action_list = "";
1911              if($actions[0] != "global")
1912              {
1913                  $action_list = implode(',', $actions);
1914              }
1915  
1916              if($actions[0] == "global")
1917              {
1918                  $global_action_checked[1] = "checked=\"checked\"";
1919                  $global_action_checked[2] = "";
1920              }
1921              else
1922              {
1923                  $global_action_checked[2] = "checked=\"checked\"";
1924                  $global_action_checked[1] = "";
1925              }
1926  
1927              $specific_file = "<dl style=\"margin-top: 0; margin-bottom: 0; width: 100%;\">
1928      <dt><label style=\"display: block;\"><input type=\"radio\" name=\"action_{$count}\" value=\"0\" {$global_action_checked[1]} class=\"action_{$count}s_check\" onclick=\"checkAction('action_{$count}');\" style=\"vertical-align: middle;\" /> {$lang->globally}</label></dt>
1929          <dt><label style=\"display: block;\"><input type=\"radio\" name=\"action_{$count}\" value=\"1\" {$global_action_checked[2]} class=\"action_{$count}s_check\" onclick=\"checkAction('action_{$count}');\" style=\"vertical-align: middle;\" /> {$lang->specific_actions}</label></dt>
1930              <dd style=\"margin-top: 4px;\" id=\"action_{$count}_1\" class=\"action_{$count}s\">
1931              <small class=\"description\">{$lang->specific_actions_desc}</small>
1932              <table cellpadding=\"4\">
1933                  <tr>
1934                      <td>".$form->generate_text_box('action_list_'.$count, $action_list, array('id' => 'action_list_'.$count, 'style' => 'width: 190px;'))."</td>
1935                  </tr>
1936              </table>
1937          </dd>
1938          </dl>";
1939  
1940              $form_container = new FormContainer();
1941              $form_container->output_row("", "", "<span style=\"float: right;\"><a href=\"\" id=\"delete_img_{$count}\"><img src=\"styles/{$page->style}/images/icons/cross.png\" alt=\"{$lang->delete}\" title=\"{$lang->delete}\" /></a></span>{$lang->file} &nbsp;".$form->generate_text_box("attached_{$count}", $name, array('id' => "attached_{$count}", 'style' => 'width: 200px;')), "attached_{$count}");
1942  
1943              $form_container->output_row("", "", $specific_file);
1944  
1945              $specific_files .= "<div id=\"attached_form_{$count}\">".$form_container->end(true)."</div><div id=\"attach_box_".($count+1)."\"></div>";
1946  
1947              $check_actions .= "\n\tcheckAction('action_{$count}');";
1948  
1949              ++$count;
1950          }
1951  
1952          if($check_actions)
1953          {
1954              $global_checked[3] = "";
1955              $global_checked[2] = "checked=\"checked\"";
1956              $global_checked[1] = "";
1957          }
1958  
1959          if(!empty($stylesheet['colors']))
1960          {
1961              $global_checked[3] = "checked=\"checked\"";
1962              $global_checked[2] = "";
1963              $global_checked[1] = "";
1964          }
1965      }
1966  
1967      $specific_files .= "</div>";
1968  
1969      // Colors
1970      $specific_colors = $specific_colors_option = '';
1971  
1972      if(is_array($properties['colors']))
1973      {
1974          $specific_colors = "<div id=\"attach_2\" class=\"attachs\">";
1975          $specific_colors_option = '<dt><label style="display: block;"><input type="radio" name="attach" value="2" '.$global_checked[3].' class="attachs_check" onclick="checkAction(\'attach\');" style="vertical-align: middle;" /> '.$lang->colors_specific_color.'</label></dt><br />';
1976  
1977          $specific_color = "
1978              <small>{$lang->colors_add_edit_desc}</small>
1979              <br /><br />
1980              ".$form->generate_select_box('color[]', $properties['colors'], $stylesheet['colors'], array('multiple' => true, 'size' => "5\" style=\"width: 200px;"))."
1981          ";
1982  
1983          $form_container = new FormContainer();
1984          $form_container->output_row("", "", $specific_color);
1985          $specific_colors .= $form_container->end(true)."</div>";
1986      }
1987  
1988      $actions = '<script type="text/javascript">
1989  	function checkAction(id)
1990      {
1991          var checked = \'\';
1992  
1993          $(\'.\'+id+\'s_check\').each(function(e, val)
1994          {
1995              if($(this).prop(\'checked\') == true)
1996              {
1997                  checked = $(this).val();
1998              }
1999          });
2000          $(\'.\'+id+\'s\').each(function(e)
2001          {
2002              $(this).hide();
2003          });
2004          if($(\'#\'+id+\'_\'+checked))
2005          {
2006              $(\'#\'+id+\'_\'+checked).show();
2007          }
2008      }
2009  </script>
2010      <dl style="margin-top: 0; margin-bottom: 0; width: 40%;">
2011          <dt><label style="display: block;"><input type="radio" name="attach" value="0" '.$global_checked[1].' class="attachs_check" onclick="checkAction(\'attach\');" style="vertical-align: middle;" /> '.$lang->globally.'</label></dt><br />
2012          <dt><label style="display: block;"><input type="radio" name="attach" value="1" '.$global_checked[2].' class="attachs_check" onclick="checkAction(\'attach\');" style="vertical-align: middle;" /> '.$lang->specific_files.' (<a id="new_specific_file">'.$lang->add_another.'</a>)</label></dt><br />
2013          '.$specific_files.'
2014          '.$specific_colors_option.'
2015          '.$specific_colors.'
2016      </dl>
2017      <script type="text/javascript">
2018      checkAction(\'attach\');'.$check_actions.'
2019      </script>';
2020  
2021      echo $form->generate_hidden_field("file", htmlspecialchars_uni($stylesheet['name']))."<br />\n";
2022      echo $form->generate_hidden_field("tid", $theme['tid'])."<br />\n";
2023  
2024      $form_container = new FormContainer("{$lang->edit_stylesheet_properties_for} ".htmlspecialchars_uni($stylesheet['name']));
2025      $form_container->output_row($lang->file_name, $lang->file_name_desc, $form->generate_text_box('name', $mybb->input['name'], array('id' => 'name', 'style' => 'width: 200px;')), 'name');
2026  
2027      $form_container->output_row($lang->attached_to, $lang->attached_to_desc, $actions);
2028  
2029      $form_container->end();
2030  
2031      $buttons[] = $form->generate_submit_button($lang->save_stylesheet_properties);
2032  
2033      $form->output_submit_wrapper($buttons);
2034  
2035      echo <<<EOF
2036  
2037      <script type="text/javascript" src="./jscripts/theme_properties.js?ver=1821"></script>
2038      <script type="text/javascript">
2039      <!---
2040      themeProperties.setup('{$count}');
2041      // -->
2042      </script>
2043  EOF;
2044  
2045      $form->end();
2046  
2047      $page->output_footer();
2048  }
2049  
2050  // Shows the page where you can actually edit a particular selector or the whole stylesheet
2051  if($mybb->input['action'] == "edit_stylesheet" && (!isset($mybb->input['mode']) || $mybb->input['mode'] == "simple"))
2052  {
2053      // Fetch the theme we want to edit this stylesheet in
2054      $query = $db->simple_select("themes", "*", "tid='".$mybb->get_input('tid', MyBB::INPUT_INT)."'");
2055      $theme = $db->fetch_array($query);
2056  
2057      if(!$theme || $theme['tid'] == 1)
2058      {
2059          flash_message($lang->error_invalid_theme, 'error');
2060          admin_redirect("index.php?module=style-themes");
2061      }
2062  
2063      $plugins->run_hooks("admin_style_themes_edit_stylesheet_simple");
2064  
2065      $parent_list = make_parent_theme_list($theme['tid']);
2066      $parent_list = implode(',', $parent_list);
2067      if(!$parent_list)
2068      {
2069          $parent_list = 1;
2070      }
2071  
2072      $query = $db->simple_select("themestylesheets", "*", "name='".$db->escape_string($mybb->input['file'])."' AND tid IN ({$parent_list})", array('order_by' => 'tid', 'order_dir' => 'desc', 'limit' => 1));
2073      $stylesheet = $db->fetch_array($query);
2074  
2075      // Does the theme not exist?
2076      if(!$stylesheet)
2077      {
2078          flash_message($lang->error_invalid_stylesheet, 'error');
2079          admin_redirect("index.php?module=style-themes");
2080      }
2081  
2082      if($mybb->request_method == "post")
2083      {
2084          $sid = $stylesheet['sid'];
2085  
2086          // Theme & stylesheet theme ID do not match, editing inherited - we copy to local theme
2087          if($theme['tid'] != $stylesheet['tid'])
2088          {
2089              $sid = copy_stylesheet_to_theme($stylesheet, $theme['tid']);
2090          }
2091  
2092          // Insert the modified CSS
2093          $new_stylesheet = $stylesheet['stylesheet'];
2094  
2095          if($mybb->input['serialized'] == 1)
2096          {
2097              $mybb->input['css_bits'] = my_unserialize($mybb->input['css_bits']);
2098          }
2099  
2100          $css_to_insert = '';
2101          foreach($mybb->input['css_bits'] as $field => $value)
2102          {
2103              if(!trim($value) || !trim($field))
2104              {
2105                  continue;
2106              }
2107  
2108              if($field == "extra")
2109              {
2110                  $css_to_insert .= $value."\n";
2111              }
2112              else
2113              {
2114                  $field = str_replace("_", "-", $field);
2115                  $css_to_insert .= "{$field}: {$value};\n";
2116              }
2117          }
2118  
2119          $new_stylesheet = insert_into_css($css_to_insert, $mybb->input['selector'], $new_stylesheet);
2120  
2121          // Now we have the new stylesheet, save it
2122          $updated_stylesheet = array(
2123              "cachefile" => $db->escape_string($stylesheet['name']),
2124              "stylesheet" => $db->escape_string($new_stylesheet),
2125              "lastmodified" => TIME_NOW
2126          );
2127          $db->update_query("themestylesheets", $updated_stylesheet, "sid='{$sid}'");
2128  
2129          // Cache the stylesheet to the file
2130          if(!cache_stylesheet($theme['tid'], $stylesheet['name'], $new_stylesheet))
2131          {
2132              $db->update_query("themestylesheets", array('cachefile' => "css.php?stylesheet={$sid}"), "sid='{$sid}'", 1);
2133          }
2134  
2135          // Update the CSS file list for this theme
2136          update_theme_stylesheet_list($theme['tid']);
2137  
2138          $plugins->run_hooks("admin_style_themes_edit_stylesheet_simple_commit");
2139  
2140          // Log admin action
2141          log_admin_action(htmlspecialchars_uni($theme['name']), $stylesheet['name']);
2142  
2143          if(!$mybb->input['ajax'])
2144          {
2145              flash_message($lang->success_stylesheet_updated, 'success');
2146  
2147              if($mybb->input['save_close'])
2148              {
2149                  admin_redirect("index.php?module=style-themes&action=edit&tid={$theme['tid']}");
2150              }
2151              else
2152              {
2153                  admin_redirect("index.php?module=style-themes&action=edit_stylesheet&tid={$theme['tid']}&file={$stylesheet['name']}");
2154              }
2155          }
2156          else
2157          {
2158              echo "1";
2159              exit;
2160          }
2161      }
2162  
2163      // Has the file on the file system been modified?
2164      if(resync_stylesheet($stylesheet))
2165      {
2166          // Need to refetch new stylesheet as it was modified
2167          $query = $db->simple_select("themestylesheets", "stylesheet", "sid='{$stylesheet['sid']}'");
2168          $stylesheet['stylesheet'] = $db->fetch_field($query, 'stylesheet');
2169      }
2170  
2171      $css_array = css_to_array($stylesheet['stylesheet']);
2172      $selector_list = get_selectors_as_options($css_array, $mybb->get_input('selector'));
2173  
2174      // Do we not have any selectors? Send em to the full edit page
2175      if(!$selector_list)
2176      {
2177          flash_message($lang->error_cannot_parse, 'error');
2178          admin_redirect("index.php?module=style-themes&action=edit_stylesheet&tid={$theme['tid']}&file=".htmlspecialchars_uni($stylesheet['name'])."&mode=advanced");
2179          exit;
2180      }
2181  
2182      // Fetch list of all of the stylesheets for this theme
2183      $stylesheets = fetch_theme_stylesheets($theme);
2184      $this_stylesheet = $stylesheets[$stylesheet['name']];
2185      unset($stylesheets);
2186  
2187      $page->extra_header .= "
2188      <script type=\"text/javascript\">
2189      var my_post_key = '".$mybb->post_code."';
2190      </script>";
2191  
2192      $page->add_breadcrumb_item(htmlspecialchars_uni($theme['name']), "index.php?module=style-themes&amp;action=edit&amp;tid={$mybb->input['tid']}");
2193      $page->add_breadcrumb_item("{$lang->editing} ".htmlspecialchars_uni($stylesheet['name']), "index.php?module=style-themes&amp;action=edit_stylesheet&amp;tid={$mybb->input['tid']}&amp;file=".htmlspecialchars_uni($mybb->input['file'])."&amp;mode=simple");
2194  
2195      $page->output_header("{$lang->themes} - {$lang->edit_stylesheets}");
2196  
2197      // If the stylesheet and theme do not match, we must be editing something that is inherited
2198      if(!empty($this_stylesheet['inherited'][$stylesheet['name']]))
2199      {
2200          $query = $db->simple_select("themes", "name", "tid='{$stylesheet['tid']}'");
2201          $stylesheet_parent = htmlspecialchars_uni($db->fetch_field($query, 'name'));
2202  
2203          // Show inherited warning
2204          if($stylesheet['tid'] == 1)
2205          {
2206              $page->output_alert($lang->sprintf($lang->stylesheet_inherited_default, $stylesheet_parent), "ajax_alert");
2207          }
2208          else
2209          {
2210              $page->output_alert($lang->sprintf($lang->stylesheet_inherited, $stylesheet_parent), "ajax_alert");
2211          }
2212      }
2213  
2214      $sub_tabs['edit_stylesheet'] = array(
2215          'title' => $lang->edit_stylesheet_simple_mode,
2216          'link' => "index.php?module=style-themes&amp;action=edit_stylesheet&amp;tid={$mybb->input['tid']}&amp;file=".htmlspecialchars_uni($mybb->input['file'])."&amp;mode=simple",
2217          'description' => $lang->edit_stylesheet_simple_mode_desc
2218      );
2219  
2220      $sub_tabs['edit_stylesheet_advanced'] = array(
2221          'title' => $lang->edit_stylesheet_advanced_mode,
2222          'link' => "index.php?module=style-themes&amp;action=edit_stylesheet&amp;tid={$mybb->input['tid']}&amp;file=".htmlspecialchars_uni($mybb->input['file'])."&amp;mode=advanced",
2223      );
2224  
2225      $page->output_nav_tabs($sub_tabs, 'edit_stylesheet');
2226  
2227      // Output the selection box
2228      $form = new Form("index.php", "get", "selector_form");
2229      echo $form->generate_hidden_field("module", "style/themes")."\n";
2230      echo $form->generate_hidden_field("action", "edit_stylesheet")."\n";
2231      echo $form->generate_hidden_field("tid", $mybb->input['tid'])."\n";
2232      echo $form->generate_hidden_field("file", htmlspecialchars_uni($mybb->input['file']))."\n";
2233  
2234      echo "{$lang->selector}: <select id=\"selector\" name=\"selector\">\n{$selector_list}</select> <span id=\"mini_spinner\">".$form->generate_submit_button($lang->go)."</span><br /><br />\n";
2235  
2236      $form->end();
2237  
2238      // Haven't chosen a selector to edit, show the first one from the stylesheet
2239      if(!$mybb->get_input('selector'))
2240      {
2241          reset($css_array);
2242          uasort($css_array, "css_selectors_sort_cmp");
2243          $selector = key($css_array);
2244          $editable_selector = $css_array[$selector];
2245      }
2246      // Show a specific selector
2247      else
2248      {
2249          $editable_selector = $css_array[$mybb->input['selector']];
2250          $selector = $mybb->input['selector'];
2251      }
2252  
2253      // Get the properties from this item
2254      $properties = parse_css_properties($editable_selector['values']);
2255  
2256      foreach(array('background', 'color', 'width', 'font-family', 'font-size', 'font-style', 'font-weight', 'text-decoration') as $_p)
2257      {
2258          if(!isset($properties[$_p]))
2259          {
2260              $properties[$_p] = '';
2261          }
2262      }
2263  
2264      $form = new Form("index.php?module=style-themes&amp;action=edit_stylesheet", "post");
2265      echo $form->generate_hidden_field("tid", $mybb->input['tid'], array('id' => "tid"))."\n";
2266      echo $form->generate_hidden_field("file", htmlspecialchars_uni($mybb->input['file']), array('id' => "file"))."\n";
2267      echo $form->generate_hidden_field("selector", htmlspecialchars_uni($selector), array('id' => 'hidden_selector'))."\n";
2268  
2269      echo "<div id=\"stylesheet\">";
2270      $table = new Table;
2271      $table->construct_cell("<div style=\"float: right;\">".$form->generate_text_box('css_bits[background]', $properties['background'], array('id' => 'css_bits[background]', 'style' => 'width: 260px;'))."</div><div><strong>{$lang->background}</strong></div>", array('style' => 'width: 20%;'));
2272      $table->construct_cell("<strong>{$lang->extra_css_atribs}</strong><br /><div style=\"align: center;\">".$form->generate_text_area('css_bits[extra]', $properties['extra'], array('id' => 'css_bits[extra]', 'style' => 'width: 98%;', 'rows' => '19'))."</div>", array('rowspan' => 8));
2273      $table->construct_row();
2274      $table->construct_cell("<div style=\"float: right;\">".$form->generate_text_box('css_bits[color]', $properties['color'], array('id' => 'css_bits[color]', 'style' => 'width: 260px;'))."</div><div><strong>{$lang->color}</strong></div>", array('style' => 'width: 40%;'));
2275      $table->construct_row();
2276      $table->construct_cell("<div style=\"float: right;\">".$form->generate_text_box('css_bits[width]', $properties['width'], array('id' => 'css_bits[width]', 'style' => 'width: 260px;'))."</div><div><strong>{$lang->width}</strong></div>", array('style' => 'width: 40%;'));
2277      $table->construct_row();
2278      $table->construct_cell("<div style=\"float: right;\">".$form->generate_text_box('css_bits[font_family]', $properties['font-family'], array('id' => 'css_bits[font_family]', 'style' => 'width: 260px;'))."</div><div><strong>{$lang->font_family}</strong></div>", array('style' => 'width: 40%;'));
2279      $table->construct_row();
2280      $table->construct_cell("<div style=\"float: right;\">".$form->generate_text_box('css_bits[font_size]', $properties['font-size'], array('id' => 'css_bits[font_size]', 'style' => 'width: 260px;'))."</div><div><strong>{$lang->font_size}</strong></div>", array('style' => 'width: 40%;'));
2281      $table->construct_row();
2282      $table->construct_cell("<div style=\"float: right;\">".$form->generate_text_box('css_bits[font_style]', $properties['font-style'], array('id' => 'css_bits[font_style]', 'style' => 'width: 260px;'))."</div><div><strong>{$lang->font_style}</strong></div>", array('style' => 'width: 40%;'));
2283      $table->construct_row();
2284      $table->construct_cell("<div style=\"float: right;\">".$form->generate_text_box('css_bits[font_weight]', $properties['font-weight'], array('id' => 'css_bits[font_weight]', 'style' => 'width: 260px;'))."</div><div><strong>{$lang->font_weight}</strong></div>", array('style' => 'width: 40%;'));
2285      $table->construct_row();
2286      $table->construct_cell("<div style=\"float: right;\">".$form->generate_text_box('css_bits[text_decoration]', $properties['text-decoration'], array('id' => 'css_bits[text_decoration]', 'style' => 'width: 260px;'))."</div><div><strong>{$lang->text_decoration}</strong></div>", array('style' => 'width: 40%;'));
2287      $table->construct_row();
2288  
2289      $table->output(htmlspecialchars_uni($editable_selector['class_name'])."<span id=\"saved\" style=\"color: #FEE0C6;\"></span>");
2290  
2291      echo "</div>";
2292  
2293      $buttons[] = $form->generate_reset_button($lang->reset);
2294      $buttons[] = $form->generate_submit_button($lang->save_changes, array('id' => 'save', 'name' => 'save'));
2295      $buttons[] = $form->generate_submit_button($lang->save_changes_and_close, array('id' => 'save_close', 'name' => 'save_close'));
2296  
2297      $form->output_submit_wrapper($buttons);
2298  
2299      echo '<script type="text/javascript" src="./jscripts/themes.js?ver=1808"></script>';
2300      echo '<script type="text/javascript">
2301  
2302  $(function() {
2303  //<![CDATA[
2304      ThemeSelector.init("./index.php?module=style-themes&action=xmlhttp_stylesheet", "./index.php?module=style-themes&action=edit_stylesheet", $("#selector"), $("#stylesheet"), "'.htmlspecialchars_uni($mybb->input['file']).'", $("#selector_form"), "'.$mybb->input['tid'].'");
2305      lang.saving = "'.$lang->saving.'";
2306  });
2307  //]]>
2308  </script>';
2309  
2310      $form->end();
2311  
2312      $page->output_footer();
2313  }
2314  
2315  if($mybb->input['action'] == "edit_stylesheet" && $mybb->input['mode'] == "advanced")
2316  {
2317      // Fetch the theme we want to edit this stylesheet in
2318      $query = $db->simple_select("themes", "*", "tid='".$mybb->get_input('tid', MyBB::INPUT_INT)."'");
2319      $theme = $db->fetch_array($query);
2320  
2321      if(!$theme || $theme['tid'] == 1)
2322      {
2323          flash_message($lang->error_invalid_theme, 'error');
2324          admin_redirect("index.php?module=style-themes");
2325      }
2326  
2327      $plugins->run_hooks("admin_style_themes_edit_stylesheet_advanced");
2328  
2329      $parent_list = make_parent_theme_list($theme['tid']);
2330      $parent_list = implode(',', $parent_list);
2331      if(!$parent_list)
2332      {
2333          $parent_list = 1;
2334      }
2335  
2336      $query = $db->simple_select("themestylesheets", "*", "name='".$db->escape_string($mybb->input['file'])."' AND tid IN ({$parent_list})", array('order_by' => 'tid', 'order_dir' => 'desc', 'limit' => 1));
2337      $stylesheet = $db->fetch_array($query);
2338  
2339      // Does the theme not exist?
2340      if($db->num_rows($query) == 0)
2341      {
2342          flash_message($lang->error_invalid_stylesheet, 'error');
2343          admin_redirect("index.php?module=style-themes");
2344      }
2345  
2346      if($mybb->request_method == "post")
2347      {
2348          $sid = $stylesheet['sid'];
2349  
2350          // Theme & stylesheet theme ID do not match, editing inherited - we copy to local theme
2351          if($theme['tid'] != $stylesheet['tid'])
2352          {
2353              $sid = copy_stylesheet_to_theme($stylesheet, $theme['tid']);
2354          }
2355  
2356          // Now we have the new stylesheet, save it
2357          $updated_stylesheet = array(
2358              "cachefile" => $db->escape_string($stylesheet['name']),
2359              "stylesheet" => $db->escape_string($mybb->input['stylesheet']),
2360              "lastmodified" => TIME_NOW
2361          );
2362          $db->update_query("themestylesheets", $updated_stylesheet, "sid='{$sid}'");
2363  
2364          // Cache the stylesheet to the file
2365          if(!cache_stylesheet($theme['tid'], $stylesheet['name'], $mybb->input['stylesheet']))
2366          {
2367              $db->update_query("themestylesheets", array('cachefile' => "css.php?stylesheet={$sid}"), "sid='{$sid}'", 1);
2368          }
2369  
2370          // Update the CSS file list for this theme
2371          update_theme_stylesheet_list($theme['tid']);
2372  
2373          $plugins->run_hooks("admin_style_themes_edit_stylesheet_advanced_commit");
2374  
2375          // Log admin action
2376          log_admin_action(htmlspecialchars_uni($theme['name']), $stylesheet['name']);
2377  
2378          flash_message($lang->success_stylesheet_updated, 'success');
2379  
2380          if(!$mybb->get_input('save_close'))
2381          {
2382              admin_redirect("index.php?module=style-themes&action=edit_stylesheet&file=".htmlspecialchars_uni($stylesheet['name'])."&tid={$theme['tid']}&mode=advanced");
2383          }
2384          else
2385          {
2386              admin_redirect("index.php?module=style-themes&action=edit&tid={$theme['tid']}");
2387          }
2388      }
2389  
2390      // Fetch list of all of the stylesheets for this theme
2391      $stylesheets = fetch_theme_stylesheets($theme);
2392      $this_stylesheet = $stylesheets[$stylesheet['name']];
2393      unset($stylesheets);
2394  
2395      if($admin_options['codepress'] != 0)
2396      {
2397          $page->extra_header .= '
2398  <link href="./jscripts/codemirror/lib/codemirror.css?ver=1813" rel="stylesheet">
2399  <link href="./jscripts/codemirror/theme/mybb.css?ver=1813" rel="stylesheet">
2400  <link href="./jscripts/codemirror/addon/dialog/dialog-mybb.css?ver=1813" rel="stylesheet">
2401  <script src="./jscripts/codemirror/lib/codemirror.js?ver=1813"></script>
2402  <script src="./jscripts/codemirror/mode/css/css.js?ver=1813"></script>
2403  <script src="./jscripts/codemirror/addon/dialog/dialog.js?ver=1813"></script>
2404  <script src="./jscripts/codemirror/addon/search/searchcursor.js?ver=1813"></script>
2405  <script src="./jscripts/codemirror/addon/search/search.js?ver=1821"></script>
2406  ';
2407      }
2408  
2409      $page->add_breadcrumb_item(htmlspecialchars_uni($theme['name']), "index.php?module=style-themes&amp;action=edit&amp;tid={$mybb->input['tid']}");
2410      $page->add_breadcrumb_item("{$lang->editing} ".htmlspecialchars_uni($stylesheet['name']), "index.php?module=style-themes&amp;action=edit_stylesheet&amp;tid={$mybb->input['tid']}&amp;file=".htmlspecialchars_uni($mybb->input['file'])."&amp;mode=advanced");
2411  
2412      $page->output_header("{$lang->themes} - {$lang->edit_stylesheet_advanced_mode}");
2413  
2414      // If the stylesheet and theme do not match, we must be editing something that is inherited
2415      if(!empty($this_stylesheet['inherited']) && $this_stylesheet['inherited'][$stylesheet['name']])
2416      {
2417          $query = $db->simple_select("themes", "name", "tid='{$stylesheet['tid']}'");
2418          $stylesheet_parent = htmlspecialchars_uni($db->fetch_field($query, 'name'));
2419  
2420          // Show inherited warning
2421          if($stylesheet['tid'] == 1)
2422          {
2423              $page->output_alert($lang->sprintf($lang->stylesheet_inherited_default, $stylesheet_parent));
2424          }
2425          else
2426          {
2427              $page->output_alert($lang->sprintf($lang->stylesheet_inherited, $stylesheet_parent));
2428          }
2429      }
2430  
2431      $sub_tabs['edit_stylesheet'] = array(
2432          'title' => $lang->edit_stylesheet_simple_mode,
2433          'link' => "index.php?module=style-themes&amp;action=edit_stylesheet&amp;tid={$mybb->input['tid']}&amp;file=".htmlspecialchars_uni($mybb->input['file'])."&amp;mode=simple"
2434      );
2435  
2436      $sub_tabs['edit_stylesheet_advanced'] = array(
2437          'title' => $lang->edit_stylesheet_advanced_mode,
2438          'link' => "index.php?module=style-themes&amp;action=edit_stylesheet&amp;tid={$mybb->input['tid']}&amp;file=".htmlspecialchars_uni($mybb->input['file'])."&amp;mode=advanced",
2439          'description' => $lang->edit_stylesheet_advanced_mode_desc
2440      );
2441  
2442      $page->output_nav_tabs($sub_tabs, 'edit_stylesheet_advanced');
2443  
2444      // Has the file on the file system been modified?
2445      if(resync_stylesheet($stylesheet))
2446      {
2447          // Need to refetch new stylesheet as it was modified
2448          $query = $db->simple_select("themestylesheets", "stylesheet", "sid='{$stylesheet['sid']}'");
2449          $stylesheet['stylesheet'] = $db->fetch_field($query, 'stylesheet');
2450      }
2451  
2452      $form = new Form("index.php?module=style-themes&amp;action=edit_stylesheet&amp;mode=advanced", "post", "edit_stylesheet");
2453      echo $form->generate_hidden_field("tid", $mybb->input['tid'])."\n";
2454      echo $form->generate_hidden_field("file", htmlspecialchars_uni($mybb->input['file']))."\n";
2455  
2456      $table = new Table;
2457      $table->construct_cell($form->generate_text_area('stylesheet', $stylesheet['stylesheet'], array('id' => 'stylesheet', 'style' => 'width: 99%;', 'class' => '', 'rows' => '30')));
2458      $table->construct_row();
2459      $table->output($lang->full_stylesheet_for.' '.htmlspecialchars_uni($stylesheet['name']), 1, 'tfixed');
2460  
2461      $buttons[] = $form->generate_submit_button($lang->save_changes, array('id' => 'save', 'name' => 'save'));
2462      $buttons[] = $form->generate_submit_button($lang->save_changes_and_close, array('id' => 'save_close', 'name' => 'save_close'));
2463  
2464      $form->output_submit_wrapper($buttons);
2465  
2466      $form->end();
2467  
2468      if($admin_options['codepress'] != 0)
2469      {
2470          echo '<script type="text/javascript">
2471              var editor = CodeMirror.fromTextArea(document.getElementById("stylesheet"), {
2472                  lineNumbers: true,
2473                  lineWrapping: true,
2474                  viewportMargin: Infinity,
2475                  indentWithTabs: true,
2476                  indentUnit: 4,
2477                  mode: "text/css",
2478                  theme: "mybb"
2479              });</script>';
2480      }
2481  
2482      $page->output_footer();
2483  }
2484  
2485  if($mybb->input['action'] == "delete_stylesheet")
2486  {
2487      // Fetch the theme we want to edit this stylesheet in
2488      $query = $db->simple_select("themes", "*", "tid='".$mybb->get_input('tid', MyBB::INPUT_INT)."'");
2489      $theme = $db->fetch_array($query);
2490  
2491      if(!$theme || $theme['tid'] == 1)
2492      {
2493          flash_message($lang->error_invalid_theme, 'error');
2494          admin_redirect("index.php?module=style-themes");
2495      }
2496  
2497      $plugins->run_hooks("admin_style_themes_delete_stylesheet");
2498  
2499      $parent_list = make_parent_theme_list($theme['tid']);
2500      $parent_list = implode(',', $parent_list);
2501      if(!$parent_list)
2502      {
2503          $parent_list = 1;
2504      }
2505  
2506      $query = $db->simple_select("themestylesheets", "*", "name='".$db->escape_string($mybb->input['file'])."' AND tid IN ({$parent_list})", array('order_by' => 'tid', 'order_dir' => 'desc', 'limit' => 1));
2507      $stylesheet = $db->fetch_array($query);
2508  
2509      // Does the theme not exist? or are we trying to delete the master?
2510      if(!$stylesheet || $stylesheet['tid'] == 1)
2511      {
2512          flash_message($lang->error_invalid_stylesheet, 'error');
2513          admin_redirect("index.php?module=style-themes");
2514      }
2515  
2516      // User clicked no
2517      if($mybb->get_input('no'))
2518      {
2519          admin_redirect("index.php?module=style-themes");
2520      }
2521  
2522      if($mybb->request_method == "post")
2523      {
2524          $db->delete_query("themestylesheets", "sid='{$stylesheet['sid']}'", 1);
2525          @unlink(MYBB_ROOT."cache/themes/theme{$theme['tid']}/{$stylesheet['cachefile']}");
2526  
2527          $filename_min = str_replace('.css', '.min.css', $stylesheet['cachefile']);
2528          @unlink(MYBB_ROOT."cache/themes/theme{$theme['tid']}/{$filename_min}");
2529  
2530          // Update the CSS file list for this theme
2531          update_theme_stylesheet_list($theme['tid'], $theme, true);
2532  
2533          $plugins->run_hooks("admin_style_themes_delete_stylesheet_commit");
2534  
2535          // Log admin action
2536          log_admin_action($stylesheet['sid'], $stylesheet['name'], $theme['tid'], $theme['name']);
2537  
2538          flash_message($lang->success_stylesheet_deleted, 'success');
2539          admin_redirect("index.php?module=style-themes&action=edit&tid={$theme['tid']}");
2540      }
2541      else
2542      {
2543          $page->output_confirm_action("index.php?module=style-themes&amp;action=force&amp;tid={$theme['tid']}", $lang->confirm_stylesheet_deletion);
2544      }
2545  }
2546  
2547  if($mybb->input['action'] == "add_stylesheet")
2548  {
2549      // Fetch the theme we want to edit this stylesheet in
2550      $query = $db->simple_select("themes", "*", "tid='".$mybb->get_input('tid', MyBB::INPUT_INT)."'");
2551      $theme = $db->fetch_array($query);
2552  
2553      if(!$theme || $theme['tid'] == 1)
2554      {
2555          flash_message($lang->error_invalid_theme, 'error');
2556          admin_redirect("index.php?module=style-themes");
2557      }
2558  
2559      $plugins->run_hooks("admin_style_themes_add_stylesheet");
2560  
2561      // Fetch list of all of the stylesheets for this theme
2562      $stylesheets = fetch_theme_stylesheets($theme);
2563  
2564      if($mybb->request_method == "post")
2565      {
2566          // Remove special characters
2567          $mybb->input['name'] = preg_replace('#([^a-z0-9-_\.]+)#i', '', $mybb->input['name']);
2568          if(!$mybb->input['name'] || $mybb->input['name'] == ".css")
2569          {
2570              $errors[] = $lang->error_missing_stylesheet_name;
2571          }
2572  
2573          // Get 30 chars only because we don't want more than that
2574          $mybb->input['name'] = my_substr($mybb->input['name'], 0, 30);
2575          if(get_extension($mybb->input['name']) != "css")
2576          {
2577              // Does not end with '.css'
2578              $errors[] = $lang->sprintf($lang->error_missing_stylesheet_extension, $mybb->input['name']);
2579          }
2580  
2581          if(!$errors)
2582          {
2583              if($mybb->input['add_type'] == 1)
2584              {
2585                  // Import from a current stylesheet
2586                  $parent_list = make_parent_theme_list($theme['tid']);
2587                  $parent_list = implode(',', $parent_list);
2588  
2589                  $query = $db->simple_select("themestylesheets", "stylesheet", "name='".$db->escape_string($mybb->input['import'])."' AND tid IN ({$parent_list})", array('limit' => 1, 'order_by' => 'tid', 'order_dir' => 'desc'));
2590                  $stylesheet = $db->fetch_field($query, "stylesheet");
2591              }
2592              else
2593              {
2594                  // Custom stylesheet
2595                  $stylesheet = $mybb->input['stylesheet'];
2596              }
2597  
2598              $attached = array();
2599  
2600              if($mybb->input['attach'] == 1)
2601              {
2602                  // Our stylesheet is attached to custom pages in MyBB
2603                  foreach($mybb->input as $id => $value)
2604                  {
2605                      $actions_list = "";
2606                      $attached_to = "";
2607  
2608                      if(strpos($id, 'attached_') !== false)
2609                      {
2610                          // We have a custom attached file
2611                          $attached_id = (int)str_replace('attached_', '', $id);
2612                          $attached_to = $value;
2613  
2614                          if($mybb->input['action_'.$attached_id] == 1)
2615                          {
2616                              // We have custom actions for attached files
2617                              $actions_list = $mybb->input['action_list_'.$attached_id];
2618                          }
2619  
2620                          if($actions_list)
2621                          {
2622                              $attached_to = $attached_to."?".$actions_list;
2623                          }
2624  
2625                          $attached[] = $attached_to;
2626                      }
2627                  }
2628              }
2629              else if($mybb->input['attach'] == 2)
2630              {
2631                  if(!is_array($mybb->input['color']))
2632                  {
2633                      $errors[] = $lang->error_no_color_picked;
2634                  }
2635                  else
2636                  {
2637                      $attached = $mybb->input['color'];
2638                  }
2639              }
2640  
2641              // Add Stylesheet
2642              $insert_array = array(
2643                  'name' => $db->escape_string($mybb->input['name']),
2644                  'tid' => $mybb->get_input('tid', MyBB::INPUT_INT),
2645                  'attachedto' => implode('|', array_map(array($db, "escape_string"), $attached)),
2646                  'stylesheet' => $db->escape_string($stylesheet),
2647                  'cachefile' => $db->escape_string(str_replace('/', '', $mybb->input['name'])),
2648                  'lastmodified' => TIME_NOW
2649              );
2650  
2651              $sid = $db->insert_query("themestylesheets", $insert_array);
2652  
2653              if(!cache_stylesheet($theme['tid'], str_replace('/', '', $mybb->input['name']), $stylesheet))
2654              {
2655                  $db->update_query("themestylesheets", array('cachefile' => "css.php?stylesheet={$sid}"), "sid='{$sid}'", 1);
2656              }
2657  
2658              // Update the CSS file list for this theme
2659              update_theme_stylesheet_list($theme['tid'], $theme, true);
2660  
2661              $plugins->run_hooks("admin_style_themes_add_stylesheet_commit");
2662  
2663              // Log admin action
2664              log_admin_action($sid, $mybb->input['name'], $theme['tid'], $theme['name']);
2665  
2666              flash_message($lang->success_stylesheet_added, 'success');
2667              admin_redirect("index.php?module=style-themes&action=edit_stylesheet&tid={$mybb->input['tid']}&sid={$sid}&file=".urlencode($mybb->input['name']));
2668          }
2669      }
2670  
2671      if($admin_options['codepress'] != 0)
2672      {
2673          $page->extra_header .= '
2674  <link href="./jscripts/codemirror/lib/codemirror.css?ver=1813" rel="stylesheet">
2675  <link href="./jscripts/codemirror/theme/mybb.css?ver=1813" rel="stylesheet">
2676  <link href="./jscripts/codemirror/addon/dialog/dialog-mybb.css?ver=1813" rel="stylesheet">
2677  <script src="./jscripts/codemirror/lib/codemirror.js?ver=1813"></script>
2678  <script src="./jscripts/codemirror/mode/css/css.js?ver=1813"></script>
2679  <script src="./jscripts/codemirror/addon/dialog/dialog.js?ver=1813"></script>
2680  <script src="./jscripts/codemirror/addon/search/searchcursor.js?ver=1813"></script>
2681  <script src="./jscripts/codemirror/addon/search/search.js?ver=1821"></script>
2682  ';
2683      }
2684  
2685      $page->add_breadcrumb_item(htmlspecialchars_uni($theme['name']), "index.php?module=style-themes&amp;action=edit&amp;tid={$mybb->input['tid']}");
2686      $page->add_breadcrumb_item($lang->add_stylesheet);
2687      $properties = my_unserialize($theme['properties']);
2688  
2689      $page->output_header("{$lang->themes} - {$lang->add_stylesheet}");
2690  
2691      $sub_tabs['edit_stylesheets'] = array(
2692          'title' => $lang->edit_stylesheets,
2693          'link' => "index.php?module=style-themes&amp;action=edit&amp;tid={$mybb->input['tid']}"
2694      );
2695  
2696      $sub_tabs['add_stylesheet'] = array(
2697          'title' => $lang->add_stylesheet,
2698          'link' => "index.php?module=style-themes&amp;action=add_stylesheet&amp;tid={$mybb->input['tid']}",
2699          'description' => $lang->add_stylesheet_desc
2700      );
2701  
2702      $sub_tabs['export_theme'] = array(
2703          'title' => $lang->export_theme,
2704          'link' => "index.php?module=style-themes&amp;action=export&amp;tid={$mybb->input['tid']}"
2705      );
2706  
2707      $sub_tabs['duplicate_theme'] = array(
2708          'title' => $lang->duplicate_theme,
2709          'link' => "index.php?module=style-themes&amp;action=duplicate&amp;tid={$mybb->input['tid']}",
2710          'description' => $lang->duplicate_theme_desc
2711      );
2712  
2713      $page->output_nav_tabs($sub_tabs, 'add_stylesheet');
2714  
2715      $add_checked = array();
2716  
2717      if($errors)
2718      {
2719          $page->output_inline_error($errors);
2720  
2721          foreach($mybb->input as $name => $value)
2722          {
2723              if(strpos($name, "attached") !== false)
2724              {
2725                  list(, $id) = explode('_', $name);
2726                  $id = (int)$id;
2727  
2728                  $mybb->input['applied_to'][$value] = array(0 => 'global');
2729  
2730                  if($mybb->input['action_'.$id] == 1)
2731                  {
2732                      $mybb->input['applied_to'][$value] = explode(',', $mybb->input['action_list_'.$id]);
2733                  }
2734              }
2735          }
2736  
2737          if($mybb->input['add_type'] == 1)
2738          {
2739              $add_checked[1] = "checked=\"checked\"";
2740              $add_checked[2] = "";
2741          }
2742          else
2743          {
2744              $add_checked[2] = "checked=\"checked\"";
2745              $add_checked[1] = "";
2746          }
2747      }
2748      else
2749      {
2750          $stylesheet = $mybb->get_input('stylesheet', MyBB::INPUT_ARRAY);
2751          if(!isset($stylesheet['sid']))
2752          {
2753              $stylesheet['sid'] = '';
2754          }
2755          if(isset($stylesheet['name']))
2756          {
2757              $mybb->input['name'] = $stylesheet['name'];
2758          }
2759  
2760          $add_checked[1] = "";
2761          $add_checked[2] = "";
2762      }
2763  
2764      $global_checked[1] = "checked=\"checked\"";
2765      $global_checked[2] = "";
2766      $global_checked[3] = "";
2767  
2768      $form = new Form("index.php?module=style-themes&amp;action=add_stylesheet", "post", "add_stylesheet");
2769  
2770      echo $form->generate_hidden_field("tid", $mybb->input['tid'])."\n";
2771  
2772      $specific_files = "<div id=\"attach_1\" class=\"attachs\">";
2773      $count = 0;
2774      $check_actions = "";
2775      $mybb->input['attach'] = $mybb->get_input('attach', MyBB::INPUT_INT);
2776      $stylesheet['colors'] = array();
2777      $stylesheet['sid'] = null;
2778  
2779      if($mybb->input['attach'] == 1 && is_array($mybb->input['applied_to']) && (!isset($mybb->input['applied_to']['global']) || $mybb->input['applied_to']['global'][0] != "global"))
2780      {
2781          foreach($mybb->input['applied_to'] as $name => $actions)
2782          {
2783              $action_list = "";
2784              if($actions[0] != "global")
2785              {
2786                  $action_list = implode(',', $actions);
2787              }
2788  
2789              if($actions[0] == "global")
2790              {
2791                  $global_action_checked[1] = "checked=\"checked\"";
2792                  $global_action_checked[2] = "";
2793              }
2794              else
2795              {
2796                  $global_action_checked[2] = "checked=\"checked\"";
2797                  $global_action_checked[1] = "";
2798              }
2799  
2800              $specific_file = "<dl style=\"margin-top: 0; margin-bottom: 0; width: 100%;\">
2801          <dt><label style=\"display: block;\"><input type=\"radio\" name=\"action_{$count}\" value=\"0\" {$global_action_checked[1]} class=\"action_{$count}s_check\" onclick=\"checkAction('action_{$count}');\" style=\"vertical-align: middle;\" /> {$lang->globally}</label></dt>
2802          <dt><label style=\"display: block;\"><input type=\"radio\" name=\"action_{$count}\" value=\"1\" {$global_action_checked[2]} class=\"action_{$count}s_check\" onclick=\"checkAction('action_{$count}');\" style=\"vertical-align: middle;\" /> {$lang->specific_actions}</label></dt>
2803              <dd style=\"margin-top: 4px;\" id=\"action_{$count}_1\" class=\"action_{$count}s\">
2804                  <small class=\"description\">{$lang->specific_actions_desc}</small>
2805                  <table cellpadding=\"4\">
2806                      <tr>
2807                          <td>".$form->generate_text_box('action_list_'.$count, $action_list, array('id' => 'action_list_'.$count, 'style' => 'width: 190px;'))."</td>
2808                      </tr>
2809                  </table>
2810              </dd>
2811          </dl>";
2812  
2813              $form_container = new FormContainer();
2814              $form_container->output_row("", "", "<span style=\"float: right;\"><a href=\"\" id=\"delete_img_{$count}\"><img src=\"styles/{$page->style}/images/icons/cross.png\" alt=\"{$lang->delete}\" title=\"{$lang->delete}\" /></a></span>{$lang->file} &nbsp;".$form->generate_text_box("attached_{$count}", $name, array('id' => "attached_{$count}", 'style' => 'width: 200px;')), "attached_{$count}");
2815  
2816              $form_container->output_row("", "", $specific_file);
2817  
2818              $specific_files .= "<div id=\"attached_form_{$count}\">".$form_container->end(true)."</div><div id=\"attach_box_{$count}\"></div>";
2819  
2820              $check_actions .= "\n\tcheckAction('action_{$count}');";
2821  
2822              ++$count;
2823          }
2824  
2825          if($check_actions)
2826          {
2827              $global_checked[3] = "";
2828              $global_checked[2] = "checked=\"checked\"";
2829              $global_checked[1] = "";
2830          }
2831      }
2832      else if($mybb->input['attach'] == 2)
2833      {
2834          // Colors
2835          if(is_array($properties['colors']))
2836          {
2837              // We might have colors here...
2838              foreach($mybb->input['color'] as $color)
2839              {
2840                  // Verify this is a color for this theme
2841                  if(array_key_exists($color, $properties['colors']))
2842                  {
2843                      $stylesheet['colors'][] = $color;
2844                  }
2845              }
2846  
2847              if(!empty($stylesheet['colors']))
2848              {
2849                  $global_checked[3] = "checked=\"checked\"";
2850                  $global_checked[2] = "";
2851                  $global_checked[1] = "";
2852              }
2853          }
2854      }
2855  
2856      $specific_files .= "</div>";
2857  
2858      // Colors
2859      $specific_colors = $specific_colors_option = '';
2860  
2861      if(isset($properties['colors']) && is_array($properties['colors']))
2862      {
2863          $specific_colors = "<br /><div id=\"attach_2\" class=\"attachs\">";
2864          $specific_colors_option = '<dt><label style="display: block;"><input type="radio" name="attach" value="2" '.$global_checked[3].' class="attachs_check" onclick="checkAction(\'attach\');" style="vertical-align: middle;" /> '.$lang->colors_specific_color.'</label></dt>';
2865  
2866          $specific_color = "
2867              <small>{$lang->colors_add_edit_desc}</small>
2868              <br /><br />
2869              ".$form->generate_select_box('color[]', $properties['colors'], $stylesheet['colors'], array('multiple' => true, 'size' => "5\" style=\"width: 200px;"))."
2870          ";
2871  
2872          $form_container = new FormContainer();
2873          $form_container->output_row("", "", $specific_color);
2874          $specific_colors .= $form_container->end(true)."</div>";
2875      }
2876  
2877      $actions = '<script type="text/javascript">
2878  	function checkAction(id)
2879      {
2880          var checked = \'\';
2881  
2882          $(\'.\'+id+\'s_check\').each(function(e, val)
2883          {
2884              if($(this).prop(\'checked\') == true)
2885              {
2886                  checked = $(this).val();
2887              }
2888          });
2889          $(\'.\'+id+\'s\').each(function(e)
2890          {
2891              $(this).hide();
2892          });
2893          if($(\'#\'+id+\'_\'+checked))
2894          {
2895              $(\'#\'+id+\'_\'+checked).show();
2896          }
2897      }
2898  </script>
2899      <dl style="margin-top: 0; margin-bottom: 0; width: 40%;">
2900          <dt><label style="display: block;"><input type="radio" name="attach" value="0" '.$global_checked[1].' class="attachs_check" onclick="checkAction(\'attach\');" style="vertical-align: middle;" /> '.$lang->globally.'</label></dt><br />
2901          <dt><label style="display: block;"><input type="radio" name="attach" value="1" '.$global_checked[2].' class="attachs_check" onclick="checkAction(\'attach\');" style="vertical-align: middle;" /> '.$lang->specific_files.' (<a id="new_specific_file">'.$lang->add_another.'</a>)</label></dt><br />
2902          '.$specific_files.'
2903          '.$specific_colors_option.'
2904          '.$specific_colors.'
2905      </dl>
2906      <script type="text/javascript">
2907      checkAction(\'attach\');'.$check_actions.'
2908      </script>';
2909  
2910      echo $form->generate_hidden_field("sid", $stylesheet['sid'])."<br />\n";
2911  
2912      $form_container = new FormContainer($lang->add_stylesheet_to.' '.htmlspecialchars_uni($theme['name']), 'tfixed');
2913      $form_container->output_row($lang->file_name, $lang->file_name_desc, $form->generate_text_box('name', $mybb->get_input('name'), array('id' => 'name', 'style' => 'width: 200px;')), 'name');
2914  
2915      $form_container->output_row($lang->attached_to, $lang->attached_to_desc, $actions);
2916  
2917      $sheetnames = array();
2918      foreach($stylesheets as $filename => $style)
2919      {
2920          $sheetnames[basename($filename)] = basename($filename);
2921      }
2922  
2923      $actions = "<dl style=\"margin-top: 0; margin-bottom: 0; width: 100%;\">
2924      <dt><label style=\"display: block;\"><input type=\"radio\" name=\"add_type\" value=\"1\" {$add_checked[1]} class=\"adds_check\" onclick=\"checkAction('add');\" style=\"vertical-align: middle;\" /> <strong>{$lang->import_stylesheet_from}</strong></label></dt>
2925          <dd style=\"margin-top: 4px;\" id=\"add_1\" class=\"adds\">
2926              <table cellpadding=\"4\">
2927                  <tr>
2928                      <td>".$form->generate_select_box('import', $sheetnames, $mybb->get_input('import'), array('id' => 'import'))."</td>
2929                  </tr>
2930              </table>
2931          </dd>
2932          <dt><label style=\"display: block;\"><input type=\"radio\" name=\"add_type\" value=\"2\" {$add_checked[2]} class=\"adds_check\" onclick=\"checkAction('add');\" style=\"vertical-align: middle;\" /> <strong>{$lang->write_own}</strong></label></dt>
2933          <span id=\"add_2\" class=\"adds\"><br />".$form->generate_text_area('stylesheet', $mybb->get_input('stylesheet'), array('id' => 'stylesheet', 'style' => 'width: 99%;', 'class' => '', 'rows' => '30'))."</span>
2934      </dl>";
2935  
2936      $form_container->output_row("", "", $actions);
2937  
2938      $form_container->end();
2939  
2940      $buttons[] = $form->generate_submit_button($lang->save_stylesheet);
2941  
2942      $form->output_submit_wrapper($buttons);
2943  
2944      if($admin_options['codepress'] != 0)
2945      {
2946          echo '<script type="text/javascript">
2947              var editor = CodeMirror.fromTextArea(document.getElementById("stylesheet"), {
2948                  lineNumbers: true,
2949                  lineWrapping: true,
2950                  viewportMargin: Infinity,
2951                  indentWithTabs: true,
2952                  indentUnit: 4,
2953                  mode: "text/css",
2954                  theme: "mybb"
2955              });</script>';
2956      }
2957  
2958      echo '<script type="text/javascript" src="./jscripts/themes.js?ver=1808"></script>';
2959      echo '<script type="text/javascript" src="./jscripts/theme_properties.js?ver=1821"></script>';
2960      echo '<script type="text/javascript">
2961  $(function() {
2962  //<![CDATA[
2963      checkAction(\'add\');
2964      lang.saving = "'.$lang->saving.'";
2965  });
2966  //]]>
2967  </script>';
2968  
2969      $form->end();
2970  
2971      $page->output_footer();
2972  }
2973  
2974  if($mybb->input['action'] == "set_default")
2975  {
2976      if(!verify_post_check($mybb->get_input('my_post_key')))
2977      {
2978          flash_message($lang->invalid_post_verify_key2, 'error');
2979          admin_redirect("index.php?module=style-themes");
2980      }
2981  
2982      $query = $db->simple_select("themes", "*", "tid='".$mybb->get_input('tid', MyBB::INPUT_INT)."'");
2983      $theme = $db->fetch_array($query);
2984  
2985      // Does the theme not exist?
2986      if(!$theme || $theme['tid'] == 1)
2987      {
2988          flash_message($lang->error_invalid_theme, 'error');
2989          admin_redirect("index.php?module=style-themes");
2990      }
2991  
2992      $plugins->run_hooks("admin_style_themes_set_default");
2993  
2994      $cache->update('default_theme', $theme);
2995  
2996      $db->update_query("themes", array('def' => 0));
2997      $db->update_query("themes", array('def' => 1), "tid='".$mybb->get_input('tid', MyBB::INPUT_INT)."'");
2998  
2999      $plugins->run_hooks("admin_style_themes_set_default_commit");
3000  
3001      // Log admin action
3002      log_admin_action($theme['tid'], $theme['name']);
3003  
3004      flash_message($lang->success_theme_set_default, 'success');
3005      admin_redirect("index.php?module=style-themes");
3006  }
3007  
3008  if($mybb->input['action'] == "force")
3009  {
3010      $query = $db->simple_select("themes", "*", "tid='".$mybb->get_input('tid', MyBB::INPUT_INT)."'");
3011      $theme = $db->fetch_array($query);
3012  
3013      // Does the theme not exist?
3014      if(!$theme || $theme['tid'] == 1)
3015      {
3016          flash_message($lang->error_invalid_theme, 'error');
3017          admin_redirect("index.php?module=style-themes");
3018      }
3019  
3020      $plugins->run_hooks("admin_style_themes_force");
3021  
3022      // User clicked no
3023      if($mybb->get_input('no'))
3024      {
3025          admin_redirect("index.php?module=style-themes");
3026      }
3027  
3028      if($mybb->request_method == "post")
3029      {
3030          $updated_users = array(
3031              "style" => $theme['tid']
3032          );
3033  
3034          $plugins->run_hooks("admin_style_themes_force_commit");
3035  
3036          $db->update_query("users", $updated_users);
3037  
3038          // The theme has to be accessible to all usergroups in order to force on all users
3039          if($theme['allowedgroups'] !== "all")
3040          {
3041              $db->update_query("themes", array("allowedgroups" => "all"), "tid='{$theme['tid']}'");
3042          }
3043  
3044          // Log admin action
3045          log_admin_action($theme['tid'], $theme['name']);
3046  
3047          flash_message($lang->success_theme_forced, 'success');
3048          admin_redirect("index.php?module=style-themes");
3049      }
3050      else
3051      {
3052          $page->output_confirm_action("index.php?module=style-themes&amp;action=force&amp;tid={$theme['tid']}", $lang->confirm_theme_forced);
3053      }
3054  }
3055  
3056  if(!$mybb->input['action'])
3057  {
3058      $page->output_header($lang->themes);
3059  
3060      $plugins->run_hooks("admin_style_themes_start");
3061  
3062      $page->output_nav_tabs($sub_tabs, 'themes');
3063  
3064      $table = new Table;
3065      $table->construct_header($lang->theme);
3066      $table->construct_header($lang->num_users, array("class" => "align_center", "width" => 100));
3067      $table->construct_header($lang->controls, array("class" => "align_center", "width" => 150));
3068  
3069      build_theme_list();
3070  
3071      $table->output($lang->themes);
3072  
3073      $page->output_footer();
3074  }


2005 - 2021 © MyBB.de | Alle Rechte vorbehalten! | Sponsor: netcup Cross-referenced by PHPXref