Skip to content

Instantly share code, notes, and snippets.

View tott's full-sized avatar

Thorsten Ott tott

  • Germany, Cologne area
View GitHub Profile
@tott
tott / gist:7908001
Created December 11, 2013 10:11
Get a space separated list of all hosts in an apache config so you can add them to your hosts file
#!/bin/bash
grep -E "Server(Name|Alias)" httpd.conf | sort | uniq | cut -d " " -f 6 | tr "\n" " "
@tott
tott / compare-urls.sh
Created December 10, 2013 11:18
Compare html served by different servers. This script will first get the page from the source ip and extract a list of links on this output. then it will compare the output of each link for both servers.
#!/bin/bash
ip1='FILL-SOURCE-IP'; ip2='FILL-DEST-IP'; host='FILL-HOSTNAME'; curl http://$host | grep -o '<a.*href=.*>' | grep "http://$host" | sed -e 's/<a .*href=['"'"'"]//' -e 's/["'"'"'].*$//' -e '/^$/ d' > links.txt; for link in `cat links.txt`; do path=`echo -n $link | cut -d "/" -f 4-`; curl --header "Host: $host" "http://$ip1/$path" > ip1.html; curl --header "Host: $host" "http://$ip2/$path" > ip2.html; echo; echo "Diff http://$ip1/$path"; echo; diff ip1.html ip2.html; done
@tott
tott / gist:7767218
Created December 3, 2013 10:38
.htaccess rule to force www and preserving the protocol http/https
# Force www
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
@tott
tott / ip_in_range.php
Created November 27, 2013 22:46
php check if IP is in given network range
/**
* 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
* @return boolean true if the ip is in this range / false if not.
*/
function ip_in_range( $ip, $range ) {
if ( strpos( $range, '/' ) == false ) {
$range .= '/32';
}
@tott
tott / secure-auth-cookies.php
Last active February 17, 2017 15:17
Encrypt WordPress auth cookies
<?php
function sav_encrypt_cookie( $decrypted ) {
$encrypted = mcrypt_encrypt( MCRYPT_RIJNDAEL_256, substr( AUTH_SALT, 0, 32 ), $decrypted, MCRYPT_MODE_ECB, mcrypt_create_iv( mcrypt_get_iv_size( MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB ), MCRYPT_RAND ) );
return trim( base64_encode( $encrypted ) );
}
function sav_decrypt_cookie( $encrypted ) {
$decrypted = mcrypt_decrypt( MCRYPT_RIJNDAEL_256, substr( AUTH_SALT, 0, 32 ), base64_decode( $encrypted ), MCRYPT_MODE_ECB, mcrypt_create_iv( mcrypt_get_iv_size( MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB ), MCRYPT_RAND ) );
return trim( $decrypted );
@tott
tott / functions.php
Created November 15, 2013 08:49
validate if a path is a subfolder of WP_CONTENT_DIR
/**
* Validate that a given path is a child of the WP_CONTENT_DIR
* @param string $path path to check
* @return boolean true/false
*/
function sav_path_is_valid( $path ) {
if ( !defined( WP_CONTENT_DIR ) )
return false;
$start = 0;
@tott
tott / alter-robots-txt.php
Created November 14, 2013 14:15
alter robots.txt
add_filter( 'robots_txt', 'sav_add_sitemap_to_robots' );
function sav_add_sitemap_to_robots( $robots ) {
$robots .= 'Sitemap: ' . site_url( 'sitemap_index.xml' );
return $robots;
}
@tott
tott / frontend-force-mapped-domain.php
Created November 14, 2013 14:14
Enforce domain mapping mapped domain for all front-end urls ran through esc_url()
add_filter( 'clean_url', 'sav_force_mapped_url' );
function sav_force_mapped_url( $url ) {
if ( ! is_admin() ) {
if ( function_exists( 'get_original_url' ) && strpos( '/wp-admin', $url ) === false ) {
$original = preg_replace( '#^https?://#', '', get_original_url('') );
$new = preg_replace( '#^https?://#', '', site_url() );
$url = str_ireplace( $original, $new, $url );
}
}
return $url;
@tott
tott / force-referer-for-import.php
Created November 13, 2013 19:35
Force http referer for imports to bypass some bad servers who would not want to serve the static assets otherwise
add_filter( 'http_request_args', 'force_referer_for_import' );
function force_referer_for_import( $r ) {
if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
$r['headers']['Referer'] = 'http://<referer>/';
}
return $r;
}
@tott
tott / gist:7384971
Created November 9, 2013 12:33
Enable exporting of Nav menus via WP Exporter
<?php
function tott_enable_menu_export() {
global $wp_post_types;
$wp_post_types['nav_menu_item']->_builtin = false;
}
add_action( 'load-export.php', 'tott_enable_menu_export' );