]> git.mxchange.org Git - friendica.git/blob - src/Model/Photo.php
Use short form array syntax everywhere
[friendica.git] / src / Model / Photo.php
1 <?php
2 /**
3  * @file src/Model/Photo.php
4  * @brief This file contains the Photo class for database interface
5  */
6 namespace Friendica\Model;
7
8 use Friendica\Core\Cache;
9 use Friendica\Core\Config;
10 use Friendica\Core\PConfig;
11 use Friendica\Core\System;
12 use Friendica\Database\DBM;
13 use Friendica\Object\Image;
14 use dba;
15
16 require_once 'include/dba.php';
17
18 /**
19  * Class to handle photo dabatase table
20  */
21 class Photo
22 {
23         /**
24          * @param Image   $Image     image
25          * @param integer $uid       uid
26          * @param integer $cid       cid
27          * @param integer $rid       rid
28          * @param string  $filename  filename
29          * @param string  $album     album name
30          * @param integer $scale     scale
31          * @param integer $profile   optional, default = 0
32          * @param string  $allow_cid optional, default = ''
33          * @param string  $allow_gid optional, default = ''
34          * @param string  $deny_cid  optional, default = ''
35          * @param string  $deny_gid  optional, default = ''
36          * @param string  $desc      optional, default = ''
37          * @return object
38          */
39         public static function store(Image $Image, $uid, $cid, $rid, $filename, $album, $scale, $profile = 0, $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '', $desc = '')
40         {
41                 $photo = dba::selectFirst('photo', ['guid'], ["`resource-id` = ? AND `guid` != ?", $rid, '']);
42                 if (DBM::is_result($photo)) {
43                         $guid = $photo['guid'];
44                 } else {
45                         $guid = get_guid();
46                 }
47
48                 $existing_photo = dba::selectFirst('photo', ['id'], ['resource-id' => $rid, 'uid' => $uid, 'contact-id' => $cid, 'scale' => $scale]);
49
50                 $fields = [
51                         'uid' => $uid,
52                         'contact-id' => $cid,
53                         'guid' => $guid,
54                         'resource-id' => $rid,
55                         'created' => datetime_convert(),
56                         'edited' => datetime_convert(),
57                         'filename' => basename($filename),
58                         'type' => $Image->getType(),
59                         'album' => $album,
60                         'height' => $Image->getHeight(),
61                         'width' => $Image->getWidth(),
62                         'datasize' => strlen($Image->asString()),
63                         'data' => $Image->asString(),
64                         'scale' => $scale,
65                         'profile' => $profile,
66                         'allow_cid' => $allow_cid,
67                         'allow_gid' => $allow_gid,
68                         'deny_cid' => $deny_cid,
69                         'deny_gid' => $deny_gid,
70                         'desc' => $desc
71                 ];
72
73                 if (DBM::is_result($existing_photo)) {
74                         $r = dba::update('photo', $fields, ['id' => $existing_photo['id']]);
75                 } else {
76                         $r = dba::insert('photo', $fields);
77                 }
78
79                 return $r;
80         }
81
82         /**
83          * @param string  $image_url     Remote URL
84          * @param integer $uid           user id
85          * @param integer $cid           contact id
86          * @param boolean $quit_on_error optional, default false
87          * @return array
88          */
89         public static function importProfilePhoto($image_url, $uid, $cid, $quit_on_error = false)
90         {
91                 $photo = dba::selectFirst(
92                         'photo', ['resource-id'], ['uid' => $uid, 'contact-id' => $cid, 'scale' => 4, 'album' => 'Contact Photos']
93                 );
94                 if (x($photo['resource-id'])) {
95                         $hash = $photo['resource-id'];
96                 } else {
97                         $hash = photo_new_resource();
98                 }
99
100                 $photo_failure = false;
101
102                 $filename = basename($image_url);
103                 $img_str = fetch_url($image_url, true);
104
105                 if ($quit_on_error && ($img_str == "")) {
106                         return false;
107                 }
108
109                 $type = Image::guessType($image_url, true);
110                 $Image = new Image($img_str, $type);
111                 if ($Image->isValid()) {
112                         $Image->scaleToSquare(175);
113
114                         $r = self::store($Image, $uid, $cid, $hash, $filename, 'Contact Photos', 4);
115
116                         if ($r === false) {
117                                 $photo_failure = true;
118                         }
119
120                         $Image->scaleDown(80);
121
122                         $r = self::store($Image, $uid, $cid, $hash, $filename, 'Contact Photos', 5);
123
124                         if ($r === false) {
125                                 $photo_failure = true;
126                         }
127
128                         $Image->scaleDown(48);
129
130                         $r = self::store($Image, $uid, $cid, $hash, $filename, 'Contact Photos', 6);
131
132                         if ($r === false) {
133                                 $photo_failure = true;
134                         }
135
136                         $suffix = '?ts=' . time();
137
138                         $image_url = System::baseUrl() . '/photo/' . $hash . '-4.' . $Image->getExt() . $suffix;
139                         $thumb = System::baseUrl() . '/photo/' . $hash . '-5.' . $Image->getExt() . $suffix;
140                         $micro = System::baseUrl() . '/photo/' . $hash . '-6.' . $Image->getExt() . $suffix;
141
142                         // Remove the cached photo
143                         $a = get_app();
144                         $basepath = $a->get_basepath();
145
146                         if (is_dir($basepath . "/photo")) {
147                                 $filename = $basepath . '/photo/' . $hash . '-4.' . $Image->getExt();
148                                 if (file_exists($filename)) {
149                                         unlink($filename);
150                                 }
151                                 $filename = $basepath . '/photo/' . $hash . '-5.' . $Image->getExt();
152                                 if (file_exists($filename)) {
153                                         unlink($filename);
154                                 }
155                                 $filename = $basepath . '/photo/' . $hash . '-6.' . $Image->getExt();
156                                 if (file_exists($filename)) {
157                                         unlink($filename);
158                                 }
159                         }
160                 } else {
161                         $photo_failure = true;
162                 }
163
164                 if ($photo_failure && $quit_on_error) {
165                         return false;
166                 }
167
168                 if ($photo_failure) {
169                         $image_url = System::baseUrl() . '/images/person-175.jpg';
170                         $thumb = System::baseUrl() . '/images/person-80.jpg';
171                         $micro = System::baseUrl() . '/images/person-48.jpg';
172                 }
173
174                 return [$image_url, $thumb, $micro];
175         }
176
177         /**
178          * @param string $exifCoord coordinate
179          * @param string $hemi      hemi
180          * @return float
181          */
182         public static function getGps($exifCoord, $hemi)
183         {
184                 $degrees = count($exifCoord) > 0 ? self::gps2Num($exifCoord[0]) : 0;
185                 $minutes = count($exifCoord) > 1 ? self::gps2Num($exifCoord[1]) : 0;
186                 $seconds = count($exifCoord) > 2 ? self::gps2Num($exifCoord[2]) : 0;
187
188                 $flip = ($hemi == 'W' || $hemi == 'S') ? -1 : 1;
189
190                 return floatval($flip * ($degrees + ($minutes / 60) + ($seconds / 3600)));
191         }
192
193         /**
194          * @param string $coordPart coordPart
195          * @return float
196          */
197         private static function gps2Num($coordPart)
198         {
199                 $parts = explode('/', $coordPart);
200
201                 if (count($parts) <= 0) {
202                         return 0;
203                 }
204
205                 if (count($parts) == 1) {
206                         return $parts[0];
207                 }
208
209                 return floatval($parts[0]) / floatval($parts[1]);
210         }
211
212         /**
213          * @brief Fetch the photo albums that are available for a viewer
214          *
215          * The query in this function is cost intensive, so it is cached.
216          *
217          * @param int  $uid    User id of the photos
218          * @param bool $update Update the cache
219          *
220          * @return array Returns array of the photo albums
221          */
222         public static function getAlbums($uid, $update = false)
223         {
224                 $sql_extra = permissions_sql($uid);
225
226                 $key = "photo_albums:".$uid.":".local_user().":".remote_user();
227                 $albums = Cache::get($key);
228                 if (is_null($albums) || $update) {
229                         if (!Config::get('system', 'no_count', false)) {
230                                 /// @todo This query needs to be renewed. It is really slow
231                                 // At this time we just store the data in the cache
232                                 $albums = q("SELECT COUNT(DISTINCT `resource-id`) AS `total`, `album`, ANY_VALUE(`created`) AS `created`
233                                         FROM `photo`
234                                         WHERE `uid` = %d  AND `album` != '%s' AND `album` != '%s' $sql_extra
235                                         GROUP BY `album` ORDER BY `created` DESC",
236                                         intval($uid),
237                                         dbesc('Contact Photos'),
238                                         dbesc(t('Contact Photos'))
239                                 );
240                         } else {
241                                 // This query doesn't do the count and is much faster
242                                 $albums = q("SELECT DISTINCT(`album`), '' AS `total`
243                                         FROM `photo` USE INDEX (`uid_album_scale_created`)
244                                         WHERE `uid` = %d  AND `album` != '%s' AND `album` != '%s' $sql_extra",
245                                         intval($uid),
246                                         dbesc('Contact Photos'),
247                                         dbesc(t('Contact Photos'))
248                                 );
249                         }
250                         Cache::set($key, $albums, CACHE_DAY);
251                 }
252                 return $albums;
253         }
254
255         /**
256          * @param int $uid User id of the photos
257          * @return void
258          */
259         public static function clearAlbumCache($uid)
260         {
261                 $key = "photo_albums:".$uid.":".local_user().":".remote_user();
262                 Cache::set($key, null, CACHE_DAY);
263         }
264 }