Skip to content

Instantly share code, notes, and snippets.

View juanparati's full-sized avatar

Juan Lago juanparati

  • Aarhus (Denmark)
View GitHub Profile
@juanparati
juanparati / mariadb_uuidv7.sql
Last active March 2, 2024 19:38
UUID7 generation function for MariaDB
DELIMITER $$
CREATE DEFINER = CURRENT_USER FUNCTION `uuidv7` () RETURNS UUID LANGUAGE SQL NOT DETERMINISTIC NO SQL SQL SECURITY DEFINER
COMMENT 'Generate a UUID7 according to https://www.ietf.org/archive/id/draft-peabody-dispatch-new-uuid-format-04.html#variant_and_version_fields'
BEGIN
-- Obtain date with 4 milliseconds precision
SET @now = sysdate(4);
/*
UNIX_TIMESTAMP returns a 32bits number, so this function will not work after the Epochalypse, I am crossing my fingers and I hope that MySQL/MariaDB teams will move to 64bits timestamps before than 2038.
*/
SET @time_sec = LPAD(HEX(TRUNCATE(UNIX_TIMESTAMP(@now), 0)), 9, '0');
@juanparati
juanparati / StorageOverride.ts
Last active May 1, 2023 12:43
Avoid Session and Local storage quota error overriding the Storage prototype
/**
* Override Localstorage and Sessionstorage setItem prototype
*/
Storage.prototype._setItem = Storage.prototype.setItem;
Storage.prototype.setItem = function(key, value)
{
const isQuotaExceededError = (err: unknown) => err instanceof DOMException &&
(
// Legacy Webkit
@juanparati
juanparati / colorize.php
Created December 1, 2021 10:31
Color helper function
/**
* Colorize helper.
*/
class Colorize
{
/**
* RGB array for a color.
*
location /foo/ {
proxy_pass https://site.com/;
proxy_redirect off;
proxy_set_header Host site.com;
proxy_ssl_session_reuse off;
proxy_ssl_server_name on;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Accept-Encoding "";
sub_filter "https://site.com" "https://reflected.to/foo";
@juanparati
juanparati / csvreader.php
Created February 19, 2018 10:54
CSV reader that support field maps
<?php
/**
* Class Model_CSVReader
*
* Parse and read CSV files as a stream keeping a low memory footprint
*/
class Model_CSVReader
{
@juanparati
juanparati / memory_size_convert.php
Last active January 16, 2018 22:08
Convert human readable memory size Like a Pro™️
<?php
function convertMemorySize($strval, string $to_unit = 'b')
{
$strval = strtolower(str_replace(' ', '', $strval));
$val = floatval($strval);
$to_unit = strtolower(trim($to_unit))[0];
$from_unit = str_replace($val, '', $strval);
$from_unit = empty($from_unit) ? 'b' : trim($from_unit)[0];
$units = 'kmgtph'; // (k)ilobyte, (m)egabyte, (g)igabyte and so on...
@juanparati
juanparati / simpleupload.vue
Created October 20, 2017 13:32
Vue 2.x Simple Upload component
// Simple upload from http://laravel-tricks.com/tricks/vuejs-simple-upload
<template>
<div>
<small>Change/Upload</small>
<input type="file" @change="onFileChange">
<button class="btn btn-success btn-xs" @click="upload">Upload</button>
</div>
</template>
@juanparati
juanparati / mysql_laravel_decrypt.sql
Created October 12, 2017 11:46
Laravel decrypt implementation as MySQL function
CREATE DEFINER=`%`@`%` FUNCTION `LARAVEL_DECRYPT`(
`encstr` TEXT,
`raw_key` VARCHAR(255)
)
RETURNS varchar(255) CHARSET utf8 COLLATE utf8_unicode_ci
LANGUAGE SQL
DETERMINISTIC
READS SQL DATA
SQL SECURITY DEFINER
@juanparati
juanparati / gist:ceb20f45d81f0a2c9a1b97bd0597d07f
Last active July 4, 2017 10:46
Laravel AES-256-CBC decryption implemented directly on MYSQL
-- Direct SELECT version
SELECT
FROM_BASE64(JSON_UNQUOTE(JSON_EXTRACT(CONVERT(FROM_BASE64(email) USING utf8), '$.iv'))) AS iv,
JSON_UNQUOTE(JSON_EXTRACT(CONVERT(FROM_BASE64(email) USING utf8), '$.value')) AS value,
FROM_BASE64('mysecretkey') AS pkey,
AES_DECRYPT(FROM_BASE64(JSON_UNQUOTE(JSON_EXTRACT(CONVERT(FROM_BASE64(email) USING utf8), '$.value'))), FROM_BASE64('mysecretkey'), FROM_BASE64(JSON_UNQUOTE(JSON_EXTRACT(CONVERT(FROM_BASE64(email) USING utf8), '$.iv')))) AS decrypted
-- Function version
CREATE FUNCTION `LARAVEL_DECRYPT`(
@juanparati
juanparati / colors.php
Last active June 6, 2017 07:44
Color palette generator
/**
* Class Colors.
* Pallete Generator Helper.
*/
class Colors implements \Iterator, \ArrayAccess
{
const COL_MIN_AVG = 64;
const COL_MAX_AVG = 192;