]> git.mxchange.org Git - friendica.git/blob - mod/profile_photo.php
cache result of (expensive) security check for visitor rights
[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         require_once("mod/profile.php");
12         profile_load($a,$a->user['nickname']);
13
14 }
15
16
17 function profile_photo_post(&$a) {
18
19         if(! local_user()) {
20                 notice ( t('Permission denied.') . EOL );
21                 return;
22         }
23
24         if((x($_POST,'cropfinal')) && ($_POST['cropfinal'] == 1)) {
25
26                 // phase 2 - we have finished cropping
27
28                 if($a->argc != 2) {
29                         notice( t('Image uploaded but image cropping failed.') . EOL );
30                         return;
31                 }
32
33                 $image_id = $a->argv[1];
34
35                 if(substr($image_id,-2,1) == '-') {
36                         $scale = substr($image_id,-1,1);
37                         $image_id = substr($image_id,0,-2);
38                 }
39                         
40
41                 $srcX = $_POST['xstart'];
42                 $srcY = $_POST['ystart'];
43                 $srcW = $_POST['xfinal'] - $srcX;
44                 $srcH = $_POST['yfinal'] - $srcY;
45
46                 $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d AND `scale` = %d LIMIT 1",
47                         dbesc($image_id),
48                         dbesc(local_user()),
49                         intval($scale));
50
51                 if(count($r)) {
52
53                         $base_image = $r[0];
54
55                         $im = new Photo($base_image['data']);
56                         if($im->is_valid()) {
57                                 $im->cropImage(175,$srcX,$srcY,$srcW,$srcH);
58
59                                 $r = $im->store(local_user(), 0, $base_image['resource-id'],$base_image['filename'], t('Profile Photos'), 4, 1);
60
61                                 if($r === false)
62                                         notice ( t('Image size reduction [175] failed.') . EOL );
63
64                                 $im->scaleImage(80);
65
66                                 $r = $im->store(local_user(), 0, $base_image['resource-id'],$base_image['filename'], t('Profile Photos'), 5, 1);
67                         
68                                 if($r === false)
69                                         notice( t('Image size reduction [80] failed.') . EOL );
70
71                                 $im->scaleImage(48);
72
73                                 $r = $im->store(local_user(), 0, $base_image['resource-id'],$base_image['filename'], t('Profile Photos'), 6, 1);
74                         
75                                 if($r === false)
76                                         notice( t('Image size reduction [48] failed.') . EOL );
77
78                                 // Unset the profile photo flag from any other photos I own
79
80                                 $r = q("UPDATE `photo` SET `profile` = 0 WHERE `profile` = 1 AND `resource-id` != '%s' AND `uid` = %d",
81                                         dbesc($base_image['resource-id']),
82                                         intval(local_user())
83                                 );
84
85                                 $r = q("UPDATE `contact` SET `avatar-date` = '%s' WHERE `self` = 1 AND `uid` = %d LIMIT 1",
86                                         dbesc(datetime_convert()),
87                                         intval(local_user())
88                                 );
89
90                                 // Update global directory in background
91                                 $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
92                                 $url = $_SESSION['my_url'];
93                                 if($url && strlen(get_config('system','directory_submit_url')))
94                                         proc_close(proc_open("\"$php_path\" \"include/directory.php\" \"$url\" &",
95                                                 array(),$foo));
96                         }
97                         else
98                                 notice( t('Unable to process image') . EOL);
99                 }
100
101                 goaway($a->get_baseurl() . '/profiles');
102                 return; // NOTREACHED
103         }
104
105         $src      = $_FILES['userfile']['tmp_name'];
106         $filename = basename($_FILES['userfile']['name']);
107         $filesize = intval($_FILES['userfile']['size']);
108
109         $maximagesize = get_config('system','maximagesize');
110
111         if(($maximagesize) && ($filesize > $maximagesize)) {
112                 notice( t('Image exceeds size limit of ') . $maximagesize . EOL);
113                 @unlink($src);
114                 return;
115         }
116
117         $imagedata = @file_get_contents($src);
118         $ph = new Photo($imagedata);
119
120         if(! $ph->is_valid()) {
121                 notice( t('Unable to process image.') . EOL );
122                 @unlink($src);
123                 return;
124         }
125
126         @unlink($src);
127
128         $width = $ph->getWidth();
129         $height = $ph->getHeight();
130
131         if($width < 175 || $height < 175) {
132                 $ph->scaleImageUp(200);
133                 $width = $ph->getWidth();
134                 $height = $ph->getHeight();
135         }
136
137         $hash = photo_new_resource();
138         
139
140         $smallest = 0;
141
142         $r = $ph->store(local_user(), 0 , $hash, $filename, t('Profile Photos'), 0 );   
143
144         if($r)
145                 notice( t('Image uploaded successfully.') . EOL );
146         else
147                 notice( t('Image upload failed.') . EOL );
148
149         if($width > 640 || $height > 640) {
150                 $ph->scaleImage(640);
151                 $r = $ph->store(local_user(), 0 , $hash, $filename, t('Profile Photos'), 1 );   
152
153                 if($r === false)
154                         notice( t('Image size reduction [640] failed.') . EOL );
155                 else
156                         $smallest = 1;
157         }
158
159         $a->config['imagecrop'] = $hash;
160         $a->config['imagecrop_resolution'] = $smallest;
161         $a->page['htmlhead'] .= load_view_file("view/crophead.tpl");
162         return;
163 }
164
165
166 if(! function_exists('profile_photo_content')) {
167 function profile_photo_content(&$a) {
168
169         if(! local_user()) {
170                 notice( t('Permission denied.') . EOL );
171                 return;
172         }
173
174         if(! x($a->config,'imagecrop')) {
175         
176                 $tpl = load_view_file('view/profile_photo.tpl');
177
178                 $o .= replace_macros($tpl,array(
179
180                 ));
181
182                 return $o;
183         }
184         else {
185                 $filename = $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'] . '.jpg';
186                 $resolution = $a->config['imagecrop_resolution'];
187                 $tpl = load_view_file("view/cropbody.tpl");
188                 $o .= replace_macros($tpl,array(
189                         '$filename' => $filename,
190                         '$resource' => $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'],
191                         '$image_url' => $a->get_baseurl() . '/photo/' . $filename
192                         ));
193
194                 return $o;
195         }
196
197         return; // NOTREACHED
198 }}