]> git.mxchange.org Git - friendica.git/blob - mod/profile_photo.php
Merge branch 'friendika-master'
[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 ( t('Image size reduction [175] failed.') . 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( t('Image size reduction [80] failed.') . 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( t('Image size reduction [48] failed.') . 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                                 $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
91                                 $url = $_SESSION['my_url'];
92                                 if($url && strlen(get_config('system','directory_submit_url')))
93                                         //proc_close(proc_open("\"$php_path\" \"include/directory.php\" \"$url\" &",array(),$foo));
94                                         proc_run($php_path,"include/directory.php","$url");
95                         }
96                         else
97                                 notice( t('Unable to process image') . EOL);
98                 }
99
100                 goaway($a->get_baseurl() . '/profiles');
101                 return; // NOTREACHED
102         }
103
104         $src      = $_FILES['userfile']['tmp_name'];
105         $filename = basename($_FILES['userfile']['name']);
106         $filesize = intval($_FILES['userfile']['size']);
107
108         $maximagesize = get_config('system','maximagesize');
109
110         if(($maximagesize) && ($filesize > $maximagesize)) {
111                 notice( t('Image exceeds size limit of ') . $maximagesize . EOL);
112                 @unlink($src);
113                 return;
114         }
115
116         $imagedata = @file_get_contents($src);
117         $ph = new Photo($imagedata);
118
119         if(! $ph->is_valid()) {
120                 notice( t('Unable to process image.') . EOL );
121                 @unlink($src);
122                 return;
123         }
124
125         @unlink($src);
126         return profile_photo_crop_ui_head($a, $ph);
127         
128 }
129
130
131 if(! function_exists('profile_photo_content')) {
132 function profile_photo_content(&$a) {
133
134         if(! local_user()) {
135                 notice( t('Permission denied.') . EOL );
136                 return;
137         }
138         
139         if( $a->argv[1]=='use'){
140                 if ($a->argc<3){
141                         notice( t('Permission denied.') . EOL );
142                         return;
143                 };
144                         
145                 $resource_id = $a->argv[2];
146                 //die(":".local_user());
147                 $r=q("SELECT * FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' ORDER BY `scale` ASC",
148                         intval(local_user()),
149                         dbesc($resource_id)
150                         );
151                 if (!count($r)){
152                         notice( t('Permission denied.') . EOL );
153                         return;
154                 }
155                 // set an already uloaded photo as profile photo
156                 // if photo is in 'Profile Photos', change it in db
157                 if ($r[0]['album']== t('Profile Photos')){
158                         $r=q("UPDATE `photo` SET `profile`=0 WHERE `profile`=1 AND `uid`=%d",
159                                 intval(local_user()));
160                         
161                         $r=q("UPDATE `photo` SET `profile`=1 WHERE `uid` = %d AND `resource-id` = '%s'",
162                                 intval(local_user()),
163                                 dbesc($resource_id)
164                                 );
165                         
166                         $r = q("UPDATE `contact` SET `avatar-date` = '%s' WHERE `self` = 1 AND `uid` = %d LIMIT 1",
167                                 dbesc(datetime_convert()),
168                                 intval(local_user())
169                         );
170                         
171                         // Update global directory in background
172                         $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
173                         $url = $_SESSION['my_url'];
174                         if($url && strlen(get_config('system','directory_submit_url')))
175                                 //proc_close(proc_open("\"$php_path\" \"include/directory.php\" \"$url\" &",array(),$foo));
176                                 proc_run($php_path,"include/directory.php","$url");
177                         
178                         goaway($a->get_baseurl() . '/profiles');
179                         return; // NOTREACHED
180                 }
181                 $ph = new Photo($r[0]['data']);
182                 profile_photo_crop_ui_head($a, $ph);
183                 // go ahead as we have jus uploaded a new photo to crop
184         }
185
186         if(! x($a->config,'imagecrop')) {
187         
188                 $tpl = load_view_file('view/profile_photo.tpl');
189
190                 $o .= replace_macros($tpl,array(
191                         '$user' => $a->user['nickname']
192                 ));
193
194                 return $o;
195         }
196         else {
197                 $filename = $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'] . '.jpg';
198                 $resolution = $a->config['imagecrop_resolution'];
199                 $tpl = load_view_file("view/cropbody.tpl");
200                 $o .= replace_macros($tpl,array(
201                         '$filename' => $filename,
202                         '$resource' => $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'],
203                         '$image_url' => $a->get_baseurl() . '/photo/' . $filename
204                         ));
205
206                 return $o;
207         }
208
209         return; // NOTREACHED
210 }}
211
212
213 if(! function_exists('_crop_ui_head')) {
214 function profile_photo_crop_ui_head(&$a, $ph){
215         $width = $ph->getWidth();
216         $height = $ph->getHeight();
217
218         if($width < 175 || $height < 175) {
219                 $ph->scaleImageUp(200);
220                 $width = $ph->getWidth();
221                 $height = $ph->getHeight();
222         }
223
224         $hash = photo_new_resource();
225         
226
227         $smallest = 0;
228
229         $r = $ph->store(local_user(), 0 , $hash, $filename, t('Profile Photos'), 0 );   
230
231         if($r)
232                 notice( t('Image uploaded successfully.') . EOL );
233         else
234                 notice( t('Image upload failed.') . EOL );
235
236         if($width > 640 || $height > 640) {
237                 $ph->scaleImage(640);
238                 $r = $ph->store(local_user(), 0 , $hash, $filename, t('Profile Photos'), 1 );   
239                 
240                 if($r === false)
241                         notice( t('Image size reduction [640] failed.') . EOL );
242                 else
243                         $smallest = 1;
244         }
245
246         $a->config['imagecrop'] = $hash;
247         $a->config['imagecrop_resolution'] = $smallest;
248         $a->page['htmlhead'] .= load_view_file("view/crophead.tpl");
249         return;
250 }}
251