<?php

/**
 * 
 * @param type $sz
 */
function redirect($sz) {
	header('Location: '.$sz);
	die();
}

/**
 * 
 * @param type $sz
 * @return type
 */
function hesc($sz) {
	return @htmlentities($sz, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
}

/**
 * 
 * @staticvar array $month_names
 * @param type $d
 * @return type
 */
function month($d) {
	static $month_names = array(
		'January', 'February', 'March', 'April', 'May', 'June', 'July',
		'August', 'September', 'October', 'November', 'December'
	);
	return substr($month_names[$d - 1], 0, 3);
}

/**
 * 
 * @param type $val
 * @param type $min
 * @param type $max
 * @return type
 */
function clamp($val, $min, $max) {
	return max($min, min($max, $val));
}

/**
 * 
 * @param type $x
 * @return type
 */
function hasGet($x) {
	return array_key_exists($x, $_GET);
}

/**
 * 
 * @return type
 */
function getHttpRoot() {
	$base = $_SERVER['REQUEST_URI'];
	if ($base[0] !== '/') {
		$base = '/'.$base;
	}
	
	$qp = strpos($base, '?');
	if ($qp !== false) {
		$base = substr($base, 0, $qp);
	}
	
	return $base;
}

function monthrange($y1, $m1, $y2, $m2) {
	$ret = [];

	for ($y = $y1, $y_e = $y2+1; $y < $y_e; $y++) {
		for ($m = 1; $m < 13; $m++) {
			if ($y == $y1 && $m < $m1) continue;
			if ($y == $y2 && $m > $m2) continue;
			
			$ret[] = [$y, $m];
		}
	}
	
	return $ret;
}
