diff --git a/boot.php b/boot.php index e1c5733704..b45bc57670 100644 --- a/boot.php +++ b/boot.php @@ -835,28 +835,6 @@ function check_addons(App $a) return; } -function get_guid($size = 16, $prefix = '') -{ - if (is_bool($prefix) && !$prefix) { - $prefix = ''; - } elseif ($prefix == '') { - $a = get_app(); - $prefix = hash('crc32', $a->get_hostname()); - } - - while (strlen($prefix) < ($size - 13)) { - $prefix .= mt_rand(); - } - - if ($size >= 24) { - $prefix = substr($prefix, 0, $size - 22); - return str_replace('.', '', uniqid($prefix, true)); - } else { - $prefix = substr($prefix, 0, max($size - 13, 0)); - return uniqid($prefix); - } -} - /** * @brief Used to end the current process, after saving session state. * @deprecated diff --git a/include/api.php b/include/api.php index 42868aa9eb..dd8daa9e66 100644 --- a/include/api.php +++ b/include/api.php @@ -4534,7 +4534,7 @@ function post_photo_item($hash, $allow_cid, $deny_cid, $allow_gid, $deny_gid, $f $owner_record = q("SELECT * FROM `contact` WHERE `uid`= %d AND `self` LIMIT 1", intval(api_user())); $arr = []; - $arr['guid'] = get_guid(32); + $arr['guid'] = System::createGUID(32); $arr['uid'] = intval(api_user()); $arr['uri'] = $uri; $arr['parent-uri'] = $uri; diff --git a/include/text.php b/include/text.php index 0066e18814..0957d92d73 100644 --- a/include/text.php +++ b/include/text.php @@ -1549,7 +1549,7 @@ function return_bytes($size_str) { function generate_user_guid() { $found = true; do { - $guid = get_guid(32); + $guid = System::createGUID(32); $x = q("SELECT `uid` FROM `user` WHERE `guid` = '%s' LIMIT 1", dbesc($guid) ); diff --git a/mod/item.php b/mod/item.php index a6a3a50c80..4e27a80cf6 100644 --- a/mod/item.php +++ b/mod/item.php @@ -232,7 +232,7 @@ function item_post(App $a) { $emailcc = notags(trim(defaults($_REQUEST, 'emailcc' , ''))); $body = escape_tags(trim(defaults($_REQUEST, 'body' , ''))); $network = notags(trim(defaults($_REQUEST, 'network' , NETWORK_DFRN))); - $guid = get_guid(32); + $guid = System::createGUID(32); $postopts = defaults($_REQUEST, 'postopts', ''); diff --git a/mod/photos.php b/mod/photos.php index 34eea0c642..d969fd3271 100644 --- a/mod/photos.php +++ b/mod/photos.php @@ -453,7 +453,7 @@ function photos_post(App $a) $uri = Item::newURI($page_owner_uid); $arr = []; - $arr['guid'] = get_guid(32); + $arr['guid'] = System::createGUID(32); $arr['uid'] = $page_owner_uid; $arr['uri'] = $uri; $arr['parent-uri'] = $uri; @@ -620,7 +620,7 @@ function photos_post(App $a) $uri = Item::newURI($page_owner_uid); $arr = []; - $arr['guid'] = get_guid(32); + $arr['guid'] = System::createGUID(32); $arr['uid'] = $page_owner_uid; $arr['uri'] = $uri; $arr['parent-uri'] = $uri; @@ -856,7 +856,7 @@ function photos_post(App $a) $arr['coord'] = $lat . ' ' . $lon; } - $arr['guid'] = get_guid(32); + $arr['guid'] = System::createGUID(32); $arr['uid'] = $page_owner_uid; $arr['uri'] = $uri; $arr['parent-uri'] = $uri; diff --git a/mod/poke.php b/mod/poke.php index 55bb799d1f..2fd7068f78 100644 --- a/mod/poke.php +++ b/mod/poke.php @@ -96,7 +96,7 @@ function poke_init(App $a) { $arr = []; - $arr['guid'] = get_guid(32); + $arr['guid'] = System::createGUID(32); $arr['uid'] = $uid; $arr['uri'] = $uri; $arr['parent-uri'] = ($parent_uri ? $parent_uri : $uri); diff --git a/mod/proxy.php b/mod/proxy.php index 0b84233e5f..ca8ced3cd6 100644 --- a/mod/proxy.php +++ b/mod/proxy.php @@ -188,7 +188,7 @@ function proxy_init(App $a) { die(); } - $fields = ['uid' => 0, 'contact-id' => 0, 'guid' => get_guid(), 'resource-id' => $urlhash, 'created' => DateTimeFormat::utcNow(), 'edited' => DateTimeFormat::utcNow(), + $fields = ['uid' => 0, 'contact-id' => 0, 'guid' => System::createGUID(), 'resource-id' => $urlhash, 'created' => DateTimeFormat::utcNow(), 'edited' => DateTimeFormat::utcNow(), 'filename' => basename($_REQUEST['url']), 'type' => '', 'album' => '', 'height' => imagesy($image), 'width' => imagesx($image), 'datasize' => 0, 'data' => $img_str, 'scale' => 100, 'profile' => 0, 'allow_cid' => '', 'allow_gid' => '', 'deny_cid' => '', 'deny_gid' => '', 'desc' => $mime]; diff --git a/mod/subthread.php b/mod/subthread.php index e05aa5fec8..a624cd9a60 100644 --- a/mod/subthread.php +++ b/mod/subthread.php @@ -118,7 +118,7 @@ EOT; $arr = []; - $arr['guid'] = get_guid(32); + $arr['guid'] = System::createGUID(32); $arr['uri'] = $uri; $arr['uid'] = $owner_uid; $arr['contact-id'] = $contact['id']; diff --git a/mod/tagger.php b/mod/tagger.php index 717c094776..0df31ac9a3 100644 --- a/mod/tagger.php +++ b/mod/tagger.php @@ -115,7 +115,7 @@ EOT; $arr = []; - $arr['guid'] = get_guid(32); + $arr['guid'] = System::createGUID(32); $arr['uri'] = $uri; $arr['uid'] = $owner_uid; $arr['contact-id'] = $contact['id']; diff --git a/mod/wall_attach.php b/mod/wall_attach.php index 07092cf180..29c790843a 100644 --- a/mod/wall_attach.php +++ b/mod/wall_attach.php @@ -6,6 +6,7 @@ use Friendica\App; use Friendica\Core\Config; use Friendica\Core\L10n; +use Friendica\Core\System; use Friendica\Database\DBM; use Friendica\Util\DateTimeFormat; use Friendica\Util\Mimetype; @@ -121,7 +122,7 @@ function wall_attach_post(App $a) { $filedata = @file_get_contents($src); $mimetype = Mimetype::getContentType($filename); - $hash = get_guid(64); + $hash = System::createGUID(64); $created = DateTimeFormat::utcNow(); $fields = ['uid' => $page_owner_uid, 'hash' => $hash, 'filename' => $filename, 'filetype' => $mimetype, diff --git a/src/Core/System.php b/src/Core/System.php index 84a64ab9a8..46d1b34e55 100644 --- a/src/Core/System.php +++ b/src/Core/System.php @@ -175,9 +175,36 @@ EOT; killme(); } + /** + * Generates a GUID with the given parameters + * + * @param int $size The size of the GUID (default is 16) + * @param bool|string $prefix A given prefix (default is empty) + * @return string a generated GUID + */ + public static function createGUID($size = 16, $prefix = '') + { + if (is_bool($prefix) && !$prefix) { + $prefix = ''; + } elseif (empty($prefix)) { + $prefix = hash('crc32', self::getApp()->get_hostname()); + } + + while (strlen($prefix) < ($size - 13)) { + $prefix .= mt_rand(); + } + + if ($size >= 24) { + $prefix = substr($prefix, 0, $size - 22); + return str_replace('.', '', uniqid($prefix, true)); + } else { + $prefix = substr($prefix, 0, max($size - 13, 0)); + return uniqid($prefix); + } + } + /// @todo Move the following functions from boot.php /* - function get_guid($size = 16, $prefix = "") function killme() function goaway($s) function local_user() diff --git a/src/Model/Event.php b/src/Model/Event.php index 49d759585a..66d5f389fe 100644 --- a/src/Model/Event.php +++ b/src/Model/Event.php @@ -305,7 +305,7 @@ class Event extends BaseObject Addon::callHooks('event_updated', $event['id']); } else { - $event['guid'] = get_guid(32); + $event['guid'] = System::createGUID(32); // New event. Store it. dba::insert('event', $event); diff --git a/src/Model/Item.php b/src/Model/Item.php index f10c20936a..c077331728 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -1075,7 +1075,7 @@ class Item extends BaseObject } elseif (!empty($item['uri'])) { $guid = self::guidFromUri($item['uri'], $prefix_host); } else { - $guid = get_guid(32, hash('crc32', $prefix_host)); + $guid = System::createGUID(32, hash('crc32', $prefix_host)); } return $guid; @@ -2172,7 +2172,7 @@ class Item extends BaseObject public static function newURI($uid, $guid = "") { if ($guid == "") { - $guid = get_guid(32); + $guid = System::createGUID(32); } $hostname = self::getApp()->get_hostname(); @@ -2496,7 +2496,7 @@ class Item extends BaseObject } if ($contact['network'] != NETWORK_FEED) { - $datarray["guid"] = get_guid(32); + $datarray["guid"] = System::createGUID(32); unset($datarray["plink"]); $datarray["uri"] = self::newURI($contact['uid'], $datarray["guid"]); $datarray["parent-uri"] = $datarray["uri"]; @@ -2933,7 +2933,7 @@ class Item extends BaseObject $objtype = $item['resource-id'] ? ACTIVITY_OBJ_IMAGE : ACTIVITY_OBJ_NOTE ; $new_item = [ - 'guid' => get_guid(32), + 'guid' => System::createGUID(32), 'uri' => self::newURI($item['uid']), 'uid' => $item['uid'], 'contact-id' => $item_contact_id, diff --git a/src/Model/Mail.php b/src/Model/Mail.php index 81494e7ebb..f10ad368ba 100644 --- a/src/Model/Mail.php +++ b/src/Model/Mail.php @@ -47,7 +47,7 @@ class Mail return -2; } - $guid = get_guid(32); + $guid = System::createGUID(32); $uri = 'urn:X-dfrn:' . System::baseUrl() . ':' . local_user() . ':' . $guid; $convid = 0; @@ -76,7 +76,7 @@ class Mail $recip_handle = (($contact['addr']) ? $contact['addr'] : $contact['nick'] . '@' . $recip_host); $sender_handle = $a->user['nickname'] . '@' . substr(System::baseUrl(), strpos(System::baseUrl(), '://') + 3); - $conv_guid = get_guid(32); + $conv_guid = System::createGUID(32); $convuri = $recip_handle . ':' . $conv_guid; $handles = $recip_handle . ';' . $sender_handle; @@ -174,7 +174,7 @@ class Mail $subject = L10n::t('[no subject]'); } - $guid = get_guid(32); + $guid = System::createGUID(32); $uri = 'urn:X-dfrn:' . System::baseUrl() . ':' . local_user() . ':' . $guid; $me = Probe::uri($replyto); @@ -183,7 +183,7 @@ class Mail return -2; } - $conv_guid = get_guid(32); + $conv_guid = System::createGUID(32); $recip_handle = $recipient['nickname'] . '@' . substr(System::baseUrl(), strpos(System::baseUrl(), '://') + 3); diff --git a/src/Model/Photo.php b/src/Model/Photo.php index c622421cc0..b08c6f0459 100644 --- a/src/Model/Photo.php +++ b/src/Model/Photo.php @@ -45,7 +45,7 @@ class Photo if (DBM::is_result($photo)) { $guid = $photo['guid']; } else { - $guid = get_guid(); + $guid = System::createGUID(); } $existing_photo = dba::selectFirst('photo', ['id'], ['resource-id' => $rid, 'uid' => $uid, 'contact-id' => $cid, 'scale' => $scale]); @@ -275,6 +275,6 @@ class Photo */ public static function newResource() { - return get_guid(32, false); + return system::createGUID(32, false); } } diff --git a/src/Protocol/Diaspora.php b/src/Protocol/Diaspora.php index 2ce54317eb..ccd825d4e1 100644 --- a/src/Protocol/Diaspora.php +++ b/src/Protocol/Diaspora.php @@ -3220,7 +3220,7 @@ class Diaspora $author = self::myHandle($owner); $message = ["author" => $author, - "guid" => get_guid(32), + "guid" => System::createGUID(32), "parent_type" => "Post", "parent_guid" => $item["guid"]]; diff --git a/tests/src/Core/SystemTest.php b/tests/src/Core/SystemTest.php new file mode 100644 index 0000000000..b85bce44cb --- /dev/null +++ b/tests/src/Core/SystemTest.php @@ -0,0 +1,37 @@ +assertRegExp("/^" . $prefix . "[a-z0-9]{" . $length . "}?$/", $guid); + } + + function testGuidWithoutParameter() + { + $guid = System::createGUID(); + $this->assertGuid($guid, 16); + } + + function testGuidWithSize32() { + $guid = System::createGUID(32); + $this->assertGuid($guid, 32); + } + + function testGuidWithSize64() { + $guid = System::createGUID(64); + $this->assertGuid($guid, 64); + } + + function testGuidWithPrefix() { + $guid = System::createGUID(23, 'test'); + $this->assertGuid($guid, 23, 'test'); + } +} \ No newline at end of file