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