This repository has been archived by the owner on Oct 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
executable file
·184 lines (140 loc) · 4.14 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
<?php
/**
* Created by PhpStorm.
* User: DDS
* Date: 2018.06.19
* Time: 16:05
*/
require_once(__DIR__ . "/config.php");
function is_child(&$parents, $id, $parent_id) {
if (($id == 0) && ($parent_id != 0)) {
return false;
}
if (($parent_id == 0)) {
return true;
}
if ($parents[$id] == $parent_id) {
return true;
}
if ($id == $parent_id) {
return true;
}
if ($parents[$id] == 0) {
return false;
}
return is_child($parents, $parents[$id], $parent_id);
}
function get_parents(&$src_data) {
return get_param($src_data, 'parent_id');
}
function get_param(&$src_data, $param) {
$result = array();
foreach ($src_data as $id => $row) {
$result[$id] = $row->$param;
}
return $result;
}
function get_labels(&$src_data) {
return get_param($src_data, 'label');
}
function get_urls(&$src_data) {
return get_param($src_data, 'url');
}
function has_children(&$parents, $id) {
if ($id == 0) {
return false;
}
foreach ($parents as $parent) {
if ($parent == $id) {
return true;
}
}
return false;
}
function update_has_child(&$conn, $id, $has_child) {
$s_has_child = intval($has_child);
$s_id = intval($id);
$sql = "UPDATE `links` SET has_child = " . $s_has_child ." WHERE id = " . $s_id;
$result = $conn->query($sql);
return $result;
}
function encode_label_as_url($label) {
$ulabel = strtolower($label);
$ulabel = str_replace(" ", "-", $ulabel);
$ulabel = str_replace("/", "-", $ulabel);
$ulabel = rawurlencode($ulabel);
return $ulabel;
}
function redirect_to_main($error) {
if ($error == true) {
unset($_POST);
header("Refresh: 3; url='admin.php'");
echo "Redirecting to main page...\n";
} else {
header("Location: admin.php");
}
}
function validate_url(&$parents, &$urls, $id, $parent, $url) {
foreach ($urls as $uid => $uurl) {
// check if this item has the same parent
if (($parents[$uid] == $parent) && ($uid != $id)) {
// if it does, check if the specified URL and this item's URL are the same
// TODO: Question: CASE SENSITIVE ?
if (strcmp($url, $uurl) == 0) {
// if they are, return false
return false;
}
}
}
return true;
}
function get_data_by_args(&$conn, $table, $what, $args = "") {
if (is_array($what)) {
$request = count($what) > 0 ? implode(", ", $what) : "*";
$key_field = isset($what[0]) ? $what[0] : "id";
} else {
$request = $what;
$key_field = "id";
}
$sql = "SELECT $request FROM `$table`";
if (strlen($args) > 0) {
$sql .= " WHERE $args";
}
$result = $conn->query($sql);
$params = array();
if ($result->num_rows > 0) {
while ($row = $result->fetch_object()) {
$params[$row->$key_field] = $row;
}
}
return $params;
}
function get_menus_by_parent_id(&$conn, $id) {
return get_data_by_args($conn, "links", "*", "parent_id = $id && active = 1 ORDER BY sort");
}
function connect_db() {
return new mysqli(SQL_SERVER_NAME, SQL_SERVER_USER, SQL_SERVER_PASS, SQL_SERVER_DBNAME);
}
function get_menu_types(&$conn) {
$data = get_data_by_args($conn, "menutypes", "*", "");
return get_param($data, "name");
}
function generate_dropdown($name, &$items, $selected_item) {
if (count($items) == 0) {
return;
}
echo "<select name='$name'>\n";
foreach ($items as $id => $item) {
$selected = $id == $selected_item ? " selected='selected'" : "";
echo "<option value='$id'$selected>$item</option>\n";
}
echo "</select>\n";
}
function sanitize_array(&$conn, &$arr, $ints = false) {
if (count($arr) == 0) {
return;
}
foreach ($arr as $id => $elem) {
$arr[$id] = $ints ? intval($elem) : $conn->real_escape_string(strip_tags($elem));
}
}