forked from Zavy86/WikiDocs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.inc.php
85 lines (81 loc) · 2.29 KB
/
functions.inc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
/**
* Functions
*
* @package WikiDocs
* @repository https://github.com/Zavy86/wikidocs
*/
/**
* Dump a variable into a debug box (only if debug is enabled)
*
* @param mixed $variable Dump variable
* @param ?string $label Dump label
* @param ?string $class Dump class
* @param bool $force Force dump also if debug is disabled
*/
function wdf_dump($variable,?string $label=null,?string $class=null,bool $force=false):void{
if(!DEBUG && !$force){return;}
echo "\n<!-- dump -->\n";
echo "<pre class='debug ".$class."'>\n";
if($label<>null){echo "<b>".$label."</b>\n";}
if(is_string($variable)){$variable=str_replace(array("<",">"),array("<",">"),$variable);}
print_r($variable);
echo "</pre>\n<!-- /dump -->\n";
}
/**
* Redirect (if debug is enabled show a redirect link)
*
* @param string $location Location URL
*/
function wdf_redirect(string $location):void{
if(DEBUG){die("<a href=\"".$location."\">".$location."</a>");}
exit(header("location: ".$location));
}
/**
* Alert (if debug is enabled show a debug message)
*
* @param string $message Alert message
* @param string $class Alert class (success|info|warning|danger)
* @return bool
*/
function wdf_alert(string $message,string $class="info"):bool{
// checks
if(!$message){return false;}
// build alert object
$alert=new stdClass();
$alert->timestamp=time();
$alert->message=$message;
$alert->class=$class;
// check for debug
if(!DEBUG){
// add alert to session alerts
$_SESSION['wikidocs']['alerts'][]=$alert;
}else{
// swicth class
switch($class){
case "success":$message="(!) ".$message;break;
case "warning":$message="/!\\ ".$message;break;
case "danger":$message="<!> ".$message;break;
default:$message="(?) ".$message;
}
// dump alert
wdf_dump($message,"ALERT");
}
// return
return true;
}
/**
* Timestamp Format
*
* @param ?int $timestamp Unix timestamp
* @param string $format Date Time format (see php.net/manual/en/function.date.php)
* @return string|boolean Formatted timestamp or false
* @throws Exception
*/
function wdf_timestamp_format(?int $timestamp,string $format="Y-m-d H:i:s"){
if(!is_numeric($timestamp) || $timestamp==0){return false;}
// build date time object
$datetime=new DateTime("@".$timestamp);
// return date time formatted
return $datetime->format($format);
}