]> git.mxchange.org Git - friendica.git/blob - mod/profile_photo.php
mucho progress on notifier, email style dfrn url's
[friendica.git] / mod / profile_photo.php
1 <?php
2
3 require_once("Photo.php");
4
5 function profile_photo_init(&$a) {
6
7         if((! x($_SESSION,'authenticated')) && (x($_SESSION,'uid'))) {
8                 $_SESSION['sysmsg'] .= "Permission denied." . EOL;
9                 $a->error = 404;
10                 return;
11         }
12         require_once("mod/profile.php");
13         profile_load($a,$_SESSION['uid']);
14 }
15
16
17 function profile_photo_post(&$a) {
18
19
20
21         if((! x($_SESSION,'authenticated')) && (! (x($_SESSION,'uid')))) {
22                 $_SESSION['sysmsg'] .= "Permission denied." . EOL;
23                 return;
24         }
25
26         if($a->argc > 1)
27                 $profile_id = intval($a->argv[1]);
28
29         if(x($_POST,'xstart') !== false) {
30                 // phase 2 - we have finished cropping
31                 if($a->argc != 3) {
32                         $_SESSION['sysmsg'] .= "Image uploaded but image cropping failed." . EOL;
33                         return;
34                 }
35                 $image_id = $a->argv[2];
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 `scale` = %d LIMIT 1",
48                         dbesc($image_id),
49                         intval($scale));
50                 if($r !== NULL && (count($r))) {
51                         $im = new Photo($r[0]['data']);
52                         $im->cropImage(175,$srcX,$srcY,$srcW,$srcH);
53                         $s = $im->imageString(); 
54                         $x = $im->getWidth();
55                         $y = $im->getHeight();
56
57                         $ret = q("INSERT INTO `photo` ( `uid`, `resource-id`, `created`, `edited`, `filename`, 
58                                 `height`, `width`, `data`, `scale` )
59                                 VALUES ( %d, '%s', '%s', '%s', '%s', %d, %d, '%s', 4 )",
60                                 intval($_SESSION['uid']),
61                                 dbesc($r[0]['resource-id']),
62                                 datetime_convert(),
63                                 datetime_convert(),
64                                 dbesc($r[0]['filename']),
65                                 intval($y),
66                                 intval($x),
67                                 dbesc($s));
68                         if($r === NULL)
69                                 $_SESSION['sysmsg'] .= "Image size reduction (175) failed." . EOL;
70
71                         $im->scaleImage(80);
72                         $s = $im->imageString();
73                         $x = $im->getWidth();
74                         $y = $im->getHeight();
75                         $ret = q("INSERT INTO `photo` ( `uid`, `resource-id`, `created`, `edited`, `filename`, 
76                                 `height`, `width`, `data`, `scale` )
77                                 VALUES ( %d, '%s', '%s', '%s', '%s', %d, %d, '%s', 5 )",
78                                 intval($_SESSION['uid']),
79                                 dbesc($r[0]['resource-id']),
80                                 datetime_convert(),
81                                 datetime_convert(),
82                                 dbesc($r[0]['filename']),
83                                 intval($y),
84                                 intval($x),
85                                 dbesc($s));
86                         if($r === NULL)
87                                 $_SESSION['sysmsg'] .= "Image size reduction (80) failed." . EOL;
88                         $r = q("UPDATE `profile` SET `photo` = '%s', `thumb` = '%s' WHERE `id` = %d LIMIT 1",
89                                 dbesc($a->get_baseurl() . '/photo/' . $image_id . '-4.jpg'),
90                                 dbesc($a->get_baseurl() . '/photo/' . $image_id . '-5.jpg'),
91                                 intval($profile_id)
92                         );
93                         if($r === false)
94                                 $_SESSION['sysmsg'] .= "Failed to add image to profile." . EOL;
95 // We should really only do this if requested, or if it is the default profile. The contact
96 //table images are used in coversations. 
97                         $r = q("UPDATE `contact` SET `photo` = '%s', `thumb` = '%s' WHERE `uid` = %d AND `self` = 1 LIMIT 1",
98                                 dbesc($a->get_baseurl() . '/photo/' . $image_id . '-4.jpg'),
99                                 dbesc($a->get_baseurl() . '/photo/' . $image_id . '-5.jpg'),
100                                 intval($_SESSION['uid'])
101                         );
102                         if($r ===false)
103                                 notice("Failed to add photo to contact table." .EOL );
104
105                 }
106                 goaway($a->get_baseurl() . '/profiles');
107         }
108
109         $extra_sql = (($profile_id) ? " AND `id` = " . intval($profile_id)  : " AND `is-default` = 1 " );
110
111
112         $r = q("SELECT `id` FROM `profile` WHERE `uid` = %d $extra_sql LIMIT 1", intval($_SESSION['uid']));
113         if($r === NULL || (! count($r))) {
114                 $_SESSION['sysmsg'] .= "Profile unavailable." . EOL;
115                 return;
116         }
117
118         $src      = $_FILES['userfile']['tmp_name'];
119         $filename = basename($_FILES['userfile']['name']);
120         $filesize = intval($_FILES['userfile']['size']);
121
122         $imagedata = @file_get_contents($src);
123         $ph = new Photo($imagedata);
124
125         if(! ($image = $ph->getImage())) {
126                 $_SESSION['sysmsg'] .= "Unable to process image." . EOL;
127                 @unlink($src);
128                 return;
129         }
130
131         @unlink($src);
132         $width = $ph->getWidth();
133         $height = $ph->getHeight();
134
135         if($width < 175 || $width < 175) {
136                 $ph->scaleImageUp(200);
137                 $width = $ph->getWidth();
138                 $height = $ph->getHeight();
139         }
140
141         $hash = hash('md5',uniqid(mt_rand(),true));
142         
143         $str_image = $ph->imageString();
144         $smallest = 0;
145         $r = q("INSERT INTO `photo` ( `uid`, `resource-id`, `created`, `edited`, `filename`, 
146                 `height`, `width`, `data`, `scale` )
147                 VALUES ( %d, '%s', '%s', '%s', '%s', %d, %d, '%s', 0 )",
148                 intval($_SESSION['uid']),
149                 dbesc($hash),
150                 datetime_convert(),
151                 datetime_convert(),
152                 dbesc(basename($filename)),
153                 intval($height),
154                 intval($width),
155                 dbesc($str_image));
156         if($r)
157                 $_SESSION['sysmsg'] .= "Image uploaded successfully." . EOL;
158         else
159                 $_SESSION['sysmsg'] .= "Image upload failed." . EOL;
160
161         if($width > 640 || $height > 640) {
162                 $ph->scaleImage(640);
163                 $str_image = $ph->imageString();
164                 $width = $ph->getWidth();
165                 $height = $ph->getHeight();
166
167                 $r = q("INSERT INTO `photo` ( `uid`, `resource-id`, `created`, `edited`, `filename`, 
168                         `height`, `width`, `data`, `scale` )
169                         VALUES ( %d, '%s', '%s', '%s', '%s', %d, %d, '%s', 1 )",
170                         intval($_SESSION['uid']),
171                         dbesc($hash),
172                         datetime_convert(),
173                         datetime_convert(),
174                         dbesc(basename($filename)),
175                         intval($height),
176                         intval($width),
177                         dbesc($str_image));
178                 if($r === NULL)
179                         $_SESSION['sysmsg'] .= "Image size reduction (640) failed." . EOL;
180                 else
181                         $smallest = 1;
182         }
183
184         $a->config['imagecrop'] = $hash;
185         $a->config['imagecrop_resolution'] = $smallest;
186         $a->page['htmlhead'] .= file_get_contents("view/crophead.tpl");
187
188 }
189
190
191 if(! function_exists('profile_photo_content')) {
192 function profile_photo_content(&$a) {
193
194
195         if(! x($a->config,'imagecrop')) {
196                 if((! x($_SESSION['authenticated'])) && (! (x($_SESSION,'uid')))) {
197                         $_SESSION['sysmsg'] .= "Permission denied." . EOL;
198                         return;
199                 }
200         
201                 if($a->argc > 1)
202                         $profile_id = intval($a->argv[1]);
203         
204                 $extra_sql = (($profile_id) ? " AND `id` = $profile_id " : " AND `is-default` = 1 " );
205
206
207                 $r = q("SELECT `id` FROM `profile` WHERE `uid` = %d $extra_sql LIMIT 1", intval($_SESSION['uid']));
208                 if($r === NULL || (! count($r))) {
209                         $_SESSION['sysmsg'] .= "Profile unavailable." . EOL;
210                         return;
211                 }
212         
213                 $o = file_get_contents('view/profile_photo.tpl');
214
215                 $o = replace_macros($o,array(
216                         '$profile_id' => $r[0]['id'],
217                         '$uid' => $_SESSION['uid'],
218                         ));
219
220                 return $o;
221         }
222         else {
223                 $filename = $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'] . '.jpg';
224                 $resolution = $a->config['imagecrop_resolution'];
225                 $o = file_get_contents("view/cropbody.tpl");
226                 $o = replace_macros($o,array(
227                         '$filename' => $filename,
228                         '$profile_id' => $a->argv[1],
229                         '$resource' => $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'],
230                         '$image_url' => $a->get_baseurl() . '/photo/' . $filename
231                         ));
232
233                 return $o;
234         }
235
236
237 }}