Advertisement
Guest User

PushoverClient

a guest
Feb 12th, 2015
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.29 KB | None | 0 0
  1. <?php
  2. class PushoverClient
  3. {
  4.     private $settings = [];
  5.  
  6.     public function __construct(array $settings)
  7.     {
  8.         foreach (['app_token', 'user_token', 'base_url'] as $option) {
  9.             if (empty($settings[$option])) {
  10.                 throw new Exception(t('Setting %s is required to Pushover', $option));
  11.             }
  12.         }
  13.  
  14.         $this->settings = $settings;
  15.     }
  16.  
  17.     private function getClient()
  18.     {
  19.         $client = new Pushover();
  20.         $client->setToken($this->settings['app_token']);
  21.         $client->setUser($this->settings['user_token']);
  22.  
  23.         $client->setTimestamp(time());
  24.  
  25.         return $client;
  26.     }
  27.  
  28.     public function userLogin($account)
  29.     {
  30.         if (((int)$account->uid !== 1) || in_array(ip_address(), ['1.1.1.1'])) {
  31.             return false;
  32.         }
  33.  
  34.         $client = $this->getClient();
  35.  
  36.         $client->setTitle('Admin user sign in');
  37.         $client->setMessage('Admin user has logged into this site: '.variable_get('site_name').'!');
  38.         $client->setUrl($this->settings['base_url']);
  39.  
  40.         $client->setPriority(2);
  41.         $client->setRetry(30);
  42.         $client->setExpire(60);
  43.  
  44.         if ($client->send()) {
  45.             return true;
  46.         }
  47.  
  48.         watchdog('Pushover', t('An admin user has logged into the site but there was an error pushing this over.'), [], WATCHDOG_ERROR, NULL);
  49.  
  50.         return false;
  51.     }
  52.  
  53.     public function commentInsert($comment)
  54.     {
  55.         if (((int)$comment->status !== 0) || ($comment->is_anonymous !== true)) {
  56.             return false;
  57.         }
  58.  
  59.         $client = $this->getClient();
  60.  
  61.         $client->setTitle('New comment on '.variable_get('site_name').'!');
  62.         $client->setMessage('Subject: '.$comment->subject);
  63.         $client->setUrl($this->settings['base_url'].'/node/'.$comment->nid.'#comment-'.$comment->cid);
  64.         $client->setDevice('Nexus');
  65.  
  66.         if ($client->send()) {
  67.             return true;
  68.         }
  69.  
  70.         watchdog('Pushover', t('A comment has been created but there was an error pushing that over.'), [], WATCHDOG_ERROR, NULL);
  71.  
  72.         return false;
  73.     }
  74. }
  75.  
  76. $Pushover = new PushoverClient([
  77.     'user_token' => 'uCpygdjfsndfi7233sdasdo33Yv',
  78.     'app_token' => 'aKH8Nwsdasdanl342jmsdaBWgoVe',
  79.     'base_url' => '/'
  80. ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement