Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

About this user

Jamie Wilkinson http://tramchase.com

« Newer Snippets
Older Snippets »
Showing 21-40 of 44 total

Handling filenames with spaces in a bash for or while loop

// The `find` here gets every directory in my home dir, one level deep, but some of them have spaces... the default 'for i in $(find ...)' behavior treats each individual word boundary as a separate variable, but piping to `read` breaks them on newlines
// Note: OS X `find` does not support "-depth 1" (its -depth opt is for depth-first searching vs. breadth-first), just use an 'ls' with an 'if [ -d $filename]' to test for directories

find ~ -type d -depth 1 | while read filename; do
# ls ~ | while read filename; do
        echo $filename
        # other stuff
done

MediaWiki LDAP configuration

Requires the LdapAuthentication extension. Regarding precise LDAP queries YMMV.

# LDAP authentication
require_once( 'extensions/LdapAuthentication/LdapAuthentication.php' );
$wgAuth = new LdapAuthenticationPlugin();

$wgLDAPDomainNames = array( "domain-ldap" );
$wgLDAPServerNames = array( "domain-ldap" => "ldap.domain.org"  );
$wgLDAPEncryptionType = array( "domain-ldap" => "clear" );   // "testADdomain"=>"tls",
$wgLDAPSearchStrings = array( "domain-ldap"=>"uid=USER-NAME,ou=People,dc=domain,dc=org"  );

$wgLDAPWriterDN                         = array( "domain-ldap" => "cn=Manager,dc=domain,dc=org" ); // for changing passwords; AddLDAPUsers below must be true
$wgLDAPWriterPassword = array( "domain-ldap" => "MANAGERPASSWORD" ); // FIXME
$wgLDAPWriteLocation    = array( "domain-ldap" => "ou=People,dc=domain,dc=org" );

$wgLDAPAddLDAPUsers = array( "domain-ldap" => true ); // allow adding users to LDAP from mediawiki? require WriterDN/Password
$wgLDAPUpdateLDAP = array( "domain-ldap" => true );    // for updating passwords;
$wgLDAPUseLocal = array( "domain-ldap" => true ); // failover to local DB?
$wgLDAPMailPassword = array( "domain-ldap" => true ); // if can't write to LDAP this is basically useless
$wgLDAPRetrievePrefs = array( "domain-ldap" => true );
$wgMinimalPasswordLength = 1;

// Don't automatically create an account for a user if the account exists in LDAP but not in MediaWiki. Default is false.
// this totally breaks things, do not turn it on
$wgLDAPDisableAutoCreate = array( "domain-ldap" => false );

$wgLDAPDebug = 1; // 3 is a lot

vi find/replaces for adding a table prefix to a SQL dump

Was importing a DB but wanted to prefix the tables, but the file was too large for TextMate. Here's all the vim substitutions

%s/DROP TABLE IF EXISTS `/DROP TABLE IF EXISTS `prefix_/g
%s/CREATE TABLE `/CREATE TABLE `prefix_/g
%s/INSERT INTO `/INSERT INTO `prefix_/g
%s/LOCK TABLES `/LOCK TABLES `prefix_/g
%s/ALTER TABLE `/ALTER TABLE `prefix_/g

mongrel_cluster monit script

Copyright (C) 2006 Peter J Jones (pjones@pmade.com).

################################################################################
# This script will update your monit configuration file to monitor your
# mongrel cluster by reading the mongrel cluster file and generating monit
# check entries.  It places delimiters in the configuration file so that you
# can run this script multiple times without generating duplicate entries.
#
# Run it like this:
#
#  mongrel_monit --mongrel=/path/to/mongrel/config.yml --monit=/path/to/monitrc
#
# You can edit the ERB template below to control the generated monit config.
#


TEMPLATE = <<EOT
check process mongrel_<%= @port %> with pidfile <%= @pidfile %>
  group mongrel
  start program = "/usr/local/bin/<%= @start %>"
  stop program = "/usr/local/bin/<%= @stop %>"

  if failed host 127.0.0.1 port <%= @port %> protocol http
    with timeout 10 seconds
    then restart

  if totalmem > 100 Mb then restart
  if cpu is greater than 60% for 2 cycles then alert
  #if cpu > 90% for 5 cycles then restart
  #if loadavg(5min) greater than 10 for 8 cycles then restart
  if 3 restarts within 5 cycles then timeout

