<?php

namespace FOP;

class Sanitizers {
  
  

  
  

    // --- Statische Methoden ---
  
  public static function convert_month_years_to_unixtime($x) { 
    
    
    // --- BLOCK: main ---
    if (!$x) return null; $ts = date_parse_from_format(_('monthyearformat_php'), $x); return $ts['month'] - 1 + ($ts['year']) * 12;
    
  }

  public static function null_filter($x) { 
    
    
    // --- BLOCK: main ---
    return intval($x) ?: null;
    
  }

  public static function null_converter($x) { 
    
    
    // --- BLOCK: main ---
    return (string)($x ?: '0');
    
  }

  public static function innerHTML_parse($html) { 
    
    
    // --- BLOCK: main ---
    
		preg_match('/<body>([\s\S]*)(?:<\/body>)/m', preg_replace('/^<!DOCTYPE.+?>/m', '',$html), $matches, PREG_UNMATCHED_AS_NULL, 0);
		if(isset($matches[0])){
			$html = str_replace('<body>', '', str_replace('</body>', '',(string) $matches[0]));
		} else {
			$html = (string) preg_replace('/.*[^.*]*<body>/m', '', (string) preg_replace('/<\/body>[^.*]*/m', '', (string) preg_replace('/^<html.+?>/m', '',str_replace('<html>', '', (string) preg_replace('/^<!DOCTYPE.+?>/m', '', $html)))));
		}
		return (string) preg_replace('/\sbody\s|\sbody/m', ' ebody ', str_replace('"body"', '""', (str_replace( array('<html>', '</html>', '<body>', '</body>'), array('', '', '', ''),$html))));
	
    
  }

  public static function htmlentities_decode($x) { 
    
    
    // --- BLOCK: main ---
    
		// alles außer brackets in HTML umwandeln
		return preg_replace_callback('/&[^;]{1-9};/', function ($x) { if ($x == '&gt;' || $x == '&lt;') return $x; else return html_entity_decode($x, ENT_QUOTES | ENT_HTML5, 'UTF-8'); }, $x);
	
    
  }

  public static function iban_filter($x) { 
    
    
    // --- BLOCK: main ---
    $x = preg_replace('/\s+/', '', $x); return implode(' ', str_split($x, 4));
    
  }

  public static function password_filter($x) { 
    
    
    // --- BLOCK: main ---
    if (preg_match('/^&&(.*)$/', $x, $match)) return $match[1]; else return password_hash($x, PASSWORD_BCRYPT);
    
  }

  public static function no_phone($x) { 
    
    
    // --- BLOCK: main ---
    
		if ($x == _('+1')) return null;
		return $x ?: null;
	
    
  }

  public static function a_phone($x) { 
    
    
    // --- BLOCK: main ---
    
		return $x ?: '';
	
    
  }

  public static function canonicalize_phone($x) { 
    
    
    // --- BLOCK: main ---
    
		if (isset($x) && $x) {
			if (preg_match('/sip:([0-9]+)@.*/', $x, $matches)) {
				// sip-coded phone numbers
				$x = '+' . $matches[1];
			}
			$x = preg_replace('/[^0-9+]/', '', $x); // alles außer Ziffern und + weg
			$x = preg_replace('/^00/', '+', $x); // Ländervorwahl international
			$x = preg_replace('/^0/', _('+1'), $x); // Ländervorwahl national
			if ($x[0] != '+') {
				$x = _('+1') . $x;
			}
		}
		return $x;
	
    
  }

  public static function no_text($x) { 
    
    
    // --- BLOCK: main ---
    
		return $x ?: null;
	
    
  }

  public static function a_text($x) { 
    
    
    // --- BLOCK: main ---
    
		return $x ?: '';
	
    
  }

  public static function convert_date_to_unix_timestamp($x) { 
    
    
    // --- BLOCK: main ---
    
		if (!$x) return null;
		// x ist gültiger Integer mit plausiblem Timestamp-Wert:
		if ($x > time() / 2) return intval($x);
		$datetime = date_create_from_format('!' . _('dateformat_php'), $x);
		return is_object($datetime) ? $datetime->getTimestamp() : null;
	
    
  }

