From a3af6807acf0aeb19a8420e2778741347bbfebd6 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 20 Feb 2018 10:02:07 +0000 Subject: [PATCH] Improved resource id creation --- boot.php | 12 +++++++----- include/api.php | 2 +- include/text.php | 25 ------------------------- mod/photos.php | 2 +- mod/profile_photo.php | 2 +- mod/wall_upload.php | 2 +- src/Model/Photo.php | 11 ++++++++++- src/Model/User.php | 2 +- src/Object/Image.php | 2 +- 9 files changed, 23 insertions(+), 37 deletions(-) diff --git a/boot.php b/boot.php index 6400274550..0d26403bcb 100644 --- a/boot.php +++ b/boot.php @@ -854,11 +854,13 @@ function check_addons(App $a) return; } -function get_guid($size = 16, $prefix = "") +function get_guid($size = 16, $prefix = '') { - if ($prefix == "") { + if (is_bool($prefix)) { + $prefix = ''; + } elseif ($prefix == '') { $a = get_app(); - $prefix = hash("crc32", $a->get_hostname()); + $prefix = hash('crc32', $a->get_hostname()); } while (strlen($prefix) < ($size - 13)) { @@ -867,10 +869,10 @@ function get_guid($size = 16, $prefix = "") if ($size >= 24) { $prefix = substr($prefix, 0, $size - 22); - return(str_replace(".", "", uniqid($prefix, true))); + return str_replace('.', '', uniqid($prefix, true)); } else { $prefix = substr($prefix, 0, max($size - 13, 0)); - return(uniqid($prefix)); + return uniqid($prefix); } } diff --git a/include/api.php b/include/api.php index f7c04aaf88..f742d2cb2a 100644 --- a/include/api.php +++ b/include/api.php @@ -4614,7 +4614,7 @@ function save_media_to_database($mediatype, $media, $type, $album, $allow_cid, $ $height = $Image->getHeight(); // create a new resource-id if not already provided - $hash = ($photo_id == null) ? photo_new_resource() : $photo_id; + $hash = ($photo_id == null) ? Photo::newResource() : $photo_id; if ($mediatype == "photo") { // upload normal image (scales 0, 1, 2) diff --git a/include/text.php b/include/text.php index 02f17fdb4b..5f01fae361 100644 --- a/include/text.php +++ b/include/text.php @@ -489,31 +489,6 @@ function item_new_uri($hostname, $uid, $guid = "") { return $uri; } - -/** - * Generate a guaranteed unique photo ID. - * safe from birthday paradox - * - * @return string - */ -function photo_new_resource() { - - do { - $found = false; - $resource = hash('md5',uniqid(mt_rand(),true)); - $r = q("SELECT `id` FROM `photo` WHERE `resource-id` = '%s' LIMIT 1", - dbesc($resource) - ); - - if (DBM::is_result($r)) { - $found = true; - } - } while ($found == true); - - return $resource; -} - - /** * @deprecated * wrapper to load a view template, checking for alternate diff --git a/mod/photos.php b/mod/photos.php index 2b6998ae1d..3cd8b75286 100644 --- a/mod/photos.php +++ b/mod/photos.php @@ -838,7 +838,7 @@ function photos_post(App $a) $smallest = 0; - $photo_hash = photo_new_resource(); + $photo_hash = Photo::newResource(); $r = Photo::store($Image, $page_owner_uid, $visitor, $photo_hash, $filename, $album, 0 , 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny); diff --git a/mod/profile_photo.php b/mod/profile_photo.php index 5556553cf9..80bdfa534e 100644 --- a/mod/profile_photo.php +++ b/mod/profile_photo.php @@ -308,7 +308,7 @@ function profile_photo_crop_ui_head(App $a, Image $Image) { $height = $Image->getHeight(); } - $hash = photo_new_resource(); + $hash = Photo::newResource(); $smallest = 0; diff --git a/mod/wall_upload.php b/mod/wall_upload.php index dd7d0f1586..61578ca5c8 100644 --- a/mod/wall_upload.php +++ b/mod/wall_upload.php @@ -222,7 +222,7 @@ function wall_upload_post(App $a, $desktopmode = true) { $width = $Image->getWidth(); $height = $Image->getHeight(); - $hash = photo_new_resource(); + $hash = Photo::newResource(); $smallest = 0; diff --git a/src/Model/Photo.php b/src/Model/Photo.php index e4e609155c..9f0007f8bc 100644 --- a/src/Model/Photo.php +++ b/src/Model/Photo.php @@ -100,7 +100,7 @@ class Photo if (x($photo['resource-id'])) { $hash = $photo['resource-id']; } else { - $hash = photo_new_resource(); + $hash = self::newResource(); } $photo_failure = false; @@ -267,4 +267,13 @@ class Photo $key = "photo_albums:".$uid.":".local_user().":".remote_user(); Cache::set($key, null, CACHE_DAY); } + + /** + * Generate a unique photo ID. + * + * @return string + */ + public static function newResource() { + return get_guid(32, false); + } } diff --git a/src/Model/User.php b/src/Model/User.php index eacd4ec33c..702e815e60 100644 --- a/src/Model/User.php +++ b/src/Model/User.php @@ -521,7 +521,7 @@ class User if ($Image->isValid()) { $Image->scaleToSquare(175); - $hash = photo_new_resource(); + $hash = Photo::newResource(); $r = Photo::store($Image, $uid, 0, $hash, $filename, L10n::t('Profile Photos'), 4); diff --git a/src/Object/Image.php b/src/Object/Image.php index d8d6dafc3c..2f4d55dc0a 100644 --- a/src/Object/Image.php +++ b/src/Object/Image.php @@ -937,7 +937,7 @@ class Image $width = $Image->getWidth(); $height = $Image->getHeight(); - $hash = photo_new_resource(); + $hash = Photo::newResource(); $smallest = 0;