]> git.mxchange.org Git - friendica.git/blob - src/Model/Photo.php
Shorten "Configuration" to "Config" again, since the Wrapper is gone
[friendica.git] / src / Model / Photo.php
1 <?php
2
3 /**
4  * @file src/Model/Photo.php
5  * This file contains the Photo class for database interface
6  */
7 namespace Friendica\Model;
8
9 use Friendica\Core\Cache\Duration;
10 use Friendica\Core\Config;
11 use Friendica\Core\Logger;
12 use Friendica\Core\System;
13 use Friendica\Database\DBA;
14 use Friendica\Database\DBStructure;
15 use Friendica\DI;
16 use Friendica\Model\Storage\SystemResource;
17 use Friendica\Object\Image;
18 use Friendica\Util\DateTimeFormat;
19 use Friendica\Util\Images;
20 use Friendica\Util\Network;
21 use Friendica\Util\Security;
22 use Friendica\Util\Strings;
23
24 require_once "include/dba.php";
25
26 /**
27  * Class to handle photo dabatase table
28  */
29 class Photo
30 {
31         /**
32          * Select rows from the photo table and returns them as array
33          *
34          * @param array $fields     Array of selected fields, empty for all
35          * @param array $conditions Array of fields for conditions
36          * @param array $params     Array of several parameters
37          *
38          * @return boolean|array
39          *
40          * @throws \Exception
41          * @see   \Friendica\Database\DBA::selectToArray
42          */
43         public static function selectToArray(array $fields = [], array $conditions = [], array $params = [])
44         {
45                 if (empty($fields)) {
46                         $fields = self::getFields();
47                 }
48
49                 return DBA::selectToArray('photo', $fields, $conditions, $params);
50         }
51
52         /**
53          * Retrieve a single record from the photo table
54          *
55          * @param array $fields     Array of selected fields, empty for all
56          * @param array $conditions Array of fields for conditions
57          * @param array $params     Array of several parameters
58          *
59          * @return bool|array
60          *
61          * @throws \Exception
62          * @see   \Friendica\Database\DBA::select
63          */
64         public static function selectFirst(array $fields = [], array $conditions = [], array $params = [])
65         {
66                 if (empty($fields)) {
67                         $fields = self::getFields();
68                 }
69
70                 return DBA::selectFirst("photo", $fields, $conditions, $params);
71         }
72
73         /**
74          * Get photos for user id
75          *
76          * @param integer $uid        User id
77          * @param string  $resourceid Rescource ID of the photo
78          * @param array   $conditions Array of fields for conditions
79          * @param array   $params     Array of several parameters
80          *
81          * @return bool|array
82          *
83          * @throws \Exception
84          * @see   \Friendica\Database\DBA::select
85          */
86         public static function getPhotosForUser($uid, $resourceid, array $conditions = [], array $params = [])
87         {
88                 $conditions["resource-id"] = $resourceid;
89                 $conditions["uid"] = $uid;
90
91                 return self::selectToArray([], $conditions, $params);
92         }
93
94         /**
95          * Get a photo for user id
96          *
97          * @param integer $uid        User id
98          * @param string  $resourceid Rescource ID of the photo
99          * @param integer $scale      Scale of the photo. Defaults to 0
100          * @param array   $conditions Array of fields for conditions
101          * @param array   $params     Array of several parameters
102          *
103          * @return bool|array
104          *
105          * @throws \Exception
106          * @see   \Friendica\Database\DBA::select
107          */
108         public static function getPhotoForUser($uid, $resourceid, $scale = 0, array $conditions = [], array $params = [])
109         {
110                 $conditions["resource-id"] = $resourceid;
111                 $conditions["uid"] = $uid;
112                 $conditions["scale"] = $scale;
113
114                 return self::selectFirst([], $conditions, $params);
115         }
116
117         /**
118          * Get a single photo given resource id and scale
119          *
120          * This method checks for permissions. Returns associative array
121          * on success, "no sign" image info, if user has no permission,
122          * false if photo does not exists
123          *
124          * @param string  $resourceid Rescource ID of the photo
125          * @param integer $scale      Scale of the photo. Defaults to 0
126          *
127          * @return boolean|array
128          * @throws \Exception
129          */
130         public static function getPhoto($resourceid, $scale = 0)
131         {
132                 $r = self::selectFirst(["uid"], ["resource-id" => $resourceid]);
133                 if (!DBA::isResult($r)) {
134                         return false;
135                 }
136
137                 $uid = $r["uid"];
138
139                 $sql_acl = Security::getPermissionsSQLByUserId($uid);
140
141                 $conditions = ["`resource-id` = ? AND `scale` <= ? " . $sql_acl, $resourceid, $scale];
142                 $params = ["order" => ["scale" => true]];
143                 $photo = self::selectFirst([], $conditions, $params);
144
145                 return $photo;
146         }
147
148         /**
149          * Check if photo with given conditions exists
150          *
151          * @param array $conditions Array of extra conditions
152          *
153          * @return boolean
154          * @throws \Exception
155          */
156         public static function exists(array $conditions)
157         {
158                 return DBA::exists("photo", $conditions);
159         }
160
161
162         /**
163          * Get Image object for given row id. null if row id does not exist
164          *
165          * @param array $photo Photo data. Needs at least 'id', 'type', 'backend-class', 'backend-ref'
166          *
167          * @return \Friendica\Object\Image
168          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
169          * @throws \ImagickException
170          */
171         public static function getImageForPhoto(array $photo)
172         {
173                 $backendClass = DI::storageManager()->getByName($photo['backend-class'] ?? '');
174                 if ($backendClass === null) {
175                         // legacy data storage in "data" column
176                         $i = self::selectFirst(['data'], ['id' => $photo['id']]);
177                         if ($i === false) {
178                                 return null;
179                         }
180                         $data = $i['data'];
181                 } else {
182                         $backendRef = $photo['backend-ref'] ?? '';
183                         $data = $backendClass->get($backendRef);
184                 }
185
186                 if (empty($data)) {
187                         return null;
188                 }
189
190                 return new Image($data, $photo['type']);
191         }
192
193         /**
194          * Return a list of fields that are associated with the photo table
195          *
196          * @return array field list
197          * @throws \Exception
198          */
199         private static function getFields()
200         {
201                 $allfields = DBStructure::definition(DI::app()->getBasePath(), false);
202                 $fields = array_keys($allfields["photo"]["fields"]);
203                 array_splice($fields, array_search("data", $fields), 1);
204                 return $fields;
205         }
206
207         /**
208          * Construct a photo array for a system resource image
209          *
210          * @param string $filename Image file name relative to code root
211          * @param string $mimetype Image mime type. Defaults to "image/jpeg"
212          *
213          * @return array
214          * @throws \Exception
215          */
216         public static function createPhotoForSystemResource($filename, $mimetype = "image/jpeg")
217         {
218                 $fields = self::getFields();
219                 $values = array_fill(0, count($fields), "");
220
221                 $photo                  = array_combine($fields, $values);
222                 $photo['backend-class'] = SystemResource::NAME;
223                 $photo['backend-ref']   = $filename;
224                 $photo['type']          = $mimetype;
225                 $photo['cacheable']     = false;
226
227                 return $photo;
228         }
229
230
231         /**
232          * store photo metadata in db and binary in default backend
233          *
234          * @param Image   $Image     Image object with data
235          * @param integer $uid       User ID
236          * @param integer $cid       Contact ID
237          * @param integer $rid       Resource ID
238          * @param string  $filename  Filename
239          * @param string  $album     Album name
240          * @param integer $scale     Scale
241          * @param integer $profile   Is a profile image? optional, default = 0
242          * @param string  $allow_cid Permissions, allowed contacts. optional, default = ""
243          * @param string  $allow_gid Permissions, allowed groups. optional, default = ""
244          * @param string  $deny_cid  Permissions, denied contacts.optional, default = ""
245          * @param string  $deny_gid  Permissions, denied greoup.optional, default = ""
246          * @param string  $desc      Photo caption. optional, default = ""
247          *
248          * @return boolean True on success
249          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
250          */
251         public static function store(Image $Image, $uid, $cid, $rid, $filename, $album, $scale, $profile = 0, $allow_cid = "", $allow_gid = "", $deny_cid = "", $deny_gid = "", $desc = "")
252         {
253                 $photo = self::selectFirst(["guid"], ["`resource-id` = ? AND `guid` != ?", $rid, ""]);
254                 if (DBA::isResult($photo)) {
255                         $guid = $photo["guid"];
256                 } else {
257                         $guid = System::createGUID();
258                 }
259
260                 $existing_photo = self::selectFirst(["id", "created", "backend-class", "backend-ref"], ["resource-id" => $rid, "uid" => $uid, "contact-id" => $cid, "scale" => $scale]);
261                 $created = DateTimeFormat::utcNow();
262                 if (DBA::isResult($existing_photo)) {
263                         $created = $existing_photo["created"];
264                 }
265
266                 // Get defined storage backend.
267                 // if no storage backend, we use old "data" column in photo table.
268                 // if is an existing photo, reuse same backend
269                 $data = "";
270                 $backend_ref = "";
271
272                 if (DBA::isResult($existing_photo)) {
273                         $backend_ref = (string)$existing_photo["backend-ref"];
274                         $storage = DI::storageManager()->getByName($existing_photo["backend-class"] ?? '');
275                 } else {
276                         $storage = DI::storage();
277                 }
278
279                 if ($storage === null) {
280                         $data = $Image->asString();
281                 } else {
282                         $backend_ref = $storage->put($Image->asString(), $backend_ref);
283                 }
284
285
286                 $fields = [
287                         "uid" => $uid,
288                         "contact-id" => $cid,
289                         "guid" => $guid,
290                         "resource-id" => $rid,
291                         "created" => $created,
292                         "edited" => DateTimeFormat::utcNow(),
293                         "filename" => basename($filename),
294                         "type" => $Image->getType(),
295                         "album" => $album,
296                         "height" => $Image->getHeight(),
297                         "width" => $Image->getWidth(),
298                         "datasize" => strlen($Image->asString()),
299                         "data" => $data,
300                         "scale" => $scale,
301                         "profile" => $profile,
302                         "allow_cid" => $allow_cid,
303                         "allow_gid" => $allow_gid,
304                         "deny_cid" => $deny_cid,
305                         "deny_gid" => $deny_gid,
306                         "desc" => $desc,
307                         "backend-class" => (string)$storage,
308                         "backend-ref" => $backend_ref
309                 ];
310
311                 if (DBA::isResult($existing_photo)) {
312                         $r = DBA::update("photo", $fields, ["id" => $existing_photo["id"]]);
313                 } else {
314                         $r = DBA::insert("photo", $fields);
315                 }
316
317                 return $r;
318         }
319
320
321         /**
322          * Delete info from table and data from storage
323          *
324          * @param array $conditions Field condition(s)
325          * @param array $options    Options array, Optional
326          *
327          * @return boolean
328          *
329          * @throws \Exception
330          * @see   \Friendica\Database\DBA::delete
331          */
332         public static function delete(array $conditions, array $options = [])
333         {
334                 // get photo to delete data info
335                 $photos = self::selectToArray(['backend-class', 'backend-ref'], $conditions);
336
337                 foreach($photos as $photo) {
338                         $backend_class = DI::storageManager()->getByName($photo['backend-class'] ?? '');
339                         if ($backend_class !== null) {
340                                 $backend_class->delete($photo["backend-ref"] ?? '');
341                         }
342                 }
343
344                 return DBA::delete("photo", $conditions, $options);
345         }
346
347         /**
348          * Update a photo
349          *
350          * @param array         $fields     Contains the fields that are updated
351          * @param array         $conditions Condition array with the key values
352          * @param Image         $img        Image to update. Optional, default null.
353          * @param array|boolean $old_fields Array with the old field values that are about to be replaced (true = update on duplicate)
354          *
355          * @return boolean  Was the update successfull?
356          *
357          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
358          * @see   \Friendica\Database\DBA::update
359          */
360         public static function update($fields, $conditions, Image $img = null, array $old_fields = [])
361         {
362                 if (!is_null($img)) {
363                         // get photo to update
364                         $photos = self::selectToArray(['backend-class', 'backend-ref'], $conditions);
365
366                         foreach($photos as $photo) {
367                                 $backend_class = DI::storageManager()->getByName($photo['backend-class'] ?? '');
368                                 if ($backend_class !== null) {
369                                         $fields["backend-ref"] = $backend_class->put($img->asString(), $photo['backend-ref']);
370                                 } else {
371                                         $fields["data"] = $img->asString();
372                                 }
373                         }
374                         $fields['updated'] = DateTimeFormat::utcNow();
375                 }
376
377                 $fields['edited'] = DateTimeFormat::utcNow();
378
379                 return DBA::update("photo", $fields, $conditions, $old_fields);
380         }
381
382         /**
383          * @param string  $image_url     Remote URL
384          * @param integer $uid           user id
385          * @param integer $cid           contact id
386          * @param boolean $quit_on_error optional, default false
387          * @return array
388          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
389          * @throws \ImagickException
390          */
391         public static function importProfilePhoto($image_url, $uid, $cid, $quit_on_error = false)
392         {
393                 $thumb = "";
394                 $micro = "";
395
396                 $photo = DBA::selectFirst(
397                         "photo", ["resource-id"], ["uid" => $uid, "contact-id" => $cid, "scale" => 4, "album" => "Contact Photos"]
398                 );
399                 if (!empty($photo['resource-id'])) {
400                         $resource_id = $photo["resource-id"];
401                 } else {
402                         $resource_id = self::newResource();
403                 }
404
405                 $photo_failure = false;
406
407                 $filename = basename($image_url);
408                 if (!empty($image_url)) {
409                         $ret = Network::curl($image_url, true);
410                         $img_str = $ret->getBody();
411                         $type = $ret->getContentType();
412                 } else {
413                         $img_str = '';
414                 }
415
416                 if ($quit_on_error && ($img_str == "")) {
417                         return false;
418                 }
419
420                 if (empty($type)) {
421                         $type = Images::guessType($image_url, true);
422                 }
423
424                 $Image = new Image($img_str, $type);
425                 if ($Image->isValid()) {
426                         $Image->scaleToSquare(300);
427
428                         $r = self::store($Image, $uid, $cid, $resource_id, $filename, "Contact Photos", 4);
429
430                         if ($r === false) {
431                                 $photo_failure = true;
432                         }
433
434                         $Image->scaleDown(80);
435
436                         $r = self::store($Image, $uid, $cid, $resource_id, $filename, "Contact Photos", 5);
437
438                         if ($r === false) {
439                                 $photo_failure = true;
440                         }
441
442                         $Image->scaleDown(48);
443
444                         $r = self::store($Image, $uid, $cid, $resource_id, $filename, "Contact Photos", 6);
445
446                         if ($r === false) {
447                                 $photo_failure = true;
448                         }
449
450                         $suffix = "?ts=" . time();
451
452                         $image_url = DI::baseUrl() . "/photo/" . $resource_id . "-4." . $Image->getExt() . $suffix;
453                         $thumb = DI::baseUrl() . "/photo/" . $resource_id . "-5." . $Image->getExt() . $suffix;
454                         $micro = DI::baseUrl() . "/photo/" . $resource_id . "-6." . $Image->getExt() . $suffix;
455
456                         // Remove the cached photo
457                         $a = DI::app();
458                         $basepath = $a->getBasePath();
459
460                         if (is_dir($basepath . "/photo")) {
461                                 $filename = $basepath . "/photo/" . $resource_id . "-4." . $Image->getExt();
462                                 if (file_exists($filename)) {
463                                         unlink($filename);
464                                 }
465                                 $filename = $basepath . "/photo/" . $resource_id . "-5." . $Image->getExt();
466                                 if (file_exists($filename)) {
467                                         unlink($filename);
468                                 }
469                                 $filename = $basepath . "/photo/" . $resource_id . "-6." . $Image->getExt();
470                                 if (file_exists($filename)) {
471                                         unlink($filename);
472                                 }
473                         }
474                 } else {
475                         $photo_failure = true;
476                 }
477
478                 if ($photo_failure && $quit_on_error) {
479                         return false;
480                 }
481
482                 if ($photo_failure) {
483                         $image_url = DI::baseUrl() . "/images/person-300.jpg";
484                         $thumb = DI::baseUrl() . "/images/person-80.jpg";
485                         $micro = DI::baseUrl() . "/images/person-48.jpg";
486                 }
487
488                 return [$image_url, $thumb, $micro];
489         }
490
491         /**
492          * @param array $exifCoord coordinate
493          * @param string $hemi      hemi
494          * @return float
495          */
496         public static function getGps($exifCoord, $hemi)
497         {
498                 $degrees = count($exifCoord) > 0 ? self::gps2Num($exifCoord[0]) : 0;
499                 $minutes = count($exifCoord) > 1 ? self::gps2Num($exifCoord[1]) : 0;
500                 $seconds = count($exifCoord) > 2 ? self::gps2Num($exifCoord[2]) : 0;
501
502                 $flip = ($hemi == "W" || $hemi == "S") ? -1 : 1;
503
504                 return floatval($flip * ($degrees + ($minutes / 60) + ($seconds / 3600)));
505         }
506
507         /**
508          * @param string $coordPart coordPart
509          * @return float
510          */
511         private static function gps2Num($coordPart)
512         {
513                 $parts = explode("/", $coordPart);
514
515                 if (count($parts) <= 0) {
516                         return 0;
517                 }
518
519                 if (count($parts) == 1) {
520                         return $parts[0];
521                 }
522
523                 return floatval($parts[0]) / floatval($parts[1]);
524         }
525
526         /**
527          * Fetch the photo albums that are available for a viewer
528          *
529          * The query in this function is cost intensive, so it is cached.
530          *
531          * @param int  $uid    User id of the photos
532          * @param bool $update Update the cache
533          *
534          * @return array Returns array of the photo albums
535          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
536          */
537         public static function getAlbums($uid, $update = false)
538         {
539                 $sql_extra = Security::getPermissionsSQLByUserId($uid);
540
541                 $key = "photo_albums:".$uid.":".local_user().":".remote_user();
542                 $albums = DI::cache()->get($key);
543                 if (is_null($albums) || $update) {
544                         if (!DI::config()->get("system", "no_count", false)) {
545                                 /// @todo This query needs to be renewed. It is really slow
546                                 // At this time we just store the data in the cache
547                                 $albums = q("SELECT COUNT(DISTINCT `resource-id`) AS `total`, `album`, ANY_VALUE(`created`) AS `created`
548                                         FROM `photo`
549                                         WHERE `uid` = %d  AND `album` != '%s' AND `album` != '%s' $sql_extra
550                                         GROUP BY `album` ORDER BY `created` DESC",
551                                         intval($uid),
552                                         DBA::escape("Contact Photos"),
553                                         DBA::escape(DI::l10n()->t("Contact Photos"))
554                                 );
555                         } else {
556                                 // This query doesn't do the count and is much faster
557                                 $albums = q("SELECT DISTINCT(`album`), '' AS `total`
558                                         FROM `photo` USE INDEX (`uid_album_scale_created`)
559                                         WHERE `uid` = %d  AND `album` != '%s' AND `album` != '%s' $sql_extra",
560                                         intval($uid),
561                                         DBA::escape("Contact Photos"),
562                                         DBA::escape(DI::l10n()->t("Contact Photos"))
563                                 );
564                         }
565                         DI::cache()->set($key, $albums, Duration::DAY);
566                 }
567                 return $albums;
568         }
569
570         /**
571          * @param int $uid User id of the photos
572          * @return void
573          * @throws \Exception
574          */
575         public static function clearAlbumCache($uid)
576         {
577                 $key = "photo_albums:".$uid.":".local_user().":".remote_user();
578                 DI::cache()->set($key, null, Duration::DAY);
579         }
580
581         /**
582          * Generate a unique photo ID.
583          *
584          * @return string
585          * @throws \Exception
586          */
587         public static function newResource()
588         {
589                 return System::createGUID(32, false);
590         }
591
592         /**
593          * Extracts the rid from a local photo URI
594          *
595          * @param string $image_uri The URI of the photo
596          * @return string The rid of the photo, or an empty string if the URI is not local
597          */
598         public static function ridFromURI(string $image_uri)
599         {
600                 if (!stristr($image_uri, DI::baseUrl() . '/photo/')) {
601                         return '';
602                 }
603                 $image_uri = substr($image_uri, strrpos($image_uri, '/') + 1);
604                 $image_uri = substr($image_uri, 0, strpos($image_uri, '-'));
605                 if (!strlen($image_uri)) {
606                         return '';
607                 }
608                 return $image_uri;
609         }
610
611         /**
612          * Changes photo permissions that had been embedded in a post
613          *
614          * @todo This function currently does have some flaws:
615          * - Sharing a post with a forum will create a photo that only the forum can see.
616          * - Sharing a photo again that been shared non public before doesn't alter the permissions.
617          *
618          * @return string
619          * @throws \Exception
620          */
621         public static function setPermissionFromBody($body, $uid, $original_contact_id, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny)
622         {
623                 // Simplify image codes
624                 $img_body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
625                 $img_body = preg_replace("/\[img\=(.*?)\](.*?)\[\/img\]/ism", '[img]$1[/img]', $img_body);
626
627                 // Search for images
628                 if (!preg_match_all("/\[img\](.*?)\[\/img\]/", $img_body, $match)) {
629                         return false;
630                 }
631                 $images = $match[1];
632                 if (empty($images)) {
633                         return false;
634                 }
635
636                 foreach ($images as $image) {
637                         $image_rid = self::ridFromURI($image);
638                         if (empty($image_rid)) {
639                                 continue;
640                         }
641
642                         // Ensure to only modify photos that you own
643                         $srch = '<' . intval($original_contact_id) . '>';
644
645                         $condition = [
646                                 'allow_cid' => $srch, 'allow_gid' => '', 'deny_cid' => '', 'deny_gid' => '',
647                                 'resource-id' => $image_rid, 'uid' => $uid
648                         ];
649                         if (!Photo::exists($condition)) {
650                                 continue;
651                         }
652
653                         /// @todo Check if $str_contact_allow does contain a public forum. Then set the permissions to public.
654
655                         $fields = ['allow_cid' => $str_contact_allow, 'allow_gid' => $str_group_allow,
656                                         'deny_cid' => $str_contact_deny, 'deny_gid' => $str_group_deny];
657                         $condition = ['resource-id' => $image_rid, 'uid' => $uid];
658                         Logger::info('Set permissions', ['condition' => $condition, 'permissions' => $fields]);
659                         Photo::update($fields, $condition);
660                 }
661
662                 return true;
663         }
664
665         /**
666          * Strips known picture extensions from picture links
667          *
668          * @param string $name Picture link
669          * @return string stripped picture link
670          * @throws \Exception
671          */
672         public static function stripExtension($name)
673         {
674                 $name = str_replace([".jpg", ".png", ".gif"], ["", "", ""], $name);
675                 foreach (Images::supportedTypes() as $m => $e) {
676                         $name = str_replace("." . $e, "", $name);
677                 }
678                 return $name;
679         }
680
681         /**
682          * Returns the GUID from picture links
683          *
684          * @param string $name Picture link
685          * @return string GUID
686          * @throws \Exception
687          */
688         public static function getGUID($name)
689         {
690                 $base = DI::baseUrl()->get();
691
692                 $guid = str_replace([Strings::normaliseLink($base), '/photo/'], '', Strings::normaliseLink($name));
693
694                 $guid = self::stripExtension($guid);
695                 if (substr($guid, -2, 1) != "-") {
696                         return '';
697                 }
698
699                 $scale = intval(substr($guid, -1, 1));
700                 if (!is_numeric($scale)) {
701                         return '';
702                 }
703
704                 $guid = substr($guid, 0, -2);
705                 return $guid;
706         }
707
708         /**
709          * Tests if the picture link points to a locally stored picture
710          *
711          * @param string $name Picture link
712          * @return boolean
713          * @throws \Exception
714          */
715         public static function isLocal($name)
716         {
717                 $guid = self::getGUID($name);
718
719                 if (empty($guid)) {
720                         return false;
721                 }
722
723                 return DBA::exists('photo', ['resource-id' => $guid]);
724         }
725
726         /**
727          * Tests if the link points to a locally stored picture page
728          *
729          * @param string $name Page link
730          * @return boolean
731          * @throws \Exception
732          */
733         public static function isLocalPage($name)
734         {
735                 $base = DI::baseUrl()->get();
736
737                 $guid = str_replace(Strings::normaliseLink($base), '', Strings::normaliseLink($name));
738                 $guid = preg_replace("=/photos/.*/image/(.*)=ism", '$1', $guid);
739                 if (empty($guid)) {
740                         return false;
741                 }
742
743                 return DBA::exists('photo', ['resource-id' => $guid]);
744         }
745 }