-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction.php
301 lines (266 loc) · 11.3 KB
/
function.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
290
291
292
293
294
295
296
297
298
299
300
301
<?php
session_start();
function checkAccount(...$info): bool
{
if (count($info) === 3) {
[$conn, $email, $password] = $info;
$sql = "SELECT freelance_email, freelance_password FROM freelance_info
WHERE freelance_email = '$email' AND freelance_password = '$password'";
} elseif (count($info) === 2) {
[$conn, $email] = $info;
$sql = "SELECT freelance_email FROM freelance_info
WHERE freelance_email = '$email'";
} else {
echo "Please Fix.";
}
$result = $conn->query($sql) or trigger_error($conn->error);
return $result->num_rows;
}
function checkSession()
{
if (!isset($_SESSION['user_id'], $_SESSION['user_username'], $_SESSION['user_email'])) {
session_destroy();
header("location: login.php");
}
}
function checkSessionGoToGig()
{
if (isset($_SESSION['user_id'], $_SESSION['user_username'], $_SESSION['user_email'])) {
header("location: gigs.php");
}
}
function infoEncrypt($simple_string)
{
$ciphering = "AES-128-CTR";
$iv_length = openssl_cipher_iv_length($ciphering);
$options = 0;
$encryption_iv = '1234567891011121';
$encryption_key = "GeeksforGeeks";
$encryption = openssl_encrypt($simple_string, $ciphering, $encryption_key, $options, $encryption_iv);
return $encryption;
}
function infoDecrypt($encryption)
{
$ciphering = "AES-128-CTR";
$options = 0;
$decryption_iv = '1234567891011121';
$decryption_key = "GeeksforGeeks";
$decryption = openssl_decrypt($encryption, $ciphering, $decryption_key, $options, $decryption_iv);
return $decryption;
}
function getSessionValues()
{
global $userId, $userFName, $userLName, $userName, $userEmail;
$userId = $_SESSION['user_id'];
$userFName = $_SESSION['user_firstname'];
$userLName = $_SESSION['user_lastname'];
$userName = $_SESSION['user_username'];
$userEmail = $_SESSION['user_email'];
}
function get_timeago($ptime)
{
$estimate_time = time() - $ptime;
if ($estimate_time < 1) {
return 'less than 1 second ago';
}
$condition = array(
12 * 30 * 24 * 60 * 60 => 'year',
30 * 24 * 60 * 60 => 'month',
24 * 60 * 60 => 'day',
60 * 60 => 'hour',
60 => 'minute',
1 => 'second'
);
foreach ($condition as $secs => $str) {
$d = $estimate_time / $secs;
if ($d >= 1) {
$r = round($d);
return 'about ' . $r . ' ' . $str . ($r > 1 ? 's' : '') . ' ago';
}
}
}
function add_image($conn, $submitBtn, $chooseImg, $database = "freelance_info"): string
{
if (isset($conn, $submitBtn) && $conn) {
$target_dir = "./assets/img/";
$filename = htmlspecialchars(basename($chooseImg['name']));
$target_file = $target_dir . $filename;
$imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
$phpFile = $database === "freelance_info" ? "settings" : "mygigs";
$user_id = $_SESSION['user_id'];
$status = 1;
if (file_exists($target_file)) {
if ($database === "freelance_info") {
$sql = "SELECT freelance_path FROM freelance_info WHERE freelance_id = ? AND freelance_path = ?";
} else {
$sql = "SELECT gigs_banner FROM freelance_gig WHERE user_id = ? AND gigs_banner = ?";
}
$stmt = $conn->prepare($sql);
$stmt->bind_param("is", $user_id, $target_file);
$stmt_result = $stmt->store_result();
// Check if file is in database path
if ($stmt_result->num_rows === 0) {
if ($database === "freelance_info") {
$sql = "UPDATE freelance_info SET freelance_path = ? WHERE freelance_id = ?";
} else {
$sql = "UPDATE freelance_gig SET gigs_banner = ? WHERE user_id = ?";
}
$stmt = $conn->prepare($sql);
$stmt->bind_param("si", $target_file, $user_id);
if ($stmt->execute()) {
echo "<script>alert('{$filename} uploaded successfully.')
window.location.href='{$phpFile}.php?upload_status=success';
</script>";
} else {
echo "<script>alert('{$filename} upload failed. User Id don\'t matched')
window.location.href='{$phpFile}.php?upload_status=failed';
</script>";
}
} else {
echo "<script>alert('Sorry, this is your current profile picture.');
window.location.href='{$phpFile}.php?upload_status=already-profile';
</script>";
}
} else if ($imageFileType !== "jpg" && $imageFileType !== "png" && $imageFileType !== "jpeg" && $imageFileType !== "gif") {
$status = 0;
echo "<script>alert('Sorry, only JPG, JPEG, PNG & GIF files are allowed.');
window.location.href='{$phpFile}.php?upload_status=invalid-file';
</script>";
}
if ($status === 0) {
echo "<script>alert('{$filename} has NOT BEEN uploaded due to not complying.')
window.location.href='{$phpFile}.php?upload_status=not-comply';
</script>";
} else if (!move_uploaded_file($chooseImg["tmp_name"], $target_file)) {
echo "<script>alert('{$filename} has NOT BEEN moved to DIR')
window.location.href='{$phpFile}.php?upload_status=not-moved';
</script>";
} else {
if ($database === "freelance_info") {
$sql = "UPDATE freelance_info SET freelance_path = ? WHERE freelance_id = ?;";
} else {
$sql = "UPDATE freelance_gig SET gigs_banner = ? WHERE user_id = ?;";
}
$stmt = $conn->prepare($sql);
$stmt->bind_param("si", $target_file, $user_id);
if ($stmt->execute()) {
echo "<script>alert('{$filename} has been uploaded');
window.location.href='{$phpFile}.php?upload_status=success';
</script>";
return $target_file;
}
echo "<script>alert('{$filename} has NOT BEEN uploaded')
window.location.href='{$phpFile}.php?upload_status=not-uploaded';
</script>";
}
} else {
trigger_error("Connection Failed: " . $conn->connect_error);
}
}
function add_image_gigs($conn, $submitBtn, $chooseImg, $database = "freelance_info", $lastId): string
{
if (isset($conn, $submitBtn) && $conn) {
$target_dir = "./assets/img/";
$filename = htmlspecialchars(basename($chooseImg['name']));
$target_file = $target_dir . $filename;
$imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
$phpFile = $database === "freelance_info" ? "settings" : "mygigs";
$user_id = $_SESSION['user_id'];
$status = 1;
if (file_exists($target_file)) {
if ($database === "freelance_info") {
$sql = "SELECT freelance_path FROM freelance_info WHERE freelance_id = ? AND freelance_path = ?";
} else {
$sql = "SELECT gigs_banner FROM freelance_gig WHERE user_id = ? AND gigs_banner = ?";
}
$stmt = $conn->prepare($sql);
$stmt->bind_param("is", $user_id, $target_file);
$stmt_result = $stmt->store_result();
// Check if file is in database path
if ($stmt_result->num_rows === 0) {
if ($database === "freelance_info") {
$sql = "UPDATE freelance_info SET freelance_path = ? WHERE freelance_id = ?";
} else {
$sql = "UPDATE freelance_gig SET gigs_banner = ? WHERE gigs_id = ?";
}
$stmt = $conn->prepare($sql);
$stmt->bind_param("si", $target_file, $lastId);
if ($stmt->execute()) {
echo "<script>alert('{$filename} uploaded successfully.')
window.location.href='{$phpFile}.php?upload_status=success';
</script>";
} else {
echo "<script>alert('{$filename} upload failed. User Id don\'t matched')
window.location.href='{$phpFile}.php?upload_status=failed';
</script>";
}
} else {
echo "<script>alert('Sorry, this is your current profile picture.');
window.location.href='{$phpFile}.php?upload_status=already-profile';
</script>";
}
} else if ($imageFileType !== "jpg" && $imageFileType !== "png" && $imageFileType !== "jpeg" && $imageFileType !== "gif") {
$status = 0;
echo "<script>alert('Sorry, only JPG, JPEG, PNG & GIF files are allowed.');
window.location.href='{$phpFile}.php?upload_status=invalid-file';
</script>";
}
if ($status === 0) {
echo "<script>alert('{$filename} has NOT BEEN uploaded due to not complying.')
window.location.href='{$phpFile}.php?upload_status=not-comply';
</script>";
} else if (!move_uploaded_file($chooseImg["tmp_name"], $target_file)) {
echo "<script>alert('{$filename} has NOT BEEN moved to DIR')
window.location.href='{$phpFile}.php?upload_status=not-moved';
</script>";
} else {
if ($database === "freelance_info") {
$sql = "UPDATE freelance_info SET freelance_path = ? WHERE freelance_id = ?;";
} else {
$sql = "UPDATE freelance_gig SET gigs_banner = ? WHERE gigs_id = ?;";
}
$stmt = $conn->prepare($sql);
$stmt->bind_param("si", $target_file, $lastId);
if ($stmt->execute()) {
echo "<script>alert('{$filename} has been uploaded');
window.location.href='{$phpFile}.php?upload_status=success';
</script>";
return $target_file;
}
echo "<script>alert('{$filename} has NOT BEEN uploaded')
window.location.href='{$phpFile}.php?upload_status=not-uploaded';
</script>";
}
} else {
trigger_error("Connection Failed: " . $conn->connect_error);
}
}
function getDatabase_info($conn, $userId, $database = "freelance_info", $get_password = FALSE)
{
if ($database === "freelance_info") {
if ($get_password) {
$sql = "SELECT * FROM freelance_info WHERE freelance_id = ?";
} else {
$sql = "SELECT freelance_id, freelance_fName, freelance_lName, freelance_username,
freelance_email, freelance_about, freelance_path FROM freelance_info
WHERE freelance_id = ?";
}
} else {
$sql = "SELECT * FROM freelance_gig WHERE gigs_id = ?";
}
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $userId);
if ($stmt->execute()) {
$result = $stmt->get_result();
return $result->fetch_assoc();
}
trigger_error("Statement did not execute: " . $stmt->error());
}
function getDirWoLast(): string
{
$dir = $_SERVER['PHP_SELF'];
$expDir = explode("/", $dir);
$dirLastPop = array_slice($expDir, 0, -1);
$phpDir = implode("/", $dirLastPop);
return $phpDir;
}