  public static function convert_datetime_to_unix_timestamp($x) { 
    
    
    // --- BLOCK: main ---
    
		if (!$x) return null;
		// x ist gültiger Integer mit plausiblem Timestamp-Wert:
		if ($x > time() / 2) return intval($x);
		$datetime = date_create_from_format(_('datetimeformat_php'), $x);
		return is_object($datetime) ? $datetime->getTimestamp() : null;
	
    
  }

  public static function convert_time_to_unix_timestamp($x) { 
    
    
    // --- BLOCK: main ---
    
		if (!$x) return null;
		// x ist gültiger Integer mit plausiblem Timestamp-Wert:
		if ($x > time() / 2) return intval($x);
		$datetime = date_create_from_format('!H:i', $x, new \DateTimeZone('UTC'));
		return is_object($datetime) ? $datetime->getTimestamp() : null;
	
    
  }

  public static function convert_timeLocal_to_unix_timestamp($x) { 
    
    
    // --- BLOCK: main ---
    
		if (!$x) return null;
		// x ist gültiger Integer mit plausiblem Timestamp-Wert:
		if ($x > time() / 2) return intval($x);
		$datetime = date_create_from_format(_('timeformat_php'), $x);
		return is_object($datetime) ? $datetime->getTimestamp() : null;
	
    
  }

  public static function convert_kw_to_unix_timestamp($x) { 
    
    
    // --- BLOCK: main ---
    
		if (!$x) return null;
		// x ist gültiger Integer mit plausiblem Timestamp-Wert:
		if ($x > time() / 2) return intval($x);
		if (preg_match('/([0-9]{4})\\s*\\/\\s*([0-9]{1,2})/', $x, $matches)) {
			$datetime = new \DateTime;
			$datetime->setISODate($matches[1], $matches[2]);
		}
		return is_object($datetime) ? $datetime->getTimestamp() : null;
	
    
  }

  public static function convert_dateMonth_to_unix_timestamp($x) { 
    
    
    // --- BLOCK: main ---
    
		if (!$x) return null;
		// x ist gültiger Integer mit plausiblem Timestamp-Wert:
		if ($x > time() / 2) return intval($x);
		$datetime = date_create_from_format('Y M', $x);
		return is_object($datetime) ? $datetime->getTimestamp() : null;
	
    
  }

  public static function convert_dateYear_to_unix_timestamp($x) { 
    
    
    // --- BLOCK: main ---
    
		if (!$x) return null;
		// x ist gültiger Integer mit plausiblem Timestamp-Wert:
		if ($x > time() / 2) return intval($x);
		$datetime = date_create_from_format('Y', $x);
		return is_object($datetime) ? $datetime->getTimestamp() : null;
	
    
  }

  public static function convert_dateWeekday_to_unix_timestamp($x) { 
    
    
    // --- BLOCK: main ---
    
		if (!$x) return null;
		// x ist gültiger Integer mit plausiblem Timestamp-Wert:
		if ($x > time() / 2) return intval($x);
		$datetime = date_create_from_format(_('dateformat_php'), $x);
		return is_object($datetime) ? $datetime->getTimestamp() : null;
	
    
  }

  public static function convert_datetimeWeekday_to_unix_timestamp($x) { 
    
    
    // --- BLOCK: main ---
    
		if (!$x) return null;
		// x ist gültiger Integer mit plausiblem Timestamp-Wert:
		if ($x > time() / 2) return intval($x);
		$datetime = date_create_from_format(_('datetimeformat_php'), $x);
		return is_object($datetime) ? $datetime->getTimestamp() : null;
	
    
  }

  public static function parseWebsite($x) { 
    
    
    // --- BLOCK: main ---
    
	// führenden Whitespace entfernen
	$x = preg_replace('/^(\s*)/', '', $x);
	if ($x === null || $x == '') return null;
	// http:// hinzufügen, falls noch kein URL-Schema existiert
	if (!parse_url($x, PHP_URL_SCHEME)) return 'http://'.$x;
	else return $x;
    
  }

