fipamo/brain/utility/Setup.php

231 lines
8.6 KiB
PHP
Raw Permalink Normal View History

<?php
namespace brain\utility;
use Carbon\Carbon;
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
$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();
$handle = $body['new_member_handle'];
$email = $body['new_member_email'];
$pass = $body['new_member_pass'];
$title = $body['new_member_title'];
$now = Carbon::now();
2022-03-19 22:55:56 +01:00
//setup folks config
$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\TH:i:sP");
$newFolks[0]['updated'] = $now->format("Y-m-d\TH:i:sP");
2022-03-19 22:55:56 +01:00
//set up settings config
$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"));
$index = [
'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\TH:i:sP"),
'updated' => $now->format("Y-m-d\TH:i:sP"),
'deleted' => 'false',
'slug' => 'first',
'menu' => 'false',
'featured' => 'false',
'published' => 'true',
'content' => "# F**k Yes \n\nIf you're seeing this, you're up and running. NICE WORK!\n\nFrom here, feel free to start dropping pages to your heart's content.\n\nFor some tips about using Fipamo, check out the ![docs](https://code.playvicio.us/Are0h/Fipamo/wiki/02-Usage)\n\nAll good? Feel free to edit this page to whatever you want!\n\nYOU'RE THE CAPTAIN NOW.",
];
$freshIndex = DocTools::objectToMD($index);
2022-03-19 22:55:56 +01:00
//once all files created, write down
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
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'];
2022-03-20 00:15:56 +01:00
//NOTE: If this fails check 'post_max_size' in php.ini
$size = $file->getSize();
$name = $file->getClientFileName();
2022-03-19 22:55:56 +01:00
//park it so it can be read
$file->moveTo('../content' . '/' . $name);
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();
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
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
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 22:55:56 +01:00
//rename("../content/settings/folks.json", "../config/folks.json");
DocTools::writeSettings('../config/folks.json', $newFolks);
rename('../content/settings/tags.json', '../config/tags.json');
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 22:55:56 +01:00
//check to see if image dirs are empty, if not chill
if ($globs = glob($blogImagePath . '/*')) {
2022-03-19 22:55:56 +01:00
//directory not empty, relax
} 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
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.
mkdir('../content/pages/start', 0755, true);
2022-03-19 22:55:56 +01:00
//move start page to appropriate spot
rename(
'../content/pages/index.md',
'../content/pages/start/index.md'
);
}
} else {
2022-03-19 22:55:56 +01:00
//chill
}
2022-03-19 22:55:56 +01:00
//clean up
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
unlink($zipPath);
} else {
$result = [
'type' => 'requestLame',
'message' => 'Could not open backup. RATS!',
];
}
return $result;
}
}