-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunctions.php
More file actions
201 lines (171 loc) · 4.68 KB
/
functions.php
File metadata and controls
201 lines (171 loc) · 4.68 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
function contains_null_values ($array) {
foreach ($array as $key => $value) {
if(is_null($value)) {
return true;
}
}
return false;
}
function dier ($data, $enable_html = 0, $exception = 0) {
$source_data = debug_backtrace()[0];
@$source = 'Called by <b>'.debug_backtrace()[1]['file'].'</b>::<i>'.debug_backtrace()[1]['function'].'</i>, line '.htmlentities($source_data['line'])."<br>\n";
$print = $source;
$print .= "<pre>\n";
ob_start();
print_r($data);
$buffer = ob_get_clean();
if($enable_html) {
$print .= $buffer;
} else {
$print .= htmlentities($buffer);
}
$print .= "</pre>\n";
$print .= "Backtrace:\n";
$print .= "<pre>\n";
foreach (debug_backtrace() as $trace) {
$print .= htmlentities(sprintf("\n%s:%s %s", $trace['file'], $trace['line'], $trace['function']));
}
$print .= "</pre>\n";
if(!$exception) {
print $print;
exit();
} else {
throw new Exception($print);
}
}
function _js ($file, $async=0, $defer=0) {
if(!file_exists($file)) {
die("$file does not exist");
}
$hostname = php_uname("n");
if (!in_array($hostname, ["arbeitslaptop", "norman-20fms48v00"])) {
$t = get_file_state_identifier($file);
$file = $file . "?t=$t";
}
if($async && $defer) {
print "<script async defer crossorigin src='$file'></script>";
} else if($async && !$defer) {
print "<script async crossorigin src='$file'></script>";
} else if(!$async && $defer) {
print "<script defer defer crossorigin src='$file'></script>";
} else {
print "<script src='$file' crossorigin></script>";
}
print "\n";
}
function _css ($file, $id=null) {
if(!file_exists($file)) {
die("$file does not exist");
}
$t = get_file_state_identifier($file);
$file = $file . "?t=$t";
if($id) {
print "<link href='$file' rel='stylesheet alternative' id='$id'>";
} else {
print "<link href='$file' rel='stylesheet'>";
}
print "\n";
}
function get_string_between($string, $start, $end){
$string = ' ' . $string;
$ini = strpos($string, $start);
if ($ini == 0) return '';
$ini += strlen($start);
$len = strpos($string, $end, $ini) - $ini;
return substr($string, $ini, $len);
}
function parse_markdown_links ($markdown) {
$str = "<ul>\n";
foreach(preg_split("/((\r?\n)|(\r\n?))/", $markdown) as $line){
if(!preg_match("/^\s*$/", $line)) {
if(preg_match("/^\s*-\s*\[(.*?)\]\((.*?)\)\s*$/", $line, $matches)) {
$str .= "<li><a target='_blank' class='sources_popup_link' href='".$matches[2]."'>".$matches[1]."</a></li>\n";
}
}
}
$str .= "</ul>\n";
return $str;
}
function filter_str_int ($data) {
if(is_array($data)) {
return null;
}
return $data;
}
function _assert ($condition, $message) {
if(!$condition) {
die($message);
}
}
function get_last_commit_hash_of_file ($file) {
if(file_exists($file)) {
return chop(shell_exec("git log -n 1 --pretty=format:%H -- $file | cat"));
} else {
die("$file not found.");
}
}
function get_file_state_identifier ($file) {
$git_hash = ""; #get_last_commit_hash_of_file($file);
if(!$git_hash) {
if(file_exists($file)) {
return filemtime($file);
} else {
die("$file not found");
}
}
return $git_hash;
}
function get_git_hash () {
$rev = "";
if(file_exists("git_hash")) {
$rev = chop(chop(file_get_contents("git_hash")));
return $rev;
}
if(file_exists(".git/refs/heads/master")) {
$rev = chop(file_get_contents(".git/refs/heads/master"));
return $rev;
}
return "";
}
function _include ($fn) {
if(file_exists($fn)) {
include($fn);
} else {
?>
<script>alert("<?php print $fn; ?> not found");</script>
<?php
}
}
function generateHTMLFromDataArray($dataArray) {
if (!is_array($dataArray)) {
throw new InvalidArgumentException("Invalid input provided. Expected an array.");
}
$html = "";
foreach ($dataArray as $data) {
$html .= "<div class='folie' style='display: none'>";
if (!isset($data['heading']) || ((!isset($data['list']) || !is_array($data['list'])) && (!isset($data["html"])))) {
throw new InvalidArgumentException("Invalid structure for page data. Each item must have 'heading' and 'list'.");
}
$html .= "<h3>" . htmlspecialchars($data['heading']) . "</h3>\n";
if (empty($data['list'])) {
if(empty($data["html"])) {
$html .= "<p>No items in the list.</p>\n";
}
} else {
$html .= "<ul class='presentation_ul'>\n";
foreach ($data['list'] as $item) {
$html .= " <li class='presentation_li'>" . $item . "</li>\n";
}
$html .= "</ul>\n";
}
if(isset($data["html"])) {
$html .= "<p>".$data["html"]."</p>\n";
} else {
$html .= "<p>—</p>\n";
}
$html .= "</div>";
}
return $html;
}
?>