2007-04-16 11:58:25 +00:00
|
|
|
<?php
|
2010-01-05 10:11:53 +00:00
|
|
|
/*
|
|
|
|
* Name: Board Config
|
|
|
|
* Author: Shish
|
2010-01-05 13:13:11 +00:00
|
|
|
* Visibility: admin
|
2010-01-05 10:11:53 +00:00
|
|
|
* Description: Allows the site admin to configure the board to his or her taste
|
|
|
|
*/
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2009-01-03 21:00:09 +00:00
|
|
|
/* ConfigSaveEvent {{{
|
|
|
|
*
|
|
|
|
* Sent when the setup screen's 'set' button has been
|
|
|
|
* activated; new config options are in $_POST
|
|
|
|
*/
|
2019-05-28 16:59:38 +00:00
|
|
|
class ConfigSaveEvent extends Event
|
|
|
|
{
|
2019-05-28 19:27:23 +00:00
|
|
|
/** @var Config */
|
2019-05-28 16:59:38 +00:00
|
|
|
public $config;
|
|
|
|
|
|
|
|
public function __construct(Config $config)
|
|
|
|
{
|
|
|
|
$this->config = $config;
|
|
|
|
}
|
2009-01-03 21:00:09 +00:00
|
|
|
}
|
|
|
|
// }}}
|
2007-04-16 11:58:25 +00:00
|
|
|
/* SetupBuildingEvent {{{
|
|
|
|
*
|
|
|
|
* Sent when the setup page is ready to be added to
|
|
|
|
*/
|
2019-05-28 16:59:38 +00:00
|
|
|
class SetupBuildingEvent extends Event
|
|
|
|
{
|
2019-05-28 19:27:23 +00:00
|
|
|
/** @var SetupPanel */
|
2019-05-28 16:59:38 +00:00
|
|
|
public $panel;
|
|
|
|
|
|
|
|
public function __construct(SetupPanel $panel)
|
|
|
|
{
|
|
|
|
$this->panel = $panel;
|
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
// }}}
|
|
|
|
/* SetupPanel {{{
|
|
|
|
*
|
|
|
|
*/
|
2019-05-28 16:59:38 +00:00
|
|
|
class SetupPanel
|
|
|
|
{
|
2019-05-28 19:27:23 +00:00
|
|
|
/** @var SetupBlock[] */
|
2019-05-28 16:59:38 +00:00
|
|
|
public $blocks = [];
|
|
|
|
|
|
|
|
public function add_block(SetupBlock $block)
|
|
|
|
{
|
|
|
|
$this->blocks[] = $block;
|
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
// }}}
|
|
|
|
/* SetupBlock {{{
|
|
|
|
*
|
|
|
|
*/
|
2019-05-28 16:59:38 +00:00
|
|
|
class SetupBlock extends Block
|
|
|
|
{
|
|
|
|
/** @var string */
|
|
|
|
public $header;
|
|
|
|
/** @var string */
|
|
|
|
public $body;
|
|
|
|
|
2019-06-27 04:00:49 +00:00
|
|
|
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
public function __construct(string $title)
|
|
|
|
{
|
|
|
|
$this->header = $title;
|
|
|
|
$this->section = "main";
|
|
|
|
$this->position = 50;
|
|
|
|
$this->body = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
public function add_label(string $text)
|
|
|
|
{
|
|
|
|
$this->body .= $text;
|
|
|
|
}
|
|
|
|
|
2019-06-27 04:00:49 +00:00
|
|
|
public function start_table()
|
|
|
|
{
|
|
|
|
$this->body .= "<table class='form'>";
|
|
|
|
}
|
|
|
|
public function end_table()
|
|
|
|
{
|
|
|
|
$this->body .= "</table>";
|
|
|
|
}
|
|
|
|
public function start_table_row()
|
|
|
|
{
|
|
|
|
$this->body .= "</tr>";
|
|
|
|
}
|
|
|
|
public function end_table_row()
|
|
|
|
{
|
|
|
|
$this->body .= "</tr>";
|
|
|
|
}
|
|
|
|
public function start_table_head()
|
|
|
|
{
|
|
|
|
$this->body .= "<thead>";
|
|
|
|
}
|
|
|
|
public function end_table_head()
|
|
|
|
{
|
|
|
|
$this->body .= "</thead>";
|
|
|
|
}
|
|
|
|
public function add_table_header($content, int $colspan = 2)
|
|
|
|
{
|
|
|
|
$this->start_table_head();
|
|
|
|
$this->start_table_row();
|
|
|
|
$this->add_table_header_cell($content, $colspan);
|
|
|
|
$this->end_table_row();
|
|
|
|
$this->end_table_head();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function start_table_cell(int $colspan = 1)
|
|
|
|
{
|
|
|
|
$this->body .= "<td colspan='$colspan'>";
|
|
|
|
}
|
|
|
|
public function end_table_cell()
|
|
|
|
{
|
|
|
|
$this->body .= "</td>";
|
|
|
|
}
|
|
|
|
public function add_table_cell($content, int $colspan = 1)
|
|
|
|
{
|
|
|
|
$this->start_table_cell($colspan);
|
|
|
|
$this->body .= $content;
|
|
|
|
$this->end_table_cell();
|
|
|
|
}
|
|
|
|
public function start_table_header_cell(int $colspan = 1)
|
|
|
|
{
|
|
|
|
$this->body .= "<th colspan='$colspan'>";
|
|
|
|
}
|
|
|
|
public function end_table_header_cell()
|
|
|
|
{
|
|
|
|
$this->body .= "</th>";
|
|
|
|
}
|
|
|
|
public function add_table_header_cell($content, int $colspan = 1)
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2019-06-27 04:00:49 +00:00
|
|
|
$this->start_table_header_cell($colspan);
|
|
|
|
$this->body .= $content;
|
|
|
|
$this->end_table_header_cell();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private function format_option(string $name, $html, ?string $label, bool $table_row) {
|
2019-05-28 16:59:38 +00:00
|
|
|
global $config;
|
2019-06-27 04:00:49 +00:00
|
|
|
|
|
|
|
if($table_row) $this->start_table_row();
|
|
|
|
if($table_row) $this->start_table_header_cell();
|
2019-05-28 16:59:38 +00:00
|
|
|
if (!is_null($label)) {
|
|
|
|
$this->body .= "<label for='{$name}'>{$label}</label>";
|
|
|
|
}
|
2019-06-27 04:00:49 +00:00
|
|
|
if($table_row) $this->end_table_header_cell();
|
|
|
|
|
|
|
|
if($table_row) $this->start_table_cell();
|
|
|
|
$this->body .= $html;
|
|
|
|
if($table_row) $this->end_table_cell();
|
|
|
|
if($table_row) $this->end_table_row();
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2019-06-27 04:00:49 +00:00
|
|
|
public function add_text_option(string $name, string $label=null, bool $table_row = false)
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $config;
|
|
|
|
$val = html_escape($config->get_string($name));
|
2019-06-27 04:00:49 +00:00
|
|
|
|
|
|
|
$html = "<input type='text' id='{$name}' name='_config_{$name}' value='{$val}'>\n";
|
|
|
|
$html .= "<input type='hidden' name='_type_{$name}' value='string'>\n";
|
|
|
|
|
|
|
|
$this->format_option($name, $html, $label, $table_row);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function add_longtext_option(string $name, string $label=null, bool $table_row = false)
|
|
|
|
{
|
|
|
|
global $config;
|
|
|
|
$val = html_escape($config->get_string($name));
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
$rows = max(3, min(10, count(explode("\n", $val))));
|
2019-06-27 04:00:49 +00:00
|
|
|
$html = "<textarea rows='$rows' id='$name' name='_config_$name'>$val</textarea>\n";
|
|
|
|
$html .= "<input type='hidden' name='_type_$name' value='string'>\n";
|
|
|
|
|
|
|
|
$this->format_option($name, $html, $label, $table_row);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2019-06-27 04:00:49 +00:00
|
|
|
public function add_bool_option(string $name, string $label=null, bool $table_row = false)
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $config;
|
|
|
|
$checked = $config->get_bool($name) ? " checked" : "";
|
2019-06-27 04:00:49 +00:00
|
|
|
|
|
|
|
$html = "<input type='checkbox' id='$name' name='_config_$name'$checked>\n";
|
2019-05-28 16:59:38 +00:00
|
|
|
if (!is_null($label)) {
|
2019-06-27 04:00:49 +00:00
|
|
|
$html .= "<label for='{$name}'>{$label}</label>";
|
|
|
|
$label = null;
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2019-06-27 04:00:49 +00:00
|
|
|
|
|
|
|
$html .= "<input type='hidden' name='_type_$name' value='bool'>\n";
|
|
|
|
|
|
|
|
$this->format_option($name, $html, $label, $table_row);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// public function add_hidden_option($name, $label=null) {
|
|
|
|
// global $config;
|
|
|
|
// $val = $config->get_string($name);
|
|
|
|
// $this->body .= "<input type='hidden' id='$name' name='$name' value='$val'>";
|
|
|
|
// }
|
|
|
|
|
2019-06-27 04:00:49 +00:00
|
|
|
public function add_int_option(string $name, string $label=null, bool $table_row = false)
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $config;
|
|
|
|
$val = html_escape($config->get_string($name));
|
2019-06-27 04:00:49 +00:00
|
|
|
|
|
|
|
$html = "<input type='text' id='$name' name='_config_$name' value='$val' size='4' style='text-align: center;'>\n";
|
|
|
|
$html .= "<input type='hidden' name='_type_$name' value='int'>\n";
|
|
|
|
|
|
|
|
$this->format_option($name, $html, $label, $table_row);
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2019-06-27 04:00:49 +00:00
|
|
|
public function add_shorthand_int_option(string $name, string $label=null, bool $table_row = false)
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $config;
|
|
|
|
$val = to_shorthand_int($config->get_string($name));
|
2019-06-27 04:00:49 +00:00
|
|
|
$html = "<input type='text' id='$name' name='_config_$name' value='$val' size='6' style='text-align: center;'>\n";
|
|
|
|
$html .= "<input type='hidden' name='_type_$name' value='int'>\n";
|
|
|
|
|
|
|
|
$this->format_option($name, $html, $label, $table_row);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2019-06-27 04:00:49 +00:00
|
|
|
public function add_choice_option(string $name, array $options, string $label=null, bool $table_row = false)
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $config;
|
|
|
|
$current = $config->get_string($name);
|
|
|
|
|
|
|
|
$html = "<select id='$name' name='_config_$name'>";
|
|
|
|
foreach ($options as $optname => $optval) {
|
|
|
|
if ($optval == $current) {
|
|
|
|
$selected=" selected";
|
|
|
|
} else {
|
|
|
|
$selected="";
|
|
|
|
}
|
|
|
|
$html .= "<option value='$optval'$selected>$optname</option>\n";
|
|
|
|
}
|
|
|
|
$html .= "</select>";
|
2019-06-27 04:00:49 +00:00
|
|
|
$html .= "<input type='hidden' name='_type_$name' value='string'>\n";
|
2019-05-28 16:59:38 +00:00
|
|
|
|
2019-06-27 04:00:49 +00:00
|
|
|
$this->format_option($name, $html, $label, $table_row);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2019-06-27 04:00:49 +00:00
|
|
|
public function add_multichoice_option(string $name, array $options, string $label=null, bool $table_row = false)
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $config;
|
|
|
|
$current = $config->get_array($name);
|
|
|
|
|
|
|
|
$html = "<select id='$name' name='_config_{$name}[]' multiple size='5'>";
|
|
|
|
foreach ($options as $optname => $optval) {
|
|
|
|
if (in_array($optval, $current)) {
|
|
|
|
$selected=" selected";
|
|
|
|
} else {
|
|
|
|
$selected="";
|
|
|
|
}
|
|
|
|
$html .= "<option value='$optval'$selected>$optname</option>\n";
|
|
|
|
}
|
|
|
|
$html .= "</select>";
|
2019-06-27 04:00:49 +00:00
|
|
|
$html .= "<input type='hidden' name='_type_$name' value='array'>\n";
|
|
|
|
$html .= "<!--<br><br><br><br>-->\n"; // setup page auto-layout counts <br> tags
|
2019-05-28 16:59:38 +00:00
|
|
|
|
2019-06-27 04:00:49 +00:00
|
|
|
$this->format_option($name, $html, $label, $table_row);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
// }}}
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
class Setup extends Extension
|
|
|
|
{
|
|
|
|
public function onInitExt(InitExtEvent $event)
|
|
|
|
{
|
|
|
|
global $config;
|
|
|
|
$config->set_default_string("title", "Shimmie");
|
|
|
|
$config->set_default_string("front_page", "post/list");
|
|
|
|
$config->set_default_string("main_page", "post/list");
|
|
|
|
$config->set_default_string("theme", "default");
|
|
|
|
$config->set_default_bool("word_wrap", true);
|
|
|
|
$config->set_default_bool("comment_captcha", false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onPageRequest(PageRequestEvent $event)
|
|
|
|
{
|
|
|
|
global $config, $page, $user;
|
|
|
|
|
|
|
|
if ($event->page_matches("nicetest")) {
|
2019-06-19 01:58:28 +00:00
|
|
|
$page->set_mode(PageMode::DATA);
|
2019-05-28 16:59:38 +00:00
|
|
|
$page->set_data("ok");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($event->page_matches("setup")) {
|
|
|
|
if (!$user->can("change_setting")) {
|
|
|
|
$this->theme->display_permission_denied();
|
|
|
|
} else {
|
|
|
|
if ($event->get_arg(0) == "save" && $user->check_auth_token()) {
|
|
|
|
send_event(new ConfigSaveEvent($config));
|
|
|
|
$config->save();
|
|
|
|
flash_message("Config saved");
|
|
|
|
|
2019-06-19 01:58:28 +00:00
|
|
|
$page->set_mode(PageMode::REDIRECT);
|
2019-05-28 16:59:38 +00:00
|
|
|
$page->set_redirect(make_link("setup"));
|
|
|
|
} elseif ($event->get_arg(0) == "advanced") {
|
|
|
|
$this->theme->display_advanced($page, $config->values);
|
|
|
|
} else {
|
|
|
|
$panel = new SetupPanel();
|
|
|
|
send_event(new SetupBuildingEvent($panel));
|
|
|
|
$this->theme->display_page($page, $panel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onSetupBuilding(SetupBuildingEvent $event)
|
|
|
|
{
|
|
|
|
$themes = [];
|
|
|
|
foreach (glob("themes/*") as $theme_dirname) {
|
|
|
|
$name = str_replace("themes/", "", $theme_dirname);
|
|
|
|
$human = str_replace("_", " ", $name);
|
|
|
|
$human = ucwords($human);
|
|
|
|
$themes[$human] = $name;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($_SERVER["HTTP_HOST"])) {
|
|
|
|
$host = $_SERVER["HTTP_HOST"];
|
|
|
|
} else {
|
|
|
|
$host = $_SERVER["SERVER_NAME"];
|
|
|
|
if ($_SERVER["SERVER_PORT"] != "80") {
|
|
|
|
$host .= ":" . $_SERVER["SERVER_PORT"];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$full = "//" . $host . $_SERVER["PHP_SELF"];
|
|
|
|
$test_url = str_replace("/index.php", "/nicetest", $full);
|
|
|
|
|
|
|
|
$nicescript = "<script type='text/javascript'>
|
2009-05-11 21:09:24 +00:00
|
|
|
function getHTTPObject() {
|
|
|
|
if (window.XMLHttpRequest){
|
|
|
|
return new XMLHttpRequest();
|
|
|
|
}
|
|
|
|
else if(window.ActiveXObject){
|
|
|
|
return new ActiveXObject('Microsoft.XMLHTTP');
|
2008-12-27 10:17:53 +00:00
|
|
|
}
|
2009-05-11 21:09:24 +00:00
|
|
|
}
|
2008-12-27 10:17:53 +00:00
|
|
|
|
2009-05-11 21:09:24 +00:00
|
|
|
checkbox = document.getElementById('nice_urls');
|
|
|
|
out_span = document.getElementById('nicetest');
|
2008-12-27 10:17:53 +00:00
|
|
|
|
2009-05-11 21:09:24 +00:00
|
|
|
checkbox.disabled = true;
|
|
|
|
out_span.innerHTML = '(testing...)';
|
2008-12-27 10:17:53 +00:00
|
|
|
|
2010-01-03 09:51:06 +00:00
|
|
|
$(document).ready(function() {
|
2015-09-12 10:43:28 +00:00
|
|
|
var http_request = getHTTPObject();
|
2010-01-03 09:51:06 +00:00
|
|
|
http_request.open('GET', '$test_url', false);
|
|
|
|
http_request.send(null);
|
|
|
|
|
|
|
|
if(http_request.status == 200 && http_request.responseText == 'ok') {
|
|
|
|
checkbox.disabled = false;
|
|
|
|
out_span.innerHTML = '(tested ok)';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
checkbox.disabled = true;
|
|
|
|
out_span.innerHTML = '(test failed)';
|
|
|
|
}
|
|
|
|
});
|
2009-05-11 21:09:24 +00:00
|
|
|
</script>";
|
2019-05-28 16:59:38 +00:00
|
|
|
$sb = new SetupBlock("General");
|
|
|
|
$sb->position = 0;
|
|
|
|
$sb->add_text_option("title", "Site title: ");
|
|
|
|
$sb->add_text_option("front_page", "<br>Front page: ");
|
|
|
|
$sb->add_text_option("main_page", "<br>Main page: ");
|
|
|
|
$sb->add_text_option("contact_link", "<br>Contact URL: ");
|
|
|
|
$sb->add_choice_option("theme", $themes, "<br>Theme: ");
|
|
|
|
//$sb->add_multichoice_option("testarray", array("a" => "b", "c" => "d"), "<br>Test Array: ");
|
|
|
|
$sb->add_bool_option("nice_urls", "<br>Nice URLs: ");
|
|
|
|
$sb->add_label("<span id='nicetest'>(Javascript inactive, can't test!)</span>$nicescript");
|
|
|
|
$event->panel->add_block($sb);
|
|
|
|
|
|
|
|
$sb = new SetupBlock("Remote API Integration");
|
|
|
|
$sb->add_label("<a href='http://akismet.com/'>Akismet</a>");
|
|
|
|
$sb->add_text_option("comment_wordpress_key", "<br>API key: ");
|
|
|
|
$sb->add_label("<br> <br><a href='https://www.google.com/recaptcha/admin'>ReCAPTCHA</a>");
|
|
|
|
$sb->add_text_option("api_recaptcha_privkey", "<br>Secret key: ");
|
|
|
|
$sb->add_text_option("api_recaptcha_pubkey", "<br>Site key: ");
|
|
|
|
$event->panel->add_block($sb);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onConfigSave(ConfigSaveEvent $event)
|
|
|
|
{
|
|
|
|
global $config;
|
|
|
|
foreach ($_POST as $_name => $junk) {
|
|
|
|
if (substr($_name, 0, 6) == "_type_") {
|
|
|
|
$name = substr($_name, 6);
|
|
|
|
$type = $_POST["_type_$name"];
|
|
|
|
$value = isset($_POST["_config_$name"]) ? $_POST["_config_$name"] : null;
|
|
|
|
switch ($type) {
|
|
|
|
case "string": $config->set_string($name, $value); break;
|
|
|
|
case "int": $config->set_int($name, $value); break;
|
|
|
|
case "bool": $config->set_bool($name, $value); break;
|
|
|
|
case "array": $config->set_array($name, $value); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log_warning("setup", "Configuration updated");
|
|
|
|
foreach (glob("data/cache/*.css") as $css_cache) {
|
|
|
|
unlink($css_cache);
|
|
|
|
}
|
|
|
|
log_warning("setup", "Cache cleared");
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onUserBlockBuilding(UserBlockBuildingEvent $event)
|
|
|
|
{
|
|
|
|
global $user;
|
|
|
|
if ($user->can("change_setting")) {
|
|
|
|
$event->add_link("Board Config", make_link("setup"));
|
|
|
|
}
|
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|