2020-11-17 23:27:25 +01:00
|
|
|
<?php
|
2021-03-29 00:22:00 +02:00
|
|
|
use function _\find;
|
2021-04-17 07:14:14 +02:00
|
|
|
use function _\pull;
|
|
|
|
use function _\remove;
|
2020-11-17 23:27:25 +01:00
|
|
|
class Settings
|
|
|
|
{
|
2021-03-25 22:05:32 +01:00
|
|
|
private $folks;
|
|
|
|
private $tags;
|
2021-04-16 07:19:59 +02:00
|
|
|
private $themes = [];
|
2021-04-14 23:51:06 +02:00
|
|
|
private static $settings;
|
2021-03-25 22:05:32 +01:00
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
//gets all settings files and converts to php objects
|
2021-03-29 00:22:00 +02:00
|
|
|
$this->folks = json_decode(file_get_contents("../config/folks.json"), true);
|
|
|
|
$this->tags = json_decode(file_get_contents("../config/tags.json"), true);
|
2021-04-14 23:51:06 +02:00
|
|
|
self::$settings = json_decode(
|
2021-03-29 00:22:00 +02:00
|
|
|
file_get_contents("../config/settings.json"),
|
2021-03-25 22:05:32 +01:00
|
|
|
true
|
|
|
|
);
|
2021-04-16 07:19:59 +02:00
|
|
|
|
|
|
|
$_themes = glob("../content/themes/*", GLOB_ONLYDIR);
|
|
|
|
foreach ($_themes as $theme) {
|
|
|
|
array_push(
|
|
|
|
$this->themes,
|
|
|
|
json_decode(file_get_contents($theme . "/theme.json"), true)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-17 01:15:47 +02:00
|
|
|
public static function sync($data)
|
|
|
|
{
|
|
|
|
$settings = self::$settings;
|
|
|
|
$settings["global"]["base_url"] = $data["global"]["base_url"];
|
|
|
|
$settings["global"]["title"] = $data["global"]["title"];
|
|
|
|
$settings["global"]["descriptions"] = $data["global"]["descriptions"];
|
|
|
|
$settings["global"]["base_url"] = $data["global"]["base_url"];
|
|
|
|
$settings["global"]["private"] = $data["global"]["private"];
|
|
|
|
$settings["global"]["renderOnSave"] = $data["global"]["renderOnSave"];
|
|
|
|
$settings["global"]["theme"] = $data["global"]["theme"];
|
|
|
|
|
|
|
|
Member::updateData("handle", $data["member"]["handle"]);
|
|
|
|
Member::updateData("email", $data["member"]["email"]);
|
|
|
|
|
|
|
|
$settings["email"]["active"] = $data["email"]["active"];
|
|
|
|
$settings["email"]["smtp"] = $data["email"]["smtp"];
|
|
|
|
$settings["email"]["mailgun"] = $data["email"]["mailgun"];
|
|
|
|
|
|
|
|
DocTools::writeSettings("../config/settings.json", $settings);
|
|
|
|
}
|
|
|
|
|
2021-04-18 02:32:45 +02:00
|
|
|
public static function navSync($data)
|
|
|
|
{
|
|
|
|
$settings = self::$settings;
|
|
|
|
|
|
|
|
$remove = $data["remove"];
|
|
|
|
//if remove contains id, find nav item page and set menu to false
|
|
|
|
if ($remove != null || $remove != "") {
|
|
|
|
$page = (new Book("../content/pages"))->findPageById($remove);
|
|
|
|
$page["menu"] = "false";
|
|
|
|
$page["published"]
|
|
|
|
? ($page["published"] = "true")
|
|
|
|
: ($page["published"] = "false");
|
|
|
|
$page["featured"]
|
|
|
|
? ($page["featured"] = "true")
|
|
|
|
: ($page["featured"] = "false");
|
|
|
|
$page["deleted"]
|
|
|
|
? ($page["deleted"] = "true")
|
|
|
|
: ($page["deleted"] = "false");
|
|
|
|
$updated = new \Moment\Moment();
|
|
|
|
$created = new \Moment\Moment($page["rawCreated"]);
|
|
|
|
$page["created"] = $created->format("Y-m-d\TH:i:sP");
|
|
|
|
$page["updated"] = $updated->format("Y-m-d\TH:i:sP");
|
|
|
|
|
|
|
|
$md = DocTools::objectToMD($page);
|
|
|
|
|
|
|
|
if ($page["layout"] == "index") {
|
|
|
|
$writePath = "../content/pages/start/index.md";
|
|
|
|
} else {
|
|
|
|
$writePath =
|
|
|
|
"../content/pages/" . $page["path"] . "/" . $page["slug"] . ".md";
|
|
|
|
}
|
|
|
|
|
|
|
|
DocTools::writePages("write", $page["path"], $writePath, $md);
|
|
|
|
}
|
|
|
|
|
|
|
|
$settings["menu"] = [];
|
|
|
|
$items = $data["menu"];
|
|
|
|
foreach ($items as $item) {
|
|
|
|
array_push($settings["menu"], [
|
|
|
|
"title" => $item["title"],
|
|
|
|
"id" => $item["id"],
|
|
|
|
"uuid" => $item["uuid"],
|
|
|
|
"slug" => $item["slug"],
|
|
|
|
"path" => $item["path"],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
DocTools::writeSettings("../config/settings.json", $settings);
|
|
|
|
}
|
|
|
|
|
2021-04-16 07:19:59 +02:00
|
|
|
public function getThemes()
|
|
|
|
{
|
|
|
|
return $this->themes;
|
2021-03-25 22:05:32 +01:00
|
|
|
}
|
2021-03-27 21:59:44 +01:00
|
|
|
|
2021-04-01 21:54:03 +02:00
|
|
|
public function getFolks($key = null)
|
2021-03-27 21:59:44 +01:00
|
|
|
{
|
2021-03-29 00:22:00 +02:00
|
|
|
if (isset($key)) {
|
|
|
|
$member = Session::get("member");
|
|
|
|
$found = find($this->folks, ["handle" => $member["handle"]]);
|
2021-04-25 01:37:25 +02:00
|
|
|
if ($found) {
|
|
|
|
return $found[$key];
|
|
|
|
}
|
2021-03-29 00:22:00 +02:00
|
|
|
} else {
|
|
|
|
return $this->folks;
|
|
|
|
}
|
2021-03-27 21:59:44 +01:00
|
|
|
}
|
2021-04-14 23:51:06 +02:00
|
|
|
|
2021-04-16 07:19:59 +02:00
|
|
|
public function getSettings($key = null)
|
|
|
|
{
|
|
|
|
return self::$settings;
|
|
|
|
}
|
|
|
|
|
2021-04-17 01:15:47 +02:00
|
|
|
public static function updateGlobalData($key, $data)
|
|
|
|
{
|
|
|
|
$settings = self::$settings;
|
|
|
|
$settings["global"][$key] = $data;
|
|
|
|
DocTools::writeSettings("../config/settings.json", $settings);
|
|
|
|
}
|
|
|
|
|
2021-04-14 23:51:06 +02:00
|
|
|
public static function getCurrentIndex()
|
|
|
|
{
|
|
|
|
$settings = self::$settings;
|
|
|
|
return $settings["library_stats"]["current_index"];
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function updateIndex()
|
|
|
|
{
|
|
|
|
$settings = self::$settings;
|
2021-04-24 05:54:56 +02:00
|
|
|
|
|
|
|
$settings["library_stats"]["current_index"] =
|
|
|
|
$settings["library_stats"]["current_index"] + 1;
|
2021-04-14 23:51:06 +02:00
|
|
|
|
|
|
|
DocTools::writeSettings("../config/settings.json", $settings);
|
|
|
|
}
|
2021-04-17 07:14:14 +02:00
|
|
|
|
|
|
|
public static function updateMenu($body)
|
|
|
|
{
|
|
|
|
$settings = self::$settings;
|
|
|
|
//$menu = $settings["menu"];
|
|
|
|
$item = [
|
|
|
|
"title" => $body["title"],
|
|
|
|
"id" => $body["id"],
|
|
|
|
"uuid" => $body["uuid"],
|
|
|
|
"slug" => $body["slug"],
|
|
|
|
"path" => $body["path"],
|
|
|
|
];
|
|
|
|
if ($body["menu"] == "true") {
|
|
|
|
if (!find($settings["menu"], ["uuid" => $item["uuid"]])) {
|
|
|
|
array_push($settings["menu"], $item);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (find($settings["menu"], ["uuid" => $item["uuid"]])) {
|
|
|
|
pull($settings["menu"], $item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DocTools::writeSettings("../config/settings.json", $settings);
|
|
|
|
}
|
2021-04-22 03:41:32 +02:00
|
|
|
|
2021-04-26 00:48:46 +02:00
|
|
|
public static function makeBackup()
|
|
|
|
{
|
|
|
|
//make sure back directory is there
|
|
|
|
if (!is_dir("../config/backups")) {
|
|
|
|
mkdir("../config/backups", 0755, true);
|
|
|
|
}
|
|
|
|
//creat backup zip
|
|
|
|
$zip = new ZipArchive();
|
|
|
|
$zip->open(
|
|
|
|
"../config/backups/latest_back.zip",
|
|
|
|
ZipArchive::CREATE | ZipArchive::OVERWRITE
|
|
|
|
);
|
|
|
|
//gather data and path info for md pages
|
|
|
|
$pagePath = "../content/pages";
|
|
|
|
$yearPaths = glob($pagePath . "/*", GLOB_ONLYDIR);
|
|
|
|
foreach ($yearPaths as $years) {
|
|
|
|
$year = explode("/", $years);
|
|
|
|
//grap the index and save it
|
|
|
|
if (trim($year[3]) == "start") {
|
|
|
|
$options = [
|
|
|
|
"add_path" => "content/pages/" . $year[3] . "/",
|
|
|
|
"remove_all_path" => true,
|
|
|
|
];
|
|
|
|
$zip->addGlob($years . "/*.md", GLOB_BRACE, $options);
|
|
|
|
}
|
|
|
|
$monthsPath = glob($pagePath . "/" . $year[3] . "/*", GLOB_ONLYDIR);
|
|
|
|
foreach ($monthsPath as $months) {
|
|
|
|
$month = explode("/", $months);
|
|
|
|
//once info is collected, add md pages to zip
|
|
|
|
$options = [
|
|
|
|
"add_path" => "content/pages/" . $year[3] . "/" . $month[4] . "/",
|
|
|
|
"remove_all_path" => true,
|
|
|
|
];
|
|
|
|
$zip->addGlob($months . "/*.md", GLOB_BRACE, $options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//tather data and path info for blog images
|
|
|
|
$blogImagesPath = "../public/assets/images/blog";
|
|
|
|
$yearPaths = glob($blogImagesPath . "/*", GLOB_ONLYDIR);
|
|
|
|
foreach ($yearPaths as $years) {
|
|
|
|
$year = explode("/", $years);
|
|
|
|
$monthsPath = glob($blogImagesPath . "/" . $year[5] . "/*", GLOB_ONLYDIR);
|
|
|
|
foreach ($monthsPath as $months) {
|
|
|
|
$month = explode("/", $months);
|
|
|
|
//once info is collected, add images pages to zip
|
|
|
|
$options = [
|
|
|
|
"add_path" =>
|
|
|
|
"public/assets/images/blog/" . $year[5] . "/" . $month[6] . "/",
|
|
|
|
"remove_all_path" => true,
|
|
|
|
];
|
|
|
|
$zip->addGlob($months . "/*.*", GLOB_BRACE, $options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//add directory for settings and save them
|
|
|
|
$zip->addEmptyDir("settings");
|
|
|
|
$zip->addFile("../config/settings.json", "settings/settings.json");
|
|
|
|
$zip->addFile("../config/folks.json", "settings/folks.json");
|
|
|
|
$zip->addFile("../config/tags.json", "settings/tags.json");
|
|
|
|
//save zip file
|
|
|
|
$zip->close();
|
|
|
|
|
|
|
|
//update settings file with latest back up date
|
|
|
|
$updated = new \Moment\Moment();
|
|
|
|
self::updateGlobalData("last_backup", $updated->format("Y-m-d\TH:i:sP"));
|
|
|
|
|
|
|
|
$result = ["message" => "Backup created. THIS IS A SAFE SPACE!"];
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2021-04-22 03:41:32 +02:00
|
|
|
public static function updateTags()
|
|
|
|
{
|
|
|
|
$tags = Sorting::tags();
|
|
|
|
DocTools::writeSettings("../config/tags.json", $tags);
|
|
|
|
}
|
2021-03-25 22:05:32 +01:00
|
|
|
}
|