-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathstyles.php
More file actions
167 lines (147 loc) · 7.19 KB
/
Copy pathstyles.php
File metadata and controls
167 lines (147 loc) · 7.19 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Display the general CSS style to tabs.
*
* @package format_onetopic
* @copyright 2023 David Herney Bernal - cirano
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define('NO_MOODLE_COOKIES', true);
// Require_login is not needed here.
// phpcs:disable moodle.Files.RequireLogin.Missing
require_once('../../../config.php');
$revision = optional_param('revision', 0, PARAM_ALPHANUM);
$withunits = ['font-size', 'line-height', 'margin', 'padding', 'border-width', 'border-radius'];
$csscontent = '';
$csstabstyles = '';
$tabstyles = get_config('format_onetopic', 'tabstyles');
if (!empty($tabstyles)) {
$tabstyles = @json_decode($tabstyles);
if (is_object($tabstyles)) {
$precedence = ['default', 'childs', 'childindex', 'active', 'parent', 'highlighted', 'disabled', 'hover'];
$orderedtabs = new \stdClass();
foreach ($precedence as $type) {
if (property_exists($tabstyles, $type)) {
$orderedtabs->$type = $tabstyles->$type;
}
}
foreach ($orderedtabs as $type => $styles) {
$important = false;
switch ($type) {
case 'active':
$csscontent .= '#tabs-tree-start .verticaltabs .format_onetopic-tabs .nav-item a.nav-link.active, ';
$csscontent .= '#tabs-tree-start .nav-tabs a.nav-link.active, ';
$csscontent .= '#tabs-tree-start .onetopic-tab-body .nav-tabs a.nav-link.active, ';
$csscontent .= '#tabs-tree-start .onetopic-tab-body .nav-tabs .nav-item.subtopic a.nav-link.active';
$important = true;
break;
case 'parent':
$csscontent .= '#tabs-tree-start .verticaltabs .format_onetopic-tabs .nav-item.haschilds a.nav-link, ';
$csscontent .= '#tabs-tree-start .nav-tabs .nav-item.haschilds a.nav-link';
break;
case 'highlighted':
$csscontent .= '#tabs-tree-start .verticaltabs .format_onetopic-tabs .nav-item.marker a.nav-link, ';
$csscontent .= '#tabs-tree-start .nav-tabs .nav-item.marker a.nav-link, ';
$csscontent .= '#tabs-tree-start .onetopic-tab-body .nav-tabs .nav-item.marker a.nav-link';
$csscontent .= '#tabs-tree-start .onetopic-tab-body .nav-tabs .nav-item.subtopic.marker a.nav-link';
$important = true;
break;
case 'disabled':
$csscontent .= '#tabs-tree-start .verticaltabs .format_onetopic-tabs .nav-item.disabled a.nav-link, ';
$csscontent .= '#tabs-tree-start .nav-tabs .nav-item.disabled a.nav-link, ';
$csscontent .= '#tabs-tree-start .onetopic-tab-body .nav-tabs .nav-item.disabled a.nav-link';
$csscontent .= '#tabs-tree-start .onetopic-tab-body .nav-tabs .nav-item.subtopic.disabled a.nav-link';
$important = true;
break;
case 'hover':
$csscontent .= '#tabs-tree-start .verticaltabs .format_onetopic-tabs .nav-item a.nav-link:hover, ';
$csscontent .= '#tabs-tree-start .format_onetopic-tabs.nav-tabs .nav-item a.nav-link:hover, ';
$csscontent .= '#tabs-tree-start .onetopic-tab-body .format_onetopic-tabs.nav-tabs' .
' .nav-item a.nav-link:hover';
break;
case 'childs':
$csscontent .= '#tabs-tree-start .onetopic-tab-body .nav-tabs .nav-item.subtopic a.nav-link';
break;
case 'childindex':
$csscontent .= '#tabs-tree-start .onetopic-tab-body .nav-tabs' .
' .nav-item.subtopic.tab_initial a.nav-link';
break;
default:
$csscontent .= '#tabs-tree-start .verticaltabs .format_onetopic-tabs .nav-item a.nav-link, ';
$csscontent .= '#tabs-tree-start .nav-tabs a.nav-link';
}
$csscontent .= '{';
$units = [];
// Check if exist units for some rules.
foreach ($styles as $key => $value) {
// Check if the key start with the units prefix.
if (strpos($key, 'unit-') === 0) {
// Remove the prefix.
$ownerkey = str_replace('unit-', '', $key);
$units[$ownerkey] = $value;
unset($styles->$key);
}
}
foreach ($styles as $key => $value) {
// If exist a unit for the rule, apply it.
if (isset($units[$key])) {
$value = $value . $units[$key];
} else if (in_array($key, $withunits)) {
// If the rule need units, apply px by default.
$value = $value . 'px';
}
if ($key == 'others') {
$csscontent .= $value . ';';
} else {
$csscontent .= $key . ':' . $value . ($important ? '!important' : '') . ';';
}
}
$csscontent .= '} ';
}
// Clean the CSS for html tags.
$csstabstyles = preg_replace('/<[^>]*>/', '', $csscontent);
}
}
$csstabstyles = trim($csstabstyles);
$etag = md5($csstabstyles . $revision);
// ETag validation: Return 304 if client has the current version. This will
// preserve the existing cache for $cachelifetime.
if (isset($_SERVER['HTTP_IF_NONE_MATCH'])) {
$clientetag = trim($_SERVER['HTTP_IF_NONE_MATCH'], '"');
if ($clientetag === $etag) {
header('HTTP/1.1 304 Not Modified');
header('ETag: "' . $etag . '"');
exit;
}
}
// Return empty response for edge cases (no styles configured & direct URL access).
if (empty($csstabstyles)) {
header('HTTP/1.1 304 Not Modified');
header('Content-Length: 0');
exit;
}
// Cache for 1 year, this is safe due to cache busting via revision param.
$cachelifetime = 31536000;
header('Cache-Control: public, max-age=' . $cachelifetime . ', immutable');
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $cachelifetime) . ' GMT');
header('ETag: "' . $etag . '"');
// Content headers.
header('Content-Length: ' . strlen($csstabstyles));
header('Content-Disposition: inline; filename="styles.php"');
header('Content-Type: text/css; charset=utf-8');
header('X-Content-Type-Options: nosniff');
echo $csstabstyles;