]> git.mxchange.org Git - friendica.git/blob - mod/profile_photo.php
f7e6825ae7d2625d25756b9a358896a932d1e6da
[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                         if($r === NULL)
93                                 $_SESSION['sysmsg'] .= "Failed to add image to profile." . EOL;
94
95                 }
96                 goaway($a->get_baseurl() . '/profiles');
97         }
98
99         $extra_sql = (($profile_id) ? " AND `id` = " . intval($profile_id)  : " AND `is-default` = 1 " );
100
101
102         $r = q("SELECT `id` FROM `profile` WHERE `uid` = %d $extra_sql LIMIT 1", intval($_SESSION['uid']));
103         if($r === NULL || (! count($r))) {
104                 $_SESSION['sysmsg'] .= "Profile unavailable." . EOL;
105                 return;
106         }
107
108         $src      = $_FILES['userfile']['tmp_name'];
109         $filename = basename($_FILES['userfile']['name']);
110         $filesize = intval($_FILES['userfile']['size']);
111
112         $imagedata = @file_get_contents($src);
113         $ph = new Photo($imagedata);
114
115         if(! ($image = $ph->getImage())) {
116                 $_SESSION['sysmsg'] .= "Unable to process image." . EOL;
117                 @unlink($src);
118                 return;
119         }
120
121         @unlink($src);
122         $width = $ph->getWidth();
123         $height = $ph->getHeight();
124
125         if($width < 175 || $width < 175) {
126                 $ph->scaleImageUp(200);
127                 $width = $ph->getWidth();
128                 $height = $ph->getHeight();
129         }
130
131         $hash = hash('md5',uniqid(mt_rand(),true));
132         
133         $str_image = $ph->imageString();
134         $smallest = 0;
135         $r = q("INSERT INTO `photo` ( `uid`, `resource-id`, `created`, `edited`, `filename`, 
136                 `height`, `width`, `data`, `scale` )
137                 VALUES ( %d, '%s', '%s', '%s', '%s', %d, %d, '%s', 0 )",
138                 intval($_SESSION['uid']),
139                 dbesc($hash),
140                 datetime_convert(),
141                 datetime_convert(),
142                 dbesc(basename($filename)),
143                 intval($height),
144                 intval($width),
145                 dbesc($str_image));
146         if($r)
147                 $_SESSION['sysmsg'] .= "Image uploaded successfully." . EOL;
148         else
149                 $_SESSION['sysmsg'] .= "Image upload failed." . EOL;
150
151         if($width > 640 || $height > 640) {
152                 $ph->scaleImage(640);
153                 $str_image = $ph->imageString();
154                 $width = $ph->getWidth();
155                 $height = $ph->getHeight();
156
157                 $r = q("INSERT INTO `photo` ( `uid`, `resource-id`, `created`, `edited`, `filename`, 
158                         `height`, `width`, `data`, `scale` )
159                         VALUES ( %d, '%s', '%s', '%s', '%s', %d, %d, '%s', 1 )",
160                         intval($_SESSION['uid']),
161                         dbesc($hash),
162                         datetime_convert(),
163                         datetime_convert(),
164                         dbesc(basename($filename)),
165                         intval($height),
166                         intval($width),
167                         dbesc($str_image));
168                 if($r === NULL)
169                         $_SESSION['sysmsg'] .= "Image size reduction (640) failed." . EOL;
170                 else
171                         $smallest = 1;
172         }
173
174         $a->config['imagecrop'] = $hash;
175         $a->config['imagecrop_resolution'] = $smallest;
176         $a->page['htmlhead'] .= file_get_contents("view/crophead.tpl");
177
178 }
179
180
181 if(! function_exists('profile_photo_content')) {
182 function profile_photo_content(&$a) {
183
184
185         if(! x($a->config,'imagecrop')) {
186                 if((! x($_SESSION['authenticated'])) && (! (x($_SESSION,'uid')))) {
187                         $_SESSION['sysmsg'] .= "Permission denied." . EOL;
188                         return;
189                 }
190         
191                 if($a->argc > 1)
192                         $profile_id = intval($a->argv[1]);
193         
194                 $extra_sql = (($profile_id) ? " AND `id` = $profile_id " : " AND `is-default` = 1 " );
195
196
197                 $r = q("SELECT `id` FROM `profile` WHERE `uid` = %d $extra_sql LIMIT 1", intval($_SESSION['uid']));
198                 if($r === NULL || (! count($r))) {
199                         $_SESSION['sysmsg'] .= "Profile unavailable." . EOL;
200                         return;
201                 }
202         
203                 $o = file_get_contents('view/profile_photo.tpl');
204
205                 $o = replace_macros($o,array(
206                         '$profile_id' => $r[0]['id'],
207                         '$uid' => $_SESSION['uid'],
208                         ));
209
210                 return $o;
211         }
212         else {
213                 $filename = $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'] . '.jpg';
214                 $resolution = $a->config['imagecrop_resolution'];
215                 $o = file_get_contents("view/cropbody.tpl");
216                 $o = replace_macros($o,array(
217                         '$filename' => $filename,
218                         '$profile_id' => $a->argv[1],
219                         '$resource' => $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'],
220                         '$image_url' => $a->get_baseurl() . '/photo/' . $filename
221                         ));
222
223                 return $o;
224         }
225
226
227 }}