]> git.mxchange.org Git - friendica.git/blob - include/photos.php
photo aside widget for profile page
[friendica.git] / include / photos.php
1 <?php
2 /**
3  * @file include/photos.php
4  * @brief Functions related to photo handling.
5  */
6
7
8 /**
9  * @brief Get the permissions for the photos page
10  * 
11  * @param int $owner_uid Owner of the photos page
12  * @param bool $community_page If it's an forum account
13  * 
14  * @return array
15  *......'can_post'
16  *......'visitor'
17  *......'contact'
18  *      'remote_contact'
19  * .....'contact_id'
20  *      'groups'
21  */
22 function photos_permissions($owner_uid, $community_page = 0) {
23
24         $arr = array();
25
26         if((local_user()) && (local_user() == $owner_uid))
27                 $arr['can_post'] = true;
28         else {
29                 if($community_page && remote_user()) {
30                         if(is_array($_SESSION['remote'])) {
31                                 foreach($_SESSION['remote'] as $v) {
32                                         if($v['uid'] == $owner_uid) {
33                                                 $arr['contact_id'] = $v['cid'];
34                                                 break;
35                                         }
36                                 }
37                         }
38                         if($arr['contact_id']) {
39
40                                 $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
41                                         intval($arr['contact_id']),
42                                         intval($owner_uid)
43                                 );
44                                 if(count($r)) {
45                                         $arr['can_post'] = true;
46                                         $arr['contact'] = $r[0];
47                                         $arr['remote_contact'] = true;
48                                         $arr['visitor'] = $cid;
49                                 }
50                         }
51                 }
52         }
53
54         // perhaps they're visiting - but not a community page, so they wouldn't have write access
55
56         if(remote_user() && (! $arr['visitor'])) {
57                 $arr['contact_id'] = 0;
58                 if(is_array($_SESSION['remote'])) {
59                         foreach($_SESSION['remote'] as $v) {
60                                 if($v['uid'] == $owner_uid) {
61                                         $arr['contact_id'] = $v['cid'];
62                                         break;
63                                 }
64                         }
65                 }
66                 if($arr['contact_id']) {
67                         $arr['groups'] = init_groups_visitor($arr['contact_id']);
68                         $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
69                                 intval($arr['contact_id']),
70                                 intval($owner_uid)
71                         );
72                         if(count($r)) {
73                                 $arr['contact'] = $r[0];
74                                 $arr['remote_contact'] = true;
75                         }
76                 }
77         }
78
79         if(! $arr['remote_contact']) {
80                 if(local_user()) {
81                         $arr['contact_id'] = $_SESSION['cid'];
82                         $arr['contact'] = $a->contact;
83                 }
84         }
85
86         return $arr;
87 }
88
89 /**
90  * @brief Construnct a widget with last uploaded photos
91  * 
92  * It displays the last 9 photos
93  * 
94  * @param array $profile_data
95  *......'profile_uid'...=> The user.id of the profile (owner of the hotos)
96  *......'nickname'......=> Nick of the owner of the profile
97  *......'page-flags'....=> Account type of the profile
98  * 
99  * @return string
100  *......formatted html
101  * 
102  * @template widget_photos.tpl
103  */
104 function widget_photos($profile_data) {
105
106         $community_page = (($profile_data['page-flags'] == PAGE_COMMUNITY) ? true : false);
107         $nickname = $profile_data['nickname'];
108         $owner_id = $profile_data['profile_uid'];
109
110         $phototypes = Photo::supportedTypes();
111         $photos_perms = photos_permissions($owner_id, $community_page);
112
113         $sql_extra = permissions_sql($owner_id, $photos_perms['remote_contact'], $photos_perms['groups']);
114
115         $r = q("SELECT `resource-id`, `id`, `filename`, `type`, max(`scale`) AS `scale` FROM `photo`
116                 WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s' AND `album` != '%s'
117                 $sql_extra GROUP BY `resource-id` ORDER BY `created` DESC LIMIT 9",
118                 intval($owner_id),
119                 dbesc('Contact Photos'),
120                 dbesc( t('Contact Photos')),
121                 dbesc( t('Profile Photos'))
122         );
123
124         $photos = array();
125         if(count($r)) {
126                 foreach($r as $rr) {
127                         $ext = $phototypes[$rr['type']];
128         
129                         $photos[] = array(
130                                 'id'            => $rr['id'],
131                                 'src'           => z_root() . '/photos/' . $nickname . '/image/' . $rr['resource-id'],
132                                 'photo'         => z_root() . '/photo/' . $rr['resource-id'] . '-' . ((($rr['scale']) == 6) ? 4 : $rr['scale']) . '.' . $ext,
133                                 'alt_text'      => $rr['filename'],
134                         );
135                 }
136
137                 $tpl = get_markup_template('widget_photos.tpl');
138                 $o .= replace_macros($tpl, array(
139                         '$title' => t('Photos'),
140                         '$photos' => $photos,
141                         '$photo_albums_page'    => z_root() . '/photos/' . $nickname,
142                         '$photo_albums_page_title' => t('Vist the Photo Albums'),
143                 ));
144
145                 return $o;
146         }
147 }
148
149 function getGps($exifCoord, $hemi) {
150         $degrees = count($exifCoord) > 0 ? gps2Num($exifCoord[0]) : 0;
151         $minutes = count($exifCoord) > 1 ? gps2Num($exifCoord[1]) : 0;
152         $seconds = count($exifCoord) > 2 ? gps2Num($exifCoord[2]) : 0;
153
154         $flip = ($hemi == 'W' or $hemi == 'S') ? -1 : 1;
155
156         return floatval($flip * ($degrees + ($minutes / 60) + ($seconds / 3600)));
157 }
158
159 function gps2Num($coordPart) {
160         $parts = explode('/', $coordPart);
161
162         if (count($parts) <= 0)
163                 return 0;
164
165         if (count($parts) == 1)
166                 return $parts[0];
167
168         return floatval($parts[0]) / floatval($parts[1]);
169 }