2018-10-26 06:53:29 +02:00
|
|
|
<?php
|
2019-05-11 05:32:07 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2019-05-11 05:32:07 +02:00
|
|
|
*/
|
2018-10-26 06:53:29 +02:00
|
|
|
|
|
|
|
namespace App\Utils\Traits;
|
|
|
|
|
|
|
|
use App\Libraries\MultiDB;
|
|
|
|
use Hashids\Hashids;
|
2020-10-28 11:10:49 +01:00
|
|
|
use Illuminate\Support\Str;
|
2018-10-26 06:53:29 +02:00
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Class MakesHash.
|
2018-10-26 06:53:29 +02:00
|
|
|
*/
|
|
|
|
trait MakesHash
|
|
|
|
{
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Creates a simple alphanumeric Hash.
|
2018-10-26 06:53:29 +02:00
|
|
|
* @return string - asd89f7as89df6asf78as6fds
|
|
|
|
*/
|
|
|
|
public function createHash() : string
|
|
|
|
{
|
2020-10-28 11:10:49 +01:00
|
|
|
return Str::random(config('ninja.key_length'));
|
2018-10-26 06:53:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Creates a simple alphanumeric Hash which is prepended with a encoded database prefix.
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
2018-10-26 06:53:29 +02:00
|
|
|
* @param $db - Full database name
|
|
|
|
* @return string 01-asfas8df76a78f6a78dfsdf
|
|
|
|
*/
|
|
|
|
public function createDbHash($db) : string
|
|
|
|
{
|
2021-10-14 09:03:03 +02:00
|
|
|
if(config('ninja.db.multi_db_enabled'))
|
|
|
|
return $this->getDbCode($db).'-'. Str::random(config('ninja.key_length'));
|
|
|
|
|
|
|
|
return Str::random(config('ninja.key_length'));
|
2018-10-26 06:53:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $db - Full database name
|
|
|
|
* @return string - hashed and encoded int 01,02,03,04
|
|
|
|
*/
|
|
|
|
public function getDbCode($db) : string
|
|
|
|
{
|
2020-03-18 10:40:15 +01:00
|
|
|
$hashids = new Hashids(config('ninja.hash_salt'), 10);
|
2018-10-26 06:53:29 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
return $hashids->encode(str_replace(MultiDB::DB_PREFIX, '', $db));
|
2018-10-26 06:53:29 +02:00
|
|
|
}
|
2018-11-02 11:54:46 +01:00
|
|
|
|
2018-11-27 08:24:26 +01:00
|
|
|
public function encodePrimaryKey($value) : string
|
2018-11-02 11:54:46 +01:00
|
|
|
{
|
2020-03-18 10:40:15 +01:00
|
|
|
$hashids = new Hashids(config('ninja.hash_salt'), 10);
|
2018-11-02 11:54:46 +01:00
|
|
|
|
|
|
|
return $hashids->encode($value);
|
|
|
|
}
|
|
|
|
|
2018-11-27 08:24:26 +01:00
|
|
|
public function decodePrimaryKey($value) : string
|
2018-11-02 11:54:46 +01:00
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
try {
|
2020-03-18 10:40:15 +01:00
|
|
|
$hashids = new Hashids(config('ninja.hash_salt'), 10);
|
2018-11-02 11:54:46 +01:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$decoded_array = $hashids->decode($value);
|
2018-11-02 11:54:46 +01:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! is_array($decoded_array)) {
|
2020-11-01 06:09:09 +01:00
|
|
|
throw new \Exception("Invalid Primary Key");
|
2020-10-28 11:10:49 +01:00
|
|
|
//response()->json(['error'=>'Invalid primary key'], 400);
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2019-04-03 03:17:21 +02:00
|
|
|
return $decoded_array[0];
|
2019-12-30 22:59:12 +01:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
return response()->json(['error'=>'Invalid primary key'], 400);
|
2019-04-03 03:17:21 +02:00
|
|
|
}
|
2018-11-02 11:54:46 +01:00
|
|
|
}
|
2019-06-12 06:22:05 +02:00
|
|
|
|
|
|
|
public function transformKeys($keys)
|
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
if (is_array($keys)) {
|
|
|
|
foreach ($keys as &$value) {
|
|
|
|
$value = $this->decodePrimaryKey($value);
|
2019-06-12 06:22:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $keys;
|
2019-12-30 22:59:12 +01:00
|
|
|
} else {
|
2019-06-12 06:22:05 +02:00
|
|
|
return $this->decodePrimaryKey($keys);
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-06-12 06:22:05 +02:00
|
|
|
}
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|