[ Index ]

PHP Cross Reference of MyBB 1.8.22

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


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