]> git.mxchange.org Git - friendica.git/blob - mod/profile_photo.php
Merge remote branch 'friendica/master' into bid-prov
[friendica.git] / mod / profile_photo.php
1 <?php
2
3 require_once("Photo.php");
4
5 function profile_photo_init(&$a) {
6
7         if(! local_user()) {
8                 return;
9         }
10
11         profile_load($a,$a->user['nickname']);
12
13 }
14
15
16 function profile_photo_post(&$a) {
17
18         if(! local_user()) {
19                 notice ( t('Permission denied.') . EOL );
20                 return;
21         }
22         
23         check_form_security_token_redirectOnErr('/profile_photo', 'profile_photo');
24         
25         if((x($_POST,'cropfinal')) && ($_POST['cropfinal'] == 1)) {
26
27                 // phase 2 - we have finished cropping
28
29                 if($a->argc != 2) {
30                         notice( t('Image uploaded but image cropping failed.') . EOL );
31                         return;
32                 }
33
34                 $image_id = $a->argv[1];
35
36                 if(substr($image_id,-2,1) == '-') {
37                         $scale = substr($image_id,-1,1);
38                         $image_id = substr($image_id,0,-2);
39                 }
40                         
41
42                 $srcX = $_POST['xstart'];
43                 $srcY = $_POST['ystart'];
44                 $srcW = $_POST['xfinal'] - $srcX;
45                 $srcH = $_POST['yfinal'] - $srcY;
46
47                 $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d AND `scale` = %d LIMIT 1",
48                         dbesc($image_id),
49                         dbesc(local_user()),
50                         intval($scale));
51
52                 if(count($r)) {
53
54                         $base_image = $r[0];
55
56                         $im = new Photo($base_image['data'], $base_image['type']);
57                         if($im->is_valid()) {
58                                 $im->cropImage(175,$srcX,$srcY,$srcW,$srcH);
59
60                                 $r = $im->store(local_user(), 0, $base_image['resource-id'],$base_image['filename'], t('Profile Photos'), 4, 1);
61
62                                 if($r === false)
63                                         notice ( sprintf(t('Image size reduction [%s] failed.'),"175") . EOL );
64
65                                 $im->scaleImage(80);
66
67                                 $r = $im->store(local_user(), 0, $base_image['resource-id'],$base_image['filename'], t('Profile Photos'), 5, 1);
68                         
69                                 if($r === false)
70                                         notice( sprintf(t('Image size reduction [%s] failed.'),"80") . EOL );
71
72                                 $im->scaleImage(48);
73
74                                 $r = $im->store(local_user(), 0, $base_image['resource-id'],$base_image['filename'], t('Profile Photos'), 6, 1);
75                         
76                                 if($r === false)
77                                         notice( sprintf(t('Image size reduction [%s] failed.'),"48") . EOL );
78
79                                 // Unset the profile photo flag from any other photos I own
80
81                                 $r = q("UPDATE `photo` SET `profile` = 0 WHERE `profile` = 1 AND `resource-id` != '%s' AND `uid` = %d",
82                                         dbesc($base_image['resource-id']),
83                                         intval(local_user())
84                                 );
85
86                                 $r = q("UPDATE `contact` SET `avatar-date` = '%s' WHERE `self` = 1 AND `uid` = %d LIMIT 1",
87                                         dbesc(datetime_convert()),
88                                         intval(local_user())
89                                 );
90
91                                 info( t('Shift-reload the page or clear browser cache if the new photo does not display immediately.') . EOL);
92                                 // Update global directory in background
93                                 $url = $a->get_baseurl() . '/profile/' . $a->user['nickname'];
94                                 if($url && strlen(get_config('system','directory_submit_url')))
95                                         proc_run('php',"include/directory.php","$url");
96
97                                 require_once('include/profile_update.php');
98                                 profile_change();
99                         }
100                         else
101                                 notice( t('Unable to process image') . EOL);
102                 }
103
104                 goaway($a->get_baseurl() . '/profiles');
105                 return; // NOTREACHED
106         }
107
108         $src      = $_FILES['userfile']['tmp_name'];
109         $filename = basename($_FILES['userfile']['name']);
110         $filesize = intval($_FILES['userfile']['size']);
111         $filetype = $_FILES['userfile']['type'];
112     if ($filetype=="") $filetype=guess_image_type($filename);
113     
114         $maximagesize = get_config('system','maximagesize');
115
116         if(($maximagesize) && ($filesize > $maximagesize)) {
117                 notice( sprintf(t('Image exceeds size limit of %d'), $maximagesize) . EOL);
118                 @unlink($src);
119                 return;
120         }
121
122         $imagedata = @file_get_contents($src);
123         $ph = new Photo($imagedata, $filetype);
124
125         if(! $ph->is_valid()) {
126                 notice( t('Unable to process image.') . EOL );
127                 @unlink($src);
128                 return;
129         }
130
131         $ph->orient($src);
132         @unlink($src);
133         return profile_photo_crop_ui_head($a, $ph);
134         
135 }
136
137
138 if(! function_exists('profile_photo_content')) {
139 function profile_photo_content(&$a) {
140
141         if(! local_user()) {
142                 notice( t('Permission denied.') . EOL );
143                 return;
144         }
145         
146         $newuser = false;
147
148         if($a->argc == 2 && $a->argv[1] === 'new')
149                 $newuser = true;
150
151         if( $a->argv[1]=='use'){
152                 if ($a->argc<3){
153                         notice( t('Permission denied.') . EOL );
154                         return;
155                 };
156                 
157 //              check_form_security_token_redirectOnErr('/profile_photo', 'profile_photo');
158         
159                 $resource_id = $a->argv[2];
160                 //die(":".local_user());
161                 $r=q("SELECT * FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' ORDER BY `scale` ASC",
162                         intval(local_user()),
163                         dbesc($resource_id)
164                         );
165                 if (!count($r)){
166                         notice( t('Permission denied.') . EOL );
167                         return;
168                 }
169                 $havescale = false;
170                 foreach($r as $rr) {
171                         if($rr['scale'] == 5)
172                                 $havescale = true;
173                 }
174
175                 // set an already uloaded photo as profile photo
176                 // if photo is in 'Profile Photos', change it in db
177                 if (($r[0]['album']== t('Profile Photos')) && ($havescale)){
178                         $r=q("UPDATE `photo` SET `profile`=0 WHERE `profile`=1 AND `uid`=%d",
179                                 intval(local_user()));
180                         
181                         $r=q("UPDATE `photo` SET `profile`=1 WHERE `uid` = %d AND `resource-id` = '%s'",
182                                 intval(local_user()),
183                                 dbesc($resource_id)
184                                 );
185                         
186                         $r = q("UPDATE `contact` SET `avatar-date` = '%s' WHERE `self` = 1 AND `uid` = %d LIMIT 1",
187                                 dbesc(datetime_convert()),
188                                 intval(local_user())
189                         );
190                         
191                         // Update global directory in background
192                         $url = $_SESSION['my_url'];
193                         if($url && strlen(get_config('system','directory_submit_url')))
194                                 proc_run('php',"include/directory.php","$url");
195                         
196                         goaway($a->get_baseurl() . '/profiles');
197                         return; // NOTREACHED
198                 }
199                 $ph = new Photo($r[0]['data'], $r[0]['type']);
200                 profile_photo_crop_ui_head($a, $ph);
201                 // go ahead as we have jus uploaded a new photo to crop
202         }
203
204         if(! x($a->config,'imagecrop')) {
205         
206                 $tpl = get_markup_template('profile_photo.tpl');
207
208                 $o .= replace_macros($tpl,array(
209                         '$user' => $a->user['nickname'],
210                         '$lbl_upfile' => t('Upload File:'),
211                         '$title' => t('Upload Profile Photo'),
212                         '$submit' => t('Upload'),
213                         '$form_security_token' => get_form_security_token("profile_photo"),
214                         '$select' => sprintf('%s %s', t('or'), ($newuser) ? '<a href="' . $a->get_baseurl() . '">' . t('skip this step') . '</a>' : '<a href="'. $a->get_baseurl() . '/photos/' . $a->user['nickname'] . '">' . t('select a photo from your photo albums') . '</a>')
215                 ));
216
217                 return $o;
218         }
219         else {
220                 $filename = $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'] . '.'.$a->config['imagecrop_ext'];
221                 $resolution = $a->config['imagecrop_resolution'];
222                 $tpl = get_markup_template("cropbody.tpl");
223                 $o .= replace_macros($tpl,array(
224                         '$filename' => $filename,
225                         '$resource' => $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'],
226                         '$image_url' => $a->get_baseurl() . '/photo/' . $filename,
227                         '$title' => t('Crop Image'),
228                         '$desc' => t('Please adjust the image cropping for optimum viewing.'),
229                         '$form_security_token' => get_form_security_token("profile_photo"),
230                         '$done' => t('Done Editing')
231                 ));
232                 return $o;
233         }
234
235         return; // NOTREACHED
236 }}
237
238
239 if(! function_exists('_crop_ui_head')) {
240 function profile_photo_crop_ui_head(&$a, $ph){
241         $max_length = get_config('system','max_image_length');
242         if(! $max_length)
243                 $max_length = MAX_IMAGE_LENGTH;
244         if($max_length > 0)
245                 $ph->scaleImage($max_length);
246
247         $width = $ph->getWidth();
248         $height = $ph->getHeight();
249
250         if($width < 175 || $height < 175) {
251                 $ph->scaleImageUp(200);
252                 $width = $ph->getWidth();
253                 $height = $ph->getHeight();
254         }
255
256         $hash = photo_new_resource();
257         
258
259         $smallest = 0;
260
261         $r = $ph->store(local_user(), 0 , $hash, $filename, t('Profile Photos'), 0 );   
262
263         if($r)
264                 info( t('Image uploaded successfully.') . EOL );
265         else
266                 notice( t('Image upload failed.') . EOL );
267
268         if($width > 640 || $height > 640) {
269                 $ph->scaleImage(640);
270                 $r = $ph->store(local_user(), 0 , $hash, $filename, t('Profile Photos'), 1 );   
271                 
272                 if($r === false)
273                         notice( sprintf(t('Image size reduction [%s] failed.'),"640") . EOL );
274                 else
275                         $smallest = 1;
276         }
277
278         $a->config['imagecrop'] = $hash;
279         $a->config['imagecrop_resolution'] = $smallest;
280         $a->config['imagecrop_ext'] = $ph->getExt();
281         $a->page['htmlhead'] .= get_markup_template("crophead.tpl");
282         $a->page['end'] .= get_markup_template("cropend.tpl");
283         return;
284 }}
285