]> git.mxchange.org Git - friendica.git/blob - mod/photos.php
4a502b9fbc518de6c287e428518402d57c89698b
[friendica.git] / mod / photos.php
1 <?php
2
3 require_once('Photo.php');
4
5 function photos_init(&$a) {
6
7         if($a->argc > 1) {
8                 $nick = $a->argv[1];
9                 $r = q("SELECT * FROM `user` WHERE `nickname` = '%s' LIMIT 1",
10                         dbesc($nick)
11                 );
12
13                 if(! count($r))
14                         return;
15
16                 $a->data['user'] = $r[0];
17
18                 $albums = q("SELECT distinct(`album`) AS `album` FROM `photo` WHERE `uid` = %d",
19                         intval($a->data['user']['uid'])
20                 );
21
22                 if(count($albums)) {
23                         $a->data['albums'] = $albums;
24
25                         $o .= '<h4><a href="' . $a->get_baseurl() . '/profile/' . $a->data['user']['nickname'] . '">' . $a->data['user']['username'] . '</a></h4>';
26                         $o .= '<h4>' . '<a href="' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '">' . t('Photo Albums') . '</a></h4>';
27                 
28                         $o .= '<ul>';
29                         foreach($albums as $album) {
30                                 if((! strlen($album['album'])) || ($album['album'] == t('Contact Photos')))
31                                         continue;
32                                 $o .= '<li>' . '<a href="photos/' . $a->argv[1] . '/album/' . bin2hex($album['album']) . '" />' . $album['album'] . '</a></li>'; 
33                         }
34                         $o .= '</ul>';
35                 }
36                 $a->page['aside'] .= $o;
37         }
38
39
40 }
41
42
43
44
45 function photos_post(&$a) {
46
47
48         if(! local_user()) {
49                 notice( t('Permission denied.') . EOL );
50                 killme();
51         }
52
53         $r = q("SELECT * FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid` WHERE `user`.`uid` = %d AND `self` = 1 LIMIT 1",
54                 intval($_SESSION['uid'])
55         );
56
57         $contact_record = $r[0];        
58
59         if(! x($_FILES,'userfile'))
60                 killme();
61
62         if($_POST['partitionCount'])
63                 $java_upload = true;
64         else
65                 $java_upload = false;
66
67         $album =  notags(trim($_POST['album']));
68         $newalbum = notags(trim($_POST['newalbum']));
69
70         if(! strlen($album)) {
71                 if(strlen($newalbum))
72                         $album = $newalbum;
73                 else
74                         $album = datetime_convert('UTC',date_default_timezone_get(),'now', 'Y');
75         }
76
77         $src      = $_FILES['userfile']['tmp_name'];
78         $filename = basename($_FILES['userfile']['name']);
79         $filesize = intval($_FILES['userfile']['size']);
80
81         $imagedata = @file_get_contents($src);
82         $ph = new Photo($imagedata);
83
84         if(! ($image = $ph->getImage())) {
85                 notice( t('Unable to process image.') . EOL );
86                 @unlink($src);
87                 killme();
88         }
89
90         @unlink($src);
91
92         $width = $ph->getWidth();
93         $height = $ph->getHeight();
94
95         $smallest = 0;
96
97         $photo_hash = hash('md5',uniqid(mt_rand(),true));
98         
99         $r = $ph->store($_SESSION['uid'], 0, $photo_hash, $filename, $album, 0 );
100
101         if(! $r) {
102                 notice( t('Image upload failed.') . EOL );
103                 killme();
104         }
105
106         if($width > 640 || $height > 640) {
107                 $ph->scaleImage(640);
108                 $ph->store($_SESSION['uid'], 0, $photo_hash, $filename, $album, 1 );
109                 $smallest = 1;
110         }
111
112         if($width > 320 || $height > 320) {
113                 $ph->scaleImage(320);
114                 $ph->store($_SESSION['uid'], 0, $photo_hash, $filename, $album, 2 );
115                 $smallest = 2;
116         }
117         
118         $basename = basename($filename);
119
120         // Create item container
121
122         $body = '[url=' . $a->get_baseurl() . '/photos/' . $contact_record['nickname'] . '/image/' . $photo_hash . ']' 
123                 . '[img]' . $a->get_baseurl() . "/photo/{$photo_hash}-{$smallest}.jpg" . '[/img]' 
124                 . '[/url]';
125
126                         do {
127                         $dups = false;
128                         $item_hash = random_string();
129
130                         $uri = "urn:X-dfrn:" . $a->get_hostname() . ':' . $profile_uid . ':' . $item_hash;
131
132                         $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
133                         dbesc($uri));
134                         if(count($r))
135                                 $dups = true;
136                 } while($dups == true);
137
138
139                 $r = q("INSERT INTO `item` (`uid`, `type`, `resource-id`, `contact-id`,`owner-name`,`owner-link`,`owner-avatar`, `created`,
140                         `edited`, `uri`, `parent-uri`, `title`, `body`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`)
141                         VALUES( %d, '%s', '%s', %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' )",
142                         intval($_SESSION['uid']),
143                         dbesc('photo'),
144                         dbesc($photo_hash),                     
145                         intval($contact_record['id']),
146                         dbesc($contact_record['name']),
147                         dbesc($contact_record['url']),
148                         dbesc($contact_record['thumb']),
149                         datetime_convert(),
150                         datetime_convert(),
151                         dbesc($uri),
152                         dbesc($uri),
153                         dbesc($title),
154                         dbesc($body),
155                         dbesc($str_contact_allow),
156                         dbesc($str_group_allow),
157                         dbesc($str_contact_deny),
158                         dbesc($str_group_deny)
159
160                 );
161                 if($r) {
162
163                         $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
164                                 dbesc($uri)
165                         );
166                         if(count($r))
167                                 q("UPDATE `item` SET `parent` = %d, `last-child` = 1 WHERE `id` = %d LIMIT 1",
168                                 intval($r[0]['id']),
169                                 intval($r[0]['id'])
170                         );
171         
172                 }
173
174         // if album has no featured photo, promote one.
175
176
177         if(! $java_upload) {
178                 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
179                 return; // NOTREACHED
180         }
181
182         killme();
183         return; // NOTREACHED
184
185 }
186
187
188
189 function photos_content(&$a) {
190
191         // URLs:
192         // photos/name
193         // photos/name/upload
194         // photos/name/album/xxxxx
195         // photos/name/album/xxxxx/edit
196         // photos/name/album/xxxxx/drop
197         // photos/name/image/xxxxx
198         // photos/name/image/xxxxx/edit
199         // photos/name/image/xxxxx/drop
200
201         if(! x($a->data,'user')) {
202                 notice( t('No photos selected') . EOL );
203                 return;
204         }
205
206         $_SESSION['photo_return'] = $a->cmd;
207
208         //
209         // Parse arguments 
210         //
211
212         if($a->argc > 3) {
213                 $datatype = $a->argv[2];
214                 $datum = $a->argv[3];
215         }
216         elseif(($a->argc > 2) && ($a->argv[2] == 'upload'))
217                 $datatype = 'upload';
218         else
219                 $datatype = 'summary';
220
221         if($a->argc > 4)
222                 $cmd = $a->argv[4];
223         else
224                 $cmd = 'view';
225
226         //
227         // Setup permissions structures
228         //
229
230         $owner_uid = $a->data['user']['uid'];
231
232         if(remote_user()) {
233                 $contact_id = $_SESSION['visitor_id'];
234                 $groups = init_groups_visitor($contact_id);
235         }
236
237         // default permissions - anonymous user
238
239         $sql_extra = " AND `allow_cid` = '' AND `allow_gid` = '' AND `deny_cid` = '' AND `deny_gid` = '' ";
240
241         // Profile owner - everything is visible
242
243         if(local_user() && ($_SESSION['uid'] == $owner_uid)) {
244                 $sql_extra = '';        
245         }
246         elseif(remote_user()) {
247                 // authenticated visitor - here lie dragons
248                 $gs = '<<>>'; // should be impossible to match
249                 if(count($groups)) {
250                         foreach($groups as $g)
251                                 $gs .= '|<' . intval($g) . '>';
252                 } 
253                 $sql_extra = sprintf(
254                         " AND ( `allow_cid` = '' OR `allow_cid` REGEXP '<%d>' ) 
255                           AND ( `deny_cid`  = '' OR  NOT `deny_cid` REGEXP '<%d>' ) 
256                           AND ( `allow_gid` = '' OR `allow_gid` REGEXP '%s' )
257                           AND ( `deny_gid`  = '' OR NOT `deny_gid` REGEXP '%s') ",
258
259                         intval($_SESSION['visitor_id']),
260                         intval($_SESSION['visitor_id']),
261                         dbesc($gs),
262                         dbesc($gs)
263                 );
264         }
265
266         //
267         // dispatch request
268         //
269
270
271         if($datatype == 'upload') {
272                 if( ! (local_user() && ($_SESSION['uid'] == $a->data['user']['uid']))) {
273                         notice( t('Permission denied.'));
274                         return;
275                 }
276                 $albumselect = '<select id="photos-upload-album-select" name="album" size="4">';
277
278                 $albumselect .= '<option value="" selected="selected" >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</option>';
279                 if(count($a->data['albums'])) {
280                         foreach($a->data['albums'] as $album) {
281                                 if(($album['album'] == '') || ($album['album'] == t('Contact Photos')))
282                                         continue;
283                                 $albumselect .= '<option value="' . $album['album'] . '">' . $album['album'] . '</option>';
284                         }
285                 }
286                 $albumselect .= '</select>';
287                 $tpl = file_get_contents('view/photos_upload.tpl');
288                 $o .= replace_macros($tpl,array(
289                         '$pagename' => t('Upload Photos'),
290                         '$sessid' => session_id(),
291                         '$newalbum' => t('New album name: '),
292                         '$existalbumtext' => t('or existing album name: '),
293                         '$filestext' => t('Select files to upload: '),
294                         '$albumselect' => $albumselect,
295                         '$archive' => $a->get_baseurl() . '/jumploader_z.jar',
296                         '$nojava' => t('Use the following controls only if the Java uploader (above) fails to launch.'),
297                         '$uploadurl' => $a->get_baseurl() . '/photos',
298                         '$submit' => t('Submit')
299                 ));
300
301                 return $o; 
302
303         }
304
305         if($datatype == 'album') {
306
307                 $album = hex2bin($datum);
308
309                 $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` = '%s' 
310                         $sql_extra GROUP BY `resource-id`",
311                         intval($a->data['user']['uid']),
312                         dbesc($album)
313                 );
314                 if(count($r))
315                         $a->set_pager_total(count($r));
316
317
318                 $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` = '%s' 
319                         $sql_extra GROUP BY `resource-id` LIMIT %d , %d",
320                         intval($a->data['user']['uid']),
321                         dbesc($album),
322                         intval($a->pager['start']),
323                         intval($a->pager['itemspage'])
324                 );
325
326                 $o .= '<h3>' . $album . '</h3>';
327
328                 $tpl = file_get_contents('view/photo_album.tpl');
329                 if(count($r))
330                         foreach($r as $rr) {
331                                 $o .= replace_macros($tpl,array(
332                                         '$id' => $rr['id'],
333                                         '$photolink' => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $rr['resource-id'],
334                                         '$phototitle' => t('View Photo'),
335                                         '$imgsrc' => $a->get_baseurl() . '/photo/' . $rr['resource-id'] . '-' . $rr['scale'] . '.jpg',
336                                         '$imgalt' => $rr['filename']
337                                 ));
338
339                 }
340                 $o .= '<div id="photo-album-end"></div>';
341                 return $o;
342
343         }       
344
345
346         if($datatype == 'image') {
347
348                 // fetch item containing image, then comments
349                 $r = q("SELECT * FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' 
350                         $sql_extra ORDER BY `scale` ASC ",
351                         intval($a->data['user']['uid']),
352                         dbesc($datum)
353                 );
354
355                 if(! count($r)) {
356                         notice( t('Photo not available') . EOL );
357                         return;
358                 }
359
360                 if(count($r) == 1)
361                         $hires = $lores = $r[0];
362                 if(count($r) > 1) {
363                         $hires = $r[0];
364                         $lores = $r[1];
365                 }
366
367                 $o .= '<a href="' . $a->get_baseurl() . '/photo/' 
368                         . $hires['resource-id'] . '-' . $hires['scale'] . '.jpg" title="' 
369                         . t('View Full Size') . '" ><img src="' . $a->get_baseurl() . '/photo/' 
370                         . $lores['resource-id'] . '-' . $lores['scale'] . '.jpg' . '" /></a>';
371
372
373                 return $o;
374         }
375
376         // Default - show recent photos with upload link (if applicable)
377
378         $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` != '%s' 
379                 $sql_extra GROUP BY `resource-id`",
380                 intval($a->data['user']['uid']),
381                 dbesc( t('Contact Photos'))
382         );
383         if(count($r))
384                 $a->set_pager_total(count($r));
385
386
387         $r = q("SELECT `resource-id`, `album`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` != '%s' 
388                 $sql_extra GROUP BY `resource-id` ORDER BY `created` DESC LIMIT %d , %d",
389                 intval($a->data['user']['uid']),
390                 dbesc( t('Contact Photos')),
391                 intval($a->pager['start']),
392                 intval($a->pager['itemspage'])
393         );
394
395         $o .= '<h3>' . t('Recent Photos') . '</h3>';
396
397         if( local_user() && ($_SESSION['uid'] == $a->data['user']['uid'])) {
398                 $o .= '<div id="photo-top-links"><a id="photo-top-upload-link" href="'. $a->get_baseurl() . '/photos/' 
399                         . $a->data['user']['nickname'] . '/upload' . '">' . t('Upload New Photos') . '</a></div>';
400         }
401
402         $tpl = file_get_contents('view/photo_top.tpl');
403         if(count($r)) {
404                 foreach($r as $rr) {
405                         $o .= replace_macros($tpl,array(
406                                 '$id' => $rr['id'],
407                                 '$photolink' => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] 
408                                         . '/image/' . $rr['resource-id'],
409                                 '$phototitle' => t('View Photo'),
410                                 '$imgsrc' => $a->get_baseurl() . '/photo/' 
411                                         . $rr['resource-id'] . '-' . $rr['scale'] . '.jpg',
412                                 '$albumlink' => $a->get_baseurl . '/photos/' 
413                                         . $a->data['user']['nickname'] . '/album/' . bin2hex($rr['album']),
414                                 '$albumname' => $rr['album'],
415                                 '$albumalt' => t('View Album'),
416                                 '$imgalt' => $rr['filename']
417                         ));
418
419                 }
420                 $o .= '<div id="photo-top-end"></div>';
421         }
422         return $o;
423 }