2022-03-19 00:00:51 +01:00
< ? php
namespace brain\utility ;
use function _\find ;
class SetUp
{
public static function status ()
{
if ( file_exists ( " ../config/settings.json " )) {
return true ;
} else {
return false ;
}
}
public static function init ( $body )
{
2022-03-19 22:55:56 +01:00
//grab template files
2022-03-19 00:00:51 +01:00
$newFolks = json_decode (
file_get_contents ( " ../config/init/folks-template.json " ),
true
);
$newSettings = json_decode (
file_get_contents ( " ../config/init/settings-template.json " ),
true
);
2022-03-19 22:55:56 +01:00
//get form values
//$body = $request->getParsedBody();
2022-03-19 00:00:51 +01:00
$handle = $body [ " new_member_handle " ];
$email = $body [ " new_member_email " ];
$pass = $body [ " new_member_pass " ];
$title = $body [ " new_member_title " ];
$now = new \Moment\Moment ();
2022-03-19 22:55:56 +01:00
//setup folks config
2022-03-19 00:00:51 +01:00
$hash = password_hash ( $pass , PASSWORD_DEFAULT );
$newFolks [ 0 ][ " id " ] = 0 ;
$newFolks [ 0 ][ " handle " ] = $handle ;
$newFolks [ 0 ][ " email " ] = $email ;
$newFolks [ 0 ][ " password " ] = $hash ;
$newFolks [ 0 ][ " key " ] = password_hash ( $email , PASSWORD_DEFAULT );
$newFolks [ 0 ][ " secret " ] = StringTools :: randomString ( 12 );
$newFolks [ 0 ][ " role " ] = " hnic " ;
$newFolks [ 0 ][ " created " ] = $now -> format ( " Y-m-d \T H:i:sP " );
$newFolks [ 0 ][ " updated " ] = $now -> format ( " Y-m-d \T H:i:sP " );
2022-03-19 22:55:56 +01:00
//set up settings config
2022-03-19 00:00:51 +01:00
$newSettings [ " global " ][ " title " ] = $title ;
2022-03-19 22:55:56 +01:00
//create index file
//$rightNow = $now->format("Y-m-d\TH:i:sP");
//var_dump($now->format("Y-m-d\TH:i:sP"));
2022-03-19 00:00:51 +01:00
$index = [
" id " => 1 ,
" uuid " => StringTools :: createUUID (),
" title " => " FIRST! " ,
" feature " => " /assets/images/global/default-bg.jpg " ,
2022-03-19 22:55:56 +01:00
" files " => " " ,
2022-03-19 00:00:51 +01:00
" path " => " content/pages/start " ,
" layout " => " index " ,
" tags " => " start, welcome " ,
" author " => $handle ,
" created " => $now -> format ( " Y-m-d \T H:i:sP " ),
" updated " => $now -> format ( " Y-m-d \T H:i:sP " ),
" deleted " => " false " ,
" slug " => " first " ,
" menu " => " false " ,
" featured " => " false " ,
" published " => " true " ,
" content " =>
" # F**k Yes \n \n If you're seeing this, you're up and running. NICE WORK! \n \n From here, feel free to start dropping pages to your heart's content. \n \n For some tips about using Fipamo, check out the ![docs](https://code.playvicio.us/Are0h/Fipamo/wiki/02-Usage) \n \n All good? Feel free to edit this page to whatever you want! \n \n YOU'RE THE CAPTAIN NOW. " ,
];
$freshIndex = DocTools :: objectToMD ( $index );
2022-03-19 22:55:56 +01:00
//once all files created, write down
2022-03-19 00:00:51 +01:00
DocTools :: writeSettings ( " ../config/settings.json " , $newSettings );
DocTools :: writeSettings ( " ../config/folks.json " , $newFolks );
DocTools :: writeSettings ( " ../config/tags.json " , []);
DocTools :: writePages (
" create " ,
" start " ,
" ../content/pages/start/index.md " ,
$freshIndex
);
2022-03-19 22:55:56 +01:00
//if there is an older session file, get rid of it
2022-03-19 00:00:51 +01:00
if ( is_file ( " ../content/.session " )) {
unlink ( " ../content/.session " );
}
$result = [ " type " => " blogInitGood " , " message " => " Site Created " ];
return $result ;
}
public static function restore ( $request )
{
$result = [
" type " => " requestLame " ,
" message " => " Still working on it. " ,
];
$body = $request -> getParsedBody ();
$backup = $request -> getUploadedFiles ();
$file = $backup [ " backup-upload " ];
$size = $file -> getSize ();
$name = $file -> getClientFileName ();
2022-03-19 22:55:56 +01:00
//park it so it can be read
2022-03-19 00:00:51 +01:00
$file -> moveTo ( " ../content " . " / " . $name );
2022-03-19 22:55:56 +01:00
//open it and get files to verify user
2022-03-19 00:00:51 +01:00
$zip = new ZipArchive ();
if ( $zip -> open ( " ../content " . " / " . $name ) === true ) {
$folks = json_decode ( $zip -> getFromName ( " settings/folks.json " ), true );
$found = find ( $folks , [ " handle " => $body [ " restore_member_handle " ]]);
2022-03-19 22:55:56 +01:00
//if member is found in back up, check pass
2022-03-19 00:00:51 +01:00
if ( $found ) {
if ( password_verify ( $body [ " restore_member_pass " ], $found [ " password " ])) {
//backup verified, restore site
//set new secret key for older folks configs
$newFolks = [];
if ( ! isset ( $found [ " secret " ])) {
$found [ " secret " ] = StringTools :: randomString ( 12 );
}
array_push ( $newFolks , $found );
//dump files in folder
$zip -> extractTo ( " ../content " );
//move to appropriate spots
/*
rename (
" ../content/settings/settings.json " ,
" ../config/settings.json "
);
*/
//load up old config file
$newConfig = json_decode (
file_get_contents ( " ../content/settings/settings.json " ),
true
);
2022-03-19 22:55:56 +01:00
//check for key, add if not there
2022-03-19 00:00:51 +01:00
if ( ! isset ( $newConfig [ " global " ][ " externalAPI " ])) {
$newConfig [ " global " ][ " externalAPI " ] = " false " ;
}
2022-03-19 22:55:56 +01:00
//write new config file
DocTools :: writeSettings ( " ../config/settings.json " , $newConfig );
2022-03-19 00:00:51 +01:00
2022-03-19 22:55:56 +01:00
//rename("../content/settings/folks.json", "../config/folks.json");
DocTools :: writeSettings ( " ../config/folks.json " , $newFolks );
2022-03-19 00:00:51 +01:00
2022-03-19 22:55:56 +01:00
rename ( " ../content/settings/tags.json " , " ../config/tags.json " );
2022-03-19 00:00:51 +01:00
2022-03-19 22:55:56 +01:00
//images path for blog and user
$blogImagePath = " ../public/assets/images/blog " ;
$userImagePath = " ../public/assets/images/user " ;
2022-03-19 00:00:51 +01:00
2022-03-19 22:55:56 +01:00
//check to see if image dirs are empty, if not chill
2022-03-19 00:00:51 +01:00
if ( $globs = glob ( $blogImagePath . " /* " )) {
2022-03-19 22:55:56 +01:00
//directory not empty, relax
2022-03-19 00:00:51 +01:00
} else {
rename ( " ../content/public/assets/images/blog " , $blogImagePath );
}
if ( $globs = glob ( $userImagePath . " /* " )) {
//directory not empty, relax
} else {
rename ( " ../content/public/assets/images/user " , $userImagePath );
}
rename ( " ../content/content/pages/ " , " ../content/pages " );
2022-03-19 22:55:56 +01:00
//legacy check for old file structure
2022-03-19 00:00:51 +01:00
if ( is_file ( " ../content/pages/index.md " )) {
if ( ! is_dir ( " ../content/pages/start " )) {
2022-03-19 22:55:56 +01:00
//Directory does not exist, so lets create it.
2022-03-19 00:00:51 +01:00
mkdir ( " ../content/pages/start " , 0755 , true );
2022-03-19 22:55:56 +01:00
//move start page to appropriate spot
2022-03-19 00:00:51 +01:00
rename (
" ../content/pages/index.md " ,
" ../content/pages/start/index.md "
);
}
} else {
2022-03-19 22:55:56 +01:00
//chill
2022-03-19 00:00:51 +01:00
}
2022-03-19 22:55:56 +01:00
//clean up
2022-03-19 00:00:51 +01:00
DocTools :: deleteFolder ( " ../content/settings " );
DocTools :: deleteFolder ( " ../content/public " );
DocTools :: deleteFolder ( " ../content/content " );
$result = [
" type " => " requestGood " ,
" message " => " Site Restored! Redirecting " ,
];
} else {
$result = [
" type " => " requestLame " ,
" message " => " Check that password, champ. " ,
];
}
} else {
$result = [
" type " => " requestLame " ,
" message " => " No member found by that name, hoss " ,
];
}
$zip -> close ();
$zipPath = " ../content/ " . $name ;
2022-03-19 22:55:56 +01:00
//trash zip when done
2022-03-19 00:00:51 +01:00
unlink ( $zipPath );
} else {
$result = [
" type " => " requestLame " ,
" message " => " Could not open backup. RATS! " ,
];
}
return $result ;
}
}