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.

Adapter/DoctrineAdapter.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
 * This file is part of HeriJobQueueBundle.
5
 *
6
 * (c) Alexandre Mogère
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Heri\Bundle\JobQueueBundle\Adapter;
13
14
use ZendQueue\Adapter\AbstractAdapter;
15
use ZendQueue\Message;
16
use ZendQueue\Queue;
17
use Heri\Bundle\JobQueueBundle\Exception\AdapterRuntimeException;
18
19
/**
20
 * Doctrine adapter.
21
 *
22
 * @see ZendQueue\Adapter\AbstractAdapter
23
 */
24
class DoctrineAdapter extends AbstractAdapter implements AdapterInterface
25
{
26
    /**
27
     * @var Doctrine\ORM\EntityManager
28
     */
29
    public $em;
30
31
    /**
32
     * @var int
33
     */
34
    public $priority = 0;
35
36
    /**
37
     * Does a queue already exist?
38
     *
39
     * Throws an exception if the adapter cannot determine if a queue exists.
40
     * use isSupported('isExists') to determine if an adapter can test for
41
     * queue existance.
42
     *
43
     * @param string $name
44
     *
45
     * @return bool
46
     *
47
     * @throws ZendQueue\Exception
48
     */
49
    public function isExists($name)
50
    {
51
        $repo = $this->em
52
            ->getRepository('Heri\Bundle\JobQueueBundle\Entity\Queue')
53
            ->findOneBy([
54
                'name' => $name,
55
            ]);
56
57
        return ($repo) ? true : false;
58
    }
59
60
    /**
61
     * Create a new queue.
62
     *
63
     * Visibility timeout is how long a message is left in the queue "invisible"
64
     * to other readers.  If the message is acknowleged (deleted) before the
65
     * timeout, then the message is deleted.  However, if the timeout expires
66
     * then the message will be made available to other queue readers.
67
     *
68
     * @param string $name    Queue name
69
     * @param int    $timeout Default visibility timeout
70
     *
71
     * @return bool
72
     *
73
     * @throws ZendQueue\Exception - database error
74
     */
75
    public function create($name, $timeout = null)
76
    {
77
        if ($this->isExists($name)) {
78
            return false;
79
        }
80
81
        $queue = new \Heri\Bundle\JobQueueBundle\Entity\Queue();
82
        $queue->setName($name);
83
        $newtimeout = (is_null($timeout)) ? self::CREATE_TIMEOUT_DEFAULT : (int) $timeout;
84
        $queue->setTimeout($newtimeout);
0 ignored issues
show
$newtimeout is of type integer, but the function expects a object<Heri\Bundle\JobQu...Bundle\Entity\smallint>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
85
86
        $this->em->persist($queue);
87
        $this->em->flush();
88
89
        return true;
90
    }
91
92
    /**
93
     * Delete a queue and all of it's messages.
94
     *
95
     * Returns false if the queue is not found, true if the queue exists
96
     *
97
     * @param string $name Queue name
98
     *
99
     * @return bool
100
     *
101
     * @throws ZendQueue\Exception
102
     */
103
    public function delete($name)
104
    {
105
        // Get primary key
106
        $id = $this->getQueueEntity($name);
107
108
        $queue = $this->em
109
            ->getRepository('Heri\Bundle\JobQueueBundle\Entity\Queue')
110
            ->find($id);
111
112
        $messages = $this->em
113
            ->getRepository('Heri\Bundle\JobQueueBundle\Entity\Message')
114
            ->findBy([
115
                'queue' => $queue,
116
            ]);
117
        foreach ($messages as $message) {
118
            $this->em->remove($message);
119
        }
120
121
        $this->em->remove($queue);
122
        $this->em->flush();
123
        $this->em->clear();
124
125
        return true;
126
    }
127
128
    /*
129
     * Get an array of all available queues
130
     *
131
     * Not all adapters support getQueues(), use isSupported('getQueues')
132
     * to determine if the adapter supports this feature.
133
     *
134
     * @return array
135
     */
136
    public function getQueues()
137
    {
138
        $list = [];
139
140
        $queues = $this->em
141
            ->getRepository('Heri\Bundle\JobQueueBundle\Entity\Queue')
142
            ->findAll();
143
        foreach ($queues as $queue) {
144
            $list[] = $queue->name;
145
        }
146
147
        return $list;
148
    }
149
150
    /**
151
     * Return the approximate number of messages in the queue.
152
     *
153
     * @param ZendQueue\Queue $queue
154
     *
155
     * @return int
156
     *
157
     * @throws ZendQueue\Exception
158
     */
159
    public function count(Queue $queue = null)
160
    {
161
        $qb = $this->em->createQueryBuilder();
162
        $qb
163
            ->select('count(m)')
164
            ->from('Heri\Bundle\JobQueueBundle\Entity\Message', 'm')
165
            ->leftJoin('m.queue', 'Queue')
166
        ;
167
168
        if ($queue instanceof Queue) {
169
            $qb
170
                ->where($qb->expr()->eq('Queue.name', ':name'))
171
                ->setParameter('name', $queue->getName())
172
            ;
173
        }
174
175
        $query = $qb->getQuery();
176
177
        return $query->getSingleScalarResult();
178
    }
179
180
    /**
181
     * Send a message to the queue.
182
     *
183
     * @param string          $message Message to send to the active queue
184
     * @param ZendQueue\Queue $queue
185
     *
186
     * @return ZendQueue\Message
187
     *
188
     * @throws ZendQueue\Exception
189
     */
190
    public function send($message, Queue $queue = null)
191
    {
192
        $body = '';
193
194
        if ($queue === null) {
195
            $queue = $this->_queue;
196
        }
197
198
        if (is_scalar($message)) {
199
            $body = (string) $message;
200
        }
201
202
        if (is_string($message)) {
203
            $body = trim($message);
204
        }
205
206
        if (!$this->isExists($queue->getName())) {
207
            throw new AdapterRuntimeException(sprintf('Queue does not exist: %s', $queue->getName()));
208
        }
209
210
        $entity = $this->createMessage($queue, $body);
211
212
        $options = [
213
            'queue' => $queue,
214
            'data' => $entity->toArray(),
215
        ];
216
217
        $classname = $queue->getMessageClass();
218
219
        return new $classname($options);
220
    }
221
222
    /**
223
     * Get messages in the queue.
224
     *
225
     * @param int             $maxMessages Maximum number of messages to return
226
     * @param int             $timeout     Visibility timeout for these messages
227
     * @param ZendQueue\Queue $queue
228
     *
229
     * @return ZendQueue\MessageIterator
230
     *
231
     * @throws ZendQueue\Exception Database error
232
     */
233
    public function receive($maxMessages = null, $timeout = null, Queue $queue = null)
234
    {
235
        $result = [];
236
237
        // Cache microtime
238
        $microtime = microtime(true);
239
240
        if (is_null($queue)) {
241
            $queue = $this->_queue;
242
        }
243
244
        if ($maxMessages > 0) {
245
            $messages = $this->getMessages(
246
                $maxMessages,
247
                $timeout,
248
                $queue,
0 ignored issues
show
$queue is of type object<ZendQueue\Queue>, but the function expects a object<Heri\Bundle\JobQu...r\ZendQueue\Queue>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
249
                $microtime
250
            );
251
252
            // Update working messages
253
            foreach ($messages as $message) {
254
                $key = md5(uniqid(rand(), true));
255
                $message->setHandle($key);
256
                $message->setTimeout($microtime);
257
258
                $result[] = $message->toArray();
259
            }
260
            $this->em->flush();
261
        }
262
263
        $options = [
264
            'queue' => $queue,
265
            'data' => $result,
266
            'messageClass' => $queue->getMessageClass(),
267
        ];
268
269
        $classname = $queue->getMessageSetClass();
270
271
        return new $classname($options);
272
    }
273
274
    /**
275
     * Delete a message from the queue.
276
     *
277
     * Returns true if the message is deleted, false if the deletion is
278
     * unsuccessful.
279
     *
280
     * @param ZendQueue\Message $message
281
     *
282
     * @return bool
283
     *
284
     * @throws ZendQueue\Exception - database error
285
     */
286
    public function deleteMessage(Message $message)
287
    {
288
        $repo = $this->em
289
            ->getRepository('Heri\Bundle\JobQueueBundle\Entity\Message')
290
            ->findOneBy([
291
                'handle' => $message->handle,
292
            ]);
293
294
        $this->em->remove($repo);
295
        $this->em->flush();
296
297
        return $this->em->clear();
298
    }
299
300
    /**
301
     * Return a list of queue capabilities functions.
302
     *
303
     * $array['function name'] = true or false
304
     * true is supported, false is not supported.
305
     *
306
     * @param string $name
0 ignored issues
show
There is no parameter named $name. 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...
307
     *
308
     * @return array
309
     */
310
    public function getCapabilities()
311
    {
312
        return [
313
            'create' => true,
314
            'delete' => true,
315
            'send' => true,
316
            'receive' => true,
317
            'deleteMessage' => true,
318
            'getQueues' => true,
319
            'count' => true,
320
            'isExists' => true,
321
        ];
322
    }
323
324
    /**
325
     * Retry failed messages.
326
     *
327
     * @param int $id
328
     */
329
    public function retry($id = null)
330
    {
331
        $sql = <<<EOL
332
            UPDATE Heri\Bundle\JobQueueBundle\Entity\Message m
333
            SET m.numRetries = 0
334
EOL;
335
336
        $query = $this->em->createQuery($sql);
337
        if (!is_null($id)) {
338
            $sql .= ' WHERE m.id = ?1';
339
340
            $query->setParameter(1, $id);
341
        }
342
343
        $query->execute();
344
    }
345
346
    /**
347
     * Delete a failed message.
348
     *
349
     * @param int $id
350
     */
351
    public function forget($id)
352
    {
353
        $sql = <<<EOL
354
        DELETE FROM Heri\Bundle\JobQueueBundle\Entity\Message m WHERE m.id = ?1
355
EOL;
356
357
        return $this->em->createQuery($sql)->setParameter(1, $id)->execute();
358
    }
359
360
    /**
361
     * {@inheritdoc}
362
     */
363
    public function setPriority($priority)
364
    {
365
        $this->priority = $priority;
366
    }
367
368
    /**
369
     * {@inheritdoc}
370
     */
371
    public function showMessages($queueName)
372
    {
373
        $results = [];
374
        if ($this->isExists($queueName)) {
375
            $qb = $this->em->createQueryBuilder();
376
            $qb
377
                ->select('m.id, m.body, m.created, m.ended, m.failed')
378
                ->from('Heri\Bundle\JobQueueBundle\Entity\Message', 'm')
379
                ->leftJoin('m.queue', 'Queue')
380
                ->where($qb->expr()->eq('Queue.name', ':name'))
381
                ->setParameter('name', $queueName)
382
            ;
383
384
            $query = $qb->getQuery();
385
            $results = $query->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
386
        }
387
388
        return $results;
389
    }
390
391
    /**
392
     * {@inheritdoc}
393
     */
394
    public function flush()
395
    {
396
        $sql = 'DELETE Heri\Bundle\JobQueueBundle\Entity\MessageLog';
397
398
        return $this->em->createQuery($sql);
399
    }
400
401
    /**
402
     * {@inheritdoc}
403
     */
404
    public function logException($message, $e)
405
    {
406
        $sql = <<<EOL
407
            UPDATE Heri\Bundle\JobQueueBundle\Entity\Message m
408
            SET
409
                m.ended = 0,
410
                m.failed = 1,
411
                m.numRetries = m.numRetries + ?1,
412
                m.priority = 0
413
            WHERE m.id = ?2
414
EOL;
415
416
        $this->em->createQuery($sql)
417
            ->setParameter(1, $message->failed ? 1 : 0)
418
            ->setParameter(2, $message->id)
419
            ->execute()
420
        ;
421
422
        $messageObject = $this->em
423
            ->getRepository('Heri\Bundle\JobQueueBundle\Entity\Message')
424
            ->find($message->id);
425
426
        $log = new \Heri\Bundle\JobQueueBundle\Entity\MessageLog();
427
        $log->setMessageId($messageObject);
428
        $log->setDateLog(new \DateTime('now'));
429
        $log->setLog($e->getMessage());
430
        $this->em->persist($log);
431
        $this->em->flush();
432
    }
433
434
    /**
435
     * Create a new message.
436
     *
437
     * @param ZendQueue\Queue $queue
438
     * @param string          $body
439
     */
440
    protected function createMessage(Queue $queue, $body)
441
    {
442
        // check if message exist
443
        $message = $this->em
444
            ->getRepository('Heri\Bundle\JobQueueBundle\Entity\Message')
445
            ->findOneBy([
446
                'md5' => md5($body),
447
            ]);
448
449
        if (!$message) {
450
            $message = new \Heri\Bundle\JobQueueBundle\Entity\Message();
451
            $message->setQueue($this->getQueueEntity($queue->getName()));
0 ignored issues
show
$this->getQueueEntity($queue->getName()) is of type object<ZendQueue\Queue>, but the function expects a null|object<Heri\Bundle\...eueBundle\Entity\Queue>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
452
            $message->setBody($body);
0 ignored issues
show
$body is of type string, but the function expects a object<Heri\Bundle\JobQueueBundle\Entity\text>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
453
            $message->setMd5(md5($body));
454
            $message->setPriority($this->priority);
0 ignored issues
show
$this->priority is of type integer, but the function expects a object<Heri\Bundle\JobQu...Bundle\Entity\smallint>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
455
            $message->setFailed(false);
456
            $message->setEnded(false);
457
458
            $this->em->persist($message);
459
            $this->em->flush();
460
            $this->em->clear();
461
        }
462
463
        return $message;
464
    }
465
466
    /**
467
     * Get messages of the queue.
468
     *
469
     * @param int             $maxMessages
470
     * @param int             $timeout
471
     * @param ZendQueue\Queue $queue
472
     * @param int             $microtime
473
     */
474
    protected function getMessages($maxMessages, $timeout, $queue = null, $microtime = null)
475
    {
476
        if (is_null($maxMessages)) {
477
            $maxMessages = 1;
478
        }
479
480
        if (is_null($timeout)) {
481
            $timeout = self::RECEIVE_TIMEOUT_DEFAULT;
482
        }
483
484
        $andWhere = '';
485
        if ($queue instanceof Queue) {
486
            $andWhere = 'AND (m.queue = :queue) ';
487
        }
488
489
        $andWhere .= 'AND (q.maxRetries IS NULL OR (q.maxRetries = 0 AND m.failed = false) OR m.numRetries < q.maxRetries)';
490
491
        // Search for all messages inside the timeout
492
        $sql = 'SELECT m '.
493
            'FROM Heri\Bundle\JobQueueBundle\Entity\Message m '.
494
            'LEFT JOIN m.queue q '.
495
            'WHERE (m.handle IS NULL OR m.handle = \'\' OR m.timeout + :timeout < :microtime) '.$andWhere.' '.
496
            'ORDER BY m.priority DESC';
497
498
        $query = $this->em->createQuery($sql);
499
500
        $query->setParameter('timeout', (int) $timeout);
501
        $query->setParameter('microtime', (int) $microtime);
502
503
        if ($queue instanceof Queue) {
504
            $query->setParameter('queue', $this->getQueueEntity($queue->getName()));
505
        }
506
        $query->setMaxResults($maxMessages);
507
508
        return $query->getResult();
509
    }
510
511
    /**
512
     * Get the queue entity.
513
     *
514
     * @param string $name
515
     *
516
     * @return Queue Entity
517
     *
518
     * @throws ZendQueue\Exception
519
     */
520
    protected function getQueueEntity($name)
521
    {
522
        $repo = $this->em
523
            ->getRepository('Heri\Bundle\JobQueueBundle\Entity\Queue')
524
            ->findOneBy([
525
                'name' => $name,
526
            ]);
527
528
        if (!$repo) {
529
            throw new AdapterRuntimeException(sprintf('Queue does not exist: %s', $name));
530
        }
531
532
        return $repo;
533
    }
534
}
535