Issues (16)

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.

BaseRecord.php (1 issue)

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
 * KumbiaPHP web & app Framework.
4
 *
5
 * LICENSE
6
 *
7
 * This source file is subject to the new BSD license that is bundled
8
 * with this package in the file LICENSE.txt.
9
 * It is also available through the world-wide-web at this URL:
10
 * http://wiki.kumbiaphp.com/Licencia
11
 * If you did not receive a copy of the license and are unable to
12
 * obtain it through the world-wide-web, please send an email
13
 * to [email protected] so we can send you a copy immediately.
14
 *
15
 * @category   Kumbia
16
 *
17
 * @copyright  2005 - 2016 Kumbia Team (http://www.kumbiaphp.com)
18
 * @license    http://wiki.kumbiaphp.com/Licencia     New BSD License
19
 */
20
namespace Kumbia\ActiveRecord;
21
22
use \PDO;
23
use \PDOStatement;
24
use \PDOException;
25
26
/**
27
 * Base del ORM ActiveRecord.
28
 */
29
abstract class BaseRecord
30
{
31
    
32
    public const VERSION = '0.5.3';
33
34
    /**
35
     * Database por defecto usa default.
36
     *
37
     * @var string
38
     */
39
    protected static $database = 'default';
40
41
    /**
42
     * Nombre de la tabla.
43
     *
44
     * @var string
45
     */
46
    protected static $table = '';
47
48
    /**
49
     * PK por defecto, si no existe mira en metadata.
50
     *
51
     * @var string|null
52
     */
53
    protected static $pk = 'id';
54
    
55
    /**
56
     * Metadata.
57
     *
58
     * @var Metadata\Metadata|null
59
     */
60
    protected static $metadata;
61
62
    /**
63
     * Constructor.
64
     *
65
     * @param array $data
66
     */
67
    public function __construct(array $data = [])
68
    {
69
        $this->dump($data);
70
    }
71
72
    /**
73
     * Get the Primary Key value for the object
74
     * @return string
75
     */
76
    public function pk(): string
77
    {
78
        return $this->{static::$pk};
79
    }
80
81
    /**
82
     * Cargar datos al objeto.
83
     *
84
     * @param array $data
85
     */
86
    public function dump(array $data = []): void
87
    {
88
        foreach ($data as $k => $v) {
89
            $this->$k = $v;
90
        }
91
    }
92
93
    /**
94
     * Listado de los campos.
95
     *
96
     * @return string[]
97
     */
98
    public function getFields(): array
99
    {
100
        return \array_keys(\get_object_vars($this));
101
    }
102
103
    /**
104
     * Alias de los campos.
105
     *
106
     * @return string[]
107
     */
108
    public function getAlias(): array
109
    {
110
        //$humanize = function ()
111
        return \array_map('\ucwords', $this->getFields());
112
    }
113
114
    /**
115
     * Verifica que PK este seteado.
116
     *
117
     * @return bool
118
     */
119
    protected function hasPK(): bool
120
    {
121
        return isset($this->{static::$pk});
122
    }
123
124
    /**
125
     * Get the name of the primary key
126
     *
127
     * @return string
128
     */
129
    public static function getPK(): string
130
    {
131
        return static::$pk ?? static::$pk = self::metadata()->getPK();
132
    }
133
134
    /**
135
     * Obtiene nombre de tabla en la bd.
136
     *
137
     * @return string smallcase del nombre de la clase
138
     */
139
    public static function getTable(): string
140
    {
141
        if (static::$table) {
142
            return static::$table;
143
        }
144
        
145
        $split = \explode('\\', static::class);
146
        $table = \preg_replace('/[A-Z]/', '_$0', \lcfirst(\end($split)));
147
148
        return static::$table = \strtolower($table);
149
    }
150
151
    /**
152
     * Obtiene el schema al que pertenece.
153
     *
154
     * @return string
155
     */
156
    public static function getSchema(): string
157
    {
158
        return '';
159
    }
160
161
    /**
162
     * Obtiene la combinación de esquema y tabla.
163
     *
164
     * @return string
165
     */
166
    public static function getSource(): string
167
    {
168
        $source = static::getTable();
169
        if ($schema = static::getSchema()) {
170
            $source = "$schema.$source";
171
        }
172
173
        return $source;
174
    }
175
176
    /**
177
     * Obtiene la conexión que se utilizará (contenidas en databases.php).
178
     *
179
     * @return string
180
     */
181
    public static function getDatabase(): string
182
    {
183
        return static::$database;
184
    }
185
186
    /**
187
     * Obtiene metadatos.
188
     *
189
     * @return Metadata\Metadata
190
     */
191
    public static function metadata(): Metadata\Metadata
192
    {
193
        return static::$metadata ?? 
194
                static::$metadata = Metadata\Metadata::get(
0 ignored issues
show
Documentation Bug introduced by Joanhey
It seems like \Kumbia\ActiveRecord\Met...), static::getSchema()) of type object<self> is incompatible with the declared type object<Kumbia\ActiveReco...Metadata\Metadata>|null of property $metadata.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
195
                    static::getDatabase(),
196
                    static::getTable(),
197
                    static::getSchema()
198
        );
199
    }
