Subversion Repositories oidplus

Rev

Rev 490 | Rev 532 | 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 - 2021 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. use MatthiasMullie\Minify;
  21.  
  22. require_once __DIR__ . '/includes/oidplus.inc.php';
  23.  
  24. require_once __DIR__ . '/3p/minify/path-converter/ConverterInterface.php';
  25. require_once __DIR__ . '/3p/minify/path-converter/Converter.php';
  26. require_once __DIR__ . '/3p/minify/php-fig-cache/CacheItemInterface.php';
  27. require_once __DIR__ . '/3p/minify/src/Minify.php';
  28. require_once __DIR__ . '/3p/minify/src/CSS.php';
  29. require_once __DIR__ . '/3p/minify/src/Exception.php';
  30.  
  31. error_reporting(OIDplus::baseConfig()->getValue('DEBUG') ? E_ALL : 0);
  32. @ini_set('display_errors', OIDplus::baseConfig()->getValue('DEBUG') ? 1 : 0);
  33.  
  34. $out = '';
  35.  
  36. # ---
  37.  
  38. $do_minify = OIDplus::baseConfig()->getValue('MINIFY_CSS', true);
  39.  
  40. function process_file($filename) {
  41.         global $do_minify;
  42.  
  43.         $filename_min = preg_replace('/\.[^.]+$/', '.min.css', $filename);
  44.         $filename_full = $filename;
  45.  
  46.         if ($do_minify) {
  47.                 if (file_exists($filename_min)) {
  48.                         // There is a file which is already minified
  49.                         $filename = $filename_min;
  50.                         $cont = file_get_contents($filename);
  51.                 } else if (file_exists($filename_full)) {
  52.                         // Otherwise, we minify it ourself
  53.                         $filename = $filename_full;
  54.                         $minifier = new Minify\CSS($filename);
  55.                         $cont = $minifier->minify();
  56.                 } else {
  57.                         return;
  58.                 }
  59.         } else {
  60.                 if (file_exists($filename_full)) {
  61.                         $filename = $filename_full;
  62.                         $cont = file_get_contents($filename);
  63.                 } else if (file_exists($filename_min)) {
  64.                         $filename = $filename_min;
  65.                         $cont = file_get_contents($filename);
  66.                 } else {
  67.                         return;
  68.                 }
  69.         }
  70.  
  71.         $thisdir = __DIR__;
  72.         $thisdir = str_replace('\\', '/', $thisdir); // change Windows Backslashes into Web-Slashes
  73.         $filename = str_replace('\\', '/', $filename); // change Windows Backslashes into Web-Slashes
  74.         $dir = dirname((strpos($filename, $thisdir.'/') === 0) ? substr($filename, strlen($thisdir.'/')) : $filename);
  75.  
  76.         $cont = preg_replace('@url\\(\s+@ism', 'url(', $cont);
  77.         $cont = str_ireplace('url("data:', 'url###("data:', $cont);
  78.         $cont = str_ireplace('url("', 'url###("'.$dir.'/', $cont);
  79.         $cont = str_ireplace("url('data:", "url###('data:", $cont);
  80.         $cont = str_ireplace("url('", "url###('".$dir.'/', $cont);
  81.         $cont = str_ireplace("url(data:", "url###(data:", $cont);
  82.         $cont = str_ireplace("url(", "url###(".$dir.'/', $cont);
  83.         $cont = str_ireplace("url###(", "url(", $cont);
  84.         return $cont."\n\n";
  85. }
  86.  
  87. # ---
  88.  
  89. // Third-party products
  90. $out .= process_file(__DIR__ . '/3p/jstree/themes/default/style.css');
  91. $out .= process_file(__DIR__ . '/3p/jquery-ui/jquery-ui.css');
  92. $out .= process_file(__DIR__ . '/3p/bootstrap4/css/bootstrap.css');
  93.  
  94. // Find out base CSS
  95. if (isset($_REQUEST['theme'])) {
  96.         $theme = $_REQUEST['theme'];
  97.         if (strpos($theme,'/') !== false) $theme = 'default';
  98.         if (strpos($theme,'\\') !== false) $theme = 'default';
  99.         if (strpos($theme,'..') !== false) $theme = 'default';
  100. } else {
  101.         $theme = 'default';
  102. }
  103.  
  104. if (file_exists(__DIR__ . '/userdata/styles/oidplus_base.css')) {
  105.         // There is a user defined CSS (not recommended, use design plugins instead!)
  106.         $out .= process_file(__DIR__ . '/userdata/styles/oidplus_base.css');
  107. } else {
  108.         // Use CSS of the design plugin
  109.         OIDplus::registerAllPlugins('design', 'OIDplusDesignPlugin', array('OIDplus','registerDesignPlugin'));
  110.         $plugins = OIDplus::getDesignPlugins();
  111.         foreach ($plugins as $plugin) {
  112.                 if ((basename($plugin->getPluginDirectory())) == $theme) {
  113.                         $manifest = $plugin->getManifest();
  114.                         foreach ($manifest->getCSSFiles() as $css_file) {
  115.                                 $out .= process_file($css_file);
  116.                         }
  117.                 }
  118.         }
  119. }
  120.  
  121. // Then plugins
  122. $manifests = OIDplus::getAllPluginManifests('*Pages', true);
  123. foreach ($manifests as $manifest) {
  124.         foreach ($manifest->getCSSFiles() as $css_file) {
  125.                 $out .= process_file($css_file);
  126.         }
  127. }
  128.  
  129. // Now user-defined (additional) definitions
  130. if (file_exists(__DIR__ . '/userdata/styles/oidplus_add.css')) {
  131.         $out .= process_file(__DIR__ . '/userdata/styles/oidplus_add.css');
  132. }
  133.  
  134. # ---
  135.  
  136. $inv = isset($_REQUEST['invert']) ? $_REQUEST['invert'] : 0;
  137. if ($inv != 0) {
  138.         $out = invertColorsOfCSS($out);
  139. }
  140.  
  141. $hs = isset($_REQUEST['h_shift']) ? $_REQUEST['h_shift'] : 0;
  142. $ss = isset($_REQUEST['s_shift']) ? $_REQUEST['s_shift'] : 0;
  143. $vs = isset($_REQUEST['v_shift']) ? $_REQUEST['v_shift'] : 0;
  144. if (($hs != 0) ||($ss != 0) || ($vs != 0)) {
  145.         $out = changeHueOfCSS($out, $hs, $ss, $vs);
  146. }
  147.  
  148. # ---
  149.  
  150. if (OIDplus::baseConfig()->getValue('DEBUG')) {
  151.         // In debug mode, we might get PHP error messages (see "error_reporting" above),
  152.         // so it would be severe if we would allow ETAG! (since $out does not contain the PHP error messages!)
  153.         header('Content-Type:text/css');
  154.         echo $out;
  155. } else {
  156.         httpOutWithETag($out, 'text/css', 'oidplus.css');
  157. }
  158.