2024-04-05 20:29:38 +02:00
|
|
|
<?php
|
|
|
|
|
2024-05-13 21:36:06 +02:00
|
|
|
namespace App\Services\Upkeep;
|
2024-04-05 20:29:38 +02:00
|
|
|
|
2024-05-13 21:36:06 +02:00
|
|
|
use App\Services\Data\SettingsService;
|
2024-04-05 20:29:38 +02:00
|
|
|
use Carbon\Carbon;
|
|
|
|
|
|
|
|
class MaintenanceService
|
|
|
|
{
|
|
|
|
protected $settings;
|
|
|
|
|
|
|
|
public function __construct(SettingsService $settingsService)
|
|
|
|
{
|
|
|
|
$this->settings = $settingsService;
|
|
|
|
}
|
|
|
|
|
2024-07-03 22:30:32 +02:00
|
|
|
public function createContentBackUp()
|
2024-04-05 20:29:38 +02:00
|
|
|
{
|
|
|
|
//make sure back directory is there
|
|
|
|
$stamp = Carbon::now()->format("YmdGis");
|
2024-06-12 22:53:10 +02:00
|
|
|
if (!is_dir(env('FIPAMO_BACKUPS'))) {
|
|
|
|
mkdir(env('FIPAMO_BACKUPS'), 0755, true);
|
2024-04-05 20:29:38 +02:00
|
|
|
}
|
|
|
|
//creat backup zip
|
|
|
|
$zip = new \ZipArchive();
|
|
|
|
|
|
|
|
$zip->open(
|
2024-07-03 22:30:32 +02:00
|
|
|
env('FIPAMO_BACKUPS') . '/backup-content-' . $stamp . '.zip',
|
2024-04-05 20:29:38 +02:00
|
|
|
\ZipArchive::CREATE | \ZipArchive::OVERWRITE
|
|
|
|
);
|
|
|
|
//gather data and path info for md pages
|
2024-06-12 22:53:10 +02:00
|
|
|
$pagePath = env('PAGES_PATH');
|
2024-04-05 20:29:38 +02:00
|
|
|
$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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//gather paths for blog images
|
|
|
|
$blogImages = [];
|
|
|
|
$dir = new \RecursiveDirectoryIterator('../public/assets/images/blog');
|
|
|
|
$flat = new \RecursiveIteratorIterator($dir);
|
|
|
|
$files = new \RegexIterator($flat, '/\.png|jpg|gif$/i');
|
|
|
|
foreach ($files as $file) {
|
|
|
|
$blogImages[] = ['path' => $file->getPath(), 'file' => $file->getFilename()];
|
|
|
|
};
|
|
|
|
|
|
|
|
//gather paths for user images
|
|
|
|
$userImages = [];
|
2024-07-03 22:30:32 +02:00
|
|
|
if (is_dir('../public/assets/images/user')) {
|
|
|
|
$dir = new \RecursiveDirectoryIterator('../public/assets/images/user');
|
|
|
|
$flat = new \RecursiveIteratorIterator($dir);
|
|
|
|
$files = new \RegexIterator($flat, '/\.png|jpg|gif$/i');
|
|
|
|
foreach ($files as $file) {
|
|
|
|
$userImages[] = ['path' => $file->getPath(), 'file' => $file->getFilename()];
|
|
|
|
};
|
|
|
|
}
|
2024-04-05 20:29:38 +02:00
|
|
|
|
2024-04-22 21:11:51 +02:00
|
|
|
//gather paths for blog documents
|
|
|
|
$blogDocs = [];
|
2024-07-03 22:30:32 +02:00
|
|
|
if (is_dir('../public/assets/docs/blog')) {
|
|
|
|
$dir = new \RecursiveDirectoryIterator('../public/assets/docs/blog');
|
|
|
|
$flat = new \RecursiveIteratorIterator($dir);
|
|
|
|
$files = new \RegexIterator($flat, '/\.txt|pdf|rtf$/i');
|
|
|
|
foreach ($files as $file) {
|
|
|
|
$blogDocs[] = ['path' => $file->getPath(), 'file' => $file->getFilename()];
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-04-22 21:11:51 +02:00
|
|
|
//gather paths for blog videos
|
|
|
|
$blogVids = [];
|
2024-07-03 22:30:32 +02:00
|
|
|
if (is_dir('../public/assets/video/blog')) {
|
|
|
|
$dir = new \RecursiveDirectoryIterator('../public/assets/video/blog');
|
|
|
|
$flat = new \RecursiveIteratorIterator($dir);
|
|
|
|
$files = new \RegexIterator($flat, '/\.mp4$/i');
|
|
|
|
foreach ($files as $file) {
|
|
|
|
$blogVids[] = ['path' => $file->getPath(), 'file' => $file->getFilename()];
|
|
|
|
};
|
|
|
|
}
|
2024-04-22 21:11:51 +02:00
|
|
|
|
2024-04-05 20:29:38 +02:00
|
|
|
//add directory for settings and save them
|
|
|
|
$zip->addEmptyDir('config');
|
2024-04-22 21:11:51 +02:00
|
|
|
$zip->addEmptyDir('assets');
|
2024-04-05 20:29:38 +02:00
|
|
|
$zip->addFile(env('SETTINGS_PATH'), 'config/settings.json');
|
|
|
|
$zip->addFile(env('FOLKS_PATH'), 'config/folks.json');
|
|
|
|
$zip->addFile(env('TAGS_PATH'), 'config/tags.json');
|
|
|
|
// create temp files for image lists
|
2024-06-12 22:53:10 +02:00
|
|
|
file_put_contents(env('FIPAMO_BACKUPS') . '/blog_images_temp.json', json_encode($blogImages));
|
|
|
|
file_put_contents(env('FIPAMO_BACKUPS') . '/user_images_temp.json', json_encode($userImages));
|
|
|
|
file_put_contents(env('FIPAMO_BACKUPS') . '/blog_docs_temp.json', json_encode($blogDocs));
|
|
|
|
file_put_contents(env('FIPAMO_BACKUPS') . '/blog_vids_temp.json', json_encode($blogVids));
|
2024-04-05 20:29:38 +02:00
|
|
|
//add to zip
|
2024-06-12 22:53:10 +02:00
|
|
|
$zip->addFile(env('FIPAMO_BACKUPS') . '/blog_images_temp.json', 'assets/blog_images.json');
|
|
|
|
$zip->addFile(env('FIPAMO_BACKUPS') . '/user_images_temp.json', 'assets/user_images.json');
|
|
|
|
$zip->addFile(env('FIPAMO_BACKUPS') . '/blog_docs_temp.json', 'assets/blog_docs.json');
|
|
|
|
$zip->addFile(env('FIPAMO_BACKUPS') . '/blog_vids_temp.json', 'assets/blog_videos.json');
|
2024-04-05 20:29:38 +02:00
|
|
|
//save zip file
|
|
|
|
$zip->close();
|
|
|
|
//clean up temp files
|
2024-06-12 22:53:10 +02:00
|
|
|
unlink(env('FIPAMO_BACKUPS') . '/blog_images_temp.json');
|
|
|
|
unlink(env('FIPAMO_BACKUPS') . '/user_images_temp.json');
|
|
|
|
unlink(env('FIPAMO_BACKUPS') . '/blog_docs_temp.json');
|
|
|
|
unlink(env('FIPAMO_BACKUPS') . '/blog_vids_temp.json');
|
2024-04-05 20:29:38 +02:00
|
|
|
|
|
|
|
//update settings file with latest back up date
|
2024-07-03 22:30:32 +02:00
|
|
|
$this->settings->updateGlobalData('last_content_backup', $stamp);
|
|
|
|
|
|
|
|
return ['message' => "Content backup created. THIS IS A SAFE SPACE!"];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function createFileBackUp()
|
|
|
|
{
|
|
|
|
$stamp = Carbon::now()->format("YmdGis");
|
|
|
|
$zip = new \ZipArchive();
|
|
|
|
$zip->open(
|
|
|
|
env('FIPAMO_BACKUPS') . '/backup-files-' . $stamp . '.zip',
|
|
|
|
\ZipArchive::CREATE | \ZipArchive::OVERWRITE
|
|
|
|
);
|
|
|
|
//gather 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);
|
|
|
|
}
|
|
|
|
}
|
2024-04-05 20:29:38 +02:00
|
|
|
|
2024-07-03 22:30:32 +02:00
|
|
|
//gather data and path info for user images
|
|
|
|
$userImagesPath = '../public/assets/images/user';
|
|
|
|
$yearPaths = glob($userImagesPath . '/*', GLOB_ONLYDIR);
|
|
|
|
foreach ($yearPaths as $years) {
|
|
|
|
$year = explode('/', $years);
|
|
|
|
$monthsPath = glob($userImagesPath . '/' . $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/user/' . $year[5] . '/' . $month[6] . '/',
|
|
|
|
'remove_all_path' => true,
|
|
|
|
];
|
|
|
|
$zip->addGlob($months . '/*.*', GLOB_BRACE, $options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$zip->close();
|
|
|
|
$this->settings->updateGlobalData('last_files_backup', $stamp);
|
|
|
|
return ['message' => "Files are backed up. Breath Easy!"];
|
2024-04-05 20:29:38 +02:00
|
|
|
}
|
|
|
|
}
|