<?php if (!defined('PmWiki')) exit();

/*
Copyright (c) 2015, Martin Fick
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
	 list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
	 this list of conditions and the following disclaimer in the documentation
	 and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.

# Author:	Martin Fick
# RandyB added "^" to return URL request, COOKIE, or SESSION variable regardless of php configuration
# 20190501: Updated for PHP 7.2 by Petko Yotov pmwiki.org/petko
# 20220220: Fix variable name by Petko Yotov pmwiki.org/petko
*/

$RecipeInfo['HttpVariables']['Version'] = '20220617';

# {$?!|@^~var} http variable substitutions before {$var}
Markup('{$?|!@^~var}1', '<{$var}', '/\{\$([\?\!\|@^~])(\w+)\}/',
	"mu_HttpVariables");

# {$?!|@^~var} http variable substitutions after {$var}
Markup('{$?|!@^~var}2', '>{$var}', '/\{\$([\?\!\|@^~])(\w+)\}/',
	"mu_HttpVariables");

# (:cookie [name] [value] [args]:)
Markup('cookie', '>if', '/\(:cookie\s+(.*?)\s*:\)/i',
	"mu_HttpVariablesCookie");

# (:session [name] [value] [args]:)
Markup('session', '>if', '/\(:session\s+(.*?)\s*:\)/i',
	"mu_HttpVariablesSession");

function mu_HttpVariables($m) {
	extract($GLOBALS['MarkupToHTML']);
	return HttpVariables($pagename, $m[1], $m[2]);
}

function mu_HttpVariablesCookie($m) {
	extract($GLOBALS['MarkupToHTML']);
	return HttpVariablesCookie($pagename,	ParseArgs($m[1]));
}

function mu_HttpVariablesSession($m) {
	extract($GLOBALS['MarkupToHTML']);
	return HttpVariablesSession($pagename,	ParseArgs($m[1]));
}

function HttpVariablesCookie($pagename, $args) {
	$name = $args['name'] ?? null;
	$val = $args['value'] ?? null;
	$exp = $args['expires'] ?? null;
	$path = $args['path'] ?? null;
	$domain = $args['domain'] ?? null;
	$sec = $args['secure'] ?? null;
	$http = $args['httponly'] ?? null;

	$sargs = $args[''];
	foreach ($sargs as $arg) {
		switch ($arg) {
			case 'secure':
				$sec = true;
				break;
			case 'httponly':
				$http = true;
				break;
			default:
				if (!$name)
					$name = $arg;
				else if (!$val)
					$val = $arg;
				break;
		} 
	}
	if (!$name)
		return;

	if (!$val)
		$val = null;
	if (!$exp)
		$exp = 0;
	else
		$exp = strtotime($exp);
	if (!$path)
		$path = '';
	if (!$domain)
		$domain = '';
	if (!$sec)
		$sec = false;
	if (!$http)
		$http = false;

	setcookie($name, $val, $exp, $path, $domain, $sec, $http);

	return;
}

function HttpVariablesSession($pagename, $args) {
	$name = $args['name'];
	$val = $args['value'];
	$sargs = $args[''];
	foreach ($sargs as $arg) {
		if (!$name)
			$name = $arg;
		else if (!$val)
			$val = $arg;
	}
	if (!$name)
		return;
	session_start();
	$_SESSION[$name] = $val;
}

function HttpVariables($pagename, $type, $var) {
	global $Charset;
	switch ($type) {
		case '?':
			$val = $_GET[$var] ?? null;
			break;
		case '|':
			$val = $_POST[$var] ?? null;
			break;
		case '!':
			$val = $_REQUEST[$var] ?? null;
			break;
		case '@':
			$val = $_COOKIE[$var] ?? null;
			break;
		case '^': 
			$val = $_GET[$var] ?? $_COOKIE[$var] ?? $_SESSION[$var] ?? null;
			break;		
		case '~': session_start();
			$val = $_SESSION[$var] ?? null;
			break;
	}
	if (isset($val)) 
		return htmlentities(stripmagic($val), ENT_COMPAT, $Charset);
	return "";
}