-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.php
215 lines (157 loc) · 5.89 KB
/
helpers.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
<?php
function upload_file($name) {
global $image_dir_path;
if (isset($_FILES[$name])) {
$filename = $_FILES[$name]['name'];
if (empty($filename)) {
return null;
}
$source = $_FILES[$name]['tmp_name'];
$target = $image_dir_path . DIRECTORY_SEPARATOR . $filename;
move_uploaded_file($source, $target);
return $filename;
// create the '400', '250', and '100' versions of the image
//process_image($image_dir_path, $filename);
}
return null;
}
function hashPassword($password) {
$crypt = crypt($password, 'Ns');
return $crypt;
}
//functions to sanitize and validate user input
function cleanString ($string){
$string = trim($string);
$string = removeTags ($string);
$string = filter_var($string, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
$string = filter_var($string, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
return $string;
}
function cleanEmail ($email){
$email = trim($email);
$email = removeTags($email);
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
$email = filter_var($email, FILTER_VALIDATE_EMAIL);
return $email;
}
function removeTags ($string){
$string = trim($string);
$string = filter_var($string, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH); //strip_tags($input);
return $string;
}
function encodeTags ($string){
$string = htmlentities($string);
/* $string = filter_var($string, FILTER_SANITIZE_SPECIAL_CHARS); //htmlspecialchars($input); */
return $string;
}
//BUILD FUNCTIONS
function createLink($id){
$link = "index.php?action=content&page_id=" . $id;
return $link;
}
function buildSearchResults($searchTerm){
$headers = array();
$headers = search($searchTerm);
if(!empty($headers)){
$navigation = "<ul>";
foreach ($headers as $head){
$navigation .= "<li><h2><a href='http://recipe.nathantschultz.com/?action=content&page_id=" . $head['post_id'] . "'>". $head['title'] . "</a></h2></li>";
}
$navigation .= "</ul>";
} else {
$navigation = 'Sorry, no results found.';
}
return $navigation;
}
function buildListOfContent(){
$headers = array();
$headers = getLinks();
if(is_array($headers)){
$navigation = "<ul>";
foreach ($headers as $head){
$navigation .= "<li><h2><a href='http://recipe.nathantschultz.com/?action=content&page_id=" . $head['post_id'] . "'>". $head['title'] . "</a></h2> <a href='http://recipe.nathantschultz.com/index.php?action=edit_content&page_id=". $head['post_id'] ."'>Edit </a> <a href='http://recipe.nathantschultz.com/index.php?action=confirm_delete_content&page_id=". $head['post_id'] ."'>Delete</a></li>";
}
$navigation .= "</ul>";
} else {
$navigation = 'Sorry, a critical error occurred.';
}
return $navigation;
}
function buildUserProfile($id){
$people = getPeople();
$content = null;
$admin = null;
foreach ($people as $person){
if ($person['user_id'] == $id){
if($person['admin']){
$admin = "YES";
} else {
$admin = "NO";
}
$content .= "<h2>".$person['name']."</h2><ul><li>EMAIL: ". $person['email']."</li><li>PASSWORD: ***********</li><li>ADMIN: ".$admin."</li><li><a href='http://recipe.nathantschultz.com/index.php?action=edit_user&page_id=".$person['user_id']."'>edit </a> <a href='http://recipe.nathantschultz.com/index.php?action=confirm_delete_user&page_id=".$person['user_id']."'>delete</a></li></ul>";
}
}
return $content;
}
function buildAdminProfile(){
//display content list link
//display list of users with editing abilitiy for each part
$people = getPeople();
$content = "<h1><a href='http://recipe.nathantschultz.com/index.php?action=list_content'>EDIT CONTENT</a></h1><h1>USERS:</h1><ul>";
$admin = null;
foreach ($people as $person){
if ($person['admin']) {
$admin = "YES";
} else {
$admin = "NO";
}
$content .= "<li><h2>".$person['name']."</h2><ul><li>EMAIL: ". $person['email']."</li><li>PASSWORD: ***********</li><li>ADMIN: ".$admin."</li><li><a href='http://recipe.nathantschultz.com/index.php?action=edit_user&page_id=".$person['user_id']."'>edit </a> <a href='http://recipe.nathantschultz.com/index.php?action=confirm_delete_user&page_id=".$person['user_id']."'>delete</a></li></ul></li>";
}
return $content;
}
function buildNav(){
$links = getLinks();
if (is_array($links)){
$nav = "";
foreach ($links as $link){
$nav .= "<li><a href='http://recipe.nathantschultz.com/?action=content&page_id=" . $link['post_id'] . "'>". $link['title'] . "</a>";
}
} else {
$nav = "<li>Sorry, a critical error occurred.</li>";
}
return $nav;
}
function buildContent($page_id){
$page = "";
//get contents of page
if ($page_id == 1){
$page .= "<h1 id='welcome'></h1>";
} else {
$contents = getContent($page_id);
if (empty($contents)){
$page .= "<h1 id='welcome'>404 Error:</h1><h1>Page not found</h1>";
} else {
foreach ($contents as $content){
if ($content['image_name']){
$page .= "<img src='/images/" . $content['image_name'] . "' alt='" . $content['title'] . "' >" . "<br />";
}
$page .= "<h1>" . $content['title'] . "</h1><p id='submitted'><em>submitted by " . $content['name'] . "</em></p><p><strong>Difficulty: </strong>" . $content['difficulty'] . "</p><p><strong>Cooking time:</strong> " . $content['cooking_time'] . "</p><p><strong>Ingredients:</strong> " . $content['ingredients'] . "</p><p><strong>Directions:</strong> " . $content['directions'] . "</p>";
}
}
}
include $_SERVER['DOCUMENT_ROOT'] . '/view/header.php';
//echo $alerts;
//$alerts = "";
echo "<section class='content' id='c$page_id'>";
if(!isset($_SESSION['$alerts'])){
$_SESSION['$alerts'] = false;
}
if($_SESSION['$alerts']){
echo $_SESSION['$alerts'] . "<br />";
$_SESSION['$alerts'] = "";
}
//display content and footer
echo $page;
echo "</section></body></html>";
}
?>