Subversion Repositories oidplus

Rev

Rev 1055 | Rev 1116 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
635 daniel-mar 1
<?php
2
 
3
/*
4
 * OIDplus 2.0
1086 daniel-mar 5
 * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft
635 daniel-mar 6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with the License.
9
 * You may obtain a copy of the License at
10
 *
11
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
18
 */
19
 
1050 daniel-mar 20
namespace ViaThinkSoft\OIDplus;
635 daniel-mar 21
 
1086 daniel-mar 22
// phpcs:disable PSR1.Files.SideEffects
23
\defined('INSIDE_OIDPLUS') or die;
24
// phpcs:enable PSR1.Files.SideEffects
25
 
635 daniel-mar 26
class OIDplusPageAdminColors extends OIDplusPagePluginAdmin {
27
 
819 daniel-mar 28
        public function htmlHeaderUpdate(&$head_elems) {
29
                foreach ($head_elems as &$line) {
30
                        if (strpos($line,'oidplus.min.css.php') !== false) {
31
                                $add_css_args = array();
32
                                $add_css_args[] = 'theme='.urlencode(OIDplus::config()->getValue('design','default'));
33
                                $add_css_args[] = 'invert='.urlencode(OIDplus::config()->getValue('color_invert',0));
34
                                $add_css_args[] = 'h_shift='.urlencode(number_format(OIDplus::config()->getValue('color_hue_shift',0)/360,5,'.',''));
35
                                $add_css_args[] = 's_shift='.urlencode(number_format(OIDplus::config()->getValue('color_sat_shift',0)/100,5,'.',''));
36
                                $add_css_args[] = 'v_shift='.urlencode(number_format(OIDplus::config()->getValue('color_val_shift',0)/100,5,'.',''));
820 daniel-mar 37
                                if (count($add_css_args) > 0) {
38
                                        $line = str_replace('oidplus.min.css.php?', 'oidplus.min.css.php&', $line);
39
                                        $line = str_replace('oidplus.min.css.php', 'oidplus.min.css.php?'.htmlentities(implode('&',$add_css_args)), $line);
40
                                }
819 daniel-mar 41
                        }
42
 
820 daniel-mar 43
                        if ((stripos($line,'<meta') !== false) && (stripos($line,'name="theme-color"') !== false)) {
819 daniel-mar 44
                                if (preg_match('@content="(.+)"@ismU', $line, $m)) {
45
                                        $theme_color = $m[1];
46
                                        $hs = OIDplus::config()->getValue('color_hue_shift',0)/360;
47
                                        $ss = OIDplus::config()->getValue('color_sat_shift',0)/100;
48
                                        $vs = OIDplus::config()->getValue('color_val_shift',0)/100;
49
                                        $theme_color = changeHueOfCSS($theme_color, $hs, $ss, $vs); // "changeHueOfCSS" can also change a single color value if it has the form #xxyyzz or #xyz
50
                                        if (OIDplus::config()->getValue('color_invert',0)) {
51
                                                $theme_color = invertColorsOfCSS($theme_color);
52
                                        }
53
                                        $line = preg_replace('@content="(.+)"@ismU', 'content="'.$theme_color.'"', $line);
54
 
55
                                }
56
                        }
57
                }
58
        }
59
 
635 daniel-mar 60
        public function action($actionID, $params) {
61
                if ($actionID == 'color_update') {
62
                        if (!OIDplus::authUtils()->isAdminLoggedIn()) {
63
                                throw new OIDplusException(_L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login$admin')));
64
                        }
65
 
717 daniel-mar 66
                        _CheckParamExists($params, 'hue_shift');
67
                        _CheckParamExists($params, 'sat_shift');
68
                        _CheckParamExists($params, 'val_shift');
69
                        _CheckParamExists($params, 'invcolors');
70
                        _CheckParamExists($params, 'theme');
635 daniel-mar 71
 
72
                        OIDplus::config()->setValue('color_hue_shift', $params['hue_shift']);
73
                        OIDplus::config()->setValue('color_sat_shift', $params['sat_shift']);
74
                        OIDplus::config()->setValue('color_val_shift', $params['val_shift']);
75
                        OIDplus::config()->setValue('color_invert',    $params['invcolors']);
76
                        OIDplus::config()->setValue('design',          $params['theme']);
77
 
78
                        OIDplus::logger()->log("[OK]A?", "Changed system color theme");
79
 
80
                        return array("status" => 0);
81
                } else {
82
                        throw new OIDplusException(_L('Unknown action ID'));
83
                }
84
        }
85
 
86
        public function init($html=true) {
87
                OIDplus::config()->prepareConfigKey('color_hue_shift', 'HSV Hue shift of CSS colors (-360..360)', '0', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
88
                        if (!is_numeric($value) || ($value < -360) || ($value > 360)) {
89
                                throw new OIDplusException(_L('Please enter a valid value.'));
90
                        }
91
                });
92
                OIDplus::config()->prepareConfigKey('color_sat_shift', 'HSV Saturation shift of CSS colors (-100..100)', '0', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
93
                        if (!is_numeric($value) || ($value < -100) || ($value > 100)) {
94
                                throw new OIDplusException(_L('Please enter a valid value.'));
95
                        }
96
                });
97
                OIDplus::config()->prepareConfigKey('color_val_shift', 'HSV Value shift of CSS colors (-100..100)', '0', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
98
                        if (!is_numeric($value) || ($value < -100) || ($value > 100)) {
99
                                throw new OIDplusException(_L('Please enter a valid value.'));
100
                        }
101
                });
