Issues (1191)

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.

src/Model/EloquentQueryBuilder.php (6 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 namespace Anomaly\Streams\Platform\Model;
2
3
use Anomaly\Streams\Platform\Assignment\AssignmentModel;
4
use Anomaly\Streams\Platform\Collection\CacheCollection;
5
use Anomaly\Streams\Platform\Entry\Contract\EntryInterface;
6
use Anomaly\Streams\Platform\Entry\EntryModel;
7
use Illuminate\Database\Eloquent\Builder;
8
use Illuminate\Database\Query\JoinClause;
9
10
/**
11
 * Class EloquentQueryBuilder
12
 *
13
 * @link    http://pyrocms.com/
14
 * @author  PyroCMS, Inc. <[email protected]>
15
 * @author  Ryan Thompson <[email protected]>
16
 */
17
class EloquentQueryBuilder extends Builder
18
{
19
20
    /**
21
     * Runtime cache.
22
     *
23
     * @var array
24
     */
25
    protected static $cache = [];
26
27
    /**
28
     * The model being queried.
29
     *
30
     * @var EloquentModel
31
     */
32
    protected $model;
33
34
    /**
35
     * Execute the query as a "select" statement.
36
     *
37
     * @param  array $columns
38
     * @return \Illuminate\Database\Eloquent\Collection|static[]
39
     */
40
    public function get($columns = ['*'])
41
    {
42
        $key = $this->getCacheKey();
43
44
        if (
45
            env('INSTALLED')
46
            && PHP_SAPI != 'cli'
47
            && env('DB_CACHE') !== false
48
            && $this->model instanceof EntryModel
49
            && isset(self::$cache[$this->model->getCacheCollectionKey()][$key])
50
        ) {
51
            return self::$cache[$this->model->getCacheCollectionKey()][$key];
52
        }
53
54
        $this->orderByDefault();
55
56
        if (PHP_SAPI != 'cli' && env('DB_CACHE') !== false && $this->model->getTtl()) {
57
58
            $this->rememberIndex();
59
60
            try {
61
                return app('cache')->remember(
62
                    $this->getCacheKey(),
63
                    $this->model->getTtl(),
64
                    function () use ($columns) {
65
                        return parent::get($columns);
66
                    }
67
                );
68
            } catch (\Exception $e) {
69
                return parent::get($columns);
0 ignored issues
show
Bug Compatibility introduced by
The expression parent::get($columns); of type Illuminate\Database\Eloq...base\Eloquent\Builder[] adds the type Illuminate\Database\Eloquent\Builder[] to the return on line 69 which is incompatible with the return type documented by Anomaly\Streams\Platform...oquentQueryBuilder::get of type Illuminate\Database\Eloq...\EloquentQueryBuilder[].
Loading history...
70
            }
71
        }
72
73
        return self::$cache[$this->model->getCacheCollectionKey()][$key] = parent::get($columns);
0 ignored issues
show
Bug Compatibility introduced by
The expression self::$cache[$this->mode... parent::get($columns); of type Illuminate\Database\Eloq...base\Eloquent\Builder[] adds the type Illuminate\Database\Eloquent\Builder[] to the return on line 73 which is incompatible with the return type documented by Anomaly\Streams\Platform...oquentQueryBuilder::get of type Illuminate\Database\Eloq...\EloquentQueryBuilder[].
Loading history...
74
    }
75
76
    /**
77
     * Return if a table has been joined or not.
78
     *
79
     * @param $table
80
     * @return bool
81
     */
82
    public function hasJoin($table)
83
    {
84
        if (!$this->query->joins) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->query->joins 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...
85
            return false;
86
        }
87
88
        /* @var JoinClause $join */
89
        foreach ($this->query->joins as $join) {
90
            if ($join->table === $table) {
91
                return true;
92
            }
93
        }
94
95
        return false;
96
    }
97
98
    /**
99
     * Remember and index.
100
     *
101
     * @return $this
102
     */
103
    protected function rememberIndex()
104
    {
105
        if ($this->model->getTtl()) {
106
            $this->indexCacheCollection();
107
        }
108
109
        return $this;
110
    }
111
112
    /**
113
     * Index cache collection
114
     *
115
     * @return object
116
     */
117
    protected function indexCacheCollection()
118
    {
119
        (new CacheCollection())
120
            ->make([$this->getCacheKey()])
121
            ->setKey($this->model->getCacheCollectionKey())
122
            ->index();
123
124
        return $this;
125
    }
126
127
    /**
128
     * Drop a cache collection
129
     * from runtime cache.
130
     *
131
     * @param $collection
132
     */
133
    public static function dropRuntimeCache($collection)
134
    {
135
        unset(self::$cache[$collection]);
136
    }
137
138
    /**
139
     * Get the unique cache key for the query.
140
     *
141
     * @return string
142
     */
143
    public function getCacheKey()
144
    {
145
        $name = $this->model->getConnectionName();
146
147
        return md5($name . $this->toSql() . serialize($this->getBindings()));
0 ignored issues
show
Documentation Bug introduced by
The method toSql does not exist on object<Anomaly\Streams\P...l\EloquentQueryBuilder>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
Documentation Bug introduced by
The method getBindings does not exist on object<Anomaly\Streams\P...l\EloquentQueryBuilder>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
148
    }
149
150
    /**
151
     * Set the model TTl.
152
     *
153
     * @param $ttl
154
     * @return $this
155
     */
156
    public function cache($ttl)
157
    {
158
        $this->model->setTtl($ttl);
159
160
        return $this;
161
    }
162
163
    /**
164
     * Get fresh models / disable cache
165
     *
166
     * @param  boolean $fresh
167
     * @return object
168
     */
169
    public function fresh($fresh = true)
170
    {
171
        if ($fresh) {
172
            $this->model->setTtl(0);
173
        }
174
175
        return $this;
176
    }
177
178
    /**
179
     * Update a record in the database.
180
     *
181
     * @param  array $values
182
     * @return int
183
     */
184
    public function update(array $values)
185
    {
186
        $this->model->fireEvent('updatingMultiple');
187
188
        $return = parent::update($values);
189
190
        $this->model->fireEvent('updatedMultiple');
191
192
        return $return;
193
    }
194
195
    /**
196
     * Delete a record from the database.
197
     *
198
     * @return mixed
199
     */
200
    public function delete()
201
    {
202
        $this->model->fireEvent('deletingMultiple');
203
204
        $return = parent::delete();
205
206
        $this->model->fireEvent('deletedMultiple');
207
208
        return $return;
209
    }
210
211
    /**
212
     * Order by sort_order if null.
213
     */
214
    protected function orderByDefault()
215
    {
216
        $model = $this->getModel();
217
        $query = $this->getQuery();
218
219
        if ($query->orders === null) {
220
            if ($model instanceof AssignmentModel) {
221
                $query->orderBy('sort_order', 'ASC');
222
            } elseif ($model instanceof EntryInterface) {
223
                if ($model->getStream()->isSortable()) {
224
                    $query->orderBy('sort_order', 'ASC');
225
                } elseif ($model->titleColumnIsTranslatable()) {
226
227
                    /**
228
                     * Postgres makes it damn near impossible
229
                     * to order by a foreign column and retain
230
                     * distinct results so let's avoid it entirely.
231
                     *
232
                     * Sorry!
233
                     */
234
                    if (env('DB_CONNECTION', 'mysql') == 'pgsql') {
235
                        return;
236
                    }
237
238
                    if (!$this->hasJoin($model->getTranslationsTableName())) {
239
                        $this->query->leftJoin(
240
                            $model->getTranslationsTableName(),
241
                            $model->getTableName() . '.id',
242
                            '=',
243
                            $model->getTranslationsTableName() . '.entry_id'
244
                        );
245
                    }
246
247
                    $this
0 ignored issues
show
Documentation Bug introduced by
The method groupBy does not exist on object<Anomaly\Streams\P...l\EloquentQueryBuilder>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
248
                        ->groupBy($model->getTableName() . '.id')
249
                        ->select($model->getTableName() . '.*')
250
                        ->where(
251
                            function (Builder $query) use ($model) {
252
                                $query->where($model->getTranslationsTableName() . '.locale', config('app.locale'));
253
                                $query->orWhere(
254
                                    $model->getTranslationsTableName() . '.locale',
255
                                    config('app.fallback_locale')
256
                                );
257
                                $query->orWhereNull($model->getTranslationsTableName() . '.locale');
258
                            }
259
                        )
260
                        ->orderBy($model->getTranslationsTableName() . '.' . $model->getTitleName(), 'ASC');
261
                } elseif ($model->getTitleName() && $model->getTitleName() !== 'id') {
262
                    $query->orderBy($model->getTitleName(), 'ASC');
263
                }
264
            }
265
        }
266
    }
267
}
268