<?php
/**
 * @copyright (c) Kufer Software GmbH, Loisachstrasse 4, D-84453 Mühldorf
 */


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

    return $cache;
  }

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

    return CP1252;
  }

  
  public static function getKuferWEBSightsEncoding()
  {
    return UTF_8;
  }

  
  public static function getSettingsEncoding()
  {
    return UTF_8;
  }

  
  public static function getAllgsettingsEncoding()
  {
    return UTF_8;
  }

  
  public static function getKuferToolsSightsEncoding()
  {
    return UTF_8;
  }

  
  public static function getKuferToolsWssEncoding()
  {
    return UTF_8;
  }

  
  public static function getEncoding4OwnKuferToolsJsFiles()
  {
    return UTF_8;
  }

  
  public static function getEncoding4ExternalKuferToolsJsFiles()
  {
    return UTF_8;
  }

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

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

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

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

  
  public static function encodeEingaben()
  {
    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';
    }

    $_FILES = self::convertFileNameSpecialChars($_FILES);
  }

  
  public static function encodeRecursive($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($files)
  {
    $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($files)
  {
    return array_map(function($file) {
      if (isset($file['name'])) {
        $file['name'] = \CodingOperations::convertSingleFileNameSpecialChars($file['name']);
      }

      return $file;
    }, $files);
  }

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

  
  public static function encodeAnzeige($pDoReturn = false, $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;
    }
  }
}