Issues (108)

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.

lib/db.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
 * ownCloud - Documents App
4
 *
5
 * @author Victor Dubiniuk
6
 * @copyright 2013 Victor Dubiniuk [email protected]
7
 *
8
 * This file is licensed under the Affero General Public License version 3 or
9
 * later.
10
 */
11
12
namespace OCA\Documents;
13
14
/**
15
 * Generic DB class 
16
 */
17
18
abstract class Db {
19
	
20
	protected $data;
21
	
22
	protected $tableName;
23
	
24
	protected $insertStatement;
25
	
26
	protected $loadStatement;
27
	
28
	public function __construct($data = array()){
29
		$this->setData($data);
30
	}
31
	
32
	/**
33
	 * Insert current object data into database
34
	 * @return mixed
35
	 */
36
	public function insert(){
37
		$result = $this->execute($this->insertStatement);
38
		return $result;
39
	}
40
	
41
	/**
42
	 * Get id of the recently inserted record
43
	 * @return mixed
44
	 */
45
	public function getLastInsertId(){
46
		return \OC::$server->getDatabaseConnection()->lastInsertId($this->tableName);
47
	}
48
	
49
	/**
50
	 * Get single record by primary key
51
	 * @param int $value primary key value
52
	 * @return \OCA\Documents\Db
53
	 */
54
	public function load($value){
55
		if (!is_array($value)){
56
			$value = array($value);
57
		}
58
		
59
		$result = $this->execute($this->loadStatement, $value);
60
		$data = $result->fetch();
61
		if (!is_array($data)){
62
			$data = array();
63
		}
64
		$this->data = $data;
65
		return $this;
66
	}
67
	
68
	/**
69
	 * Get single record matching condition
70
	 * @param string $field for WHERE condition
71
	 * @param mixed $value matching value(s)
72
	 * @return \OCA\Documents\Db
73
	 * @throws Exception
74
	 */
75
	public function loadBy($field, $value){
76
		if (!is_array($value)){
77
			$value = array($value);
78
		}
79
		$result = $this->execute('SELECT * FROM ' . $this->tableName . ' WHERE `'. $field .'` =?', $value);
80
		$data = $result->fetchAll();
81
		if (!is_array($data) || !count($data)){
82
			$this->data = array();
83
		} elseif (count($data)!=1) {
84
			throw new Exception('Duplicate ' . $value . ' for the filed ' . $field);
85
		} else {
86
			$this->data = $data[0];
87
		}
88
		
89
		return $this;
90
	}
91
92
	/**
93
	 * Delete records matching the condition
94
	 * @param string $field for WHERE condition
95
	 * @param mixed $value matching value(s)
96
	 */
97
	public function deleteBy($field, $value){
98
		if (!is_array($value)){
99
			$value = array($value);
100
		}
101
		$count = count($value);
102 View Code Duplication
		if ($count===0){
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...
103
			return;
104
		} elseif ($count===1){
105
			$this->execute('DELETE FROM ' . $this->tableName . ' WHERE `'. $field .'` =?', $value);
106
		} else {
107
			$stmt = $this->buildInQuery($field, $value);
108
			$this->execute('DELETE FROM ' . $this->tableName . ' WHERE ' . $stmt, $value);
109
		}
110
	}
111
	
112
	/**
113
	 * Get all records from the table
114
	 * @return array
115
	 */
116
	public function getCollection(){
117
		$result = $this->execute('SELECT * FROM ' . $this->tableName);
118
		$data = $result->fetchAll();
119
		if (!is_array($data)){
120
			$data = array();
121
		}
122
		return $data;
123
	}
124
	
125
	/**
126
	 * Get array of matching records
127
	 * @param string $field for WHERE condition
128
	 * @param mixed $value matching value(s)
129
	 * @return array
130
	 */
131
	public function getCollectionBy($field, $value){
132
		if (!is_array($value)){
133
			$value = array($value);
134
		}
135
		$count = count($value);
136 View Code Duplication
		if ($count===0){
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...
137
			return array();
138
		} elseif ($count===1){
139
			$result = $this->execute('SELECT * FROM ' . $this->tableName . ' WHERE `'. $field .'` =?', $value);
140
		} else {
141
			$stmt = $this->buildInQuery($field, $value);
142
			$result = $this->execute('SELECT * FROM ' . $this->tableName . ' WHERE '. $stmt , $value);
143
		}
144
		
145
		$data = $result->fetchAll();
146
		if (!is_array($data)){
147
			$data = array();
148
		}
149
		return $data;
150
	}
151
	
152
	/**
153
	 * Get object data
154
	 * @return Array
155
	 */
156
	public function getData(){
157
		return $this->data;
158
	}
159
	
160
	/**
161
	 * Set object data
162
	 * @param array $data
163
	 */
164
	public function setData($data){
165
		$this->data = $data;
166
	}
167
	
168
	/**
169
	 * Check if there are any data in current object
170
	 * @return bool 
171
	 */
172
	public function hasData(){
173
		return count($this->data)>0;
174
	}
175
	
176
	/**
177
	 * Build placeholders for the query with variable input data 
178
	 * @param string $field field name
179
	 * @param Array $array data
180
	 * @return String `field` IN (?, ?...) placeholders matching the number of elements in array
181
	 */
182
	protected function buildInQuery($field, $array){
183
		$count = count($array);
184
		$placeholders = array_fill(0, $count, '?');
185
		$stmt = implode(', ', $placeholders);
186
		return '`' . $field . '` IN ('  . $stmt . ')';
187
	}
188
	
189
	/**
190
	 * Execute a query on database
191
	 * @param string $statement query to be executed
192
	 * @param mixed $args value(s) for the query. 
193
	 * If omited the query will be run on the current object $data
194
	 * @return mixed (array/false)
195
	 */
196
	protected function execute($statement, $args = null){
197
		$query = \OC::$server->getDatabaseConnection()->prepare($statement);
198
		
199
		if (!is_null($args)){
200
			$result = $query->execute($args);
201
		} elseif (count($this->data)){
202
			$result = $query->execute($this->data);
203
		} else {
204
			$result = $query->execute();
205
		}
206
		
207
		return $result ? $query : false;
208
	}
209
	
210
	public function __call($name, $arguments){
211
		if (substr($name, 0, 3) === 'get'){
212
			$requestedProperty = substr($name, 3);
213
			$property = strtolower(preg_replace('/(.)([A-Z])/', "$1_$2", $requestedProperty));
214
			if (isset($this->data[$property])){
215
				return $this->data[$property];
216
			}
217
		}
218
		return null;
219
	}
220
}
221