You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
339 lines
8.1 KiB
339 lines
8.1 KiB
<?php
|
|
/**
|
|
* File Logger
|
|
*
|
|
* PHP version 8.3
|
|
*
|
|
* @category Util
|
|
* @package Neato
|
|
* @author Robert S. <tips@technowizardbob.com>
|
|
* @license https://mit-license.org/ MIT License
|
|
* @link https://git.mysnippetsofcode.com/tts/neatoDeploy
|
|
*/
|
|
|
|
set_time_limit(0);
|
|
|
|
require 'neato_registry.php';
|
|
require 'neato_auto_loader.php';
|
|
require 'neato_init.php';
|
|
require 'neato_colors.php';
|
|
require 'neato_configure.php';
|
|
require 'neato_logger.php';
|
|
require 'neato_fns.php';
|
|
require 'neato_enc.php';
|
|
|
|
Configure::set('logger_time_zone', 'America/Detroit');
|
|
|
|
Registry::set('loader', new \Neato_Auto_Loader);
|
|
Registry::get('loader')->register();
|
|
Registry::get('loader')->addNamespace('utils', 'utils');
|
|
Registry::get('loader')->addNamespace('traits', 'traits');
|
|
Registry::get('loader')->addNamespace('package_managers', 'package_managers');
|
|
Registry::get('loader')->addNamespace('init_systems', 'init_systems');
|
|
|
|
/**
|
|
* Force script to require being root to run
|
|
*
|
|
* @return void not ah
|
|
*/
|
|
function forceRoot(): void
|
|
{
|
|
if (posix_getuid() > 0) {
|
|
echo 'Please run as root' . PHP_EOL;
|
|
exit(1);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Force script to require being a Regular user to run
|
|
*
|
|
* @return void not ah
|
|
*/
|
|
function forceNormal(): void
|
|
{
|
|
if (posix_getuid() === 0) {
|
|
echo 'Please run as a normal user' . PHP_EOL;
|
|
exit(1);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* SHA Sum on a file. Make a hash on file.
|
|
*
|
|
* @param string $file filename to give a SHA256 hash on.
|
|
*
|
|
* @return string|false Hash Value of File
|
|
*/
|
|
function shaSum(string $file): string|false
|
|
{
|
|
if (file_exists($file)) {
|
|
$hash = hash_file('sha256', $file, false);
|
|
if ($hash === false) {
|
|
return false;
|
|
}
|
|
return (string) $hash;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* XOR Encrypt/Decrypt, weak encoding...
|
|
*
|
|
* @param string $text Message
|
|
* @param string $key Password
|
|
*
|
|
* @return string Results
|
|
*/
|
|
function xorEncrypt(string $text, string $key): string
|
|
{
|
|
$result = '';
|
|
$textLength = strlen($text);
|
|
$keyLength = strlen($key);
|
|
for ($i = 0; $i < $textLength; $i++) {
|
|
$result .= $text[$i] ^ $key[$i % $keyLength];
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Get the contents of a file.
|
|
*
|
|
* @param string $file file to open and read...
|
|
*
|
|
* @return string|false Contents of the file are returned
|
|
*/
|
|
function readMyFile(string $file): string|false
|
|
{
|
|
$ret = file_get_contents($file);
|
|
if ($ret === false) {
|
|
display("Unable to read from file: {$file}");
|
|
}
|
|
return $ret;
|
|
}
|
|
|
|
/**
|
|
* Write text to a new file.
|
|
*
|
|
* @param string $file Filename to save to
|
|
* @param string $data What to put in the file.
|
|
*
|
|
* @return bool Success?
|
|
*/
|
|
function writeFile(string $file, string $data): bool
|
|
{
|
|
$exit_code = file_put_contents($file, $data);
|
|
$real = ($exit_code === false) ? false : true;
|
|
checkForError($real, "Unable to save to file: {$file}");
|
|
return $real;
|
|
}
|
|
|
|
/**
|
|
* Append text to a existing file.
|
|
*
|
|
* @param string $file Filename to save to
|
|
* @param string $data What to add to the file.
|
|
*
|
|
* @return bool Success?
|
|
*/
|
|
function appendToFile(string $file, string $data): bool
|
|
{
|
|
$exit_code = file_put_contents($file, $data, FILE_APPEND | LOCK_EX);
|
|
$real = ($exit_code === false) ? false : true;
|
|
checkForError($real, "Unable to save to file: {$file}");
|
|
return $real;
|
|
}
|
|
|
|
/**
|
|
* Remove/Delete file
|
|
*
|
|
* @param string $file Filename to erase
|
|
*
|
|
* @return bool Success?
|
|
*/
|
|
function rm(string $file): bool
|
|
{
|
|
$exit_code = unlink($file);
|
|
checkForError($exit_code, "Unable to Delete file: {$file}");
|
|
return $exit_code;
|
|
}
|
|
|
|
/**
|
|
* Renames a file or directory.
|
|
*
|
|
* @param string $old Existing file
|
|
* @param string $new Rename it to this new filename
|
|
*
|
|
* @return bool Success?
|
|
*/
|
|
function mv(string $old, string $new): bool
|
|
{
|
|
$exit_code = rename($old, $new);
|
|
checkForError($exit_code, "Unable to Move file: {$old} to {$new}");
|
|
return $exit_code;
|
|
}
|
|
|
|
/**
|
|
* Copies a file
|
|
*
|
|
* @param string $source Existing file
|
|
* @param string $dest To make cloned file
|
|
*
|
|
* @return bool Success?
|
|
*/
|
|
function cp(string $source, string $dest): bool
|
|
{
|
|
$exit_code = copy($source, $dest);
|
|
checkForError($exit_code, "Unable to Copy file: {$source} to: {$dest}");
|
|
return $exit_code;
|
|
}
|
|
|
|
/**
|
|
* Make a symbolic-Link
|
|
*
|
|
* @param string $source Existing file
|
|
* @param string $new_link Create new linked file
|
|
*
|
|
* @return bool Success?
|
|
*/
|
|
function ln(string $source, string $new_link): bool
|
|
{
|
|
$exit_code = symlink($source, $new_link);
|
|
checkForError($exit_code, "Unable to make Link for file: {$source} to: {$new_link}");
|
|
return $exit_code;
|
|
}
|
|
|
|
/**
|
|
* Make a new Directory
|
|
*
|
|
* @param string $new_folder Create the new folder here
|
|
* @param int $perms Set permissions
|
|
*
|
|
* @return bool Success?
|
|
*/
|
|
function makeDir(string $new_folder, int $perms=0775): bool
|
|
{
|
|
if (!is_dir($new_folder)) {
|
|
$exit_code = mkdir($new_folder, $perms, true);
|
|
checkForError($exit_code, "Unable to mkdir: {$new_folder}");
|
|
return $exit_code;
|
|
} else {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Chmod Change Permissions on File or Directory.
|
|
*
|
|
* @param string $file filename to change permissions on
|
|
* @param string|int $kind Set new permissions
|
|
*
|
|
* @return bool Success?
|
|
*/
|
|
function chmodFileOrDir(string $file, string|int $kind): bool
|
|
{
|
|
if (!is_file($file) && !is_dir($file)) {
|
|
$ret = false;
|
|
} else {
|
|
$perms = getPerms($kind);
|
|
$ret = chmod($file, $perms);
|
|
}
|
|
checkForError($ret, "Unable to chmod: {$file}");
|
|
return $ret;
|
|
}
|
|
|
|
/**
|
|
* Change User-ownership and Group-ownership of a file
|
|
*
|
|
* @param string $file Filename to change
|
|
* @param string|int $uid New User ID
|
|
* @param string|int $gid New Group ID
|
|
*
|
|
* @return bool Success?
|
|
*/
|
|
function changeOwner(string $file, string|int $uid, string|int $gid): bool
|
|
{
|
|
$ret_owner = chown($file, $uid);
|
|
$ret_group = chgrp($file, $gid);
|
|
$exit_code = ($ret_owner && $ret_group) ? true : false;
|
|
checkForError($exit_code, "Unable to chown on: {$file}");
|
|
return $exit_code;
|
|
}
|
|
|
|
/**
|
|
* Recursive change ownership on files
|
|
*
|
|
* @param string $mypath Directory path
|
|
* @param string|int $uid New User ID
|
|
* @param string|int $gid New Group ID
|
|
*
|
|
* @return void IDK
|
|
*/
|
|
function recursiveChangeOwner(string $mypath, string|int $uid, string|int $gid): void
|
|
{
|
|
$d = opendir($mypath);
|
|
while (($file = readdir($d)) !== false) {
|
|
if ($file != "." && $file != "..") {
|
|
$typepath = $mypath . "/" . $file;
|
|
if (filetype($typepath) == 'dir') {
|
|
recursiveChangeOwner($typepath, $uid, $gid);
|
|
}
|
|
|
|
chown($typepath, $uid);
|
|
chgrp($typepath, $gid);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Make a random Password
|
|
*
|
|
* @param int $length Give max length
|
|
*
|
|
* @return string New Password Assigned
|
|
*/
|
|
function makePassword(int $length = 12): string
|
|
{
|
|
$conso = array("b", "c", "d", "f", "g", "h", "j", "k", "l",
|
|
"m", "n", "p", "r", "s", "t", "v", "w", "x", "y", "z");
|
|
$vocal = array("a", "e", "i", "o", "u");
|
|
$special = array("!", "@", "#", "%", "&", "*", ".");
|
|
$password = "";
|
|
$did_special_chr = false;
|
|
$did_number = false;
|
|
|
|
srand((int) microtime() * 1000000);
|
|
|
|
if (rand(0, 100) > 50) {
|
|
$password .= $special[rand(0, 6)];
|
|
$did_special_chr = true;
|
|
$length--;
|
|
}
|
|
|
|
while ($length > 0) {
|
|
$password .= $conso[rand(0, 19)];
|
|
$length--;
|
|
|
|
if ($length >= 1) {
|
|
$v = $vocal[rand(0, 4)];
|
|
$vp = ($v == 'o') ? $v : strtoupper($v); // A,E,I,o,U
|
|
$password .= $vp;
|
|
if ($v == 'o') {
|
|
// Make Extra, letter upper since, o is lower....
|
|
$password .= strtoupper($conso[rand(0, 19)]);
|
|
$length --;
|
|
}
|
|
$length --;
|
|
}
|
|
if ($length >= 1) {
|
|
if ($did_special_chr === false || ( $did_number === true && (rand(0, 100) > 50) )) {
|
|
$password .= $special[rand(0, 6)];
|
|
$did_special_chr = true;
|
|
} else {
|
|
$password .= rand(0, 9);
|
|
$did_number = true;
|
|
}
|
|
$length--;
|
|
}
|
|
}
|
|
return $password;
|
|
}
|
|
|