This repository has been archived on 2022-07-28. You can view files and clone it, but cannot push or open issues or pull requests.
md-cms/inc/functions.php

222 lines
6.8 KiB
PHP
Raw Normal View History

2018-06-12 07:53:27 +02:00
<?PHP
/**
* Main file for functions.
*
* @file
*
* @author Joshua Ramon Enslin <joshua@jrenslin.de>
*/
/**
* Function ensureDir checks if a directory exists, and creates it if it does not yet.
*
* @param string $filepath File path.
*
* @return void
*/
function ensureDir(string $filepath) {
if (!is_dir($filepath)) mkdir($filepath);
}
/**
* Function ensureJson checks that a JSON file exists. If not, it creates an empty one at the specified location.
*
* @param string $filepath File path to the JSON file.
*
* @return void
*/
function ensureJson(string $filepath) {
if (!file_exists($filepath) or filesize($filepath) < 2) {
file_put_contents($filepath, "[]");
}
}
/**
* Function for ensuring the existence of an appropriate environment.
*
* @return void
*/
function ensureEnvironment() {
// Enable error reporting
error_reporting(E_ALL);
ini_set("display_errors", 1);
// Ensure existence of directories
foreach ([__DIR__ . "/../data", __DIR__ . "/../data/static", __DIR__ . "/../data/caches", __DIR__ . "/../js"] as $folder) {
ensureDir($folder);
}
// Ensure existence of settings files
foreach ([
__DIR__ . "/../data/settings.json",
__DIR__ . "/../data/caches/pages.json",
__DIR__ . "/../data/users.json",
] as $jsonFile) {
ensureJson($jsonFile);
}
}
/**
* Function scanDirConts is a wrapper around scandir(), which removes [".", ".."].
*
* @param string $folder Folder to scan.
*
* @return string[]
*/
function scanDirConts(string $folder):array {
return array_values(array_diff(scandir($folder), [".", "..", ".git"]));
}
/**
* Function printHiddenInputs takes an array, in which each entry stands for a new hidden input field.
* The key to the entry stands for the name attribute, the value for the value attribute.
*
* @param array $array Associative array containing all the variable names and corresponding values to print
* @param int $indent Integer describing how far each line in the returned string should be indented.
*
* @return string
*/
function printHiddenInputs(array $array, int $indent = 0):string {
$output = '';
$indentString = PHP_EOL;
for ($i = 0; $i < $indent; $i++) $indentString .= ' ';
foreach ($array as $name => $value) {
$output .= $indentString . '<input type="hidden" name="'.$name.'" value="'.$value.'" />';
}
return $output;
}
/**
* Function printHiddenInputsFromGlobalsSimple takes an array, in which each entry stands for a new hidden input field.
* The value stands for the name attribute of the input field and the key in $GLOBALS.
*
* @param array $array Input array.
* @param int $indent Number of spaces for indenting the output input fields in HTML. Optional.
*
* @return string
*/
function printHiddenInputsFromGlobalsSimple(array $array, int $indent = 0):string {
$output = '';
$indentString = PHP_EOL;
for ($i = 0; $i < $indent; $i++) $indentString .= ' ';
foreach ($array as $value) {
if (!isset($GLOBALS[$value])) continue;
$output .= $indentString . '<input type="hidden" name="'.$value.'" value="'.$GLOBALS[$value].'" />';
}
return $output;
}
/**
* Function write_get_vars prints checks for GET variables specified in the input array and returns them as a single string.
* Useful for avoiding long blocks of links working to write meaningful links.
*
* @param string[] $input Input array: both simple and associative arrays are accepted.
*
* @return string
*/
function write_get_vars(array $input):string {
// Check if keys have been specified in the array (in Python terms, if it is a dict or a list).
// If keys are not specified, write new working variable $vars with keys equaling the value.
// $str is the string that will eventually be returned.
$vars = array();
$str = '';
if (isset($input[0])) {
foreach ($input as $value) $vars[$value] = $value;
}
else $vars = $input;
// For each of the variables specified in $vars, check if a corresponding GET variable is set.
// If so, add that to the return string.
// The key is used in place of the original GET variable's name ($value),
// because some pages may have the same GET variables carry different names.
foreach ($vars as $key => $value) {
if (isset($GLOBALS['_GET'][$value])) $str .= '&'.$key.'='.$GLOBALS['_GET'][$value];
}
return ($str);
}
/**
* Function write_post_vars prints checks for POST variables specified in the input
* array and returns them as a single string.
* Useful for avoiding long blocks of links working to write meaningful links.
*
* @param string[] $input Input array: both simple and associative arrays are accepted.
*
* @return string
*/
function write_post_vars(array $input):string {
// Check if keys have been specified in the array (in Python terms, if it is a dict or a list).
// If keys are not specified, write new working variable $vars with keys equaling the value.
// $str is the string that will eventually be returned.
$vars = array();
$str = '';
if (isset($input[0])) {
foreach ($input as $value) $vars[$value] = $value;
}
else $vars = $input;
// For each of the variables specified in $vars, check if a corresponding GET variable is set.
// If so, add that to the return string. The key is used in place of the original POST variable's name ($value),
// because some pages may have the same GET variables carry different names.
foreach ($vars as $key => $value) {
if (isset($GLOBALS['_POST'][$value])) $str .= '&'.$key.'='.$GLOBALS['_POST'][$value];
}
return ($str);
}
/**
* Function write_common_vars prints checks for global variables specified in the input
* array and returns them as a single string.
* Useful for avoiding long blocks of links working to write meaningful links.
*
* @param string[] $input Input array: both simple and associative arrays are accepted.
*
* @return string
*/
function write_common_vars(array $input):string {
// Check if keys have been specified in the array (in Python terms, if it is a dict or a list).
// If keys are not specified, write new working variable $vars with keys equaling the value.
// $str is the string that will eventually be returned.
$vars = array();
$str = '';
if (isset($input[0])) {
foreach ($input as $value) $vars[$value] = $value;
}
else $vars = $input;
// For each of the variables specified in $vars, check if a corresponding GET variable is set.
// If so, add that to the return string. The key is used in place of the original GET variable's name ($value),
// because some pages may have the same GET variables carry different names.
foreach ($vars as $key => $value) {
if (isset($GLOBALS[$value])) $str .= '&'.$key.'='.$GLOBALS[$value];
}
return ($str);
}
?>