Please make sure IN_MYBB is defined.");
}
$page->add_breadcrumb_item($lang->template_sets, "index.php?module=style-templates");
$sid = $mybb->get_input('sid', MyBB::INPUT_INT);
$expand_str = "";
$expand_str2 = "";
$expand_array = array();
if(isset($mybb->input['expand']))
{
$expand_array = explode("|", $mybb->input['expand']);
$expand_array = array_map("intval", $expand_array);
$expand_str = "&expand=".implode("|", $expand_array);
$expand_str2 = "&expand=".implode("|", $expand_array);
}
if($mybb->input['action'] == "add_set" || $mybb->input['action'] == "add_template" || ($mybb->input['action'] == "add_template_group" && !$sid) || $mybb->input['action'] == "search_replace" || $mybb->input['action'] == "find_updated" || (!$mybb->input['action'] && !$sid))
{
$sub_tabs['templates'] = array(
'title' => $lang->manage_template_sets,
'link' => "index.php?module=style-templates",
'description' => $lang->manage_template_sets_desc
);
$sub_tabs['add_set'] = array(
'title' => $lang->add_set,
'link' => "index.php?module=style-templates&action=add_set".$expand_str
);
if($mybb->get_input('sid', MyBB::INPUT_INT) != -1)
{
$sub_tabs['add_template_group'] = array(
'title' => $lang->add_template_group,
'link' => "index.php?module=style-templates&action=add_template_group".$expand_str,
'description' => $lang->add_template_group_desc
);
}
$sub_tabs['search_replace'] = array(
'title' => $lang->search_replace,
'link' => "index.php?module=style-templates&action=search_replace",
'description' => $lang->search_replace_desc
);
$sub_tabs['find_updated'] = array(
'title' => $lang->find_updated,
'link' => "index.php?module=style-templates&action=find_updated",
'description' => $lang->find_updated_desc
);
}
else if(($sid && !$mybb->input['action']) || $mybb->input['action'] == "edit_set" || $mybb->input['action'] == "check_set" || $mybb->input['action'] == "edit_template" || $mybb->input['action'] == "add_template_group")
{
$sub_tabs['manage_templates'] = array(
'title' => $lang->manage_templates,
'link' => "index.php?module=style-templates&sid=".$sid.$expand_str,
'description' => $lang->manage_templates_desc
);
if($sid > 0)
{
$sub_tabs['edit_set'] = array(
'title' => $lang->edit_set,
'link' => "index.php?module=style-templates&action=edit_set&sid=".$sid.$expand_str,
'description' => $lang->edit_set_desc
);
}
$sub_tabs['add_template'] = array(
'title' => $lang->add_template,
'link' => "index.php?module=style-templates&action=add_template&sid=".$sid.$expand_str,
'description' => $lang->add_template_desc
);
if($mybb->get_input('sid', MyBB::INPUT_INT) != -1)
{
$sub_tabs['add_template_group'] = array(
'title' => $lang->add_template_group,
'link' => "index.php?module=style-templates&action=add_template_group&sid=".$sid.$expand_str,
'description' => $lang->add_template_group_desc
);
}
}
$template_sets = array();
$template_sets[-1] = $lang->global_templates;
$query = $db->simple_select("templatesets", "*", "", array('order_by' => 'title', 'order_dir' => 'ASC'));
while($template_set = $db->fetch_array($query))
{
$template_sets[$template_set['sid']] = htmlspecialchars_uni($template_set['title']);
}
$plugins->run_hooks("admin_style_templates");
if($mybb->input['action'] == "add_set")
{
$plugins->run_hooks("admin_style_templates_add_set");
if($mybb->request_method == "post")
{
if(!trim($mybb->input['title']))
{
$errors[] = $lang->error_missing_set_title;
}
if(!$errors)
{
$sid = $db->insert_query("templatesets", array('title' => $db->escape_string($mybb->input['title'])));
$plugins->run_hooks("admin_style_templates_add_set_commit");
// Log admin action
log_admin_action($sid, $mybb->input['title']);
flash_message($lang->success_template_set_saved, 'success');
admin_redirect("index.php?module=style-templates&sid=".$sid);
}
}
$page->add_breadcrumb_item($lang->add_set);
$page->output_header($lang->add_set);
$sub_tabs = array();
$sub_tabs['add_set'] = array(
'title' => $lang->add_set,
'link' => "index.php?module=style-templates&action=add_set",
'description' => $lang->add_set_desc
);
$page->output_nav_tabs($sub_tabs, 'add_set');
if($errors)
{
$page->output_inline_error($errors);
}
else
{
$mybb->input['title'] = "";
}
$form = new Form("index.php?module=style-templates&action=add_set", "post", "add_set");
$form_container = new FormContainer($lang->add_set);
$form_container->output_row($lang->title, "", $form->generate_text_box('title', htmlspecialchars_uni($mybb->input['title']), array('id' => 'title')), 'title');
$form_container->end();
$buttons = array();
$buttons[] = $form->generate_submit_button($lang->save);
$form->output_submit_wrapper($buttons);
$form->end();
$page->output_footer();
}
if($mybb->input['action'] == "add_template")
{
$plugins->run_hooks("admin_style_templates_add_template");
if($mybb->request_method == "post")
{
if(empty($mybb->input['title']))
{
$errors[] = $lang->error_missing_set_title;
}
else
{
$query = $db->simple_select("templates", "COUNT(tid) as count", "title='".$db->escape_string($mybb->input['title'])."' AND (sid = '-2' OR sid = '{$sid}')");
if($db->fetch_field($query, "count") > 0)
{
$errors[] = $lang->error_already_exists;
}
}
if(!isset($template_sets[$sid]))
{
$errors[] = $lang->error_invalid_set;
}
// Are we trying to do malicious things in our template?
if(check_template($mybb->input['template']))
{
$errors[] = $lang->error_security_problem;
}
if(!$errors)
{
$template_array = array(
'title' => $db->escape_string($mybb->input['title']),
'sid' => $sid,
'template' => $db->escape_string(rtrim($mybb->input['template'])),
'version' => $db->escape_string($mybb->version_code),
'status' => '',
'dateline' => TIME_NOW
);
$tid = $db->insert_query("templates", $template_array);
$plugins->run_hooks("admin_style_templates_add_template_commit");
// Log admin action
log_admin_action($tid, $mybb->input['title'], $sid, $template_sets[$sid]);
flash_message($lang->success_template_saved, 'success');
if($mybb->input['continue'])
{
admin_redirect("index.php?module=style-templates&action=edit_template&title=".urlencode($mybb->input['title'])."&sid=".$sid.$expand_str2);
}
else
{
admin_redirect("index.php?module=style-templates&sid=".$sid.$expand_str2);
}
}
}
if($errors)
{
$template = $mybb->input;
}
else
{
if(!$sid)
{
$sid = -1;
}
$template['title'] = "";
$template['template'] = "";
$template['sid'] = $sid;
}
if($mybb->input['sid'])
{
$page->add_breadcrumb_item($template_sets[$sid], "index.php?module=style-templates&sid={$sid}{$expand_str}");
}
if($admin_options['codepress'] != 0)
{
$page->extra_header .= '
';
}
$page->add_breadcrumb_item($lang->add_template);
$page->output_header($lang->add_template);
$sub_tabs = array();
$sub_tabs['add_template'] = array(
'title' => $lang->add_template,
'link' => "index.php?module=style-templates&action=add_template&sid=".$template['sid'].$expand_str,
'description' => $lang->add_template_desc
);
$page->output_nav_tabs($sub_tabs, 'add_template');
if($errors)
{
$page->output_inline_error($errors);
}
$form = new Form("index.php?module=style-templates&action=add_template{$expand_str}", "post", "add_template");
$form_container = new FormContainer($lang->add_template, 'tfixed');
$form_container->output_row($lang->template_name, $lang->template_name_desc, $form->generate_text_box('title', $template['title'], array('id' => 'title')), 'title');
$form_container->output_row($lang->template_set, $lang->template_set_desc, $form->generate_select_box('sid', $template_sets, $sid), 'sid');
$form_container->output_row("", "", $form->generate_text_area('template', $template['template'], array('id' => 'template', 'class' => '', 'style' => 'width: 100%; height: 500px;')), 'template');
$form_container->end();
$buttons[] = $form->generate_submit_button($lang->save_continue, array('name' => 'continue'));
$buttons[] = $form->generate_submit_button($lang->save_close, array('name' => 'close'));
$form->output_submit_wrapper($buttons);
$form->end();
if($admin_options['codepress'] != 0)
{
echo '';
}
$page->output_footer();
}
if($mybb->input['action'] == "add_template_group")
{
$plugins->run_hooks("admin_style_templates_add_template_group");
if($mybb->get_input('sid', MyBB::INPUT_INT) == -1)
{
admin_redirect("index.php?module=style-templates&sid={$sid}".$expand_str2);
}
$errors = array();
if($mybb->request_method == "post")
{
$prefix = trim($mybb->input['prefix']);
if(!$prefix)
{
$errors[] = $lang->error_missing_group_prefix;
}
if(strpos($prefix, "_") !== false)
{
$errors[] = $lang->error_invalid_group_title;
}
$title = trim($mybb->input['title']);
if(!$title)
{
$errors[] = $lang->error_missing_group_title;
}
if(!$errors)
{
$query = $db->simple_select("templategroups", "COUNT(gid) AS gid", "prefix = '".$db->escape_string($mybb->input['prefix'])."'");
$prefix_count = $db->fetch_field($query, 'gid');
if($prefix_count >= 1)
{
$errors[] = $lang->error_duplicate_group_prefix;
}
else
{
// Add template group
$insert_array = array(
'prefix' => $db->escape_string($prefix),
'title' => $db->escape_string($title),
'isdefault' => 0
);
$gid = $db->insert_query('templategroups', $insert_array);
$plugins->run_hooks('admin_style_templates_add_template_group_commit');
log_admin_action($gid, $title);
flash_message($lang->success_template_group_saved, 'success');
if($sid)
{
admin_redirect("index.php?module=style-templates&sid={$sid}".$expand_str2);
}
admin_redirect('index.php?module=style-templates');
}
}
}
if(!empty($mybb->input['sid']))
{
$page->add_breadcrumb_item($template_sets[$sid], "index.php?module=style-templates&sid={$sid}{$expand_str}");
}
$page->add_breadcrumb_item($lang->add_template_group);
$page->output_header($lang->add_template_group);
$page->output_nav_tabs($sub_tabs, 'add_template_group');
$template_group = array();
if($errors)
{
$template_group = array(
'prefix' => $prefix,
'title' => $title
);
$page->output_inline_error($errors);
}
else
{
$template_group = array(
'prefix' => null,
'title' => null,
);
}
$form = new Form("index.php?module=style-templates&action=add_template_group{$expand_str}", "post", "add_template_group");
echo $form->generate_hidden_field('sid', $sid);
$form_container = new FormContainer($lang->add_template_group);
$form_container->output_row($lang->template_group_prefix, $lang->template_group_prefix_desc, $form->generate_text_box('prefix', $template_group['prefix'], array('id' => 'prefix')), 'prefix');
$form_container->output_row($lang->template_group_title, $lang->template_group_title_desc, $form->generate_text_box('title', $template_group['title'], array('id' => 'title')), 'title');
$form_container->end();
$buttons = array(
$form->generate_submit_button($lang->add_template_group)
);
$form->output_submit_wrapper($buttons);
$form->end();
$page->output_footer();
}
if($mybb->input['action'] == "edit_set")
{
$query = $db->simple_select("templatesets", "*", "sid='{$sid}'");
$set = $db->fetch_array($query);
if(!$set)
{
flash_message($lang->error_invalid_input, 'error');
admin_redirect("index.php?module=style-templates");
}
$plugins->run_hooks("admin_style_templates_edit_set");
$sid = $set['sid'];
if($mybb->request_method == "post")
{
if(!trim($mybb->input['title']))
{
$errors[] = $lang->error_missing_set_title;
}
if(!$errors)
{
$query = $db->update_query("templatesets", array('title' => $db->escape_string($mybb->input['title'])), "sid='{$sid}'");
$plugins->run_hooks("admin_style_templates_edit_set_commit");
// Log admin action
log_admin_action($sid, $set['title']);
flash_message($lang->success_template_set_saved, 'success');
admin_redirect("index.php?module=style-templates&sid=".$sid.$expand_str2);
}
}
if($sid)
{
$page->add_breadcrumb_item($template_sets[$sid], "index.php?module=style-templates&sid={$sid}{$expand_str}");
}
$page->add_breadcrumb_item($lang->edit_set);
$page->output_header($lang->edit_set);
$sub_tabs = array();
$sub_tabs['edit_set'] = array(
'title' => $lang->edit_set,
'link' => "index.php?module=style-templates&action=edit_set&sid=".$sid,
'description' => $lang->edit_set_desc
);
$page->output_nav_tabs($sub_tabs, 'edit_set');
if($errors)
{
$page->output_inline_error($errors);
}
else
{
$query = $db->simple_select("templatesets", "title", "sid='{$sid}'");
$mybb->input['title'] = $db->fetch_field($query, "title");
}
$form = new Form("index.php?module=style-templates&action=edit_set{$expand_str}", "post", "edit_set");
echo $form->generate_hidden_field("sid", $sid);
$form_container = new FormContainer($lang->edit_set);
$form_container->output_row($lang->title, "", $form->generate_text_box('title', $mybb->input['title'], array('id' => 'title')), 'title');
$form_container->end();
$buttons = array();
$buttons[] = $form->generate_submit_button($lang->save);
$form->output_submit_wrapper($buttons);
$form->end();
$page->output_footer();
}
if($mybb->input['action'] == "edit_template")
{
if(!$mybb->input['title'] || !$sid || !isset($template_sets[$sid]))
{
flash_message($lang->error_missing_input, 'error');
admin_redirect("index.php?module=style-templates");
}
$plugins->run_hooks("admin_style_templates_edit_template");
if($mybb->request_method == "post")
{
if(empty($mybb->input['title']))
{
$errors[] = $lang->error_missing_title;
}
// Are we trying to do malicious things in our template?
if(check_template($mybb->input['template']))
{
$errors[] = $lang->error_security_problem;
}
if(!$errors)
{
$query = $db->simple_select("templates", "*", "tid='{$mybb->input['tid']}'");
$template = $db->fetch_array($query);
$template_array = array(
'title' => $db->escape_string($mybb->input['title']),
'sid' => $sid,
'template' => $db->escape_string(rtrim($mybb->input['template'])),
'version' => $mybb->version_code,
'status' => '',
'dateline' => TIME_NOW
);
// 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
$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));
$template['tid'] = $db->fetch_field($query, "tid");
$plugins->run_hooks("admin_style_templates_edit_template_commit_start");
if($sid > 0)
{
// Check to see if it's never been edited before (i.e. master) or if this a new template (i.e. we've renamed it) or if it's a custom template
$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'));
$existing_sid = $db->fetch_field($query, "sid");
$existing_rows = $db->num_rows($query);
if(($existing_sid == -2 && $existing_rows == 1) || $existing_rows == 0)
{
$template['tid'] = $db->insert_query("templates", $template_array);
}
else
{
$db->update_query("templates", $template_array, "tid='{$template['tid']}' AND sid != '-2'");
}
}
else
{
// Global template set
$db->update_query("templates", $template_array, "tid='{$template['tid']}' AND sid != '-2'");
}
$plugins->run_hooks("admin_style_templates_edit_template_commit");
$query = $db->simple_select("templatesets", "title", "sid='{$sid}'");
$set = $db->fetch_array($query);
$exploded = explode("_", $template_array['title'], 2);
$prefix = $exploded[0];
$query = $db->simple_select("templategroups", "gid", "prefix = '".$db->escape_string($prefix)."'");
$group = $db->fetch_field($query, "gid");
if(!$group)
{
$group = "-1";
}
// Log admin action
if(!empty($set['title']))
{
$title = $set['title'];
}
else
{
$title = null;
}
log_admin_action($template['tid'], $mybb->get_input('title'), $mybb->get_input('sid'), $title);
flash_message($lang->success_template_saved, 'success');
if($mybb->get_input('continue'))
{
if($mybb->get_input('from') == "diff_report")
{
admin_redirect("index.php?module=style-templates&action=edit_template&title=".urlencode($mybb->input['title'])."&sid=".$mybb->get_input('sid', MyBB::INPUT_INT).$expand_str2."&from=diff_report");
}
else
{
admin_redirect("index.php?module=style-templates&action=edit_template&title=".urlencode($mybb->input['title'])."&sid=".$mybb->get_input('sid', MyBB::INPUT_INT).$expand_str2);
}
}
else
{
if($mybb->get_input('from') == "diff_report")
{
admin_redirect("index.php?module=style-templates&action=find_updated");
}
else
{
admin_redirect("index.php?module=style-templates&sid=".$mybb->get_input('sid', MyBB::INPUT_INT).$expand_str2."#group_{$group}");
}
}
}
}
if($errors)
{
$template = $mybb->input;
}
else
{
$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));
$template = $db->fetch_array($query);
}
$template['title'] = htmlspecialchars_uni($template['title']);
if($admin_options['codepress'] != 0)
{
$page->extra_header .= '
';
}
$page->add_breadcrumb_item($template_sets[$sid], "index.php?module=style-templates&sid={$sid}{$expand_str}");
if(!isset($mybb->input['from']))
{
$mybb->input['from'] = '';
}
if($mybb->input['from'] == "diff_report")
{
$page->add_breadcrumb_item($lang->find_updated, "index.php?module=style-templates&action=find_updated");
}
$page->add_breadcrumb_item($lang->edit_template_breadcrumb.$template['title'], "index.php?module=style-templates&sid={$sid}");
$page->output_header($lang->sprintf($lang->editing_template, $template['title']));
$sub_tabs = array();
if($mybb->input['from'] == "diff_report")
{
$sub_tabs['find_updated'] = array(
'title' => $lang->find_updated,
'link' => "index.php?module=style-templates&action=find_updated"
);
$sub_tabs['diff_report'] = array(
'title' => $lang->diff_report,
'link' => "index.php?module=style-templates&action=diff_report&title=".urlencode($template['title'])."&sid1=".(int)$template['sid']."&sid2=-2",
);
}
$sub_tabs['edit_template'] = array(
'title' => $lang->edit_template,
'link' => "index.php?module=style-templates&action=edit_template&title=".htmlspecialchars_uni($template['title']).$expand_str,
'description' => $lang->edit_template_desc
);
$page->output_nav_tabs($sub_tabs, 'edit_template');
if($errors)
{
$page->output_inline_error($errors);
}
$form = new Form("index.php?module=style-templates&action=edit_template{$expand_str}", "post", "edit_template");
echo $form->generate_hidden_field('tid', $template['tid'])."\n";
if($mybb->input['from'] == "diff_report")
{
echo $form->generate_hidden_field('from', "diff_report");
}
$form_container = new FormContainer($lang->edit_template_breadcrumb.$template['title'], 'tfixed');
$form_container->output_row($lang->template_name, $lang->template_name_desc, $form->generate_text_box('title', $template['title'], array('id' => 'title')), 'title');
// Force users to save the default template to a specific set, rather than the "global" templates - where they can delete it
if($template['sid'] == "-2")
{
unset($template_sets[-1]);
}
$form_container->output_row($lang->template_set, $lang->template_set_desc, $form->generate_select_box('sid', $template_sets, $sid));
$form_container->output_row("", "", $form->generate_text_area('template', $template['template'], array('id' => 'template', 'class' => '', 'style' => 'width: 100%; height: 500px;')));
$form_container->end();
$buttons[] = $form->generate_submit_button($lang->save_continue, array('name' => 'continue'));
$buttons[] = $form->generate_submit_button($lang->save_close, array('name' => 'close'));
$form->output_submit_wrapper($buttons);
$form->end();
if($admin_options['codepress'] != 0)
{
echo '';
}
$page->output_footer();
}
if($mybb->input['action'] == "edit_template_group")
{
$query = $db->simple_select("templategroups", "*", "gid = '".$mybb->get_input('gid', MyBB::INPUT_INT)."'");
if(!$db->num_rows($query))
{
flash_message($lang->error_missing_template_group, 'error');
admin_redirect("index.php?module=style-templates&sid={$sid}{$expand_str}");
}
$template_group = $db->fetch_array($query);
if(isset($template_group['isdefault']) && $template_group['isdefault'] == 1)
{
flash_message($lang->error_default_template_group, 'error');
admin_redirect("index.php?module=style-templates&sid={$sid}{$expand_str}");
}
$plugins->run_hooks("admin_style_templates_edit_template_group");
$errors = array();
if($mybb->request_method == "post")
{
$prefix = trim($mybb->input['prefix']);
if(!$prefix)
{
$errors[] = $lang->error_missing_group_prefix;
}
if(strpos($prefix, "_") !== false)
{
$errors[] = $lang->error_invalid_group_title;
}
$title = trim($mybb->input['title']);
if(!$title)
{
$errors[] = $lang->error_missing_group_title;
}
if(!$errors)
{
if($prefix != $template_group['prefix'])
{
$query = $db->simple_select("templategroups", "COUNT(gid) AS gid", "prefix = '".$db->escape_string($mybb->input['prefix'])."'");
$prefix_count = $db->fetch_field($query, 'gid');
if($prefix_count >= 1)
{
$errors[] = $lang->error_duplicate_group_prefix;
}
}
if(!$errors)
{
// Add template group
$update_array = array(
'prefix' => $db->escape_string($prefix),
'title' => $db->escape_string($title),
'isdefault' => 0
);
$plugins->run_hooks('admin_style_templates_edit_template_group_commit');
$db->update_query('templategroups', $update_array, "gid = '{$template_group['gid']}'");
log_admin_action($template_group['gid'], $title);
flash_message($lang->success_template_group_saved, 'success');
admin_redirect("index.php?module=style-templates&sid={$sid}");
}
}
}
$lang->editing_template_group = $lang->sprintf($lang->editing_template_group, htmlspecialchars_uni($template_group['title']));
$page->add_breadcrumb_item($template_sets[$sid], "index.php?module=style-templates&sid={$sid}{$expand_str}");
$page->add_breadcrumb_item($lang->editing_template_group, "index.php?module=style-templates&sid={$sid}");
$page->output_header($lang->editing_template_group);
if($errors)
{
$template_group['prefix'] = $prefix;
$template_group['title'] = $title;
$page->output_inline_error($errors);
}
$form = new Form("index.php?module=style-templates&action=edit_template_group", "post");
echo $form->generate_hidden_field('sid', $sid);
echo $form->generate_hidden_field('gid', $template_group['gid']);
$form_container = new FormContainer($lang->edit_template_group);
$form_container->output_row($lang->template_group_prefix, $lang->template_group_prefix_desc, $form->generate_text_box('prefix', $template_group['prefix'], array('id' => 'prefix')), 'prefix');
$form_container->output_row($lang->template_group_title, $lang->template_group_title_desc, $form->generate_text_box('title', $template_group['title'], array('id' => 'title')), 'title');
$form_container->end();
$buttons = array(
$form->generate_submit_button($lang->save_template_group)
);
$form->output_submit_wrapper($buttons);
$form->end();
$page->output_footer();
}
if($mybb->input['action'] == "search_replace")
{
$plugins->run_hooks("admin_style_templates_search_replace");
if($mybb->request_method == "post")
{
if($mybb->input['type'] == "templates")
{
// Search and replace in templates
if(!$mybb->input['find'])
{
flash_message($lang->search_noneset, "error");
admin_redirect("index.php?module=style-templates&action=search_replace");
}
else
{
$page->add_breadcrumb_item($lang->search_replace);
$page->output_header($lang->search_replace);
$plugins->run_hooks("admin_style_templates_search_replace_find");
$page->output_nav_tabs($sub_tabs, 'search_replace');
$templates_list = array();
$table = new Table;
$template_sets = array();
// Get the names of all template sets
$template_sets[-2] = $lang->master_templates;
$template_sets[-1] = $lang->global_templates;
$query = $db->simple_select("templatesets", "sid, title");
while($set = $db->fetch_array($query))
{
$template_sets[$set['sid']] = $set['title'];
}
$search_string = str_replace(array("\r\n", "\r"), "\n", $mybb->input['find']);
$search_string = str_replace(array(" ", "\t"), "", $search_string);
// Select all templates with that search term
$query = $db->query("
SELECT t.tid, t.title, t.sid, t.template
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 REPLACE(REPLACE(REPLACE(REPLACE(t.template, '\r\n', '\n'), '\r', '\n'), ' ', ''), '\t', '') LIKE '%".$db->escape_string_like($search_string)."%' AND NOT (t.sid = -2 AND (t2.tid) IS NOT NULL)
ORDER BY t.title ASC
");
if($db->num_rows($query) == 0)
{
$table->construct_cell($lang->sprintf($lang->search_noresults, htmlspecialchars_uni($mybb->input['find'])), array("class" => "align_center"));
$table->construct_row();
$table->output($lang->search_results);
}
else
{
$template_list = array();
while($template = $db->fetch_array($query))
{
$template_list[$template['sid']][$template['title']] = $template;
}
$count = 0;
foreach($template_list as $sid => $templates)
{
++$count;
$search_header = $lang->sprintf($lang->search_header, htmlspecialchars_uni($mybb->input['find']), $template_sets[$sid]);
$table->construct_header($search_header, array("colspan" => 2));
foreach($templates as $title => $template)
{
// Do replacement
$newtemplate = str_ireplace($mybb->input['find'], $mybb->input['replace'], $template['template']);
if($newtemplate != $template['template'] && check_template($newtemplate) === false)
{
// If the template is different, that means the search term has been found.
if(trim($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_NOW
);
$new_tid = $db->insert_query("templates", $new_template);
$label = $lang->sprintf($lang->search_created_custom, $template['title']);
$url = "index.php?module=style-templates&action=edit_template&title=".urlencode($template['title'])."&sid=1";
}
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("templates", $updatedtemplate, "tid='".$template['tid']."'");
$label = $lang->sprintf($lang->search_updated, $template['title']);
$url = "index.php?module=style-templates&action=edit_template&title=".urlencode($template['title'])."&sid={$template['sid']}";
}
}
else
{
// Just show that the term was found
if($template['sid'] == -2)
{
$label = $lang->sprintf($lang->search_found, $template['title']);
}
else
{
$label = $lang->sprintf($lang->search_found, $template['title']);
$url = "index.php?module=style-templates&action=edit_template&title=".urlencode($template['title'])."&sid={$template['sid']}";
}
}
}
else
{
// Just show that the term was found
if($template['sid'] == -2)
{
$label = $lang->sprintf($lang->search_found, $template['title']);
}
else
{
$label = $lang->sprintf($lang->search_found, $template['title']);
$url = "index.php?module=style-templates&action=edit_template&title=".urlencode($template['title'])."&sid={$template['sid']}";
}
}
$table->construct_cell(htmlspecialchars_uni($label), array("width" => "85%"));
if($sid == -2)
{
$popup = new PopupMenu("template_{$template['tid']}", $lang->options);
foreach($template_sets as $set_sid => $title)
{
if($set_sid > 0)
{
$popup->add_item($lang->edit_in." ".htmlspecialchars_uni($title), "index.php?module=style-templates&action=edit_template&title=".urlencode($template['title'])."&sid={$set_sid}");
}
}
$table->construct_cell($popup->fetch(), array("class" => "align_center"));
}
else
{
$table->construct_cell("{$lang->edit}", array("class" => "align_center"));
}
$table->construct_row();
}
if($count == 1)
{
$table->output($lang->search_results);
}
else
{
$table->output();
}
}
}
if(trim($mybb->input['replace']) != "")
{
// Log admin action - only if replace
log_admin_action($mybb->input['find'], $mybb->input['replace']);
}
$page->output_footer();
exit;
}
}
else
{
if(!$mybb->input['title'])
{
flash_message($lang->search_noneset, "error");
admin_redirect("index.php?module=style-templates&action=search_replace");
}
else
{
// Search Template Titles
$templatessets = array();
$templates_sets = array();
// Get the names of all template sets
$template_sets[-2] = $lang->master_templates;
$template_sets[-1] = $lang->global_templates;
$plugins->run_hooks("admin_style_templates_search_replace_title");
$query = $db->simple_select("templatesets", "sid, title");
while($set = $db->fetch_array($query))
{
$template_sets[$set['sid']] = $set['title'];
}
$table = new Table;
$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_like($mybb->input['title'])."%'
ORDER BY t.title ASC
");
while($template = $db->fetch_array($query))
{
if($template['sid'] == -2)
{
if(!$template['customtid'])
{
$template['original'] = true;
}
else
{
$template['modified'] = true;
}
}
else
{
$template['original'] = false;
$template['modified'] = false;
}
$templatessets[$template['sid']][$template['title']] = $template;
}
$page->add_breadcrumb_item($lang->search_replace);
$page->output_header($lang->search_replace);
$page->output_nav_tabs($sub_tabs, 'search_replace');
if(empty($templatessets))
{
$table->construct_cell($lang->sprintf($lang->search_noresults_title, htmlspecialchars_uni($mybb->input['title'])), array("class" => "align_center"));
$table->construct_row();
$table->output($lang->search_results);
}
$count = 0;
foreach($templatessets as $sid => $templates)
{
++$count;
$table->construct_header($template_sets[$sid], array("colspan" => 2));
foreach($templates as $template)
{
$template['pretty_title'] = htmlspecialchars_uni($template['title']);
$popup = new PopupMenu("template_{$template['tid']}", $lang->options);
if($sid == -2)
{
foreach($template_sets as $set_sid => $title)
{
if($set_sid < 0) continue;
$popup->add_item($lang->edit_in." ".htmlspecialchars_uni($title), "index.php?module=style-templates&action=edit_template&title=".urlencode($template['title'])."&sid={$set_sid}");
}
}
else
{
$popup->add_item($lang->full_edit, "index.php?module=style-templates&action=edit_template&title=".urlencode($template['title'])."&sid={$sid}");
}
if(isset($template['modified']) && $template['modified'] == true)
{
if($sid > 0)
{
$popup->add_item($lang->diff_report, "index.php?module=style-templates&action=diff_report&title=".urlencode($template['title'])."&sid2={$sid}");
$popup->add_item($lang->revert_to_orig, "index.php?module=style-templates&action=revert&title=".urlencode($template['title'])."&sid={$sid}&my_post_key={$mybb->post_code}", "return AdminCP.deleteConfirmation(this, '{$lang->confirm_template_revertion}')");
}
$template['pretty_title'] = "".htmlspecialchars_uni($template['title'])."";
}
// This template does not exist in the master list
else if(!isset($template['original']) || $template['original'] == false)
{
$popup->add_item($lang->delete_template, "index.php?module=style-templates&action=delete_template&title=".urlencode($template['title'])."&sid={$sid}&my_post_key={$mybb->post_code}", "return AdminCP.deleteConfirmation(this, '{$lang->confirm_template_deletion}')");
$template['pretty_title'] = "".htmlspecialchars_uni($template['title'])."";
}
$table->construct_cell("{$template['pretty_title']}", array("width" => "85%"));
$table->construct_cell($popup->fetch(), array("class" => "align_center"));
$table->construct_row();
}
if($count == 1)
{
$table->output($lang->sprintf($lang->search_names_header, htmlspecialchars_uni($mybb->input['title'])));
}
else if($count > 0)
{
$table->output();
}
}
$page->output_footer();
exit;
}
}
}
if($admin_options['codepress'] != 0)
{
$page->extra_header .= '
';
}
$page->add_breadcrumb_item($lang->search_replace);
$page->output_header($lang->search_replace);
$page->output_nav_tabs($sub_tabs, 'search_replace');
$form = new Form("index.php?module=style-templates&action=search_replace", "post", "do_template");
echo $form->generate_hidden_field('type', "templates");
$form_container = new FormContainer($lang->search_replace, 'tfixed');
$form_container->output_row($lang->search_for, "", $form->generate_text_area('find', $mybb->get_input('find'), array('id' => 'find', 'class' => '', 'style' => 'width: 100%; height: 200px;')));
$form_container->output_row($lang->replace_with, "", $form->generate_text_area('replace', $mybb->get_input('replace'), array('id' => 'replace', 'class' => '', 'style' => 'width: 100%; height: 200px;')));
$form_container->end();
$buttons[] = $form->generate_submit_button($lang->find_and_replace);
$form->output_submit_wrapper($buttons);
$form->end();
echo "
";
$form = new Form("index.php?module=style-templates&action=search_replace", "post", "do_title");
echo $form->generate_hidden_field('type', "titles");
$form_container = new FormContainer($lang->search_template_names);
$form_container->output_row($lang->search_for, "", $form->generate_text_box('title', $mybb->get_input('title'), array('id' => 'title')), 'title');
$form_container->end();
$buttons = array();
$buttons[] = $form->generate_submit_button($lang->find_templates);
$buttons[] = $form->generate_reset_button($lang->reset);
$form->output_submit_wrapper($buttons);
$form->end();
if($admin_options['codepress'] != 0)
{
echo '';
}
$page->output_footer();
}
if($mybb->input['action'] == "find_updated")
{
// Finds templates that are old and have been updated by MyBB
$compare_version = $mybb->version_code;
$query = $db->query("
SELECT COUNT(*) AS updated_count
FROM ".TABLE_PREFIX."templates t
LEFT JOIN ".TABLE_PREFIX."templates m ON (m.title=t.title AND m.sid=-2 AND m.version > t.version)
WHERE t.sid > 0 AND m.template != t.template
");
$count = $db->fetch_array($query);
if($count['updated_count'] < 1)
{
flash_message($lang->no_updated_templates, 'success');
admin_redirect("index.php?module=style-templates");
}
$plugins->run_hooks("admin_style_templates_find_updated");
$page->add_breadcrumb_item($lang->find_updated, "index.php?module=style-templates&action=find_updated");
$page->output_header($lang->find_updated);
$page->output_nav_tabs($sub_tabs, 'find_updated');
$query = $db->simple_select("templatesets", "*", "", array('order_by' => 'title'));
while($templateset = $db->fetch_array($query))
{
$templatesets[$templateset['sid']] = $templateset;
}
echo <<