move code up one directory
[atutor.git] / mods / _standard / profile_pictures / save_profile_picture.php
1 <?php
2 /***********************************************************************/
3 /* ATutor                                                                                                                          */
4 /***********************************************************************/
5 /* Copyright (c) 2002-2010                                             */
6 /* Inclusive Design Institute                                          */
7 /* http://atutor.ca                                                                                                        */
8 /*                                                                                                                                         */
9 /* This program is free software. You can redistribute it and/or           */
10 /* modify it under the terms of the GNU General Public License             */
11 /* as published by the Free Software Foundation.                                           */
12 /***********************************************************************/
13
14 if (!isset($member_id) || $member_id == 0) $member_id = $_SESSION['member_id'];
15
16 function resize_image($src, $dest, $src_h, $src_w, $dest_h, $dest_w, $type, $src_x=0, $src_y=0) {
17         $thumbnail_img = imagecreatetruecolor($dest_w, $dest_h);
18
19         if ($type == 'gif') {
20                 $source = imagecreatefromgif($src);
21         } else if ($type == 'jpg') {
22                 $source = imagecreatefromjpeg($src);
23         } else {
24                 $source = imagecreatefrompng($src);
25         }
26         
27         if ($src_x > 0 || $src_y > 0){
28                 imagecopyresized($thumbnail_img, $source, 0, 0, $src_x, $src_y, $dest_w, $dest_h, $src_w, $src_h);
29         } else {
30                 imagecopyresampled($thumbnail_img, $source, $src_x, $src_y, 0, 0, $dest_w, $dest_h, $src_w, $src_h);
31         }
32
33         if ($type == 'gif') {
34                 imagegif($thumbnail_img, $dest);
35         } else if ($type == 'jpg') {
36                 imagejpeg($thumbnail_img, $dest, 75);
37         } else {
38                 imagepng($thumbnail_img, $dest, 7);
39         }
40 }
41
42 // check if GD is installed
43 if (!extension_loaded('gd')) {
44         require(AT_INCLUDE_PATH.'header.inc.php');
45         $msg->printInfos('FEATURE_NOT_AVAILABLE');
46         require(AT_INCLUDE_PATH.'footer.inc.php');
47         exit;
48 }
49
50 // check if folder exists, if not, create it
51 if (!is_dir(AT_CONTENT_DIR.'/profile_pictures/profile')) {
52         mkdir(AT_CONTENT_DIR.'/profile_pictures/profile');
53 }
54
55 // check if this is a request from the photo album
56 $aid = intval($_GET['aid']);
57 $pid = intval($_GET['pid']);
58 if ($pid>0 && $aid>0){
59         $photo_set_profile = true;
60 } else {
61         $photo_set_profile = false;
62 }
63
64 $gd_info = gd_info();
65 $supported_images = array();
66 if ($gd_info['GIF Create Support']) {
67         $supported_images[] = 'gif';
68 }
69 if ($gd_info['JPG Support'] || $gd_info['JPEG Support']) {
70         $supported_images[] = 'jpg';
71 }
72 if ($gd_info['PNG Support']) {
73         $supported_images[] = 'png';
74 }
75
76 if (!$supported_images) {
77         require(AT_INCLUDE_PATH.'header.inc.php');
78         $msg->printInfos('FEATURE_NOT_AVAILABLE');
79         require(AT_INCLUDE_PATH.'footer.inc.php');
80         exit;
81 }
82
83 if (isset($_POST['cancel'])) {
84         $msg->addFeedback('CANCELLED');
85         header('Location: '.$_SERVER['PHP_SELF'].'?member_id='.$member_id);
86         exit;
87 } else if (isset($_POST['submit']) || $photo_set_profile) {
88         if (isset($_POST['delete']) && !$_FILES['file']['size']) {
89                 profile_image_delete($member_id);
90
91                 $msg->addFeedback('PROFILE_UPDATED');
92
93                 header('Location: '.$_SERVER['PHP_SELF'].'?member_id='.$member_id);
94                 exit;
95         } else if ($_FILES['file']['error'] == UPLOAD_ERR_FORM_SIZE) {
96                 $msg->addError(array('FILE_MAX_SIZE', $_config['prof_pic_max_file_size'] . ' ' . _AT('bytes')));
97                 header('Location: '.$_SERVER['PHP_SELF'].'?member_id='.$member_id);
98                 exit;
99         } else if (!$_FILES['file']['size'] && !$photo_set_profile) {
100                 header('Location: '.$_SERVER['PHP_SELF'].'?member_id='.$member_id);
101                 exit;
102         }
103
104         // if this is a picture from the photo album
105         if ($photo_set_profile) {
106                 include (AT_PA_INCLUDE.'lib.inc.php');
107                 include (AT_PA_INCLUDE.'classes/PhotoAlbum.class.php');
108         //run a check to see if any personal album exists, if not, create one.
109         $sql = 'SELECT * FROM '.TABLE_PREFIX.'pa_albums WHERE member_id='.$_SESSION['member_id'].' AND type_id='.AT_PA_TYPE_PERSONAL;
110         $result = mysql_query($sql, $db);
111         if ($result){
112             //precondition: Profile Album always exists.
113                 $row = mysql_fetch_assoc($result);      //album info.
114                 $profile_aid = $row['id'];  //current profile album id
115         }
116         $pa_profile = new PhotoAlbum($profile_aid);
117                 
118                 // album id of the GET requests (via set profile picture link)
119                 $pa = new PhotoAlbum($aid);
120                 $album_info = $pa->getAlbumInfo();
121                 $photo_info = $pa->getPhotoInfo($pid);
122
123                 //Validate users, using permission and course album control.
124                 $visible_albums = $pa->getAlbums($_SESSION['member_id'], $photo_info['type_id']);
125                 if(!isset($visible_albums[$aid]) && $album_info['permission']==AT_PA_PRIVATE_ALBUM){
126                         //TODO msg;
127                         $msg->addError("ACCESS_DENIED");
128                         header('location: index.php');
129                         exit;
130                 }
131         
132         // get the current photo info, and paths
133                 $album_file_path = getAlbumFilePath($album_info['id'], $album_info['created_date']);
134                 $album_file_path_tn = $album_file_path.'_tn'.DIRECTORY_SEPARATOR;
135         $album_file_path .= DIRECTORY_SEPARATOR;
136                 $photo_file_path = getPhotoFilePath($photo_info['id'], $photo_info['name'], $photo_info['created_date']);
137                 $photo_location = AT_PA_CONTENT_DIR . $album_file_path . $photo_file_path;
138                 $photo_tn_location = AT_PA_CONTENT_DIR . $album_file_path_tn . $photo_file_path;
139                 
140                 if ($aid!=$profile_aid){
141                     // now, get the new photo info, and path
142                     $pa_profile->addPhoto($photo_info['name'], $photo_info['description'], $_SESSION['member_id']);
143                     $album_info_new = $pa_profile->getAlbumInfo();
144                     $album_file_path_new = getAlbumFilePath($album_info_new['id'], $album_info_new['created_date']);
145                     $album_file_path_tn_new = $album_file_path_new.'_tn'.DIRECTORY_SEPARATOR;
146                 $album_file_path_new .= DIRECTORY_SEPARATOR;            
147                     $added_photo_id = mysql_insert_id();                
148                     $photo_info_new = $pa->getPhotoInfo($added_photo_id);
149                     $photo_file_path_new = getPhotoFilePath($added_photo_id, $photo_info_new['name'], $photo_info_new['created_date']);
150                     $photo_location_new = AT_PA_CONTENT_DIR . $album_file_path_new . $photo_file_path_new;
151                     $photo_tn_location_new = AT_PA_CONTENT_DIR . $album_file_path_tn_new . $photo_file_path_new;
152                 
153                     // if directory does not exist, create it. 
154                     if (!is_dir(AT_PA_CONTENT_DIR.$album_file_path_new)){
155                         mkdir(AT_PA_CONTENT_DIR.$album_file_path_new);          
156                 }
157                 if (!is_dir(AT_PA_CONTENT_DIR.$album_file_path_tn_new)){
158                         mkdir(AT_PA_CONTENT_DIR.$album_file_path_tn_new);
159                 }
160                 
161                 // copy both original and thumbnail over to the profile album
162                     copy($photo_location, $photo_location_new);
163                     copy($photo_tn_location, $photo_tn_location_new);
164                 }
165             
166                 $filename = $photo_info['name'];
167                 $image_attributes = getimagesize($photo_location);
168         } else {
169                 // check if this is a supported file type
170                 $filename   = $stripslashes($_FILES['file']['name']);
171                 $image_attributes = getimagesize($_FILES['file']['tmp_name']);
172         }
173         $path_parts = pathinfo($filename);
174         $extension  = strtolower($path_parts['extension']);     
175
176         if ($extension == 'jpeg') {
177                 $extension = 'jpg';
178         }
179
180         if (!in_array($extension, $supported_images)) {
181                 $msg->addError(array('FILE_ILLEGAL', $extension));
182                 header('Location: '.$_SERVER['PHP_SELF'].'?member_id='.$member_id);
183                 exit;
184         } else if ($image_attributes[2] > IMAGETYPE_PNG) {
185                 $msg->addError(array('FILE_ILLEGAL', $extension));
186                 header('Location: '.$_SERVER['PHP_SELF'].'?member_id='.$member_id);
187                 exit;
188         }
189
190         // make sure under max file size
191         if ($_FILES['file']['size'] > $_config['prof_pic_max_file_size']) {
192                 $msg->addError('FILE_MAX_SIZE');
193                 header('Location: '.$_SERVER['PHP_SELF'].'?member_id='.$member_id);
194                 exit;
195         }
196
197         // delete the old images (if any)
198         profile_image_delete($member_id);
199
200         $new_filename   = $member_id . '.' . $extension;
201         $original_img  = AT_CONTENT_DIR.'profile_pictures/originals/'. $new_filename;
202         $profile_img   = AT_CONTENT_DIR.'profile_pictures/profile/'. $new_filename;
203         $thumbnail_img = AT_CONTENT_DIR.'profile_pictures/thumbs/'. $new_filename;
204
205         // save original
206         if ($photo_set_profile){
207                 copy($photo_location, $original_img);           
208         } else {
209                 if (!move_uploaded_file($_FILES['file']['tmp_name'], $original_img)) {
210                         $msg->addError('CANNOT_OVERWRITE_FILE');
211                         header('Location: '.$_SERVER['PHP_SELF'].'?member_id='.$member_id);
212                         exit;
213                 }               
214         }
215
216         // resize the original and save it at $thumbnail_file
217         $width  = $image_attributes[0];
218         $height = $image_attributes[1];
219
220         $thumbnail_fixed_height = 60; 
221         $thumbnail_fixed_width = 60; 
222
223         if ($width > $height && $height > $thumbnail_fixed_height) {
224                 $thumbnail_height= $thumbnail_fixed_height;
225                 $thumbnail_width = intval($thumbnail_fixed_height * $width / $height);
226                 resize_image($original_img, $thumbnail_img, $height, $width, $thumbnail_height, $thumbnail_width, $extension);
227                 //cropping
228                 resize_image($thumbnail_img, $thumbnail_img, $thumbnail_fixed_height, $thumbnail_fixed_width, $thumbnail_fixed_height, $thumbnail_fixed_width, $extension, ($thumbnail_width-$thumbnail_fixed_width)/2);
229         } else if ($width <= $height && $width>$thumbnail_fixed_width) {
230                 $thumbnail_height = intval($thumbnail_fixed_width * $height / $width);
231                 $thumbnail_width  = $thumbnail_fixed_width;
232                 resize_image($original_img, $thumbnail_img, $height, $width, $thumbnail_height, $thumbnail_width, $extension);
233                 //cropping
234                 resize_image($thumbnail_img, $thumbnail_img, $thumbnail_fixed_height, $thumbnail_fixed_width, $thumbnail_fixed_height, $thumbnail_fixed_width, $extension, 0, ($thumbnail_height-$thumbnail_fixed_height)/2);
235         } else {
236                 // no resizing, just copy the image.
237                 // it's too small to resize.
238                 copy($original_img, $thumbnail_img);
239         }
240
241         // resize the original and save it to profile
242         $profile_fixed_height = 320;
243         $profile_fixed_width = 240;
244         if ($width > $height && $height>$profile_fixed_height) {
245                 $profile_width = intval($profile_fixed_height * $width / $height);
246                 $profile_height  = $profile_fixed_height;
247                 resize_image($original_img, $profile_img, $height, $width, $profile_height, $profile_width, $extension);
248                 //cropping
249                 resize_image($profile_img, $profile_img, $profile_fixed_height, $profile_fixed_width, $profile_fixed_height, $profile_fixed_width, $extension, ($profile_width-$profile_fixed_width)/2);
250         } else if ($width <= $height && $width > $profile_fixed_width) {
251                 $profile_width = $profile_fixed_width;
252                 $profile_height = intval($profile_fixed_width * $height / $width);
253                 resize_image($original_img, $profile_img, $height, $width, $profile_height, $profile_width, $extension);
254                 //cropping
255                 resize_image($profile_img, $profile_img, $profile_fixed_height, $profile_fixed_width, $profile_fixed_height, $profile_fixed_width, $extension, 0, ($profile_height-$profile_fixed_height)/2);
256         } else {
257                 // no resizing, just copy the image.
258                 // it's too small to resize.
259                 copy($original_img, $profile_img);
260         }
261
262         $msg->addFeedback('PROFILE_UPDATED');
263
264         header('Location: '.$_SERVER['PHP_SELF'].'?member_id='.$member_id);
265         exit;
266 }
267 ?>