Issues (42)

Security Analysis    no request data  

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/DSN.php (5 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
 * This file is part of Yolk - Gamer Network's PHP Framework.
4
 *
5
 * Copyright (c) 2014 Gamer Network Ltd.
6
 * 
7
 * Distributed under the MIT License, a copy of which is available in the
8
 * LICENSE file that was bundled with this package, or online at:
9
 * https://github.com/gamernetwork/yolk-database
10
 */
11
12
namespace yolk\database;
13
14
use yolk\database\exceptions\ConfigurationException;
15
16
/**
17
 * Describes database connection details.
18
 *
19
 * @property-read string $type type of database being connected to
20
 * @property-read string $host hostname or ip address of database server
21
 * @property-read string $port network port to connect on
22
 * @property-read string $user user name used for authentication
23
 * @property-read string $pass password used for authentication
24
 * @property-read string $db   name of the database schema to use
25
 * @property-read array  $options array of database specific options
26
 */
27
class DSN {
28
29
	const TYPE_MYSQL  = 'mysql';
30
	const TYPE_PGSQL  = 'pgsql';
31
	const TYPE_SQLITE = 'sqlite';
32
33
	protected $config;
34
35
	public static function fromString( $config ) {
36
37
		// parse the string into some components
38
		$parts = parse_url(urldecode((string) $config));
39
40
		// no point continuing if it went wrong
41
		if( !$parts || empty($parts['scheme']) )
42
			throw new ConfigurationException('Invalid DSN string: '. $config);
43
44
		// use a closure to save loads of duplicate logic
45
		$select = function( $k, array $arr ) {
46
			return isset($arr[$k]) ? $arr[$k] : null;
47
		};
48
49
		// construct a well-formed array from the available components
50
		$config = array(
51
			'type'    => $select('scheme', $parts),
52
			'host'    => $select('host', $parts),
53
			'port'    => $select('port', $parts),
54
			'user'    => $select('user', $parts),
55
			'pass'    => $select('pass', $parts),
56
			'db'      => trim($select('path', $parts), '/'),
57
			'options' => [],
58
		);
59
60
		if( isset($parts['query']) ) {
61
			parse_str($parts['query'], $config['options']);
62
		}
63
64
		return new static($config);
65
66
	}
67
68
	public static function fromJSON( $config ) {
69
70
		$config = json_decode($config, true);
71
72
		if( !$config )
73
			throw new ConfigurationException('Invalid JSON configuration');
74
75
		return new static($config);
76
77
	}
78
79
	/**
80
	 * Create a DSN from an array of parameters.
81
	 * scheme - type of database (mysql, pgsql, sqlite) required
82
	 * host - hostname of database server
83
	 * port - network port to connect on
84
	 * user - user to connect as
85
	 * pass - user's password
86
	 * db - name of the database schema to connect to
87
	 * options - an array of database specific options
88
	 * @param array $dsn
0 ignored issues
show
There is no parameter named $dsn. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
89
	 */
90
	public function __construct( array $config ) {
91
92
		if( empty($config['type']) )
93
			throw new ConfigurationException('No database type specified');
94
95
		$config = $config + array(
96
			'host'    => 'localhost',
97
			'port'    => null,
98
			'user'    => null,
99
			'pass'    => null,
100
			'db'      => null,
101
			'options' => [],
102
		);
103
104
		$this->configure($config);
105
106
	}
107
108
	public function isMySQL() {
109
		return $this->config['type'] == static::TYPE_MYSQL;
110
	}
111
112
	public function isPgSQL() {
113
		return $this->config['type'] == static::TYPE_PGSQL;
114
	}
115
116
	public function isSQLite() {
117
		return $this->config['type'] == static::TYPE_SQLITE;
118
	}
119
120
	/**
121
	 * Dynamic property access.
122
	 * @param  string $key
123
	 * @return mixed
124
	 */
125
	public function __get( $key ) {
126
		return isset($this->config[$key]) ? $this->config[$key] : null;
127
	}
128
129
	/**
130
	 * Dynamic property access.
131
	 * @param  string  $key
132
	 * @return boolean
133
	 */
134
	public function __isset( $key ) {
135
		return isset($this->config[$key]);
136
	}
137
138
	/**
139
	 * Convert the DSN into a URI-type string.
140
	 * @return string
141
	 */
142
	public function toString() {
143
144
		$str = $this->config['type']. '://';
145
146 View Code Duplication
		if( $this->config['user'] ) {
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.

Loading history...
147
			$str .= $this->config['user'];
148
			if( $this->config['pass'] )
149
				$str .= ':'. $this->config['pass'];
150
			$str .= '@';
151
		}
152
153 View Code Duplication
		if( $this->config['host'] ) {
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.

Loading history...
154
			$str .= $this->config['host'];
155
			if( $this->config['port'] )
156
				$str .= ':'. $this->config['port'];
157
		}
158
159
		$str .= '/'. $this->config['db'];
160
161
		if( $this->config['options'] ) {
162
			$str .= '?';
163
			foreach( $this->config['options'] as $k => $v ) {
164
				$str .= "$k=>$v&";
165
			}
166
			$str = substr($str, 0, -1);
167
		}
168
169
		return $str;
170
171
	}
172
173
	/**
174
	 * Return a connection string for use by PDO.
175
	 * @return string
176
	 */
177
	public function getConnectionString() {
178
		return $this->config['pdo'];
179
	}
180
181
	/**
182
	 * Ensure the dsn configuration is valid.
183
	 * @param  array  $config
184
	 * @return void
185
	 */
186
	protected function configure( array $config ) {
187
188
		if( !$config['db'] )
189
			throw new ConfigurationException('No database schema specified');
190
191
		switch( $config['type'] ) {
192
193
			case static::TYPE_MYSQL:
194
				$this->config = $this->configureMySQL($config);
195
				break;
196
197
			case static::TYPE_PGSQL:
198
				$this->config = $this->configureMySQL($config);
199
				break;
200
201
			case static::TYPE_SQLITE:
202
				$this->config = $this->configureMySQL($config);
203
				break;
204
205
			default:
206
				throw new ConfigurationException('Invalid database type: '. $config['type']);
207
208
		}
209
210
	}
211
212
	/**
213
	 * Configure a MySQL DSN.
214
	 * @param  array  $config
215
	 * @return array
216
	 */
217 View Code Duplication
	protected function configureMySQL( array $config ) {
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...
218
		
219
		if( !$config['port'] )
220
			$config['port'] = 3306;
221
222
		// construct a MySQL PDO connection string
223
		$config['pdo'] = sprintf(
224
			"mysql:host=%s;port=%s;dbname=%s",
225
			$config['host'],
226
			$config['port'],
227
			$config['db']
228
		);
229
230
		return $config;
231
232
	}
233
234
	/**
235
	 * Configure a PostgreSQL DSN.
236
	 * @param  array  $config
237
	 * @return array
238
	 */
239 View Code Duplication
	protected function configurePgSQL( array $config ) {
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...
240
		
241
		if( !$config['port'] )
242
			$config['port'] = 5432;
243
244
		// construct a PgSQL PDO connection string
245
		$config['pdo'] = sprintf(
246
			"pgsql:host=%s;port=%s;dbname=%s",
247
			$config['host'],
248
			$config['port'],
249
			$config['db']
250
		);
251
252
		return $config;
253
254
	}
255
256
	/**
257
	 * Configure a SQLite DSN.
258
	 * @param  array  $config
259
	 * @return array
260
	 */
261
	protected function configureSQLite( array $config ) {
262
263
		// these should always be null as they're invalid for SQLite connections
264
		$config['host'] = 'localhost';
265
		$config['port'] = null;
266
		$config['user'] = null;
267
		$config['pass'] = null;
268
269
		// construct a SQLite PDO connection string
270
		$config['pdo'] = sprintf(
271
			'sqlite::%s',
272
			$config['db']
273
		);
274
275
		return $config;
276
277
	}
278
279
}
280
281
// EOF