forked from aqibos/CookbookNetwork
/
recipe-search-handler.php
216 lines (187 loc) · 4.88 KB
/
recipe-search-handler.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
<?php
function titleSearch()
{
$conn = getConn() ;
$title = clean($_POST["title"]);
$title = $conn -> real_escape_string($title) ;
$sql = "SELECT * FROM recipe WHERE recipe_title ='$title'";
printResult($conn -> query($sql)) ;
$conn -> close() ;
}
function ingredientSearch()
{
$conn = getConn() ;
$ingredients = array_filter($_POST["ingredient"]);
$len = count($ingredients);
if($len <= 1)
{
$ing = clean($ingredients[0]);
$ing = $conn -> real_escape_string($ing) ;
$sql = " SELECT * FROM recipe
WHERE recipe_id in (
SELECT recipe_id FROM ingredient
WHERE name='$ing' )";
}
else
{
$first = clean($ingredients[0]) ;
$first= $conn -> real_escape_string($first) ;
$sql = " SELECT recipe_id FROM ingredient
WHERE name='$first'";
for($i = 1 ; $i < $len ; $i++ )
{
$current = clean($ingredients[$i]) ;
$current = $conn -> real_escape_string($current) ;
$sql = " SELECT recipe_id FROM ingredient
WHERE name = '$current'
AND recipe_id in (".$sql.")";
}
$sql = " SELECT * FROM recipe
WHERE recipe_id in (".$sql.")";
}
printResult( $conn -> query($sql) ) ;
$conn -> close() ;
}
function tagBrowse()
{
$conn = getConn() ;
$tags = $_POST["tags"] ;
$len = count($tags) ;
if($len <= 1)
{
$tag = $tags[0] ;
$sql = " SELECT * FROM recipe
WHERE recipe_id in (
SELECT type_id FROM tag
WHERE name = '$tag' AND type = 'RECIPE')";
}
else
{
$first = $tags[0] ;
$sql = " SELECT type_id FROM tag
WHERE name = '$first AND type = 'RECIPE'";
for($i = 1 ; $i < $len ; $i++)
{
$current = $tags[$i] ;
$sql = " SELECT type_id FROM tag
WHERE name = '$current'
AND type = 'RECIPE'
AND type_id in (".$sql.")" ;
}
$sql = " SELECT * FROM recipe
WHERE recipe_id in (".$sql.")";
}
printResult($conn -> query($sql)) ;
$conn -> close() ;
}
function getConn()
{
include 'db-credentials.php';
return new mysqli($servername, $username, $password, $dbname);
}
function printResult($result)
{
$visibleRecipes = $rowNum = $result -> num_rows ;
$html = "" ;
while($rowNum > 0)
{
$html .= "<div class=\"recipe-preview-row\">";
for($i = 0 ; $i < 3 ; $i++ )
{
if($row = $result -> fetch_assoc() )
{
if(isVisible($row["recipe_id"]))
$html .= "<a href=\"view-recipe.php?recipe_id=".$row["recipe_id"]."\">
<div class=\"recipe-preview-row-icon\">
<img class=\"thumbnail\" src=\"".$row["img_path"]."\">
<p>".$row["recipe_title"]."</p>
</div>
</a>";
else
$visibleRecipes-- ;
}
$rowNum = $rowNum - 1 ;
}
$html .= "</div>";
}
echo '<h1>'.$visibleRecipes.' recipes were found:</h1>' ;
echo $html ;
}
function printResultWithX($result)
{
$rowNum = $result -> num_rows ;
echo '<h1>'.$rowNum.' recipes were found:</h1>' ;
while($rowNum > 0)
{
echo'<div class="recipe-preview-row">';
for($i = 0 ; $i < 3 ; $i++ )
{
if($row = $result -> fetch_assoc() )
{
if(isVisible($row["recipe_id"]))
{
echo '<a href="view-recipe.php?recipe_id='.$row["recipe_id"].'">
<div class="recipe-preview-row-icon">
<img class="thumbnail" src="'.$row["img_path"].'">
<p>'.$row["recipe_title"].'</p>
</div>
</a>
<a href="delete.php?recipe_id='.$row["recipe_id"].'" onclick="return confirm(\'Are you sure youu want to delete '.$row["recipe_title"].'\')">
<img class="x" src="images/x.png"></a>';
}
}
$rowNum = $rowNum - 1 ;
}
echo'</div>';
}
}
function isVisible($recipe_id)
{
$visibility = getVisibility($recipe_id) ;
if($visibility == 'PRIVATE' )
return isset($_SESSION["loggedin"]) and isAuthor($recipe_id) ;
else if($visibility == 'PUBLIC')
return true ;
else if($visibility == 'REGISTERED')
return isset($_SESSION["loggedin"]) and $_SESSION["loggedin"] ;
else if($visibility == 'FRIENDLY')
return isset($_SESSION["loggedin"]) and isAuthor($recipe_id) or isFriend($recipe_id) ;
}
function getVisibility($recipe_id)
{
$conn = getConn() ;
$sql = "SELECT visibility FROM recipe WHERE recipe_id = '$recipe_id'" ;
$result = $conn -> query($sql) ;
$row = $result -> fetch_assoc();
return $row["visibility"];
}
function isAuthor($recipe_id)
{
$conn = getConn() ;
$sql = " SELECT author FROM recipe
WHERE recipe_id = '$recipe_id'" ;
$result = $conn -> query($sql) ;
$row = $result -> fetch_assoc() ;
return $_SESSION["userid"] == $row["author"] ;
}
function isFriend($recipe_id)
{
$conn = getConn() ;
$userid = $_SESSION["userid"] ;
$sql = " SELECT * FROM account
WHERE user_id = '$userid'
AND email in (
SELECT email FROM friends
WHERE type = 'RECIPE'
AND type_id = '$recipe_id')" ;
$result = $conn -> query($sql) ;
return $result -> num_rows > 0 ;
}
function clean($data)
{
$data = trim($data) ;
$data = stripslashes($data) ;
$data = htmlspecialchars($data) ;
return $data ;
}
?>