2016-10-12 20:39:04 +00:00
< ? php
2020-09-23 22:22:39 +00:00
/* This Source Code Form is subject to the terms of the Mozilla Public
* License , v . 2.0 . If a copy of the MPL was not distributed with this
* file , You can obtain one at https :// mozilla . org / MPL / 2.0 /. */
2016-10-12 20:39:04 +00:00
register_module ([
" name " => " Settings GUI " ,
2024-09-06 01:19:21 +00:00
" version " => " 0.1.9 " ,
2016-10-12 20:39:04 +00:00
" author " => " Starbeamrainbowlabs " ,
" description " => " The module everyone has been waiting for! Adds a web based gui that lets mods change the wiki settings. " ,
" id " => " feature-guiconfig " ,
" code " => function () {
global $settings ;
/**
2016-12-11 18:52:53 +00:00
* @ api { get } ? action = configure Get a page to change the global wiki settings
2016-10-12 20:39:04 +00:00
* @ apiName ConfigureSettings
* @ apiGroup Utility
* @ apiPermission Moderator
*/
/*
* ██████ ██████ ███ ██ ███████ ██ ██████ ██ ██ ██████ ███████
* ██ ██ ██ ████ ██ ██ ██ ██ ██ ██ ██ ██ ██
* ██ ██ ██ ██ ██ ██ █████ ██ ██ ███ ██ ██ ██████ █████
* ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
* ██████ ██████ ██ ████ ██ ██ ██████ ██████ ██ ██ ███████
*/
add_action ( " configure " , function () {
2018-04-22 16:52:57 +00:00
global $settings , $env , $guiConfig , $version , $commit ;
2016-10-12 20:39:04 +00:00
2016-10-30 10:50:38 +00:00
if ( ! $env -> is_admin )
{
2016-12-11 18:52:53 +00:00
$errorMessage = " <p>You don't have permission to change $settings->sitename 's master settings.</p> \n " ;
2016-10-30 10:50:38 +00:00
if ( ! $env -> is_logged_in )
$errorMessage .= " <p>You could try <a href='?action=login&returnto=%3Faction%3Dconfigure'>logging in</a>.</p> " ;
else
2016-12-11 18:52:53 +00:00
$errorMessage .= " <p>You could try <a href='?action=logout&returnto=%3Faction%3Dconfigure'>logging out</a> and then <a href='?action=login&returnto=%3Faction%3Dconfigure'>logging in</a> again with a different account that has the appropriate privileges.</a>.</p> " ;
2016-10-30 10:50:38 +00:00
exit ( page_renderer :: render_main ( " Error - $settings->sitename " , $errorMessage ));
}
2016-12-11 18:52:53 +00:00
$content = " <h1>Master Control Panel</h1> \n " ;
2016-12-11 19:28:03 +00:00
$content .= " <p>This page lets you configure $settings->sitename 's master settings. Please be careful - you can break things easily on this page if you're not careful!</p> \n " ;
2019-05-11 11:44:28 +00:00
if ( module_exists ( " feature-user-table " ))
2020-05-09 14:43:12 +00:00
$content .= " <p><em>Looking to manage $settings->sitename 's users? Try the <a href='?action=user-table'>user table</a> instead!</em></p> \n " ;
2019-09-29 14:42:48 +00:00
if ( module_exists ( " feature-theme-gallery " ))
2019-10-20 00:19:35 +00:00
$content .= " <p><em>Want to change $settings->sitename 's theme? Try the <a href='?action=theme-gallery'>theme gallery</a>!</em></p> " ;
2018-05-15 16:59:36 +00:00
$content .= " <p>You're currently running Pepperminty Wiki $version + " . substr ( $commit , 0 , 7 ) . " .</p> \n " ;
2017-07-11 19:21:20 +00:00
$content .= " <h2>Actions</h2> " ;
2018-04-07 12:51:35 +00:00
$content .= " <button class='action-invindex-rebuild' title='Rebuilds the index that is consulted when searching the wiki. Hit this button if some pages are not showing up.'>Rebuild Search Index</button> \n " ;
2017-07-11 19:21:20 +00:00
$content .= " <progress class='action-invindex-rebuild-progress' min='0' max='100' value='0' style='display: none;'></progress><br /> \n " ;
2018-04-07 12:47:39 +00:00
$content .= " <output class='action-invindex-rebuild-latestmessage'></output><br /> \n " ;
2017-07-11 19:21:20 +00:00
$invindex_rebuild_script = <<< SCRIPT
window . addEventListener ( " load " , function ( event ) {
document . querySelector ( " .action-invindex-rebuild " ) . addEventListener ( " click " , function ( event ) {
var rebuildActionEvents = new EventSource ( " ?action=invindex-rebuild " );
var latestMessageElement = document . querySelector ( " .action-invindex-rebuild-latestmessage " );
var progressElement = document . querySelector ( " .action-invindex-rebuild-progress " );
rebuildActionEvents . addEventListener ( " message " , function ( event ) {
console . log ( event );
let message = event . data ;
latestMessageElement . value = event . data ;
let parts = message . match ( /^ \ [ \s * ( \d + ) \s + \ / \s + ( \d + ) \s * \ ] / );
if ( parts != null ) {
progressElement . style . display = " " ;
progressElement . min = 0 ;
progressElement . max = parseInt ( parts [ 2 ]);
progressElement . value = parseInt ( parts [ 1 ]);
}
if ( message . startsWith ( " Done! Saving new search index to " ))
rebuildActionEvents . close ();
});
2019-08-22 20:38:17 +00:00
// Close the connection on error & don't try again
rebuildActionEvents . addEventListener ( " error " , ( _event ) => rebuildActionEvents . close ());
2017-07-11 19:21:20 +00:00
});
});
SCRIPT ;
2019-01-27 22:56:51 +00:00
page_renderer :: add_js_snippet ( $invindex_rebuild_script );
2017-07-11 19:21:20 +00:00
$content .= " <h2>Settings</h2> " ;
2017-03-20 19:57:50 +00:00
$content .= " <p>Mouse over the name of each setting to see a description of what it does.</p> \n " ;
2016-12-11 18:52:53 +00:00
$content .= " <form action='?action=configure-save' method='post'> \n " ;
2016-10-12 20:39:04 +00:00
foreach ( $guiConfig as $configKey => $configData )
{
2016-10-30 10:50:38 +00:00
// Don't display the site secret~!
// Apparently it got lost in translation, but I'll be re-adding
// it again at some point I'm sure - so support for it is
// included here.
if ( $configKey == " sitesecret " ) continue ;
2016-10-12 20:39:04 +00:00
$reverse = false ;
$inputControl = " " ;
2016-10-30 10:50:38 +00:00
$label = " <label for='setting- $configKey ' title= \" $configData->description\ " class = 'cursor-query' > $configKey </ label > " ;
2016-10-12 20:39:04 +00:00
switch ( $configData -> type )
{
2016-10-18 16:55:59 +00:00
case " url " :
2016-10-30 10:50:38 +00:00
case " email " :
2016-10-18 16:55:59 +00:00
case " number " :
2016-10-12 20:39:04 +00:00
case " text " :
2021-09-02 21:53:59 +00:00
$inputControl = " <input type=' $configData->type ' id=' $configKey ' name=' $configKey ' value=' " . htmlentities ( $settings -> $configKey ) . " ' /> " ;
2016-10-12 20:39:04 +00:00
break ;
case " textarea " :
2021-09-02 21:53:59 +00:00
$inputControl = " <textarea id=' $configKey ' name=' $configKey '> " . htmlentities ( $settings -> $configKey ) . " </textarea> " ;
2016-10-30 10:50:38 +00:00
break ;
2016-10-18 16:55:59 +00:00
case " checkbox " :
$reverse = true ;
2016-12-11 18:52:53 +00:00
$inputControl = " <input type='checkbox' id=' $configKey ' name=' $configKey ' " . ( $settings -> $configKey ? " checked " : " " ) . " /> " ;
2016-10-30 10:50:38 +00:00
break ;
2018-05-13 22:37:26 +00:00
case " usertable " :
$label = " " ;
if ( module_exists ( " feature-user-table " ))
$inputControl = " <p>The users can be managed in the <a href='?action=user-table'>User Table</a>.</p> " ;
else
$inputControl = " <p><em>The users can be managed in the user table, but the required module <code>feature-user-table</code> is not installed.</em></p> " ;
break ;
2016-10-18 16:55:59 +00:00
default :
$label = " " ;
2021-09-02 21:53:59 +00:00
$inputControl = " <p><em>Sorry! The <code> " . htmlentities ( $configKey ) . " </code> setting isn't editable yet through the gui. Please try editing <code>peppermint.json</code> for the time being.</em></p> " ;
2016-10-30 10:50:38 +00:00
break ;
2016-10-12 20:39:04 +00:00
}
2016-10-30 10:50:38 +00:00
$content .= " <div class='setting-configurator'> \n \t " ;
$content .= $reverse ? " $inputControl\n\t $label " : " $label\n\t $inputControl " ;
$content .= " \n </div> \n " ;
2016-10-12 20:39:04 +00:00
}
2016-12-11 18:52:53 +00:00
$content .= " <input type='submit' value='Save Settings' /> " ;
$content .= " </form> \n " ;
2016-10-30 10:50:38 +00:00
exit ( page_renderer :: render_main ( " Master Control Panel - $settings->sitename " , $content ));
2016-10-12 20:39:04 +00:00
});
2016-12-11 18:52:53 +00:00
/**
* @ api { post } ? action = configure - save Save changes to the global wiki settings
* @ apiName ConfigureSettings
* @ apiGroup Utility
* @ apiPermission Moderator
*/
/*
* ██████ ██████ ███ ██ ███████ ██ ██████ ██ ██ ██████ ███████
* ██ ██ ██ ████ ██ ██ ██ ██ ██ ██ ██ ██ ██
* ██ ██ ██ ██ ██ ██ █████ ██ ██ ███ ██ ██ ██████ █████ █████
* ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
* ██████ ██████ ██ ████ ██ ██ ██████ ██████ ██ ██ ███████
* ███████ █████ ██ ██ ███████
* ██ ██ ██ ██ ██ ██
* ███████ ███████ ██ ██ █████
* ██ ██ ██ ██ ██ ██
* ███████ ██ ██ ████ ███████
*/
add_action ( " configure-save " , function () {
2016-12-11 19:28:03 +00:00
global $env , $settings , $paths , $defaultCSS ;
2016-12-11 18:52:53 +00:00
// If the user isn't an admin, then the regular configuration page will display an appropriate error
if ( ! $env -> is_admin )
{
http_response_code ( 307 );
header ( " location: ?action=configure " );
exit ();
}
// Build a new settings object
$newSettings = new stdClass ();
foreach ( $settings as $configKey => $rawValue )
{
$configValue = $rawValue ;
if ( isset ( $_POST [ $configKey ]))
{
$decodedConfigValue = json_decode ( $_POST [ $configKey ]);
if ( json_last_error () === JSON_ERROR_NONE )
$configValue = $decodedConfigValue ;
else
$configValue = $_POST [ $configKey ];
2020-01-05 20:59:21 +00:00
// Convert bool settings to a bool, since POST
2016-12-11 18:52:53 +00:00
// parameters don't decode correctly.
if ( is_bool ( $settings -> $configKey ))
$configValue = in_array ( $ configValue , [ 1 , " on " ], true ) ? true : false ;
// If the CSS hasn't changed, then we can replace it with
// 'auto' - this will ensure that upon update the new
// default CSS will be used. Also make sure we ignore line
// ending nonsense & differences here, since they really
// don't matter
if ( $configKey === " css " && str_replace ( " \r \n " , " \n " , $defaultCSS ) === str_replace ( " \r \n " , " \n " , $configValue ))
$configValue = " auto " ;
}
$newSettings -> $configKey = $configValue ;
}
2016-12-11 19:28:03 +00:00
// Take a backup of the current settings file
rename ( $paths -> settings_file , " $paths->settings_file .bak " );
// Save the new settings file
file_put_contents ( $paths -> settings_file , json_encode ( $newSettings , JSON_PRETTY_PRINT ));
2024-09-06 01:10:22 +00:00
$content = " <h1>Master settings updated successfully</h1> \n " ;
2016-12-11 19:28:03 +00:00
$content .= " <p> $settings->sitename 's master settings file has been updated successfully. A backup of the original settings has been created under the name <code>peppermint.json.bak</code>, just in case. You can <a href='?action=configure'>go back</a> and continue editing the master settings file, or you can go to the <a href='?action=view&page= " . rawurlencode ( $settings -> defaultpage ) . " '> " . htmlentities ( $settings -> defaultpage ) . " </a>.</p> \n " ;
$content .= " <p>For reference, the newly generated master settings file is as follows:</p> \n " ;
$content .= " <textarea name='content'> " ;
$content .= json_encode ( $newSettings , JSON_PRETTY_PRINT );
$content .= " </textarea> \n " ;
exit ( page_renderer :: render_main ( " Master Settings Updated - $settings->sitename " , $content ));
2016-12-11 18:52:53 +00:00
});
2024-09-06 01:19:21 +00:00
add_help_section ( " 800-raw-page-content " , " Viewing Raw Page Content " , " <p>Although you can use the edit page to view a page's source, you can also ask $settings->sitename to send you the raw page source and nothing else. This feature is intended for those who want to automate their interaction with $settings->sitename .</p>
2016-10-12 20:39:04 +00:00
< p > To use this feature , navigate to the page for which you want to see the source , and then alter the < code > action </ code > parameter in the url 's query string to be <code>raw</code>. If the <code>action</code> parameter doesn' t exist , add it . Note that when used on an file ' s page this action will return the source of the description and not the file itself .</ p > " );
}
]);
?>