Last active
August 28, 2015 21:19
-
-
Save SoftCreatR/fc86180f3dbd433cc51a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<data xmlns="http://www.woltlab.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.woltlab.com http://www.woltlab.com/XSD/typhoon/eventListener.xsd"> | |
<import> | |
<eventlistener> | |
<eventclassname>wcf\data\user\notification\UserNotificationAction</eventclassname> | |
<eventname>finalizeAction</eventname> | |
<listenerclassname>wcf\system\event\listener\UserNotificationPushoverListener</listenerclassname> | |
<environment>user</environment> | |
</eventlistener> | |
</import> | |
</data> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace wcf\system\pushover\API; | |
use wcf\util\ArrayUtil; | |
use wcf\util\HTTPRequest; | |
use wcf\util\JSON; | |
use wcf\util\StringUtil; | |
/** | |
* Pushover API | |
* | |
* @author Sascha Greuel | |
* @copyright 2015 Sascha Greuel | |
* @license All rights reserved | |
* @package de.softcreatr.typhoon.pushover | |
* @subpackage system.pushover.api | |
* @category Community Framework | |
*/ | |
final class PushoverApi { | |
// API version | |
const API_VERSION = '1'; | |
// API version | |
const API_BASE_URL = 'https://api.pushover.net/'; | |
/** | |
* The user/group key (not e-mail address) of your user (or you) | |
* @var string | |
*/ | |
private $userToken; | |
/** | |
* Your message (max. 512 characters) | |
* @var string | |
*/ | |
private $message; | |
/** | |
* Further options | |
* @var array | |
*/ | |
private $options = array(); | |
/** | |
* Constructs a new instance of the Pushover API. | |
* | |
* @param string $user | |
* @param string $message | |
* @param array<string> $options | |
*/ | |
public function __construct($userToken, $message, array $options = array()) { | |
$userToken = StringUtil::trim($userToken); | |
$message = StringUtil::trim($message); | |
$options = ArrayUtil::trim($options, false); | |
$this->setUser($userToken); | |
$this->setOptions($options); | |
$this->message = $message; | |
} | |
/** | |
* Send Push notifications | |
*/ | |
public function push() { | |
if (PUSHOVER_API_TOKEN != '' && !empty($this->userToken) && !empty($this->message)) { | |
try { | |
$request = new HTTPRequest(self::API_BASE_URL . self::API_VERSION . '/messages.json', array(), array_merge($this->options, array( | |
'token' => PUSHOVER_API_TOKEN, | |
'user' => $this->userToken, | |
'message' => $this->message | |
))); | |
$request->execute(); | |
$reply = $request->getReply(); | |
$content = JSON::decode($reply['body']); | |
return $content['status'] == 1; | |
} | |
catch (\Exception $e) { | |
// force logging | |
$e->getExceptionID(); | |
} | |
} | |
return false; | |
} | |
/*********** SETTER ***********/ | |
/** | |
* Sets and validates a user token | |
* | |
* @param string $user | |
*/ | |
private function setUser($userToken) { | |
if (PUSHOVER_API_TOKEN) { | |
try { | |
// fetch access_token | |
$request = new HTTPRequest(self::API_BASE_URL . self::API_VERSION . '/users/validate.json', array(), array( | |
'token' => PUSHOVER_API_TOKEN, | |
'user' => $userToken | |
)); | |
$request->execute(); | |
$reply = $request->getReply(); | |
$content = JSON::decode($reply['body']); | |
if ($content['status'] == 1) { | |
$this->userToken = $userToken; | |
} | |
} | |
catch (\Exception $e) { | |
// force logging | |
$e->getExceptionID(); | |
} | |
} | |
} | |
/** | |
* Sets options and applies default values when an option is omitted or misconfigured. | |
* | |
* @param array $options | |
*/ | |
private function setOptions(array $options) { | |
if (!isset($options['timestamp'])) { | |
$options['timestamp'] = TIME_NOW; | |
} | |
if (!isset($options['priority'])) { | |
$options['priority'] = 0; | |
} | |
if (!isset($options['sound'])) { | |
$options['sound'] = 'none'; | |
} | |
if (!isset($options['retry']) || intval($options['retry']) < 30) { | |
$options['retry'] = 30; | |
} | |
else if (intval($options['retry']) > 86400) { | |
$options['retry'] = 86400; | |
} | |
$this->options = $options; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace wcf\system\event\listener; | |
use wcf\system\event\IEventListener; | |
use wcf\system\WCF; | |
use wcf\system\pushover\api\PushoverApi; | |
use wcf\system\user\notification\UserNotificationHandler; | |
use wcf\system\database\util\PreparedStatementConditionBuilder; | |
/** | |
* Pushover notifications | |
* | |
* @author Sascha Greuel | |
* @copyright 2015 Sascha Greuel | |
* @license All rights reserved | |
* @package de.softcreatr.typhoon.pushover | |
* @subpackage system.event.listener | |
* @category Community Framework | |
*/ | |
class UserNotificationPushoverListener implements IEventListener { | |
/** | |
* @see \wcf\system\event\IEventListener::execute() | |
*/ | |
public function execute($eventObj, $className, $eventName) { | |
if ($eventObj->getActionName() !== 'addRecipients' && $eventObj->getActionName() !== 'createStackable' && $eventObj->getActionName() !== 'createDefault') return; | |
$pushoverNotifications = $notificationIDs = array(); | |
$returnValues = $eventObj->getReturnValues(); | |
$parameters = $eventObj->getParameters(); | |
foreach ($returnValues['returnValues'] as $returnValue) { | |
$notificationIDs[] = $returnValue['object']->notificationID; | |
// let's build some fugly arrays to avoid overheads | |
$user = $parameters['recipients'][$returnValue['object']->userID]; | |
$pushoverNotifications[$user->languageID][] = array( | |
$user->pushoverUserKey => array( | |
'device' => $user->pushoverDevice, | |
'sound' => $user->pushoverSound, | |
'priority' => $user->pushoverPriority | |
) | |
); | |
} | |
$conditions = new PreparedStatementConditionBuilder(); | |
$conditions->add("notification.notificationID IN (?)", $notificationIDs); | |
$sql = "SELECT notification.*, notification_event.eventID, object_type.objectType | |
FROM wcf" . WCF_N . "_user_notification notification | |
LEFT JOIN wcf" . WCF_N . "_user_notification_event notification_event | |
ON (notification_event.eventID = notification.eventID) | |
LEFT JOIN wcf" . WCF_N . "_object_type object_type | |
ON (object_type.objectTypeID = notification_event.objectTypeID) | |
" . $conditions . " | |
ORDER BY notification.time DESC"; | |
$statement = WCF::getDB()->prepareStatement($sql); | |
$statement->execute($conditions->getParameters()); | |
$notifications = UserNotificationHandler::getInstance()->processNotifications(array( | |
$statement->fetchObject('\wcf\data\user\notification\UserNotification') | |
)); | |
if (!empty($notifications['notifications'][0]['event'])) { | |
$notification = $notifications['notifications'][0]['event']; | |
$link = $notification->getLink(); | |
foreach ($pushoverNotifications as $languageID => $pushoverDataArr) { | |
// set locale | |
WCF::setLanguage($languageID); | |
// localized texts | |
$title = WCF::getLanguage()->getDynamicVariable('wcf.user.notification.mail.daily.subject', array('count' => 1)); | |
$message = strip_tags($notification->getMessage()); | |
foreach ($pushoverDataArr as $pushoverData) { | |
foreach ($pushoverData as $key => $options) { | |
// push | |
$pushoverApi = new PushoverAPI($key, $message, array_merge($options, array( | |
'title' => $title, | |
'url' => $link, | |
'url_title' => $link | |
))); | |
$pushoverApi->push(); | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment