ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
PushoverHandler.php
Go to the documentation of this file.
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 
23 {
24  private $token;
25  private $users;
26  private $title;
27  private $user;
28  private $retry;
29  private $expire;
30 
32  private $emergencyLevel;
33  private $useFormattedMessage = false;
34 
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 
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 
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 
181  public function useFormattedMessage($value)
182  {
183  $this->useFormattedMessage = (bool) $value;
184  }
185 }
$context
Definition: webdav.php:25
Stores to any socket - uses fsockopen() or pfsockopen().
static toMonologLevel($level)
Converts PSR-3 levels to Monolog ones if necessary.
Definition: Logger.php:528
user()
Definition: user.php:4
catch(Exception $e) $message
useFormattedMessage($value)
Use the formatted message?
const EMERGENCY
Urgent alert.
Definition: Logger.php:78
const CRITICAL
Critical conditions.
Definition: Logger.php:65
foreach($mandatory_scripts as $file) $timestamp
Definition: buildRTE.php:81
__construct($token, $users, $title=null, $level=Logger::CRITICAL, $bubble=true, $useSSL=true, $highPriorityLevel=Logger::CRITICAL, $emergencyLevel=Logger::EMERGENCY, $retry=30, $expire=25200)
closeSocket()
Close socket, if open.
Sends notifications through the pushover api to mobile phones.