1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 00:41:34 +02:00
invoiceninja/app/Utils/Traits/MakesHash.php

92 lines
2.1 KiB
PHP
Raw Normal View History

<?php
2019-05-11 05:32:07 +02:00
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2019. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Utils\Traits;
use App\Libraries\MultiDB;
use Hashids\Hashids;
/**
* Class MakesHash
* @package App\Utils\Traits
*/
trait MakesHash
{
/**
* Creates a simple alphanumeric Hash
* @return string - asd89f7as89df6asf78as6fds
*/
public function createHash() : string
{
2019-04-24 04:34:39 +02:00
return str_random(config('ninja.key_length'));
}
/**
* Creates a simple alphanumeric Hash which is prepended with a encoded database prefix
2019-04-18 07:01:40 +02:00
*
* @param $db - Full database name
* @return string 01-asfas8df76a78f6a78dfsdf
*/
public function createDbHash($db) : string
{
2019-04-24 04:34:39 +02:00
return $this->getDbCode($db) . '-' . str_random(config('ninja.key_length'));
}
/**
* @param $db - Full database name
* @return string - hashed and encoded int 01,02,03,04
*/
public function getDbCode($db) : string
{
2019-09-22 12:25:05 +02:00
$hashids = new Hashids('', 10);
return $hashids->encode( str_replace( MultiDB::DB_PREFIX, "", $db ) );
}
public function encodePrimaryKey($value) : string
{
2019-09-22 12:25:05 +02:00
$hashids = new Hashids('', 10);
return $hashids->encode($value);
}
public function decodePrimaryKey($value) : string
{
2019-04-03 03:17:21 +02:00
try{
2019-09-22 12:25:05 +02:00
$hashids = new Hashids('', 10);
2019-04-03 03:17:21 +02:00
$decoded_array = $hashids->decode($value);
2019-04-03 03:17:21 +02:00
return $decoded_array[0];
}
catch(\Exception $e)
{
return response()->json(['error'=>'Invalid primary key'],400);
}
}
2019-06-12 06:22:05 +02:00
public function transformKeys($keys)
{
if(is_array($keys))
{
foreach($keys as &$value)
{
$value = $this->decodePrimaryKey($value);
}
return $keys;
}
else
return $this->decodePrimaryKey($keys);
}
}