[ Index ]

PHP Cross Reference of MyBB 1.6.7

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


Generated: Sat Mar 31 17:55:03 2012 Cross-referenced by PHPXref 0.7.1