-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAdminNotification.php
More file actions
114 lines (92 loc) · 3.87 KB
/
AdminNotification.php
File metadata and controls
114 lines (92 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
namespace Piwik\Plugins\AdminNotification;
use Piwik\Piwik;
use Piwik\Common;
use Piwik\Notification;
class AdminNotification extends \Piwik\Plugin
{
private static $hooks = array(
'Login.authenticate.successful' => 'setNotificationV3', //Version 3.X Post login handler
'SystemSettings.updated' => 'settingsChangedV3' //Version 3.X Setting updated handler
);
public function registerEvents()
{
return self::$hooks;
}
public function settingsChangedV3($settings)
{
if ($settings->getPluginName() === 'AdminNotification') {
self::cancel_notification();
$this->setNotificationV3();
}
}
public function setNotificationV3()
{
// Known issue. The alert notification is not updated until login/logout on v3.x.
// 2.X Compatibility. This method appears to be getting called in v2.X which I didn't
// believe would trigger the newer hooks.
if (!class_exists('\Piwik\Settings\Plugin\SystemSettings')) { //If class doesn't exist just get out.
return;
}
$settings = new SystemSettings();
//print_r($settings->enabled->getValue());
if ($settings->enabled->getValue()) {
//Transform newlines to avoid removal in sanitization
$message = str_replace(["\r\n","\r","\n"],"!nl",$settings->message->getValue());
$sanitized_message = Common::sanitizeInputValue($message);
//Reverse newline transform
$sanitized_message = str_replace("!nl","\n",$sanitized_message);
//Process markdown
$markdowned_message = self::minimal_markdown($sanitized_message);
$notification = new Notification($markdowned_message);
$notification->title = $settings->messageTitle->getValue();
$notification->context = $settings->context->getValue();
$notification->type = $settings->type->getValue();
$notification->priority = $settings->priority->getValue();
$notification->raw = true;
//echo "NOTIFY";
//print_r($notification);
Notification\Manager::notify('AdminNotification_notice', $notification);
Piwik::postEvent('AdminNotification.notice', [&$notification]);
} else {
self::cancel_notification();
}
}
public function deactivate()
{
self::cancel_notification();
}
private static function cancel_notification(){
Notification\Manager::cancel('AdminNotification_notice');
}
private static function minimal_markdown($escaped_input){
//Replace with common Markdown markup
$markdown_processed = preg_replace(
[
"/\*{3}([\w\s]*)\*{3}/im",
"/\*{2}([\w\s]*)\*{2}/im",
"/\*([\w\s]*)\*/im",
"/^#\s(.*)$/im",
"/^##\s(.*)$/im",
"/^###\s(.*)$/im",
"/^####\s(.*)$/im",
"/\[(.*)\]\(((?:https?:\/\/.)?(?:www\.)?[-a-zA-Z0-9@%._\+~#=]{2,256}\.[a-z]{2,6}\b(?:[-a-zA-Z0-9@:%_\+.~#?&\/\/=]*))\)/im",
"/\n/m"
],
[
"<em><strong>$1</strong></em>",
"<strong>$1</strong>",
"<em>$1</em>",
"<h1>$1</h1>",
"<h2>$1</h2>",
"<h3>$1</h3>",
"<h4>$1</h4>",
"<a target=\"_blank\" rel=\"noreferrer noopener\" href=\"$2\">$1</a>",
"<br>\n"
],
$escaped_input
);
//Remove break from headers
return preg_replace("/<\/h(\d)><br>/m","</h$1>",$markdown_processed);
}
}