Blame view

sources/apps/files_external/3rdparty/aws-sdk-php/Monolog/Handler/PushoverHandler.php 2.74 KB
31b7f2792   Kload   Upgrade to ownclo...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
  <?php
  
  /*
   * This file is part of the Monolog package.
   *
   * (c) Jordi Boggiano <j.boggiano@seld.be>
   *
   * For the full copyright and license information, please view the LICENSE
   * file that was distributed with this source code.
   */
  
  namespace Monolog\Handler;
  
  use Monolog\Logger;
  
  /**
   * Sends notifications through the pushover api to mobile phones
   *
   * @author Sebastian Göttschkes <sebastian.goettschkes@googlemail.com>
   * @see    https://www.pushover.net/api
   */
  class PushoverHandler extends SocketHandler
  {
      private $token;
      private $user;
      private $title;
  
      /**
       * @param string  $token  Pushover api token
       * @param string  $user   Pushover user id the message will be sent to
       * @param string  $title  Title sent to Pushover API
       * @param integer $level  The minimum logging level at which this handler will be triggered
       * @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not
       * @param Boolean $useSSL Whether to connect via SSL. Required when pushing messages to users that are not
       *                        the pushover.net app owner. OpenSSL is required for this option.
       */
      public function __construct($token, $user, $title = null, $level = Logger::CRITICAL, $bubble = true, $useSSL = true)
      {
          $connectionString = $useSSL ? 'ssl://api.pushover.net:443' : 'api.pushover.net:80';
          parent::__construct($connectionString, $level, $bubble);
  
          $this->token = $token;
          $this->user = $user;
          $this->title = $title ?: gethostname();
      }
  
      protected function generateDataStream($record)
      {
          $content = $this->buildContent($record);
  
          return $this->buildHeader($content) . $content;
      }
  
      private function buildContent($record)
      {
          // Pushover has a limit of 512 characters on title and message combined.
          $maxMessageLength = 512 - strlen($this->title);
          $message = substr($record['message'], 0, $maxMessageLength);
          $timestamp = $record['datetime']->getTimestamp();
  
          $dataArray = array(
              'token' => $this->token,
              'user' => $this->user,
              'message' => $message,
              'title' => $this->title,
              'timestamp' => $timestamp
          );
  
          return http_build_query($dataArray);
      }
  
      private function buildHeader($content)
      {
          $header = "POST /1/messages.json HTTP/1.1\r
  ";
          $header .= "Host: api.pushover.net\r
  ";
          $header .= "Content-Type: application/x-www-form-urlencoded\r
  ";
          $header .= "Content-Length: " . strlen($content) . "\r
  ";
          $header .= "\r
  ";
  
          return $header;
      }
  
      public function write(array $record)
      {
          parent::write($record);
          $this->closeSocket();
      }
  }