102
                OIDplus::config()->prepareConfigKey('color_invert', 'Invert colors? (0=no, 1=yes)', '0', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
103
                        if (!is_numeric($value) || ($value < 0) || ($value > 1)) {
104
                                throw new OIDplusException(_L('Please enter a valid value (0=no, 1=yes).'));
105
                        }
106
                });
107
                OIDplus::config()->prepareConfigKey('design', 'Which design to use (must exist in plugins/[vendorname]/design/)?', 'default', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
108
                        $good = true;
109
                        if (strpos($value,'/') !== false) $good = false;
110
                        if (strpos($value,'\\') !== false) $good = false;
111
                        if (strpos($value,'..') !== false) $good = false;
112
                        if (!$good) {
113
                                throw new OIDplusException(_L('Invalid design folder name. Do only enter a folder name, not an absolute or relative path'));
114
                        }
115
 
926 daniel-mar 116
                        if (!wildcard_is_dir(OIDplus::localpath().'plugins/'.'*'.'/design/'.$value)) {
635 daniel-mar 117
                                throw new OIDplusException(_L('The design "%1" does not exist in plugin directory %2',$value,'plugins/[vendorname]/design/'));
118
                        }
119
                });
120
                OIDplus::config()->prepareConfigKey('oobe_colors_done', '"Out Of Box Experience" wizard for OIDplusPageAdminColors done once?', '0', OIDplusConfig::PROTECTION_HIDDEN, function($value) {});
121
        }
122
 
123
        public function gui($id, &$out, &$handled) {
124
                if ($id === 'oidplus:colors') {
125
                        $handled = true;
126
                        $out['title'] = _L('Design');
801 daniel-mar 127
                        $out['icon']  = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png';
635 daniel-mar 128
 
129
                        if (!OIDplus::authUtils()->isAdminLoggedIn()) {
800 daniel-mar 130
                                $out['icon'] = 'img/error.png';
635 daniel-mar 131
                                $out['text'] = '<p>'._L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login$admin')).'</p>';
132
                                return;
133
                        }
134
 
135
                        $out['text']  = '<br><p>';
136
                        $out['text'] .= '  <label for="theme">'._L('Design').':</label>';
137
                        $out['text'] .= '  <select name="theme" id="theme">';
138
                        foreach (OIDplus::getDesignPlugins() as $plugin) {
139
                                $folder = basename($plugin->getPluginDirectory());
140
                                $selected = $folder == OIDplus::config()->getValue('design') ? ' selected="true"' : '';
141
                                $out['text'] .= '<option value="'.htmlentities($folder).'"'.$selected.'>'.htmlentities($plugin->getManifest()->getName()).'</option>';
142
                        }
143
                        $out['text'] .= '  </select>';
144
                        $out['text'] .= '</p>';
145
 
146
                        $out['text'] .= '<br><p>';
147
                        $out['text'] .= '  <label for="amount">'._L('Hue shift').':</label>';
148
                        $out['text'] .= '  <input type="text" id="hshift" readonly style="border:0; background:transparent; font-weight:bold;">';
149
                        $out['text'] .= '</p>';
150
                        $out['text'] .= '<div id="slider-hshift"></div>';
151
 
152
                        $out['text'] .= '<br><p>';
153
                        $out['text'] .= '  <label for="amount">'._L('Saturation shift').':</label>';
154
                        $out['text'] .= '  <input type="text" id="sshift" readonly style="border:0; background:transparent; font-weight:bold;">';
155
                        $out['text'] .= '</p>';
156
                        $out['text'] .= '<div id="slider-sshift"></div>';
157
 
158
                        $out['text'] .= '<br><p>';
159
                        $out['text'] .= '  <label for="amount">'._L('Value shift').':</label>';
160
                        $out['text'] .= '  <input type="text" id="vshift" readonly style="border:0; background:transparent; font-weight:bold;">';
161
                        $out['text'] .= '</p>';
162
                        $out['text'] .= '<div id="slider-vshift"></div>';
163
 
987 daniel-mar 164
                        $out['text'] .= '<p><div><input type="checkbox" id="icolor"> <label for="icolor">'._L('Invert colors').'</label></div></p>';
635 daniel-mar 165
 
166
                        $out['text'] .= '<script>';
167
                        $out['text'] .= 'if (OIDplusPageAdminColors.hue_shift == null) OIDplusPageAdminColors.hue_shift = OIDplusPageAdminColors.hue_shift_saved = '.OIDplus::config()->getValue('color_hue_shift').";\n";
168
                        $out['text'] .= 'if (OIDplusPageAdminColors.sat_shift == null) OIDplusPageAdminColors.sat_shift = OIDplusPageAdminColors.sat_shift_saved = '.OIDplus::config()->getValue('color_sat_shift').";\n";
169
                        $out['text'] .= 'if (OIDplusPageAdminColors.val_shift == null) OIDplusPageAdminColors.val_shift = OIDplusPageAdminColors.val_shift_saved = '.OIDplus::config()->getValue('color_val_shift').";\n";
170
                        $out['text'] .= 'if (OIDplusPageAdminColors.invcolors == null) OIDplusPageAdminColors.invcolors = OIDplusPageAdminColors.invcolors_saved = '.OIDplus::config()->getValue('color_invert').";\n";
171
                        $out['text'] .= 'if (OIDplusPageAdminColors.activetheme == null) OIDplusPageAdminColors.activetheme_saved = '.js_escape(OIDplus::config()->getValue('design')).";\n";
172
                        $out['text'] .= 'OIDplusPageAdminColors.setup_color_sliders();';
173
                        $out['text'] .= '</script>';
174
 
175
                        $out['text'] .= '<br>';
176
                        $out['text'] .= '<input type="button" onclick="OIDplusPageAdminColors.color_reset_sliders_cfg()" value="'._L('Reset to last saved config').'">'.str_repeat('&nbsp;',5);
177
                        $out['text'] .= '<input type="button" onclick="OIDplusPageAdminColors.color_reset_sliders_factory()" value="'._L('Reset default setting').'">'.str_repeat('&nbsp;',5);
178
                        $out['text'] .= '<input type="button" onclick="OIDplusPageAdminColors.test_color_theme()" value="'._L('Test').'">'.str_repeat('&nbsp;',5);
179
                        $out['text'] .= '<input type="button" onclick="OIDplusPageAdminColors.crudActionColorUpdate()" value="'._L('Set permanently').'">';
180
                }
181
        }
