1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class AvatarModel |
4
|
|
|
{ |
5
|
|
|
/** |
6
|
|
|
* Gets a gravatar image link from given email address |
7
|
|
|
* |
8
|
|
|
* Gravatar is the #1 (free) provider for email address based global avatar hosting. |
9
|
|
|
* The URL (or image) returns always a .jpg file ! For deeper info on the different parameter possibilities: |
10
|
|
|
* @see http://gravatar.com/site/implement/images/ |
11
|
|
|
* @source http://gravatar.com/site/implement/images/php/ |
12
|
|
|
* |
13
|
|
|
* This method will return something like http://www.gravatar.com/avatar/79e2e5b48aec07710c08d50?s=80&d=mm&r=g |
14
|
|
|
* Note: the url does NOT have something like ".jpg" ! It works without. |
15
|
|
|
* |
16
|
|
|
* Set the configs inside the application/config/ files. |
17
|
|
|
* |
18
|
|
|
* @param string $email The email address |
19
|
|
|
* @return string |
20
|
|
|
*/ |
21
|
|
|
public static function getGravatarLinkByEmail($email) |
22
|
|
|
{ |
23
|
|
|
return 'http://www.gravatar.com/avatar/' . |
24
|
|
|
md5(strtolower(trim($email))) . |
25
|
|
|
'?s=' . Config::get('AVATAR_SIZE') . '&d=' . Config::get('GRAVATAR_DEFAULT_IMAGESET') . '&r=' . Config::get('GRAVATAR_RATING'); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Gets the user's avatar file path |
30
|
|
|
* @param int $user_has_avatar Marker from database |
31
|
|
|
* @param int $user_id User's id |
32
|
|
|
* @return string Avatar file path |
33
|
|
|
*/ |
34
|
|
|
public static function getPublicAvatarFilePathOfUser($user_has_avatar, $user_id) |
35
|
|
|
{ |
36
|
|
|
if ($user_has_avatar) { |
37
|
|
|
return Config::get('URL') . Config::get('PATH_AVATARS_PUBLIC') . $user_id . '.jpg'; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return Config::get('URL') . Config::get('PATH_AVATARS_PUBLIC') . Config::get('AVATAR_DEFAULT_IMAGE'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Gets the user's avatar file path |
45
|
|
|
* @param $user_id integer The user's id |
46
|
|
|
* @return string avatar picture path |
47
|
|
|
*/ |
48
|
|
|
public static function getPublicUserAvatarFilePathByUserId($user_id) |
49
|
|
|
{ |
50
|
|
|
$database = DatabaseFactory::getFactory()->getConnection(); |
51
|
|
|
|
52
|
|
|
$query = $database->prepare("SELECT user_has_avatar FROM users WHERE user_id = :user_id LIMIT 1"); |
53
|
|
|
$query->execute(array(':user_id' => $user_id)); |
54
|
|
|
|
55
|
|
|
if ($query->fetch()->user_has_avatar) { |
56
|
|
|
return Config::get('URL') . Config::get('PATH_AVATARS_PUBLIC') . $user_id . '.jpg'; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return Config::get('URL') . Config::get('PATH_AVATARS_PUBLIC') . Config::get('AVATAR_DEFAULT_IMAGE'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Create an avatar picture (and checks all necessary things too) |
64
|
|
|
* TODO decouple |
65
|
|
|
* TODO total rebuild |
66
|
|
|
*/ |
67
|
|
|
public static function createAvatar() |
68
|
|
|
{ |
69
|
|
|
// check avatar folder writing rights, check if upload fits all rules |
70
|
|
|
if (self::isAvatarFolderWritable() AND self::validateImageFile()) { |
71
|
|
|
|
72
|
|
|
// create a jpg file in the avatar folder, write marker to database |
73
|
|
|
$target_file_path = Config::get('PATH_AVATARS') . Session::get('user_id'); |
74
|
|
|
self::resizeAvatarImage($_FILES['avatar_file']['tmp_name'], $target_file_path, Config::get('AVATAR_SIZE'), Config::get('AVATAR_SIZE')); |
75
|
|
|
self::writeAvatarToDatabase(Session::get('user_id')); |
76
|
|
|
Session::set('user_avatar_file', self::getPublicUserAvatarFilePathByUserId(Session::get('user_id'))); |
77
|
|
|
Session::add('feedback_positive', Text::get('FEEDBACK_AVATAR_UPLOAD_SUCCESSFUL')); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Checks if the avatar folder exists and is writable |
83
|
|
|
* |
84
|
|
|
* @return bool success status |
85
|
|
|
*/ |
86
|
|
|
public static function isAvatarFolderWritable() |
87
|
|
|
{ |
88
|
|
|
if (is_dir(Config::get('PATH_AVATARS')) AND is_writable(Config::get('PATH_AVATARS'))) { |
89
|
|
|
return true; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
Session::add('feedback_negative', Text::get('FEEDBACK_AVATAR_FOLDER_DOES_NOT_EXIST_OR_NOT_WRITABLE')); |
93
|
|
|
return false; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Validates the image |
98
|
|
|
* Only accepts gif, jpg, png types |
99
|
|
|
* @see http://php.net/manual/en/function.image-type-to-mime-type.php |
100
|
|
|
* |
101
|
|
|
* @return bool |
102
|
|
|
*/ |
103
|
|
|
public static function validateImageFile() |
104
|
|
|
{ |
105
|
|
|
if (!isset($_FILES['avatar_file'])) { |
106
|
|
|
Session::add('feedback_negative', Text::get('FEEDBACK_AVATAR_IMAGE_UPLOAD_FAILED')); |
107
|
|
|
return false; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
// if input file too big (>5MB) |
111
|
|
|
if ($_FILES['avatar_file']['size'] > 5000000) { |
112
|
|
|
Session::add('feedback_negative', Text::get('FEEDBACK_AVATAR_UPLOAD_TOO_BIG')); |
113
|
|
|
return false; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
// get the image width, height and mime type |
117
|
|
|
$image_proportions = getimagesize($_FILES['avatar_file']['tmp_name']); |
118
|
|
|
|
119
|
|
|
// if input file too small, [0] is the width, [1] is the height |
120
|
|
|
if ($image_proportions[0] < Config::get('AVATAR_SIZE') OR $image_proportions[1] < Config::get('AVATAR_SIZE')) { |
121
|
|
|
Session::add('feedback_negative', Text::get('FEEDBACK_AVATAR_UPLOAD_TOO_SMALL')); |
122
|
|
|
return false; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
// if file type is not jpg, gif or png |
126
|
|
|
if (!in_array($image_proportions['mime'], array('image/jpeg', 'image/gif', 'image/png'))) { |
127
|
|
|
Session::add('feedback_negative', Text::get('FEEDBACK_AVATAR_UPLOAD_WRONG_TYPE')); |
128
|
|
|
return false; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return true; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Writes marker to database, saying user has an avatar now |
136
|
|
|
* |
137
|
|
|
* @param $user_id |
138
|
|
|
*/ |
139
|
|
|
public static function writeAvatarToDatabase($user_id) |
140
|
|
|
{ |
141
|
|
|
$database = DatabaseFactory::getFactory()->getConnection(); |
142
|
|
|
|
143
|
|
|
$query = $database->prepare("UPDATE users SET user_has_avatar = TRUE WHERE user_id = :user_id LIMIT 1"); |
144
|
|
|
$query->execute(array(':user_id' => $user_id)); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Resize avatar image (while keeping aspect ratio and cropping it off in a clean way). |
149
|
|
|
* Only works with gif, jpg and png file types. If you want to change this also have a look into |
150
|
|
|
* method validateImageFile() inside this model. |
151
|
|
|
* |
152
|
|
|
* TROUBLESHOOTING: You don't see the new image ? Press F5 or CTRL-F5 to refresh browser cache. |
153
|
|
|
* |
154
|
|
|
* @param string $source_image The location to the original raw image |
155
|
|
|
* @param string $destination The location to save the new image |
156
|
|
|
* @param int $final_width The desired width of the new image |
157
|
|
|
* @param int $final_height The desired height of the new image |
158
|
|
|
* |
159
|
|
|
* @return bool success state |
160
|
|
|
*/ |
161
|
|
|
public static function resizeAvatarImage($source_image, $destination, $final_width = 44, $final_height = 44) |
162
|
|
|
{ |
163
|
|
|
$imageData = getimagesize($source_image); |
164
|
|
|
$width = $imageData[0]; |
165
|
|
|
$height = $imageData[1]; |
166
|
|
|
$mimeType = $imageData['mime']; |
167
|
|
|
|
168
|
|
|
if (!$width || !$height) { |
169
|
|
|
return false; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
switch ($mimeType) { |
173
|
|
|
case 'image/jpeg': $myImage = imagecreatefromjpeg($source_image); break; |
174
|
|
|
case 'image/png': $myImage = imagecreatefrompng($source_image); break; |
175
|
|
|
case 'image/gif': $myImage = imagecreatefromgif($source_image); break; |
176
|
|
|
default: return false; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
// calculating the part of the image to use for thumbnail |
180
|
|
|
if ($width > $height) { |
181
|
|
|
$verticalCoordinateOfSource = 0; |
182
|
|
|
$horizontalCoordinateOfSource = ($width - $height) / 2; |
183
|
|
|
$smallestSide = $height; |
184
|
|
|
} else { |
185
|
|
|
$horizontalCoordinateOfSource = 0; |
186
|
|
|
$verticalCoordinateOfSource = ($height - $width) / 2; |
187
|
|
|
$smallestSide = $width; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
// copying the part into thumbnail, maybe edit this for square avatars |
191
|
|
|
$thumb = imagecreatetruecolor($final_width, $final_height); |
192
|
|
|
imagecopyresampled($thumb, $myImage, 0, 0, $horizontalCoordinateOfSource, $verticalCoordinateOfSource, $final_width, $final_height, $smallestSide, $smallestSide); |
193
|
|
|
|
194
|
|
|
// add '.jpg' to file path, save it as a .jpg file with our $destination_filename parameter |
195
|
|
|
imagejpeg($thumb, $destination . '.jpg', Config::get('AVATAR_JPEG_QUALITY')); |
196
|
|
|
imagedestroy($thumb); |
197
|
|
|
|
198
|
|
|
if (file_exists($destination)) { |
199
|
|
|
return true; |
200
|
|
|
} |
201
|
|
|
return false; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Delete a user's avatar |
206
|
|
|
* |
207
|
|
|
* @param int $userId |
208
|
|
|
* @return bool success |
209
|
|
|
*/ |
210
|
|
|
public static function deleteAvatar($userId) |
211
|
|
|
{ |
212
|
|
|
if (!ctype_digit($userId)) { |
213
|
|
|
Session::add("feedback_negative", Text::get("FEEDBACK_AVATAR_IMAGE_DELETE_FAILED")); |
214
|
|
|
return false; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
// try to delete image, but still go on regardless of file deletion result |
218
|
|
|
self::deleteAvatarImageFile($userId); |
219
|
|
|
|
220
|
|
|
$database = DatabaseFactory::getFactory()->getConnection(); |
221
|
|
|
|
222
|
|
|
$sth = $database->prepare("UPDATE users SET user_has_avatar = 0 WHERE user_id = :user_id LIMIT 1"); |
223
|
|
|
$sth->bindValue(":user_id", (int)$userId, PDO::PARAM_INT); |
224
|
|
|
$sth->execute(); |
225
|
|
|
|
226
|
|
|
if ($sth->rowCount() == 1) { |
227
|
|
|
Session::set('user_avatar_file', self::getPublicUserAvatarFilePathByUserId($userId)); |
228
|
|
|
Session::add("feedback_positive", Text::get("FEEDBACK_AVATAR_IMAGE_DELETE_SUCCESSFUL")); |
229
|
|
|
return true; |
230
|
|
|
} else { |
231
|
|
|
Session::add("feedback_negative", Text::get("FEEDBACK_AVATAR_IMAGE_DELETE_FAILED")); |
232
|
|
|
return false; |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Removes the avatar image file from the filesystem |
238
|
|
|
* |
239
|
|
|
* @param integer $userId |
240
|
|
|
* @return bool |
241
|
|
|
*/ |
242
|
|
|
public static function deleteAvatarImageFile($userId) |
243
|
|
|
{ |
244
|
|
|
// Check if file exists |
245
|
|
|
if (!file_exists(Config::get('PATH_AVATARS') . $userId . ".jpg")) { |
246
|
|
|
Session::add("feedback_negative", Text::get("FEEDBACK_AVATAR_IMAGE_DELETE_NO_FILE")); |
247
|
|
|
return false; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
// Delete avatar file |
251
|
|
|
if (!unlink(Config::get('PATH_AVATARS') . $userId . ".jpg")) { |
252
|
|
|
Session::add("feedback_negative", Text::get("FEEDBACK_AVATAR_IMAGE_DELETE_FAILED")); |
253
|
|
|
return false; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
return true; |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|