EOT
################################################################################
require 'erb'
require 'optparse'
require 'fileutils'
require 'rubygems'
require 'mongrel_cluster/init'
################################################################################
module Kernel
  $commands = []

  # Hack Kernel::` so that we can capture what mongrel_cluster does
  def ` (cmd)
    $commands << cmd
    system(':') # to set $?
  end

  # Hack Kernel::puts to shut mongrel_cluster up
  def puts (str)
    true
  end
end
################################################################################
options = OptionParser.new

options.on('--mongrel=FILE', 'Mongrel configuration file') do |o|
  $mongrel_config = o
end

options.on('--monit=FILE', 'Monit configuration file') do |o|
  $monit_config = o
end

options.parse!

unless $mongrel_config and $monit_config
  puts "please give --mongrel and --monit"
  exit 1
end
################################################################################
mongrel_config_data = YAML.load_file($mongrel_config)
Dir.chdir(mongrel_config_data['cwd']) if mongrel_config_data['cwd']
################################################################################
# This is the only way to access the cmd that mongrel_cluster uses so that we
# can keep things DRY
start = Cluster::Start.new
start.instance_variable_set(:@config_file, $mongrel_config)
start.run
start_commands = $commands.dup
$commands.clear

stop = Cluster::Stop.new
stop.instance_variable_set(:@config_file, $mongrel_config)
stop.run
stop_commands = $commands.dup
$commands.clear
################################################################################
template = ERB.new(TEMPLATE)
config = ''

0.upto(start_commands.length - 1) do |i|
  @port = start_commands[i].match(/-p\s*(\d+)/)[1]
  @pidfile = File.expand_path(start_commands[i].match(/-P\s*(\S+)/)[1])
  @start = start_commands[i]
  @stop = stop_commands[i]
  config << template.result(binding)
end
################################################################################
monit_config_data = File.open($monit_config) {|f| f.read}

# TODO: if you have more than one cluster, this won't do
banner_start = "#= Mongrel_Monit Config Start =#\n"
banner_stop  = "#= Mongrel_Monit Config End =#\n"

if monit_config_data.match(/#{banner_start}.*#{banner_stop}/m)
  monit_config_data.sub!(/#{banner_start}.*#{banner_stop}/m, "#{banner_start}#{config}#{banner_stop}")
else
  monit_config_data << "#{banner_start}#{config}#{banner_stop}"
end

File.open($monit_config, 'w') {|f| f << monit_config_data}
################################################################################

ssh tunnel with local port forwarding (MySQL)

// ssh tunnel remote mysql to a local port (in this case, 3307)

ssh -2 -f -C -N user@servername.com -L 3307/127.0.0.1/3306

DAAP tunneling / remote iTunes Music Share

// requirements
// * server running mt-daapd/Firefly (alternative server for iTunes music sharing)
// * tunneleling & port-forwading via ssh
// * local bonjour/zeroconf broadcasting using Apple's mDNSProxyResponder
// based on info from
// for more: see Music Blackhole
// Jamie Wilkinson


ssh USER@SERVERNAME -N -f -L 3690:SERVERNAME:3689 && mDNSProxyResponderPosix 127.0.0.1 musicserver "My remote music server" _daap._tcp. 3689 &

"Digg this page" XSS

// To include the nice little live-updated Digg widget

<script src="http://digg.com/tools/diggthis.js" type="text/javascript">script>

Mass find-replace using sed

Replace some widespread nasty hardcoded strings. 'sed $file > $file' doesn't work so hot so we use a temp file, and also make a backup of the old file.

for i in $(find . -type f); do sed 's/oldstring/newstring/g' $i > $i-tmp; mv $i $i-backup; mv $i-tmp $i; done

Close dangling HTML tags

Close open HTML tags, e.g. if you allow HTML in your comments. Not quite as robust as running it through tidy, but tidy is not always available.

function close_dangling_tags($html){
  #put all opened tags into an array
  preg_match_all("#<([a-z]+)( .*)?(?!/)>#iU",$html,$result);
  $openedtags=$result[1];

  #put all closed tags into an array
  preg_match_all("##iU",$html,$result);
  $closedtags=$result[1];
  $len_opened = count($openedtags);
  # all tags are closed
  if(count($closedtags) == $len_opened){
    return $html;
  }

  $openedtags = array_reverse($openedtags);
  # close tags
  for($i=0;$i < $len_opened;$i++) {
    if (!in_array($openedtags[$i],$closedtags)){
      $html .= ''.$openedtags[$i].'>';
    } else {
      unset($closedtags[array_search($openedtags[$i],$closedtags)]);
    }
  }
  return $html;
}

301 Permanent Redirect rule to consolidate domains

// redirect domain.com to www.domain.com (or vice versa)
// helps substantially with delicious links and other SEO optimization
// see for discussion

RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]

Tar up, excluding common media extensions

Just the code, please! Hop in the dir you want to back up (note: this means it'll expand without a base directory, so do a mkdir/cd first!)

find . ! -type d -print | egrep '/,|%$|~$|\.old$|\.mpg$|\.zip$|\.wmv$|\.mp3$|\.MP3$|\.mp4$|\.MP4$|\.av2$|\.ppt$|\.dir$|\.pps$|\.qt$|SCCS|/core$|\.o$|\.orig$|\.mpeg$|\.mov$|\.doc$|\.xls$|\.pdf$|\.swf$|\.fla$|\.wav$|\.aif$|\.aiff$|\.mp3$|\.jpg$|\.JPG$|\.jpeg$|\.JPEG$|\.gif$|\.GIF$|\.png$|\.PNG$|\.psd$|\.PSD$|\.tar.gz$|\.tgz$|\.TGZ$|\.tif$|\.TIF$|\.tiff$|\.TIFF$|\.tga$|\.TGA$|\.ram$|\.rm$|\.rma$|\.psd$|\.PSD$|\.ai$|\.AI$' > Exclude
tar czfvX ~/backup_text_back.tgz Exclude .

ajax_request? method to XHR detection

A oneline helper I put in all of my apps. Looks for HTTP headers that signal you're being called via an XmlHttpRequest. I use it for instant degradable AJAX by rendering a template if it's as usual but just rendering a partial if they just want that part!

  def ajax_request?
    request.env['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' || params[:ajax]  # params[:ajax] for safe-keeping, FIXME if it works w/ all browsers
  end

Apache + mongrel_cluster VirtualHost snippet

Put it in yr httpd.conf, or better yet, an included file. (that thing is monstrous as is)

<VirtualHost *:80>
        ServerName test.com
        DirectoryIndex index.html dispatch.fcgi
        DocumentRoot /domains/test.com/web/public
        RewriteEngine On
        RewriteRule ^(.*/)?.svn/ - [F,L]
        ProxyPass /images !
        ProxyPass /stylesheets !
        ProxyPass /javascripts !
        ProxyPass /examples !
        ProxyPass /map_data !
        ProxyPass /engine_files !
        ProxyPass /javascript !
        ProxyPass /feregions !
        ProxyPass / balancer://test/
        ProxyPassReverse / balancer://test
        ProxyPreserveHost On
        ErrorLog /opt/csw/apache2/var/log/test.com.error
        CustomLog /opt/csw/apache2/var/log/test.com common

        <Proxy balancer://test>
                BalancerMember http://127.0.0.1:8000
                BalancerMember http://127.0.0.1:8001
                BalancerMember http://127.0.0.1:8002
                BalancerMember http://127.0.0.1:8003
                BalancerMember http://127.0.0.1:8004
        Proxy>
</VirtualHost>

'Site is under maintenance' redirect, except for you

Upgrades woohoo! Tell those lusers to hold their horses while you happily debug. Some .htaccess magic, just sub in your IP:

<IfModule mod_rewrite.c>
        RewriteEngine On
        # 'under construction' override
        RewriteCond %{REMOTE_ADDR} !^127\.0\.0\.1$
        RewriteRule ^(.*)$ maintenance.html
IfModule>

Set up apache2+SSL on FreeBSD

A nice commandline walkthrough. Replace 'eyebeam' with your org name


# setting up apache2 + SSL on FreeBSD
# a list of commands you can copy/paste!
# @author Jamie Wilkinson 
# ganked from 

# edit /etc/openssl.conf to set some nice defaults for location, org. name, etc.
# important! change the default dir ./demoCA to /root/sslCA


# setup
cd ~root/
mkdir sslCA
chmod 700 sslCA
cd sslCA
mkdir certs private newcerts
echo 1000 > serial
touch index.txt

# generate certs
openssl req -new -nodes -out eyebeam-req.pem -keyout private/eyebeam-key.pem -config /etc/ssl/openssl.cnf
openssl req -new -x509 -days 3650 -extensions v3_ca -keyout private/cakey.pem -out cacert.pem -config /etc/ssl/openssl.cnf
openssl ca -config /etc/ssl/openssl.cnf -out eyebeam-cert.pem -days 3650 -infiles eyebeam-req.pem 

# keep them somewhere handy
mkdir /etc/ssl/crt
mkdir /etc/ssl/key
cp ~root/sslCA/eyebeam-cert.pem /etc/ssl/crt
cp ~root/sslCA/private/eyebeam-key.pem /etc/ssl/key

# add below to an ssl.conf that you include in your httpd.conf
<VirtualHost *:443>
        ServerName colossus.eyebeam.org:443
        SSLEngine on
        SSLCertificateFile /etc/ssl/crt/eyebeam-cert.pem
        SSLCertificateKeyFile /etc/ssl/key/eyebeam-key.pem
        DocumentRoot /www
        CustomLog /var/log/httpd-ssl-access.log combined
        ErrorLog /var/log/httpd-ssl-error.log
VirtualHost>

svk quickstart

I want to mirror the radiant source for local hacking with the fabulous svk

svk mkdir //mirror
svk mkdir //mirror/radiant
svk mirror http://dev.radiantcms.org/svn/radiant/trunk/radiant  //mirror/radiant/trunk
svk sync //mirror/radiant/trunk
svk cp //mirror/radiant/trunk //local/radiant/branches/jamie
svk co //local/radiant/branches/jamie

# use svk commit/sync like normal svn to your local svk depot
# use svk pull to grab new changes from the base repository
# use svk push to send them


Note:
* Capistrano does not support deploying from svk repositories. You need to create a local svn and then smerge things there first... more info here: http://octopod.info/2006/8/19/managing-multiple-local-mephisto-repos-with-svk

sup: bash script for universal SCM syncing (svn, svk, cvs, darcs, etc.)

I really can never remember if the project I'm in uses svn or cvs or if it's my own local svk mirror, etc. etc. Thus was born 'sup'!

Save and drop into a location that's in your PATH (I use ~/bin, YMMV)

#!/bin/bash
# sup -- a quick bash script to sync w/ various SCM tools
# @author   Jamie Wilkinson 

HERE=$(pwd)

## subversion
if [ -e ".svn" ]; then
        svn up
## cvs
elif [ -e "CVS" ]; then
        cvs up -dP
## darcs
elif [ -e "_darcs" ]; then
        darcs pull --all
## svk
elif [ -n "`grep $HERE ~/.svk/config`" ]; then
    svk up
## git
elif [ -e ".git" ]; then
    git pull
## perforce
#   todo
## arch
#   todo
## bzr
#   todo

fi

Build OpenLDAP 2.3 on FreeBSD using unixODBC instead of libODBC

cd /usr/ports/net/openldap23-server
sudo make install WITH_ODBC=y WITH_ODBC_TYPE=unixodbc


More to come on configuring the beast and accompanying phpLDAPadmin...

Configure proxy in Windows command shell (cmd.exe)

For use with gems, cpan, etc. Inside cmd.exe, type

set HTTP_proxy=http://my.proxy.server:8000

Increase the number of simultaneous XmlHttpRequests in Firefox

Firefox doesn't do a lot of simultaneous AJAX (or any kind of HTTP) requests. IE caps it at 2, too. This will allow you to test your XHR overload scripts, or just load pages faster in general.

1. Go to "about:config"
2. Find and edit the following
* network.http.pipelining=false
* network.http.pipelining.maxrequests=4
* network.http.proxy.pipeline=false
3. Make the false's true; I set my maxrequests at 20
« Newer Snippets
Older Snippets »
Showing 21-40 of 44 total