Debian Codegraph @

classes/Common.php

raw symbols SCIP
1 <?php
2
3 /**
4 * dmarc-srg - A php parser, viewer and summary report generator for incoming DMARC reports.
5 * Copyright (C) 2020-2024 Aleksey Andreev (liuch)
6 *
7 * Available at:
8 * https://github.com/liuch/dmarc-srg
9 *
10 * This program is free software: you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation, either version 3 of the License.
13 *
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * more details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 * =========================
23 *
24 * This file contains common classes
25 *
26 * @category Common
27 * @package DmarcSrg
28 * @author Aleksey Andreev (liuch)
29 * @license https://www.gnu.org/licenses/gpl-3.0.html GNU/GPLv3
30 */
31
32 namespace Liuch\DmarcSrg;
33
34 use Liuch\DmarcSrg\Exception\SoftException;
35 use Liuch\DmarcSrg\Exception\RuntimeException;
36
37 /**
38 * Static common arrays and methods
39 */
40 class Common
41 {
42 /**
43 * This array needs for converting the align result text constant to integer value and back
44 * in Report and ReportList classes
45 *
46 * @var string[]
47 */
48 public static $align_res = [ 'fail', 'unknown', 'pass' ];
49
50 /**
51 * This array needs for converting the the disposition result text constant to integer value and back
52 * in Report and ReportList classes
53 *
54 * @var string[]
55 */
56 public static $disposition = [ 'reject', 'quarantine', 'none' ];
57
58 /**
59 * Retrieves filter values from HTTP GET parameters and returns them as a key-value array or null
60 *
61 * @return array|null
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 * Converts month to date range
81 *
82 * @param string $month Month representation in yyyy-mm format
83 *
84 * @return array Range array:
85 * - DateTime, Start of the month
86 * - DateTime, First second of the next month
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 * Returns the range string in short format (without the current year)
106 *
107 * @param array $range Array with two dates
108 *
109 * @return string
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 * Returns the percentage with the original number. If $per is 0 then '0' is returned.
124 *
125 * @param int $per Value
126 * @param int $cent Divisor for percentage calculation
127 * @param bool $with_num Whether to add the numeric value to the result
128 *
129 * @return string
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 * Converts array to CSV string
145 *
146 * @param array $data Array of data to be converted
147 *
148 * @return string CSV string
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 * Returns a random string of the specified length
167 *
168 * @param int $len String length
169 *
170 * @return string
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 }