Skip to content

Instantly share code, notes, and snippets.

View BlaM's full-sized avatar

Dominik Deobald BlaM

View GitHub Profile
@BlaM
BlaM / escape-html.js
Created August 4, 2020 11:13
escape-html.js
function escapeHtml(str) {
escapeHtml.tmp.innerText = str;
var res = escapeHtml.tmp.innerHTML.replace(escapeHtml.quot, '"').replace(escapeHtml.apos, ''');
return res;
}
escapeHtml.tmp = document.createElement('span');
escapeHtml.quot = /"/g;
escapeHtml.apos = /'/g;
@BlaM
BlaM / random-id-generator.js
Created August 4, 2020 11:12
random-id-generator.js
function RandomIdGenerator() {
var self = this;
var lut = RandomIdGenerator.lut;
var myCounter = ++RandomIdGenerator.counter;
var idCounter = 0;
self.get = function() {
++idCounter;
@BlaM
BlaM / deepcopy.js
Created August 4, 2020 11:11
deepcopy.js
module.exports = deepCopy;
/**
* Deep copy an object (make copies of all its object properties, sub-properties, etc.)
* An improved version of http://keithdevens.com/weblog/archive/2007/Jun/07/javascript.clone
* that doesn't break if the constructor has required parameters
*
* It also borrows some code from http://stackoverflow.com/a/11621004/560114
*/
function deepCopy(src, /* INTERNAL */ _visited, _copiesVisited) {
@BlaM
BlaM / get-32-hex-random-key.js
Last active July 12, 2019 14:41
Generate 32 character random hex key
function randomKey() {
var r='', l=0;
do {
r += Math.random().toString(16).replace(/[01]\./g, '');
} while (r.length < 32 && l++ < 32);
return r.substr(-32, 32);
};
@BlaM
BlaM / ip-in-range.php
Created April 10, 2019 07:51
Check if a given ip is in a network
<?php
/**
* Check if a given ip is in a network
* @param string $ip IP to check in IPV4 format eg. 127.0.0.1
* @param string $range IP/CIDR netmask eg. 127.0.0.0/24, also 127.0.0.1 is accepted and /32 assumed. Hostnames are accepted, too.
* @return boolean true if the ip is in this range / false if not.
*/
function ip_in_range( $ip, $range ) {
if (!preg_match('/^[0-9\.]+$/', $ip)) {
@BlaM
BlaM / error_codes.php
Created April 8, 2019 11:54
HTTP Error Codes as PHP array
<?php
// https://de.wikipedia.org/wiki/HTTP-Statuscode
return array(
0 => 'OK',
100 => 'Continue',
101 => 'Switching Protocols',
102 => 'Processing',
200 => 'OK',
201 => 'Created',
@BlaM
BlaM / crypt_apr1_md5.php
Last active January 27, 2023 13:01
APR1-MD5 encryption method (windows compatible) for Apache .htpasswd
<?php
// APR1-MD5 encryption method (windows compatible)
// https://gist.github.com/BlaM/f3a9d535963900d1fbddc890c66f3891
//
// 2019-04-09 - https://github.com/BlaM - $salt
// 2022-03-04 - https://github.com/BlaM - fix php7 deprecation warning
// 2023-01-11 - https://github.com/BlaM and https://github.com/alecos71 - prepare for php8 sunset of str_shuffle
function crypt_apr1_md5($plainpasswd, $salt = null) {
if (empty($salt)) {
$chars = 'abcdefghijklmnopqrstuvwxyz012345689';
if (!defined('__PUBLIC__')) define('__PUBLIC__', __FILE__);
define('SCHEMA', (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ) ? 'https' : 'http');
define('ROOT_LINK', str_replace(array(str_replace($_SERVER['SCRIPT_NAME'], '', $_SERVER['SCRIPT_FILENAME']), basename(__PUBLIC__)), array('', ''), __PUBLIC__));
define('ROOT_URL', SCHEMA . '://' . $_SERVER['HTTP_HOST'] . ROOT_LINK);
define('ROOT_URL_NO_SLASH', substr(ROOT_URL, 0, -1));
@BlaM
BlaM / add_query.php
Created January 3, 2019 09:57
Simple "add query" function that does not use parse_str()
<?php
function add_query($url, $params, $replace = false) {
$pu = parse_url($url);
foreach ($params as $k => $v) {
$np = $k . '=' . urlencode($v);
if ($replace && preg_match('/(^|&)' . $k . '=/', $pu['query'])) {
$pu['query'] = preg_replace('/(^|&)' . $k . '=([^&]*)?/', '$1' . str_replace('$', '\$', $np), $pu['query']);
@BlaM
BlaM / telefonbuch-spam.xml
Created November 29, 2018 14:14
Fritz!box Telefonbuch: SPAM-Anrufer
<?xml version="1.0" encoding="utf-8"?>
<phonebooks>
<phonebook owner="1" name="SPAM">
<contact>
<category>0</category>
<person>
<realName>[SPAM] Adwords</realName>
</person>
<telephony nid="9">
<number type="home" prio="1" id="0">023199953665</number>