From b4cc92a4b4fced2ab64f92fd850f1e5ada5abcab Mon Sep 17 00:00:00 2001 From: rsgrinko Date: Fri, 24 Dec 2021 16:03:36 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20=D0=BF=D1=83=D1=88=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inc/bootstrap.php | 2 + inc/lib/CPushover.class.php | 172 ++++++++++++++++++++++++++++++++++++ 2 files changed, 174 insertions(+) create mode 100644 inc/lib/CPushover.class.php diff --git a/inc/bootstrap.php b/inc/bootstrap.php index eac21c8..2737410 100644 --- a/inc/bootstrap.php +++ b/inc/bootstrap.php @@ -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 diff --git a/inc/lib/CPushover.class.php b/inc/lib/CPushover.class.php new file mode 100644 index 0000000..f55cafa --- /dev/null +++ b/inc/lib/CPushover.class.php @@ -0,0 +1,172 @@ +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; + } +} \ No newline at end of file