[ Index ]

PHP Cross Reference of MyBB 1.8.21

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


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