| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
namespace Liuch\DmarcSrg; |
| 33 |
|
| 34 |
use Liuch\DmarcSrg\Exception\SoftException; |
| 35 |
use Liuch\DmarcSrg\Exception\RuntimeException; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
class Common |
| 41 |
{ |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public static $align_res = [ 'fail', 'unknown', 'pass' ]; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public static $disposition = [ 'reject', 'quarantine', 'none' ]; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public static function getFilter() |
| 64 |
{ |
| 65 |
$filter = null; |
| 66 |
if (isset($_GET['filter'])) { |
| 67 |
$filter = []; |
| 68 |
$pa = gettype($_GET['filter']) == 'array' ? $_GET['filter'] : [ $_GET['filter'] ]; |
| 69 |
foreach ($pa as $it) { |
| 70 |
$ia = explode(':', $it, 2); |
| 71 |
if (count($ia) == 2) { |
| 72 |
$filter[$ia[0]] = $ia[1]; |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
return $filter; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public static function monthToRange(string $month): array |
| 89 |
{ |
| 90 |
$ma = explode('-', $month); |
| 91 |
if (count($ma) != 2) { |
| 92 |
throw new SoftException('Incorrect date format'); |
| 93 |
} |
| 94 |
$year = intval($ma[0]); |
| 95 |
$month = intval($ma[1]); |
| 96 |
if ($year <= 0 || $month < 1 || $month > 12) { |
| 97 |
throw new SoftException('Incorrect month or year value'); |
| 98 |
} |
| 99 |
$date1 = new DateTime("{$year}-{$month}-01"); |
| 100 |
$date2 = (clone $date1)->modify('first day of next month'); |
| 101 |
return [ $date1, $date2 ]; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public static function rangeToString(array $range): string |
| 112 |
{ |
| 113 |
$cyear = (new \Datetime())->format('Y'); |
| 114 |
$dform = ($range[0]->format('Y') !== $cyear || $range[1]->format('Y') !== $cyear) ? 'M d Y' : 'M d'; |
| 115 |
$res = $range[0]->format($dform); |
| 116 |
if ($range[0] != $range[1]) { |
| 117 |
$res .= ' - ' . $range[1]->format($dform); |
| 118 |
} |
| 119 |
return $res; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public static function num2percent(int $per, int $cent, bool $with_num): string |
| 132 |
{ |
| 133 |
if (!$per) { |
| 134 |
return '0'; |
| 135 |
} |
| 136 |
$res = sprintf('%.0f%%', $per / $cent * 100); |
| 137 |
if ($with_num) { |
| 138 |
$res .= "({$per})"; |
| 139 |
} |
| 140 |
return $res; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
public static function arrayToCSV(array $data): string |
| 151 |
{ |
| 152 |
$tfd = fopen('php://temp/maxmemory:' . (10 * 1024 * 1024), 'r+'); |
| 153 |
foreach ($data as &$row) { |
| 154 |
if (!fputcsv($tfd, is_array($row) ? $row : [$row], ',', '"', "\\", "\n\r")) { |
| 155 |
throw new RuntimeException('fputcsv failed'); |
| 156 |
} |
| 157 |
} |
| 158 |
unset($row); |
| 159 |
rewind($tfd); |
| 160 |
$res = stream_get_contents($tfd); |
| 161 |
fclose($tfd); |
| 162 |
return $res; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
public static function randomString(int $len = 16): string |
| 173 |
{ |
| 174 |
$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
| 175 |
$c_len = strlen($chars); |
| 176 |
$res = ''; |
| 177 |
do { |
| 178 |
$res .= $chars[random_int(0, 61)]; |
| 179 |
} while (--$len > 0); |
| 180 |
return $res; |
| 181 |
} |
| 182 |
} |