<?php
	require_once("../system.engine/system.core.php");
	
	$mPong = array();
	$a = &$mPong;
	$p = &$_POST;
	
	global $systemHandler;
	$systemExecution = $systemHandler->loadHandler("EXECUTION");
	$systemHash = $systemHandler->loadHandler("HASH");
	/* ### DO YOUR CODES ONLY FROM HERE ### */
	
	$mSettingLocation = $p["settingLocation"];
	$mSettingValue = $p["settingValue"];
	$mUserId = !isset($p["settingUser"]) ? $systemHandler->loadHandler("USER")->mUserId : $p["settingUser"];
	$mAdminRequired = isset($p["settingUser"]);
	$mIsPrivileged = true;
	if($mAdminRequired)
		$mIsPrivileged = $systemHandler->loadHandler("USER")->mAdmin;
	
	if($mIsPrivileged) {
		switch(strtolower($mSettingLocation)) {
			case "firstname":
				$systemExecution->runQuery("UPDATE web_user SET firstname = '%s' WHERE id = '%s'", array($mSettingValue, $mUserId));
			break;
			
			case "lastname":
				$systemExecution->runQuery("UPDATE web_user SET lastname = '%s' WHERE id = '%s'", array($mSettingValue, $mUserId));
			break;
		
			case "workplace":
				$systemExecution->runQuery("UPDATE web_user SET workplace = '%s' WHERE id = '%s'", array($mSettingValue, $mUserId));
			break;
			
			case "residence":
				$systemExecution->runQuery("UPDATE web_user SET residence = '%s' WHERE id = '%s'", array($mSettingValue, $mUserId));
			break;
			
			case "details":
				$mSettingValue = strlen($mSettingValue) > 250 ? substr($mSettingValue, 0, 250) : $mSettingValue;
				$systemExecution->runQuery("UPDATE web_user SET details = '%s' WHERE id = '%s'", array($mSettingValue, $mUserId));
			break;
			
			case "mail":
				if($systemHandler->loadHandler("USER")->mUserId !== "17") {
					if (filter_var($mSettingValue, FILTER_VALIDATE_EMAIL))
						$systemExecution->runQuery("UPDATE web_user SET mail = '%s' WHERE id = '%s'", array($mSettingValue, $mUserId));
				}
			break;
			
			case "pass":
				if($systemHandler->loadHandler("USER")->mUserId !== "17") {
					$mNewPass = $systemHandler->loadHandler("HASH")->hashString($mSettingValue);
					$systemExecution->runQuery("UPDATE web_user SET password = '%s'", $mNewPass);
				}
			break;
		}
	}

	/* ### DO NOT CHANGE ANYTHING FROM HERE ### */
	header("Content-Type: application/json");
	echo json_encode($mPong);
?>