Issues (24)

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/Notifications/Notifier.php (7 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
namespace EricMakesStuff\ServerMonitor\Notifications;
4
5
use EricMakesStuff\ServerMonitor\Monitors\HttpPingMonitor;
6
use EricMakesStuff\ServerMonitor\Monitors\SSLCertificateMonitor;
7
use Psr\Log\LoggerInterface as LogContract;
8
use EricMakesStuff\ServerMonitor\Monitors\DiskUsageMonitor;
9
use Exception;
10
11
class Notifier
12
{
13
    /** @var array */
14
    protected $config;
15
16
    /** @var \Illuminate\Contracts\Logging\Log */
17
    protected $log;
18
19
    protected $serverName;
20
21
    /**
22
     * @param \Illuminate\Contracts\Logging\Log $log
23
     */
24
    public function __construct(LogContract $log)
25
    {
26
        $this->log = $log;
0 ignored issues
show
Documentation Bug introduced by
It seems like $log of type object<Psr\Log\LoggerInterface> is incompatible with the declared type object<Illuminate\Contracts\Logging\Log> of property $log.

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...
27
28
        $this->serverName = config('server-monitor.server.name');
29
30
        $this->subject = "{$this->serverName} Server Monitoring";
0 ignored issues
show
The property subject does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
31
    }
32
33 View Code Duplication
    public function diskUsageHealthy(DiskUsageMonitor $diskUsageMonitor)
0 ignored issues
show
This method seems to be duplicated in 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...
34
    {
35
        $this->sendNotification(
36
            'whenDiskUsageHealthy',
37
            "Disk Usage on {$this->serverName} is Healthy at {$diskUsageMonitor->getPercentageUsed()} Used",
38
            "Disk Usage is healthy on {$this->serverName}. Filesystem {$diskUsageMonitor->getPath()} is okay: {$diskUsageMonitor->getPercentageUsed()}",
39
            BaseSender::TYPE_SUCCESS
40
        );
41
    }
42
43
    /**
44
     * @param \EricMakesStuff\ServerMonitor\Monitors\DiskUsageMonitor $diskUsageMonitor
45
     */
46 View Code Duplication
    public function diskUsageAlarm(DiskUsageMonitor $diskUsageMonitor)
0 ignored issues
show
This method seems to be duplicated in 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...
47
    {
48
        $this->sendNotification(
49
            'whenDiskUsageAlarm',
50
            "Disk Usage on {$this->serverName} High! {$diskUsageMonitor->getPercentageUsed()} Used",
51
            "Disk Usage Alarm on {$this->serverName}! Filesystem {$diskUsageMonitor->getPath()} is above the alarm threshold ({$diskUsageMonitor->getAlarmPercentage()}) at {$diskUsageMonitor->getPercentageUsed()}",
52
            BaseSender::TYPE_ERROR
53
        );
54
    }
55
56
    /**
57
     * @param HttpPingMonitor $httpPingMonitor
58
     */
59
    public function httpPingUp(HttpPingMonitor $httpPingMonitor)
60
    {
61
        $this->sendNotification(
62
            'whenHttpPingUp',
63
            "HTTP Ping Success: {$httpPingMonitor->getUrl()}",
64
            "HTTP Ping Succeeded for {$httpPingMonitor->getUrl()}. Response Code {$httpPingMonitor->getResponseCode()}.",
65
            BaseSender::TYPE_SUCCESS
66
        );
67
    }
68
69
    /**
70
     * @param HttpPingMonitor $httpPingMonitor
71
     */
72
    public function httpPingDown(HttpPingMonitor $httpPingMonitor)
73
    {
74
        $additionalInfo = '';
75
        if ($httpPingMonitor->getCheckPhrase() && ! $httpPingMonitor->getResponseContainsPhrase()) {
76
            $additionalInfo = " Response did not contain \"{$httpPingMonitor->getCheckPhrase()}\"";
77
        }
78
79
        $this->sendNotification(
80
            'whenHttpPingDown',
81
            "HTTP Ping Failed: {$httpPingMonitor->getUrl()}!",
82
            "HTTP Ping Failed for {$httpPingMonitor->getUrl()}! Response Code {$httpPingMonitor->getResponseCode()}.{$additionalInfo}",
83
            BaseSender::TYPE_ERROR
84
        );
85
    }
86
87
    /**
88
     * @param SSLCertificateMonitor $sslCertificateMonitor
89
     */
90
    public function sslCertificateValid(SSLCertificateMonitor $sslCertificateMonitor)
91
    {
92
        $this->sendNotification(
93
            'whenSSLCertificateValid',
94
            "SSL Certificate Valid: {$sslCertificateMonitor->getUrl()}",
95
            "SSL Certificate is valid for {$sslCertificateMonitor->getUrl()}. Expires in {$sslCertificateMonitor->getCertificateDaysUntilExpiration()} days.",
96
            BaseSender::TYPE_SUCCESS
97
        );
98
    }
99
100
    /**
101
     * @param SSLCertificateMonitor $sslCertificateMonitor
102
     */
103 View Code Duplication
    public function sslCertificateInvalid(SSLCertificateMonitor $sslCertificateMonitor)
0 ignored issues
show
This method seems to be duplicated in 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...
104
    {
105
        $this->sendNotification(
106
            'whenSSLCertificateInvalid',
107
            "SSL Certificate Invalid: {$sslCertificateMonitor->getUrl()}",
108
            "SSL Certificate is invalid for {$sslCertificateMonitor->getUrl()}. Certificate domain is {$sslCertificateMonitor->getCertificateDomain()}. Certificate expiration date is {$sslCertificateMonitor->getCertificateExpiration()} ({$sslCertificateMonitor->getCertificateDaysUntilExpiration()} days).",
109
            BaseSender::TYPE_ERROR
110
        );
111
    }
112
113
    /**
114
     * @param SSLCertificateMonitor $sslCertificateMonitor
115
     */
116 View Code Duplication
    public function sslCertificateExpiring(SSLCertificateMonitor $sslCertificateMonitor)
0 ignored issues
show
This method seems to be duplicated in 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...
117
    {
118
        $this->sendNotification(
119
            'whenSSLCertificateInvalid',
120
            "SSL Certificate Expiring: {$sslCertificateMonitor->getUrl()}",
121
            "SSL Certificate for {$sslCertificateMonitor->getUrl()} is expiring on {$sslCertificateMonitor->getCertificateExpiration()} ({$sslCertificateMonitor->getCertificateDaysUntilExpiration()} days).",
122
            BaseSender::TYPE_ERROR
123
        );
124
    }
125
126
    /**
127
     * @param string $eventName
128
     * @param string $subject
129
     * @param string $message
130
     * @param string $type
131
     */
132
    protected function sendNotification($eventName, $subject, $message, $type)
133
    {
134
        $senderNames = config("server-monitor.notifications.events.{$eventName}");
135
136
        collect($senderNames)
137
            ->map(function ($senderName) {
138
                $className = $senderName;
139
140
                if (file_exists(__DIR__.'/Senders/'.ucfirst($senderName).'.php')) {
141
                    $className = '\\EricMakesStuff\\ServerMonitor\\Notifications\\Senders\\'.ucfirst($senderName);
142
                }
143
144
                return app($className);
145
            })
146
            ->each(function (SendsNotifications $sender) use ($subject, $message, $type) {
147
                try {
148
                    $sender
149
                        ->setSubject($subject)
150
                        ->setMessage($message)
151
                        ->setType($type)
152
                        ->send();
153
                } catch (Exception $exception) {
154
                    $errorMessage = "Server Monitor notifier failed because {$exception->getMessage()}"
155
                        .PHP_EOL
156
                        .$exception->getTraceAsString();
157
158
                    $this->log->error($errorMessage);
159
                    monitorConsoleOutput()->error($errorMessage);
0 ignored issues
show
Documentation Bug introduced by
The method error does not exist on object<EricMakesStuff\Se...\Helpers\ConsoleOutput>? 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...
160
                }
161
            });
162
    }
163
}
164