Issues (45)

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/Xhgui/Searcher/Mongo.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
2
3
/**
4
 * A Searcher for a MongoDB backend.
5
 */
6
class Xhgui_Searcher_Mongo implements Xhgui_Searcher_Interface
7
{
8
    protected $_collection;
9
10
    protected $_watches;
11
12
    protected $_mapper;
13
14
    public function __construct(MongoDb $db)
15
    {
16
        $this->_collection = $db->results;
0 ignored issues
show
The property results does not seem to exist in MongoDB.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
17
        $this->_watches = $db->watches;
0 ignored issues
show
The property watches does not seem to exist in MongoDB.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
18
        $this->_mapper = new Xhgui_Db_Mapper();
19
    }
20
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function latest()
25
    {
26
        $cursor = $this->_collection->find()
27
            ->sort(array('meta.request_date' => -1))
28
            ->limit(1);
29
        $result = $cursor->getNext();
30
        return $this->_wrap($result);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->_wrap($result); of type Xhgui_Profile|array adds the type array to the return on line 30 which is incompatible with the return type declared by the interface Xhgui_Searcher_Interface::latest of type Xhgui_Profile.
Loading history...
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function query($conditions, $limit, $fields = [])
37
    {
38
        $result = $this->_collection->find($conditions, $fields)
39
            ->limit($limit);
40
41
        return iterator_to_array($result);
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function get($id)
48
    {
49
        return $this->_wrap($this->_collection->findOne(array(
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->_wrap($this->_col...> new \MongoId($id)))); of type Xhgui_Profile|array adds the type array to the return on line 49 which is incompatible with the return type declared by the interface Xhgui_Searcher_Interface::get of type Xhgui_Profile.
Loading history...
50
            '_id' => new MongoId($id)
51
        )));
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function getForUrl($url, $options, $conditions = [])
58
    {
59
        $conditions = array_merge(
60
            (array)$conditions,
61
            array('simple_url' => $url)
62
        );
63
        $options = array_merge($options, array(
64
            'conditions' => $conditions,
65
        ));
66
        return $this->paginate($options);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->paginate($options); (array) is incompatible with the return type declared by the interface Xhgui_Searcher_Interface::getForUrl of type MongoCursor.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function getPercentileForUrl($percentile, $url, $search = [])
73
    {
74
        $result = $this->_mapper->convert(array(
75
            'conditions' => $search + array('simple_url' => $url)
76
        ));
77
        $match = $result['conditions'];
78
79
        $col = '$meta.request_date';
80 View Code Duplication
        if (!empty($search['limit']) && $search['limit'][0] == "P") {
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...
81
            $col = '$meta.request_ts';
82
        }
83
84
        $results = $this->_collection->aggregate(array(
85
            array('$match' => $match),
86
            array(
87
                '$project' => array(
88
                    'date' => $col,
89
                    'profile.main()' => 1
90
                )
91
            ),
92
            array(
93
                '$group' => array(
94
                    '_id' => '$date',
95
                    'row_count' => array('$sum' => 1),
96
                    'wall_times' => array('$push' => '$profile.main().wt'),
97
                    'cpu_times' => array('$push' => '$profile.main().cpu'),
98
                    'mu_times' => array('$push' => '$profile.main().mu'),
99
                    'pmu_times' => array('$push' => '$profile.main().pmu'),
100
                )
101
            ),
102
            array(
103
                '$project' => array(
104
                    'date' => '$date',
105
                    'row_count' => '$row_count',
106
                    'raw_index' => array(
107
                        '$multiply' => array(
108
                            '$row_count',
109
                            $percentile / 100
110
                        )
111
                    ),
112
                    'wall_times' => '$wall_times',
113
                    'cpu_times' => '$cpu_times',
114
                    'mu_times' => '$mu_times',
115
                    'pmu_times' => '$pmu_times',
116
                )
117
            ),
118
            array('$sort' => array('_id' => 1)),
119
            ),
120
            array('cursor' => array('batchSize' => 0))
121
        );
122
123
        if (empty($results['result'])) {
124
            return [];
125
        }
126
        $keys = array(
127
            'wall_times' => 'wt',
128
            'cpu_times' => 'cpu',
129
            'mu_times' => 'mu',
130
            'pmu_times' => 'pmu'
131
        );
132
        foreach ($results['result'] as &$result) {
133
            $result['date'] = ($result['_id'] instanceof MongoDate) ? date('Y-m-d H:i:s', $result['_id']->sec) : $result['_id'];
134
            unset($result['_id']);
135
            $index = max(round($result['raw_index']) - 1, 0);
136
            foreach ($keys as $key => $out) {
137
                sort($result[$key]);
138
                $result[$out] = isset($result[$key][$index]) ? $result[$key][$index] : null;
139
                unset($result[$key]);
140
            }
141
        }
142
        return $results['result'];
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148
    public function getAvgsForUrl($url, $search = [])
149
    {
150
        $match = array('meta.simple_url' => $url);
151
        if (isset($search['date_start'])) {
152
            $match['meta.request_date']['$gte'] = (string)$search['date_start'];
153
        }
154
        if (isset($search['date_end'])) {
155
            $match['meta.request_date']['$lte'] = (string)$search['date_end'];
156
        }
157
        $results = $this->_collection->aggregate(array(
158
            array('$match' => $match),
159
            array(
160
                '$project' => array(
161
                    'date' => '$meta.request_date',
162
                    'profile.main()' => 1,
163
                )
164
            ),
165
            array(
166
                '$group' => array(
167
                    '_id' => '$date',
168
                    'avg_wt' => array('$avg' => '$profile.main().wt'),
169
                    'avg_cpu' => array('$avg' => '$profile.main().cpu'),
170
                    'avg_mu' => array('$avg' => '$profile.main().mu'),
171
                    'avg_pmu' => array('$avg' => '$profile.main().pmu'),
172
                )
173
            ),
174
            array('$sort' => array('_id' => 1))
175
        ),
176
            array('cursor' => array('batchSize' => 0))
177
        );
178
        if (empty($results['result'])) {
179
            return [];
180
        }
181
        foreach ($results['result'] as $i => $result) {
182
            $results['result'][$i]['date'] = $result['_id'];
183
            unset($results['result'][$i]['_id']);
184
        }
185
        return $results['result'];
186
    }
187
188
    /**
189
     * {@inheritdoc}
190
     */
191
    public function getAll($options = [])
192
    {
193
        return $this->paginate($options);
194
    }
195
196
    /**
197
     * {@inheritdoc}
198
     */
199
    public function delete($id)
200
    {
201
        $this->_collection->remove(array('_id' => new MongoId($id)), []);
202
    }
203
204
    /**
205
     * {@inheritdoc}
206
     */
207
    public function truncate()
208
    {
209
        return $this->_collection->drop();
210
    }
211
212
    /**
213
     * {@inheritdoc}
214
     */
215
    public function saveWatch(array $data)
216
    {
217
        if (empty($data['name'])) {
218
            return false;
219
        }
220
221
        if (!empty($data['removed']) && isset($data['_id'])) {
222
            $this->_watches->remove(
223
                array('_id' => new MongoId($data['_id'])),
224
                array('w' => 1)
225
            );
226
            return true;
227
        }
228
229
        if (empty($data['_id'])) {
230
            $this->_watches->insert(
231
                $data,
232
                array('w' => 1)
233
            );
234
            return true;
235
        }
236
237
        $data['_id'] = new MongoId($data['_id']);
238
        $this->_watches->update(
239
            array('_id' => $data['_id']),
240
            $data,
241
            array('w' => 1)
242
        );
243
        return true;
244
    }
245
246
    /**
247
     * {@inheritdoc}
248
     */
249
    public function getAllWatches()
250
    {
251
        $cursor = $this->_watches->find();
252
        return array_values(iterator_to_array($cursor));
253
    }
254
255
    /**
256
     * {@inheritdoc}
257
     */
258
    public function truncateWatches()
259
    {
260
        $this->_watches->drop();
261
    }
262
263
    /**
264
     * {@inheritdoc}
265
     */
266
    private function paginate($options)
267
    {
268
        $opts = $this->_mapper->convert($options);
269
270
        $totalRows = $this->_collection->find(
271
            $opts['conditions'],
272
            array('_id' => 1))->count();
273
274
        $totalPages = max(ceil($totalRows / $opts['perPage']), 1);
275
        $page = 1;
276
        if (isset($options['page'])) {
277
            $page = min(max($options['page'], 1), $totalPages);
278
        }
279
280
        $projection = false;
281
        if (isset($options['projection'])) {
282
            if ($options['projection'] === true) {
283
                $projection = array('meta' => 1, 'profile.main()' => 1);
284
            } else {
285
                $projection = $options['projection'];
286
            }
287
        }
288
289
        if ($projection === false) {
290
            $cursor = $this->_collection->find($opts['conditions'])
291
                ->sort($opts['sort'])
292
                ->skip((int)($page - 1) * $opts['perPage'])
293
                ->limit($opts['perPage']);
294
        } else {
295
            $cursor = $this->_collection->find($opts['conditions'], $projection)
296
                ->sort($opts['sort'])
297
                ->skip((int)($page - 1) * $opts['perPage'])
298
                ->limit($opts['perPage']);
299
        }
300
301
        return array(
302
            'results' => $this->_wrap($cursor),
303
            'sort' => $opts['sort'],
304
            'direction' => $opts['direction'],
305
            'page' => $page,
306
            'perPage' => $opts['perPage'],
307
            'totalPages' => $totalPages
308
        );
309
    }
310
311
    /**
312
     * Converts arrays + MongoCursors into Xhgui_Profile instances.
313
     *
314
     * @param array|MongoCursor $data The data to transform.
315
     * @return Xhgui_Profile|Xhgui_Profile[] The transformed/wrapped results.
316
     */
317
    private function _wrap($data)
318
    {
319
        if ($data === null) {
320
            throw new Exception('No profile data found.');
321
        }
322
323
        if (is_array($data)) {
324
            return new Xhgui_Profile($data);
325
        }
326
        $results = [];
327
        foreach ($data as $row) {
328
            $results[] = new Xhgui_Profile($row);
329
        }
330
        return $results;
331
    }
332
}
333