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