2022-03-19 00:00:51 +01:00
< ? php
namespace brain\utility ;
use function _\find ;
class SetUp
{
public static function status ()
{
2022-05-17 02:41:15 +02:00
if ( file_exists ( '../config/settings.json' )) {
2022-03-19 00:00:51 +01:00
return true ;
} else {
return false ;
}
}
2022-05-17 04:14:38 +02:00
2022-03-19 00:00:51 +01:00
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 (
2022-05-17 02:41:15 +02:00
file_get_contents ( '../config/init/folks-template.json' ),
2022-03-19 00:00:51 +01:00
true
);
$newSettings = json_decode (
2022-05-17 02:41:15 +02:00
file_get_contents ( '../config/init/settings-template.json' ),
2022-03-19 00:00:51 +01:00
true
);
2022-03-19 22:55:56 +01:00
//get form values
//$body = $request->getParsedBody();
2022-05-17 02:41:15 +02:00
$handle = $body [ 'new_member_handle' ];
$email = $body [ 'new_member_email' ];
$pass = $body [ 'new_member_pass' ];
$title = $body [ 'new_member_title' ];
2022-03-19 00:00:51 +01:00
$now = new \Moment\Moment ();
2022-03-19 22:55:56 +01:00
//setup folks config
2022-05-17 02:41:15 +02: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-05-17 02:41:15 +02:00
$newSettings [ 'global' ][ 'title' ] = $title ;
2022-03-19 00:00:51 +01:00
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 = [
2022-05-17 02:41:15 +02:00
'id' => 1 ,
'uuid' => StringTools :: createUUID (),
'title' => 'FIRST!' ,
'feature' => '/assets/images/global/default-bg.jpg' ,
'files' => '' ,
'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. " ,
2022-03-19 00:00:51 +01:00
];
$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
2022-05-17 02:41:15 +02:00
DocTools :: writeSettings ( '../config/settings.json' , $newSettings );
DocTools :: writeSettings ( '../config/folks.json' , $newFolks );
DocTools :: writeSettings ( '../config/tags.json' , []);
2022-03-19 00:00:51 +01:00
DocTools :: writePages (
2022-05-17 02:41:15 +02:00
'create' ,
'start' ,
'../content/pages/start/index.md' ,
2022-03-19 00:00:51 +01:00
$freshIndex
);
2022-03-19 22:55:56 +01:00
//if there is an older session file, get rid of it
2022-05-17 02:41:15 +02:00
if ( is_file ( '../content/.session' )) {
unlink ( '../content/.session' );
2022-03-19 00:00:51 +01:00
}
2022-05-17 02:41:15 +02:00
$result = [ 'type' => 'blogInitGood' , 'message' => 'Site Created' ];
2022-03-19 00:00:51 +01:00
return $result ;
}
2022-05-17 04:14:38 +02:00
2022-03-19 00:00:51 +01:00
public static function restore ( $request )
{
$result = [
2022-05-17 02:41:15 +02:00
'type' => 'requestLame' ,
'message' => 'Still working on it.' ,
2022-03-19 00:00:51 +01:00
];
$body = $request -> getParsedBody ();
$backup = $request -> getUploadedFiles ();
2022-05-17 02:41:15 +02:00
$file = $backup [ 'backup-upload' ];
2022-03-20 00:15:56 +01:00
//NOTE: If this fails check 'post_max_size' in php.ini
2022-03-19 00:00:51 +01:00
$size = $file -> getSize ();
$name = $file -> getClientFileName ();
2022-03-19 22:55:56 +01:00
//park it so it can be read
2022-05-17 02:41:15 +02:00
$file -> moveTo ( '../content' . '/' . $name );
2022-03-19 00:00:51 +01:00
2022-03-19 22:55:56 +01:00
//open it and get files to verify user
2022-03-20 00:15:56 +01:00
$zip = new \ZipArchive ();
2022-05-17 02:41:15 +02:00
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 00:00:51 +01:00
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 ) {
2022-05-17 02:41:15 +02:00
if ( password_verify ( $body [ 'restore_member_pass' ], $found [ 'password' ])) {
2022-03-19 00:00:51 +01:00
//backup verified, restore site
//set new secret key for older folks configs
$newFolks = [];
2022-05-17 02:41:15 +02:00
if ( ! isset ( $found [ 'secret' ])) {
$found [ 'secret' ] = StringTools :: randomString ( 12 );
2022-03-19 00:00:51 +01:00
}
array_push ( $newFolks , $found );
//dump files in folder
2022-05-17 02:41:15 +02:00
$zip -> extractTo ( '../content' );
2022-03-19 00:00:51 +01:00
//move to appropriate spots
/*
rename (
" ../content/settings/settings.json " ,
" ../config/settings.json "
);
*/
//load up old config file
$newConfig = json_decode (
2022-05-17 02:41:15 +02:00
file_get_contents ( '../content/settings/settings.json' ),
2022-03-19 00:00:51 +01:00
true
);
2022-03-19 22:55:56 +01:00
//check for key, add if not there
2022-05-17 02:41:15 +02:00
if ( ! isset ( $newConfig [ 'global' ][ 'externalAPI' ])) {
$newConfig [ 'global' ][ 'externalAPI' ] = 'false' ;
2022-03-19 00:00:51 +01:00
}
2022-03-19 22:55:56 +01:00
//write new config file
2022-05-17 02:41:15 +02:00
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");
2022-05-17 02:41:15 +02:00
DocTools :: writeSettings ( '../config/folks.json' , $newFolks );
2022-03-19 00:00:51 +01:00
2022-05-17 02:41:15 +02: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
2022-05-17 02:41:15 +02:00
$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-05-17 02:41:15 +02: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 {
2022-05-17 02:41:15 +02:00
rename ( '../content/public/assets/images/blog' , $blogImagePath );
2022-03-19 00:00:51 +01:00
}
2022-05-17 02:41:15 +02:00
if ( $globs = glob ( $userImagePath . '/*' )) {
2022-03-19 00:00:51 +01:00
//directory not empty, relax
} else {
2022-05-17 02:41:15 +02:00
rename ( '../content/public/assets/images/user' , $userImagePath );
2022-03-19 00:00:51 +01:00
}
2022-05-17 02:41:15 +02:00
rename ( '../content/content/pages/' , '../content/pages' );
2022-03-19 00:00:51 +01:00
2022-03-19 22:55:56 +01:00
//legacy check for old file structure
2022-05-17 02:41:15 +02: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-05-17 02:41:15 +02: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 (
2022-05-17 02:41:15 +02:00
'../content/pages/index.md' ,
'../content/pages/start/index.md'
2022-03-19 00:00:51 +01:00
);
}
} 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
2022-05-17 02:41:15 +02:00
DocTools :: deleteFolder ( '../content/settings' );
DocTools :: deleteFolder ( '../content/public' );
DocTools :: deleteFolder ( '../content/content' );
2022-03-19 00:00:51 +01:00
$result = [
2022-05-17 02:41:15 +02:00
'type' => 'requestGood' ,
'message' => 'Site Restored! Redirecting' ,
2022-03-19 00:00:51 +01:00
];
} else {
$result = [
2022-05-17 02:41:15 +02:00
'type' => 'requestLame' ,
'message' => 'Check that password, champ.' ,
2022-03-19 00:00:51 +01:00
];
}
} else {
$result = [
2022-05-17 02:41:15 +02:00
'type' => 'requestLame' ,
'message' => 'No member found by that name, hoss' ,
2022-03-19 00:00:51 +01:00
];
}
$zip -> close ();
2022-05-17 02:41:15 +02:00
$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 = [
2022-05-17 02:41:15 +02:00
'type' => 'requestLame' ,
'message' => 'Could not open backup. RATS!' ,
2022-03-19 00:00:51 +01:00
];
}
return $result ;
}
}