[ Index ]

PHP Cross Reference of MyBB 1.8.21

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


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