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