]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - vendor/monolog/monolog/src/Monolog/Handler/PushoverHandler.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / vendor / monolog / monolog / src / Monolog / Handler / PushoverHandler.php
1 <?php
2
3 /*
4  * This file is part of the Monolog package.
5  *
6  * (c) Jordi Boggiano <j.boggiano@seld.be>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Monolog\Handler;
13
14 use Monolog\Logger;
15
16 /**
17  * Sends notifications through the pushover api to mobile phones
18  *
19  * @author Sebastian Göttschkes <sebastian.goettschkes@googlemail.com>
20  * @see    https://www.pushover.net/api
21  */
22 class PushoverHandler extends SocketHandler
23 {
24     private $token;
25     private $users;
26     private $title;
27     private $user;
28     private $retry;
29     private $expire;
30
31     private $highPriorityLevel;
32     private $emergencyLevel;
33     private $useFormattedMessage = false;
34
35     /**
36      * All parameters that can be sent to Pushover
37      * @see https://pushover.net/api
38      * @var array
39      */
40     private $parameterNames = array(
41         'token' => true,
42         'user' => true,
43         'message' => true,
44         'device' => true,
45         'title' => true,
46         'url' => true,
47         'url_title' => true,
48         'priority' => true,
49         'timestamp' => true,
50         'sound' => true,
51         'retry' => true,
52         'expire' => true,
53         'callback' => true,
54     );
55
56     /**
57      * Sounds the api supports by default
58      * @see https://pushover.net/api#sounds
59      * @var array
60      */
61     private $sounds = array(
62         'pushover', 'bike', 'bugle', 'cashregister', 'classical', 'cosmic', 'falling', 'gamelan', 'incoming',
63         'intermission', 'magic', 'mechanical', 'pianobar', 'siren', 'spacealarm', 'tugboat', 'alien', 'climb',
64         'persistent', 'echo', 'updown', 'none',
65     );
66
67     /**
68      * @param string       $token             Pushover api token
69      * @param string|array $users             Pushover user id or array of ids the message will be sent to
70      * @param string       $title             Title sent to the Pushover API
71      * @param int          $level             The minimum logging level at which this handler will be triggered
72      * @param Boolean      $bubble            Whether the messages that are handled can bubble up the stack or not
73      * @param Boolean      $useSSL            Whether to connect via SSL. Required when pushing messages to users that are not
74      *                                        the pushover.net app owner. OpenSSL is required for this option.
75      * @param int          $highPriorityLevel The minimum logging level at which this handler will start
76      *                                        sending "high priority" requests to the Pushover API
77      * @param int          $emergencyLevel    The minimum logging level at which this handler will start
78      *                                        sending "emergency" requests to the Pushover API
79      * @param int          $retry             The retry parameter specifies how often (in seconds) the Pushover servers will send the same notification to the user.
80      * @param int          $expire            The expire parameter specifies how many seconds your notification will continue to be retried for (every retry seconds).
81      */
82     public function __construct($token, $users, $title = null, $level = Logger::CRITICAL, $bubble = true, $useSSL = true, $highPriorityLevel = Logger::CRITICAL, $emergencyLevel = Logger::EMERGENCY, $retry = 30, $expire = 25200)
83     {
84         $connectionString = $useSSL ? 'ssl://api.pushover.net:443' : 'api.pushover.net:80';
85         parent::__construct($connectionString, $level, $bubble);
86
87         $this->token = $token;
88         $this->users = (array) $users;
89         $this->title = $title ?: gethostname();
90         $this->highPriorityLevel = Logger::toMonologLevel($highPriorityLevel);
91         $this->emergencyLevel = Logger::toMonologLevel($emergencyLevel);
92         $this->retry = $retry;
93         $this->expire = $expire;
94     }
95
96     protected function generateDataStream($record)
97     {
98         $content = $this->buildContent($record);
99
100         return $this->buildHeader($content) . $content;
101     }
102
103     private function buildContent($record)
104     {
105         // Pushover has a limit of 512 characters on title and message combined.
106         $maxMessageLength = 512 - strlen($this->title);
107
108         $message = ($this->useFormattedMessage) ? $record['formatted'] : $record['message'];
109         $message = substr($message, 0, $maxMessageLength);
110
111         $timestamp = $record['datetime']->getTimestamp();
112
113         $dataArray = array(
114             'token' => $this->token,
115             'user' => $this->user,
116             'message' => $message,
117             'title' => $this->title,
118             'timestamp' => $timestamp,
119         );
120
121         if (isset($record['level']) && $record['level'] >= $this->emergencyLevel) {
122             $dataArray['priority'] = 2;
123             $dataArray['retry'] = $this->retry;
124             $dataArray['expire'] = $this->expire;
125         } elseif (isset($record['level']) && $record['level'] >= $this->highPriorityLevel) {
126             $dataArray['priority'] = 1;
127         }
128
129         // First determine the available parameters
130         $context = array_intersect_key($record['context'], $this->parameterNames);
131         $extra = array_intersect_key($record['extra'], $this->parameterNames);
132
133         // Least important info should be merged with subsequent info
134         $dataArray = array_merge($extra, $context, $dataArray);
135
136         // Only pass sounds that are supported by the API
137         if (isset($dataArray['sound']) && !in_array($dataArray['sound'], $this->sounds)) {
138             unset($dataArray['sound']);
139         }
140
141         return http_build_query($dataArray);
142     }
143
144     private function buildHeader($content)
145     {
146         $header = "POST /1/messages.json HTTP/1.1\r\n";
147         $header .= "Host: api.pushover.net\r\n";
148         $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
149         $header .= "Content-Length: " . strlen($content) . "\r\n";
150         $header .= "\r\n";
151
152         return $header;
153     }
154
155     protected function write(array $record)
156     {
157         foreach ($this->users as $user) {
158             $this->user = $user;
159
160             parent::write($record);
161             $this->closeSocket();
162         }
163
164         $this->user = null;
165     }
166
167     public function setHighPriorityLevel($value)
168     {
169         $this->highPriorityLevel = $value;
170     }
171
172     public function setEmergencyLevel($value)
173     {
174         $this->emergencyLevel = $value;
175     }
176
177     /**
178      * Use the formatted message?
179      * @param bool $value
180      */
181     public function useFormattedMessage($value)
182     {
183         $this->useFormattedMessage = (boolean) $value;
184     }
185 }