forked from Zavy86/WikiDocs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.inc.php
133 lines (125 loc) · 3.35 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?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);
}
/**
* Regenerate Sitemap
*/
function wdf_regenerate_sitemap(){
$baseURL=URL;
$lastMod=date('Y-m-d\TH:i:sP',Document::getUpdateDate("/homepage"));
// open sitemap
$sitemap=<<<EOS
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>$baseURL</loc>
<lastmod>$lastMod</lastmod>
</url>
EOS;
// add documents from root
$sitemap.=wdf_regenerate_sitemap_documents();
// close sitemap
$sitemap.=<<<EOS
</urlset>
EOS;
// write sitemap to file
file_put_contents(DIR."sitemap.xml",$sitemap);
}
function wdf_regenerate_sitemap_documents(?string $parent=null){
$sitemap='';
$documents=Document::index($parent);
// cycle all documents
foreach($documents as $document){
// set variables
$url=URL.$document->url;
$lastMod=date('Y-m-d\TH:i:sP',Document::getUpdateDate($document->url));
// add to sitemap
$sitemap.=<<<EOS
<url>
<loc>$url</loc>
<lastmod>$lastMod</lastmod>
</url>
EOS;
// add sub documents recursively
$sitemap.= wdf_regenerate_sitemap_documents($document->url);
}
return $sitemap;
}