2019-05-10 23:35:17 +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 /. */
2019-05-10 23:35:17 +00:00
register_module ([
" name " => " First run wizard " ,
2021-08-06 00:50:08 +00:00
" version " => " 0.2 " ,
2019-05-10 23:35:17 +00:00
" author " => " Starbeamrainbowlabs " ,
" description " => " Displays a special page to aid in setting up a new wiki for the first time. " ,
" id " => " feature-firstrun " ,
" code " => function () {
2019-05-11 21:52:55 +00:00
global $settings , $env ;
2019-05-10 23:35:17 +00:00
2019-05-11 14:39:55 +00:00
// NOTE: We auto-detect pre-existing wikis in 01-settings.fragment.php
2019-05-11 21:52:55 +00:00
if ( ! $settings -> firstrun_complete && preg_match ( " /^firstrun/ " , $env -> action ) !== 1 ) {
http_response_code ( 307 );
header ( " location: ?action=firstrun " );
exit ( " Redirecting you to the first-run wizard.... " );
}
2019-05-11 11:45:02 +00:00
2019-05-10 23:35:17 +00:00
/**
* @ api { get } ? action = firstrun Display the firstrun page
* @ apiName FirstRun
* @ apiGroup Settings
* @ apiPermission Anonymous
*
*/
/*
* ███████ ██ ██████ ███████ ████████ ██████ ██ ██ ███ ██
* ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██
* █████ ██ ██████ ███████ ██ ██████ ██ ██ ██ ██ ██
* ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
* ██ ██ ██ ██ ███████ ██ ██ ██ ██████ ██ ████
*/
2019-05-11 11:45:02 +00:00
add_action ( " firstrun " , function () {
2019-11-24 11:50:49 +00:00
global $settings , $settingsFilename , $version ;
2019-05-10 23:35:17 +00:00
2019-05-11 11:45:02 +00:00
if ( $settings -> firstrun_complete ) {
2019-05-10 23:35:17 +00:00
http_response_code ( 400 );
exit ( page_renderer :: render_main ( " Setup complete - Error - $settings->sitename " , " <p>Oops! Looks like $settings->sitename is already setup and ready to go! Go to the <a href='?action= $settings->defaultaction &page= " . rawurlencode ( $settings -> defaultpage ) . " '> " . htmlentities ( $settings -> defaultpage ) . " </a> to get started!</p> " ));
}
2019-05-11 11:45:02 +00:00
if ( ! module_exists ( " page-login " )) {
http_response_code ( 503 );
exit ( page_renderer :: render_main ( " Build error - Pepperminty Wiki " , " <p>The <code>page-login</code> wasn't included in this build of Pepperminty Wiki, so the first-run installation wizard will not work correctly.</p>
< p > You can still complete the setup manually , however ! Once done , set < code > firstrun_complete </ code > in peppermint . json to < code > true </ code >.</ p > " ));
}
2019-05-11 21:52:55 +00:00
if ( ! $settings -> disable_peppermint_access_check &&
php_sapi_name () !== " cli-server " ) { // The CLI server is single threaded, so it can't support loopback requests
$request_url = full_url ();
2020-01-10 17:07:09 +00:00
$request_url = preg_replace ( " / \ /(index.php)? \ ?.* $ / " , " / $settingsFilename " , $request_url );
2019-09-18 10:28:47 +00:00
@ file_get_contents ( $request_url );
// $http_response_header is a global reserved variable. More information: https://devdocs.io/php/reserved.variables.httpresponseheader
2019-05-11 21:52:55 +00:00
$response_code = intval ( explode ( " " , $http_response_header [ 0 ])[ 1 ]);
2020-01-09 01:25:46 +00:00
if ( $response_code >= 200 && $response_code < 300 ) {
2019-05-11 21:52:55 +00:00
file_put_contents ( " $settingsFilename .compromised " , " compromised " );
http_response_code ( 307 );
header ( " location: index.php " );
exit ();
}
}
else {
2020-07-28 18:42:41 +00:00
error_log ( " [PeppermintyWiki/firstrun] Warning: The public peppermint.json access check has been disabled (either manually or because you're using a local PHP development server with php -S ....). It's strongly recommended you ensure that access from outside is blocked to peppermint.json to avoid (many) security issues and other nastiness such as stealing of site secrets and password hashes. " );
2019-05-11 12:06:19 +00:00
}
2019-05-11 11:54:09 +00:00
2019-05-10 23:35:17 +00:00
// TODO: Check the environment here first
// - Check for required modules?
// TODO: Add a button to skip the firstrun wizard & do your own manual setup
2019-08-26 14:27:24 +00:00
// TODO: Add option to configure theme auto-update here - make sure it doesn't do anything until configuration it complete!
2021-08-06 00:50:08 +00:00
$system_checks = implode ( " \n \t \t \t " , array_map ( function ( $check ) {
$result = " <li> " ;
if ( ! $check [ 0 ]) $result .= " <strong> " ;
$result .= $check [ 0 ] ? " ✅ " : ( $check [ 2 ] === " optional " ? " ⚠️ " : " ❌ " );
$result .= " { $check [ 1 ] } " ;
if ( ! $check [ 0 ]) $result .= " </strong> " ;
return $result ;
}, do_system_checks ()));
2019-05-10 23:35:17 +00:00
$result = " <h1>Welcome!</h1>
< p > Welcome to Pepperminty Wiki .</ p >
< p > Fill out the below form to get your wiki up and running !</ p >
2019-11-24 11:50:49 +00:00
< p > Optionally , < a target = '_blank' href = 'https://starbeamrainbowlabs.com/blog/viewtracker.php?action=record&post-id=pepperminty-wiki/$version&format=text' > click this link </ a > to say hi and let Starbeamrainbowlabs know that you ' re setting up a new Pepperminty Wiki $version instance .</ p >
2019-05-10 23:35:17 +00:00
< form method = 'post' action = '?action=firstrun-complete' >
2021-08-06 00:50:08 +00:00
< fieldset >
< legend > System requirements </ legend >
< ul >
$system_checks
</ ul >
</ fieldset >
2019-05-11 11:51:22 +00:00
< fieldset >
< legend > Authorisation </ legend >
< p >< em > Find your wiki secret in the < code > secret </ code > property inside < code > peppermint . json </ code >. Don ' t forget to avoid copying the quotes surrounding the value !</ em ></ p >
< label for = 'secret' > Wiki Secret :</ label >
< input type = 'password' id = 'secret' name = 'secret' />
</ fieldset >
2019-05-10 23:35:17 +00:00
< fieldset >
< legend > Admin account details </ legend >
< label for = 'username' > Username :</ label >
< input type = 'text' id = 'username' name = 'username' placeholder = 'e.g. bob, admin' required />
< br />
2019-05-11 11:45:02 +00:00
< label for = 'username' > Email address :</ label >
< input type = 'text' id = 'email-address' name = 'email-address' required />
< br />
2019-05-10 23:35:17 +00:00
< p >< em > Longer is better ! Aim for at least 14 characters .</ em ></ p >
< label for = 'username' > Password :</ label >
2019-05-11 21:52:55 +00:00
< input type = 'password' id = 'password' name = 'password' required />
2019-05-10 23:35:17 +00:00
< br />
< label for = 'username' > Repeat Password :</ label >
2019-05-11 21:52:55 +00:00
< input type = 'password' id = 'password-again' name = 'password-again' required />
2019-05-10 23:35:17 +00:00
</ fieldset >
< fieldset >
2021-08-06 00:50:08 +00:00
< legend > Wiki details </ legend >
2019-05-10 23:35:17 +00:00
< label for = 'wiki-name' > Wiki Name :</ label >
< input type = 'text' id = 'wiki-name' name = 'wiki-name' placeholder = \ " e.g. Bob's Rockets Compendium \" required />
<!-- FUTURE : Have a logo url box here ? -->
< p >< em > The location on the server ' s disk to store the wiki data . Relative paths are ok - the default is < code >.</ code > ( i . e . the current directory ) .</ em ></ p >
< label for = 'data-dir' > Data Storage Directory :</ label >
< input type = 'text' id = 'data-dir' name = 'data-dir' value = '.' required />
</ fieldset >
< input type = 'submit' value = 'Create Wiki!' />
</ form > " ;
2019-05-11 11:45:02 +00:00
exit ( page_renderer :: render_main ( " Welcome! - Pepperminty Wiki " , $result ));
2019-05-10 23:35:17 +00:00
});
2019-05-11 14:39:55 +00:00
/**
* @ api { post } ? action = firstrun - complete Complete the first - run wizard .
* @ apiName FirstRunComplete
* @ apiGroup Settings
* @ apiPermission Anonymous
*
* @ apiParam { string } username The username for the first admin account
* @ apiParam { string } password The password for the first admin account
* @ apiParam { string } password - again The password repeated for the first admin account
* @ apiParam { string } email - address The email address for the first admin account
* @ apiParam { string } wiki - name The name of the wiki . Saved to $settings -> sitename
* @ apiParam { string } data - dir The directory on the server to save the wiki data to . Saved to $settings -> data_storage_dir .
*/
2019-05-10 23:35:17 +00:00
add_action ( " firstrun-complete " , function () {
2019-05-11 11:45:02 +00:00
global $version , $commit , $settings ;
2019-05-10 23:35:17 +00:00
2019-05-11 11:45:02 +00:00
if ( $settings -> firstrun_complete ) {
2019-05-10 23:35:17 +00:00
http_response_code ( 400 );
exit ( page_renderer :: render_main ( " Setup complete - Error - $settings->sitename " , " <p>Oops! Looks like $settings->sitename is already setup and ready to go! Go to the <a href='?action= $settings->defaultaction &page= " . rawurlencode ( $settings -> defaultpage ) . " '> " . htmlentities ( $settings -> defaultpage ) . " </a> to get started!</p> " ));
}
2019-05-11 11:51:22 +00:00
if ( $_POST [ " secret " ] !== $settings -> secret ) {
http_response_code ( 401 );
exit ( page_renderer :: render_main ( " Incorrect secret - Pepperminty Wiki " , " <p>Oops! That secret was incorrect. Open <code>peppermint.json</code> that is automatically written to the directory alongside the <code>index.php</code> that you uploaded to your web server and copy the value of the <code>secret</code> property into the wiki secret box on the previous page, taking care to avoid copying the quotation marks.</p> " ));
}
2019-05-11 11:45:02 +00:00
// $_POST: username, email-address, password, password-again, wiki-name, data-dir
2019-05-10 23:35:17 +00:00
if ( empty ( $_POST [ " username " ])) {
2019-05-11 11:45:02 +00:00
http_response_code ( 400 );
2019-05-10 23:35:17 +00:00
exit ( page_renderer :: render_main ( " Missing information - Error - Pepperminty Wiki " , " <p>Oops! Looks like you forgot to enter a username. Try going back in your browser and filling one in.</p> " ));
}
2019-05-11 11:45:02 +00:00
if ( empty ( $_POST [ " email-address " ])) {
http_response_code ( 400 );
exit ( page_renderer :: render_main ( " Missing information - Error - Pepperminty Wiki " , " <p>Oops! Looks like you forgot to enter an email address. Try going back in your browser and filling one in.</p> " ));
}
2019-05-11 11:54:09 +00:00
if ( filter_var ( $_POST [ " email-address " ], FILTER_VALIDATE_EMAIL ) === false ) {
2019-05-11 11:45:02 +00:00
http_response_code ( 400 );
exit ( page_renderer :: render_main ( " Invalid email address - Error - Pepperminty Wiki " , " <p>Oops! Looks like that email address isn't valid. Try going back in your browser and correcting it.</p> " ));
}
2019-05-10 23:35:17 +00:00
if ( empty ( $_POST [ " password " ]) || empty ( $_POST [ " password-again " ])) {
2019-05-11 11:45:02 +00:00
http_response_code ( 400 );
2019-05-10 23:35:17 +00:00
exit ( page_renderer :: render_main ( " Missing information - Error - Pepperminty Wiki " , " <p>Oops! Looks like you forgot to enter a password. Try going back in your browser and filling one in.</p> " ));
}
2019-05-11 11:45:02 +00:00
if ( $_POST [ " password " ] !== $_POST [ " password-again " ]) {
2019-05-10 23:35:17 +00:00
http_response_code ( 422 );
2019-05-11 11:45:02 +00:00
exit ( page_renderer :: render_main ( " Password mismatch - Error - Pepperminty Wiki " , " <p>Oops! Looks like the passwords you entered aren't the same. Try going back in your browser and entering it again.</p> " ));
}
if ( empty ( $_POST [ " wiki-name " ])) {
http_response_code ( 400 );
2019-05-10 23:35:17 +00:00
exit ( page_renderer :: render_main ( " Missing information - Error - Pepperminty Wiki " , " <p>Oops! Looks like you forgot to enter a name for your wiki. Try going back in your browser and filling one in.</p> " ));
}
if ( empty ( $_POST [ " data-dir " ])) {
2019-05-11 11:45:02 +00:00
http_response_code ( 400 );
2019-05-10 23:35:17 +00:00
exit ( page_renderer :: render_main ( " Missing information - Error - Pepperminty Wiki " , " <p>Oops! Looks like you forgot to enter a directory on the server to store the wiki's data in. Try going back in your browser and filling one in. Relative paths are ok - the default is <code>.</code> (i.e. the current directory).</p> " ));
}
2019-05-11 11:45:02 +00:00
// Generate the user data object & replace the pre-generated users
$user_data = new stdClass ();
$user_data -> password = hash_password ( $_POST [ " password " ]);
$user_data -> emailAddress = $_POST [ " email-address " ];
$settings -> users = new stdClass ();
$settings -> users -> { $_POST [ " username " ]} = $user_data ;
2019-05-11 14:39:55 +00:00
$settings -> admins = [ $_POST [ " username " ] ]; // Don't forget to mark them as a mod
2019-05-10 23:35:17 +00:00
2019-05-11 11:45:02 +00:00
// Apply the settings
$settings -> firstrun_complete = true ;
$settings -> sitename = $_POST [ " wiki-name " ];
$settings -> data_storage_dir = $_POST [ " data-dir " ];
2019-05-10 23:35:17 +00:00
2019-05-11 11:45:02 +00:00
if ( ! save_settings ()) {
http_response_code ( 500 );
exit ( page_renderer :: render_main ( " Server Error - Pepperminty Wiki " , " <p>Oops! Pepperminty Wiki was unable to save your settings back to disk. This can happen if Pepperminty Wiki does not have write permissions on it's own directory and the files contained within (except <code>index.php</code> of course).</p>
< p > Try contacting your server owner and ask them to correct it . If you are the server owner , you may need to run < code > sudo chown - R WEBSERVER_USERNAME : WEBSERVER_USERNAME PATH / TO / WIKI / DIRECTORY </ code > , replacing the bits in UPPERCASE .</ p > " ));
}
2019-05-10 23:35:17 +00:00
2019-05-11 11:45:02 +00:00
http_response_code ( 201 );
exit ( page_renderer :: render_main ( " Setup complete! - Pepperminty Wiki " , " <p>Congratulations! You've completed the Pepperminty Wiki setup.</p>
< p >< a href = '?action=$settings->defaultaction' > Click here </ a > to start using $settings -> sitename , your new wiki !</ p > " ));
2019-05-10 23:35:17 +00:00
});
}
]);
2021-08-06 00:50:08 +00:00
function do_system_checks () {
$checks = [
function_exists ( " mb_strpos " )
? [ true , " php-mbstring is installed " ]
: [ false , " php-mbstring is not installed " ],
class_exists ( " Transliterator " ) && class_exists ( " Collator " )
? [ true , " php-intl is installed for item sorting, search indexing, and sending non-utf8 emails " ]
: [ false , " php-intl is not installed (needed for item sorting, search indexing, and sending non-utf8 emails) " ]
];
if ( module_exists ( " feature-upload " )) {
$checks [] = class_exists ( " Imagick " )
? [ true , " php-imagick is installed for preview generation " ]
: [ false , " php-imagick is not installed (needed for image preview generation) " , " optional " ];
$checks [] = function_exists ( " finfo_file " )
? [ true , " php-fileinfo is installed for upload file type checking " ]
: [ false , " php-fileinfo is not installed (needed for file type checking on uploads) " , " optional " ];
}
if ( module_exists ( " page-export " ))
$checks [] = class_exists ( " ZipArchive " )
? [ true , " php-zip is installed for compressing exports " ]
: [ false , " php-zip is not install (needed for compressing exports) " , " optional " ];
if ( module_exists ( " lib-search-engine " ) or module_exists ( " feature-search-didyoumean " ))
$checks [] = extension_loaded ( " sqlite3 " )
? [ true , " php-sqlite3 is installed for search indexing " ]
: [ false , " php-sqlite3 is not installed (needed for search indexing) " , " optional " ];
return $checks ;
}