Issues (115)

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/Adapter/CodeigniterQueryBuilder.php (15 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
namespace SimpleORM\Adapter;
4
5
/**
6
 * Description of QueryBuilder
7
 *
8
 * @author Dmitriy
9
 */
10
class CodeigniterQueryBuilder implements \SimpleORM\QueryBuilderInterface
11
{
12
	
13
	protected $adapter;
14
	
15
	protected $database;
16
	
17
	protected $TableName;
18
	
19
	protected $bind_marker = '?';
20
			
21
	function __construct(\CI_DB_mysqli_driver $db)
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
22
	{
23
		
24
		$this->adapter = $db;
25
		
26
	}
27
28
	/**
29
	 * "Smart" Escape String
30
	 *
31
	 * Escapes data based on type
32
	 * Sets boolean and null types
33
	 *
34
	 * @access	public
35
	 * @param	string
36
	 * @return	mixed
37
	 */
38
	protected function escape($str)
39
	{
40
		if (is_string($str)) {
41
			$str = "'" . $this->escape_str($str) . "'";
42
		} elseif (is_bool($str)) {
43
			$str = ($str === FALSE) ? 0 : 1;
44
		} elseif (is_null($str)) {
45
			$str = 'NULL';
46
		}
47
48
		return $str;
49
	}
50
	
51
	public function update($table,array $data,$where = []){
52
		return $this->adapter->update($table,$data,$where);
53
	}
54
	
55
	public function insert($table,array $data)
56
	{
57
		return $this->adapter->insert($table,$data);
58
	}
59
	
60
	public function insert_id()
61
	{
62
		return $this->adapter->insert_id();
63
	}
64
65
	public function delete($table,$where = []){
66
		return $this->adapter->delete($table,$where);
67
	}
68
69
	protected function escape_str($str, $like = FALSE)
70
	{
71
72
		if(!$like){
73
			return $this->adapter->escape_str($str);
74
		}
75
		else{
76
			return $this->adapter->escape_like_str($str);
77
		}	
78
		
79
		
80
//		if (is_array($str)) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% 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...
81
//			foreach ($str as $key => $val) {
82
//				$str[$key] = $this->escape_str($val, $like);
83
//			}
84
//
85
//			return $str;
86
//		}
87
//
88
//		if (function_exists('mysql_real_escape_string') AND is_resource($this->adapter->dbserver->rsLink)) {
89
//			$str = mysql_real_escape_string($str, $this->adapter->dbserver->rsLink);
90
//		} elseif (function_exists('mysql_escape_string')) {
91
//			$str = mysql_escape_string($str);
92
//		} else {
93
//			$str = addslashes($str);
94
//		}
95
//
96
//		// escape LIKE condition wildcards
97
//		if ($like === TRUE) {
98
//			$str = str_replace(array('%', '_'), array('\\%', '\\_'), $str);
99
//		}
100
//
101
//		return $str;
102
	}
103
104
	
105
	/**
106
	 * Проверка корректности поля
107
	 */
108
	protected function fieldCheck($field)
109
	{
110
		if (empty($field)) {
111
			throw new HttpException('You cannot have an empty field name.');
112
		}
113
114
		if (strpos($field, '.') === false) {
115
			return $this->TableName . '.' . $field;
116
		}
117
118
		return $field;
119
	}	
120
	
121
	/**
122
	 * 
123
	 * @param type $param
0 ignored issues
show
There is no parameter named $param. 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...
124
	 */
125
	public function getResultQuery($table,\SimpleORM\ISpecificationCriteria $Criteria ) {
126
		
127
		$this->setTable($table);
128
		
129
		$res = $this->buildQuery(
130
				$Criteria->getWhere(), 
131
				$Criteria->getLimit(), 
132
				$Criteria->getOfset(), 
133
				$Criteria->getJoins(), 
134
				$Criteria->getOrder(), 
135
				$Criteria->getManualJoins(), 
136
				$Criteria->getGroup(), 
137
				$Criteria->getManualWheres(), 
138
				$Criteria->getWhereType(),
139
				$Criteria->getManualSelect()
140
				);
141
142
		return $res;		
143
	}
144
145
	protected function setTable($table){
146
		if(preg_match('~(.*?)\.(.*?)$~is',$table,$m)){
147
			$this->database = $m[1];
148
			$this->TableName = $m[2];
149
		}
150
		else{
151
			$this->TableName = $table;
152
		}		
153
	}	
154
	
155
	
156
	/**
157
	 * Создает селект не только для основной таблицы но и для приджойненых таблиц
158
	 * @param type $joins
159
	 * @return type
160
	 */
161
	protected function createSelect(array $joins,$manualSelect)
162
	{
163
		$s = !empty($manualSelect) ? $manualSelect :"`" . $this->TableName . '`.*';
164
		
165
		foreach ($joins as $table => $join) {
166
			$table = isset($join['alias']) ? "`{$join['alias']}`": $table;
167
			$s .= ", $table.*";
168
		}
169
		return $s;
170
	}	
171
172
	
173
	/**
174
	 * Получение записей по условию
175
	 * @param type $where
176
	 * @param type $limit
177
	 * @param type $offset
178
	 * @param type $joins
179
	 * @param type $order
180
	 * @param type $manualJoins
181
	 * @param type $group
182
	 * @param type $manualWheres
183
	 * @param type $whereType
184
	 * @return boolean
185
	 * @throws \PDOException
186
	 */
187
	protected function buildQuery($where = array(), $limit = 25, $offset = 0, $joins = array(), $order = array(), $manualJoins = array(), $group = null, $manualWheres = array(), $whereType = 'AND', $manualSelect = '')
188
	{
189
		$table = !empty($this->database)? "`{$this->database}`.".$this->TableName : $this->TableName;
190
		$query = 'SELECT ' . $this->createSelect($joins, $manualSelect) . " FROM `".$table."`";
191
		//$countQuery = "SELECT COUNT(*) AS cnt FROM `{$this->database}`.".$this->getTableName();
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% 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...
192
193
		$wheres = array();
194
		$params = array();
195
		foreach ($where as $key => $value) {
196
			$key = $this->fieldCheck($key);
197
198
			if (!is_array($value)) {
199
				$params[] = $value;
200
				$wheres[] = $key . ' = ?';
201
			} else {
202
				if (isset($value['operator'])) {
203
					if (is_array($value['value'])) {
204
						if ($value['operator'] == 'between') {
205
							$params[] = $value['value'][0];
206
							$params[] = $value['value'][1];
207
							$wheres[] = $key . ' BETWEEN ? AND ?';
208
						} elseif ($value['operator'] == 'IN') {
209
							$in = array();
210
211
							foreach ($value['value'] as $item) {
212
								$params[] = $item;
213
								$in[] = '?';
214
							}
215
216
							$wheres[] = $key . ' IN (' . implode(', ', $in) . ') ';
217
						} else {
218
							$ors = array();
219
							foreach ($value['value'] as $item) {
220
								if ($item == 'null') {
221 View Code Duplication
									switch ($value['operator']) {
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...
222
										case '!=':
223
											$ors[] = $key . ' IS NOT NULL';
224
											break;
225
226
										case '==':
227
										default:
228
											$ors[] = $key . ' IS NULL';
229
											break;
230
									}
231
								} else {
232
									$params[] = $item;
233
									$ors[] = $this->fieldCheck($key) . ' ' . $value['operator'] . ' ?';
234
								}
235
							}
236
							$wheres[] = '(' . implode(' OR ', $ors) . ')';
237
						}
238
					} else {
239
						if ($value['operator'] == 'like') {
240
							$params[] = '%' . $value['value'] . '%';
241
							$wheres[] = $key . ' ' . $value['operator'] . ' ?';
242
						} else {
243
							if ($value['value'] === 'null') {
244 View Code Duplication
								switch ($value['operator']) {
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...
245
									case '!=':
246
										$wheres[] = $key . ' IS NOT NULL';
247
										break;
248
249
									case '==':
250
									default:
251
										$wheres[] = $key . ' IS NULL';
252
										break;
253
								}
254
							} else {
255
								$params[] = $value['value'];
256
								$wheres[] = $key . ' ' . $value['operator'] . ' ?';
257
							}
258
						}
259
					}
260
				} else {
261
					$wheres[] = $key . ' IN (' . implode(', ', array_map(array($this, 'escape'), $value)) . ')';
262
				}
263
			}
264
		}
265
266
		if (count($joins)) {
267
			foreach ($joins as $table => $join) {
268
				$type = isset($join['type'])?$join['type']:'INNER';
269
				$query .= ' '. $type.' JOIN `' . $table . '` as `' . $join['alias'] . '` ON ' . $join['on'] . ' ';
270
				//$countQuery .= ' '.$type.' JOIN ' . $table . ' ' . $join['alias'] . ' ON ' . $join['on'] . ' ';
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% 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...
271
			}
272
		}
273
274
		if (count($manualJoins)) {
275
			foreach ($manualJoins as $join) {
276
				$query .= ' ' . $join . ' ';
277
				//$countQuery .= ' ' . $join . ' ';
278
			}
279
		}
280
281
		$hasWhere = false;
282
		if (count($wheres)) {
283
			$hasWhere = true;
284
			$query .= ' WHERE (' . implode(' ' . $whereType . ' ', $wheres) . ')';
285
			//$countQuery .= ' WHERE (' . implode(' ' . $whereType . ' ', $wheres) . ')';
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% 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...
286
		}
287
288
		if (count($manualWheres)) {
289
			foreach ($manualWheres as $where) {
290
				if (!$hasWhere) {
291
					$hasWhere = true;
292
					$query .= ' WHERE ';
293
					//$countQuery .= ' WHERE ';
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% 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...
294
				} else {
295
					$query .= ' ' . $where['type'] . ' ';
296
					//$countQuery .= ' ' . $where['type'] . ' ';
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% 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...
297
				}
298
299
				$query .= ' ' . $where['query'];
300
				//$countQuery .= ' ' . $where['query'];
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
301
302
				if (isset($where['params'])) {
303
					foreach ($where['params'] as $param) {
304
						$params[] = $param;
305
					}
306
				}
307
			}
308
		}
309
310
		if (!is_null($group)) {
311
			$query .= ' GROUP BY ' . $group . ' ';
312
		}
313
314
		if (count($order)) {
315
			$orders = array();
316
			if (is_string($order) && $order == 'rand') {
317
				$query .= ' ORDER BY RAND() ';
318
			} else {
319
				foreach ($order as $key => $value) {
0 ignored issues
show
The expression $order of type object<SimpleORM\Adapter\type>|array|string is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
320
					$orders[] = $this->fieldCheck($key) . ' ' . $value;
321
				}
322
323
				$query .= ' ORDER BY ' . implode(', ', $orders);
324
			}
325
		}
326
327
		if ($limit) {
328
			$query .= ' LIMIT ' . $limit;
329
		}
330
331
		if ($offset) {
332
			$query .= ' OFFSET ' . $offset;
333
		}
334
335
336
337
338
//		try {
0 ignored issues
show
Unused Code Comprehensibility introduced by
51% 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...
339
//			$countQuery = $this->compile_binds($countQuery, $params);
340
//			$res = $this->adapter->getRow($countQuery);
341
//			$count = (int) $res['cnt'];
342
//		} catch(\PDOException $ex) {
343
//			$count = 0;
344
//		}
345
346
347
		try {
348
			//$query = $this->compile_binds($query, $params);
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% 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...
349
			
350
			return $this->adapter->query($query,$params);
351
			//ed( $this->adapter->query($query) ,1);
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% 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...
352
//			if ($res = $this->adapter->getRows($query)) {
353
//				$rtn = array();
354
//
355
//				foreach ($res as $data) {
356
//					$rtn[] = $this->mapper->buildEntity($data);
357
//					unset($data);
358
//				}
359
//
360
//				return array('items' => $rtn, 'count' => $count);
361
//			} else {
362
//				return false;
363
//			}
364
		} catch(\PDOException $ex) {
365
			throw $ex;
366
		}
367
	}	
368
	
369
	
370
	/**
371
	 * применение значений в стиле PDO
372
	 * 
373
	 * @param type $sql
374
	 * @param type $binds
375
	 * @return type
376
	 */
377
	protected function compile_binds($sql, $binds)
378
	{
379
		if (strpos($sql, $this->bind_marker) === FALSE) {
380
			return $sql;
381
		}
382
383
		if (!is_array($binds)) {
384
			$binds = array($binds);
385
		}
386
387
		// Get the sql segments around the bind markers
388
		$segments = explode($this->bind_marker, $sql);
389
390
		// The count of bind should be 1 less then the count of segments
391
		// If there are more bind arguments trim it down
392
		if (count($binds) >= count($segments)) {
393
			$binds = array_slice($binds, 0, count($segments) - 1);
394
		}
395
396
		// Construct the binded query
397
		$result = $segments[0];
398
		$i = 0;
399
		foreach ($binds as $bind) {
400
			$result .= $this->escape($bind);
401
			$result .= $segments[++$i];
402
		}
403
404
		return $result;
405
	}	
406
	
407
	public function endTransaction() {
408
		$this->adapter->trans_start();
409
	}
410
411
	public function startTransaction() {
412
		$this->adapter->trans_complete();
413
	}
414
415
		
416
}
417