diff --git a/include/autoloader.inc.php b/include/autoloader.inc.php index a46c4dde..ab0dd64a 100644 --- a/include/autoloader.inc.php +++ b/include/autoloader.inc.php @@ -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'); diff --git a/include/classes/base.class.php b/include/classes/base.class.php index de77b23d..41b8d245 100644 --- a/include/classes/base.class.php +++ b/include/classes/base.class.php @@ -40,6 +40,10 @@ class Base { public function setSalty($salt) { $this->salty = $salt; } + /** + * @var Smarty + */ + var $smarty; public function setSmarty($smarty) { $this->smarty = $smarty; } diff --git a/include/classes/ipushnotification.interface.php b/include/classes/ipushnotification.interface.php new file mode 100644 index 00000000..e259c849 --- /dev/null +++ b/include/classes/ipushnotification.interface.php @@ -0,0 +1,6 @@ +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; diff --git a/include/classes/push_notification/notifymyandroid.php b/include/classes/push_notification/notifymyandroid.php new file mode 100644 index 00000000..87f0b80e --- /dev/null +++ b/include/classes/push_notification/notifymyandroid.php @@ -0,0 +1,41 @@ +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); + } + } \ No newline at end of file diff --git a/include/classes/push_notification/pushover.php b/include/classes/push_notification/pushover.php new file mode 100644 index 00000000..3c232fed --- /dev/null +++ b/include/classes/push_notification/pushover.php @@ -0,0 +1,46 @@ +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), "


"), + "title" => strip_tags($event), + "priority" => (int)array_search($severity, self::$priorities), + "timestamp" => time(), + "html" => preg_match('/<[^>]+>/', $code), + )), + )); + curl_exec($ch); + curl_close($ch); + } + } \ No newline at end of file diff --git a/include/classes/pushnotification.class.php b/include/classes/pushnotification.class.php new file mode 100644 index 00000000..05cfc5b6 --- /dev/null +++ b/include/classes/pushnotification.class.php @@ -0,0 +1,184 @@ +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); + \ No newline at end of file diff --git a/include/pages/account/notifications.inc.php b/include/pages/account/notifications.inc.php index 2b5c850e..b6b910cd 100644 --- a/include/pages/account/notifications.inc.php +++ b/include/pages/account/notifications.inc.php @@ -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'); } diff --git a/public/site_assets/bootstrap/css/mpos.css b/public/site_assets/bootstrap/css/mpos.css index 5defa9ee..5f52e288 100644 --- a/public/site_assets/bootstrap/css/mpos.css +++ b/public/site_assets/bootstrap/css/mpos.css @@ -660,3 +660,8 @@ div.fade { } /* END EDIT */ + +.push-notifications-params, +.push-notifications-disabled .push-notifications { + display : none; +} \ No newline at end of file diff --git a/public/site_assets/bootstrap/js/mpos.js b/public/site_assets/bootstrap/js/mpos.js index 830319f4..206989f7 100644 --- a/public/site_assets/bootstrap/js/mpos.js +++ b/public/site_assets/bootstrap/js/mpos.js @@ -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'); +}); \ No newline at end of file diff --git a/sql/000_base_structure.sql b/sql/000_base_structure.sql index 839789eb..45718525 100644 --- a/sql/000_base_structure.sql +++ b/sql/000_base_structure.sql @@ -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 */; diff --git a/templates/bootstrap/account/notifications/default.tpl b/templates/bootstrap/account/notifications/default.tpl index 12dbcd0b..3abd9dc3 100644 --- a/templates/bootstrap/account/notifications/default.tpl +++ b/templates/bootstrap/account/notifications/default.tpl @@ -1,72 +1,138 @@

-
- - - - -
-
- Notification Settings -
-
- - {if $DISABLE_IDLEWORKERNOTIFICATIONS|default:"" != 1} - - - - - {/if} - {if $DISABLE_BLOCKNOTIFICATIONS|default:"" != 1} - - - - - {/if} - - - - - - - - - {if $DISABLE_POOLNEWSLETTER|default:"" != 1} - - - - - {/if} -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
-
- - +
+
+ + + + + +
+
+ Push Notification Settings +
+
+ + + + + + + {section pushnotification $PUSHNOTIFICATIONS} + + {foreach $PUSHNOTIFICATIONS[pushnotification].parameters key=name item=text} + + + + + {/foreach} + + {/section} +
+ + + +
+
+
+ +
+
+ Notification Settings +
+
+ + + + + + + + + + {if $DISABLE_IDLEWORKERNOTIFICATIONS|default:"" != 1} + + + + + + {/if} + {if $DISABLE_BLOCKNOTIFICATIONS|default:"" != 1} + + + + + + {/if} + + + + + + + + + + + {if $DISABLE_POOLNEWSLETTER|default:"" != 1} + + + + + + {/if} + +
EventEmailPush
+ + + + + + + +
+ + + + + + + +
+ + + + + + + +
+ + + + + + + +
+ + + + + + + +
+
+ + +
diff --git a/upgrade/definitions/1.0.1_to_1.0.2.inc.php b/upgrade/definitions/1.0.1_to_1.0.2.inc.php new file mode 100644 index 00000000..8b18ac0d --- /dev/null +++ b/upgrade/definitions/1.0.1_to_1.0.2.inc.php @@ -0,0 +1,35 @@ +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); + } + } + } +} +?>