Subversion Repositories oidplus

Rev

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