Subversion Repositories oidplus

Rev

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

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