-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ProcessLoginHistoryRSS.module
117 lines (103 loc) · 4.34 KB
/
ProcessLoginHistoryRSS.module
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
115
116
117
<?php namespace ProcessWire;
/**
* RSS feed for Process Login History
*
* This module provides an optional RSS feed for the data collected by the ProcessLoginHistoryHooks. RSS feeds can be
* accessed via a pre-defined URL and a key stored within the configuration settings of this module.
*
* @copyright 2016-2022 Teppo Koivula
* @license https://www.gnu.org/licenses/old-licenses/gpl-2.0.txt GNU General Public License, version 2
*/
class ProcessLoginHistoryRSS extends WireData implements Module, ConfigurableModule {
/**
* Return information about this module
*
* @return array
*/
public static function getModuleInfo() {
return [
'title' => __('Login History RSS'),
'summary' => __('Output an RSS feed from Process Login History data'),
'href' => 'https://modules.processwire.com/modules/process-login-history/',
'author' => 'Teppo Koivula',
'version' => '1.2.0',
'singular' => true,
'autoload' => true,
'icon' => 'rss',
'requires' => 'ProcessLoginHistory',
];
}
/**
* Base filename for the RSS feed URL
*
* @var string
*/
const RSS_FILENAME = 'process-login-history-rss.xml';
/**
* Module configuration
*
* @param array $data
* @return InputfieldWrapper
*/
public function getModuleConfigInputfields(array $data) {
// container for fields
$fields = $this->wire(new InputfieldWrapper());
// key
$field = $this->modules->get("InputfieldText");
$field->name = "key";
$field->label = __("Key");
$field->description = __("Key required to view the public login history RSS feed; if omitted, the feed won't be available. Please note that the key has to be at least 10 characters long.");
$field->notes = __("RSS feed is currently unavailable. Fill in a key and save module settings to see the feed URL.");
if (!empty($data[$field->name])) {
$field->notes = sprintf(__("URL of the RSS feed: %s"), $this->getRSSFeedURL($data[$field->name]));
$field->value = $data[$field->name];
}
$field->attr('minlength', 10);
$fields->add($field);
// show feed link on the Login History page?
$field = $this->modules->get("InputfieldCheckbox");
$field->name = "show_feed_link";
$field->label = __("Show feed link on the Login History page");
$field->description = __("Check this option if you want to provide a link to the publicly viewable RSS feed on the Login History page. This option is disabled by default as you may want to limit the number of users with access to the feed.");
$field->checked = !empty($data[$field->name]);
$fields->add($field);
return $fields;
}
/**
* Initialization function, where we set up required hooks
*/
public function init() {
// make sure that a key is configured and that a valid key was provided for current request
if (!is_string($this->key) || !strlen($this->key) || !is_string($this->input->get->key) || $this->input->get->key !== $this->key) {
return;
}
// if this request is for the login history RSS feed, add hook that renders the feed
$root = $this->config->urls->root ?: '/';
$feed = $root . self::RSS_FILENAME;
if (isset($_SERVER['REQUEST_URI']) && mb_substr(strtok($_SERVER['REQUEST_URI'], "?"), -strlen($feed)) === $feed) {
$this->addHookBefore("ProcessPageView::pageNotFound", $this, "renderRSS");
}
}
/**
* Get RSS feed URL
*
* @param string|null $key
* @return string
*/
public function getRSSFeedURL(?string $key = null): string {
if ($key === null && (!is_string($this->key) || !strlen($this->key))) {
return '';
}
return "http" . ($this->config->https ? "s" : "") . "://" . $this->config->httpHost . $this->config->urls->root . self::RSS_FILENAME . "?key=" . rawurlencode($key ?? $this->key);
}
/**
* Render RSS feed
*/
protected function renderRSS() {
/** @var ProcessLoginHistory */
$loginHistory = $this->modules->getModule('ProcessLoginHistory', [
'noPermissionCheck' => true,
]);
$loginHistory->executeRSS();
}
}