<?php


/**
 * file: utility.php
 *
 * Copyright Miller Electric Mfg. Co. as an unpublished work.
 * All rights reserved.
 *
 * The information contained herein is confidential property of
 * Miller Electric Mfg. Co.  The use of, copying, transfer or
 * disclosure of such information is prohibited except by express
 * written agreement with Miller Electric.
 */


/**
 * Navigates to the given path.
 * This function calls header() then exit().
 * @param string The path to navigate to.
 */
function navigateTo($pagePath) {
	header("Location: $pagePath");
	exit();
}


/**
 * Navigates to the login page.
 * @see navigateTo().
 */
function navigateToLoginPage() {
	navigateTo('/password/login.php');
}


/**
 * Navigates to the page for setting a password.
 * @see navigateTo().
 */
function navigateToSetPasswordPage() {
	navigateTo('/password/set_password.php');
}


/**
 * Gets $inFilePath's contents as an array of json data put into $outJsonArray.
 * Specify if the array should be associative with $associative as true or 
 * false.
 * Returns true if the file previously existed, or was successfully created,
 * false if not.
 */
function getJsonArrayFromFile($inFilePath, $associative, &$outJsonArray) {
	$fileExists = file_exists($inFilePath);
	if($fileExists) {
		$jsonString = file_get_contents($inFilePath);
		$outJsonArray = json_decode($jsonString, $associative);
	} else {
		$fileExists = (file_put_contents($inFilePath, '') !== false);
	}

	return $fileExists;
}


function writeJsonArrayToFile($inFilePath, $pretty, $inJsonArray) {
	$result = -1;
	$jsonString = '';

	// Save the json data with the new password to the file.
	$jsonString = json_encode($inJsonArray, $pretty ? JSON_PRETTY_PRINT : 0);
	$result = file_put_contents($inFilePath, $jsonString);

	return $result;
}