Skip to content

Instantly share code, notes, and snippets.

View Sommerregen's full-sized avatar

Sommerregen Sommerregen

View GitHub Profile
@Sommerregen
Sommerregen / htmlfilter.php
Created January 19, 2018 19:16
filter html user input allowing only specifics tags and attributes
<?php
// filter html user input allowing only specifics tags and attributes
// XSS - You shall not pass ! ;)
$html = 'TEST <div>Hello PHP/SQL developers, <img src="https://foxorm.com/img/foxorm.png" height="24" width="24" on-click="alert(\'javascript injection try\');"><a href="https://foxorm.com">FoxORM</a> is <b>awesome</b> !
<a href="javascript:alert(\'another javascript injection try\');"> !!! </a>
<script>window.location = http://xss-injection.hack; </script></div> IFY';
$securisedHtml = htmlfilter($html, [
'*'=>[
@Sommerregen
Sommerregen / multi_curl.php
Created February 10, 2017 14:18
Asynchronous multi Curl request without blocking and rolling window (see https://www.onlineaspect.com/2009/01/26/how-to-use-curl_multi-without-blocking/)
<?php
function rolling_curl($urls, $callback, $custom_options = null) {
// make sure the rolling window isn't greater than the # of urls
$rolling_window = 5;
$rolling_window = (sizeof($urls) < $rolling_window) ? sizeof($urls) : $rolling_window;
$master = curl_multi_init();
$curl_arr = array();
// add additional curl options here
$std_options = array(CURLOPT_RETURNTRANSFER => true,
@Sommerregen
Sommerregen / _decimal.scss
Created April 28, 2016 20:30 — forked from terkel/_decimal.scss
Rounding decimals in Sass
// _decimal.scss | MIT License | gist.github.com/terkel/4373420
// Round a number to specified digits.
//
// @param {Number} $number A number to round
// @param {Number} [$digits:0] Digits to output
// @param {String} [$mode:round] (round|ceil|floor) How to round a number
// @return {Number} A rounded number
// @example
// decimal-round(0.333) => 0
@Sommerregen
Sommerregen / pbkdf2.php
Created March 22, 2016 21:19
PBKDF2 (Password-Based Key Derivation Function) is a key stretching algorithm. It can be used to hash passwords in a computationally intensive manner, so that dictionary and brute-force attacks are less effective. See CrackStation's Hashing Security Article for instructions on implementing salted password hashing. See https://defuse.ca/php-pbkdf…
<?php
/*
* PBKDF2 key derivation function as defined by RSA's PKCS #5: https://www.ietf.org/rfc/rfc2898.txt
* $algorithm - The hash algorithm to use. Recommended: SHA256
* $password - The password.
* $salt - A salt that is unique to the password.
* $count - Iteration count. Higher is better, but slower. Recommended: At least 1000.
* $key_length - The length of the derived key in bytes.
* $raw_output - If true, the key is returned in raw binary format. Hex encoded otherwise.
* Returns: A $key_length-byte key derived from the password and salt.
@Sommerregen
Sommerregen / UUID.php
Created March 22, 2016 21:15
UUID class
<?php
/**
* UUID class
*
* The following class generates VALID RFC 4122 COMPLIANT
* Universally Unique IDentifiers (UUID) version 3, 4 and 5.
*
* UUIDs generated validates using OSSP UUID Tool, and output
* for named-based UUIDs are exactly the same. This is a pure
* PHP implementation.
<?php
/**
* PseudoCrypt by KevBurns (http://blog.kevburnsjr.com/php-unique-hash)
* Reference/source: http://stackoverflow.com/a/1464155/933782
*
* I want a short alphanumeric hash that’s unique and who’s sequence is difficult to deduce.
* I could run it out to md5 and trim the first n chars but that’s not going to be very unique.
* Storing a truncated checksum in a unique field means that the frequency of collisions will increase
* geometrically as the number of unique keys for a base 62 encoded integer approaches 62^n.
* I’d rather do it right than code myself a timebomb. So I came up with this.
@Sommerregen
Sommerregen / alphaID.js
Created March 22, 2016 21:12
Create Youtube-Like IDs With PHP/Python/Javascript/Java/SQL (source: http://kvz.io/blog/2009/06/10/create-short-ids-with-php-like-youtube-or-tinyurl/ )
/**
* Javascript AlphabeticID class
* (based on a script by Kevin van Zonneveld <kevin@vanzonneveld.net>)
*
* Author: Even Simon <even.simon@gmail.com>
*
* Description: Translates a numeric identifier into a short string and backwords.
*
* Usage:
* var str = AlphabeticID.encode(9007199254740989); // str = 'fE2XnNGpF'
@Sommerregen
Sommerregen / php-html-css-js-minifier.php
Created December 14, 2015 08:38 — forked from taufik-nurrohman/php-html-css-js-minifier.php
PHP Function to Minify HTML, CSS and JavaScript
<?php
/**
* -----------------------------------------------------------------------------------------
* Based on `https://github.com/mecha-cms/mecha-cms/blob/master/system/kernel/converter.php`
* -----------------------------------------------------------------------------------------
*/
// HTML Minifier
function minify_html($input) {