forked from silecs/mantis-plugin-ticketlist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventHooks.php
77 lines (72 loc) · 2.46 KB
/
EventHooks.php
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
<?php
trait EventHooks
{
public function onBeforeOutput(): void
{
global $g_bypass_headers;
if ($this->isPluginRequested("index")) {
$this->addHttpHeaders();
}
if ($this->isPluginRequested("api")) {
// Hidden global setting, found by reverse engineering this junk of Mantis.
$g_bypass_headers = true;
}
}
/**
* Add entries to the menu on the page "Summary".
*/
public function onMenu(): array
{
return [
[
'title' => "Lister des tickets",
'url' => plugin_page('index'),
'access_level' => ANYBODY,
'icon' => 'fa-list'
],
];
}
public function addHtmlHeadContent(): string
{
if (!$this->isPluginRequested("index")) {
return '';
}
$cssPath = htmlspecialchars(plugin_file('ticketlist.css'));
$jsPath = htmlspecialchars(plugin_file('main.js')); // json_encode(plugin_page('list'));
$projectId = (int) helper_get_current_project();
if ($projectId > 0) {
$record = project_get_row($projectId);
$project = ['id' => (int) $record['id'], 'name' => $record['name']];
} else {
$project = ['id' => 0, 'name' => "Tous les projets"];
}
$project['accessLevel'] = (int) access_get_project_level($project['id'], auth_get_current_user_id());
$data = htmlspecialchars(json_encode($project), ENT_NOQUOTES);
return <<<EOHTML
<link rel="stylesheet" type="text/css" href="{$cssPath}" />
<script id="ticket-list-data" type="application/json">{$data}</script>
<script src="{$jsPath}"></script>
EOHTML;
}
/**
* Add Content Security Policy headers for our script.
*/
protected function addHttpHeaders(): void
{
$hash = hash_file('sha256', __DIR__ . '/files/main.js');
http_csp_add('script-src', "'sha256-{$hash}'");
}
protected function isPluginRequested(string $page = ''): bool
{
if (strpos($_SERVER['REQUEST_URI'], "plugin.php") === false) {
return false;
}
$pageRequested = $_GET['page'] ?? '';
if ($page) {
return ($pageRequested === "TicketList/$page")
|| (strpos($pageRequested, "TicketList/$page/") === 0);
} else {
return (strncmp($page, 'TicketList', 10) === 0);
}
}
}