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 Vipertecpro\PaginateRoute; |
||
4 | |||
5 | use Illuminate\Pagination\LengthAwarePaginator; |
||
6 | use Illuminate\Contracts\Routing\UrlGenerator; |
||
7 | use Illuminate\Routing\RouteParameterBinder; |
||
8 | use Illuminate\Routing\Router; |
||
9 | use Illuminate\Support\Facades\Request; |
||
10 | use Illuminate\Translation\Translator; |
||
11 | |||
12 | class PaginateRoute |
||
13 | { |
||
14 | /** |
||
15 | * @var Translator |
||
16 | */ |
||
17 | protected $translator; |
||
18 | |||
19 | /** |
||
20 | * @var Router |
||
21 | */ |
||
22 | protected $router; |
||
23 | |||
24 | /** |
||
25 | * @var UrlGenerator |
||
26 | */ |
||
27 | protected $urlGenerator; |
||
28 | |||
29 | /** |
||
30 | * @var string|array |
||
31 | */ |
||
32 | protected $pageKeyword; |
||
33 | |||
34 | /** |
||
35 | * @param Translator $translator |
||
36 | * @param Router $router |
||
37 | * @param UrlGenerator $urlGenerator |
||
38 | */ |
||
39 | public function __construct(Translator $translator, Router $router, UrlGenerator $urlGenerator) |
||
40 | { |
||
41 | $this->translator = $translator; |
||
42 | $this->router = $router; |
||
43 | $this->urlGenerator = $urlGenerator; |
||
44 | |||
45 | // Unfortunately we can't do this in the service provider since routes are booted first |
||
46 | $this->translator->addNamespace('paginateroute', __DIR__.'/../resources/lang'); |
||
47 | |||
48 | $this->pageKeyword = $this->translator->get('paginateroute::paginateroute.page'); |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * Return the current page. |
||
53 | * |
||
54 | * @return int |
||
55 | */ |
||
56 | public function currentPage() |
||
57 | { |
||
58 | $currentRoute = $this->router->getCurrentRoute(); |
||
59 | |||
60 | if (! $currentRoute) { |
||
61 | return 1; |
||
62 | } |
||
63 | |||
64 | $query = $currentRoute->parameter('pageQuery'); |
||
65 | |||
66 | return (int) str_replace($this->pageKeyword.'/', '', $query) ?: 1; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Check if the given page is the current page. |
||
71 | * |
||
72 | * @param int $page |
||
73 | * |
||
74 | * @return bool |
||
75 | */ |
||
76 | public function isCurrentPage($page): bool |
||
77 | { |
||
78 | return $this->currentPage() === $page; |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * Get the next page number. |
||
83 | * |
||
84 | * @param LengthAwarePaginator $paginator |
||
85 | * |
||
86 | * @return int|void |
||
87 | */ |
||
88 | public function nextPage(LengthAwarePaginator $paginator) |
||
89 | { |
||
90 | if (! $paginator->hasMorePages()) { |
||
91 | return null; |
||
92 | } |
||
93 | |||
94 | return $this->currentPage() + 1; |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Determine weather there is a next page. |
||
99 | * |
||
100 | * @param LengthAwarePaginator $paginator |
||
101 | * |
||
102 | * @return bool |
||
103 | */ |
||
104 | public function hasNextPage(LengthAwarePaginator $paginator): bool |
||
105 | { |
||
106 | return $this->nextPage($paginator) !== null; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Get the next page URL. |
||
111 | * |
||
112 | * @param LengthAwarePaginator $paginator |
||
113 | * |
||
114 | * @return string|void |
||
115 | */ |
||
116 | public function nextPageUrl(LengthAwarePaginator $paginator) |
||
117 | { |
||
118 | $nextPage = $this->nextPage($paginator); |
||
119 | |||
120 | if ($nextPage === null) { |
||
121 | return null; |
||
122 | } |
||
123 | |||
124 | return $this->pageUrl($nextPage); |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Get the previous page number. |
||
129 | * |
||
130 | * @return int|void|null |
||
131 | */ |
||
132 | public function previousPage() |
||
133 | { |
||
134 | if ($this->currentPage() <= 1) { |
||
135 | return null; |
||
136 | } |
||
137 | |||
138 | return $this->currentPage() - 1; |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * Determine wether there is a previous page. |
||
143 | * |
||
144 | * @return bool |
||
145 | */ |
||
146 | public function hasPreviousPage(): bool |
||
147 | { |
||
148 | return $this->previousPage() !== null; |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Get the previous page URL. |
||
153 | * |
||
154 | * @param bool $full Return the full version of the URL in for the first page |
||
155 | * Ex. /users/page/1 instead of /users |
||
156 | * |
||
157 | * @return string|void|null |
||
158 | */ |
||
159 | public function previousPageUrl($full = false): ?string |
||
160 | { |
||
161 | $previousPage = $this->previousPage(); |
||
162 | |||
163 | if ($previousPage === null) { |
||
164 | return null; |
||
165 | } |
||
166 | |||
167 | return $this->pageUrl($previousPage, $full); |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * Get all urls in an array. |
||
172 | * |
||
173 | * @param LengthAwarePaginator $paginator |
||
174 | * @param bool $full Return the full version of the URL in for the first page |
||
175 | * Ex. /users/page/1 instead of /users |
||
176 | * |
||
177 | * @return array |
||
178 | */ |
||
179 | public function allUrls(LengthAwarePaginator $paginator, $full = false): array |
||
180 | { |
||
181 | if (! $paginator->hasPages()) { |
||
182 | return []; |
||
183 | } |
||
184 | |||
185 | $urls = []; |
||
186 | $left = $this->getLeftPoint($paginator); |
||
187 | $right = $this->getRightPoint($paginator); |
||
188 | for ($page = $left; $page <= $right; $page++) { |
||
189 | $urls[$page] = $this->pageUrl($page, $full); |
||
190 | } |
||
191 | |||
192 | return $urls; |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * Get the left most point in the pagination element. |
||
197 | * |
||
198 | * @param LengthAwarePaginator $paginator |
||
199 | * @return int |
||
200 | */ |
||
201 | public function getLeftPoint(LengthAwarePaginator $paginator): int |
||
202 | { |
||
203 | $side = $paginator->onEachSide; |
||
204 | $current = $paginator->currentPage(); |
||
205 | $last = $paginator->lastPage(); |
||
206 | |||
207 | if (! empty($side)) { |
||
208 | $x = $current + $side; |
||
209 | $offset = $x >= $last ? $x - $last : 0; |
||
210 | $left = $current - $side - $offset; |
||
211 | } |
||
212 | if(!isset($left) || $left < 1){ |
||
213 | return 1; |
||
214 | } |
||
215 | return $left; |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * Get the right or last point of the pagination element. |
||
220 | * |
||
221 | * @param LengthAwarePaginator $paginator |
||
222 | * @return int |
||
223 | */ |
||
224 | public function getRightPoint(LengthAwarePaginator $paginator): int |
||
225 | { |
||
226 | $side = $paginator->onEachSide; |
||
227 | $current = $paginator->currentPage(); |
||
228 | $last = $paginator->lastPage(); |
||
229 | |||
230 | if (! empty($side)) { |
||
231 | $offset = $current <= $side ? $side - $current + 1 : 0; |
||
232 | $right = $current + $side + $offset; |
||
233 | } |
||
234 | if(!isset($right) || $right > $last){ |
||
235 | return $last; |
||
236 | } |
||
237 | return $right; |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * Render a plain html list with previous, next and all urls. The current page gets a current class on the list item. |
||
242 | * |
||
243 | * @param LengthAwarePaginator $paginator |
||
244 | * @param bool $full Return the full version of the URL in for the first page |
||
245 | * Ex. /users/page/1 instead of /users |
||
246 | * @param string $class Include class on pagination list |
||
247 | * Ex. <ul class="pagination"> |
||
248 | * @param bool $additionalLinks Include prev and next links on pagination list |
||
249 | * |
||
250 | * @return string |
||
251 | */ |
||
252 | |||
253 | public function renderPageList(LengthAwarePaginator $paginator, $full = false, $class = null, $additionalLinks = false): string |
||
254 | { |
||
255 | $urls = $this->allUrls($paginator, $full); |
||
256 | if ($class !== null) { |
||
257 | $class = " class=\"$class\""; |
||
258 | } |
||
259 | $listItems = "<ul{$class}>"; |
||
260 | if ($this->hasPreviousPage() && $additionalLinks) { |
||
261 | $listItems .= "<li class='page-item'> <a class='page-link' href=\"{$this->previousPageUrl()}\">«</a></li>"; |
||
262 | } |
||
263 | foreach ($urls as $i => $url) { |
||
264 | $pageNum = $i; |
||
265 | $css = ' class="page-item"'; |
||
266 | $link = "<a class='page-link' href=\"{$url}\">{$pageNum}</a>"; |
||
267 | if ($pageNum === $this->currentPage()) { |
||
268 | $css = ' class="page-item active"'; |
||
269 | $link = "<span class='page-link' href=\"{$url}\">{$pageNum}</span>"; |
||
270 | } |
||
271 | $listItems .= "<li{$css}>$link</li>"; |
||
272 | } |
||
273 | if ($this->hasNextPage($paginator) && $additionalLinks) { |
||
274 | $listItems .= "<li class='page-item'> <a class='page-link' href=\"{$this->nextPageUrl($paginator)}\">»</a></li>"; |
||
275 | } |
||
276 | $listItems .= '</ul>'; |
||
277 | return $listItems; |
||
278 | } |
||
279 | /** |
||
280 | * Render html link tags for SEO indication of previous and next page. |
||
281 | * |
||
282 | * @param LengthAwarePaginator $paginator |
||
283 | * @param bool $full Return the full version of the URL in for the first page |
||
284 | * Ex. /users/page/1 instead of /users |
||
285 | * |
||
286 | * @return string |
||
287 | */ |
||
288 | public function renderRelLinks(LengthAwarePaginator $paginator, $full = false): string |
||
289 | { |
||
290 | $urls = $this->allUrls($paginator, $full); |
||
291 | |||
292 | $linkItems = ''; |
||
293 | |||
294 | foreach ($urls as $i => $url) { |
||
295 | $pageNum = $i + 1; |
||
296 | |||
297 | switch ($pageNum - $this->currentPage()) { |
||
298 | case -1: |
||
299 | $linkItems .= "<link rel=\"prev\" href=\"{$url}\" />"; |
||
300 | break; |
||
301 | case 1: |
||
302 | $linkItems .= "<link rel=\"next\" href=\"{$url}\" />"; |
||
303 | break; |
||
304 | } |
||
305 | } |
||
306 | |||
307 | return $linkItems; |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * @param LengthAwarePaginator $paginator |
||
312 | * @param bool $full Return the full version of the URL in for the first page |
||
313 | * Ex. /users/page/1 instead of /users |
||
314 | * |
||
315 | * @return string |
||
316 | * @deprecated in favor of renderPageList. |
||
317 | */ |
||
318 | public function renderHtml(LengthAwarePaginator $paginator, $full = false): string |
||
319 | { |
||
320 | return $this->renderPageList($paginator, $full); |
||
321 | } |
||
322 | |||
323 | /** |
||
324 | * Generate a page URL, based on the request's current URL. |
||
325 | * |
||
326 | * @param int $page |
||
327 | * @param bool $full Return the full version of the URL in for the first page |
||
328 | * Ex. /users/page/1 instead of /users |
||
329 | * |
||
330 | * @return string|null |
||
331 | */ |
||
332 | public function pageUrl($page, $full = false): string |
||
333 | { |
||
334 | $currentPageUrl = $this->router->getCurrentRoute()->uri(); |
||
335 | |||
336 | $url = $this->addPageQuery(str_replace('{pageQuery?}', '', $currentPageUrl), $page, $full); |
||
337 | |||
338 | foreach ((new RouteParameterBinder($this->router->getCurrentRoute()))->parameters(app('request')) as $parameterName => $parameterValue) { |
||
0 ignored issues
–
show
|
|||
339 | $url = str_replace(['{'.$parameterName.'}', '{'.$parameterName.'?}'], $parameterValue, $url); |
||
340 | } |
||
341 | |||
342 | $query = Request::getQueryString(); |
||
343 | |||
344 | $query = $query |
||
345 | ? '?'.$query |
||
346 | : ''; |
||
347 | |||
348 | return $this->urlGenerator->to($url).$query; |
||
349 | } |
||
350 | |||
351 | /** |
||
352 | * Append the page query to a URL. |
||
353 | * |
||
354 | * @param string $url |
||
355 | * @param int $page |
||
356 | * @param bool $full Return the full version of the URL in for the first page |
||
357 | * Ex. /users/page/1 instead of /users |
||
358 | * |
||
359 | * @return string |
||
360 | */ |
||
361 | public function addPageQuery($url, $page, $full = false): string |
||
362 | { |
||
363 | // If the first page's URL is requested and $full is set to false, there's nothing to be added. |
||
364 | if ($page === 1 && ! $full) { |
||
365 | return $url; |
||
366 | } |
||
367 | |||
368 | return trim($url, '/')."/{$this->pageKeyword}/{$page}"; |
||
369 | } |
||
370 | |||
371 | /** |
||
372 | * Register the Route::paginate macro. |
||
373 | */ |
||
374 | public function registerMacros(): void |
||
375 | { |
||
376 | $pageKeyword = $this->pageKeyword; |
||
377 | $router = $this->router; |
||
378 | |||
379 | $router->macro('paginate', function ($uri, $action) use ($pageKeyword, $router) { |
||
380 | $route = null; |
||
381 | |||
382 | $router->group( |
||
383 | ['middleware' => 'Vipertecpro\PaginateRoute\SetPageMiddleware'], |
||
384 | function () use ($pageKeyword, $router, $uri, $action, &$route) { |
||
385 | $route = $router->get($uri.'/{pageQuery?}', $action)->where('pageQuery', $pageKeyword.'/[0-9]+'); |
||
386 | }); |
||
387 | |||
388 | return $route; |
||
389 | }); |
||
390 | } |
||
391 | } |
||
392 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: