]> git.mxchange.org Git - friendica.git/commitdiff
moved get_guid to System::createGUID
authorPhilipp Holzer <admin@philipp.info>
Mon, 9 Jul 2018 19:38:16 +0000 (21:38 +0200)
committerPhilipp Holzer <admin@philipp.info>
Mon, 9 Jul 2018 19:38:16 +0000 (21:38 +0200)
21 files changed:
boot.php
include/api.php
include/text.php
mod/item.php
mod/photos.php
mod/poke.php
mod/proxy.php
mod/subthread.php
mod/tagger.php
mod/wall_attach.php
src/Core/System.php
src/Model/Event.php
src/Model/Item.php
src/Model/Mail.php
src/Model/Photo.php
src/Protocol/Diaspora.php
tests/ApiTest.php
tests/DatabaseTest.php
tests/src/Core/Cache/CacheTest.php
tests/src/Core/Lock/LockTest.php
tests/src/Core/SystemTest.php [new file with mode: 0644]

index e1c5733704dc55b85cb30454e9ed626af04402f7..b45bc57670cde628999decffbaf90ebd8212cd54 100644 (file)
--- 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
index 42868aa9eb81566c5f2587dafcdca9f3b2db4158..dd8daa9e66b7bc9a9441f667acb88aaf93fb6ef3 100644 (file)
@@ -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;
index 0066e188145deb1c11d8158433162a08df28c419..0957d92d734fe0d284faf91812469b7af8365f84 100644 (file)
@@ -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)
                );
index a6a3a50c801644ab06b66f1bb16059f9821ebf6c..4e27a80cf64c5b957e1854e9dafdeafbf797b14a 100644 (file)
@@ -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', '');
 
index 34eea0c64296ac846759779685166d7e1a7bb416..d969fd32719c357eca92652b4d8e19a00523fa8b 100644 (file)
@@ -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;
index 55bb799d1fab3c3ff87a43913b54456d2678ee0c..2fd7068f78b8721e0b95660583eb02a92a35940d 100644 (file)
@@ -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);
index 0b84233e5f356088916aca39b59b0762438af06c..ca8ced3cd6751292dd06c2b3b0198d0ce0d0b06c 100644 (file)
@@ -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];
index e05aa5fec83588de0155dfabbe406cc91042f539..a624cd9a60be2715721da9b89c92ea71e72195bd 100644 (file)
@@ -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'];
index 717c0947769d47b9fb30bd6101332e4a90c22e47..0df31ac9a325d20cdeac2093bb4ff769c77917bc 100644 (file)
@@ -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'];
index 07092cf180f8163ca1b4812bf7cd8ade8fbd4604..29c790843a7dd8ef4513b08c7fe835735a1ea6eb 100644 (file)
@@ -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,
index 84a64ab9a8f4ee0d24d842eeb25d586c12bb49d1..058aca130dfc78b4a1bdf7d1878cf44bd868f198 100644 (file)
@@ -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 (!isset($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()
index 49d759585a20bc513d865014dd21e383638f42eb..66d5f389fe709f05583fd5efecb6748556e7d097 100644 (file)
@@ -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);
index f10c20936a4d04c779233567021e25a9ae870cdd..c077331728b777e04e6f750a8232aa0797bdc222 100644 (file)
@@ -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,
index 81494e7ebb4e15dbcc0e1f8f8fef0416dcef23b3..f10ad368ba4b02052d26c009975ee4fa4e803944 100644 (file)
@@ -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);
 
index c622421cc039e74d0bd298284a26cf8471fd65f2..b08c6f0459b8654fe0fdfa220255e24107674376 100644 (file)
@@ -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);
        }
 }
index 2ce54317eba82db136ed2beac6b423b8088c1a1f..ccd825d4e17bb2350a7152e81981780df057fd5a 100644 (file)
@@ -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"]];
 
index c8443512c8a461e417b5992c383a608e35f94809..a56290198398bb665de93caf9248a26881165488 100644 (file)
@@ -5,7 +5,6 @@
 
 namespace Friendica\Test;
 
-use Friendica\App;
 use Friendica\Core\Config;
 use Friendica\Core\PConfig;
 use Friendica\Network\BadRequestException;
@@ -26,13 +25,8 @@ class ApiTest extends DatabaseTest
         */
        protected function setUp()
        {
-               global $a;
                parent::setUp();
 
-               // Reusable App object
-               $this->app = new App(__DIR__.'/../');
-               $a = $this->app;
-
                // User data that the test database is populated with
                $this->selfUser = [
                        'id' => 42,
@@ -62,13 +56,6 @@ class ApiTest extends DatabaseTest
                        'authenticated' => true,
                        'uid' => $this->selfUser['id']
                ];
-
-               // Default config
-               Config::set('config', 'hostname', 'localhost');
-               Config::set('system', 'throttle_limit_day', 100);
-               Config::set('system', 'throttle_limit_week', 100);
-               Config::set('system', 'throttle_limit_month', 100);
-               Config::set('system', 'theme', 'system_theme');
        }
 
        /**
index e79e9237be9d3a9ce0f1031290cc96cdd23d89e5..04509724ac103b3aaf568a750992845cba68a986 100644 (file)
@@ -6,6 +6,8 @@
 namespace Friendica\Test;
 
 use dba;
+use Friendica\App;
+use Friendica\Core\Config;
 use Friendica\Database\DBStructure;
 use PHPUnit_Extensions_Database_DB_IDatabaseConnection;
 use PHPUnit\DbUnit\DataSet\YamlDataSet;
@@ -17,9 +19,35 @@ use PHPUnit\Framework\TestCase;
  */
 abstract class DatabaseTest extends TestCase
 {
+       /**
+        * @var \Friendica\App
+        */
+       protected $app;
 
        use TestCaseTrait;
 
+       /**
+        * Creates basic instances for testing with databases
+        *
+        * @throws \Exception
+        */
+       protected function setUp()
+       {
+               global $a;
+               parent::setUp();
+
+               // Reusable App object
+               $this->app = new App(__DIR__.'/../');
+               $a = $this->app;
+
+               // Default config
+               Config::set('config', 'hostname', 'localhost');
+               Config::set('system', 'throttle_limit_day', 100);
+               Config::set('system', 'throttle_limit_week', 100);
+               Config::set('system', 'throttle_limit_month', 100);
+               Config::set('system', 'theme', 'system_theme');
+       }
+
        /**
         * Renames an eventually existing .htconfig.php to .htconfig.php.tmp
         * Creates a new .htconfig.php for bin/worker.php execution
index 5be0e4b893dd7cba8d1c129fee858ed32d826902..bbbb6506a4c5b2977ba5c854c4f5bead77d2a2ba 100644 (file)
@@ -2,8 +2,6 @@
 
 namespace Friendica\Test\src\Core\Cache;
 
-use Friendica\App;
-use Friendica\Core\Config;
 use Friendica\Test\DatabaseTest;
 use Friendica\Util\DateTimeFormat;
 
@@ -18,20 +16,8 @@ abstract class CacheTest extends DatabaseTest
 
        protected function setUp()
        {
-               global $a;
                parent::setUp();
                $this->instance = $this->getInstance();
-
-               // Reusable App object
-               $this->app = new App(__DIR__.'/../');
-               $a = $this->app;
-
-               // Default config
-               Config::set('config', 'hostname', 'localhost');
-               Config::set('system', 'throttle_limit_day', 100);
-               Config::set('system', 'throttle_limit_week', 100);
-               Config::set('system', 'throttle_limit_month', 100);
-               Config::set('system', 'theme', 'system_theme');
        }
 
        function testSimple() {
index dafbd74a6ff19d1bc1371f6d9cf42ee76b19e19f..c8acd74067daa30d4d95a7d12e6f2fb4dbeba60c 100644 (file)
@@ -2,10 +2,7 @@
 
 namespace Friendica\Test\src\Core\Lock;
 
-use Friendica\App;
-use Friendica\Core\Config;
 use Friendica\Test\DatabaseTest;
-use PHPUnit\Framework\TestCase;
 
 abstract class LockTest extends DatabaseTest
 {
@@ -18,20 +15,8 @@ abstract class LockTest extends DatabaseTest
 
        protected function setUp()
        {
-               global $a;
                parent::setUp();
                $this->instance = $this->getInstance();
-
-               // Reusable App object
-               $this->app = new App(__DIR__.'/../');
-               $a = $this->app;
-
-               // Default config
-               Config::set('config', 'hostname', 'localhost');
-               Config::set('system', 'throttle_limit_day', 100);
-               Config::set('system', 'throttle_limit_week', 100);
-               Config::set('system', 'throttle_limit_month', 100);
-               Config::set('system', 'theme', 'system_theme');
        }
 
        public function testLock() {
diff --git a/tests/src/Core/SystemTest.php b/tests/src/Core/SystemTest.php
new file mode 100644 (file)
index 0000000..189e880
--- /dev/null
@@ -0,0 +1,25 @@
+<?php
+
+namespace Friendica\Test\src\Core;
+
+use Friendica\Core\System;
+use PHPUnit\Framework\TestCase;
+
+class SystemTest extends TestCase
+{
+       private function assertGuid($guid, $length)
+       {
+               $this->assertRegExp("/^[a-z0-9]{" . $length . "}?$/", $guid);
+       }
+
+       function testGuidWithoutParameter()
+       {
+               $guid = System::createGUID();
+               $this->assertGuid($guid, 16);
+       }
+
+       function testGuidWithSize() {
+               $guid = System::createGUID(20);
+               $this->assertGuid($guid, 20);
+       }
+}
\ No newline at end of file