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/query/Select.php (4 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) 2015 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\query;
13
14
use yolk\contracts\database\DatabaseConnection;
15
16
/**
17
 * Generic.
18
 */
19
class Select extends BaseQuery {
20
21
	protected $cols;
22
	protected $distinct;
23
	protected $from;
24
	protected $group;
25
	protected $having;
26
27
28
	public function __construct( DatabaseConnection $db ) {
29
		parent::__construct($db);
30
		$this->cols     = [];
31
		$this->distinct = false;
32
		$this->from     = '';
33
		$this->group    = [];
34
		$this->having   = [];
35
	}
36
37
	public function cols( $columns = ['*'] ) {
38
39
		// default to everything
40
		if( !$columns )
0 ignored issues
show
Bug Best Practice introduced by
The expression $columns of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
41
			$columns = ['*'];
42
43
		// if we don't have an array of columns then they were specified as individual arguments
44
		elseif( !is_array($columns) )
45
			$columns = func_get_args();
46
47
		// $columns = [
48
		// 	'column',
49
		// 	['column', 'alias'],
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
50
		// 	'id',
51
		// 	['related_id', 'related'],
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
52
		// ];
53
54
		$this->cols = $columns;
55
56
		return $this;
57
58
	}
59
60
	// use raw columns statement
61
	public function colsRaw( $sql ) {
62
		$this->cols = $sql;
63
		return $this;
64
	}
65
66
	public function distinct( $distinct = true ) {
67
		$this->distinct = (bool) $distinct;
68
		return $this;
69
	}
70
71
	public function from( $table ) {
72
		$this->from = $this->quoteIdentifier($table);
73
		return $this;
74
	}
75
76
	public function fromRaw( $sql ) {
77
		$this->from = $sql;
78
		return $this;
79
	}
80
81
	public function groupBy( $columns ) {
82
83
		if( !is_array($columns) )
84
			$columns = [$columns];
85
86
		foreach( $columns as $column ) {
87
			$this->group[] = $this->quoteIdentifier($column);
88
		}
89
90
		return $this;
91
92
	}
93
94
	public function having( $having ) {
95
		$this->having = [$having];
96
	}
97
98
	public function __call( $method, $args ) {
99
100
		if( !in_array($method, ['getOne', 'getCol', 'getRow', 'getAssoc', 'getAll']) )
101
			throw new \BadMethodCallException("Unknown Method: {$method}");
102
103
		return $this->db->$method(
104
			$this->__toString(),
105
			$this->params
106
		);
107
108
	}
109
110
	public function compile() {
111
112
		$cols = $this->cols;
113
114
		if( is_array($cols) )
115
			$cols = $this->compileCols($cols);
116
117
		return array_merge(
118
			[
119
				($this->distinct ? 'SELECT DISTINCT' : 'SELECT'). ' '. $cols,
120
				'FROM '. $this->from,
121
			],
122
			$this->compileJoins(),
123
			$this->compileWhere(),
124
			$this->compileGroupBy(),
125
			$this->having,
126
			$this->compileOrderBy(),
127
			$this->compileOffsetLimit()
128
		);
129
130
	}
131
132
	protected function compileCols( array $cols ) {
133
134
		foreach( $cols as &$col ) {
135
136
			// if column is an array is should have two elements
137
			// the first being the column name, the second being the alias
138
			if( is_array($col) ) {
139
				list($column, $alias) = $col;
140
				$col = sprintf(
141
					'%s AS %s',
142
					$this->quoteIdentifier($column),
143
					$this->db->quoteIdentifier($alias)
144
				);
145
			}
146
			else {
147
				$col = $this->quoteIdentifier($col);
148
			}
149
150
		}
151
152
		return implode(', ', $cols);
153
154
	}
155
156
	protected function compileGroupBy() {
157
158
		$sql = [];
159
160
		if( $this->group )
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->group of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
161
			$sql[] = 'GROUP BY '. implode(', ', $this->group);
162
163
		return $sql;
164
165
	}
166
167
}
168
169
// EOF