<?php
/**
* MyBB 1.2
* Copyright © 2006 MyBB Group, All Rights Reserved
*
* Website: http://www.mybboard.net
* License: http://www.mybboard.net/eula.html
*
* $Id: templates.php 3030 2007-04-26 00:19:47Z Tikitiki $
*/
define("IN_MYBB", 1);
require_once "./global.php";
// Just a little fix here
$db->query("DELETE FROM ".TABLE_PREFIX."templates WHERE title=''");
// Load language packs for this section
global $lang;
$lang->load("templates");
$mybb->input['tid'] = intval($mybb->input['tid']);
$mybb->input['setid'] = intval($mybb->input['setid']);
$mybb->input['expand'] = intval($mybb->input['expand']);
$mybb->input['sid2'] = intval($mybb->input['sid2']);
$mybb->input['sid'] = intval($mybb->input['sid']);
addacpnav($lang->nav_templates, "templates.php?".SID);
switch($mybb->input['action'])
{
case "add":
addacpnav($lang->nav_add_template);
break;
case "edit":
addacpnav($lang->nav_edit_template);
break;
case "delete":
addacpnav($lang->nav_delete_template);
break;
case "addset":
addacpnav($lang->nav_add_set);
break;
case "editset":
addacpnav($lang->nav_edit_set);
break;
case "deleteset":
addacpnav($lang->nav_delete_set);
break;
case "findupdated":
addacpnav($lang->nav_find_updated);
break;
case "diff":
addacpnav($lang->nav_diff);
break;
default:
if($mybb->input['expand'])
{
if($mybb->input['expand'] == "-1")
{
addacpnav($lang->global_templates);
}
else
{
$query = $db->query("SELECT * FROM ".TABLE_PREFIX."templatesets WHERE sid='".intval($mybb->input['expand'])."'");
$set = $db->fetch_array($query);
addacpnav($set['title']);
}
}
break;
}
$plugins->run_hooks("admin_templates_start");
$expand = $mybb->input['expand'];
$group = $mybb->input['group'];
checkadminpermissions("canedittemps");
logadmin();
if($mybb->input['action'] == "do_add")
{
$query = $db->query("SELECT * FROM ".TABLE_PREFIX."templates WHERE sid='".intval($mybb->input['setid'])."' AND title='".$db->escape_string($mybb->input['title'])."'");
$temp = $db->fetch_array($query);
if($temp['tid'])
{
cperror($lang->name_exists);
}
$query = $db->query("SELECT * FROM ".TABLE_PREFIX."templates WHERE title='".$db->escape_string($mybb->input['title'])."' AND sid='-2'");
$templateinfo = $db->fetch_array($query);
if($templateinfo['template'] == $mybb->input['template'])
{
cperror($lang->template_same_master);
}
$newtemplate = array(
"title" => $db->escape_string($mybb->input['title']),
"template" => $db->escape_string($mybb->input['template']),
"sid" => intval($mybb->input['setid']),
"version" => $mybb->version_code,
"status" => "",
"dateline" => time()
);
$plugins->run_hooks("admin_templates_do_add");
$db->insert_query(TABLE_PREFIX."templates", $newtemplate);
$tid = $db->insert_id();
if($mybb->input['group'])
{
$opengroup = "&group=".$mybb->input['group']."#".$mybb->input['group'];
}
if($mybb->input['continue'] != "yes")
{
$editurl = "templates.php?".SID."&expand=".$mybb->input['setid'].$opengroup;
}
else
{
$editurl = "templates.php?".SID."&action=edit&tid=".$tid."&continue=yes&group=".$mybb->input['group'];
}
cpredirect($editurl, $lang->template_added);
}
if($mybb->input['action'] == "do_addset")
{
$newset = array(
"title" => $db->escape_string($mybb->input['title'])
);
$plugins->run_hooks("admin_templates_do_addset");
$db->insert_query(TABLE_PREFIX."templatesets", $newset);
$setid = $db->insert_id();
cpredirect("templates.php?".SID."&expand=$setid", $lang->set_added);
}
if($mybb->input['action'] == "do_delete")
{
if($mybb->input['deletesubmit'])
{
$plugins->run_hooks("admin_templates_do_delete");
$db->query("DELETE FROM ".TABLE_PREFIX."templates WHERE tid='".$mybb->input['tid']."'");
if($mybb->input['group'])
{
$opengroup = "&group=".$mybb->input['group']."#".$mybb->input['group'];
}
cpredirect("templates.php?".SID."&expand=".$mybb->input['expand'].$opengroup, $lang->template_deleted);
}
else
{
$mybb->input['action'] = "modify";
$expand = $template[sid];
}
}
if($mybb->input['action'] == "do_deleteset")
{
if($mybb->input['deletesubmit'])
{
$plugins->run_hooks("admin_templates_do_deleteset");
$db->query("DELETE FROM ".TABLE_PREFIX."templatesets WHERE sid='".$mybb->input['setid']."'");
$db->query("DELETE FROM ".TABLE_PREFIX."templates WHERE sid='".$mybb->input['setid']."'");
cpredirect("templates.php?".SID."&action=modify", $lang->set_deleted);
}
else
{
cpredirect("templates.php?".SID);
}
}
if($mybb->input['action'] == "do_editset")
{
$plugins->run_hooks("admin_templates_do_editset");
$db->query("UPDATE ".TABLE_PREFIX."templatesets SET title='".$db->escape_string($mybb->input['title'])."' WHERE sid='".intval($mybb->input['setid'])."'");
cpredirect("templates.php?".SID, $lang->set_edited);
}
if($mybb->input['action'] == "do_edit")
{
$query = $db->query("SELECT * FROM ".TABLE_PREFIX."templates WHERE tid='".$mybb->input['tid']."'");
$templateinfo = $db->fetch_array($query);
if($mybb->input['title'] == "")
{
$mybb->input['title'] = $templateinfo['title'];
}
$updatedtemplate = array(
"title" => $db->escape_string($mybb->input['title']),
"template" => $db->escape_string($mybb->input['template']),
"sid" => intval($mybb->input['setid']),
"version" => $mybb->version_code,
"status" => "",
"dateline" => time()
);
$plugins->run_hooks("admin_templates_do_edit");
$db->update_query(TABLE_PREFIX."templates", $updatedtemplate, "tid='".$mybb->input['tid']."'");
if($mybb->input['group'])
{
$opengroup = "&group=".$mybb->input['group']."#".$mybb->input['group'];
}
if($mybb->input['continue'] != "yes")
{
$editurl = "templates.php?".SID."&expand=".$mybb->input['setid'].$opengroup;
}
else
{
$editurl = "templates.php?".SID."&action=edit&tid=".$mybb->input['tid']."&continue=yes&group=".$mybb->input['group'];
}
cpredirect($editurl, $lang->template_edited);
}
if($mybb->input['action'] == "do_replace")
{
$noheader = 1;
// Is there something to search for?
if(!$mybb->input['find'])
{ // Nope!
cperror($lang->search_noneset);
}
else
{ // Yup!
cpheader();
starttable();
tableheader($lang->search_results);
// Get the names of all template sets
$query = $db->query("SELECT sid, title FROM ".TABLE_PREFIX."templatesets");
$template_groups[-2] = $lang->default_templates;
$template_groups[-1] = $lang->global_templates;
while($tgroup = $db->fetch_array($query))
{
$template_groups[$tgroup['sid']] = $tgroup['title'];
}
$plugins->run_hooks("admin_templates_do_replace");
// Select all templates with that search term
$query = $db->query("SELECT tid, title, template, sid FROM ".TABLE_PREFIX."templates WHERE template LIKE '%".$db->escape_string($mybb->input['find'])."%' ORDER BY sid,title ASC");
if($db->num_rows($query) == 0)
{
makelabelcode(sprintf($lang->search_noresults, $mybb->input['find']));
}
else
{
while($template = $db->fetch_array($query))
{
if($template['sid'] == 1)
{
$template_list[-2][$template['title']] = $template;
}
else
{
$template_list[$template['sid']][$template['title']] = $template;
}
}
// Loop templates we found
foreach($template_list as $sid => $templates)
{
// Show group header
$search_header = sprintf($lang->search_header, $mybb->input['find'], $template_groups[$sid]);
tablesubheader($search_header);
foreach($templates as $title => $template)
{
// Do replacement
$newtemplate = str_replace($mybb->input['find'], $mybb->input['replace'], $template['template']);
if($newtemplate != $template['template'])
{
// If the template is different, that means the search term has been found.
if($mybb->input['replace'] != "")
{
if($template['sid'] == -2)
{
// The template is a master template. We have to make a new custom template.
$new_template = array(
"title" => $db->escape_string($title),
"template" => $db->escape_string($newtemplate),
"sid" => 1,
"version" => $mybb->version_code,
"status" => '',
"dateline" => time()
);
$db->insert_query(TABLE_PREFIX."templates", $new_template);
$new_tid = $db->insert_id();
$label = sprintf($lang->search_created_custom, $template['title']);
makelabelcode($label, makelinkcode($lang->search_edit, "templates.php?".SID."&action=edit&tid=".$new_tid));
}
else
{
// The template is a custom template. Replace as normal.
// Update the template if there is a replacement term
$updatedtemplate = array(
"template" => $db->escape_string($newtemplate)
);
$db->update_query(TABLE_PREFIX."templates", $updatedtemplate, "tid='".$template['tid']."'");
$label = sprintf($lang->search_updated, $template['title']);
makelabelcode($label, makelinkcode($lang->search_edit, "templates.php?".SID."&action=edit&tid=".$template['tid']));
}
}
else
{
// Just show that the term was found
if($template['sid'] == -2)
{
$label = sprintf($lang->search_found, $template['title']);
makelabelcode($label, makelinkcode($lang->search_change_original, "templates.php?".SID."&action=add&title=".$template['title']."&sid=1"));
}
else
{
$label = sprintf($lang->search_found, $template['title']);
makelabelcode($label, makelinkcode($lang->search_edit, "templates.php?".SID."&action=edit&tid=".$template['tid']));
}
}
}
}
}
}
endtable();
cpfooter();
}
}
if($mybb->input['action'] == "do_search_names")
{
// Is there something to search for?
if(!$mybb->input['title'])
{ // Nope!
cperror($lang->search_noneset);
}
else
{ // Yup!
$plugins->run_hooks("admin_templates_do_search_names");
cpheader();
starttable();
tableheader($lang->search_results);
$lang->search_names_header = sprintf($lang->search_names_header, $mybb->input['title']);
tablesubheader($lang->search_names_header);
// Query for templates
$query = $db->query("
SELECT t.tid, t.title, t.sid, s.title as settitle, t2.tid as customtid
FROM ".TABLE_PREFIX."templates t
LEFT JOIN ".TABLE_PREFIX."templatesets s ON (t.sid=s.sid)
LEFT JOIN ".TABLE_PREFIX."templates t2 ON (t.title=t2.title AND t2.sid='1')
WHERE t.title LIKE '%".$db->escape_string($mybb->input['title'])."%'
ORDER BY t.title ASC
");
while($template = $db->fetch_array($query))
{
$link = makelinkcode($lang->search_edit, "templates.php?".SID."&action=edit&tid=".$template['tid']);
if($template['sid'] == -2