commit | author | age
|
9f6455
|
1 |
<?php |
DC |
2 |
|
|
3 |
class Base64 |
|
4 |
{ |
|
5 |
/*** |
|
6 |
* #Encode method |
|
7 |
* @param string $str una stringa da codificare in base64 o un Path di un file da codificare |
|
8 |
* @param bool $isFilePath |
|
9 |
* @param bool $requireSaving se true il risultato viene salvato in un file nella cartella /encoded_files/new_encoded_file.txt |
|
10 |
* @param string $destinationPath il path di destinazione del file in cui salvare il risultato essere in formato "il/tuo/path/nome_file.txt |
|
11 |
*/ |
|
12 |
public static function encode(string $str, $isFilePath = false, $requireSaving = true, $destinationPath = __DIR__ . "/encoded_files/new_encoded_file.txt") |
|
13 |
{ |
|
14 |
if ($isFilePath) { |
|
15 |
$isFile = is_file($str); |
|
16 |
if (!$isFile) { |
|
17 |
echo "File non trovato!"; |
|
18 |
die(); |
|
19 |
} |
|
20 |
$str = file_get_contents($str); |
|
21 |
} |
|
22 |
$encodedData = base64_encode($str); |
|
23 |
|
|
24 |
if ($requireSaving) { |
|
25 |
self::fileWriter($destinationPath, $encodedData); |
|
26 |
} else { |
|
27 |
return $encodedData; |
|
28 |
} |
|
29 |
} |
|
30 |
|
|
31 |
/** |
|
32 |
* @param $filePath string il Path in cui andrà il file |
|
33 |
* @param $text string il testo da inserire nel file |
|
34 |
*/ |
|
35 |
public static function fileWriter(string $filePath, string $text) |
|
36 |
{ |
|
37 |
$newFile = fopen($filePath, "w"); |
|
38 |
fwrite($newFile, $text); |
|
39 |
fclose($newFile); |
|
40 |
} |
|
41 |
|
|
42 |
/*** |
|
43 |
* #Decode method |
|
44 |
* @param string $str una stringa in base64 da decodificare o un Path |
|
45 |
* @param bool $isFilePath [mandatory(true) if $str is a Path optional otherwise] $isFile true se il primo arg è un Path |
|
46 |
* @param bool $requireSaving se true il risultato viene salvato in un file nella cartella /decoded_files/new_decoded_file.txt |
|
47 |
* @param string $destinationPath il path di destinazione del file in cui salvare il risultato essere in formato "il/tuo/path/nome_file.txt |
|
48 |
* @return string la stringa decodificata |
|
49 |
*/ |
|
50 |
public static function decode(string $str, $isFilePath = false, $requireSaving = true, $destinationPath = __DIR__ . "/decoded_files/new_decoded_file.txt") |
|
51 |
{ |
|
52 |
if ($isFilePath) { |
|
53 |
$isFile = is_file($str); |
|
54 |
if (!$isFile) { |
|
55 |
echo "File non trovato!"; |
|
56 |
die(); |
|
57 |
} |
|
58 |
$str = file_get_contents($str); |
|
59 |
} |
|
60 |
$decodedData = base64_decode($str); |
|
61 |
|
|
62 |
if ($requireSaving) { |
|
63 |
self::fileWriter($destinationPath, $decodedData); |
|
64 |
} else { |
|
65 |
return $decodedData; |
|
66 |
} |
|
67 |
} |
|
68 |
} |