<?php
|
|
class Base64
|
{
|
/***
|
* #Encode method
|
* @param string $str una stringa da codificare in base64 o un Path di un file da codificare
|
* @param bool $isFilePath
|
* @param bool $requireSaving se true il risultato viene salvato in un file nella cartella /encoded_files/new_encoded_file.txt
|
* @param string $destinationPath il path di destinazione del file in cui salvare il risultato essere in formato "il/tuo/path/nome_file.txt
|
*/
|
public static function encode(string $str, $isFilePath = false, $requireSaving = true, $destinationPath = __DIR__ . "/encoded_files/new_encoded_file.txt")
|
{
|
if ($isFilePath) {
|
$isFile = is_file($str);
|
if (!$isFile) {
|
echo "File non trovato!";
|
die();
|
}
|
$str = file_get_contents($str);
|
}
|
$encodedData = base64_encode($str);
|
|
if ($requireSaving) {
|
self::fileWriter($destinationPath, $encodedData);
|
} else {
|
return $encodedData;
|
}
|
}
|
|
/**
|
* @param $filePath string il Path in cui andrà il file
|
* @param $text string il testo da inserire nel file
|
*/
|
public static function fileWriter(string $filePath, string $text)
|
{
|
$newFile = fopen($filePath, "w");
|
fwrite($newFile, $text);
|
fclose($newFile);
|
}
|
|
/***
|
* #Decode method
|
* @param string $str una stringa in base64 da decodificare o un Path
|
* @param bool $isFilePath [mandatory(true) if $str is a Path optional otherwise] $isFile true se il primo arg è un Path
|
* @param bool $requireSaving se true il risultato viene salvato in un file nella cartella /decoded_files/new_decoded_file.txt
|
* @param string $destinationPath il path di destinazione del file in cui salvare il risultato essere in formato "il/tuo/path/nome_file.txt
|
* @return string la stringa decodificata
|
*/
|
public static function decode(string $str, $isFilePath = false, $requireSaving = true, $destinationPath = __DIR__ . "/decoded_files/new_decoded_file.txt")
|
{
|
if ($isFilePath) {
|
$isFile = is_file($str);
|
if (!$isFile) {
|
echo "File non trovato!";
|
die();
|
}
|
$str = file_get_contents($str);
|
}
|
$decodedData = base64_decode($str);
|
|
if ($requireSaving) {
|
self::fileWriter($destinationPath, $decodedData);
|
} else {
|
return $decodedData;
|
}
|
}
|
}
|