]> git.mxchange.org Git - friendica.git/blob - src/Model/Photo.php
Merge pull request #4092 from MrPetovan/task/3954-move-auth-to-src
[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                 $r = dba::select('photo', array('guid'), array("`resource-id` = ? AND `guid` != ?", $rid, ''), array('limit' => 1));
42                 if (DBM::is_result($r)) {
43                         $guid = $r['guid'];
44                 } else {
45                         $guid = get_guid();
46                 }
47
48                 $x = dba::select('photo', array('id'), array('resource-id' => $rid, 'uid' => $uid, 'contact-id' => $cid, 'scale' => $scale), array('limit' => 1));
49
50                 $fields = array(
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($x)) {
74                         $r = dba::update('photo', $fields, array('id' => $x['id']));
75                 } else {
76                         $r = dba::insert('photo', $fields);
77                 }
78
79                 return $r;
80         }
81
82         /**
83          * @param string  $photo         photo
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($photo, $uid, $cid, $quit_on_error = false)
90         {
91                 $r = dba::select(
92                         'photo', array('resource-id'), array('uid' => $uid, 'contact-id' => $cid, 'scale' => 4, 'album' => 'Contact Photos'), array('limit' => 1)
93                 );
94
95                 if (DBM::is_result($r) && strlen($r['resource-id'])) {
96                         $hash = $r['resource-id'];
97                 } else {
98                         $hash = photo_new_resource();
99                 }
100
101                 $photo_failure = false;
102
103                 $filename = basename($photo);
104                 $img_str = fetch_url($photo, true);
105
106                 if ($quit_on_error && ($img_str == "")) {
107                         return false;
108                 }
109
110                 $type = Image::guessType($photo, 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                         $photo = 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                         $photo = 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 array($photo, $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(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(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 }