добавлены пуши

This commit is contained in:
rsgrinko committed 2021-12-24 16:03:36 +03:00
1 parent caf6345f63
commit b4cc92a4b4
2 files changed
+174

No files matched your search

+2
View File
@@ -13,6 +13,7 @@ if (session_status() == PHP_SESSION_NONE) {
define('START_TIME', microtime(true)); // засекаем время старта скрипта
define('CORE_LOADED', true); // флаг корректного запуска
require_once __DIR__ . '/config.php'; // подключаем конфигурационный файл
require_once DIR.'/inc/lib/CPushover.class.php'; // пуш уведомления
require_once DIR.'/inc/lib/CFiles.class.php'; // работа с файлами и папками
require_once DIR.'/inc/lib/CJson.class.php'; // работа с json
require_once DIR.'/inc/lib/CMail.class.php'; // отправка почтовых сообщений
@@ -27,6 +28,7 @@ require_once DIR.'/inc/lib/CCron.class.php'; // планировщик з
require_once DIR.'/inc/lib/CIoT.class.php'; // работа с контроллером
$DB = new CDB(DB_HOST, DB_LOGIN, DB_PASSWORD, DB_NAME); // создаем объект для работы с базой данных
$DB->query('SET sql_mode = \'\''); // сбрасываем режим работы sql_mode=only_full_group_by
+172
View File
@@ -0,0 +1,172 @@
<?php
class CPushover
{
/*
* Pushover json api service url
*/
const C_API_URL = 'https://api.pushover.net/1/messages.json';
/**
* Properties storage array
* @var array
*/
private $fProperties;
/**
* cURL instance
*/
private $fCurl;
//--------------------------------------------------------------------------//
/**
* Properties getter
* @param string $aPropertyName Property name
* @return mixed
*/
public function __get($aPropertyName)
{
if(array_key_exists($aPropertyName, $this->fProperties))
return $this->fProperties[$aPropertyName];
return null;
}
/**
* Properties setter
* @param string $aPropertyName Property name
* @param mixed $aValue Property value
*/
public function __set($aPropertyName, $aValue)
{
$this->fProperties[$aPropertyName] = $aValue;
}
//--------------------------------------------------------------------------//
/**
* Class constructor
* @param string $aApplicationToken Application token
*/
public function __construct($aApplicationToken = null)
{
if(!empty($aApplicationToken))
$this->applicationToken = $aApplicationToken;
$this->fCurl = curl_init();
curl_setopt($this->fCurl, CURLOPT_URL, self::C_API_URL);
curl_setopt($this->fCurl, CURLOPT_HEADER, false);
curl_setopt($this->fCurl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->fCurl, CURLOPT_SSL_VERIFYPEER, false);
}
/**
* Class destructor
*/
public function __destruct()
{
curl_close($this->fCurl);
}
//--------------------------------------------------------------------------//
/**
* Throws an exceprion with single message
* @param mixed $aMessage
* @throws PushoverException
*/
public function throwMessage($aMessage)
{
throw new PushoverException(array($aMessage));
}
/**
* Throws an exceprion with an array of messages
* @param array $aMessages
* @throws PushoverException
*/
public function throwMessages(array $aMessages)
{
throw new PushoverException($aMessages);
}
//--------------------------------------------------------------------------//
/**
* Send pushover notification
*/
public function send()
{
if(!strlen($this->applicationToken))
$this->throwMessage('Application token is empty');
if(!strlen($this->userToken))
$this->throwMessage('User token is empty');
if(!strlen($this->notificationMessage))
$this->throwMessage('Notification message is empty');
if(intval($this->notificationTimestamp) <= 0)
$this->notificationTimestamp = time();
$lSendParams = array(
'token' => $this->applicationToken,
'user' => $this->userToken,
'device' => $this->userDevice,
'title' => $this->notificationTitle,
'message' => $this->notificationMessage,
'priority' => $this->notificationPriority,
'timestamp' => $this->notificationTimestamp,
'url' => $this->notificationUrl,
'url_title' => $this->notificationUrlTitle
);
foreach($lSendParams as $lKey => $lParam)
if(empty($lParam))
unset($lSendParams[$lKey]);
curl_setopt($this->fCurl, CURLOPT_POSTFIELDS, $lSendParams);
$lResponseJson = curl_exec($this->fCurl);
if($lResponseJson === false)
$this->throwMessage('API request error');
$lResponse = json_decode($lResponseJson, true);
if(empty($lResponse) || !is_array($lResponse))
$this->throwMessage('Bad API response');
if(!empty($lResponse['errors']))
$this->throwMessages($lResponse['errors']);
if(empty($lResponse['status']) || intval($lResponse['status']) != 1)
$this->throwMessage('Unknown notification send error');
}
}
class PushoverException extends Exception
{
/**
* Messages array
* @var array
*/
private $fMessages;
/**
* Exception constructor
* @param array $aMessages An array of messages
*/
public function __construct(array $aMessages)
{
parent::__construct('PushoverException exception');
$this->fMessages = $aMessages;
}
/**
* Get messages array
* @return array
*/
public function getMessages()
{
return empty($this->fMessages) ? array() : $this->fMessages;
}
}