From 51fcbbe7f1e4698826059081304e95c1d525444a Mon Sep 17 00:00:00 2001 From: Moritz Strohm Date: Fri, 21 Oct 2022 17:01:13 +0200 Subject: added initial code --- app/controllers/u.php | 45 +++++++++++++++ app/views/u/add.php | 3 + composer.json | 3 +- composer.lock | 77 +++++++++++++++++++++++++- db/migrations/5.3.1.2_add_short_urls_table.php | 33 +++++++++++ lib/models/ShortURL.php | 43 ++++++++++++++ templates/footer.php | 8 +++ 7 files changed, 209 insertions(+), 3 deletions(-) create mode 100644 app/controllers/u.php create mode 100644 app/views/u/add.php create mode 100644 db/migrations/5.3.1.2_add_short_urls_table.php create mode 100644 lib/models/ShortURL.php diff --git a/app/controllers/u.php b/app/controllers/u.php new file mode 100644 index 0000000..a952dc4 --- /dev/null +++ b/app/controllers/u.php @@ -0,0 +1,45 @@ +redirect($url->url); + } else { + throw new AccessDeniedException(_('Die Kurz-URL ist ungültig!')); + } + } + + + public function add_action() + { + $short_url = new ShortURL(); + $short_url->url = Request::get('from_url'); //TODO: Stud.IP-Pfad statt Server-Pfad + $short_url->user_id = $GLOBALS['user']->id; + $this->form = \Studip\Forms\Form::fromSORM( + $short_url, + [ + 'fields' => [ + 'alias' => [ + 'label' => _('URL-Bezeichnung'), + 'type' => 'text', + 'pattern' => '[a-záæäéèôøöü0-9\-]{4,256}' + ], + 'url' => [ + 'type' => 'hidden' + ] + ] + ] + ); + $this->form->autoStore(); + } +} diff --git a/app/views/u/add.php b/app/views/u/add.php new file mode 100644 index 0000000..ae35892 --- /dev/null +++ b/app/views/u/add.php @@ -0,0 +1,3 @@ + + render() ?> + diff --git a/composer.json b/composer.json index f3fc0a4..6e960fe 100644 --- a/composer.json +++ b/composer.json @@ -56,6 +56,7 @@ "symfony/process": "^5.4", "jumbojett/openid-connect-php": "^0.9.2", "league/oauth2-server": "^8.3", - "willdurand/negotiation": "^3.1" + "willdurand/negotiation": "^3.1", + "hashids/hashids": "^4.1" } } diff --git a/composer.lock b/composer.lock index 64a95d9..1b55e77 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "7a9043e1984ae4fb02e5872928486f0a", + "content-hash": "b51ee3affb9b9fa50c14c160dd62531c", "packages": [ { "name": "algo26-matthias/idna-convert", @@ -514,6 +514,76 @@ "time": "2017-03-20T17:10:46+00:00" }, { + "name": "hashids/hashids", + "version": "4.1.0", + "source": { + "type": "git", + "url": "https://github.com/vinkla/hashids.git", + "reference": "8cab111f78e0bd9c76953b082919fc9e251761be" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/vinkla/hashids/zipball/8cab111f78e0bd9c76953b082919fc9e251761be", + "reference": "8cab111f78e0bd9c76953b082919fc9e251761be", + "shasum": "" + }, + "require": { + "ext-mbstring": "*", + "php": "^7.2 || ^8.0" + }, + "require-dev": { + "phpunit/phpunit": "^8.0 || ^9.4", + "squizlabs/php_codesniffer": "^3.5" + }, + "suggest": { + "ext-bcmath": "Required to use BC Math arbitrary precision mathematics (*).", + "ext-gmp": "Required to use GNU multiple precision mathematics (*)." + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "4.1-dev" + } + }, + "autoload": { + "psr-4": { + "Hashids\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Ivan Akimov", + "email": "ivan@barreleye.com" + }, + { + "name": "Vincent Klaiber", + "email": "hello@doubledip.se" + } + ], + "description": "Generate short, unique, non-sequential ids (like YouTube and Bitly) from numbers", + "homepage": "https://hashids.org/php", + "keywords": [ + "bitly", + "decode", + "encode", + "hash", + "hashid", + "hashids", + "ids", + "obfuscate", + "youtube" + ], + "support": { + "issues": "https://github.com/vinkla/hashids/issues", + "source": "https://github.com/vinkla/hashids/tree/4.1.0" + }, + "time": "2020-11-26T19:24:33+00:00" + }, + { "name": "jakeasmith/http_build_url", "version": "1.0.1", "source": { @@ -7105,5 +7175,8 @@ "ext-dom": "*" }, "platform-dev": [], - "plugin-api-version": "2.3.0" + "platform-overrides": { + "php": "7.2.5" + }, + "plugin-api-version": "2.0.0" } diff --git a/db/migrations/5.3.1.2_add_short_urls_table.php b/db/migrations/5.3.1.2_add_short_urls_table.php new file mode 100644 index 0000000..dfa9eff --- /dev/null +++ b/db/migrations/5.3.1.2_add_short_urls_table.php @@ -0,0 +1,33 @@ +exec( + "CREATE TABLE short_urls ( + id int PRIMARY KEY NOT NULL AUTO_INCREMENT, + alias VARCHAR(256) UNIQUE NOT NULL DEFAULT '', + url VARCHAR(1024) NOT NULL, + user_id VARCHAR(32) NOT NULL DEFAULT '', + mkdate int(10) NOT NULL DEFAULT '0', + chdate int(10) NOT NULL DEFAULT '0' + )" + ); + } + + + public function down() + { + $db = DBManager::get(); + $db->exec("DROP TABLE short_urls"); + } +} diff --git a/lib/models/ShortURL.php b/lib/models/ShortURL.php new file mode 100644 index 0000000..3615e0b --- /dev/null +++ b/lib/models/ShortURL.php @@ -0,0 +1,43 @@ + User::class, + 'foreign_key' => 'user_id' + ]; + $config['registered_callbacks']['after_store'][] = 'cbGenerateAlias'; + + parent::configure($config); + } + + + public function cbGenerateAlias() + { + if (!$this->alias) { + //Generate the alias from the ID. + $hash_id = new Hashids($GLOBALS['UNI_NAME_CLEAN'], 8); + $this->alias = $hash_id->encode($this->id); + if ($this->isDirty()) { + $this->store(); + } + } + } +} diff --git a/templates/footer.php b/templates/footer.php index 86e0df3..71a37e7 100644 --- a/templates/footer.php +++ b/templates/footer.php @@ -36,6 +36,14 @@