200
201
    /**
202
     * Obtiene manejador de conexion a la base de datos.
203
     *
204
     * @return \PDO
205
     */
206
    protected static function dbh(): \PDO
207
    {
208
        return Db::get(static::getDatabase());
209
    }
210
211
    /**
212
     * Consulta sql preparada.
213
     *
214
     * @param  string          $sql
215
     * 
216
     * @throws \PDOException
217
     * @return \PDOStatement
218
     */
219
    public static function prepare(string $sql): PDOStatement
220
    {
221
        $sth = self::dbh()->prepare($sql);
222
        $sth->setFetchMode(\PDO::FETCH_CLASS, static::class);
223
224
        return $sth;
225
    }
226
227
    /**
228
     * Retorna el último ID insertado.
229
     *
230
     * @return string
231
     */
232
    public static function lastInsertId(): string
233
    {
234
        return self::dbh()->lastInsertId();
235
    }
236
237
    /**
238
     * Consulta sql.
239
     *
240
     * @param  string          $sql
241
     * 
242
     * @throws \PDOException
243
     * @return \PDOStatement
244
     */
245
    public static function sql(string $sql): PDOStatement
246
    {
247
        return self::dbh()->query($sql, \PDO::FETCH_CLASS, static::class);
248
    }
249
250
    /**
251
     * Ejecuta consulta sql.
252
     *
253
     * @param  string        $sql
254
     * @param  array         $values valores
255
     * 
256
     * @throws PDOException 
257
     * @return bool|PDOStatement
258
     */
259
    public static function query(string $sql, array $values = [])
260
    {
261
        if (empty($values)) {
262
            return self::sql($sql);
263
        }
264
265
        $sth = self::prepare($sql);
266
267
        return $sth->execute($values) ? $sth : \false;
268
    }
269
270
    /**
271
     * Verifica si existe el registro.
272
     *
273
     * @param  string $pk valor para clave primaria
274
     * @return bool
275
     */
276
    public static function exists($pk): bool
277
    {
278
        $source  = static::getSource();
279
        $pkField = static::getPK();
280
281
        return self::query("SELECT COUNT(*) AS count FROM $source WHERE $pkField = ?", [$pk])->fetch()->count > 0;
282
    }
283
284
    /**
285
     * Paginar consulta sql.
286
     *
287
     * @param  string      $sql     consulta select sql
288
     * @param  int         $page    numero de pagina
289
     * @param  int         $perPage cantidad de items por pagina
290
     * @param  array       $values  valores
291
     * @return Paginator
292
     */
293
    public static function paginateQuery(string $sql, int $page, int $perPage, array $values = []): Paginator
294
    {
295
        return new Paginator(static::class, $sql, $page, $perPage, $values);
296
    }
297
298
    /**
299
     * Devuelve el nombre del drive PDO utilizado.
300
     *
301
     * @return string
302
     */
303
    public static function getDriver(): string
304
    {
305
        return self::dbh()->getAttribute(\PDO::ATTR_DRIVER_NAME);
306
    }
307
308
    /**
309
     * Comienza una trasacción.
310
     *
311
     * @throws PDOException If there is already a transaction started or the driver does not support transactions
312
     * @return bool
313
     */
314
    public static function begin(): bool
315
    {
316
        return self::dbh()->beginTransaction();
317
    }
318
319
    /**
320
     * Da marcha atrás a una trasacción.
321
     *
322
     * @throws PDOException if there is no active transaction.
323
     * @return bool
324
     */
325
    public static function rollback(): bool
326
    {
327
        return self::dbh()->rollBack();
328
    }
329
330
    /**
331
     * Realiza el commit de  una trasacción.
332
     *
333
     * @throws \PDOException if there is no active transaction.
334
     * @return bool
335
     */
336
    public static function commit(): bool
337
    {
338
        return self::dbh()->commit();
339
    }
340
}
341