<?php
/**
 * @copyright (c) Kufer Software GmbH, Oderstraße 7, D-84453 Mühldorf
 */


class CodingOperations
{
  
  public static function getInternalEncoding(): string
  {
    
    static $cache = null;
    if (is_null($cache)) {
      if (\System::isUnicode()) {
        $cache = UTF_8;
      } else {
        $cache = CP1252;
      }
    }

    return $cache;
  }

  
  public static function getExternalEncoding(): string
  {
    if (\ConstantOperations::getEncoding() == 'UTF_8' || \RequestOperations::isAjax()) {
      return UTF_8;
    }

    return CP1252;
  }

  
  public static function getKuferWEBSightsEncoding(): string
  {
    return UTF_8;
  }

  
  public static function getSettingsEncoding(): string
  {
    return UTF_8;
  }

  
  public static function getAllgsettingsEncoding(): string
  {
    return UTF_8;
  }

  
  public static function getKuferToolsSightsEncoding(): string
  {
    return UTF_8;
  }

  
  public static function getKuferToolsWssEncoding(): string
  {
    return UTF_8;
  }

  
  public static function getEncoding4OwnKuferToolsJsFiles(): string
  {
    return UTF_8;
  }

  
  public static function getEncoding4ExternalKuferToolsJsFiles(): string
  {
    return UTF_8;
  }

  
  public static function areInternalAndExternalEncodingEqual(): bool
  {
    return self::getInternalEncoding() == self::getExternalEncoding();
  }

  
  public static function setEncodingResponseHeader(): void
  {
    header('Content-Type: text/html; charset=' . self::getExternalEncoding());
  }

  
  public static function setJsonEncodingResponseHeader(): void
  {
    header('Content-Type: application/json; charset=UTF-8');
  }

  
  public static function setObStartIfInternalAndExternalEncodingAreDiffrent(): void
  {
    if (!self::areInternalAndExternalEncodingEqual()) {
      ob_start();
    }
  }

  
  public static function encodeEingaben(): void
  {
    if (!self::areInternalAndExternalEncodingEqual() && GetOperations::getVarValue('wb__already__decoded', '##') !== 'done') {
      $_GET = self::encodeRecursive($_GET);
      $_POST = self::encodeRecursive($_POST);
      $_REQUEST = self::encodeRecursive($_REQUEST);
      $_FILES = self::encodeFileNames($_FILES);
      $_GET['wb__already__decoded'] = 'done';
    }

    if (!empty($_FILES)) {
      $_FILES = self::convertFileNameSpecialChars($_FILES);
    }
  }

  
  public static function encodeRecursive(array $array): array
  {
    $encodeRequest = new EncodeCharset4RequestAndResponse();
    $result = array();
    foreach ($array as $key => $value) {
      if (is_array($value)) {
        $encodedValue = self::encodeRecursive($value);
      } else {
        $encodedValue = $encodeRequest->encode($value);
      }
      
      $encodedKey = $encodeRequest->encode($key);
      $result[$encodedKey] = $encodedValue;
    }

    return $result;
  }

  
  public static function encodeFileNames(array $files): array
  {
    $encodeRequest = new EncodeCharset4RequestAndResponse();

    return array_map(function($file) use ($encodeRequest) {
      if (isset($file['name'])) {
        $file['name'] = $encodeRequest->encode($file['name']);
      }

      return $file;
    }, $files);
  }

  
  public static function convertFileNameSpecialChars(array $files): array
  {
    return array_map(function($file) {
      if (isset($file['name'])) {
        $file['name'] = \CodingOperations::convertSingleFileNameSpecialChars($file['name']);
      }

      return $file;
    }, $files);
  }

  public static function convertSingleFileNameSpecialChars(string $file): string
  {
    
    $filename = str_replace(array('ä',  'Ä',  'ü',  'Ü',  'ö',  'Ö',  'ß'), array('ae', 'Ae', 'ue', 'Ue', 'oe', 'Oe', 'ss'), $file);

    
    return preg_replace('/[^0-9a-zA-Z._-]/', '_', $filename);
  }

  
  public static function encodeAnzeige(bool $pDoReturn = false, string $pStringToConvert = '')
  {
    if ($pStringToConvert != '') {
      $content = $pStringToConvert;
    } else {
      
      
      if (self::areInternalAndExternalEncodingEqual()) {
        return;
      }
      $content = ob_get_contents();
      ob_end_clean();
    }

    $encodeRequest = new EncodeCharset4RequestAndResponse();
    $content = $encodeRequest->decode($content);

    if ($pDoReturn) {
      return $content;
    } else {
      echo $content;
    }
  }
}