-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofile.php
305 lines (227 loc) · 7.88 KB
/
profile.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
302
303
<?php
ob_start();
error_reporting(E_ALL);
ini_set("display_errors", 1);
/**
* User profile controller
*/
/**
* IMPORTANT: add following things in every controller:
* session_start()
* require_once("rolestarter.php");
* "user" => $_SESSION['user'] needs to be sent to Twig
*/
session_start();
use src\ProjectWhisky\business\ProfileBusiness;
use src\ProjectWhisky\exceptions\WrongDataException;
use src\ProjectWhisky\exceptions\PasswordsDontMatchException;
use src\ProjectWhisky\exceptions\FuckedUpException;
use src\ProjectWhisky\exceptions\EmptyDataException;
use Doctrine\Common\ClassLoader;
require_once("rolestarter.php"); // gives to user role = 0 on first visit of the website: role = 0 - guest
if (isset($_SESSION['user']['id']) && (is_int((int)$_SESSION['user']['id'])))
{
/**
* Dialog variable which stores and shows errors or success messages
*/
if (!isset($_SESSION['dialogBlock']))
{
$_SESSION['dialogBlock'] = "";
}
$userId = $_SESSION['user']['id'];
/**
* Connecting doctrine autoloader
*/
require_once'Doctrine/Common/ClassLoader.php';
$classLoader = new ClassLoader("src");
$classLoader->register();
require_once("lib/Twig/Autoloader.php");
Twig_Autoloader::register();
/**
* Creating new profile object
*/
$profile = new ProfileBusiness();
/**
* Get user data by user id: firstname, lastname, e-mail, user image
*/
$userData = $profile->getUserDataByUserId($userId); // sended to twig on the end
/**
* Perform form workout
*/
if (isset($_POST['userFirstName']))
{
try
{
if((empty($_POST['userFirstName'])) || (empty($_POST['userLastName'])) || empty($_POST['userEmail'])) throw new EmptyDataException;
$firstname = $_POST['userFirstName'];
$lastname = $_POST['userLastName'];
$email = $_POST['userEmail'];
/**
* Perform updating fields
*/
if ($profile->updateUserDataByUserId($userId, $firstname, $lastname, $email) !== true) throw new FuckedUpException();
$_SESSION['dialogBlock'] = "Profile updated";
$_SESSION['reload'] = 1;
// header("Refresh: 0");
header('Location: profile.php?updated=1');
}
catch (EmptyDataException $e)
{
$_SESSION['dialogBlock'] = "We need your firstname, lastname and e-mail";
}
catch (FuckedUpException $e)
{
$_SESSION['dialogBlock'] = "Someting went wrong, restart your computer 3 times.";
}
}
/*
* A defined function which renames image path
* Object $profile required to be given to this function
*/
function updateImagePathInDB($userId, $newImagePath, $profile)
{
return $profile->updateUserImagePath($userId, $newImagePath);
}
/**
* Upload image
*/
if (isset($_FILES['userImage']) && !empty($_FILES['userImage']['name']))
{
$file = $_FILES['userImage'];
/**
* File properties
*/
$file_name = $file['name'];
$file_tmp = $file['tmp_name'];
$file_size = $file['size'];
$file_error = $file['error'];
/**
* Work out the file extension
* If move_uploaded_file gives permission error, 'cd' to 'userimages' folder and give it wright permissions: 'chmod 0777 userimages' (both machines)
*/
$file_ext = explode('.', $file_name);
$file_ext = strtolower(end($file_ext));
/**
* Define allowed file extensions
*/
$allowed = array('png', 'jpg', 'jpeg', 'gif');
if (in_array($file_ext, $allowed))
{
if ($file_error === 0)
{
if ($file_size <= 2097152)
{
$file_name_new = uniqid('', true) . '.' . $file_ext; // uniqid - creates unique id based on current timestamp in microseconds
$file_destination = 'src/ProjectWhisky/presentation/userimages/' . $file_name_new;
if (move_uploaded_file($file_tmp, $file_destination))
{
$newImagePath = $file_name_new;
/**
* Calling defined function and giving parameters. $profile is an object
*/
updateImagePathInDB($userId, $newImagePath, $profile);
if($userData['image_path'] !== "default.jpg")
{
$toRemoveImage = "src/ProjectWhisky/presentation/userimages/" . $userData['image_path'];
unlink($toRemoveImage);
}
}
}
else
{
$_SESSION['dialogBlock'] = "Allowed file size is 2MB";
}
}
}
else
{
$_SESSION['dialogBlock'] = "Only .png, .jpg, .jpeg and .gif files are allowed.";
}
}
/**
* Remove image path (images itself remain in userimages folder) unlink('test.html');
*/
if (isset($_POST['removeProfileImageBtn']))
{
/**
* if user removes his picture, a default.jpg will be added to his profile
*/
$newImagePath = "default.jpg";
/**
* Change image name in DB
*/
if (updateImagePathInDB($userId, $newImagePath, $profile))
{
$toRemoveImage = "src/ProjectWhisky/presentation/userimages/" . $userData['image_path'];
unlink($toRemoveImage);
$_SESSION['dialogBlock'] = "Your profile image has been removed";
}
else
{
$_SESSION['dialogBlock'] = "Something went wrong";
}
header('Location: profile.php?updated=1');
}
/**
* Perform password change only if user presses "Change password" button on profile page
*/
if (isset($_POST['userOldPassword']))
{
$oldPassword = $_POST['userOldPassword'];
$newPassword = $_POST['userNewPassword'];
$newPasswordRepeat = $_POST['userNewPasswordRepeat'];
try
{
$profile->checkOldPassword($userId, $oldPassword);
/**
* Check if passwords are matching
*/
if ($_POST['userNewPassword'] !== $_POST['userNewPasswordRepeat']) throw new PasswordsDontMatchException();
/**
* Update old password
*/
if ($profile->updateNewPassword($userId, $newPassword) !== true) throw new FuckedUpException();
echo "password changed";
}
catch (WrongDataException $e)
{
$_SESSION['dialogBlock'] = "Wrong old password";
}
catch (PasswordsDontMatchException $e)
{
$_SESSION['dialogBlock'] = "New passwords don't match";
}
catch (FuckedUpException $e)
{
$_SESSION['dialogBlock'] = "Someting went wrong, restart your computer 3 times.";
}
}
if (!isset($file_name))
{
$file_name = "Change profile image";
}
/**
* Load Twig template
*/
$loader = new Twig_Loader_Filesystem("src/ProjectWhisky/presentation");
$twig = new Twig_Environment($loader);
$view = $twig->render("profile.twig", array("user" => $_SESSION['user'], "userData" => $userData, "dialogBlock" => $_SESSION['dialogBlock'], 'imageName' => $file_name));
print($view);
/**
* Handling messages removal and appearance
*/
if (isset($_GET['updated']) && (empty($_SESSION['dialogBlock'])))
{
header('Location: profile.php');
}
if(isset($_GET['updated']) && ($_GET['updated'] == 1))
{
$_SESSION['dialogBlock'] = "";
unset($_SESSION['reload']);
}
}
else
{
header("Location: index.php");
}
ob_flush();