[ Index ]

PHP Cross Reference of MyBB

title

Body

[close]

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

   1  <?php
   2  /**
   3   * MyBB 1.6
   4   * Copyright 2010 MyBB Group, All Rights Reserved
   5   *
   6   * Website: http://mybb.com
   7   * License: http://mybb.com/about/license
   8   *
   9   * $Id$
  10   */
  11  
  12  // Disallow direct access to this file for security reasons
  13  if(!defined("IN_MYBB"))
  14  {
  15      die("Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.");
  16  }
  17  
  18  $page->add_breadcrumb_item($lang->template_sets, "index.php?module=style-templates");
  19  
  20  $sid = intval($mybb->input['sid']);
  21  
  22  $expand_str = "";
  23  $expand_str2 = "";
  24  $expand_array = array();
  25  if(isset($mybb->input['expand']))
  26  {
  27      $expand_array = explode("|", $mybb->input['expand']);
  28      $expand_array = array_map("intval", $expand_array);
  29      $expand_str = "&amp;expand=".implode("|", $expand_array);
  30      $expand_str2 = "&expand=".implode("|", $expand_array);
  31  }
  32  
  33  if($mybb->input['action'] == "add_set" || $mybb->input['action'] == "add_template" || $mybb->input['action'] == "search_replace" || $mybb->input['action'] == "find_updated" || (!$mybb->input['action'] && !$sid))
  34  {
  35      $sub_tabs['templates'] = array(
  36          'title' => $lang->manage_template_sets,
  37          'link' => "index.php?module=style-templates",
  38          'description' => $lang->manage_template_sets_desc
  39      );
  40  
  41      $sub_tabs['add_set'] = array(
  42          'title' => $lang->add_set,
  43          'link' => "index.php?module=style-templates&amp;action=add_set"
  44      );
  45  
  46      $sub_tabs['add_template'] = array(
  47          'title' => $lang->add_template,
  48          'link' => "index.php?module=style-templates&amp;action=add_template{$expand_str}"
  49      );
  50  
  51      $sub_tabs['search_replace'] = array(
  52          'title' => $lang->search_replace,
  53          'link' => "index.php?module=style-templates&amp;action=search_replace",
  54          'description' => $lang->search_replace_desc
  55      );
  56  
  57      $sub_tabs['find_updated'] = array(
  58          'title' => $lang->find_updated,
  59          'link' => "index.php?module=style-templates&amp;action=find_updated",
  60          'description' => $lang->find_updated_desc
  61      );
  62  }
  63  else if(($sid && !$mybb->input['action']) || $mybb->input['action'] == "edit_set" || $mybb->input['action'] == "check_set" || $mybb->input['action'] == "edit_template")
  64  {
  65      $sub_tabs['manage_templates'] = array(
  66          'title' => $lang->manage_templates,
  67          'link' => "index.php?module=style-templates&amp;sid=".$sid.$expand_str,
  68          'description' => $lang->manage_templates_desc
  69      );
  70  
  71      if($sid > 0)
  72      {
  73          $sub_tabs['edit_set'] = array(
  74              'title' => $lang->edit_set,
  75              'link' => "index.php?module=style-templates&amp;action=edit_set&amp;sid=".$sid.$expand_str,
  76              'description' => $lang->edit_set_desc
  77          );
  78      }
  79  
  80      $sub_tabs['add_template'] = array(
  81          'title' => $lang->add_template,
  82          'link' => "index.php?module=style-templates&amp;action=add_template&amp;sid=".$sid.$expand_str,
  83          'description' => $lang->add_template_desc
  84      );
  85  }
  86  
  87  $template_sets = array();
  88  $template_sets[-1] = $lang->global_templates;
  89  
  90  $query = $db->simple_select("templatesets", "*", "", array('order_by' => 'title', 'order_dir' => 'ASC'));
  91  while($template_set = $db->fetch_array($query))
  92  {
  93      $template_sets[$template_set['sid']] = $template_set['title'];
  94  }
  95  
  96  $plugins->run_hooks("admin_style_templates");
  97  
  98  if($mybb->input['action'] == "add_set")
  99  {
 100      $plugins->run_hooks("admin_style_templates_add_set");
 101  
 102      if($mybb->request_method == "post")
 103      {
 104          if(!trim($mybb->input['title']))
 105          {
 106              $errors[] = $lang->error_missing_set_title;
 107          }
 108  
 109          if(!$errors)
 110          {
 111              $sid = $db->insert_query("templatesets", array('title' => $db->escape_string($mybb->input['title'])));
 112  
 113              // Log admin action
 114              log_admin_action($sid, $mybb->input['title']);
 115  
 116              flash_message($lang->success_template_set_saved, 'success');
 117              admin_redirect("index.php?module=style-templates&sid=".$sid);
 118          }
 119      }
 120  
 121      $page->add_breadcrumb_item($lang->add_set);
 122  
 123      $page->output_header($lang->add_set);
 124  
 125      $sub_tabs = array();
 126      $sub_tabs['add_set'] = array(
 127          'title' => $lang->add_set,
 128          'link' => "index.php?module=style-templates&amp;action=add_set",
 129          'description' => $lang->add_set_desc
 130      );
 131  
 132      $page->output_nav_tabs($sub_tabs, 'add_set');
 133  
 134      if($errors)
 135      {
 136          $page->output_inline_error($errors);
 137      }
 138      else
 139      {
 140          $mybb->input['title'] = "";
 141      }
 142  
 143      $form = new Form("index.php?module=style-templates&amp;action=add_set", "post", "add_set");
 144  
 145      $form_container = new FormContainer($lang->add_set);
 146      $form_container->output_row($lang->title, "", $form->generate_text_box('title', $mybb->input['title'], array('id' => 'title')), 'title');
 147      $form_container->end();
 148  
 149      $buttons = array();
 150      $buttons[] = $form->generate_submit_button($lang->save);
 151  
 152      $form->output_submit_wrapper($buttons);
 153  
 154      $form->end();
 155  
 156      $page->output_footer();
 157  }
 158  
 159  if($mybb->input['action'] == "add_template")
 160  {
 161      $plugins->run_hooks("admin_style_templates_add_template");
 162  
 163      if($mybb->request_method == "post")
 164      {
 165          if(empty($mybb->input['title']))
 166          {
 167              $errors[] = $lang->error_missing_set_title;
 168          }
 169          else
 170          {
 171              $query = $db->simple_select("templates", "COUNT(tid) as count", "title='".$db->escape_string($mybb->input['title'])."' AND (sid = '-2' OR sid = '{$sid}')");
 172              if($db->fetch_field($query, "count") > 0)
 173              {
 174                  $errors[] = $lang->error_already_exists;
 175              }
 176          }
 177  
 178          if(!isset($template_sets[$sid]))
 179          {
 180              $errors[] = $lang->error_invalid_set;
 181          }
 182  
 183          // Are we trying to do malicious things in our template?
 184          if(check_template($mybb->input['template']))
 185          {
 186              $errors[] = $lang->error_security_problem;
 187          }
 188  
 189          if(!$errors)
 190          {
 191              $template_array = array(
 192                  'title' => $db->escape_string($mybb->input['title']),
 193                  'sid' => $sid,
 194                  'template' => $db->escape_string($mybb->input['template']),
 195                  'version' => $db->escape_string($mybb->version_code),
 196                  'status' => '',
 197                  'dateline' => TIME_NOW
 198              );
 199  
 200              $tid = $db->insert_query("templates", $template_array);
 201  
 202              $plugins->run_hooks("admin_style_templates_add_template_commit");
 203  
 204              // Log admin action
 205              log_admin_action($tid, $mybb->input['title'], $sid, $template_sets[$sid]);
 206  
 207              flash_message($lang->success_template_saved, 'success');
 208  
 209              if($mybb->input['continue'])
 210              {
 211                  admin_redirect("index.php?module=style-templates&action=edit_template&title=".urlencode($mybb->input['title'])."&sid=".$sid.$expand_str2);
 212              }
 213              else
 214              {
 215                  admin_redirect("index.php?module=style-templates&sid=".$sid.$expand_str2);
 216              }
 217          }
 218      }
 219  
 220      if($errors)
 221      {
 222          $template = $mybb->input;
 223      }
 224      else
 225      {
 226          if(!$sid)
 227          {
 228              $sid = -1;
 229          }
 230  
 231          $template['template'] = "";
 232          $template['sid'] = $sid;
 233      }
 234  
 235      if($mybb->input['sid'])
 236      {
 237          $page->add_breadcrumb_item($template_sets[$sid], "index.php?module=style-templates&amp;sid={$sid}{$expand_str}");
 238      }
 239  
 240      if($admin_options['codepress'] != 0)
 241      {
 242          $page->extra_header .= '
 243      <link type="text/css" href="./jscripts/codepress/languages/codepress-mybb.css" rel="stylesheet" id="cp-lang-style" />
 244      <script type="text/javascript" src="./jscripts/codepress/codepress.js"></script>
 245      <script type="text/javascript">
 246          CodePress.language = \'mybb\';
 247      </script>';
 248      }
 249  
 250      $page->add_breadcrumb_item($lang->add_template);
 251  
 252      $page->output_header($lang->add_template);
 253  
 254      $sub_tabs = array();
 255      $sub_tabs['add_template'] = array(
 256          'title' => $lang->add_template,
 257          'link' => "index.php?module=style-templates&amp;action=add_template&amp;sid=".$template['sid'].$expand_str,
 258          'description' => $lang->add_template_desc
 259      );
 260  
 261      $page->output_nav_tabs($sub_tabs, 'add_template');
 262  
 263      if($errors)
 264      {
 265          $page->output_inline_error($errors);
 266      }
 267  
 268      $form = new Form("index.php?module=style-templates&amp;action=add_template{$expand_str}", "post", "add_template");
 269  
 270      $form_container = new FormContainer($lang->add_template);
 271      $form_container->output_row($lang->template_name, $lang->template_name_desc, $form->generate_text_box('title', $template['title'], array('id' => 'title')), 'title');
 272      $form_container->output_row($lang->template_set, $lang->template_set_desc, $form->generate_select_box('sid', $template_sets, $sid), 'sid');
 273      $form_container->output_row("", "", $form->generate_text_area('template', $template['template'], array('id' => 'template', 'class' => 'codepress php', 'style' => 'width: 100%; height: 500px;')), 'template');
 274      $form_container->end();
 275  
 276      $buttons[] = $form->generate_submit_button($lang->save_continue, array('name' => 'continue'));
 277      $buttons[] = $form->generate_submit_button($lang->save_close, array('name' => 'close'));
 278  
 279      $form->output_submit_wrapper($buttons);
 280  
 281      $form->end();
 282  
 283      if($admin_options['codepress'] != 0)
 284      {
 285          echo "<script type=\"text/javascript\">
 286      Event.observe('add_template', 'submit', function()
 287      {
 288          if($('template_cp')) {
 289              var area = $('template_cp');
 290              area.id = 'template';
 291              area.value = template.getCode();
 292              area.disabled = false;
 293          }
 294      });
 295  </script>";
 296      }
 297  
 298      $page->output_footer();
 299  }
 300  
 301  if($mybb->input['action'] == "edit_set")
 302  {
 303      $plugins->run_hooks("admin_style_templates_edit_set");
 304  
 305      $query = $db->simple_select("templatesets", "*", "sid='{$sid}'");
 306      $set = $db->fetch_array($query);
 307      if(!$set)
 308      {
 309          flash_message($lang->error_invalid_input, 'error');
 310          admin_redirect("index.php?module=style-templates");
 311      }
 312      $sid = $set['sid'];
 313  
 314      if($mybb->request_method == "post")
 315      {
 316          if(!trim($mybb->input['title']))
 317          {
 318              $errors[] = $lang->error_missing_set_title;
 319          }
 320  
 321          if(!$errors)
 322          {
 323              $query = $db->update_query("templatesets", array('title' => $db->escape_string($mybb->input['title'])), "sid='{$sid}'");
 324  
 325              // Log admin action
 326              log_admin_action($sid, $set['title']);
 327  
 328              flash_message($lang->success_template_set_saved, 'success');
 329              admin_redirect("index.php?module=style-templates&sid=".$sid.$expand_str2);
 330          }
 331      }
 332  
 333      if($sid)
 334      {
 335          $page->add_breadcrumb_item($template_sets[$sid], "index.php?module=style-templates&amp;sid={$sid}{$expand_str}");
 336      }
 337  
 338      $page->add_breadcrumb_item($lang->edit_set);
 339  
 340      $page->output_header($lang->edit_set);
 341  
 342      $sub_tabs = array();
 343      $sub_tabs['edit_set'] = array(
 344          'title' => $lang->edit_set,
 345          'link' => "index.php?module=style-templates&amp;action=edit_set&amp;sid=".$sid,
 346          'description' => $lang->edit_set_desc
 347      );
 348  
 349      $page->output_nav_tabs($sub_tabs, 'edit_set');
 350  
 351      if($errors)
 352      {
 353          $page->output_inline_error($errors);
 354      }
 355      else
 356      {
 357          $query = $db->simple_select("templatesets", "title", "sid='{$sid}'");
 358          $mybb->input['title'] = $db->fetch_field($query, "title");
 359      }
 360  
 361      $form = new Form("index.php?module=style-templates&amp;action=edit_set{$expand_str}", "post", "edit_set");
 362      echo $form->generate_hidden_field("sid", $sid);
 363  
 364      $form_container = new FormContainer($lang->edit_set);
 365      $form_container->output_row($lang->title, "", $form->generate_text_box('title', $mybb->input['title'], array('id' => 'title')), 'title');
 366      $form_container->end();
 367  
 368      $buttons = array();
 369      $buttons[] = $form->generate_submit_button($lang->save);
 370  
 371      $form->output_submit_wrapper($buttons);
 372  
 373      $form->end();
 374  
 375      $page->output_footer();
 376  }
 377  
 378  if($mybb->input['action'] == "edit_template")
 379  {
 380      $plugins->run_hooks("admin_style_templates_edit_template");
 381  
 382      if(!$mybb->input['title'] || !$sid || !isset($template_sets[$sid]))
 383      {
 384          flash_message($lang->error_missing_input, 'error');
 385          admin_redirect("index.php?module=style-templates");
 386      }
 387  
 388      if($mybb->request_method == "post")
 389      {
 390          if(empty($mybb->input['title']))
 391          {
 392              $errors[] = $lang->error_missing_title;
 393          }
 394  
 395          // Are we trying to do malicious things in our template?
 396          if(check_template($mybb->input['template']))
 397          {
 398              $errors[] = $lang->error_security_problem;
 399          }
 400  
 401          if(!$errors)
 402          {
 403              $query = $db->simple_select("templates", "*", "tid='{$mybb->input['tid']}'");
 404              $template = $db->fetch_array($query);
 405  
 406              $template_array = array(
 407                  'title' => $db->escape_string($mybb->input['title']),
 408                  'sid' => $sid,
 409                  'template' => $db->escape_string(trim($mybb->input['template'])),
 410                  'version' => $mybb->version_code,
 411                  'status' => '',
 412                  'dateline' => TIME_NOW
 413              );
 414  
 415              // 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
 416              $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));
 417              $template['tid'] = $db->fetch_field($query, "tid");
 418  
 419              if($sid > 0)
 420              {
 421                  // Check to see if it's never been edited before (i.e. master) of if this a new template (i.e. we've renamed it)  or if it's a custom template
 422                  $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'));
 423                  $existing_sid = $db->fetch_field($query, "sid");
 424                  $existing_rows = $db->num_rows($query);
 425  
 426                  if(($existing_sid == -2 && $existing_rows == 1) || $existing_rows == 0)
 427                  {
 428                      $tid = $db->insert_query("templates", $template_array);
 429                  }
 430                  else
 431                  {
 432                      $db->update_query("templates", $template_array, "tid='{$template['tid']}' AND sid != '-2'");
 433                  }
 434              }
 435              else
 436              {
 437                  // Global template set
 438                  $db->update_query("templates", $template_array, "tid='{$template['tid']}' AND sid != '-2'");
 439              }
 440  
 441              $plugins->run_hooks("admin_style_templates_edit_template_commit");
 442  
 443              $query = $db->simple_select("templatesets", "title", "sid='{$sid}'");
 444              $set = $db->fetch_array($query);
 445  
 446              $exploded = explode("_", $template_array['title'], 2);
 447              $prefix = $exploded[0];
 448  
 449              $query = $db->simple_select("templategroups", "gid", "prefix = '".$db->escape_string($prefix)."'");
 450              $group = $db->fetch_field($query, "gid");
 451  
 452              if(!$group)
 453              {
 454                  $group = "-1";
 455              }
 456  
 457              // Log admin action
 458              log_admin_action($tid, $mybb->input['title'], $mybb->input['sid'], $set['title']);
 459  
 460              flash_message($lang->success_template_saved, 'success');
 461  
 462              if($mybb->input['continue'])
 463              {
 464                  if($mybb->input['from'] == "diff_report")
 465                  {
 466                      admin_redirect("index.php?module=style-templates&action=edit_template&title=".urlencode($mybb->input['title'])."&sid=".intval($mybb->input['sid']).$expand_str2."&amp;from=diff_report");
 467                  }
 468                  else
 469                  {
 470                      admin_redirect("index.php?module=style-templates&action=edit_template&title=".urlencode($mybb->input['title'])."&sid=".intval($mybb->input['sid']).$expand_str2);
 471                  }
 472              }
 473              else
 474              {
 475                  if($mybb->input['from'] == "diff_report")
 476                  {
 477                      admin_redirect("index.php?module=style-templates&amp;action=find_updated");
 478                  }
 479                  else
 480                  {
 481                      admin_redirect("index.php?module=style-templates&sid=".intval($mybb->input['sid']).$expand_str2."#group_{$group}");
 482                  }
 483              }
 484          }
 485      }
 486  
 487      if($errors)
 488      {
 489          $template = $mybb->input;
 490      }
 491      else
 492      {
 493          $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));
 494          $template = $db->fetch_array($query);
 495      }
 496  
 497      if($admin_options['codepress'] != 0)
 498      {
 499          $page->extra_header .= '
 500      <link type="text/css" href="./jscripts/codepress/languages/codepress-mybb.css" rel="stylesheet" id="cp-lang-style" />
 501      <script type="text/javascript" src="./jscripts/codepress/codepress.js"></script>
 502      <script type="text/javascript">
 503          CodePress.language = \'mybb\';
 504      </script>';
 505      }
 506  
 507      $page->add_breadcrumb_item($template_sets[$sid], "index.php?module=style-templates&amp;sid={$sid}{$expand_str}");
 508  
 509      if(!isset($mybb->input['from']))
 510      {
 511          $mybb->input['from'] = '';
 512      }
 513  
 514      if($mybb->input['from'] == "diff_report")
 515      {
 516          $page->add_breadcrumb_item($lang->find_updated, "index.php?module=style-templates&amp;action=find_updated");
 517      }
 518  
 519      $page->add_breadcrumb_item($lang->edit_template_breadcrumb.$template['title'], "index.php?module=style-templates&amp;sid={$sid}");
 520      $page->output_header($lang->edit_template);
 521  
 522      $sub_tabs = array();
 523  
 524      if($mybb->input['from'] == "diff_report")
 525      {
 526          $sub_tabs['find_updated'] = array(
 527              'title' => $lang->find_updated,
 528              'link' => "index.php?module=style-templates&amp;action=find_updated"
 529          );
 530  
 531          $sub_tabs['diff_report'] = array(
 532              'title' => $lang->diff_report,
 533              'link' => "index.php?module=style-templates&amp;action=diff_report&amp;title=".$db->escape_string($template['title'])."&amp;sid1=".intval($template['sid'])."&amp;sid2=-2",
 534          );
 535      }
 536  
 537      $sub_tabs['edit_template'] = array(
 538          'title' => $lang->edit_template,
 539          'link' => "index.php?module=style-templates&amp;action=edit_template&amp;title=".htmlspecialchars_uni($template['title']).$expand_str,
 540          'description' => $lang->edit_template_desc
 541      );
 542  
 543      $page->output_nav_tabs($sub_tabs, 'edit_template');
 544  
 545      if($errors)
 546      {
 547          $page->output_inline_error($errors);
 548      }
 549  
 550      $form = new Form("index.php?module=style-templates&amp;action=edit_template{$expand_str}", "post", "edit_template");
 551      echo $form->generate_hidden_field('tid', $template['tid'])."\n";
 552  
 553      if($mybb->input['from'] == "diff_report")
 554      {
 555          echo $form->generate_hidden_field('from', "diff_report");
 556      }
 557  
 558      $form_container = new FormContainer($lang->edit_template_breadcrumb.$template['title']);
 559      $form_container->output_row($lang->template_name, $lang->template_name_desc, $form->generate_text_box('title', $template['title'], array('id' => 'title')), 'title');
 560  
 561      // Force users to save the default template to a specific set, rather than the "global" templates - where they can delete it
 562      if($template['sid'] == "-2")
 563      {
 564          unset($template_sets[-1]);
 565      }
 566  
 567      $form_container->output_row($lang->template_set, $lang->template_set_desc, $form->generate_select_box('sid', $template_sets, $sid));
 568  
 569      $form_container->output_row("", "", $form->generate_text_area('template', $template['template'], array('id' => 'template', 'class' => 'codepress mybb', 'style' => 'width: 100%; height: 500px;')));
 570      $form_container->end();
 571  
 572      $buttons[] = $form->generate_submit_button($lang->save_continue, array('name' => 'continue'));
 573      $buttons[] = $form->generate_submit_button($lang->save_close, array('name' => 'close'));
 574  
 575      $form->output_submit_wrapper($buttons);
 576  
 577      $form->end();
 578  
 579      if($admin_options['codepress'] != 0)
 580      {
 581          echo "<script type=\"text/javascript\">
 582      Event.observe('edit_template', 'submit', function()
 583      {
 584          if($('template_cp')) {
 585              var area = $('template_cp');
 586              area.id = 'template';
 587              area.value = template.getCode();
 588              area.disabled = false;
 589          }
 590      });
 591  </script>";
 592      }
 593  
 594      $page->output_footer();
 595  }
 596  
 597  if($mybb->input['action'] == "search_replace")
 598  {
 599      $plugins->run_hooks("admin_style_templates_search_replace");
 600  
 601      if($mybb->request_method == "post")
 602      {
 603          if($mybb->input['type'] == "templates")
 604          {
 605              // Search and replace in templates
 606  
 607              if(!$mybb->input['find'])
 608              {
 609                  flash_message($lang->search_noneset, "error");
 610                  admin_redirect("index.php?module=style-templates&action=search_replace");
 611              }
 612              else
 613              {
 614                  $page->add_breadcrumb_item($lang->search_replace);
 615  
 616                  $page->output_header($lang->search_replace);
 617  
 618                  $page->output_nav_tabs($sub_tabs, 'search_replace');
 619  
 620                  $templates_list = array();
 621                  $table = new Table;
 622  
 623                  $template_sets = array();
 624  
 625                  // Get the names of all template sets
 626                  $template_sets[-2] = $lang->master_templates;
 627                  $template_sets[-1] = $lang->global_templates;
 628  
 629                  $query = $db->simple_select("templatesets", "sid, title");
 630                  while($set = $db->fetch_array($query))
 631                  {
 632                      $template_sets[$set['sid']] = $set['title'];
 633                  }
 634  
 635                  // Select all templates with that search term
 636                  $query = $db->query("
 637                      SELECT t.tid, t.title, t.sid, t.template
 638                      FROM ".TABLE_PREFIX."templates t
 639                      LEFT JOIN ".TABLE_PREFIX."templatesets s ON (t.sid=s.sid)
 640                      LEFT JOIN ".TABLE_PREFIX."templates t2 ON (t.title=t2.title AND t2.sid='1')
 641                      WHERE t.template LIKE '%".$db->escape_string_like($mybb->input['find'])."%' AND NOT (t.sid = -2 AND NOT ISNULL(t2.tid))
 642                      ORDER BY t.title ASC
 643                  ");
 644                  if($db->num_rows($query) == 0)
 645                  {
 646                      $table->construct_cell($lang->sprintf($lang->search_noresults, htmlspecialchars_uni($mybb->input['find'])), array("class" => "align_center"));
 647  
 648                      $table->construct_row();
 649  
 650                      $table->output($lang->search_results);
 651                  }
 652                  else
 653                  {
 654                      while($template = $db->fetch_array($query))
 655                      {
 656                          $template_list[$template['sid']][$template['title']] = $template;
 657                      }
 658  
 659                      $count = 0;
 660  
 661                      foreach($template_list as $sid => $templates)
 662                      {
 663                          ++$count;
 664  
 665                          $search_header = $lang->sprintf($lang->search_header, htmlspecialchars_uni($mybb->input['find']), $template_sets[$sid]);
 666                          $table->construct_header($search_header, array("colspan" => 2));
 667  
 668                          foreach($templates as $title => $template)
 669                          {
 670                              // Do replacement
 671                              $newtemplate = str_ireplace($mybb->input['find'], $mybb->input['replace'], $template['template']);
 672                              if($newtemplate != $template['template'] && check_template($newtemplate) === false)
 673                              {
 674                                  // If the template is different, that means the search term has been found.
 675                                  if(trim($mybb->input['replace']) != "")
 676                                  {
 677                                      if($template['sid'] == -2)
 678                                      {
 679                                          // The template is a master template.  We have to make a new custom template.
 680                                          $new_template = array(
 681                                              "title" => $db->escape_string($title),
 682                                              "template" => $db->escape_string($newtemplate),
 683                                              "sid" => 1,
 684                                              "version" => $mybb->version_code,
 685                                              "status" => '',
 686                                              "dateline" => TIME_NOW
 687                                          );
 688                                          $new_tid = $db->insert_query("templates", $new_template);
 689                                          $label = $lang->sprintf($lang->search_created_custom, $template['title']);
 690                                          $url = "index.php?module=style-templates&amp;action=edit_template&amp;title=".urlencode($template['title'])."&amp;sid=1";
 691                                      }
 692                                      else
 693                                      {
 694                                          // The template is a custom template.  Replace as normal.
 695                                          // Update the template if there is a replacement term
 696                                          $updatedtemplate = array(
 697                                              "template" => $db->escape_string($newtemplate)
 698                                          );
 699                                          $db->update_query("templates", $updatedtemplate, "tid='".$template['tid']."'");
 700                                          $label = $lang->sprintf($lang->search_updated, $template['title']);
 701                                          $url = "index.php?module=style-templates&amp;action=edit_template&amp;title=".urlencode($template['title'])."&amp;sid={$template['sid']}";
 702                                      }
 703                                  }
 704                                  else
 705                                  {
 706                                      // Just show that the term was found
 707                                      if($template['sid'] == -2)
 708                                      {
 709                                          $label = $lang->sprintf($lang->search_found, $template['title']);
 710                                      }
 711                                      else
 712                                      {
 713                                          $label = $lang->sprintf($lang->search_found, $template['title']);
 714                                          $url = "index.php?module=style-templates&amp;action=edit_template&amp;title=".urlencode($template['title'])."&amp;sid={$template['sid']}";
 715                                      }
 716                                  }
 717                              }
 718                              else
 719                              {
 720                                  // Just show that the term was found
 721                                  if($template['sid'] == -2)
 722                                  {
 723                                      $label = $lang->sprintf($lang->search_found, $template['title']);
 724                                  }
 725                                  else
 726                                  {
 727                                      $label = $lang->sprintf($lang->search_found, $template['title']);
 728                                      $url = "index.php?module=style-templates&amp;action=edit_template&amp;title=".urlencode($template['title'])."&amp;sid={$template['sid']}";
 729                                  }
 730                              }
 731  
 732                              $table->construct_cell($label, array("width" => "85%"));
 733  
 734                              if($sid == -2)
 735                              {
 736                                  $popup = new PopupMenu("template_{$template['tid']}", $lang->options);
 737  
 738                                  foreach($template_sets as $set_sid => $title)
 739                                  {
 740                                      if($set_sid > 0)
 741                                      {
 742                                          $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}");
 743                                      }
 744                                  }
 745  
 746                                  $table->construct_cell($popup->fetch(), array("class" => "align_center"));
 747                              }
 748                              else
 749                              {
 750                                  $table->construct_cell("<a href=\"{$url}\">{$lang->edit}</a>", array("class" => "align_center"));
 751                              }
 752  
 753                              $table->construct_row();
 754                          }
 755  
 756                          if($count == 1)
 757                          {
 758                              $table->output($lang->search_results);
 759                          }
 760                          else
 761                          {
 762                              $table->output();
 763                          }
 764                      }
 765                  }
 766  
 767                  if(trim($mybb->input['replace']) != "")
 768                  {
 769                      // Log admin action - only if replace
 770                      log_admin_action($mybb->input['find'], $mybb->input['replace']);
 771                  }
 772  
 773                  $page->output_footer();
 774                  exit;
 775              }
 776          }
 777          else
 778          {
 779              if(!$mybb->input['title'])
 780              {
 781                  flash_message($lang->search_noneset, "error");
 782                  admin_redirect("index.php?module=style-templates&action=search_replace");
 783              }
 784              else
 785              {
 786                  // Search Template Titles
 787  
 788                  $templatessets = array();
 789  
 790                  $templates_sets = array();
 791                  // Get the names of all template sets
 792                  $template_sets[-2] = $lang->master_templates;
 793                  $template_sets[-1] = $lang->global_templates;
 794  
 795                  $query = $db->simple_select("templatesets", "sid, title");
 796                  while($set = $db->fetch_array($query))
 797                  {
 798                      $template_sets[$set['sid']] = $set['title'];
 799                  }
 800  
 801                  $table = new Table;
 802  
 803                  $query = $db->query("
 804                      SELECT t.tid, t.title, t.sid, s.title as settitle, t2.tid as customtid
 805                      FROM ".TABLE_PREFIX."templates t
 806                      LEFT JOIN ".TABLE_PREFIX."templatesets s ON (t.sid=s.sid)
 807                      LEFT JOIN ".TABLE_PREFIX."templates t2 ON (t.title=t2.title AND t2.sid='1')
 808                      WHERE t.title LIKE '%".$db->escape_string_like($mybb->input['title'])."%'
 809                      ORDER BY t.title ASC
 810                  ");
 811                  while($template = $db->fetch_array($query))
 812                  {
 813                      if($template['sid'] == -2)
 814                      {
 815                          if(!$template['customtid'])
 816                          {
 817                              $template['original'] = true;
 818                          }
 819                          else
 820                          {
 821                              $template['modified'] = true;
 822                          }
 823                      }
 824                      else
 825                      {
 826                          $template['original'] = false;
 827                          $template['modified'] = false;
 828                      }
 829                      $templatessets[$template['sid']][$template['title']] = $template;
 830                  }
 831  
 832                  $page->add_breadcrumb_item($lang->search_replace);
 833  
 834                  $page->output_header($lang->search_replace);
 835  
 836                  $page->output_nav_tabs($sub_tabs, 'search_replace');
 837  
 838                  if(empty($templatesets))
 839                  {
 840                      $table->construct_cell($lang->sprintf($lang->search_noresults_title, htmlspecialchars_uni($mybb->input['title'])), array("class" => "align_center"));
 841  
 842                      $table->construct_row();
 843  
 844                      $table->output($lang->search_results);
 845                  }
 846  
 847                  $count = 0;
 848  
 849                  foreach($templatessets as $sid => $templates)
 850                  {
 851                      ++$count;
 852  
 853                      $table->construct_header($template_sets[$sid], array("colspan" => 2));
 854  
 855                      foreach($templates as $template)
 856                      {
 857                          $template['pretty_title'] = $template['title'];
 858  
 859                          $popup = new PopupMenu("template_{$template['tid']}", $lang->options);
 860  
 861                          if($sid == -2)
 862                          {
 863                              foreach($template_sets as $set_sid => $title)
 864                              {
 865                                  if($set_sid < 0) continue;
 866  
 867                                  $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}");
 868                              }
 869                          }
 870                          else
 871                          {
 872                              $popup->add_item($lang->full_edit, "index.php?module=style-templates&amp;action=edit_template&amp;title=".urlencode($template['title'])."&amp;sid={$sid}");
 873                          }
 874  
 875                          if(isset($template['modified']) && $template['modified'] == true)
 876                          {
 877                              if($sid > 0)
 878                              {
 879                                  $popup->add_item($lang->diff_report, "index.php?module=style-templates&amp;action=diff_report&amp;title=".urlencode($template['title'])."&amp;sid2={$sid}");
 880  
 881                                  $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}')");
 882                              }
 883  
 884                              $template['pretty_title'] = "<span style=\"color: green;\">{$template['title']}</span>";
 885                          }
 886                          // This template does not exist in the master list
 887                          else if(!isset($template['original']) || $template['original'] == false)
 888                          {
 889                              $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}')");
 890  
 891                              $template['pretty_title'] = "<span style=\"color: blue;\">{$template['title']}</span>";
 892                          }
 893  
 894                          $table->construct_cell("<span style=\"padding: 20px;\">{$template['pretty_title']}</span>", array("width" => "85%"));
 895                          $table->construct_cell($popup->fetch(), array("class" => "align_center"));
 896  
 897                          $table->construct_row();
 898                      }
 899  
 900                      if($count == 1)
 901                      {
 902                          $table->output($lang->sprintf($lang->search_names_header, htmlspecialchars_uni($mybb->input['title'])));
 903                      }
 904                      else if($count > 0)
 905                      {
 906                          $table->output();
 907                      }
 908                  }
 909  
 910                  $page->output_footer();
 911                  exit;
 912              }
 913          }
 914      }
 915  
 916      if($admin_options['codepress'] != 0)
 917      {
 918          $page->extra_header .= '
 919      <link type="text/css" href="./jscripts/codepress/languages/codepress-php.css" rel="stylesheet" id="cp-lang-style" />
 920      <script type="text/javascript" src="./jscripts/codepress/codepress.js"></script>
 921      <script type="text/javascript">
 922          CodePress.language = \'php\';
 923      </script>';
 924      }
 925  
 926      $page->add_breadcrumb_item($lang->search_replace);
 927  
 928      $page->output_header($lang->search_replace);
 929  
 930      $page->output_nav_tabs($sub_tabs, 'search_replace');
 931  
 932      $form = new Form("index.php?module=style-templates&amp;action=search_replace", "post", "do_template");
 933      echo $form->generate_hidden_field('type', "templates");
 934  
 935      $form_container = new FormContainer($lang->search_replace);
 936      $form_container->output_row($lang->search_for, "", $form->generate_text_area('find', $mybb->input['find'], array('id' => 'find', 'class' => 'codepress mybb', 'style' => 'width: 100%; height: 200px;')));
 937  
 938      $form_container->output_row($lang->replace_with, "", $form->generate_text_area('replace', $mybb->input['replace'], array('id' => 'replace', 'class' => 'codepress mybb', 'style' => 'width: 100%; height: 200px;')));
 939      $form_container->end();
 940  
 941      $buttons[] = $form->generate_submit_button($lang->find_and_replace);
 942  
 943      $form->output_submit_wrapper($buttons);
 944  
 945      $form->end();
 946  
 947      echo "<br />";
 948  
 949  
 950      $form = new Form("index.php?module=style-templates&amp;action=search_replace", "post", "do_title");
 951      echo $form->generate_hidden_field('type', "titles");
 952  
 953      $form_container = new FormContainer($lang->search_template_names);
 954  
 955      $form_container->output_row($lang->search_for, "", $form->generate_text_box('title', $mybb->input['title'], array('id' => 'title')), 'title');
 956  
 957      $form_container->end();
 958  
 959      $buttons = array();
 960      $buttons[] = $form->generate_submit_button($lang->find_templates);
 961      $buttons[] = $form->generate_reset_button($lang->reset);
 962  
 963      $form->output_submit_wrapper($buttons);
 964  
 965      $form->end();
 966  
 967      if($admin_options['codepress'] != 0)
 968      {
 969          echo "<script type=\"text/javascript\">
 970      Event.observe('do_template', 'submit', function()
 971      {
 972          if($('find_cp')) {
 973              var area = $('find_cp');
 974              area.id = 'find';
 975              area.value = find.getCode();
 976              area.disabled = false;
 977          }
 978  
 979          if($('replace_cp')) {
 980              var area = $('replace_cp');
 981              area.id = 'replace';
 982              area.value = replace.getCode();
 983              area.disabled = false;
 984          }
 985      });
 986  </script>";
 987      }
 988  
 989      $page->output_footer();
 990  }
 991  
 992  if($mybb->input['action'] == "find_updated")
 993  {
 994      $plugins->run_hooks("admin_style_templates_find_updated");
 995  
 996      // Finds templates that are old and have been updated by MyBB
 997      $compare_version = $mybb->version_code;
 998      $query = $db->query("
 999          SELECT COUNT(*) AS updated_count
1000          FROM ".TABLE_PREFIX."templates t
1001          LEFT JOIN ".TABLE_PREFIX."templates m ON (m.title=t.title AND m.sid=-2 AND m.version > t.version)
1002          WHERE t.sid > 0 AND m.template != t.template
1003      ");
1004      $count = $db->fetch_array($query);
1005  
1006      if($count['updated_count'] < 1)
1007      {
1008          flash_message($lang->no_updated_templates, 'success');
1009          admin_redirect("index.php?module=style-templates");
1010      }
1011  
1012      $page->add_breadcrumb_item($lang->find_updated, "index.php?module=style-templates&amp;action=find_updated");
1013  
1014      $page->output_header($lang->find_updated);
1015  
1016      $page->output_nav_tabs($sub_tabs, 'find_updated');
1017  
1018      $query = $db->simple_select("templatesets", "*", "", array('order_by' => 'title'));
1019      while($templateset = $db->fetch_array($query))
1020      {
1021          $templatesets[$templateset['sid']] = $templateset;
1022      }
1023  
1024  
1025      echo <<<LEGEND
1026      <fieldset>
1027  <legend>{$lang->legend}</legend>
1028  <ul>
1029  <li>{$lang->updated_template_welcome1}</li>
1030  <li>{$lang->updated_template_welcome2}</li>
1031  <li>{$lang->updated_template_welcome3}</li>
1032  </ul>
1033  </fieldset>
1034  LEGEND;
1035  
1036      $count = 0;
1037      $done_set = array();
1038      $done_output = array();
1039      $templates = array();
1040      $table = new Table;
1041  
1042      $query = $db->query("
1043          SELECT t.tid, t.title, t.sid, t.version
1044          FROM ".TABLE_PREFIX."templates t
1045          LEFT JOIN ".TABLE_PREFIX."templates m ON (m.title=t.title AND m.sid=-2 AND m.version > t.version)
1046          WHERE t.sid > 0 AND m.template != t.template
1047          ORDER BY t.sid ASC, title ASC
1048      ");
1049      while($template = $db->fetch_array($query))
1050      {
1051          $templates[$template['sid']][] = $template;
1052      }
1053  
1054      foreach($templates as $sid => $templates)
1055      {
1056          if(!$done_set[$sid])
1057          {
1058              $table->construct_header($templatesets[$sid]['title'], array("colspan" => 2));
1059  
1060              $done_set[$sid] = 1;
1061              ++$count;
1062          }
1063  
1064          foreach($templates as $template)
1065          {
1066              $popup = new PopupMenu("template_{$template['tid']}", $lang->options);
1067              $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");
1068              $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");
1069              $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}')");
1070  
1071              $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%'));
1072              $table->construct_cell($popup->fetch(), array("class" => "align_center"));
1073  
1074              $table->construct_row();
1075          }
1076  
1077          if($done_set[$sid] && !$done_output[$sid])
1078          {
1079              $done_output[$sid] = 1;
1080              if($count == 1)
1081              {
1082                  $table->output($lang->find_updated);
1083              }
1084              else
1085              {
1086                  $table->output();
1087              }
1088          }
1089      }
1090  
1091      $page->output_footer();
1092  }
1093  
1094  if($mybb->input['action'] == "delete_set")
1095  {
1096      $plugins->run_hooks("admin_style_templates_delete_set");
1097  
1098      $query = $db->simple_select("templatesets", "*", "sid='{$sid}' AND sid > 0");
1099      $set = $db->fetch_array($query);
1100  
1101      // Does the template not exist?
1102      if(!$set['sid'])
1103      {
1104          flash_message($lang->error_invalid_template_set, 'error');
1105          admin_redirect("index.php?module=style-templates");
1106      }
1107  
1108      // Is there a theme attached to this set?
1109      $query = $db->simple_select("themes", "properties");
1110      while($theme = $db->fetch_array($query))
1111      {
1112          $properties = @unserialize($theme['properties']);
1113          if($properties['templateset'] == $sid)
1114          {
1115              flash_message($lang->error_themes_attached_template_set, 'error');
1116              admin_redirect("index.php?module=style-templates");
1117              break;
1118          }
1119      }
1120  
1121      // User clicked no
1122      if($mybb->input['no'])
1123      {
1124          admin_redirect("index.php?module=style-templates");
1125      }
1126  
1127      if($mybb->request_method == "post")
1128      {
1129          // Delete the templateset
1130          $db->delete_query("templatesets", "sid='{$set['sid']}'");
1131          // Delete all custom templates in this templateset
1132          $db->delete_query("templates", "sid='{$set['sid']}'");
1133  
1134          $plugins->run_hooks("admin_style_templates_delete_set_commit");
1135  
1136          // Log admin action
1137          log_admin_action($set['sid'], $set['title']);
1138  
1139          flash_message($lang->success_template_set_deleted, 'success');
1140          admin_redirect("index.php?module=style-templates");
1141      }
1142      else
1143      {
1144          $page->output_confirm_action("index.php?module=style-templates&amp;action=delete_set&amp;sid={$set['sid']}", $lang->confirm_template_set_deletion);
1145      }
1146  
1147  }
1148  
1149  if($mybb->input['action'] == "delete_template")
1150  {
1151      $plugins->run_hooks("admin_style_templates_delete_template");
1152  
1153      $query = $db->query("
1154          SELECT t.*, s.title as set_title
1155          FROM ".TABLE_PREFIX."templates t
1156          LEFT JOIN ".TABLE_PREFIX."templatesets s ON(t.sid=s.sid)
1157          WHERE t.title='".$db->escape_string($mybb->input['title'])."' AND t.sid > '-2' AND t.sid = '{$sid}'
1158      ");
1159      $template = $db->fetch_array($query);
1160  
1161      // Does the template not exist?
1162      if(!$template)
1163      {
1164          flash_message($lang->error_invalid_template, 'error');
1165          admin_redirect("index.php?module=style-templates");
1166      }
1167  
1168      // User clicked no
1169      if($mybb->input['no'])
1170      {
1171          admin_redirect("index.php?module=style-templates&sid={$template['sid']}{$expand_str2}");
1172      }
1173  
1174      if($mybb->request_method == "post")
1175      {
1176          // Delete the template
1177          $db->delete_query("templates", "tid='{$template['tid']}'");
1178  
1179          $plugins->run_hooks("admin_style_templates_delete_template_commit");
1180  
1181          // Log admin action
1182          log_admin_action($template['tid'], $template['title'], $template['sid'], $template['set_title']);
1183  
1184          flash_message($lang->success_template_deleted, 'success');
1185          admin_redirect("index.php?module=style-templates&sid={$template['sid']}{$expand_str2}");
1186      }
1187      else
1188      {
1189          $page->output_confirm_action("index.php?module=style-templates&amp;action=delete_template&amp;sid={$template['sid']}{$expand_str}", $lang->confirm_template_deletion);
1190      }
1191  }
1192  
1193  if($mybb->input['action'] == "diff_report")
1194  {
1195      // Compares a template of sid1 with that of sid2, if no sid1, it is assumed -2
1196      if(!$mybb->input['sid1'] || !isset($template_sets[$mybb->input['sid1']]))
1197      {
1198          $mybb->input['sid1'] = -2;
1199      }
1200  
1201      if($mybb->input['sid2'] == -2)
1202      {
1203          $sub_tabs['find_updated'] = array(
1204              'title' => $lang->find_updated,
1205              'link' => "index.php?module=style-templates&amp;action=find_updated"
1206          );
1207      }
1208  
1209      if($mybb->input['sid2'] != -2 && !isset($template_sets[$mybb->input['sid2']]))
1210      {
1211          flash_message($lang->error_invalid_input, 'error');
1212          admin_redirect("index.php?module=style-templates");
1213      }
1214  
1215      if(!$mybb->input['from'])
1216      {
1217          $mybb->input['from'] = 0;
1218      }
1219  
1220      $sub_tabs['diff_report'] = array(
1221          'title' => $lang->diff_report,
1222          'link' => "index.php?module=style-templates&amp;action=diff_report&amp;title=".$db->escape_string($mybb->input['title'])."&amp;from=".$mybb->input['from']."sid1=".intval($mybb->input['sid1'])."&amp;sid2=".intval($mybb->input['sid2']),
1223          'description' => $lang->diff_report_desc
1224      );
1225  
1226      $plugins->run_hooks("admin_style_templates_diff_report");
1227  
1228      $query = $db->simple_select("templates", "*", "title='".$db->escape_string($mybb->input['title'])."' AND sid='".intval($mybb->input['sid1'])."'");
1229      $template1 = $db->fetch_array($query);
1230  
1231      $query = $db->simple_select("templates", "*", "title='".$db->escape_string($mybb->input['title'])."' AND sid='".intval($mybb->input['sid2'])."'");
1232      $template2 = $db->fetch_array($query);
1233  
1234      if($mybb->input['sid2'] == -2)
1235      {
1236          $sub_tabs['full_edit'] = array(
1237              'title' => $lang->full_edit,
1238              'link' => "index.php?module=style-templates&action=edit_template&title=".urlencode($template1['title'])."&sid=".intval($mybb->input['sid1'])."&amp;from=diff_report",
1239          );
1240      }
1241  
1242      if($template1['template'] == $template2['template'])
1243      {
1244          flash_message($lang->templates_the_same, 'error');
1245          admin_redirect("index.php?module=style-templates&sid=".intval($mybb->input['sid2']).$expand_str);
1246      }
1247  
1248      $template1['template'] = explode("\n", $template1['template']);
1249      $template2['template'] = explode("\n", $template2['template']);
1250  
1251      $plugins->run_hooks("admin_style_templates_diff_report_run");
1252  
1253      require_once  MYBB_ROOT."inc/3rdparty/diff/Diff.php";
1254      require_once  MYBB_ROOT."inc/3rdparty/diff/Diff/Renderer.php";
1255      require_once  MYBB_ROOT."inc/3rdparty/diff/Diff/Renderer/Inline.php";
1256  
1257      $diff = new Horde_Text_Diff('auto', array($template1['template'], $template2['template']));
1258      $renderer = new Horde_Text_Diff_Renderer_Inline();
1259  
1260      if($sid)
1261      {
1262          $page->add_breadcrumb_item($template_sets[$sid], "index.php?module=style-templates&amp;sid={$sid}{$expand_str}");
1263      }
1264  
1265      if($mybb->input['sid2'] == -2)
1266      {
1267          $page->add_breadcrumb_item($lang->find_updated, "index.php?module=style-templates&amp;action=find_updated");
1268      }
1269  
1270      $page->add_breadcrumb_item($lang->diff_report.": ".$template1['title'], "index.php?module=style-templates&amp;action=diff_report&amp;title=".$db->escape_string($mybb->input['title'])."&amp;from=".$mybb->input['from']."&amp;sid1=".intval($mybb->input['sid1'])."&amp;sid2=".intval($mybb->input['sid2']));
1271  
1272      $page->output_header($lang->template_sets);
1273  
1274      $page->output_nav_tabs($sub_tabs, 'diff_report');
1275  
1276      $table = new Table;
1277  
1278      if($mybb->input['from'])
1279      {
1280          $table->construct_header("<ins>".$lang->master_updated_ins."</ins><br /><del>".$lang->master_updated_del."</del>");
1281      }
1282      else
1283      {
1284          $table->construct_header("<ins>".$lang->master_updated_del."</ins><br /><del>".$lang->master_updated_ins."</del>");
1285      }
1286  
1287      $table->construct_cell("<pre class=\"differential\">".$renderer->render($diff)."</pre>");
1288      $table->construct_row();
1289  
1290      $table->output($lang->template_diff_analysis.": ".$template1['title']);
1291  
1292      $page->output_footer();
1293  }
1294  
1295  if($mybb->input['action'] == "revert")
1296  {
1297      $plugins->run_hooks("admin_style_templates_revert");
1298  
1299      $query = $db->query("
1300          SELECT t.*, s.title as set_title
1301          FROM ".TABLE_PREFIX."templates t
1302          LEFT JOIN ".TABLE_PREFIX."templatesets s ON(s.sid=t.sid)
1303          WHERE t.title='".$db->escape_string($mybb->input['title'])."' AND t.sid > 0 AND t.sid = '".intval($mybb->input['sid'])."'
1304      ");
1305      $template = $db->fetch_array($query);
1306  
1307      // Does the template not exist?
1308      if(!$template)
1309      {
1310          flash_message($lang->error_invalid_template, 'error');
1311          admin_redirect("index.php?module=style-templates");
1312      }
1313  
1314      // User clicked no
1315      if($mybb->input['no'])
1316      {
1317          admin_redirect("index.php?module=style-templates&sid={$template['sid']}{$expand_str2}");
1318      }
1319  
1320      if($mybb->request_method == "post")
1321      {
1322          // Revert the template
1323          $db->delete_query("templates", "tid='{$template['tid']}'");
1324  
1325          $plugins->run_hooks("admin_style_templates_revert_commit");
1326  
1327          // Log admin action
1328          log_admin_action($template['tid'], $template['sid'], $template['sid'], $template['set_title']);
1329  
1330          flash_message($lang->success_template_reverted, 'success');
1331  
1332          if($mybb->input['from'] == "diff_report")
1333          {
1334              admin_redirect("index.php?module=style-templates&action=find_updated");
1335          }
1336          else
1337          {
1338              admin_redirect("index.php?module=style-templates&sid={$template['sid']}{$expand_str2}");
1339          }
1340      }
1341      else
1342      {
1343          $page->output_confirm_action("index.php?module=style-templates&amp;sid={$template['sid']}{$expand_str}", $lang->confirm_template_revertion);
1344      }
1345  }
1346  
1347  if($mybb->input['sid'] && !$mybb->input['action'])
1348  {
1349      if(!isset($template_sets[$mybb->input['sid']]))
1350      {
1351          flash_message($lang->error_invalid_input, 'error');
1352          admin_redirect("index.php?module=style-templates");
1353      }
1354  
1355      $plugins->run_hooks("admin_style_templates_set");
1356  
1357      $table = new Table;
1358  
1359      $page->add_breadcrumb_item($template_sets[$sid], "index.php?module=style-templates&amp;sid={$sid}");
1360  
1361      $page->output_header($lang->template_sets);
1362  
1363      $page->output_nav_tabs($sub_tabs, 'manage_templates');
1364  
1365      $table->construct_header($lang->template_set);
1366      $table->construct_header($lang->controls, array("class" => "align_center", "width" => 150));
1367  
1368      // Global Templates
1369      if($sid == -1)
1370      {
1371          $query = $db->simple_select("templates", "tid,title", "sid='-1'", array('order_by' => 'title', 'order_dir' => 'ASC'));
1372          while($template = $db->fetch_array($query))
1373          {
1374              $popup = new PopupMenu("template_{$template['tid']}", $lang->options);
1375              $popup->add_item($lang->full_edit, "index.php?module=style-templates&amp;action=edit_template&amp;title=".urlencode($template['title'])."&amp;sid=-1");
1376              $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}')");
1377  
1378              $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>");
1379              $table->construct_cell($popup->fetch(), array("class" => "align_center"));
1380  
1381              $table->construct_row();
1382          }
1383  
1384          if($table->num_rows() == 0)
1385          {
1386              $table->construct_cell($lang->no_global_templates, array('colspan' => 2));
1387              $table->construct_row();
1388          }
1389  
1390          $table->output($template_sets[$sid]);
1391  
1392          $page->output_footer();
1393      }
1394  
1395      if(!isset($mybb->input['expand']))
1396      {
1397          $mybb->input['expand'] = '';
1398      }
1399      if($mybb->input['expand'] == 'all')
1400      {
1401          // If we're expanding everything, stick in the ungrouped templates in the list as well
1402          $expand_array = array(-1);
1403      }
1404      // Fetch Groups
1405      $query = $db->simple_select("templategroups", "*");
1406      while($templategroup = $db->fetch_array($query))
1407      {
1408          $templategroup['title'] = $lang->parse($templategroup['title'])." ".$lang->templates;
1409          if($mybb->input['expand'] == 'all')
1410          {
1411              $expand_array[] = $templategroup['gid'];
1412          }
1413          if(in_array($templategroup['gid'], $expand_array))
1414          {
1415              $templategroup['expanded'] = 1;
1416          }
1417          $template_groups[$templategroup['prefix']] = $templategroup;
1418      }
1419  
1420  	function sort_template_groups($a, $b)
1421      {
1422          return strcasecmp($a['title'], $b['title']);
1423      }
1424      uasort($template_groups, "sort_template_groups");
1425  
1426      // Add the ungrouped templates group at the bottom
1427      $template_groups['-1'] = array(
1428          "prefix" => "",
1429          "title" => $lang->ungrouped_templates,
1430          "gid" => -1
1431      );
1432  
1433      // Load the list of templates
1434      $query = $db->simple_select("templates", "*", "sid='".intval($mybb->input['sid'])."' OR sid='-2'", array('order_by' => 'sid DESC, title', 'order_dir' => 'ASC'));
1435      while($template = $db->fetch_array($query))
1436      {
1437          $exploded = explode("_", $template['title'], 2);
1438  
1439          if(isset($template_groups[$exploded[0]]))
1440          {
1441              $group = $exploded[0];
1442          }
1443          else
1444          {
1445              $group = -1;
1446          }
1447  
1448          $template['gid'] = -1;
1449          if(isset($template_groups[$exploded[0]]['gid']))
1450          {
1451              $template['gid'] = $template_groups[$exploded[0]]['gid'];
1452          }
1453  
1454          // If this template is not a master template, we simple add it to the list
1455          if($template['sid'] != -2)
1456          {
1457              $template['original'] = false;
1458              $template['modified'] = false;
1459              $template_groups[$group]['templates'][$template['title']] = $template;
1460          }
1461          else if(!in_array($template['gid'], $expand_array) && !isset($expand_array[-1]))
1462          {
1463              $template['original'] = true;
1464              $template['modified'] = false;
1465              $template_groups[$group]['templates'][$template['title']] = $template;
1466  
1467              // Save some memory!
1468              unset($template_groups[$group]['templates'][$template['title']]['template']);
1469          }
1470          // Otherwise, if we are down to master templates we need to do a few extra things
1471          else
1472          {
1473              // Master template that hasn't been customised in the set we have expanded
1474              if(!isset($template_groups[$group]['templates'][$template['title']]) || $template_groups[$group]['templates'][$template['title']]['template'] == $template['template'])
1475              {
1476                  $template['original'] = true;
1477                  $template_groups[$group]['templates'][$template['title']] = $template;
1478              }
1479              // Template has been modified in the set we have expanded (it doesn't match the master)
1480              else if($template_groups[$group]['templates'][$template['title']]['template'] != $template['template'] && $template_groups[$group]['templates'][$template['title']]['sid'] != -2)
1481              {
1482                  $template_groups[$group]['templates'][$template['title']]['modified'] = true;
1483              }
1484  
1485              // Save some memory!
1486              unset($template_groups[$group]['templates'][$template['title']]['template']);
1487          }
1488      }
1489  
1490      foreach($template_groups as $prefix => $group)
1491      {
1492          $tmp_expand = "";
1493          if(in_array($group['gid'], $expand_array))
1494          {
1495              $expand = $lang->collapse;
1496              $expanded = true;
1497  
1498              $tmp_expand = $expand_array;
1499              $unsetgid = array_search($group['gid'], $tmp_expand);
1500              unset($tmp_expand[$unsetgid]);
1501              $group['expand_str'] = implode("|", $tmp_expand);
1502          }
1503          else
1504          {
1505              $expand = $lang->expand;
1506              $expanded = false;
1507  
1508              $group['expand_str'] = implode("|", $expand_array);
1509              if($group['expand_str'])
1510              {
1511                  $group['expand_str'] .= "|";
1512              }
1513              $group['expand_str'] .= $group['gid'];
1514          }
1515  
1516          if($group['expand_str'])
1517          {
1518              $group['expand_str'] = "&amp;expand={$group['expand_str']}";
1519          }
1520  
1521          if($expanded == true && isset($group['templates']) && count($group['templates']) > 0)
1522          {
1523              $table->construct_cell("<strong><a href=\"index.php?module=style-templates&amp;sid={$sid}{$group['expand_str']}#group_{$group['gid']}\">{$group['title']}</a></strong>");
1524              $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"));
1525              $table->construct_row(array("class" => "alt_row", "id" => "group_".$group['gid'], "name" => "group_".$group['gid']));
1526  
1527              $templates = $group['templates'];
1528              ksort($templates);
1529  
1530              foreach($templates as $template)
1531              {
1532                  $template['pretty_title'] = $template['title'];
1533  
1534                  $popup = new PopupMenu("template_{$template['tid']}", $lang->options);
1535                  $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}");
1536  
1537                  if(isset($template['modified']) && $template['modified'] == true)
1538                  {
1539                      if($sid > 0)
1540                      {
1541                          $popup->add_item($lang->diff_report, "index.php?module=style-templates&amp;action=diff_report&amp;title=".urlencode($template['title'])."&amp;sid2={$sid}");
1542  
1543                          $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}')");
1544                      }
1545  
1546                      $template['pretty_title'] = "<span style=\"color: green;\">{$template['title']}</span>";
1547                  }
1548                  // This template does not exist in the master list
1549                  else if(isset($template['original']) && $template['original'] == false)
1550                  {
1551                      $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}')");
1552  
1553                      $template['pretty_title'] = "<span style=\"color: blue;\">{$template['title']}</span>";
1554                  }
1555  
1556                  $table->construct_cell("<span style=\"padding: 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>");
1557                  $table->construct_cell($popup->fetch(), array("class" => "align_center"));
1558  
1559                  $table->construct_row();
1560              }
1561          }
1562          else if(isset($group['templates']) && count($group['templates']) > 0)
1563          {
1564              $table->construct_cell("<strong><a href=\"index.php?module=style-templates&amp;sid={$sid}{$group['expand_str']}#group_{$group['gid']}\">{$group['title']}</a></strong>");
1565               $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"));
1566               $table->construct_row(array("class" => "alt_row", "id" => "group_".$group['gid'], "name" => "group_".$group['gid']));
1567          }
1568      }
1569  
1570      $table->output($template_sets[$sid]);
1571  
1572      $page->output_footer();
1573  }
1574  
1575  if(!$mybb->input['action'])
1576  {
1577      $plugins->run_hooks("admin_style_templates_start");
1578  
1579      $page->output_header($lang->template_sets);
1580  
1581      $page->output_nav_tabs($sub_tabs, 'templates');
1582  
1583      $themes = array();
1584      $query = $db->simple_select("themes", "name,tid,properties", "tid != '1'");
1585      while($theme = $db->fetch_array($query))
1586      {
1587          $tbits = unserialize($theme['properties']);
1588          $themes[$tbits['templateset']][$theme['tid']] = htmlspecialchars_uni($theme['name']);
1589      }
1590  
1591      $template_sets = array();
1592      $template_sets[-1]['title'] = $lang->global_templates;
1593      $template_sets[-1]['sid'] = -1;
1594  
1595      $query = $db->simple_select("templatesets", "*", "", array('order_by' => 'title', 'order_dir' => 'ASC'));
1596      while($template_set = $db->fetch_array($query))
1597      {
1598          $template_sets[$template_set['sid']] = $template_set;
1599      }
1600  
1601      $table = new Table;
1602      $table->construct_header($lang->template_set);
1603      $table->construct_header($lang->controls, array("class" => "align_center", "width" => 150));
1604  
1605      foreach($template_sets as $set)
1606      {
1607          if($set['sid'] == -1)
1608          {
1609              $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>");
1610              $table->construct_cell("<a href=\"index.php?module=style-templates&amp;sid=-1\">{$lang->expand_templates}</a>", array("class" => "align_center"));
1611              $table->construct_row();
1612              continue;
1613          }
1614  
1615          if($themes[$set['sid']])
1616          {
1617              $used_by_note = $lang->used_by;
1618              $comma = "";
1619              foreach($themes[$set['sid']] as $theme_name)
1620              {
1621                  $used_by_note .= $comma.$theme_name;
1622                  $comma = $lang->comma;
1623              }
1624          }
1625          else
1626          {
1627              $used_by_note = $lang->not_used_by_any_themes;
1628          }
1629  
1630          if($set['sid'] == 1)
1631          {
1632              $actions = "<a href=\"index.php?module=style-templates&amp;sid={$set['sid']}\">{$lang->expand_templates}</a>";
1633          }
1634          else
1635          {
1636              $popup = new PopupMenu("templateset_{$set['sid']}", $lang->options);
1637              $popup->add_item($lang->expand_templates, "index.php?module=style-templates&amp;sid={$set['sid']}");
1638  
1639              if($set['sid'] != 1)
1640              {
1641                  $popup->add_item($lang->edit_template_set, "index.php?module=style-templates&amp;action=edit_set&amp;sid={$set['sid']}");
1642  
1643                  if(!$themes[$set['sid']])
1644                  {
1645                      $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}')");
1646                  }
1647              }
1648  
1649              $actions = $popup->fetch();
1650          }
1651  
1652          $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>");
1653          $table->construct_cell($actions, array("class" => "align_center"));
1654          $table->construct_row();
1655      }
1656  
1657      $table->output($lang->template_sets);
1658  
1659      $page->output_footer();
1660  }
1661  ?>


Generated: Tue Oct 8 19:19:50 2013 Cross-referenced by PHPXref 0.7.1