#2539 - Feature request: Pushover.net notifications

This commit is contained in:
Zdeněk Jurka 2017-01-12 16:32:30 +01:00
parent a1cb113902
commit 556db98835
13 changed files with 519 additions and 78 deletions

View File

@ -78,5 +78,7 @@ require_once(CLASS_DIR . '/transaction.class.php');
require_once(CLASS_DIR . '/roundstats.class.php');
require_once(CLASS_DIR . '/news.class.php');
require_once(CLASS_DIR . '/api.class.php');
require_once(CLASS_DIR . '/ipushnotification.interface.php');
require_once(CLASS_DIR . '/pushnotification.class.php');
require_once(INCLUDE_DIR . '/lib/Michelf/Markdown.php');
require_once(INCLUDE_DIR . '/lib/scrypt.php');

View File

@ -40,6 +40,10 @@ class Base {
public function setSalty($salt) {
$this->salty = $salt;
}
/**
* @var Smarty
*/
var $smarty;
public function setSmarty($smarty) {
$this->smarty = $smarty;
}

View File

@ -0,0 +1,6 @@
<?php
interface IPushNotification {
public static function getName();
public static function getParameters();
public function notify($message, $severity, $event);
}

View File

@ -149,14 +149,27 @@ class Notification extends Mail {
return false;
}
// Check if this user wants strType notifications
$stmt = $this->mysqli->prepare("SELECT account_id FROM $this->tableSettings WHERE type = ? AND active = 1 AND account_id = ?");
if ($stmt && $stmt->bind_param('si', $strType, $account_id) && $stmt->execute() && $stmt->bind_result($id) && $stmt->fetch()) {
if ($stmt->close() && $this->sendMail('notifications/' . $strType, $aMailData) && $this->addNotification($account_id, $strType, $aMailData)) {
return true;
} else {
$this->setErrorMessage('SendMail call failed: ' . $this->getError());
return false;
}
$stmt = $this->mysqli->prepare("SELECT type FROM $this->tableSettings WHERE type IN (?, ?) AND active = 1 AND account_id = ?");
if ($stmt && $stmt->bind_param('ssi', $strType, substr('push_'.$strType, 0, 15), $account_id) && $stmt->execute() && $result = $stmt->get_result()) {
$types = array_map(function($a){ return reset($a);}, $result->fetch_all(MYSQLI_ASSOC));
$stmt->close();
$result = true;
foreach ($types as $type){
if (strpos($type, 'push_') === 0){
if (PushNotification::Instance() instanceof PushNotification){
$result &= PushNotification::Instance()->sendNotification($account_id, $strType, $aMailData);
}
} else {
$result &= $this->sendMail('notifications/' . $strType, $aMailData);
}
}
if ($result){
$this->addNotification($account_id, $strType, $aMailData);
return true;
} else {
$this->setErrorMessage('SendMail call failed: ' . $this->getError());
return false;
}
} else {
$this->setErrorMessage('User disabled ' . $strType . ' notifications');
return true;

View File

@ -0,0 +1,41 @@
<?php
class Notifications_NotifyMyAndroid implements IPushNotification {
private $apiKey;
public function __construct($apikey){
$this->apiKey = $apikey;
}
static $priorities = array(
0 => 'info',
2 => 'error',
);
public static function getName(){
return "notifymyandroid.com";
}
public static function getParameters(){
return array(
'apikey' => 'API key',
);
}
public function notify($message, $severity = 'info', $event = null){
curl_setopt_array($ch = curl_init(), array(
CURLOPT_URL => "https://www.notifymyandroid.com/publicapi/notify",
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => http_build_query($data = array(
"apikey" => $this->apiKey,
"application" => "CryptoGlance",
"description" => $message,
"content-type" => "text/html",
"event" => $event,
"priority" => array_search($severity, self::$priorities),
)),
));
curl_exec($ch);
curl_close($ch);
}
}

View File

@ -0,0 +1,46 @@
<?php
class Notifications_Pushover implements IPushNotification {
private $token;
private $user;
public function __construct($token, $user){
$this->token = $token;
$this->user = $user;
}
static $priorities = array(
0 => 'info',
1 => 'warning',
2 => 'error',
);
public static function getName(){
return "pushover.net";
}
public static function getParameters(){
return array(
'token' => 'API Token/Key',
'user' => 'Your User Key',
);
}
public function notify($message, $severity = 'info', $event = null){
curl_setopt_array($ch = curl_init(), array(
CURLOPT_URL => "https://api.pushover.net/1/messages.json",
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => http_build_query($data = array(
"token" => $this->token,
"user" => $this->user,
"message" => $code = strip_tags(preg_replace('/<([\/]?)span[^>]*>/i', '<\1b>', $message), "<b><i><u><a><font><p><br>"),
"title" => strip_tags($event),
"priority" => (int)array_search($severity, self::$priorities),
"timestamp" => time(),
"html" => preg_match('/<[^>]+>/', $code),
)),
));
curl_exec($ch);
curl_close($ch);
}
}

View File

@ -0,0 +1,184 @@
<?php
class PushNotification extends Base {
var $tableSettings = 'push_notification_settings';
private static function getClassesInFile($file){
$classes = array();
$tokens = token_get_all(file_get_contents($file));
$count = count($tokens);
for ($i = 2; $i < $count; $i++) {
if ($tokens[$i - 2][0] == T_CLASS && $tokens[$i - 1][0] == T_WHITESPACE && $tokens[$i][0] == T_STRING) {
$class_name = $tokens[$i][1];
$classes[] = $class_name;
}
}
return $classes;
}
private static $classes = null;
public function getClasses(){
if (self::$classes === null){
$directory = new DirectoryIterator(__DIR__.'/push_notification');
foreach ($directory as $fileInfo) {
if (($fileInfo->getExtension() != 'php') || $fileInfo->isDot()) {
continue;
}
foreach (self::getClassesInFile($fileInfo->getRealPath()) as $class){
if (!class_exists($class)){
include $fileInfo->getRealPath();
}
$cr = new ReflectionClass($class);
if ($cr->isSubclassOf('IPushNotification')){
self::$classes[$class] = array($fileInfo->getFilename(), $cr->getMethod('getName')->invoke(null), $cr->getMethod('getParameters')->invoke(null));
}
}
}
}
return self::$classes;
}
public function getClassesForSmarty(){
$c = $this->getClasses();
return array_map(function($a, $b){
return array(
'class' => $b,
'file' => $a[0],
'name' => $a[1],
'parameters' => $a[2],
);
}, $c, array_keys($c));
}
/**
* @param string|array $notificator
* @param array $data
* @return IPushNotification|bool
*/
public function getNotificatorInstance($notificator, $data){
$class = null;
$file = null;
if (is_array($notificator)){
if (count($notificator) == 2){
list($class, $file) = $notificator;
} else {
$class = reset($notificator);
}
} else {
$class = $notificator;
}
if (!class_exists($class)){
if ($file === null){
foreach (self::getClasses() as $_class => $_info){
if ($_class == $class){
$file = $_info[0];
break;
}
}
} else {
include __DIR__.'/push_notification/'.$file;
}
if (!class_exists($class)){
return false;
}
}
$cr = new ReflectionClass($class);
$constructor = $cr->getConstructor();
$constructorParameters = array();
foreach (array_map(function($a){ return $a->getName();}, $constructor->getParameters()) as $param){
$constructorParameters[] = array_key_exists($param, $data)?$data[$param]:null;
}
$instance = $cr->newInstanceArgs($constructorParameters);
return $instance;
}
/**
* Update accounts push notification settings
* @param account_id int Account ID
* @param data array Data array
* @return bool
**/
public function updateSettings($account_id, $data) {
$this->debug->append("STA " . __METHOD__, 4);
$stmt = $this->mysqli->prepare("INSERT INTO $this->tableSettings (value, account_id) VALUES (?, ?) ON DUPLICATE KEY UPDATE value = VALUES(value)");
if (!($stmt && $stmt->bind_param('si', json_encode($data), $account_id) && $stmt->execute())) {
$this->setErrorMessage($this->getErrorMsg('E0047', __CLASS__));
return $this->sqlError();
}
$this->log->log("info", "User $account_id updated notification settings");
return true;
}
/**
* Fetch notification settings for user account
* @param id int Account ID
* @return array Notification settings
**/
public function getNotificationSettings($account_id) {
$this->debug->append("STA " . __METHOD__, 4);
$stmt = $this->mysqli->prepare("SELECT value FROM $this->tableSettings WHERE account_id = ?");
if ($stmt && $stmt->bind_param('i', $account_id) && $stmt->execute() && $result = $stmt->get_result()) {
if ($result->num_rows) {
/* @var $result mysqli_result */
$aData = json_decode(current($result->fetch_row()), true);
return $aData;
} else {
return array(
'class' => false,
'params' => null,
'file' => null,
);
}
}
return $this->sqlError('E0045');
}
private static $instance = null;
/**
* @param PushNotification $instance
*/
public static function Instance($instance = null){
if (func_num_args() == 0){
return self::$instance;
}
return self::$instance = $instance;
}
public function sendNotification($account_id, $template, $aData){
$settings = $this->getNotificationSettings($account_id);
if ($settings['class']){
$instance = $this->getNotificatorInstance(array($settings['class'], $settings['file']), $settings['params']);
if ($instance){
$this->smarty->assign('WEBSITENAME', $this->setting->getValue('website_name'));
$this->smarty->assign('SUBJECT', $aData['subject']);
$this->smarty->assign('DATA', $aData);
$message = false;
foreach (array('/mail/push_notifications/', '/mail/notifications/') as $dir){
$this->smarty->clearCache($templateFile = TEMPLATE_DIR.$dir.$template.'.tpl');
try {
$message = $this->smarty->fetch($templateFile);
} catch (SmartyException $e){
}
}
if ($message){
$instance->notify($message, 'info', $aData['subject']);
}
}
}
return true;
}
}
$pushnotification = PushNotification::Instance(new PushNotification());
$pushnotification->setDebug($debug);
$pushnotification->setLog($log);
$pushnotification->setMysql($mysqli);
$pushnotification->setSmarty($smarty);
$pushnotification->setConfig($config);
$pushnotification->setSetting($setting);
$pushnotification->setErrorCodes($aErrorCodes);

View File

@ -8,7 +8,25 @@ if ($user->isAuthenticated()) {
} else {
if (@$_REQUEST['do'] == 'save') {
if (!$config['csrf']['enabled'] || $config['csrf']['enabled'] && $csrftoken->valid) {
if ($notification->updateSettings($_SESSION['USERDATA']['id'], $_REQUEST['data'])) {
$pushSettings = array(
'class' => $_REQUEST['pushnotification-class'],
'params' => null,
'file' => null,
);
if ($pushSettings['class'] && array_key_exists($pushSettings['class'], $_REQUEST['pushnotification'])){
$pushSettings['params'] = $_REQUEST['pushnotification'][$pushSettings['class']];
}
if ($pushSettings['class']){
$c = $pushnotification->getClasses();
if (array_key_exists($pushSettings['class'], $c)){
$pushSettings['file'] = $c[$pushSettings['class']][0];
}
}
if (!$pushnotification->updateSettings($_SESSION['USERDATA']['id'], $pushSettings)){
$_SESSION['POPUP'][] = array('CONTENT' => $pushnotification->getError(), 'TYPE' => 'alert alert-danger');
}elseif ($notification->updateSettings($_SESSION['USERDATA']['id'], $_REQUEST['data'])) {
$_SESSION['POPUP'][] = array('CONTENT' => 'Updated notification settings', 'TYPE' => 'alert alert-success');
} else {
$_SESSION['POPUP'][] = array('CONTENT' => $notification->getError(), 'TYPE' => 'alert alert-danger');
@ -29,8 +47,12 @@ if ($user->isAuthenticated()) {
// Fetch user notification settings
$aSettings = $notification->getNotificationSettings($_SESSION['USERDATA']['id']);
$aPushSettings = $pushnotification->getNotificationSettings($_SESSION['USERDATA']['id']);
$aSmartyClasses = $pushnotification->getClassesForSmarty();
$smarty->assign('NOTIFICATIONS', $aNotifications);
$smarty->assign('PUSHNOTIFICATIONS', $aSmartyClasses);
$smarty->assign('PUSHSETTINGS', $aPushSettings);
$smarty->assign('SETTINGS', $aSettings);
$smarty->assign('CONTENT', 'default.tpl');
}

View File

@ -660,3 +660,8 @@ div.fade {
}
/* END EDIT */
.push-notifications-params,
.push-notifications-disabled .push-notifications {
display : none;
}

View File

@ -88,3 +88,14 @@ $(function() {
});
});
$(document).on('change', '#push-notifications', function(e){
var notificationClass = $(this).val();
$('#push-notifications-pannel').toggleClass('push-notifications-disabled', notificationClass == 0);
$('.push-notifications-params').each(function(){
var $this = $(this);
$this.toggle($this.attr('data-class-name') == notificationClass);
});
}).ready(function(){
$('#push-notifications').trigger('change');
});

View File

@ -142,7 +142,7 @@ CREATE TABLE IF NOT EXISTS `settings` (
UNIQUE KEY `setting` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `settings` (`name`, `value`) VALUES ('DB_VERSION', '1.0.1');
INSERT INTO `settings` (`name`, `value`) VALUES ('DB_VERSION', '1.0.2');
CREATE TABLE IF NOT EXISTS `shares` (
`id` bigint(30) NOT NULL AUTO_INCREMENT,
@ -248,6 +248,12 @@ CREATE TABLE `statistics_users` (
KEY `account_id_timestamp` (`account_id`,`timestamp`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `push_notification_settings` (
`account_id` int(11) NOT NULL,
`value` text DEFAULT NULL,
PRIMARY KEY (`account_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

View File

@ -1,72 +1,138 @@
<div class="row">
<form class="col-lg-4" method="POST" role="form">
<input type="hidden" name="page" value="{$smarty.request.page|escape}">
<input type="hidden" name="action" value="{$smarty.request.action|escape}">
<input type="hidden" name="do" value="save">
<input type="hidden" name="ctoken" value="{$CTOKEN|escape|default:""}">
<div class="panel panel-info">
<div class="panel-heading">
<i class="fa fa-gear fa-fw"></i> Notification Settings
</div>
<div class="panel-body no-padding">
<table class="table table-striped table-bordered table-hover">
{if $DISABLE_IDLEWORKERNOTIFICATIONS|default:"" != 1}
<tr>
<td>
<label>Idle Worker</label>
</td>
<td>
<input type="hidden" name="data[idle_worker]" value="0" />
<input type="checkbox" class="switch" data-size="mini" name="data[idle_worker]" id="idle_worker" value="1"{nocache}{if $SETTINGS['idle_worker']|default:"0" == 1}checked{/if}{/nocache} />
</td>
</tr>
{/if}
{if $DISABLE_BLOCKNOTIFICATIONS|default:"" != 1}
<tr>
<td>
<label>New Blocks</label>
</td>
<td>
<input type="hidden" name="data[new_block]" value="0" />
<input type="checkbox"class="switch" data-size="mini" name="data[new_block]" id="new_block" value="1"{nocache}{if $SETTINGS['new_block']|default:"0" == 1}checked{/if}{/nocache} />
</td>
</tr>
{/if}
<tr>
<td>
<label>Payout</label>
</td>
<td>
<input type="hidden" name="data[payout]" value="0" />
<input type="checkbox" class="switch" data-size="mini" name="data[payout]" id="payout" value="1"{nocache}{if $SETTINGS['payout']|default:"0" == 1}checked{/if}{/nocache} />
</td>
</tr>
<tr>
<td>
<label>Successful Login</label>
</td>
<td>
<input type="hidden" name="data[success_login]" value="0" />
<input type="checkbox" class="switch" data-size="mini" name="data[success_login]" id="success_login" value="1"{nocache}{if $SETTINGS['success_login']|default:"0" == 1}checked{/if}{/nocache} />
</td>
</tr>
{if $DISABLE_POOLNEWSLETTER|default:"" != 1}
<tr>
<td>
<label>Pool Newsletter</label>
</td>
<td>
<input type="hidden" name="data[newsletter]" value="0" />
<input type="checkbox"class="switch" data-size="mini" name="data[newsletter]" id="new_block" value="1"{nocache}{if $SETTINGS['newsletter']|default:"1" == 1}checked{/if}{/nocache} />
</td>
</tr>
{/if}
</table>
</div>
<div class="panel-footer">
<input type="submit" value="Update" class="btn btn-success btn-sm">
</div>
</form>
<div class="col-lg-4">
<form method="POST" role="form">
<input type="hidden" name="page" value="{$smarty.request.page|escape}">
<input type="hidden" name="action" value="{$smarty.request.action|escape}">
<input type="hidden" name="do" value="save">
<input type="hidden" name="ctoken" value="{$CTOKEN|escape|default:""}">
<div class="panel panel-info">
<div class="panel-heading">
<i class="fa fa-gear fa-fw"></i> Push Notification Settings
</div>
<div class="panel-body no-padding">
<table class="table table-striped table-bordered table-hover">
<tbody>
<tr>
<td>
<label>Push Notifications</label>
</td>
<td>
<select name="pushnotification-class" class="form-control select-mini" id="push-notifications">
<option value="0">Disabled</option>
{section pushnotification $PUSHNOTIFICATIONS}
<option value="{$PUSHNOTIFICATIONS[pushnotification].class}"{nocache}{if $PUSHNOTIFICATIONS[pushnotification].class == $PUSHSETTINGS.class} selected="selected"{/if}{/nocache}>{$PUSHNOTIFICATIONS[pushnotification].name}</option>
{/section}
</select>
</td>
</tbody>
{section pushnotification $PUSHNOTIFICATIONS}
<tbody class="push-notifications-params" data-class-name="{$PUSHNOTIFICATIONS[pushnotification].class}">
{foreach $PUSHNOTIFICATIONS[pushnotification].parameters key=name item=text}
<tr>
<td><label>{$text}</label></td>
<td><input class="form-control" type="text" name="pushnotification[{$PUSHNOTIFICATIONS[pushnotification].class}][{$name}]" {nocache}value="{$PUSHSETTINGS.params[$name]|default:""}"{/nocache}/></td>
</tr>
{/foreach}
</tbody>
{/section}
</table>
</div>
</div>
<div class="panel panel-info">
<div class="panel-heading">
<i class="fa fa-gear fa-fw"></i> Notification Settings
</div>
<div class="panel-body no-padding push-notifications-disabled" id="push-notifications-pannel">
<table class="table table-striped table-bordered table-hover">
<thread>
<tr>
<th>Event</th>
<th>Email</th>
<th class="push-notifications">Push</th>
</tr>
</thread>
<tbody>
{if $DISABLE_IDLEWORKERNOTIFICATIONS|default:"" != 1}
<tr>
<td>
<label>Idle Worker</label>
</td>
<td>
<input type="hidden" name="data[idle_worker]" value="0" />
<input type="checkbox" class="switch" data-size="mini" name="data[idle_worker]" id="idle_worker" value="1"{nocache}{if $SETTINGS['idle_worker']|default:"0" == 1}checked{/if}{/nocache} />
</td>
<td class="push-notifications">
<input type="hidden" name="data[push_idle_worke]" value="0" />
<input type="checkbox" class="switch" data-size="mini" name="data[push_idle_worke]" id="push_idle_worke" value="1"{nocache}{if $SETTINGS['push_idle_worke']|default:"0" == 1}checked{/if}{/nocache} />
</td>
</tr>
{/if}
{if $DISABLE_BLOCKNOTIFICATIONS|default:"" != 1}
<tr>
<td>
<label>New Blocks</label>
</td>
<td>
<input type="hidden" name="data[new_block]" value="0" />
<input type="checkbox"class="switch" data-size="mini" name="data[new_block]" id="new_block" value="1"{nocache}{if $SETTINGS['new_block']|default:"0" == 1}checked{/if}{/nocache} />
</td>
<td class="push-notifications">
<input type="hidden" name="data[push_new_block]" value="0" />
<input type="checkbox"class="switch" data-size="mini" name="data[push_new_block]" id="new_block" value="1"{nocache}{if $SETTINGS['push_new_block']|default:"0" == 1}checked{/if}{/nocache} />
</td>
</tr>
{/if}
<tr>
<td>
<label>Payout</label>
</td>
<td>
<input type="hidden" name="data[payout]" value="0" />
<input type="checkbox" class="switch" data-size="mini" name="data[payout]" id="payout" value="1"{nocache}{if $SETTINGS['payout']|default:"0" == 1}checked{/if}{/nocache} />
</td>
<td class="push-notifications">
<input type="hidden" name="data[push_payout]" value="0" />
<input type="checkbox" class="switch" data-size="mini" name="data[push_payout]" id="push_payout" value="1"{nocache}{if $SETTINGS['push_payout']|default:"0" == 1}checked{/if}{/nocache} />
</td>
</tr>
<tr>
<td>
<label>Successful Login</label>
</td>
<td>
<input type="hidden" name="data[success_login]" value="0" />
<input type="checkbox" class="switch" data-size="mini" name="data[success_login]" id="success_login" value="1"{nocache}{if $SETTINGS['success_login']|default:"0" == 1}checked{/if}{/nocache} />
</td>
<td class="push-notifications">
<input type="hidden" name="data[push_success_lo]" value="0" />
<input type="checkbox" class="switch" data-size="mini" name="data[push_success_lo]" id="push_success_lo" value="1"{nocache}{if $SETTINGS['push_success_lo']|default:"0" == 1}checked{/if}{/nocache} />
</td>
</tr>
{if $DISABLE_POOLNEWSLETTER|default:"" != 1}
<tr>
<td>
<label>Pool Newsletter</label>
</td>
<td>
<input type="hidden" name="data[newsletter]" value="0" />
<input type="checkbox"class="switch" data-size="mini" name="data[newsletter]" id="newsletter" value="1"{nocache}{if $SETTINGS['newsletter']|default:"1" == 1}checked{/if}{/nocache} />
</td>
<td class="push-notifications">
<input type="hidden" name="data[push_newsletter]" value="0" />
<input type="checkbox"class="switch" data-size="mini" name="data[push_newsletter]" id="push_newsletter" value="1"{nocache}{if $SETTINGS['push_newsletter']|default:"0" == 1}checked{/if}{/nocache} />
</td>
</tr>
{/if}
</tbody>
</table>
</div>
<div class="panel-footer">
<input type="submit" value="Update" class="btn btn-success btn-sm">
</div>
</form>
</div>
</div>
<div class="col-lg-8">

View File

@ -0,0 +1,35 @@
<?php
function run_102() {
// Ugly but haven't found a better way
global $setting, $config, $coin_address, $user, $mysqli, $notification;
// Version information
$db_version_old = '1.0.1'; // What version do we expect
$db_version_new = '1.0.2'; // What is the new version we wish to upgrade to
$db_version_now = $setting->getValue('DB_VERSION'); // Our actual version installed
// Upgrade specific variables
$aSql[] = "
CREATE TABLE IF NOT EXISTS `push_notification_settings` (
`account_id` int(11) NOT NULL,
`value` text DEFAULT NULL,
PRIMARY KEY (`account_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
";
if ($db_version_now == $db_version_old && version_compare($db_version_now, DB_VERSION, '<')) {
// Run the upgrade
echo '- Starting database migration to version ' . $db_version_new . PHP_EOL;
foreach ($aSql as $sql) {
echo '- Preparing: ' . $sql . PHP_EOL;
$stmt = $mysqli->prepare($sql);
if ($stmt && $stmt->execute()) {
echo '- success' . PHP_EOL;
} else {
echo '- failed: ' . $mysqli->error . PHP_EOL;
exit(1);
}
}
}
}
?>