Improved resource id creation

This commit is contained in:
Michael 2018-02-20 10:02:07 +00:00
parent d90b93cfda
commit a3af6807ac
9 changed files with 23 additions and 37 deletions

View file

@ -854,11 +854,13 @@ function check_addons(App $a)
return; 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(); $a = get_app();
$prefix = hash("crc32", $a->get_hostname()); $prefix = hash('crc32', $a->get_hostname());
} }
while (strlen($prefix) < ($size - 13)) { while (strlen($prefix) < ($size - 13)) {
@ -867,10 +869,10 @@ function get_guid($size = 16, $prefix = "")
if ($size >= 24) { if ($size >= 24) {
$prefix = substr($prefix, 0, $size - 22); $prefix = substr($prefix, 0, $size - 22);
return(str_replace(".", "", uniqid($prefix, true))); return str_replace('.', '', uniqid($prefix, true));
} else { } else {
$prefix = substr($prefix, 0, max($size - 13, 0)); $prefix = substr($prefix, 0, max($size - 13, 0));
return(uniqid($prefix)); return uniqid($prefix);
} }
} }

View file

@ -4614,7 +4614,7 @@ function save_media_to_database($mediatype, $media, $type, $album, $allow_cid, $
$height = $Image->getHeight(); $height = $Image->getHeight();
// create a new resource-id if not already provided // 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") { if ($mediatype == "photo") {
// upload normal image (scales 0, 1, 2) // upload normal image (scales 0, 1, 2)

View file

@ -489,31 +489,6 @@ function item_new_uri($hostname, $uid, $guid = "") {
return $uri; 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 * @deprecated
* wrapper to load a view template, checking for alternate * wrapper to load a view template, checking for alternate

View file

@ -838,7 +838,7 @@ function photos_post(App $a)
$smallest = 0; $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); $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);

View file

@ -308,7 +308,7 @@ function profile_photo_crop_ui_head(App $a, Image $Image) {
$height = $Image->getHeight(); $height = $Image->getHeight();
} }
$hash = photo_new_resource(); $hash = Photo::newResource();
$smallest = 0; $smallest = 0;

View file

@ -222,7 +222,7 @@ function wall_upload_post(App $a, $desktopmode = true) {
$width = $Image->getWidth(); $width = $Image->getWidth();
$height = $Image->getHeight(); $height = $Image->getHeight();
$hash = photo_new_resource(); $hash = Photo::newResource();
$smallest = 0; $smallest = 0;

View file

@ -100,7 +100,7 @@ class Photo
if (x($photo['resource-id'])) { if (x($photo['resource-id'])) {
$hash = $photo['resource-id']; $hash = $photo['resource-id'];
} else { } else {
$hash = photo_new_resource(); $hash = self::newResource();
} }
$photo_failure = false; $photo_failure = false;
@ -267,4 +267,13 @@ class Photo
$key = "photo_albums:".$uid.":".local_user().":".remote_user(); $key = "photo_albums:".$uid.":".local_user().":".remote_user();
Cache::set($key, null, CACHE_DAY); Cache::set($key, null, CACHE_DAY);
} }
/**
* Generate a unique photo ID.
*
* @return string
*/
public static function newResource() {
return get_guid(32, false);
}
} }

View file

@ -521,7 +521,7 @@ class User
if ($Image->isValid()) { if ($Image->isValid()) {
$Image->scaleToSquare(175); $Image->scaleToSquare(175);
$hash = photo_new_resource(); $hash = Photo::newResource();
$r = Photo::store($Image, $uid, 0, $hash, $filename, L10n::t('Profile Photos'), 4); $r = Photo::store($Image, $uid, 0, $hash, $filename, L10n::t('Profile Photos'), 4);

View file

@ -937,7 +937,7 @@ class Image
$width = $Image->getWidth(); $width = $Image->getWidth();
$height = $Image->getHeight(); $height = $Image->getHeight();
$hash = photo_new_resource(); $hash = Photo::newResource();
$smallest = 0; $smallest = 0;