Subversion Repositories oidplus

Rev

Rev 481 | Go to most recent revision | Blame | 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. define('SPACER_PNG', base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAACnej3aAAAAAXRSTlMAQObYZgAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII='));
  21.  
  22. // TODO: should we also check security.ini ?
  23.  
  24. require_once __DIR__ . '/../../../includes/functions.inc.php';
  25.  
  26. error_reporting(0);
  27.  
  28. if (!isset($_REQUEST['file'])) {
  29.         httpOutWithETag(SPACER_PNG, 'image/png', 'spacer.png');
  30. } else {
  31.         $file = $_REQUEST['file'];
  32. }
  33.  
  34. if (!isset($_REQUEST['mode'])) {
  35.         httpOutWithETag(SPACER_PNG, 'image/png', 'spacer.png');
  36. } else {
  37.         $mode = $_REQUEST['mode'];
  38. }
  39.  
  40. if (!isset($_REQUEST['lang'])) {
  41.         $lang = 'enus';
  42. } else {
  43.         $lang = $_REQUEST['lang'];
  44. }
  45.  
  46. $candidate1 = __DIR__ . '/../../../userdata/resources/' . $file;
  47. $candidate2 = __DIR__ . '/../../../res/' . $file;
  48.  
  49. if (file_exists($candidate1) || is_dir($candidate1)) {
  50.         // It is a file inside userdata/ (or it is overwritten by userdata)
  51.         $file = $candidate1;
  52. } else {
  53.         // It is a file in res/
  54.         $file = $candidate2;
  55. }
  56.  
  57. if (($mode == 'treeicon_folder') || ($mode == 'treeicon_leaf_url') || ($mode == 'treeicon_leaf_doc')) {
  58.  
  59.         if (file_exists($icon_candidate = getIconCandidate($file, 'png', 'tree', $lang))) {
  60.                 httpOutWithETag(file_get_contents($icon_candidate), 'image/png', basename($icon_candidate));
  61.         } else if (file_exists($icon_candidate = getIconCandidate($file, 'png', 'tree', ''))) {
  62.                 httpOutWithETag(file_get_contents($icon_candidate), 'image/png', basename($icon_candidate));
  63.         } else if (file_exists($icon_candidate = __DIR__.'/'.$mode.'.png')) { // default icon for mode
  64.                 httpOutWithETag(file_get_contents($icon_candidate), 'image/png', basename($icon_candidate));
  65.         } else {
  66.                 httpOutWithETag(SPACER_PNG, 'image/png'); // should not happen
  67.         }
  68.  
  69. } else if (($mode == 'icon_leaf_url_big') || ($mode == 'icon_leaf_doc_big') || ($mode == 'icon_folder_big')) {
  70.  
  71.         if (file_exists($icon_candidate = getIconCandidate($file, 'png', 'big', $lang))) {
  72.                 httpOutWithETag(file_get_contents($icon_candidate), 'image/png', basename($icon_candidate));
  73.         } else if (file_exists($icon_candidate = getIconCandidate($file, 'png', 'big', ''))) {
  74.                 httpOutWithETag(file_get_contents($icon_candidate), 'image/png', basename($icon_candidate));
  75.         } else if (file_exists($icon_candidate = __DIR__.'/'.$mode.'.png')) { // default icon for mode
  76.                 httpOutWithETag(file_get_contents($icon_candidate), 'image/png', basename($icon_candidate));
  77.         } else {
  78.                 httpOutWithETag(SPACER_PNG, 'image/png', 'spacer.png'); // should not happen
  79.         }
  80.  
  81. } else {
  82.  
  83.         // Invalid $mode value
  84.         httpOutWithETag(SPACER_PNG, 'image/png', 'spacer.png'); // should not happen
  85.  
  86. }
  87.  
  88. # ---
  89.  
  90. function getIconCandidate($file, $picFormat, $treeOrBig, $lang) {
  91.         $cnt = 0;
  92.         if (!empty($lang)) {
  93.                 $appendix = '_'.$treeOrBig.'$'.$lang.'.'.$picFormat;
  94.         } else {
  95.                 $appendix = '_'.$treeOrBig.'.'.$picFormat;
  96.         }
  97.         $tmp = preg_replace('/\.([^.]+)$/', $appendix, basename($file), -1, $cnt);
  98.         if ($cnt == 0) $tmp .= $appendix;
  99.         return dirname($file).'/'.$tmp;
  100. }
  101.