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