  public static function canonicalize_number49($x) { 
    
    
    // --- BLOCK: main ---
    
		if ($x) {
			$x = self::canonicalize_phone($x);
			$x = str_replace(_('+1'), '+49', $x);
		}
		return $x;
	
    
  }

  public static function canonicalize_number43($x) { 
    
    
    // --- BLOCK: main ---
    
		if ($x) {
			$x = self::canonicalize_phone($x);
			$x = str_replace(_('+1'), '+43', $x);
		}
		return $x;
	
    
  }

  public static function canonicalize_number41($x) { 
    
    
    // --- BLOCK: main ---
    
		if ($x) {
			$x = self::canonicalize_phone($x);
			$x = str_replace(_('+1'), '+41', $x);
		}
		return $x;
	
    
  }

  public static function canonicalize_vname($x) { 
    
    
    // --- BLOCK: main ---
    
		if ($x) {
			if (preg_match('/(.+) [^ ]+/s', $x, $matches)) {
				$x = $matches[1]; // use first capture group
			}
		}
		return $x;
	
    
  }

  public static function canonicalize_nname($x) { 
    
    
    // --- BLOCK: main ---
    
		if ($x) {
			if (preg_match('/.+ ([^ ]+)/s', $x, $matches)) {
				$x = $matches[1]; // use first capture group
			}
		}
		return $x;
	
    
  }

  public static function canonicalize_plz($x) { 
    
    
    // --- BLOCK: main ---
    
		if ($x) {
			if (preg_match('/([0-9]{4-5})/s', $x, $matches)) {
				$x = $matches[1]; // use first capture group
			}
		}
		return $x;
	
    
  }

  public static function canonicalize_ort($x) { 
    
    
    // --- BLOCK: main ---
    
		if ($x) {
			if (preg_match('/[0-9]{4-5}\\s+(.*)/s', $x, $matches)) {
				$x = $matches[1]; // use first capture group
			}
		}
		return $x;
	
    
  }

  public static function canonicalize_strasse($x) { 
    
    
    // --- BLOCK: main ---
    
		if ($x) {
			if (preg_match('/(.*)\\s*[0-9]{4-5}/s', $x, $matches)) {
				$x = $matches[1]; // use first capture group
			}
		}
		return $x;
	
    
  }

  public static function parseText($x) { 
    
    
    // --- BLOCK: main ---
    
	if ($x === null) return '';
	else return $x;

    
  }

  public static function parseDate($x) { 
    
    
    // --- BLOCK: main ---
    
	// x ist gültiger Integer mit plausiblem Timestamp-Wert:
	if ($x > time() / 2) return intval($x);
	// Format dd.mm.yyyy gesondert behandeln, weil strtotime dort fehlerhaft ist
	if (preg_match('/[0-9]{1,2}.[0-9]{1,2}.[0-9]{4}$/', $x)) {
		$ts = date_parse_from_format('j.n.Y', $x);
		if (!$ts) return strtotime($x);		// fallback
		$ts = mktime(0, 0, 0, $ts['month'], $ts['day'], $ts['year']);
		return $ts;
	}
	// Format dd.mm.yy gesondert behandeln, weil strtotime dort fehlerhaft ist
	if (preg_match('/[0-9]{1,2}.[0-9]{1,2}.[0-9]{2}$/', $x)) {
		$ts = date_parse_from_format('j.n.y', $x);
		if (!$ts) return strtotime($x);		// fallback
		$ts = mktime(0, 0, 0, $ts['month'], $ts['day'], $ts['year']);
		return $ts;
	}
	// sonst PHP-Funktion strtotime (gibt bei Scheitern false zurück)
	$ts = strtotime($x);
	return $ts ? $ts : null;

    
  }

  public static function parseDateExcel($x) { 
    
    
    // --- BLOCK: main ---
    
       return \PhpOffice\PhpSpreadsheet\Shared\Date::excelToTimestamp(floatval($x), date_default_timezone_get());
    
  }

  public static function color_filter($x) { 
    
    
    // --- BLOCK: main ---
    if (preg_match('/^#([a-f0-9]{3}){1,2}$/i', $x, $match)) return $x; else return 'none';
    
  }


  
  

  
  
}



