Skip to content

Instantly share code, notes, and snippets.

View Langmans's full-sized avatar

Langmans Langmans

  • Netherlands
  • 10:22 (UTC +02:00)
View GitHub Profile
@Langmans
Langmans / .htaccess
Last active February 16, 2017 08:04
wordpress settings
###DMG SECURITY BEGIN###
<IfModule mod_rewrite.c>
RewriteEngine On
#whitelist:
RewriteRule ^/?wp-content/plugins/wp-spamshield/ - [QSA,L]
#blacklist:
RewriteRule ^/?xmlrpc.php$ - [F,L]
RewriteRule ^/?wp-admin/includes/ - [F,L]
RewriteRule ^/?wp-includes/[^/]+\.php$ - [F,L]
RewriteRule ^/?wp-includes/js/tinymce/langs/.+\.php - [F,L]
@Langmans
Langmans / to_utf8.php
Last active June 2, 2016 14:52
Anything to UTF-8. Support for arrays and object to string conversion as well.
<?php
/**
* Converts anything to UTF-8.
*
* @param $string string|object|array
* @param $stringify_objects bool|true
* @param $throw_exceptions bool|true
*
* @throws InvalidArgumentException
<?php
/**
* @param DateTime $dob
* @param string|DateTime $end
*
* @return DatePeriod
*/
function getBirthdays(DateTime $dob, $end = 'today 23:59:59')
{
<?php
// https://en.wikipedia.org/wiki/Binary_prefix
function binairy_prefix_to_number($string, $mult = null, $exceptions = true)
{
if (is_numeric($string)) {
return $string;
}
$units = array('', 'k', 'm', 'g', 't', 'p', 'e', 'z', 'y');
$pattern = sprintf('@^(?<size>\d+([\.,]\d{1,2})?)(?<unit>[%s]?)(?<binary>i?)b?$@i', implode('', $units));
@Langmans
Langmans / htmlify.php
Created February 11, 2016 09:45
htmlify for stuff like youtube descriptions
<?php
function htmlify($text)
{
$html = htmlspecialchars($text);
$html = " <p> " . preg_replace("@(\r\n|\n|\r){2,}@", " </p> <p> ", $html) . " </p> ";
$html = str_replace("\n", " <br> ", $html);
$html = preg_replace('/\s\s+/', ' ', $html);
$html = preg_replace_callback('/([a-z]+:\/\/)(\S+)/', function ($matches) {
$matches[2] = preg_replace('@^www\.@i', '', $matches[2]);
@Langmans
Langmans / audio-video-classes.js
Created March 10, 2016 15:27
add classes to video / audio DOM elements so you can add custom controls.
jQuery(function($){
$('audio,video')
.on('init ended pause play playing progress ratechange seeked seeking stalled suspend waiting', function () {
var obj = this, $obj = $(this);
if ($obj.is('.has-custom-controls')) {
$obj.removeAttr('controls');
}
$.each(['ended', 'error', 'loop', 'muted', 'paused', 'seeking'], function (i, prop) {
try {
var bool = obj[prop];
@Langmans
Langmans / composer.json
Created March 16, 2016 16:00
database migrator source DB (altered tables or columns) > target DB
{
"config": {
"platform": {
"php": "5.3.10"
}
},
"require": {
"doctrine/dbal": "^2.5",
"jdorn/sql-formatter": "^1.2"
}
@Langmans
Langmans / url.php
Created March 17, 2016 11:00
get requested url when rewriteengine is on.
<?php
$url = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$base = rtrim(str_replace('\\', '/', dirname($_SERVER['SCRIPT_NAME'])), '/');
$len = strlen($base);
if (substr($url, 0, $len) == $base) {
$url = substr($url, $len);
}
@Langmans
Langmans / bla.php
Last active April 4, 2016 11:47
bla.php
<?php $sql = sprintf("SELECT GROUP_CONCAT(dc.document_category_id) as cat_ids, d.*
FROM %s dc
LEFT JOIN %s d ON(d.id = dc.document_id)
GROUP BY d.id
ORDER BY d.added",
DocumentCategorization::getBacktickTableName(),
Document::getBacktickTableName()
);
/** @var As_Database $db */
$db = Document::getDb();
@Langmans
Langmans / git_status.php
Created April 13, 2016 14:51
returns the current git branch, remote name, hash and timestamp for a git repository dir or git submodule dir
<?php
function git_status($dir) {
$git_line = 'Unable to determine git hash';
$git_dir = $dir . '/.git';
$repo = null;
$submodule = null;
if (is_file($git_dir) && preg_match('@gitdir: (.*)@', trim(file_get_contents($git_dir)), $m)) {
$repo = basename($m[1]);