This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Netdudes\DataSourceryBundle\Extension; |
||
4 | |||
5 | use Netdudes\DataSourceryBundle\Util\CurrentDateTimeProvider; |
||
6 | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
||
7 | |||
8 | class BuiltInFunctionsExtension implements UqlExtensionInterface |
||
9 | { |
||
10 | /** |
||
11 | * @var TokenStorageInterface |
||
12 | */ |
||
13 | private $tokenStorage; |
||
14 | |||
15 | /** |
||
16 | * @var CurrentDateTimeProvider |
||
17 | */ |
||
18 | private $dateTimeProvider; |
||
19 | |||
20 | /** |
||
21 | * @param TokenStorageInterface $tokenStorage |
||
22 | * @param CurrentDateTimeProvider $dateTimeProvider |
||
23 | */ |
||
24 | public function __construct(TokenStorageInterface $tokenStorage, CurrentDateTimeProvider $dateTimeProvider) |
||
25 | { |
||
26 | $this->tokenStorage = $tokenStorage; |
||
27 | $this->dateTimeProvider = $dateTimeProvider; |
||
28 | } |
||
29 | |||
30 | /** |
||
31 | * {@inheritdoc} |
||
32 | */ |
||
33 | public function getFunctions() |
||
34 | { |
||
35 | return [ |
||
36 | new UqlFunction('now', $this, 'now'), |
||
37 | new UqlFunction('startOfDay', $this, 'startOfDay'), |
||
38 | new UqlFunction('startOfWeek', $this, 'startOfWeek'), |
||
39 | new UqlFunction('startOfMonth', $this, 'startOfMonth'), |
||
40 | new UqlFunction('startOfYear', $this, 'startOfYear'), |
||
41 | new UqlFunction('currentUser', $this, 'currentUser'), |
||
42 | new UqlFunction('random', $this, 'random') |
||
43 | ]; |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * Gets the current timestamp, with an offset string |
||
48 | * |
||
49 | * @param string $offset |
||
50 | * |
||
51 | * @return string |
||
52 | * @throws \Exception |
||
53 | */ |
||
54 | public function now($offset = null) |
||
55 | { |
||
56 | $now = clone $this->dateTimeProvider->get(); |
||
57 | |||
58 | if ($offset) { |
||
0 ignored issues
–
show
|
|||
59 | $interval = \DateInterval::createFromDateString($offset); |
||
60 | $now->add($interval); |
||
61 | |||
62 | if ($now == $this->dateTimeProvider->get()) { |
||
63 | // The date didn't change therefore we assume the given offset is not valid |
||
64 | throw new \Exception($offset . ' is not a valid date/time interval.'); |
||
65 | } |
||
66 | } |
||
67 | |||
68 | return $now->format(\DateTime::ISO8601); |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * Gets a date with the hour 00:00:00 |
||
73 | * |
||
74 | * @param string $date |
||
75 | * |
||
76 | * @return string |
||
77 | * @throws \Exception |
||
78 | */ |
||
79 | public function startOfDay($date = null) |
||
80 | { |
||
81 | $now = clone $this->dateTimeProvider->get(); |
||
82 | |||
83 | if ($date) { |
||
0 ignored issues
–
show
The expression
$date of type string|null is loosely compared to true ; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.
In PHP, under loose comparison (like For '' == false // true
'' == null // true
'ab' == false // false
'ab' == null // false
// It is often better to use strict comparison
'' === false // false
'' === null // false
![]() |
|||
84 | $now = $this->modifyDate($now, $date); |
||
85 | } |
||
86 | |||
87 | $now->setTime(0, 0, 0); |
||
88 | |||
89 | return $now->format(\DateTime::ISO8601); |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Gets the Monday of the week for the specified date with the hour 00:00:00 |
||
94 | * |
||
95 | * @param string $date |
||
96 | * |
||
97 | * @return string |
||
98 | * @throws \Exception |
||
99 | */ |
||
100 | View Code Duplication | public function startOfWeek($date = null) |
|
101 | { |
||
102 | $now = clone $this->dateTimeProvider->get(); |
||
103 | |||
104 | if ($date) { |
||
0 ignored issues
–
show
The expression
$date of type string|null is loosely compared to true ; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.
In PHP, under loose comparison (like For '' == false // true
'' == null // true
'ab' == false // false
'ab' == null // false
// It is often better to use strict comparison
'' === false // false
'' === null // false
![]() |
|||
105 | $now = $this->modifyDate($now, $date); |
||
106 | } |
||
107 | |||
108 | $year = $now->format('o'); // o = ISO-8601 year number |
||
109 | $week = $now->format('W'); // W = ISO-8601 week number of year, weeks starting on Monday |
||
110 | |||
111 | $startOfWeek = $now->setISODate($year, $week); |
||
112 | $startOfWeek->setTime(0, 0, 0); |
||
113 | |||
114 | return $startOfWeek->format(\DateTime::ISO8601); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Gets the first day of the month for the specified date with the hour 00:00:00 |
||
119 | * |
||
120 | * @param string $date |
||
121 | * |
||
122 | * @return string |
||
123 | * @throws \Exception |
||
124 | */ |
||
125 | View Code Duplication | public function startOfMonth($date = null) |
|
126 | { |
||
127 | $now = clone $this->dateTimeProvider->get(); |
||
128 | |||
129 | if ($date) { |
||
0 ignored issues
–
show
The expression
$date of type string|null is loosely compared to true ; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.
In PHP, under loose comparison (like For '' == false // true
'' == null // true
'ab' == false // false
'ab' == null // false
// It is often better to use strict comparison
'' === false // false
'' === null // false
![]() |
|||
130 | $now = $this->modifyDate($now, $date); |
||
131 | } |
||
132 | |||
133 | $year = $now->format('Y'); |
||
134 | $month = $now->format('m'); |
||
135 | |||
136 | $startOfMonth = $now->setDate($year, $month, 1); |
||
137 | $startOfMonth->setTime(0, 0, 0); |
||
138 | |||
139 | return $startOfMonth->format(\DateTime::ISO8601); |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * Gets the first day of the year for the specified date with the hour 00:00:00 |
||
144 | * |
||
145 | * @param string $date |
||
146 | * |
||
147 | * @return string |
||
148 | * @throws \Exception |
||
149 | */ |
||
150 | public function startOfYear($date = null) |
||
151 | { |
||
152 | $now = clone $this->dateTimeProvider->get(); |
||
153 | |||
154 | if ($date) { |
||
0 ignored issues
–
show
The expression
$date of type string|null is loosely compared to true ; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.
In PHP, under loose comparison (like For '' == false // true
'' == null // true
'ab' == false // false
'ab' == null // false
// It is often better to use strict comparison
'' === false // false
'' === null // false
![]() |
|||
155 | $now = $this->modifyDate($now, $date); |
||
156 | } |
||
157 | |||
158 | $year = $now->format('Y'); |
||
159 | |||
160 | $startOfYear = $now->setDate($year, 1, 1); |
||
161 | $startOfYear->setTime(0, 0, 0); |
||
162 | |||
163 | return $startOfYear->format(\DateTime::ISO8601); |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * Gets the current users' username |
||
168 | * |
||
169 | * @return string |
||
170 | */ |
||
171 | public function currentUser() |
||
172 | { |
||
173 | return $this->tokenStorage->getToken()->getUsername(); |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * Gets a random value between $min and $max |
||
178 | * |
||
179 | * @param int $min |
||
180 | * @param int $max |
||
181 | * |
||
182 | * @return int |
||
183 | */ |
||
184 | public function random($min = 0, $max = 10) |
||
185 | { |
||
186 | return rand($min, $max); |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * @param \DateTime $date |
||
191 | * @param string $change |
||
192 | * |
||
193 | * @return \DateTime |
||
194 | * @throws \Exception |
||
195 | */ |
||
196 | private function modifyDate(\DateTime $date, $change) |
||
197 | { |
||
198 | try { |
||
199 | $date->modify($change); |
||
200 | } catch (\Exception $e) { |
||
201 | throw new \Exception($change . ' is not a valid date or date offset.'); |
||
202 | } |
||
203 | |||
204 | return ($date); |
||
205 | } |
||
206 | } |
||
207 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: