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 | namespace Agavi\Database; |
||
3 | |||
4 | // +---------------------------------------------------------------------------+ |
||
5 | // | This file is part of the Agavi package. | |
||
6 | // | Copyright (c) 2005-2011 the Agavi Project. | |
||
7 | // | Based on the Mojavi3 MVC Framework, Copyright (c) 2003-2005 Sean Kerr. | |
||
8 | // | | |
||
9 | // | For the full copyright and license information, please view the LICENSE | |
||
10 | // | file that was distributed with this source code. You can also view the | |
||
11 | // | LICENSE file online at http://www.agavi.org/LICENSE.txt | |
||
12 | // | vi: set noexpandtab: | |
||
13 | // | Local Variables: | |
||
14 | // | indent-tabs-mode: t | |
||
15 | // | End: | |
||
16 | // +---------------------------------------------------------------------------+ |
||
17 | use Agavi\Exception\DatabaseException; |
||
18 | |||
19 | /** |
||
20 | * MysqlDatabase provides connectivity for the MySQL brand database. |
||
21 | * |
||
22 | * <b>Optional parameters:</b> |
||
23 | * |
||
24 | * # <b>database</b> - [none] - The database name. |
||
25 | * # <b>host</b> - [localhost] - The database host. |
||
26 | * # <b>method</b> - [normal] - How to read connection parameters. |
||
27 | * Possible values are normal, server, and |
||
28 | * env. The normal method reads them from |
||
29 | * the specified values. server reads them |
||
30 | * from $_SERVER where the keys to retrieve |
||
31 | * the values are what you specify the value |
||
32 | * as in the settings. env reads them from |
||
33 | * $_ENV and works like $_SERVER. |
||
34 | * # <b>password</b> - [none] - The database password. |
||
35 | * # <b>persistent</b> - [No] - Indicates that the connection should be |
||
36 | * persistent. |
||
37 | * # <b>username</b> - [none] - The database user. |
||
38 | * |
||
39 | * @package agavi |
||
40 | * @subpackage database |
||
41 | * |
||
42 | * @author Sean Kerr <[email protected]> |
||
43 | * @copyright Authors |
||
44 | * @copyright The Agavi Project |
||
45 | * |
||
46 | * @since 0.9.0 |
||
47 | * |
||
48 | * @version $Id$ |
||
49 | */ |
||
50 | class MysqlDatabase extends Database |
||
51 | { |
||
52 | /** |
||
53 | * Initialize this Database. |
||
54 | * |
||
55 | * @param DatabaseManager $databaseManager The database manager of this instance. |
||
56 | * @param array $parameters An assoc array of initialization params. |
||
57 | * |
||
58 | * @author David Zülke <[email protected]> |
||
59 | * @since 1.0.5 |
||
60 | */ |
||
61 | View Code Duplication | public function initialize(DatabaseManager $databaseManager, array $parameters = array()) |
|
0 ignored issues
–
show
|
|||
62 | { |
||
63 | parent::initialize($databaseManager, $parameters); |
||
64 | |||
65 | if ($matches = preg_grep('/^\s*SET\s+NAMES\b/i', (array)$this->getParameter('init_queries'))) { |
||
66 | throw new DatabaseException(sprintf('Depending on your MySQL server configuration, it may not be safe to use "SET NAMES" to configure the connection encoding, as the underlying MySQL client library will not be aware of the changed character set. As a result, string escaping may be applied incorrectly, leading to potential attack vectors in combination with certain multi-byte character sets such as GBK or Big5.' . "\n\n" . 'Please remove the "%s" statement from the "init_queries" configuration parameter in databases.xml and use the configuration parameter "charset" instead.' . "\n\n" . 'The associated PHP bug ticket http://bugs.php.net/47802 contains further information (describes PDO, but the basic issue is the same).', $matches[0])); |
||
67 | } |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Connect to the database. |
||
72 | * |
||
73 | * @throws <b>AgaviDatabaseException</b> If a connection could not be |
||
74 | * created. |
||
75 | * |
||
76 | * @author Sean Kerr <[email protected]> |
||
77 | * @since 0.9.0 |
||
78 | */ |
||
79 | protected function connect() |
||
0 ignored issues
–
show
connect uses the super-global variable $_SERVER which is generally not recommended.
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: // Bad
class Router
{
public function generate($path)
{
return $_SERVER['HOST'].$path;
}
}
// Better
class Router
{
private $host;
public function __construct($host)
{
$this->host = $host;
}
public function generate($path)
{
return $this->host.$path;
}
}
class Controller
{
public function myAction(Request $request)
{
// Instead of
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
// Better (assuming you use the Symfony2 request)
$page = $request->query->get('page', 1);
}
}
![]() connect uses the super-global variable $_ENV which is generally not recommended.
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: // Bad
class Router
{
public function generate($path)
{
return $_SERVER['HOST'].$path;
}
}
// Better
class Router
{
private $host;
public function __construct($host)
{
$this->host = $host;
}
public function generate($path)
{
return $this->host.$path;
}
}
class Controller
{
public function myAction(Request $request)
{
// Instead of
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
// Better (assuming you use the Symfony2 request)
$page = $request->query->get('page', 1);
}
}
![]() |
|||
80 | { |
||
81 | // determine how to get our |
||
82 | $method = $this->getParameter('method', 'normal'); |
||
83 | |||
84 | View Code Duplication | switch ($method) { |
|
0 ignored issues
–
show
This code seems to be duplicated across 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. ![]() |
|||
85 | case 'normal': |
||
86 | // get parameters normally |
||
87 | $database = $this->getParameter('database'); |
||
88 | $host = $this->getParameter('host', 'localhost'); |
||
89 | $password = $this->getParameter('password'); |
||
90 | $user = $this->getParameter('username'); |
||
91 | break; |
||
92 | |||
93 | case 'server': |
||
94 | // construct a connection string from existing $_SERVER values |
||
95 | // and extract them to local scope |
||
96 | $parameters = $this->loadParameters($_SERVER); |
||
97 | extract($parameters); |
||
98 | break; |
||
99 | |||
100 | case 'env': |
||
101 | // construct a connection string from existing $_ENV values |
||
102 | // and extract them to local scope |
||
103 | $parameters = $this->loadParameters($_ENV); |
||
104 | extract($parameters); |
||
105 | break; |
||
106 | |||
107 | default: |
||
108 | // who knows what the user wants... |
||
109 | $error = 'Invalid AgaviMySQLDatabase parameter retrieval method ' . |
||
110 | '"%s"'; |
||
111 | $error = sprintf($error, $method); |
||
112 | throw new DatabaseException($error); |
||
113 | } |
||
114 | |||
115 | // let's see if we need a persistent connection |
||
116 | $persistent = $this->getParameter('persistent', false); |
||
117 | |||
118 | View Code Duplication | if ($password === null) { |
|
0 ignored issues
–
show
This code seems to be duplicated across 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. ![]() |
|||
119 | if ($user === null) { |
||
120 | $args = array($host, null, null); |
||
121 | } else { |
||
122 | $args = array($host, $user, null); |
||
123 | } |
||
124 | } else { |
||
125 | $args = array($host, $user, $password); |
||
126 | } |
||
127 | |||
128 | if ($persistent) { |
||
129 | $this->connection = call_user_func_array('mysql_pconnect', $args); |
||
130 | } else { |
||
131 | $this->connection = call_user_func_array('mysql_connect', $args + array(true)); |
||
132 | } |
||
133 | |||
134 | // make sure the connection went through |
||
135 | if ($this->connection === false) { |
||
136 | // the connection's foobar'd |
||
137 | $error = 'Failed to create a AgaviMySQLDatabase connection'; |
||
138 | throw new DatabaseException($error); |
||
139 | } |
||
140 | |||
141 | View Code Duplication | if ($this->hasParameter('charset')) { |
|
0 ignored issues
–
show
This code seems to be duplicated across 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. ![]() |
|||
142 | if (!mysql_set_charset($this->getParameter('charset'), $this->connection)) { |
||
143 | $error = 'Failed to set charset "%s"'; |
||
144 | $error = sprintf($error, $this->getParameter('charset')); |
||
145 | throw new DatabaseException($error); |
||
146 | } |
||
147 | } |
||
148 | |||
149 | // select our database |
||
150 | View Code Duplication | if ($database !== null && !@mysql_select_db($database, $this->connection)) { |
|
0 ignored issues
–
show
This code seems to be duplicated across 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. ![]() |
|||
151 | // can't select the database |
||
152 | $error = 'Failed to select AgaviMySQLDatabase "%s"'; |
||
153 | $error = sprintf($error, $database); |
||
154 | throw new DatabaseException($error); |
||
155 | } |
||
156 | |||
157 | // since we're not an abstraction layer, we copy the connection |
||
158 | // to the resource |
||
159 | $this->resource =& $this->connection; |
||
160 | |||
161 | foreach ((array)$this->getParameter('init_queries') as $query) { |
||
162 | mysql_query($query, $this->connection); |
||
163 | } |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * Load connection parameters from an existing array. |
||
168 | * |
||
169 | * @param array $array An array containing the connection information. |
||
170 | * |
||
171 | * @return array An associative array of connection parameters. |
||
172 | * |
||
173 | * @author Sean Kerr <[email protected]> |
||
174 | * @since 0.9.0 |
||
175 | */ |
||
176 | protected function loadParameters(array $array) |
||
177 | { |
||
178 | // list of available parameters |
||
179 | $available = array('database', 'host', 'password', 'username'); |
||
180 | |||
181 | $parameters = array(); |
||
182 | |||
183 | foreach ($available as $parameter) { |
||
184 | $$parameter = $this->getParameter($parameter); |
||
185 | $parameters[$parameter] = ($$parameter != null) ? $array[$$parameter] : null; |
||
186 | } |
||
187 | |||
188 | return $parameters; |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Execute the shutdown procedure. |
||
193 | * |
||
194 | * @throws <b>AgaviDatabaseException</b> If an error occurs while shutting |
||
195 | * down this database. |
||
196 | * |
||
197 | * @author Sean Kerr <[email protected]> |
||
198 | * @since 0.9.0 |
||
199 | */ |
||
200 | public function shutdown() |
||
201 | { |
||
202 | if ($this->connection != null) { |
||
203 | @mysql_close($this->connection); |
||
0 ignored issues
–
show
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.
If you suppress an error, we recommend checking for the error condition explicitly: // For example instead of
@mkdir($dir);
// Better use
if (@mkdir($dir) === false) {
throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
![]() |
|||
204 | $this->connection = $this->resource = null; |
||
205 | } |
||
206 | } |
||
207 | } |
||
208 |
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.