Subversion Repositories oidplus

Rev

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