Issues (9)

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.

src/Connection.php (1 issue)

Labels
Severity

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
namespace Ddeboer\Imap;
4
5
use Ddeboer\Imap\Exception\Exception;
6
use Ddeboer\Imap\Exception\MailboxDoesNotExistException;
7
8
/**
9
 * A connection to an IMAP server that is authenticated for a user
10
 */
11
class Connection
12
{
13
    private $server;
14
    private $resource;
15
    private $mailboxes;
16
    private $mailboxNames;
17
18
    /**
19
     * Constructor
20
     *
21
     * @param resource $resource
22
     * @param string   $server
23
     *
24
     * @throws \InvalidArgumentException
25
     */
26
    public function __construct($resource, $server)
27
    {
28
        if (!is_resource($resource)) {
29
            throw new \InvalidArgumentException('$resource must be a resource');
30
        }
31
32
        $this->resource = $resource;
33
        $this->server = $server;
34
    }
35
36
    /**
37
     * Get a list of mailboxes (also known as folders)
38
     *
39
     * @return Mailbox[]
40
     */
41
    public function getMailboxes()
42
    {
43
        if (null === $this->mailboxes) {
44
            foreach ($this->getMailboxNames() as $mailboxName) {
0 ignored issues
show
The expression $this->getMailboxNames() of type array|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
45
                $this->mailboxes[] = $this->getMailbox($mailboxName);
46
            }
47
        }
48
49
        return $this->mailboxes;
50
    }
51
52
    /**
53
     * Check that a mailbox with the given name exists
54
     *
55
     * @param string $name Mailbox name
56
     *
57
     * @return bool
58
     */
59
    public function hasMailbox($name)
60
    {
61
        return in_array($name, $this->getMailboxNames());
62
    }
63
64
    /**
65
     * Get a mailbox by its name
66
     *
67
     * @param string $name Mailbox name
68
     *
69
     * @return Mailbox
70
     * @throws MailboxDoesNotExistException If mailbox does not exist
71
     */
72
    public function getMailbox($name)
73
    {
74
        if (!$this->hasMailbox($name)) {
75
            throw new MailboxDoesNotExistException($name);
76
        }
77
78
        return new Mailbox($this->server . imap_utf7_encode($name), $this);
79
    }
80
81
    /**
82
     * Count number of messages not in any mailbox
83
     *
84
     * @return int
85
     */
86
    public function count()
87
    {
88
        return imap_num_msg($this->resource);
89
    }
90
91
    /**
92
     * Create mailbox
93
     *
94
     * @param $name
95
     *
96
     * @return Mailbox
97
     * @throws Exception
98
     */
99
    public function createMailbox($name)
100
    {
101
        if (imap_createmailbox($this->resource, $this->server . $name)) {
102
            $this->mailboxNames = $this->mailboxes = null;
103
104
            return $this->getMailbox($name);
105
        }
106
107
        throw new Exception("Can not create '{$name}' mailbox at '{$this->server}'");
108
    }
109
110
    /**
111
     * Close connection
112
     *
113
     * @param int $flag
114
     *
115
     * @return bool
116
     */
117
    public function close($flag = 0)
118
    {
119
        return imap_close($this->resource, $flag);
120
    }
121
122
    public function deleteMailbox(Mailbox $mailbox)
123
    {
124
        if (false === imap_deletemailbox(
125
            $this->resource,
126
            $this->server . $mailbox->getName()
127
        )) {
128
            throw new Exception('Mailbox ' . $mailbox->getName() . ' could not be deleted');
129
        }
130
131
        $this->mailboxes = $this->mailboxNames = null;
132
    }
133
134
    /**
135
     * Get IMAP resource
136
     *
137
     * @return resource
138
     */
139
    public function getResource()
140
    {
141
        return $this->resource;
142
    }
143
144
    /**
145
     * Get mailbox names
146
     * 
147
     * @return array
148
     */
149
    private function getMailboxNames()
150
    {
151
        if (null === $this->mailboxNames) {
152
            $mailboxes = imap_getmailboxes($this->resource, $this->server, '*');
153
            foreach ($mailboxes as $mailbox) {
154
                $this->mailboxNames[] = imap_utf7_decode(str_replace($this->server, '', $mailbox->name));
155
            }
156
        }
157
158
        return $this->mailboxNames;
159
    }
160
}
161