]> git.mxchange.org Git - friendica.git/blob - src/Model/Photo.php
Merge pull request #4386 from MrPetovan/task/3878-move-friendica_smarty-to-src
[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 = get_guid();
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                 $photo = dba::selectFirst(
95                         'photo', ['resource-id'], ['uid' => $uid, 'contact-id' => $cid, 'scale' => 4, 'album' => 'Contact Photos']
96                 );
97                 if (x($photo['resource-id'])) {
98                         $hash = $photo['resource-id'];
99                 } else {
100                         $hash = photo_new_resource();
101                 }
102
103                 $photo_failure = false;
104
105                 $filename = basename($image_url);
106                 $img_str = Network::fetchUrl($image_url, true);
107
108                 if ($quit_on_error && ($img_str == "")) {
109                         return false;
110                 }
111
112                 $type = Image::guessType($image_url, true);
113                 $Image = new Image($img_str, $type);
114                 if ($Image->isValid()) {
115                         $Image->scaleToSquare(175);
116
117                         $r = self::store($Image, $uid, $cid, $hash, $filename, 'Contact Photos', 4);
118
119                         if ($r === false) {
120                                 $photo_failure = true;
121                         }
122
123                         $Image->scaleDown(80);
124
125                         $r = self::store($Image, $uid, $cid, $hash, $filename, 'Contact Photos', 5);
126
127                         if ($r === false) {
128                                 $photo_failure = true;
129                         }
130
131                         $Image->scaleDown(48);
132
133                         $r = self::store($Image, $uid, $cid, $hash, $filename, 'Contact Photos', 6);
134
135                         if ($r === false) {
136                                 $photo_failure = true;
137                         }
138
139                         $suffix = '?ts=' . time();
140
141                         $image_url = System::baseUrl() . '/photo/' . $hash . '-4.' . $Image->getExt() . $suffix;
142                         $thumb = System::baseUrl() . '/photo/' . $hash . '-5.' . $Image->getExt() . $suffix;
143                         $micro = System::baseUrl() . '/photo/' . $hash . '-6.' . $Image->getExt() . $suffix;
144
145                         // Remove the cached photo
146                         $a = get_app();
147                         $basepath = $a->get_basepath();
148
149                         if (is_dir($basepath . "/photo")) {
150                                 $filename = $basepath . '/photo/' . $hash . '-4.' . $Image->getExt();
151                                 if (file_exists($filename)) {
152                                         unlink($filename);
153                                 }
154                                 $filename = $basepath . '/photo/' . $hash . '-5.' . $Image->getExt();
155                                 if (file_exists($filename)) {
156                                         unlink($filename);
157                                 }
158                                 $filename = $basepath . '/photo/' . $hash . '-6.' . $Image->getExt();
159                                 if (file_exists($filename)) {
160                                         unlink($filename);
161                                 }
162                         }
163                 } else {
164                         $photo_failure = true;
165                 }
166
167                 if ($photo_failure && $quit_on_error) {
168                         return false;
169                 }
170
171                 if ($photo_failure) {
172                         $image_url = System::baseUrl() . '/images/person-175.jpg';
173                         $thumb = System::baseUrl() . '/images/person-80.jpg';
174                         $micro = System::baseUrl() . '/images/person-48.jpg';
175                 }
176
177                 return [$image_url, $thumb, $micro];
178         }
179
180         /**
181          * @param string $exifCoord coordinate
182          * @param string $hemi      hemi
183          * @return float
184          */
185         public static function getGps($exifCoord, $hemi)
186         {
187                 $degrees = count($exifCoord) > 0 ? self::gps2Num($exifCoord[0]) : 0;
188                 $minutes = count($exifCoord) > 1 ? self::gps2Num($exifCoord[1]) : 0;
189                 $seconds = count($exifCoord) > 2 ? self::gps2Num($exifCoord[2]) : 0;
190
191                 $flip = ($hemi == 'W' || $hemi == 'S') ? -1 : 1;
192
193                 return floatval($flip * ($degrees + ($minutes / 60) + ($seconds / 3600)));
194         }
195
196         /**
197          * @param string $coordPart coordPart
198          * @return float
199          */
200         private static function gps2Num($coordPart)
201         {
202                 $parts = explode('/', $coordPart);
203
204                 if (count($parts) <= 0) {
205                         return 0;
206                 }
207
208                 if (count($parts) == 1) {
209                         return $parts[0];
210                 }
211
212                 return floatval($parts[0]) / floatval($parts[1]);
213         }
214
215         /**
216          * @brief Fetch the photo albums that are available for a viewer
217          *
218          * The query in this function is cost intensive, so it is cached.
219          *
220          * @param int  $uid    User id of the photos
221          * @param bool $update Update the cache
222          *
223          * @return array Returns array of the photo albums
224          */
225         public static function getAlbums($uid, $update = false)
226         {
227                 $sql_extra = permissions_sql($uid);
228
229                 $key = "photo_albums:".$uid.":".local_user().":".remote_user();
230                 $albums = Cache::get($key);
231                 if (is_null($albums) || $update) {
232                         if (!Config::get('system', 'no_count', false)) {
233                                 /// @todo This query needs to be renewed. It is really slow
234                                 // At this time we just store the data in the cache
235                                 $albums = q("SELECT COUNT(DISTINCT `resource-id`) AS `total`, `album`, ANY_VALUE(`created`) AS `created`
236                                         FROM `photo`
237                                         WHERE `uid` = %d  AND `album` != '%s' AND `album` != '%s' $sql_extra
238                                         GROUP BY `album` ORDER BY `created` DESC",
239                                         intval($uid),
240                                         dbesc('Contact Photos'),
241                                         dbesc(L10n::t('Contact Photos'))
242                                 );
243                         } else {
244                                 // This query doesn't do the count and is much faster
245                                 $albums = q("SELECT DISTINCT(`album`), '' AS `total`
246                                         FROM `photo` USE INDEX (`uid_album_scale_created`)
247                                         WHERE `uid` = %d  AND `album` != '%s' AND `album` != '%s' $sql_extra",
248                                         intval($uid),
249                                         dbesc('Contact Photos'),
250                                         dbesc(L10n::t('Contact Photos'))
251                                 );
252                         }
253                         Cache::set($key, $albums, CACHE_DAY);
254                 }
255                 return $albums;
256         }
257
258         /**
259          * @param int $uid User id of the photos
260          * @return void
261          */
262         public static function clearAlbumCache($uid)
263         {
264                 $key = "photo_albums:".$uid.":".local_user().":".remote_user();
265                 Cache::set($key, null, CACHE_DAY);
266         }
267 }