Subversion Repositories oidplus

Rev

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