Issues (3)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/Service/Builder.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * GpsLab component.
5
 *
6
 * @author    Peter Gribanov <[email protected]>
7
 * @copyright Copyright (c) 2011, Peter Gribanov
8
 * @license   http://opensource.org/licenses/MIT
9
 */
10
11
namespace GpsLab\Bundle\PaginationBundle\Service;
12
13
use Doctrine\ORM\QueryBuilder;
14
use GpsLab\Bundle\PaginationBundle\Exception\IncorrectPageNumberException;
15
use GpsLab\Bundle\PaginationBundle\Exception\OutOfRangeException;
16
use Symfony\Bundle\FrameworkBundle\Routing\Router;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
19
20
class Builder
21
{
22
    /**
23
     * @var Router
24
     */
25
    private $router;
26
27
    /**
28
     * The number of pages displayed in the navigation.
29
     *
30
     * @var int
31
     */
32
    private $max_navigate;
33
34
    /**
35
     * Name of URL parameter for page number.
36
     *
37
     * @var string
38
     */
39
    private $parameter_name;
40
41
    /**
42
     * @param Router $router         Router service
43
     * @param int    $max_navigate   Maximum showing navigation links in pagination
44
     * @param string $parameter_name Name of URL parameter for page number
45
     */
46 14
    public function __construct(Router $router, $max_navigate, $parameter_name)
47
    {
48 14
        $this->router = $router;
49 14
        $this->max_navigate = $max_navigate;
50 14
        $this->parameter_name = $parameter_name;
51 14
    }
52
53
    /**
54
     * @param int $total_pages  Total available pages
55
     * @param int $current_page The current page number
56
     *
57
     * @return Configuration
58
     */
59 7
    public function paginate($total_pages = 1, $current_page = 1)
60
    {
61 7
        return (new Configuration($total_pages, $current_page))
62 7
            ->setMaxNavigate($this->max_navigate)
63 7
            ->setPageLink(sprintf('?%s=%%d', $this->parameter_name))
64
        ;
65
    }
66
67
    /**
68
     * @param QueryBuilder $query        Query for select entities
69
     * @param int          $per_page     Entities per page
70
     * @param int          $current_page The current page number
71
     *
72
     * @return Configuration
73
     */
74 7
    public function paginateQuery(QueryBuilder $query, $per_page, $current_page = 1)
75
    {
76 7
        $counter = clone $query;
77
        $total = $counter
78 7
            ->select(sprintf('COUNT(%s)', current($query->getRootAliases())))
79 7
            ->getQuery()
80 7
            ->getSingleScalarResult()
81
        ;
82
83 7
        $total_pages = (int) ceil($total / $per_page);
84 7
        $current_page = $this->validateCurrentPage($current_page, $total_pages);
85
86
        $query
87 3
            ->setFirstResult(($current_page - 1) * $per_page)
88 3
            ->setMaxResults($per_page)
89
        ;
90
91 3
        return $this->paginate($total_pages, $current_page);
92
    }
93
94
    /**
95
     * @param Request $request        Current HTTP request
96
     * @param int     $total_pages    Total available pages
97
     * @param string  $parameter_name Name of URL parameter for page number
98
     * @param int     $reference_type The type of reference (one of the constants in UrlGeneratorInterface)
99
     *
100
     * @return Configuration
101
     */
102 4 View Code Duplication
    public function paginateRequest(
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
        Request $request,
104
        $total_pages,
105
        $parameter_name = '',
106
        $reference_type = UrlGeneratorInterface::ABSOLUTE_PATH
107
    ) {
108 4
        $parameter_name = $parameter_name ?: $this->parameter_name;
109 4
        $current_page = $this->validateCurrentPage($request->get($parameter_name), $total_pages);
110
111 1
        return $this->configureFromRequest(
112 1
            $request,
113 1
            $this->paginate($total_pages, $current_page),
114 1
            $parameter_name,
115 1
            $reference_type
116
        );
117
    }
118
119
    /**
120
     * @param Request      $request        Current HTTP request
121
     * @param QueryBuilder $query          Query for select entities
122
     * @param int          $per_page       Entities per page
123
     * @param string       $parameter_name Name of URL parameter for page number
124
     * @param int          $reference_type The type of reference (one of the constants in UrlGeneratorInterface)
125
     *
126
     * @return Configuration
127
     */
128 4 View Code Duplication
    public function paginateRequestQuery(
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
129
        Request $request,
130
        QueryBuilder $query,
131
        $per_page,
132
        $parameter_name = 'page',
133
        $reference_type = UrlGeneratorInterface::ABSOLUTE_PATH
134
    ) {
135 4
        $parameter_name = $parameter_name ?: $this->parameter_name;
136
137 4
        return $this->configureFromRequest(
138 4
            $request,
139 4
            $this->paginateQuery($query, $per_page, $request->get($parameter_name)),
140 1
            $parameter_name,
141 1
            $reference_type
142
        );
143
    }
144
145
    /**
146
     * @param mixed $current_page
147
     * @param int   $total_pages
148
     *
149
     * @return int
150
     */
151 11
    private function validateCurrentPage($current_page, $total_pages)
152
    {
153 11
        if ($current_page === null) {
154 1
            return 1;
155
        }
156
157 10
        if (!is_int($current_page) && (!is_string($current_page) || !ctype_digit($current_page))) {
158 2
            throw IncorrectPageNumberException::incorrect($current_page);
159
        }
160
161 8
        if ($current_page < 1 || $current_page > $total_pages) {
162 5
            throw OutOfRangeException::out((int) $current_page, $total_pages);
163
        }
164
165 3
        return (int) $current_page;
166
    }
167
168
    /**
169
     * @param Request       $request
170
     * @param Configuration $configuration
171
     * @param string        $parameter_name
172
     * @param int           $reference_type
173
     *
174
     * @return Configuration
175
     */
176 2
    private function configureFromRequest(
177
        Request $request,
178
        Configuration $configuration,
179
        $parameter_name,
180
        $reference_type
181
    ) {
182 2
        $route = $request->get('_route');
183 2
        $route_params = array_merge($request->query->all(), $request->get('_route_params', []));
184 2
        unset($route_params[$parameter_name]);
185
186
        return $configuration
187 2
            ->setPageLink(function ($number) use ($route, $route_params, $parameter_name, $reference_type) {
188 2
                $params = array_merge($route_params, [$parameter_name => $number]);
189
190 2
                return $this->router->generate($route, $params, $reference_type);
191 2
            })
192 2
            ->setFirstPageLink($this->router->generate($route, $route_params, $reference_type))
193
        ;
194
    }
195
}
196