[ Index ]

PHP Cross Reference of MyBB 1.8.37

title

Body

[close]

/admin/modules/style/ -> templates.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  $page->add_breadcrumb_item($lang->template_sets, "index.php?module=style-templates");
  18  
  19  $sid = $mybb->get_input('sid', MyBB::INPUT_INT);
  20  
  21  $expand_str = "";
  22  $expand_str2 = "";
  23  $expand_array = array();
  24  if(isset($mybb->input['expand']))
  25  {
  26      $expand_array = explode("|", $mybb->input['expand']);
  27      $expand_array = array_map("intval", $expand_array);
  28      $expand_str = "&amp;expand=".implode("|", $expand_array);
  29      $expand_str2 = "&expand=".implode("|", $expand_array);
  30  }
  31  
  32  if($mybb->input['action'] == "add_set" || $mybb->input['action'] == "add_template" || ($mybb->input['action'] == "add_template_group" && !$sid) || $mybb->input['action'] == "search_replace" || $mybb->input['action'] == "find_updated" || (!$mybb->input['action'] && !$sid))
  33  {
  34      $sub_tabs['templates'] = array(
  35          'title' => $lang->manage_template_sets,
  36          'link' => "index.php?module=style-templates",
  37          'description' => $lang->manage_template_sets_desc
  38      );
  39  
  40      $sub_tabs['add_set'] = array(
  41          'title' => $lang->add_set,
  42          'link' => "index.php?module=style-templates&amp;action=add_set".$expand_str
  43      );
  44  
  45      if($mybb->get_input('sid', MyBB::INPUT_INT) != -1)
  46      {
  47          $sub_tabs['add_template_group'] = array(
  48              'title' => $lang->add_template_group,
  49              'link' => "index.php?module=style-templates&amp;action=add_template_group".$expand_str,
  50              'description' => $lang->add_template_group_desc
  51          );
  52      }
  53  
  54      $sub_tabs['search_replace'] = array(
  55          'title' => $lang->search_replace,
  56          'link' => "index.php?module=style-templates&amp;action=search_replace",
  57          'description' => $lang->search_replace_desc
  58      );
  59  
  60      $sub_tabs['find_updated'] = array(
  61          'title' => $lang->find_updated,
  62          'link' => "index.php?module=style-templates&amp;action=find_updated",
  63          'description' => $lang->find_updated_desc
  64      );
  65  }
  66  else if(($sid && !$mybb->input['action']) || $mybb->input['action'] == "edit_set" || $mybb->input['action'] == "check_set" || $mybb->input['action'] == "edit_template" || $mybb->input['action'] == "add_template_group")
  67  {
  68      $sub_tabs['manage_templates'] = array(
  69          'title' => $lang->manage_templates,
  70          'link' => "index.php?module=style-templates&amp;sid=".$sid.$expand_str,
  71          'description' => $lang->manage_templates_desc
  72      );
  73  
  74      if($sid > 0)
  75      {
  76          $sub_tabs['edit_set'] = array(
  77              'title' => $lang->edit_set,
  78              'link' => "index.php?module=style-templates&amp;action=edit_set&amp;sid=".$sid.$expand_str,
  79              'description' => $lang->edit_set_desc
  80          );
  81      }
  82  
  83      $sub_tabs['add_template'] = array(
  84          'title' => $lang->add_template,
  85          'link' => "index.php?module=style-templates&amp;action=add_template&amp;sid=".$sid.$expand_str,
  86          'description' => $lang->add_template_desc
  87      );
  88  
  89      if($mybb->get_input('sid', MyBB::INPUT_INT) != -1)
  90      {
  91          $sub_tabs['add_template_group'] = array(
  92              'title' => $lang->add_template_group,
  93              'link' => "index.php?module=style-templates&amp;action=add_template_group&amp;sid=".$sid.$expand_str,
  94              'description' => $lang->add_template_group_desc
  95          );
  96      }
  97  }
  98  
  99  $template_sets = array();
 100  $template_sets[-1] = $lang->global_templates;
 101  
 102  $query = $db->simple_select("templatesets", "*", "", array('order_by' => 'title', 'order_dir' => 'ASC'));
 103  while($template_set = $db->fetch_array($query))
 104  {
 105      $template_sets[$template_set['sid']] = htmlspecialchars_uni($template_set['title']);
 106  }
 107  
 108  $plugins->run_hooks("admin_style_templates");
 109  
 110  if($mybb->input['action'] == "add_set")
 111  {
 112      $plugins->run_hooks("admin_style_templates_add_set");
 113  
 114      if($mybb->request_method == "post")
 115      {
 116          if(!trim($mybb->input['title']))
 117          {
 118              $errors[] = $lang->error_missing_set_title;
 119          }
 120  
 121          if(!$errors)
 122          {
 123              $sid = $db->insert_query("templatesets", array('title' => $db->escape_string($mybb->input['title'])));
 124  
 125              $plugins->run_hooks("admin_style_templates_add_set_commit");
 126  
 127              // Log admin action
 128              log_admin_action($sid, $mybb->input['title']);
 129  
 130              flash_message($lang->success_template_set_saved, 'success');
 131              admin_redirect("index.php?module=style-templates&sid=".$sid);
 132          }
 133      }
 134  
 135      $page->add_breadcrumb_item($lang->add_set);
 136  
 137      $page->output_header($lang->add_set);
 138  
 139      $sub_tabs = array();
 140      $sub_tabs['add_set'] = array(
 141          'title' => $lang->add_set,
 142          'link' => "index.php?module=style-templates&amp;action=add_set",
 143          'description' => $lang->add_set_desc
 144      );
 145  
 146      $page->output_nav_tabs($sub_tabs, 'add_set');
 147  
 148      if($errors)
 149      {
 150          $page->output_inline_error($errors);
 151      }
 152      else
 153      {
 154          $mybb->input['title'] = "";
 155      }
 156  
 157      $form = new Form("index.php?module=style-templates&amp;action=add_set", "post", "add_set");
 158  
 159      $form_container = new FormContainer($lang->add_set);
 160      $form_container->output_row($lang->title, "", $form->generate_text_box('title', htmlspecialchars_uni($mybb->input['title']), array('id' => 'title')), 'title');
 161      $form_container->end();
 162  
 163      $buttons = array();
 164      $buttons[] = $form->generate_submit_button($lang->save);
 165  
 166      $form->output_submit_wrapper($buttons);
 167  
 168      $form->end();
 169  
 170      $page->output_footer();
 171  }
 172  
 173  if($mybb->input['action'] == "add_template")
 174  {
 175      $plugins->run_hooks("admin_style_templates_add_template");
 176  
 177      if($mybb->request_method == "post")
 178      {
 179          if(empty($mybb->input['title']))
 180          {
 181              $errors[] = $lang->error_missing_set_title;
 182          }
 183          else
 184          {
 185              $query = $db->simple_select("templates", "COUNT(tid) as count", "title='".$db->escape_string($mybb->input['title'])."' AND (sid = '-2' OR sid = '{$sid}')");
 186              if($db->fetch_field($query, "count") > 0)
 187              {
 188                  $errors[] = $lang->error_already_exists;
 189              }
 190          }
 191  
 192          if(!isset($template_sets[$sid]))
 193          {
 194              $errors[] = $lang->error_invalid_set;
 195          }
 196  
 197          // Are we trying to do malicious things in our template?
 198          if(check_template($mybb->input['template']))
 199          {
 200              $errors[] = $lang->error_security_problem;
 201          }
 202  
 203          if(!$errors)
 204          {
 205              $template_array = array(
 206                  'title' => $db->escape_string($mybb->input['title']),
 207                  'sid' => $sid,
 208                  'template' => $db->escape_string(rtrim($mybb->input['template'])),
 209                  'version' => $db->escape_string($mybb->version_code),
 210                  'status' => '',
 211                  'dateline' => TIME_NOW
 212              );
 213  
 214              $tid = $db->insert_query("templates", $template_array);
 215  
 216              $plugins->run_hooks("admin_style_templates_add_template_commit");
 217  
 218              // Log admin action
 219              log_admin_action($tid, $mybb->input['title'], $sid, $template_sets[$sid]);
 220  
 221              flash_message($lang->success_template_saved, 'success');
 222  
 223              if($mybb->input['continue'])
 224              {
 225                  admin_redirect("index.php?module=style-templates&action=edit_template&title=".urlencode($mybb->input['title'])."&sid=".$sid.$expand_str2);
 226              }
 227              else
 228              {
 229                  admin_redirect("index.php?module=style-templates&sid=".$sid.$expand_str2);
 230              }
 231          }
 232      }
 233  
 234      if($errors)
 235      {
 236          $template = $mybb->input;
 237      }
 238      else
 239      {
 240          if(!$sid)
 241          {
 242              $sid = -1;
 243          }
 244  
 245          $template['title'] = "";
 246          $template['template'] = "";
 247          $template['sid'] = $sid;
 248      }
 249  
 250      if($mybb->input['sid'])
 251      {
 252          $page->add_breadcrumb_item($template_sets[$sid], "index.php?module=style-templates&amp;sid={$sid}{$expand_str}");
 253      }
 254  
 255      if($admin_options['codepress'] != 0)
 256      {
 257          $page->extra_header .= '
 258  <link href="./jscripts/codemirror/lib/codemirror.css?ver=1813" rel="stylesheet">
 259  <link href="./jscripts/codemirror/theme/mybb.css?ver=1813" rel="stylesheet">
 260  <script src="./jscripts/codemirror/lib/codemirror.js?ver=1813"></script>
 261  <script src="./jscripts/codemirror/mode/xml/xml.js?ver=1813"></script>
 262  <script src="./jscripts/codemirror/mode/javascript/javascript.js?ver=1813"></script>
 263  <script src="./jscripts/codemirror/mode/css/css.js?ver=1813"></script>
 264  <script src="./jscripts/codemirror/mode/htmlmixed/htmlmixed.js?ver=1813"></script>
 265  <link href="./jscripts/codemirror/addon/dialog/dialog-mybb.css?ver=1813" rel="stylesheet">
 266  <script src="./jscripts/codemirror/addon/dialog/dialog.js?ver=1813"></script>
 267  <script src="./jscripts/codemirror/addon/search/searchcursor.js?ver=1813"></script>
 268  <script src="./jscripts/codemirror/addon/search/search.js?ver=1821"></script>
 269  <script src="./jscripts/codemirror/addon/fold/foldcode.js?ver=1813"></script>
 270  <script src="./jscripts/codemirror/addon/fold/xml-fold.js?ver=1813"></script>
 271  <script src="./jscripts/codemirror/addon/fold/foldgutter.js?ver=1813"></script>
 272  <link href="./jscripts/codemirror/addon/fold/foldgutter.css?ver=1813" rel="stylesheet">
 273  ';
 274      }
 275  
 276      $page->add_breadcrumb_item($lang->add_template);
 277  
 278      $page->output_header($lang->add_template);
 279  
 280      $sub_tabs = array();
 281      $sub_tabs['add_template'] = array(
 282          'title' => $lang->add_template,
 283          'link' => "index.php?module=style-templates&amp;action=add_template&amp;sid=".$template['sid'].$expand_str,
 284          'description' => $lang->add_template_desc
 285      );
 286  
 287      $page->output_nav_tabs($sub_tabs, 'add_template');
 288  
 289      if($errors)
 290      {
 291          $page->output_inline_error($errors);
 292      }
 293  
 294      $form = new Form("index.php?module=style-templates&amp;action=add_template{$expand_str}", "post", "add_template");
 295  
 296      $form_container = new FormContainer($lang->add_template, 'tfixed');
 297      $form_container->output_row($lang->template_name, $lang->template_name_desc, $form->generate_text_box('title', $template['title'], array('id' => 'title')), 'title');
 298      $form_container->output_row($lang->template_set, $lang->template_set_desc, $form->generate_select_box('sid', $template_sets, $sid), 'sid');
 299      $form_container->output_row("", "", $form->generate_text_area('template', $template['template'], array('id' => 'template', 'class' => '', 'style' => 'width: 100%; height: 500px;')), 'template');
 300      $form_container->end();
 301  
 302      $buttons[] = $form->generate_submit_button($lang->save_continue, array('name' => 'continue'));
 303      $buttons[] = $form->generate_submit_button($lang->save_close, array('name' => 'close'));
 304  
 305      $form->output_submit_wrapper($buttons);
 306  
 307      $form->end();
 308  
 309      if($admin_options['codepress'] != 0)
 310      {
 311          echo '<script type="text/javascript">
 312              var editor = CodeMirror.fromTextArea(document.getElementById("template"), {
 313                  lineNumbers: true,
 314                  lineWrapping: true,
 315                  foldGutter: true,
 316                  gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"],
 317                  viewportMargin: Infinity,
 318                  indentWithTabs: true,
 319                  indentUnit: 4,
 320                  mode: "text/html",
 321                  theme: "mybb"
 322              });
 323          </script>';
 324      }
 325  
 326      $page->output_footer();
 327  }
 328  
 329  if($mybb->input['action'] == "add_template_group")
 330  {
 331      $plugins->run_hooks("admin_style_templates_add_template_group");
 332  
 333      if($mybb->get_input('sid', MyBB::INPUT_INT) == -1)
 334      {
 335          admin_redirect("index.php?module=style-templates&amp;sid={$sid}".$expand_str2);
 336      }
 337  
 338      $errors = array();
 339      if($mybb->request_method == "post")
 340      {
 341          $prefix = trim($mybb->input['prefix']);
 342          if(!$prefix)
 343          {
 344              $errors[] = $lang->error_missing_group_prefix;
 345          }
 346  
 347          if(strpos($prefix, "_") !== false)
 348          {
 349              $errors[] = $lang->error_invalid_group_title;
 350          }
 351  
 352          $title = trim($mybb->input['title']);
 353          if(!$title)
 354          {
 355              $errors[] = $lang->error_missing_group_title;
 356          }
 357  
 358          if(!$errors)
 359          {
 360              $query = $db->simple_select("templategroups", "COUNT(gid) AS gid", "prefix = '".$db->escape_string($mybb->input['prefix'])."'");
 361              $prefix_count = $db->fetch_field($query, 'gid');
 362  
 363              if($prefix_count >= 1)
 364              {
 365                  $errors[] = $lang->error_duplicate_group_prefix;
 366              }
 367              else
 368              {
 369                  // Add template group
 370                  $insert_array = array(
 371                      'prefix' => $db->escape_string($prefix),
 372                      'title' => $db->escape_string($title),
 373                      'isdefault' => 0
 374                  );
 375  
 376                  $gid = $db->insert_query('templategroups', $insert_array);
 377  
 378                  $plugins->run_hooks('admin_style_templates_add_template_group_commit');
 379  
 380                  log_admin_action($gid, $title);
 381                  flash_message($lang->success_template_group_saved, 'success');
 382  
 383                  if($sid)
 384                  {
 385                      admin_redirect("index.php?module=style-templates&amp;sid={$sid}".$expand_str2);
 386                  }
 387  
 388                  admin_redirect('index.php?module=style-templates');
 389              }
 390          }
 391      }
 392  
 393      if(!empty($mybb->input['sid']))
 394      {
 395          $page->add_breadcrumb_item($template_sets[$sid], "index.php?module=style-templates&amp;sid={$sid}{$expand_str}");
 396      }
 397  
 398      $page->add_breadcrumb_item($lang->add_template_group);
 399      $page->output_header($lang->add_template_group);
 400      $page->output_nav_tabs($sub_tabs, 'add_template_group');
 401  
 402      $template_group = array();
 403      if($errors)
 404      {
 405          $template_group = array(
 406              'prefix' => $prefix,
 407              'title' => $title
 408          );
 409  
 410          $page->output_inline_error($errors);
 411      }
 412      else
 413      {
 414          $template_group = array(
 415              'prefix' => null,
 416              'title' => null,
 417          );
 418      }
 419  
 420      $form = new Form("index.php?module=style-templates&amp;action=add_template_group{$expand_str}", "post", "add_template_group");
 421      echo $form->generate_hidden_field('sid', $sid);
 422  
 423      $form_container = new FormContainer($lang->add_template_group);
 424      $form_container->output_row($lang->template_group_prefix, $lang->template_group_prefix_desc, $form->generate_text_box('prefix', $template_group['prefix'], array('id' => 'prefix')), 'prefix');
 425      $form_container->output_row($lang->template_group_title, $lang->template_group_title_desc, $form->generate_text_box('title', $template_group['title'], array('id' => 'title')), 'title');
 426      $form_container->end();
 427  
 428      $buttons = array(
 429          $form->generate_submit_button($lang->add_template_group)
 430      );
 431  
 432      $form->output_submit_wrapper($buttons);
 433      $form->end();
 434  
 435      $page->output_footer();
 436  }
 437  
 438  if($mybb->input['action'] == "edit_set")
 439  {
 440      $query = $db->simple_select("templatesets", "*", "sid='{$sid}'");
 441      $set = $db->fetch_array($query);
 442      if(!$set)
 443      {
 444          flash_message($lang->error_invalid_input, 'error');
 445          admin_redirect("index.php?module=style-templates");
 446      }
 447  
 448      $plugins->run_hooks("admin_style_templates_edit_set");
 449  
 450      $sid = $set['sid'];
 451  
 452      if($mybb->request_method == "post")
 453      {
 454          if(!trim($mybb->input['title']))
 455          {
 456              $errors[] = $lang->error_missing_set_title;
 457          }
 458  
 459          if(!$errors)
 460          {
 461              $query = $db->update_query("templatesets", array('title' => $db->escape_string($mybb->input['title'])), "sid='{$sid}'");
 462  
 463              $plugins->run_hooks("admin_style_templates_edit_set_commit");
 464  
 465              // Log admin action
 466              log_admin_action($sid, $set['title']);
 467  
 468              flash_message($lang->success_template_set_saved, 'success');
 469              admin_redirect("index.php?module=style-templates&sid=".$sid.$expand_str2);
 470          }
 471      }
 472  
 473      if($sid)
 474      {
 475          $page->add_breadcrumb_item($template_sets[$sid], "index.php?module=style-templates&amp;sid={$sid}{$expand_str}");
 476      }
 477  
 478      $page->add_breadcrumb_item($lang->edit_set);
 479  
 480      $page->output_header($lang->edit_set);
 481  
 482      $sub_tabs = array();
 483      $sub_tabs['edit_set'] = array(
 484          'title' => $lang->edit_set,
 485          'link' => "index.php?module=style-templates&amp;action=edit_set&amp;sid=".$sid,
 486          'description' => $lang->edit_set_desc
 487      );
 488  
 489      $page->output_nav_tabs($sub_tabs, 'edit_set');
 490  
 491      if($errors)
 492      {
 493          $page->output_inline_error($errors);
 494      }
 495      else
 496      {
 497          $query = $db->simple_select("templatesets", "title", "sid='{$sid}'");
 498          $mybb->input['title'] = $db->fetch_field($query, "title");
 499      }
 500  
 501      $form = new Form("index.php?module=style-templates&amp;action=edit_set{$expand_str}", "post", "edit_set");
 502      echo $form->generate_hidden_field("sid", $sid);
 503  
 504      $form_container = new FormContainer($lang->edit_set);
 505      $form_container->output_row($lang->title, "", $form->generate_text_box('title', $mybb->input['title'], array('id' => 'title')), 'title');
 506      $form_container->end();
 507  
 508      $buttons = array();
 509      $buttons[] = $form->generate_submit_button($lang->save);
 510  
 511      $form->output_submit_wrapper($buttons);
 512  
 513      $form->end();
 514  
 515      $page->output_footer();
 516  }
 517  
 518  if($mybb->input['action'] == "edit_template")
 519  {
 520      if(!$mybb->input['title'] || !$sid || !isset($template_sets[$sid]))
 521      {
 522          flash_message($lang->error_missing_input, 'error');
 523          admin_redirect("index.php?module=style-templates");
 524      }
 525  
 526      $plugins->run_hooks("admin_style_templates_edit_template");
 527  
 528      if($mybb->request_method == "post")
 529      {
 530          if(empty($mybb->input['title']))
 531          {
 532              $errors[] = $lang->error_missing_title;
 533          }
 534  
 535          // Are we trying to do malicious things in our template?
 536          if(check_template($mybb->input['template']))
 537          {
 538              $errors[] = $lang->error_security_problem;
 539          }
 540  
 541          if(!$errors)
 542          {
 543              $query = $db->simple_select("templates", "*", "tid='{$mybb->input['tid']}'");
 544              $template = $db->fetch_array($query);
 545  
 546              $template_array = array(
 547                  'title' => $db->escape_string($mybb->input['title']),
 548                  'sid' => $sid,
 549                  'template' => $db->escape_string(rtrim($mybb->input['template'])),
 550                  'version' => $mybb->version_code,
 551                  'status' => '',
 552                  'dateline' => TIME_NOW
 553              );
 554  
 555              // Make sure we have the correct tid associated with this template. If the user double submits then the tid could originally be the master template tid, but because the form is sumbitted again, the tid doesn't get updated to the new modified template one. This then causes the master template to be overwritten
 556              $query = $db->simple_select("templates", "tid", "title='".$db->escape_string($template['title'])."' AND (sid = '-2' OR sid = '{$template['sid']}')", array('order_by' => 'sid', 'order_dir' => 'desc', 'limit' => 1));
 557              $template['tid'] = $db->fetch_field($query, "tid");
 558  
 559              $plugins->run_hooks("admin_style_templates_edit_template_commit_start");
 560  
 561              if($sid > 0)
 562              {
 563                  // Check to see if it's never been edited before (i.e. master) or if this a new template (i.e. we've renamed it)  or if it's a custom template
 564                  $query = $db->simple_select("templates", "sid", "title='".$db->escape_string($mybb->input['title'])."' AND (sid = '-2' OR sid = '{$sid}' OR sid='{$template['sid']}')", array('order_by' => 'sid', 'order_dir' => 'desc'));
 565                  $existing_sid = $db->fetch_field($query, "sid");
 566                  $existing_rows = $db->num_rows($query);
 567  
 568                  if(($existing_sid == -2 && $existing_rows == 1) || $existing_rows == 0)
 569                  {
 570                      $template['tid'] = $db->insert_query("templates", $template_array);
 571                  }
 572                  else
 573                  {
 574                      $db->update_query("templates", $template_array, "tid='{$template['tid']}' AND sid != '-2'");
 575                  }
 576              }
 577              else
 578              {
 579                  // Global template set
 580                  $db->update_query("templates", $template_array, "tid='{$template['tid']}' AND sid != '-2'");
 581              }
 582  
 583              $plugins->run_hooks("admin_style_templates_edit_template_commit");
 584  
 585              $query = $db->simple_select("templatesets", "title", "sid='{$sid}'");
 586              $set = $db->fetch_array($query);
 587  
 588              $exploded = explode("_", $template_array['title'], 2);
 589              $prefix = $exploded[0];
 590  
 591              $query = $db->simple_select("templategroups", "gid", "prefix = '".$db->escape_string($prefix)."'");
 592              $group = $db->fetch_field($query, "gid");
 593  
 594              if(!$group)
 595              {
 596                  $group = "-1";
 597              }
 598  
 599              // Log admin action
 600              if(!empty($set['title']))
 601              {
 602                  $title = $set['title'];
 603              }
 604              else
 605              {
 606                  $title = null;
 607              }
 608  
 609              log_admin_action($template['tid'], $mybb->get_input('title'), $mybb->get_input('sid'), $title);
 610  
 611              flash_message($lang->success_template_saved, 'success');
 612  
 613              if($mybb->get_input('continue'))
 614              {
 615                  if($mybb->get_input('from') == "diff_report")
 616                  {
 617                      admin_redirect("index.php?module=style-templates&action=edit_template&title=".urlencode($mybb->input['title'])."&sid=".$mybb->get_input('sid', MyBB::INPUT_INT).$expand_str2."&amp;from=diff_report");
 618                  }
 619                  else
 620                  {
 621                      admin_redirect("index.php?module=style-templates&action=edit_template&title=".urlencode($mybb->input['title'])."&sid=".$mybb->get_input('sid', MyBB::INPUT_INT).$expand_str2);
 622                  }
 623              }
 624              else
 625              {
 626                  if($mybb->get_input('from') == "diff_report")
 627                  {
 628                      admin_redirect("index.php?module=style-templates&amp;action=find_updated");
 629                  }
 630                  else
 631                  {
 632                      admin_redirect("index.php?module=style-templates&sid=".$mybb->get_input('sid', MyBB::INPUT_INT).$expand_str2."#group_{$group}");
 633                  }
 634              }
 635          }
 636      }
 637  
 638      if($errors)
 639      {
 640          $template = $mybb->input;
 641      }
 642      else
 643      {
 644          $query = $db->simple_select("templates", "*", "title='".$db->escape_string($mybb->input['title'])."' AND (sid='-2' OR sid='{$sid}')", array('order_by' => 'sid', 'order_dir' => 'DESC', 'limit' => 1));
 645          $template = $db->fetch_array($query);
 646      }
 647      $template['title'] = htmlspecialchars_uni($template['title']);
 648  
 649      if($admin_options['codepress'] != 0)
 650      {
 651          $page->extra_header .= '
 652  <link href="./jscripts/codemirror/lib/codemirror.css?ver=1813" rel="stylesheet">
 653  <link href="./jscripts/codemirror/theme/mybb.css?ver=1813" rel="stylesheet">
 654  <script src="./jscripts/codemirror/lib/codemirror.js?ver=1813"></script>
 655  <script src="./jscripts/codemirror/mode/xml/xml.js?ver=1813"></script>
 656  <script src="./jscripts/codemirror/mode/javascript/javascript.js?ver=1813"></script>
 657  <script src="./jscripts/codemirror/mode/css/css.js?ver=1813"></script>
 658  <script src="./jscripts/codemirror/mode/htmlmixed/htmlmixed.js?ver=1813"></script>
 659  <link href="./jscripts/codemirror/addon/dialog/dialog-mybb.css?ver=1813" rel="stylesheet">
 660  <script src="./jscripts/codemirror/addon/dialog/dialog.js?ver=1813"></script>
 661  <script src="./jscripts/codemirror/addon/search/searchcursor.js?ver=1813"></script>
 662  <script src="./jscripts/codemirror/addon/search/search.js?ver=1821"></script>
 663  <script src="./jscripts/codemirror/addon/fold/foldcode.js?ver=1813"></script>
 664  <script src="./jscripts/codemirror/addon/fold/xml-fold.js?ver=1813"></script>
 665  <script src="./jscripts/codemirror/addon/fold/foldgutter.js?ver=1813"></script>
 666  <link href="./jscripts/codemirror/addon/fold/foldgutter.css?ver=1813" rel="stylesheet">
 667  ';
 668      }
 669  
 670      $page->add_breadcrumb_item($template_sets[$sid], "index.php?module=style-templates&amp;sid={$sid}{$expand_str}");
 671  
 672      if(!isset($mybb->input['from']))
 673      {
 674          $mybb->input['from'] = '';
 675      }
 676  
 677      if($mybb->input['from'] == "diff_report")
 678      {
 679          $page->add_breadcrumb_item($lang->find_updated, "index.php?module=style-templates&amp;action=find_updated");
 680      }
 681  
 682      $page->add_breadcrumb_item($lang->edit_template_breadcrumb.$template['title'], "index.php?module=style-templates&amp;sid={$sid}");
 683  
 684      $page->output_header($lang->sprintf($lang->editing_template, $template['title']));
 685  
 686  
 687      $sub_tabs = array();
 688  
 689      if($mybb->input['from'] == "diff_report")
 690      {
 691          $sub_tabs['find_updated'] = array(
 692              'title' => $lang->find_updated,
 693              'link' => "index.php?module=style-templates&amp;action=find_updated"
 694          );
 695  
 696          $sub_tabs['diff_report'] = array(
 697              'title' => $lang->diff_report,
 698              'link' => "index.php?module=style-templates&amp;action=diff_report&amp;title=".urlencode($template['title'])."&amp;sid1=".(int)$template['sid']."&amp;sid2=-2",
 699          );
 700      }
 701  
 702      $sub_tabs['edit_template'] = array(
 703          'title' => $lang->edit_template,
 704          'link' => "index.php?module=style-templates&amp;action=edit_template&amp;title=".htmlspecialchars_uni($template['title']).$expand_str,
 705          'description' => $lang->edit_template_desc
 706      );
 707  
 708      $page->output_nav_tabs($sub_tabs, 'edit_template');
 709  
 710      if($errors)
 711      {
 712          $page->output_inline_error($errors);
 713      }
 714  
 715      $form = new Form("index.php?module=style-templates&amp;action=edit_template{$expand_str}", "post", "edit_template");
 716      echo $form->generate_hidden_field('tid', $template['tid'])."\n";
 717  
 718      if($mybb->input['from'] == "diff_report")
 719      {
 720          echo $form->generate_hidden_field('from', "diff_report");
 721      }
 722  
 723      $form_container = new FormContainer($lang->edit_template_breadcrumb.$template['title'], 'tfixed');
 724      $form_container->output_row($lang->template_name, $lang->template_name_desc, $form->generate_text_box('title', $template['title'], array('id' => 'title')), 'title');
 725  
 726      // Force users to save the default template to a specific set, rather than the "global" templates - where they can delete it
 727      if($template['sid'] == "-2")
 728      {
 729          unset($template_sets[-1]);
 730      }
 731  
 732      $form_container->output_row($lang->template_set, $lang->template_set_desc, $form->generate_select_box('sid', $template_sets, $sid));
 733  
 734      $form_container->output_row("", "", $form->generate_text_area('template', $template['template'], array('id' => 'template', 'class' => '', 'style' => 'width: 100%; height: 500px;')));
 735      $form_container->end();
 736  
 737      $buttons[] = $form->generate_submit_button($lang->save_continue, array('name' => 'continue'));
 738      $buttons[] = $form->generate_submit_button($lang->save_close, array('name' => 'close'));
 739  
 740      $form->output_submit_wrapper($buttons);
 741  
 742      $form->end();
 743  
 744      if($admin_options['codepress'] != 0)
 745      {
 746          echo '<script type="text/javascript">
 747              var editor = CodeMirror.fromTextArea(document.getElementById("template"), {
 748                  lineNumbers: true,
 749                  lineWrapping: true,
 750                  foldGutter: true,
 751                  gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"],
 752                  viewportMargin: Infinity,
 753                  indentWithTabs: true,
 754                  indentUnit: 4,
 755                  mode: "text/html",
 756                  theme: "mybb"
 757              });
 758          </script>';
 759      }
 760  
 761      $page->output_footer();
 762  }
 763  
 764  if($mybb->input['action'] == "edit_template_group")
 765  {
 766      $query = $db->simple_select("templategroups", "*", "gid = '".$mybb->get_input('gid', MyBB::INPUT_INT)."'");
 767  
 768      if(!$db->num_rows($query))
 769      {
 770          flash_message($lang->error_missing_template_group, 'error');
 771          admin_redirect("index.php?module=style-templates&amp;sid={$sid}{$expand_str}");
 772      }
 773  
 774      $template_group = $db->fetch_array($query);
 775      if(isset($template_group['isdefault']) && $template_group['isdefault'] == 1)
 776      {
 777          flash_message($lang->error_default_template_group, 'error');
 778          admin_redirect("index.php?module=style-templates&amp;sid={$sid}{$expand_str}");
 779      }
 780  
 781      $plugins->run_hooks("admin_style_templates_edit_template_group");
 782  
 783      $errors = array();
 784      if($mybb->request_method == "post")
 785      {
 786          $prefix = trim($mybb->input['prefix']);
 787          if(!$prefix)
 788          {
 789              $errors[] = $lang->error_missing_group_prefix;
 790          }
 791  
 792          if(strpos($prefix, "_") !== false)
 793          {
 794              $errors[] = $lang->error_invalid_group_title;
 795          }
 796  
 797          $title = trim($mybb->input['title']);
 798          if(!$title)
 799          {
 800              $errors[] = $lang->error_missing_group_title;
 801          }
 802  
 803          if(!$errors)
 804          {
 805              if($prefix != $template_group['prefix'])
 806              {
 807                  $query = $db->simple_select("templategroups", "COUNT(gid) AS gid", "prefix = '".$db->escape_string($mybb->input['prefix'])."'");
 808                  $prefix_count = $db->fetch_field($query, 'gid');
 809  
 810                  if($prefix_count >= 1)
 811                  {
 812                      $errors[] = $lang->error_duplicate_group_prefix;
 813                  }
 814              }
 815  
 816              if(!$errors)
 817              {
 818                  // Add template group
 819                  $update_array = array(
 820                      'prefix' => $db->escape_string($prefix),
 821                      'title' => $db->escape_string($title),
 822                      'isdefault' => 0
 823                  );
 824  
 825                  $plugins->run_hooks('admin_style_templates_edit_template_group_commit');
 826  
 827                  $db->update_query('templategroups', $update_array, "gid = '{$template_group['gid']}'");
 828  
 829                  log_admin_action($template_group['gid'], $title);
 830                  flash_message($lang->success_template_group_saved, 'success');
 831                  admin_redirect("index.php?module=style-templates&amp;sid={$sid}");
 832              }
 833          }
 834      }
 835  
 836      $lang->editing_template_group = $lang->sprintf($lang->editing_template_group, htmlspecialchars_uni($template_group['title']));
 837  
 838      $page->add_breadcrumb_item($template_sets[$sid], "index.php?module=style-templates&amp;sid={$sid}{$expand_str}");
 839      $page->add_breadcrumb_item($lang->editing_template_group, "index.php?module=style-templates&amp;sid={$sid}");
 840  
 841      $page->output_header($lang->editing_template_group);
 842  
 843      if($errors)
 844      {
 845          $template_group['prefix'] = $prefix;
 846          $template_group['title'] = $title;
 847  
 848          $page->output_inline_error($errors);
 849      }
 850  
 851      $form = new Form("index.php?module=style-templates&amp;action=edit_template_group", "post");
 852      echo $form->generate_hidden_field('sid', $sid);
 853      echo $form->generate_hidden_field('gid', $template_group['gid']);
 854  
 855      $form_container = new FormContainer($lang->edit_template_group);
 856      $form_container->output_row($lang->template_group_prefix, $lang->template_group_prefix_desc, $form->generate_text_box('prefix', $template_group['prefix'], array('id' => 'prefix')), 'prefix');
 857      $form_container->output_row($lang->template_group_title, $lang->template_group_title_desc, $form->generate_text_box('title', $template_group['title'], array('id' => 'title')), 'title');
 858      $form_container->end();
 859  
 860      $buttons = array(
 861          $form->generate_submit_button($lang->save_template_group)
 862      );
 863  
 864      $form->output_submit_wrapper($buttons);
 865      $form->end();
 866  
 867      $page->output_footer();
 868  }
 869  
 870  if($mybb->input['action'] == "search_replace")
 871  {
 872      $plugins->run_hooks("admin_style_templates_search_replace");
 873  
 874      if($mybb->request_method == "post")
 875      {
 876          if($mybb->input['type'] == "templates")
 877          {
 878              // Search and replace in templates
 879  
 880              if(!$mybb->input['find'])
 881              {
 882                  flash_message($lang->search_noneset, "error");
 883                  admin_redirect("index.php?module=style-templates&action=search_replace");
 884              }
 885              else
 886              {
 887                  $page->add_breadcrumb_item($lang->search_replace);
 888  
 889                  $page->output_header($lang->search_replace);
 890  
 891                  $plugins->run_hooks("admin_style_templates_search_replace_find");
 892  
 893                  $page->output_nav_tabs($sub_tabs, 'search_replace');
 894  
 895                  $templates_list = array();
 896                  $table = new Table;
 897  
 898                  $template_sets = array();
 899  
 900                  // Get the names of all template sets
 901                  $template_sets[-2] = $lang->master_templates;
 902                  $template_sets[-1] = $lang->global_templates;
 903  
 904                  $query = $db->simple_select("templatesets", "sid, title");
 905                  while($set = $db->fetch_array($query))
 906                  {
 907                      $template_sets[$set['sid']] = $set['title'];
 908                  }
 909  
 910                  $search_string = str_replace(array("\r\n", "\r"), "\n", $mybb->input['find']);
 911                  $search_string = str_replace(array("  ", "\t"), "", $search_string);
 912  
 913                  // Select all templates with that search term
 914                  $query = $db->query("
 915                      SELECT t.tid, t.title, t.sid, t.template
 916                      FROM ".TABLE_PREFIX."templates t
 917                      LEFT JOIN ".TABLE_PREFIX."templatesets s ON (t.sid=s.sid)
 918                      LEFT JOIN ".TABLE_PREFIX."templates t2 ON (t.title=t2.title AND t2.sid='1')
 919                      WHERE REPLACE(REPLACE(REPLACE(REPLACE(t.template, '\r\n', '\n'), '\r', '\n'), '  ', ''), '\t', '') LIKE '%".$db->escape_string_like($search_string)."%' AND NOT (t.sid = -2 AND (t2.tid) IS NOT NULL)
 920                      ORDER BY t.title ASC
 921                  ");
 922                  if($db->num_rows($query) == 0)
 923                  {
 924                      $table->construct_cell($lang->sprintf($lang->search_noresults, htmlspecialchars_uni($mybb->input['find'])), array("class" => "align_center"));
 925  
 926                      $table->construct_row();
 927  
 928                      $table->output($lang->search_results);
 929                  }
 930                  else
 931                  {
 932                      $template_list = array();
 933                      while($template = $db->fetch_array($query))
 934                      {
 935                          $template_list[$template['sid']][$template['title']] = $template;
 936                      }
 937  
 938                      $count = 0;
 939  
 940                      foreach($template_list as $sid => $templates)
 941                      {
 942                          ++$count;
 943  
 944                          $search_header = $lang->sprintf($lang->search_header, htmlspecialchars_uni($mybb->input['find']), $template_sets[$sid]);
 945                          $table->construct_header($search_header, array("colspan" => 2));
 946  
 947                          foreach($templates as $title => $template)
 948                          {
 949                              // Do replacement
 950                              $newtemplate = str_ireplace($mybb->input['find'], $mybb->input['replace'], $template['template']);
 951                              if($newtemplate != $template['template'] && check_template($newtemplate) === false)
 952                              {
 953                                  // If the template is different, that means the search term has been found.
 954                                  if(trim($mybb->input['replace']) != "")
 955                                  {
 956                                      if($template['sid'] == -2)
 957                                      {
 958                                          // The template is a master template.  We have to make a new custom template.
 959                                          $new_template = array(
 960                                              "title" => $db->escape_string($title),
 961                                              "template" => $db->escape_string($newtemplate),
 962                                              "sid" => 1,
 963                                              "version" => $mybb->version_code,
 964                                              "status" => '',
 965                                              "dateline" => TIME_NOW
 966                                          );
 967                                          $new_tid = $db->insert_query("templates", $new_template);
 968                                          $label = $lang->sprintf($lang->search_created_custom, $template['title']);
 969                                          $url = "index.php?module=style-templates&amp;action=edit_template&amp;title=".urlencode($template['title'])."&amp;sid=1";
 970                                      }
 971                                      else
 972                                      {
 973                                          // The template is a custom template.  Replace as normal.
 974                                          // Update the template if there is a replacement term
 975                                          $updatedtemplate = array(
 976                                              "template" => $db->escape_string($newtemplate)
 977                                          );
 978                                          $db->update_query("templates", $updatedtemplate, "tid='".$template['tid']."'");
 979                                          $label = $lang->sprintf($lang->search_updated, $template['title']);
 980                                          $url = "index.php?module=style-templates&amp;action=edit_template&amp;title=".urlencode($template['title'])."&amp;sid={$template['sid']}";
 981                                      }
 982                                  }
 983                                  else
 984                                  {
 985                                      // Just show that the term was found
 986                                      if($template['sid'] == -2)
 987                                      {
 988                                          $label = $lang->sprintf($lang->search_found, $template['title']);
 989                                      }
 990                                      else
 991                                      {
 992                                          $label = $lang->sprintf($lang->search_found, $template['title']);
 993                                          $url = "index.php?module=style-templates&amp;action=edit_template&amp;title=".urlencode($template['title'])."&amp;sid={$template['sid']}";
 994                                      }
 995                                  }
 996                              }
 997                              else
 998                              {
 999                                  // Just show that the term was found
1000                                  if($template['sid'] == -2)
1001                                  {
1002                                      $label = $lang->sprintf($lang->search_found, $template['title']);
1003                                  }
1004                                  else
1005                                  {
1006                                      $label = $lang->sprintf($lang->search_found, $template['title']);
1007                                      $url = "index.php?module=style-templates&amp;action=edit_template&amp;title=".urlencode($template['title'])."&amp;sid={$template['sid']}";
1008                                  }
1009                              }
1010  
1011                              $table->construct_cell(htmlspecialchars_uni($label), array("width" => "85%"));
1012  
1013                              if($sid == -2)
1014                              {
1015                                  $popup = new PopupMenu("template_{$template['tid']}", $lang->options);
1016  
1017                                  foreach($template_sets as $set_sid => $title)
1018                                  {
1019                                      if($set_sid > 0)
1020                                      {
1021                                          $popup->add_item($lang->edit_in." ".htmlspecialchars_uni($title), "index.php?module=style-templates&amp;action=edit_template&amp;title=".urlencode($template['title'])."&amp;sid={$set_sid}");
1022                                      }
1023                                  }
1024  
1025                                  $table->construct_cell($popup->fetch(), array("class" => "align_center"));
1026                              }
1027                              else
1028                              {
1029                                  $table->construct_cell("<a href=\"{$url}\">{$lang->edit}</a>", array("class" => "align_center"));
1030                              }
1031  
1032                              $table->construct_row();
1033                          }
1034  
1035                          if($count == 1)
1036                          {
1037                              $table->output($lang->search_results);
1038                          }
1039                          else
1040                          {
1041                              $table->output();
1042                          }
1043                      }
1044                  }
1045  
1046                  if(trim($mybb->input['replace']) != "")
1047                  {
1048                      // Log admin action - only if replace
1049                      log_admin_action($mybb->input['find'], $mybb->input['replace']);
1050                  }
1051  
1052                  $page->output_footer();
1053                  exit;
1054              }
1055          }
1056          else
1057          {
1058              if(!$mybb->input['title'])
1059              {
1060                  flash_message($lang->search_noneset, "error");
1061                  admin_redirect("index.php?module=style-templates&action=search_replace");
1062              }
1063              else
1064              {
1065                  // Search Template Titles
1066  
1067                  $templatessets = array();
1068  
1069                  $templates_sets = array();
1070                  // Get the names of all template sets
1071                  $template_sets[-2] = $lang->master_templates;
1072                  $template_sets[-1] = $lang->global_templates;
1073  
1074                  $plugins->run_hooks("admin_style_templates_search_replace_title");
1075  
1076                  $query = $db->simple_select("templatesets", "sid, title");
1077                  while($set = $db->fetch_array($query))
1078                  {
1079                      $template_sets[$set['sid']] = $set['title'];
1080                  }
1081  
1082                  $table = new Table;
1083  
1084                  $query = $db->query("
1085                      SELECT t.tid, t.title, t.sid, s.title as settitle, t2.tid as customtid
1086                      FROM ".TABLE_PREFIX."templates t
1087                      LEFT JOIN ".TABLE_PREFIX."templatesets s ON (t.sid=s.sid)
1088                      LEFT JOIN ".TABLE_PREFIX."templates t2 ON (t.title=t2.title AND t2.sid='1')
1089                      WHERE t.title LIKE '%".$db->escape_string_like($mybb->input['title'])."%'
1090                      ORDER BY t.title ASC
1091                  ");
1092                  while($template = $db->fetch_array($query))
1093                  {
1094                      if($template['sid'] == -2)
1095                      {
1096                          if(!$template['customtid'])
1097                          {
1098                              $template['original'] = true;
1099                          }
1100                          else
1101                          {
1102                              $template['modified'] = true;
1103                          }
1104                      }
1105                      else
1106                      {
1107                          $template['original'] = false;
1108                          $template['modified'] = false;
1109                      }
1110                      $templatessets[$template['sid']][$template['title']] = $template;
1111                  }
1112  
1113                  $page->add_breadcrumb_item($lang->search_replace);
1114  
1115                  $page->output_header($lang->search_replace);
1116  
1117                  $page->output_nav_tabs($sub_tabs, 'search_replace');
1118  
1119                  if(empty($templatessets))
1120                  {
1121                      $table->construct_cell($lang->sprintf($lang->search_noresults_title, htmlspecialchars_uni($mybb->input['title'])), array("class" => "align_center"));
1122  
1123                      $table->construct_row();
1124  
1125                      $table->output($lang->search_results);
1126                  }
1127  
1128                  $count = 0;
1129  
1130                  foreach($templatessets as $sid => $templates)
1131                  {
1132                      ++$count;
1133  
1134                      $table->construct_header($template_sets[$sid], array("colspan" => 2));
1135  
1136                      foreach($templates as $template)
1137                      {
1138                          $template['pretty_title'] = htmlspecialchars_uni($template['title']);
1139  
1140                          $popup = new PopupMenu("template_{$template['tid']}", $lang->options);
1141  
1142                          if($sid == -2)
1143                          {
1144                              foreach($template_sets as $set_sid => $title)
1145                              {
1146                                  if($set_sid < 0) continue;
1147  
1148                                  $popup->add_item($lang->edit_in." ".htmlspecialchars_uni($title), "index.php?module=style-templates&amp;action=edit_template&amp;title=".urlencode($template['title'])."&amp;sid={$set_sid}");
1149                              }
1150                          }
1151                          else
1152                          {
1153                              $popup->add_item($lang->full_edit, "index.php?module=style-templates&amp;action=edit_template&amp;title=".urlencode($template['title'])."&amp;sid={$sid}");
1154                          }
1155  
1156                          if(isset($template['modified']) && $template['modified'] == true)
1157                          {
1158                              if($sid > 0)
1159                              {
1160                                  $popup->add_item($lang->diff_report, "index.php?module=style-templates&amp;action=diff_report&amp;title=".urlencode($template['title'])."&amp;sid2={$sid}");
1161  
1162                                  $popup->add_item($lang->revert_to_orig, "index.php?module=style-templates&amp;action=revert&amp;title=".urlencode($template['title'])."&amp;sid={$sid}&amp;my_post_key={$mybb->post_code}", "return AdminCP.deleteConfirmation(this, '{$lang->confirm_template_revertion}')");
1163                              }
1164  
1165                              $template['pretty_title'] = "<span style=\"color: green;\">".htmlspecialchars_uni($template['title'])."</span>";
1166                          }
1167                          // This template does not exist in the master list
1168                          else if(!isset($template['original']) || $template['original'] == false)
1169                          {
1170                              $popup->add_item($lang->delete_template, "index.php?module=style-templates&amp;action=delete_template&amp;title=".urlencode($template['title'])."&amp;sid={$sid}&amp;my_post_key={$mybb->post_code}", "return AdminCP.deleteConfirmation(this, '{$lang->confirm_template_deletion}')");
1171  
1172                              $template['pretty_title'] = "<span style=\"color: blue;\">".htmlspecialchars_uni($template['title'])."</span>";
1173                          }
1174  
1175                          $table->construct_cell("<span style=\"padding: 20px;\">{$template['pretty_title']}</span>", array("width" => "85%"));
1176                          $table->construct_cell($popup->fetch(), array("class" => "align_center"));
1177  
1178                          $table->construct_row();
1179                      }
1180  
1181                      if($count == 1)
1182                      {
1183                          $table->output($lang->sprintf($lang->search_names_header, htmlspecialchars_uni($mybb->input['title'])));
1184                      }
1185                      else if($count > 0)
1186                      {
1187                          $table->output();
1188                      }
1189                  }
1190  
1191                  $page->output_footer();
1192                  exit;
1193              }
1194          }
1195      }
1196  
1197      if($admin_options['codepress'] != 0)
1198      {
1199          $page->extra_header .= '
1200  <link href="./jscripts/codemirror/lib/codemirror.css?ver=1813" rel="stylesheet">
1201  <link href="./jscripts/codemirror/theme/mybb.css?ver=1813" rel="stylesheet">
1202  <script src="./jscripts/codemirror/lib/codemirror.js?ver=1813"></script>
1203  <script src="./jscripts/codemirror/mode/xml/xml.js?ver=1813"></script>
1204  <script src="./jscripts/codemirror/mode/javascript/javascript.js?ver=1813"></script>
1205  <script src="./jscripts/codemirror/mode/css/css.js?ver=1813"></script>
1206  <script src="./jscripts/codemirror/mode/htmlmixed/htmlmixed.js?ver=1813"></script>
1207  <link href="./jscripts/codemirror/addon/dialog/dialog-mybb.css?ver=1813" rel="stylesheet">
1208  <script src="./jscripts/codemirror/addon/dialog/dialog.js?ver=1813"></script>
1209  <script src="./jscripts/codemirror/addon/search/searchcursor.js?ver=1813"></script>
1210  <script src="./jscripts/codemirror/addon/search/search.js?ver=1821"></script>
1211  <script src="./jscripts/codemirror/addon/fold/foldcode.js?ver=1813"></script>
1212  <script src="./jscripts/codemirror/addon/fold/xml-fold.js?ver=1813"></script>
1213  <script src="./jscripts/codemirror/addon/fold/foldgutter.js?ver=1813"></script>
1214  <link href="./jscripts/codemirror/addon/fold/foldgutter.css?ver=1813" rel="stylesheet">
1215  ';
1216      }
1217  
1218      $page->add_breadcrumb_item($lang->search_replace);
1219  
1220      $page->output_header($lang->search_replace);
1221  
1222      $page->output_nav_tabs($sub_tabs, 'search_replace');
1223  
1224      $form = new Form("index.php?module=style-templates&amp;action=search_replace", "post", "do_template");
1225      echo $form->generate_hidden_field('type', "templates");
1226  
1227      $form_container = new FormContainer($lang->search_replace, 'tfixed');
1228      $form_container->output_row($lang->search_for, "", $form->generate_text_area('find', $mybb->get_input('find'), array('id' => 'find', 'class' => '', 'style' => 'width: 100%; height: 200px;')));
1229  
1230      $form_container->output_row($lang->replace_with, "", $form->generate_text_area('replace', $mybb->get_input('replace'), array('id' => 'replace', 'class' => '', 'style' => 'width: 100%; height: 200px;')));
1231      $form_container->end();
1232  
1233      $buttons[] = $form->generate_submit_button($lang->find_and_replace);
1234  
1235      $form->output_submit_wrapper($buttons);
1236  
1237      $form->end();
1238  
1239      echo "<br />";
1240  
1241  
1242      $form = new Form("index.php?module=style-templates&amp;action=search_replace", "post", "do_title");
1243      echo $form->generate_hidden_field('type', "titles");
1244  
1245      $form_container = new FormContainer($lang->search_template_names);
1246  
1247      $form_container->output_row($lang->search_for, "", $form->generate_text_box('title', $mybb->get_input('title'), array('id' => 'title')), 'title');
1248  
1249      $form_container->end();
1250  
1251      $buttons = array();
1252      $buttons[] = $form->generate_submit_button($lang->find_templates);
1253      $buttons[] = $form->generate_reset_button($lang->reset);
1254  
1255      $form->output_submit_wrapper($buttons);
1256  
1257      $form->end();
1258  
1259      if($admin_options['codepress'] != 0)
1260      {
1261          echo '<script type="text/javascript">
1262              var editor1 = CodeMirror.fromTextArea(document.getElementById("find"), {
1263                  lineNumbers: true,
1264                  lineWrapping: true,
1265                  foldGutter: true,
1266                  gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"],
1267                  viewportMargin: Infinity,
1268                  indentWithTabs: true,
1269                  indentUnit: 4,
1270                  mode: "text/html",
1271                  theme: "mybb"
1272              });
1273  
1274              var editor2 = CodeMirror.fromTextArea(document.getElementById("replace"), {
1275                  lineNumbers: true,
1276                  lineWrapping: true,
1277                  foldGutter: true,
1278                  gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"],
1279                  viewportMargin: Infinity,
1280                  indentWithTabs: true,
1281                  indentUnit: 4,
1282                  mode: "text/html",
1283                  theme: "mybb"
1284              });
1285          </script>';
1286      }
1287  
1288      $page->output_footer();
1289  }
1290  
1291  if($mybb->input['action'] == "find_updated")
1292  {
1293      // Finds templates that are old and have been updated by MyBB
1294      $compare_version = $mybb->version_code;
1295      $query = $db->query("
1296          SELECT COUNT(*) AS updated_count
1297          FROM ".TABLE_PREFIX."templates t
1298          LEFT JOIN ".TABLE_PREFIX."templates m ON (m.title=t.title AND m.sid=-2 AND m.version > t.version)
1299          WHERE t.sid > 0 AND m.template != t.template
1300      ");
1301      $count = $db->fetch_array($query);
1302  
1303      if($count['updated_count'] < 1)
1304      {
1305          flash_message($lang->no_updated_templates, 'success');
1306          admin_redirect("index.php?module=style-templates");
1307      }
1308  
1309      $plugins->run_hooks("admin_style_templates_find_updated");
1310  
1311      $page->add_breadcrumb_item($lang->find_updated, "index.php?module=style-templates&amp;action=find_updated");
1312  
1313      $page->output_header($lang->find_updated);
1314  
1315      $page->output_nav_tabs($sub_tabs, 'find_updated');
1316  
1317      $query = $db->simple_select("templatesets", "*", "", array('order_by' => 'title'));
1318      while($templateset = $db->fetch_array($query))
1319      {
1320          $templatesets[$templateset['sid']] = $templateset;
1321      }
1322  
1323      echo <<<LEGEND
1324      <fieldset>
1325  <legend>{$lang->legend}</legend>
1326  <ul>
1327  <li>{$lang->updated_template_welcome1}</li>
1328  <li>{$lang->updated_template_welcome2}</li>
1329  <li>{$lang->updated_template_welcome3}</li>
1330  </ul>
1331  </fieldset>
1332  LEGEND;
1333  
1334      $count = 0;
1335      $done_set = array();
1336      $done_output = array();
1337      $templates = array();
1338      $table = new Table;
1339  
1340      $query = $db->query("
1341          SELECT t.tid, t.title, t.sid, t.version
1342          FROM ".TABLE_PREFIX."templates t
1343          LEFT JOIN ".TABLE_PREFIX."templates m ON (m.title=t.title AND m.sid=-2 AND m.version > t.version)
1344          WHERE t.sid > 0 AND m.template != t.template
1345          ORDER BY t.sid ASC, t.title ASC
1346      ");
1347      while($template = $db->fetch_array($query))
1348      {
1349          $templates[$template['sid']][] = $template;
1350      }
1351  
1352      foreach($templates as $sid => $templates)
1353      {
1354          if(empty($done_set[$sid]))
1355          {
1356              $table->construct_header(htmlspecialchars_uni($templatesets[$sid]['title']), array("colspan" => 2));
1357  
1358              $done_set[$sid] = 1;
1359              ++$count;
1360          }
1361  
1362          foreach($templates as $template)
1363          {
1364              $popup = new PopupMenu("template_{$template['tid']}", $lang->options);
1365              $popup->add_item($lang->full_edit, "index.php?module=style-templates&amp;action=edit_template&amp;title=".urlencode($template['title'])."&amp;sid={$sid}&amp;from=diff_report");
1366              $popup->add_item($lang->diff_report, "index.php?module=style-templates&amp;action=diff_report&amp;title=".urlencode($template['title'])."&amp;sid1=".$template['sid']."&amp;sid2=-2&amp;from=diff_report");
1367              $popup->add_item($lang->revert_to_orig, "index.php?module=style-templates&amp;action=revert&amp;title=".urlencode($template['title'])."&amp;sid={$sid}&amp;from=diff_report&amp;my_post_key={$mybb->post_code}", "return AdminCP.deleteConfirmation(this, '{$lang->confirm_template_revertion}')");
1368  
1369              $table->construct_cell("<a href=\"index.php?module=style-templates&amp;action=edit_template&amp;title=".urlencode($template['title'])."&amp;sid={$sid}&amp;from=diff_report\">".htmlspecialchars_uni($template['title'])."</a>", array('width' => '80%'));
1370              $table->construct_cell($popup->fetch(), array("class" => "align_center"));
1371  
1372              $table->construct_row();
1373          }
1374  
1375          if(!empty($done_set[$sid]) && empty($done_output[$sid]))
1376          {
1377              $done_output[$sid] = 1;
1378              if($count == 1)
1379              {
1380                  $table->output($lang->find_updated);
1381              }
1382              else
1383              {
1384                  $table->output();
1385              }
1386          }
1387      }
1388  
1389      $page->output_footer();
1390  }
1391  
1392  if($mybb->input['action'] == "delete_template_group")
1393  {
1394      $gid = $mybb->get_input('gid', MyBB::INPUT_INT);
1395      $query = $db->simple_select("templategroups", "*", "gid='{$gid}'");
1396  
1397      if(!$db->num_rows($query))
1398      {
1399          flash_message($lang->error_missing_template_group, 'error');
1400          admin_redirect("index.php?module=style-templates&amp;sid={$sid}");
1401      }
1402  
1403      // User clicked no
1404      if($mybb->get_input('no'))
1405      {
1406          admin_redirect("index.php?module=style-templates&amp;sid={$sid}");
1407      }
1408  
1409      $plugins->run_hooks("admin_style_template_group_delete");
1410  
1411      $template_group = $db->fetch_array($query);
1412  
1413      if($mybb->request_method == "post")
1414      {
1415          // Delete the group
1416          $db->delete_query("templategroups", "gid = '{$template_group['gid']}'");
1417  
1418          $plugins->run_hooks("admin_style_template_group_delete_commit");
1419  
1420          // Log admin action
1421          log_admin_action($template_group['gid'], $template_group['title']);
1422  
1423          flash_message($lang->success_template_group_deleted, 'success');
1424          admin_redirect("index.php?module=style-templates&amp;sid={$sid}");
1425      }
1426      else
1427      {
1428          $page->output_confirm_action("index.php?module=style-templates&amp;action=delete_template_group&amp;gid={$template_group['gid']}&amp;sid={$sid}", $lang->confirm_template_group_delete);
1429      }
1430  }
1431  
1432  if($mybb->input['action'] == "delete_set")
1433  {
1434      $query = $db->simple_select("templatesets", "*", "sid='{$sid}' AND sid > 0");
1435      $set = $db->fetch_array($query);
1436  
1437      // Does the template not exist?
1438      if(!$set)
1439      {
1440          flash_message($lang->error_invalid_template_set, 'error');
1441          admin_redirect("index.php?module=style-templates");
1442      }
1443  
1444      $plugins->run_hooks("admin_style_templates_delete_set");
1445  
1446      // Is there a theme attached to this set?
1447      $query = $db->simple_select("themes", "properties");
1448      while($theme = $db->fetch_array($query))
1449      {
1450          $properties = my_unserialize($theme['properties']);
1451          if($properties['templateset'] == $sid)
1452          {
1453              flash_message($lang->error_themes_attached_template_set, 'error');
1454              admin_redirect("index.php?module=style-templates");
1455              break;
1456          }
1457      }
1458  
1459      // User clicked no
1460      if($mybb->get_input('no'))
1461      {
1462          admin_redirect("index.php?module=style-templates");
1463      }
1464  
1465      if($mybb->request_method == "post")
1466      {
1467          // Delete the templateset
1468          $db->delete_query("templatesets", "sid='{$set['sid']}'");
1469          // Delete all custom templates in this templateset
1470          $db->delete_query("templates", "sid='{$set['sid']}'");
1471  
1472          $plugins->run_hooks("admin_style_templates_delete_set_commit");
1473  
1474          // Log admin action
1475          log_admin_action($set['sid'], $set['title']);
1476  
1477          flash_message($lang->success_template_set_deleted, 'success');
1478          admin_redirect("index.php?module=style-templates");
1479      }
1480      else
1481      {
1482          $page->output_confirm_action("index.php?module=style-templates&amp;action=delete_set&amp;sid={$set['sid']}", $lang->confirm_template_set_deletion);
1483      }
1484  
1485  }
1486  
1487  if($mybb->input['action'] == "delete_template")
1488  {
1489      $query = $db->query("
1490          SELECT t.*, s.title as set_title
1491          FROM ".TABLE_PREFIX."templates t
1492          LEFT JOIN ".TABLE_PREFIX."templatesets s ON(t.sid=s.sid)
1493          WHERE t.title='".$db->escape_string($mybb->input['title'])."' AND t.sid > '-2' AND t.sid = '{$sid}'
1494      ");
1495      $template = $db->fetch_array($query);
1496  
1497      // Does the template not exist?
1498      if(!$template)
1499      {
1500          flash_message($lang->error_invalid_template, 'error');
1501          admin_redirect("index.php?module=style-templates");
1502      }
1503  
1504      // User clicked no
1505      if($mybb->get_input('no'))
1506      {
1507          admin_redirect("index.php?module=style-templates&sid={$template['sid']}{$expand_str2}");
1508      }
1509  
1510      $plugins->run_hooks("admin_style_templates_delete_template");
1511  
1512      if($mybb->request_method == "post")
1513      {
1514          // Delete the template
1515          $db->delete_query("templates", "tid='{$template['tid']}'");
1516  
1517          $plugins->run_hooks("admin_style_templates_delete_template_commit");
1518  
1519          // Log admin action
1520          log_admin_action($template['tid'], $template['title'], $template['sid'], $template['set_title']);
1521  
1522          flash_message($lang->success_template_deleted, 'success');
1523          admin_redirect("index.php?module=style-templates&sid={$template['sid']}{$expand_str2}");
1524      }
1525      else
1526      {
1527          $page->output_confirm_action("index.php?module=style-templates&amp;action=delete_template&amp;sid={$template['sid']}{$expand_str}", $lang->confirm_template_deletion);
1528      }
1529  }
1530  
1531  if($mybb->input['action'] == "diff_report")
1532  {
1533      // Compares a template of sid1 with that of sid2, if no sid1, it is assumed -2
1534      if(empty($mybb->input['sid1']) || !isset($template_sets[$mybb->input['sid1']]))
1535      {
1536          $mybb->input['sid1'] = -2;
1537      }
1538  
1539      if($mybb->input['sid2'] == -2)
1540      {
1541          $sub_tabs['find_updated'] = array(
1542              'title' => $lang->find_updated,
1543              'link' => "index.php?module=style-templates&amp;action=find_updated"
1544          );
1545      }
1546  
1547      if($mybb->input['sid2'] != -2 && !isset($template_sets[$mybb->input['sid2']]))
1548      {
1549          flash_message($lang->error_invalid_input, 'error');
1550          admin_redirect("index.php?module=style-templates");
1551      }
1552  
1553      if(empty($mybb->input['from']))
1554      {
1555          $mybb->input['from'] = 0;
1556      }
1557  
1558      $sub_tabs['diff_report'] = array(
1559          'title' => $lang->diff_report,
1560          'link' => "index.php?module=style-templates&amp;action=diff_report&amp;title=".urlencode($mybb->input['title'])."&amp;from=".htmlspecialchars_uni($mybb->input['from'])."sid1=".$mybb->get_input('sid1', MyBB::INPUT_INT)."&amp;sid2=".$mybb->get_input('sid2', MyBB::INPUT_INT),
1561          'description' => $lang->diff_report_desc
1562      );
1563  
1564      $plugins->run_hooks("admin_style_templates_diff_report");
1565  
1566      $query = $db->simple_select("templates", "*", "title='".$db->escape_string($mybb->input['title'])."' AND sid='".$mybb->get_input('sid1', MyBB::INPUT_INT)."'");
1567      $template1 = $db->fetch_array($query);
1568  
1569      $query = $db->simple_select("templates", "*", "title='".$db->escape_string($mybb->input['title'])."' AND sid='".$mybb->get_input('sid2', MyBB::INPUT_INT)."'");
1570      $template2 = $db->fetch_array($query);
1571  
1572      if($mybb->input['sid2'] == -2)
1573      {
1574          $sub_tabs['full_edit'] = array(
1575              'title' => $lang->full_edit,
1576              'link' => "index.php?module=style-templates&action=edit_template&title=".urlencode($template1['title'])."&sid=".$mybb->get_input('sid1', MyBB::INPUT_INT)."&amp;from=diff_report",
1577          );
1578      }
1579  
1580      if($template1['template'] == $template2['template'])
1581      {
1582          flash_message($lang->templates_the_same, 'error');
1583          admin_redirect("index.php?module=style-templates&sid=".$mybb->get_input('sid2', MyBB::INPUT_INT).$expand_str);
1584      }
1585  
1586      $template1['template'] = explode("\n", $template1['template']);
1587      $template2['template'] = explode("\n", $template2['template']);
1588  
1589      $plugins->run_hooks("admin_style_templates_diff_report_run");
1590  
1591      require_once  MYBB_ROOT."inc/3rdparty/diff/Diff.php";
1592      require_once  MYBB_ROOT."inc/3rdparty/diff/Diff/Renderer.php";
1593      require_once  MYBB_ROOT."inc/3rdparty/diff/Diff/Renderer/Inline.php";
1594  
1595      $diff = new Horde_Text_Diff('auto', array($template1['template'], $template2['template']));
1596      $renderer = new Horde_Text_Diff_Renderer_Inline();
1597  
1598      if($sid)
1599      {
1600          $page->add_breadcrumb_item($template_sets[$sid], "index.php?module=style-templates&amp;sid={$sid}{$expand_str}");
1601      }
1602  
1603      if($mybb->input['sid2'] == -2)
1604      {
1605          $page->add_breadcrumb_item($lang->find_updated, "index.php?module=style-templates&amp;action=find_updated");
1606      }
1607  
1608      $page->add_breadcrumb_item($lang->diff_report.": ".htmlspecialchars_uni($template1['title']), "index.php?module=style-templates&amp;action=diff_report&amp;title=".urlencode($mybb->input['title'])."&amp;from=".htmlspecialchars_uni($mybb->input['from'])."&amp;sid1=".$mybb->get_input('sid1', MyBB::INPUT_INT)."&amp;sid2=".$mybb->get_input('sid2', MyBB::INPUT_INT));
1609  
1610      $page->output_header($lang->template_sets);
1611  
1612      $page->output_nav_tabs($sub_tabs, 'diff_report');
1613  
1614      $table = new Table;
1615  
1616      if($mybb->input['from'])
1617      {
1618          $table->construct_header("<ins>".$lang->master_updated_ins."</ins><br /><del>".$lang->master_updated_del."</del>");
1619      }
1620      else
1621      {
1622          $table->construct_header("<ins>".$lang->master_updated_del."</ins><br /><del>".$lang->master_updated_ins."</del>");
1623      }
1624  
1625      $table->construct_cell("<pre class=\"differential\">".$renderer->render($diff)."</pre>");
1626      $table->construct_row();
1627  
1628      $table->output($lang->template_diff_analysis.": ".htmlspecialchars_uni($template1['title']));
1629  
1630      $page->output_footer();
1631  }
1632  
1633  if($mybb->input['action'] == "revert")
1634  {
1635      $query = $db->query("
1636          SELECT t.*, s.title as set_title
1637          FROM ".TABLE_PREFIX."templates t
1638          LEFT JOIN ".TABLE_PREFIX."templatesets s ON(s.sid=t.sid)
1639          WHERE t.title='".$db->escape_string($mybb->input['title'])."' AND t.sid > 0 AND t.sid = '".$mybb->get_input('sid', MyBB::INPUT_INT)."'
1640      ");
1641      $template = $db->fetch_array($query);
1642  
1643      // Does the template not exist?
1644      if(!$template)
1645      {
1646          flash_message($lang->error_invalid_template, 'error');
1647          admin_redirect("index.php?module=style-templates");
1648      }
1649  
1650      // User clicked no
1651      if($mybb->get_input('no'))
1652      {
1653          admin_redirect("index.php?module=style-templates&sid={$template['sid']}{$expand_str2}");
1654      }
1655  
1656      $plugins->run_hooks("admin_style_templates_revert");
1657  
1658      if($mybb->request_method == "post")
1659      {
1660          // Revert the template
1661          $db->delete_query("templates", "tid='{$template['tid']}'");
1662  
1663          $plugins->run_hooks("admin_style_templates_revert_commit");
1664  
1665          // Log admin action
1666          log_admin_action($template['tid'], $template['title'], $template['sid'], $template['set_title']);
1667  
1668          flash_message($lang->success_template_reverted, 'success');
1669  
1670          if($mybb->input['from'] == "diff_report")
1671          {
1672              admin_redirect("index.php?module=style-templates&action=find_updated");
1673          }
1674          else
1675          {
1676              admin_redirect("index.php?module=style-templates&sid={$template['sid']}{$expand_str2}");
1677          }
1678      }
1679      else
1680      {
1681          $page->output_confirm_action("index.php?module=style-templates&amp;sid={$template['sid']}{$expand_str}", $lang->confirm_template_revertion);
1682      }
1683  }
1684  
1685  if(!empty($mybb->input['sid']) && !$mybb->input['action'])
1686  {
1687      if(!isset($template_sets[$mybb->input['sid']]))
1688      {
1689          flash_message($lang->error_invalid_input, 'error');
1690          admin_redirect("index.php?module=style-templates");
1691      }
1692  
1693      $plugins->run_hooks("admin_style_templates_set");
1694  
1695      $table = new Table;
1696  
1697      $page->add_breadcrumb_item($template_sets[$sid], "index.php?module=style-templates&amp;sid={$sid}");
1698  
1699      $page->output_header($lang->template_sets);
1700  
1701      $page->output_nav_tabs($sub_tabs, 'manage_templates');
1702  
1703      $table->construct_header($lang->template_set);
1704      $table->construct_header($lang->controls, array("class" => "align_center", "width" => 150));
1705  
1706      // Global Templates
1707      if($sid == -1)
1708      {
1709          $query = $db->simple_select("templates", "tid,title", "sid='-1'", array('order_by' => 'title', 'order_dir' => 'ASC'));
1710          while($template = $db->fetch_array($query))
1711          {
1712              $popup = new PopupMenu("template_{$template['tid']}", $lang->options);
1713              $popup->add_item($lang->full_edit, "index.php?module=style-templates&amp;action=edit_template&amp;title=".urlencode($template['title'])."&amp;sid=-1");
1714              $popup->add_item($lang->delete_template, "index.php?module=style-templates&amp;action=delete_template&amp;title=".urlencode($template['title'])."&amp;sid=-1&amp;my_post_key={$mybb->post_code}", "return AdminCP.deleteConfirmation(this, '{$lang->confirm_template_deletion}')");
1715  
1716              $table->construct_cell("<a href=\"index.php?module=style-templates&amp;action=edit_template&amp;title=".urlencode($template['title'])."&amp;sid=-1\">".htmlspecialchars_uni($template['title'])."</a>");
1717              $table->construct_cell($popup->fetch(), array("class" => "align_center"));
1718  
1719              $table->construct_row();
1720          }
1721  
1722          if($table->num_rows() == 0)
1723          {
1724              $table->construct_cell($lang->no_global_templates, array('colspan' => 2));
1725              $table->construct_row();
1726          }
1727  
1728          $table->output($template_sets[$sid]);
1729  
1730          $page->output_footer();
1731      }
1732  
1733      if(!isset($mybb->input['expand']))
1734      {
1735          $mybb->input['expand'] = '';
1736      }
1737      if($mybb->input['expand'] == 'all')
1738      {
1739          // If we're expanding everything, stick in the ungrouped templates in the list as well
1740          $expand_array = array(-1);
1741      }
1742      // Fetch Groups
1743      $query = $db->simple_select("templategroups", "*");
1744  
1745      $template_groups = array();
1746      while($templategroup = $db->fetch_array($query))
1747      {
1748          $templategroup['title'] = $lang->sprintf($lang->templates, htmlspecialchars_uni($lang->parse($templategroup['title'])));
1749          if($mybb->input['expand'] == 'all')
1750          {
1751              $expand_array[] = $templategroup['gid'];
1752          }
1753          if(in_array($templategroup['gid'], $expand_array))
1754          {
1755              $templategroup['expanded'] = 1;
1756          }
1757          $template_groups[$templategroup['prefix']] = $templategroup;
1758      }
1759  
1760      /**
1761       * @param array $a
1762       * @param array $b
1763       *
1764       * @return int
1765       */
1766  	function sort_template_groups($a, $b)
1767      {
1768          return strcasecmp($a['title'], $b['title']);
1769      }
1770      uasort($template_groups, "sort_template_groups");
1771  
1772      // Add the ungrouped templates group at the bottom
1773      $template_groups['-1'] = array(
1774          "prefix" => "",
1775          "title" => $lang->ungrouped_templates,
1776          "gid" => -1
1777      );
1778  
1779      // Set the template group keys to lowercase for case insensitive comparison.
1780      $template_groups = array_change_key_case($template_groups, CASE_LOWER);
1781  
1782      // Load the list of templates
1783      $query = $db->simple_select("templates", "*", "sid='".$mybb->get_input('sid', MyBB::INPUT_INT)."' OR sid='-2'", array('order_by' => 'sid DESC, title', 'order_dir' => 'ASC'));
1784      while($template = $db->fetch_array($query))
1785      {
1786          $exploded = explode("_", $template['title'], 2);
1787  
1788          // Set the prefix to lowercase for case insensitive comparison.
1789          $exploded[0] = strtolower($exploded[0]);
1790  
1791          if(isset($template_groups[$exploded[0]]))
1792          {
1793              $group = $exploded[0];
1794          }
1795          else
1796          {
1797              $group = -1;
1798          }
1799  
1800          $template['gid'] = -1;
1801          if(isset($template_groups[$exploded[0]]['gid']))
1802          {
1803              $template['gid'] = $template_groups[$exploded[0]]['gid'];
1804          }
1805  
1806          // If this template is not a master template, we simple add it to the list
1807          if($template['sid'] != -2)
1808          {
1809              $template['original'] = false;
1810              $template['modified'] = false;
1811              $template_groups[$group]['templates'][$template['title']] = $template;
1812          }
1813          else if(!in_array($template['gid'], $expand_array) && !isset($expand_array[-1]))
1814          {
1815              $template['original'] = true;
1816              $template['modified'] = false;
1817              $template_groups[$group]['templates'][$template['title']] = $template;
1818  
1819              // Save some memory!
1820              unset($template_groups[$group]['templates'][$template['title']]['template']);
1821          }
1822          // Otherwise, if we are down to master templates we need to do a few extra things
1823          else
1824          {
1825              // Master template that hasn't been customised in the set we have expanded
1826              if(!isset($template_groups[$group]['templates'][$template['title']]) || $template_groups[$group]['templates'][$template['title']]['template'] == $template['template'])
1827              {
1828                  $template['original'] = true;
1829                  $template_groups[$group]['templates'][$template['title']] = $template;
1830              }
1831              // Template has been modified in the set we have expanded (it doesn't match the master)
1832              else if($template_groups[$group]['templates'][$template['title']]['template'] != $template['template'] && $template_groups[$group]['templates'][$template['title']]['sid'] != -2)
1833              {
1834                  $template_groups[$group]['templates'][$template['title']]['modified'] = true;
1835              }
1836  
1837              // Save some memory!
1838              unset($template_groups[$group]['templates'][$template['title']]['template']);
1839          }
1840      }
1841  
1842      foreach($template_groups as $prefix => $group)
1843      {
1844          $tmp_expand = "";
1845          if(in_array($group['gid'], $expand_array))
1846          {
1847              $expand = $lang->collapse;
1848              $expanded = true;
1849  
1850              $tmp_expand = $expand_array;
1851              $unsetgid = array_search($group['gid'], $tmp_expand);
1852              unset($tmp_expand[$unsetgid]);
1853              $group['expand_str'] = implode("|", $tmp_expand);
1854          }
1855          else
1856          {
1857              $expand = $lang->expand;
1858              $expanded = false;
1859  
1860              $group['expand_str'] = implode("|", $expand_array);
1861              if($group['expand_str'])
1862              {
1863                  $group['expand_str'] .= "|";
1864              }
1865              $group['expand_str'] .= $group['gid'];
1866          }
1867  
1868          if($group['expand_str'])
1869          {
1870              $group['expand_str'] = "&amp;expand={$group['expand_str']}";
1871          }
1872  
1873          $set_popup = '';
1874          if(isset($group['isdefault']) && !$group['isdefault'])
1875          {
1876              $popup = new PopupMenu("template_set_{$group['gid']}", $lang->options);
1877              $popup->add_item($lang->edit_template_group, "index.php?module=style-templates&amp;sid={$sid}&amp;action=edit_template_group&amp;gid={$group['gid']}{$group['expand_str']}");
1878              $popup->add_item($lang->delete_template_group, "index.php?module=style-templates&amp;sid={$sid}&amp;action=delete_template_group&amp;gid={$group['gid']}&amp;my_post_key={$mybb->post_code}{$group['expand_str']}", "return AdminCP.deleteConfirmation(this, '{$lang->confirm_template_group_delete}')");
1879  
1880              $set_popup = "<div class=\"float_right\">{$popup->fetch()}</div>";
1881          }
1882  
1883          if($expanded == true)
1884          {
1885              // Show templates in this group
1886              $table->construct_cell("{$set_popup}<strong><a href=\"index.php?module=style-templates&amp;sid={$sid}{$group['expand_str']}#group_{$group['gid']}\">{$group['title']}</a></strong>");
1887              $table->construct_cell("<a href=\"index.php?module=style-templates&amp;sid={$sid}{$group['expand_str']}#group_{$group['gid']}\">{$expand}</a>", array("class" => "align_center"));
1888              $table->construct_row(array("class" => "alt_row", "id" => "group_".$group['gid'], "name" => "group_".$group['gid']));
1889  
1890              if(isset($group['templates']) && is_array($group['templates']) && count($group['templates']) > 0)
1891              {
1892                  $templates = $group['templates'];
1893                  ksort($templates);
1894  
1895                  foreach($templates as $template)
1896                  {
1897                      $template['pretty_title'] = htmlspecialchars_uni($template['title']);
1898  
1899                      $popup = new PopupMenu("template_{$template['tid']}", $lang->options);
1900                      $popup->add_item($lang->full_edit, "index.php?module=style-templates&amp;action=edit_template&amp;title=".urlencode($template['title'])."&amp;sid={$sid}{$expand_str}");
1901  
1902                      if(isset($template['modified']) && $template['modified'] == true)
1903                      {
1904                          if($sid > 0)
1905                          {
1906                              $popup->add_item($lang->diff_report, "index.php?module=style-templates&amp;action=diff_report&amp;title=".urlencode($template['title'])."&amp;sid2={$sid}");
1907  
1908                              $popup->add_item($lang->revert_to_orig, "index.php?module=style-templates&amp;action=revert&amp;title=".urlencode($template['title'])."&amp;sid={$sid}&amp;my_post_key={$mybb->post_code}{$expand_str}", "return AdminCP.deleteConfirmation(this, '{$lang->confirm_template_revertion}')");
1909                          }
1910  
1911                          $template['pretty_title'] = "<span style=\"color: green;\">".htmlspecialchars_uni($template['title'])."</span>";
1912                      }
1913                      // This template does not exist in the master list
1914                      else if(isset($template['original']) && $template['original'] == false)
1915                      {
1916                          $popup->add_item($lang->delete_template, "index.php?module=style-templates&amp;action=delete_template&amp;title=".urlencode($template['title'])."&amp;sid={$sid}&amp;my_post_key={$mybb->post_code}{$expand_str}", "return AdminCP.deleteConfirmation(this, '{$lang->confirm_template_deletion}')");
1917  
1918                          $template['pretty_title'] = "<span style=\"color: blue;\">".htmlspecialchars_uni($template['title'])."</span>";
1919                      }
1920  
1921                      $table->construct_cell("<span style=\"padding-left: 20px;\"><a href=\"index.php?module=style-templates&amp;action=edit_template&amp;title=".urlencode($template['title'])."&amp;sid={$sid}{$expand_str}\" >{$template['pretty_title']}</a></span>");
1922                      $table->construct_cell($popup->fetch(), array("class" => "align_center"));
1923  
1924                      $table->construct_row();
1925                  }
1926              }
1927              else
1928              {
1929                  // No templates in this group
1930                  $table->construct_cell($lang->empty_template_set, array('colspan' => 2));
1931                  $table->construct_row();
1932              }
1933          }
1934          else
1935          {
1936              // Collapse template set
1937              $table->construct_cell("{$set_popup}<strong><a href=\"index.php?module=style-templates&amp;sid={$sid}{$group['expand_str']}#group_{$group['gid']}\">{$group['title']}</a></strong>");
1938               $table->construct_cell("<a href=\"index.php?module=style-templates&amp;sid={$sid}{$group['expand_str']}#group_{$group['gid']}\">{$expand}</a>", array("class" => "align_center"));
1939               $table->construct_row(array("class" => "alt_row", "id" => "group_".$group['gid'], "name" => "group_".$group['gid']));
1940          }
1941      }
1942  
1943      $table->output($template_sets[$sid]);
1944  
1945      $page->output_footer();
1946  }
1947  
1948  if(!$mybb->input['action'])
1949  {
1950      $plugins->run_hooks("admin_style_templates_start");
1951  
1952      $page->output_header($lang->template_sets);
1953  
1954      $page->output_nav_tabs($sub_tabs, 'templates');
1955  
1956      $themes = array();
1957      $query = $db->simple_select("themes", "name,tid,properties", "tid != '1'");
1958      while($theme = $db->fetch_array($query))
1959      {
1960          $tbits = my_unserialize($theme['properties']);
1961          $themes[$tbits['templateset']][$theme['tid']] = htmlspecialchars_uni($theme['name']);
1962      }
1963  
1964      $template_sets = array();
1965      $template_sets[-1]['title'] = $lang->global_templates;
1966      $template_sets[-1]['sid'] = -1;
1967  
1968      $query = $db->simple_select("templatesets", "*", "", array('order_by' => 'title', 'order_dir' => 'ASC'));
1969      while($template_set = $db->fetch_array($query))
1970      {
1971          $template_sets[$template_set['sid']] = $template_set;
1972      }
1973  
1974      $table = new Table;
1975      $table->construct_header($lang->template_set);
1976      $table->construct_header($lang->controls, array("class" => "align_center", "width" => 150));
1977  
1978      foreach($template_sets as $set)
1979      {
1980          if($set['sid'] == -1)
1981          {
1982              $table->construct_cell("<strong><a href=\"index.php?module=style-templates&amp;sid=-1\">{$lang->global_templates}</a></strong><br /><small>{$lang->used_by_all_themes}</small>");
1983              $table->construct_cell("<a href=\"index.php?module=style-templates&amp;sid=-1\">{$lang->expand_templates}</a>", array("class" => "align_center"));
1984              $table->construct_row();
1985              continue;
1986          }
1987  
1988          if(!empty($themes[$set['sid']]))
1989          {
1990              $used_by_note = $lang->used_by;
1991              $comma = "";
1992              foreach($themes[$set['sid']] as $theme_name)
1993              {
1994                  $used_by_note .= $comma.$theme_name;
1995                  $comma = $lang->comma;
1996              }
1997          }
1998          else
1999          {
2000              $used_by_note = $lang->not_used_by_any_themes;
2001          }
2002  
2003          if($set['sid'] == 1)
2004          {
2005              $actions = "<a href=\"index.php?module=style-templates&amp;sid={$set['sid']}\">{$lang->expand_templates}</a>";
2006          }
2007          else
2008          {
2009              $popup = new PopupMenu("templateset_{$set['sid']}", $lang->options);
2010              $popup->add_item($lang->expand_templates, "index.php?module=style-templates&amp;sid={$set['sid']}");
2011  
2012              if($set['sid'] != 1)
2013              {
2014                  $popup->add_item($lang->edit_template_set, "index.php?module=style-templates&amp;action=edit_set&amp;sid={$set['sid']}");
2015  
2016                  if(empty($themes[$set['sid']]))
2017                  {
2018                      $popup->add_item($lang->delete_template_set, "index.php?module=style-templates&amp;action=delete_set&amp;sid={$set['sid']}&amp;my_post_key={$mybb->post_code}", "return AdminCP.deleteConfirmation(this, '{$lang->confirm_template_set_deletion}')");
2019                  }
2020              }
2021  
2022              $actions = $popup->fetch();
2023          }
2024  
2025          $set['title'] = htmlspecialchars_uni($set['title']);
2026  
2027          $table->construct_cell("<strong><a href=\"index.php?module=style-templates&amp;sid={$set['sid']}\">{$set['title']}</a></strong><br /><small>{$used_by_note}</small>");
2028          $table->construct_cell($actions, array("class" => "align_center"));
2029          $table->construct_row();
2030      }
2031  
2032      $table->output($lang->template_sets);
2033  
2034      $page->output_footer();
2035  }


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