182
 
183
        public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
184
                if (!OIDplus::authUtils()->isAdminLoggedIn()) return false;
185
 
800 daniel-mar 186
                if (file_exists(__DIR__.'/img/main_icon16.png')) {
801 daniel-mar 187
                        $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png';
635 daniel-mar 188
                } else {
189
                        $tree_icon = null; // default icon (folder)
190
                }
191
 
192
                $json[] = array(
193
                        'id' => 'oidplus:colors',
194
                        'icon' => $tree_icon,
195
                        'text' => _L('Design')
196
                );
197
 
198
                return true;
199
        }
200
 
201
        public function tree_search($request) {
202
                return false;
203
        }
204
 
205
        public function implementsFeature($id) {
206
                if (strtolower($id) == '1.3.6.1.4.1.37476.2.5.2.3.1') return true; // oobeEntry, oobeRequested
207
                return false;
208
        }
209
 
210
        public function oobeRequested(): bool {
211
                // Interface 1.3.6.1.4.1.37476.2.5.2.3.1
212
 
213
                return OIDplus::config()->getValue('oobe_colors_done') == '0';
214
        }
215
 
216
        public function oobeEntry($step, $do_edits, &$errors_happened)/*: void*/ {
217
                // Interface 1.3.6.1.4.1.37476.2.5.2.3.1
218
 
1055 daniel-mar 219
                echo '<h2>'._L('Step %1: Color Theme',$step).'</h2>';
635 daniel-mar 220
 
221
                echo '<input type="checkbox" name="color_invert" id="color_invert"';
1033 daniel-mar 222
                if (isset($_POST['sent'])) {
223
                        if ($set_value = isset($_POST['color_invert'])) {
635 daniel-mar 224
                                echo ' checked';
225
                        }
226
                } else {
227
                        if ($set_value = (OIDplus::config()->getValue('color_invert') == 1)) {
228
                                echo ' checked';
229
                        }
230
                }
231
                echo '> <label for="color_invert">'._L('Dark Theme (inverted colors)').'</label><br>';
232
 
233
                $msg = '';
234
                if ($do_edits) {
235
                        try {
236
                                OIDplus::config()->setValue('color_invert', $set_value ? 1 : 0);
237
                                OIDplus::config()->setValue('oobe_colors_done', '1');
1050 daniel-mar 238
                        } catch (\Exception $e) {
635 daniel-mar 239
                                $msg = $e->getMessage();
240
                                $errors_happened = true;
241
                        }
242
                }
243
 
244
                echo ' <font color="red"><b>'.$msg.'</b></font>';
245
        }
246
 
247
}