-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
executable file
·289 lines (251 loc) · 9.38 KB
/
functions.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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<?php
// Get configuration variables ///////////////////////////////////////////////
function config($key = '') {
$config = parse_ini_file("/srv/config.ini");
return isset($config[$key]) ? $config[$key] : null;
}
// Create DB connection //////////////////////////////////////////////////////
function dbConnect() {
$con = new mysqli(config('server'), config('username'), config('password'), config('db'));
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
die();
}
return $con;
}
// Set GET variables that Apache hides ///////////////////////////////////////
function getGetVars() {
$uri = explode('?', $_SERVER['REQUEST_URI']);
if (!empty($uri[1])) {
$getAssignments = explode('&', $uri[1]);
foreach ($getAssignments as $assignment) {
$var = explode('=', $assignment);
$_GET["$var[0]"] = urldecode($var[1]);
}
}
}
// Create Array of all posts /////////////////////////////////////////////////
function getAllPosts($id="") {
$postsArr = [];
$sql = "SELECT * FROM posts ORDER BY id";
if ($result = mysqli_query(dbConnect(), $sql)) {
while ($row = mysqli_fetch_array($result)) {
$postsArr[$row['id']] = $row;
}
}
if (!empty($id)) {
return in_array("$id", $postsArr) ? $postsArr["$id"] : '';
}
return $postsArr;
}
// Get details of a post /////////////////////////////////////////////////////
function getPostArray($id) {
//var_dump($GLOBALS['allposts']);
return in_array("$id", $GLOBALS['allposts']) ? $GLOBALS['allposts']["$id"] : '';
}
// Create Array of posts arranged by parent it ///////////////////////////////
function getChildrenArray() {
$children = [];
$sql = "SELECT * FROM posts WHERE time < CURRENT_TIMESTAMP ORDER BY sort, time, title";
if ($result = mysqli_query(dbConnect(), $sql)) {
while ($row = mysqli_fetch_array($result)) {
if (!isset($children["$row[parent]"])) {
$children["$row[parent]"] = [];
}
$children["$row[parent]"][] = $row;
}
}
return $children;
}
//Get children of provided parent id /////////////////////////////////////////
function getChildren($parentid) {
$sql = "SELECT * FROM posts
WHERE parent = $parentid
AND time < CURRENT_TIMESTAMP
ORDER BY sort ASC,
time ASC,
title ASC";
if ($result = mysqli_query(dbConnect(), $sql)) {
if (mysqli_num_rows($result) > 0) {
return $result;
}
}
return "";
}
// Get posts by location /////////////////////////////////////////////////////
function getPostByLocation($location) {
$allposts = getAllPosts();
$location = strtolower(trim($location));
$location = explode('/', $location);
$temploc = implode('/', $location);
$location = $temploc;
foreach ($allposts as $post) {
if (in_array($location, $post)) {
$postDetails = $post;
break;
}
}
return !empty($postDetails) ? $postDetails : '';
}
//Create array of navigation links ///////////////////////////////////////////
function createNavLinks() {
$navArr = [];
$topLinks = getChildren(0);
if (!empty($topLinks)) {
while ($topLink = mysqli_fetch_array($topLinks)) {
$navArr["$topLink[id]"] = $topLink;
$midLinks = getChildren($topLink['id']);
if (!empty($midLinks)) {
while ($midLink = mysqli_fetch_array($midLinks)) {
$navArr["$topLink[id]"]["children"]["$midLink[id]"] = $midLink;
$btmLinks = getChildren($midLink['id']);
if (!empty($btmLinks)) {
while ($btmLink = mysqli_fetch_array($btmLinks)) {
$navArr["$topLink[id]"]["children"]["$midLink[id]"]["children"]["$btmLink[id]"] = $btmLink;
}
}
}
}
}
}
return $navArr;
}
// Get an array of all tags //////////////////////////////////////////////////
function allTags() {
$alltags = [];
$tagsql = "SELECT tags FROM posts WHERE time < CURRENT_TIMESTAMP";
if ($tagresult = mysqli_query(dbConnect(), $tagsql)) {
while ($row = mysqli_fetch_array($tagresult)) {
if (!empty($row['tags'])) {
$taggrp = explode(",", $row['tags']);
foreach ($taggrp as $atag) {
if (!in_array($atag, $alltags)){
$alltags[] = $atag;
}
}
}
}
}
sort($alltags, SORT_STRING);
return $alltags;
}
// Get an array of all tags //////////////////////////////////////////////////
function getAllTags ($selected="") {
$alltags = allTags();
if (!empty($alltags)) {
echo "<br /><p><i class='glyphicon glyphicon-tags'></i> ";
foreach ($alltags as $tag) {
$tagclass = $tag == $selected ? "label-primary" : "label-default";
echo "<a href='/search/?tag=$tag' class='label $tagclass'>$tag</a> ";
}
echo "</p>";
}
}
// Get the theme array ///////////////////////////////////////////////////////
function theme($key="") {
if (!empty($_GET['theme'])) {
$_SESSION['theme'] = $_GET['theme'];
}
$theme = [
"bodybg" => "#fcfcfc",
"contentbg" => "#ffffff",
"color" => "#333333",
"breadcrumb" => "",
"pager" => "",
"modal" => ""
];
if (!empty($_SESSION['theme']) && $_SESSION['theme'] == 'dark') {
$theme = [
"bodybg" => "#1c1c1c",
"contentbg" => "#222222",
"color" => "#aaaaaa",
"breadcrumb" => "background-color: #333333",
"pager" => "background-color: #2f2f2f;",
"modal" => ".modal-content{background-color:#222222;color:#aaaaaa;}"
];
} elseif (!empty($_SESSION['theme'])) {
unset($_SESSION['theme']);
}
return $theme["$key"];
}
// Get a list of all of share buttons ////////////////////////////////////////
function getShareButtons($url) {
$sharebtns = "
<a href='https://plus.google.com/share?url=$url' target='_blank'><img src='/resources/images/googleplus-share.png' /></a>
<a href='https://facebook.com/sharer.php?u=$url' target='_blank'><img src='/resources/images/facebook-share.png' /></a>
<a href='https://www.reddit.com/submit?url=$url' target='_blank'><img src='/resources/images/reddit-share.png' /></a>
<a href='https://twitter.com/share?url=$url' target='_blank'><img src='/resources/images/twitter-share.png' /></a>
";
return $sharebtns;
}
// Get top nav ///////////////////////////////////////////////////////////////
function getTopNav() {
$navArr = createNavLinks();
foreach ($navArr as $topLink){
echo "<li><a href='$topLink[location]'>$topLink[title]";
if (array_key_exists("children", $topLink) && !empty($topLink["children"])) {
echo " <i class='glyphicon glyphicon-menu-down'></i></a><div class='navdrop'><ul>";
foreach ($topLink["children"] as $midLink) {
echo "<li><a href='$midLink[location]'>$midLink[title]";
if (array_key_exists("children", $midLink) && !empty($midLink["children"])) {
echo " <i class='glyphicon glyphicon-menu-down dropdown'></i></a><ul>";
foreach ($midLink["children"] as $btmLink) {
echo "<li><a href='$btmLink[location]'>$btmLink[title]</a></li>";
}
} else {
echo "</a><ul>";
}
echo "</ul></li>";
}
} else {
echo "</a><div class='hidden'><ul>";
}
echo "</ul><div></li>";
}
}
// Get side nav //////////////////////////////////////////////////////////////
function getSideNav() {
$navArr = createNavLinks();
foreach ($navArr as $topLink){
echo "<li><a href='$topLink[location]'>" . strtoupper($topLink['title']);
if (array_key_exists("children", $topLink) && !empty($topLink["children"])) {
echo " <i class='glyphicon glyphicon-menu-down dropdown'></i></a><ul>";
foreach ($topLink["children"] as $midLink) {
echo "<li><a href='$midLink[location]'> $midLink[title]";
if (array_key_exists("children", $midLink) && !empty($midLink["children"])) {
echo " <i class='glyphicon glyphicon-menu-down dropdown'></i></a><ul>";
foreach ($midLink["children"] as $btmLink) {
echo "<li><a href='$btmLink[location]'> $btmLink[title]</a></li>";
}
echo "</ul></li>";
} else { echo "</a></li>"; }
}
echo "</ul></li>";
} else { echo "</a></li>"; }
}
echo "</a></li>";
}
// Get Children of a post ////////////////////////////////////////////////////
function findChildren($id, $thisPostID) {
$children = getChildrenArray();
if (!empty($children["$id"])) {
echo "<ul>";
foreach ($children["$id"] as $child) {
if ($child["id"] == $thisPostID) {
echo "<li><b>$child[title]</b>";
} else {
echo "<li><a href='$child[location]'>$child[title]</a>";
}
if (!empty($children["$child[id]"])) {
findChildren($child["id"], $children, $thisPostID);
}
echo "</li>";
}
echo "</ul>";
}
}
// Subscribe if requested ////////////////////////////////////////////////////
function subscribe() {
return true;
}