Snippets

Redirect all requests to HTTPS

Submitted by Ron Golan on Thu, 02/21/2013 - 11:16am
Ron Golan's picture

To redirect all requests to HTTPS, place the following code into the vhost configuration file.

<IfModule mod_rewrite.c>
  RewriteEngine on
        RewriteCond %{SERVER_PORT} 80
        RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,L]
        php_value session.cookie_secure 1
</IfModule>
Category: 

jQuery to select image that is the only child

Submitted by Ki Kim on Thu, 01/31/2013 - 11:24am
Ki Kim's picture

This jQuery code will select <img> tags that are the only child inside a <p>.

var images = jQuery('p img:only-child');

However, the code is no good if we want <img> without anything around, not even texts such as,

  <p><img src="foo" />Some text goes here</p>

Using DOM properties, previousSibling and nextSibling, does the job.

var images = $('p img:only-child').filter(function() {
   return !this.previousSibling && !this.nextSibling;
});

This will detect texts as siblings as well, while jQuery methods don't.

Category: 
Tags: 

Turn your Web Browser into Text Editor.

Submitted by Minnur Yunusov on Wed, 01/30/2013 - 11:53am
Minnur Yunusov's picture

Copy and paste the following command to address line and press "Return"
data:text/html, <html contenteditable>

Category: 

Remove .DS_Store files periodically on OS X

Submitted by Ki Kim on Thu, 01/24/2013 - 8:57am
Ki Kim's picture

Find all .DS_Store files under the directory /Users/jsmith/www

sudo find /Users/jsmith/www -name .DS_Store -depth

And remove them as well.

sudo find /Users/jsmith/www -name .DS_Store -depth -exec rm {} \;

Add the command to crontab so it does it at 10:15am and 3:15pm every day.

# Enter the command and your password when prompted.
sudo crontab -e

# The command should have opened an editor of your choice.
# If an editor is not assigned, you can temporarily assign nano editor.
export EDITOR=nano

# In the editor, enter this line, save it and exit.
15 10,15 * * * root find /Users/jsmith/www -name .DS_Store -depth -exec rm {} \;

I could have removed all .DS_Store files in the system, but doing it with a directory that contains web sites, which can be copied over the network, was good enough for me. Version control softwares usually make exceptions for .DS_Store file by default, so no worry on unwillingly putting them in the code repositories, but it would not hurt to clean them up locally too.

Category: 
Tags: 

Sendmail Error on OS X Mountain Lion

Submitted by Ki Kim on Tue, 11/20/2012 - 8:46am
Ki Kim's picture

If your local drupal site stopped sending out emails after upgrading to OS X Mountain Lion, run these commands:

sudo mkdir -p /Library/Server/Mail/Data/spool
sudo /usr/sbin/postfix set-permissions (ignore error message after you run it)
sudo /usr/sbin/postfix start

Source: http://apple.stackexchange.com/questions/54051/sendmail-error-on-os-x-mo...

Category: 

Special characters in CSS generated content

Submitted by Ki Kim on Wed, 11/07/2012 - 3:06pm
Ki Kim's picture

When inserting special characters in CSS generated content using :before or :after, use hexadecimal notation.

// Like this
a:after {
  content: "\25C2";
}
// Instead of this
a:after {
  content: "&#9666;";
}

Ref) http://alanhogan.com/tips/css/special-characters-in-generated-content

Category: 

Installing node.js on a CentOS 5.5 server

Submitted by Ki Kim on Wed, 10/31/2012 - 1:15pm
Ki Kim's picture

If you manage to install binaries and run it, that's much simpler. If you need to compile yourself, then these tips can save you some time.

These are prerequisites to installing node.js. You can skip the ones that are already installed.

1) Install g++

$ yum install sudo gcc-c++

2) Install bzip2-devel (besides bzip2)

$ yum install bzip2-devel

3) Upgrade python to 2.7.3 (from 2.4)
- Node.js requires python 2.6 or 2.7
- If you have 3.0, see https://github.com/joyent/node/wiki/Installation

$ curl -O http://python.org/ftp/python/2.7.3/Python-2.7.3.tgz
$ tar xfz Python-2.7.3.tgz
$ cd Python-2.7.3
$ ./configure
$ make
# You'd need to be either root or a sudoer
$ sudo make install

After these, I was able to compile node.js without an error. Latest version of node.js may be different from the example.

$ curl -O http://nodejs.org/dist/v0.8.14/node-v0.8.14-linux-x64.tar.gz
$ tar -zxf node-v0.8.14.tar.gz
$ cd node-v0.8.14
$ ./configure
$ make
# You'd need to be either root or a sudoer
$ sudo make install
Category: 

Running wget from php page on OS X

Submitted by Ki Kim on Fri, 10/19/2012 - 1:32pm
Ki Kim's picture

My php page had a line like,

<?php
  shell_exec
('wget http://www.example.com/path/to/something');
?>

The actual parameters of wget command were more complicated. There were two things I had to resolve. wget command does not come with OS X by default, so you needed to install it first. One of easy ways to install wget is through homebrew, which I already had. I simply ran,

$ brew install wget

Ron explained how to install homebrew.

But then I figured that apache running the php page could not find wget command, which is located in /usr/local/bin/wget. I use MAMP and I had to tell MAMP where to look for wget. I opened up text file, /Applications/MAMP/Library/bin/envvars and added the following line in it,

export PATH=$PATH:/usr/local/bin

Stop and start Apache Server of MAMP and the problem was solved.

Category: 

Running scp through ssh tunnel

Submitted by Ki Kim on Wed, 10/03/2012 - 6:13pm
Ki Kim's picture

Run scp to machine R, which is only accessible through gateway machine G.

Step 1: Establish SSH tunnel. Pick a temporary port between 1024 and 32768 (1234 in this example). Port 22 will be used by scp.

$ ssh -L 1234:<address of R known to G>:22 <user at G>@<address of G>

# Adding "cat -" will keep it running while above will get you connected to G
$ ssh -L 1234:<address of R known to G>:22 <user at G>@<address of G> cat -

Either way you run it, open another terminal for next step.

Step 2: Run scp against port 1234 pretending 127.0.0.1 (localhost) is the remote machine R, and the command will be sent to R.

$ scp -P 1234 <user at R>@127.0.0.1:/path/to/file file-name-to-be-copied

References:
http://whoochee.blogspot.com/2012/07/scp-via-ssh-tunnel.html
http://www.rzg.mpg.de/networkservices/ssh-tunnelling-port-forwarding

Category: 

Online Diff Tool

Submitted by Ki Kim on Thu, 09/27/2012 - 10:06am
Ki Kim's picture

http://www.diffchecker.com
Diff Checker is a free online diff tool that gives you the text differences between two blocks of text.

Category: 

Directory based organisational layer

Submitted by Ki Kim on Wed, 08/08/2012 - 3:04pm
Ki Kim's picture

http://drupal.org/project/odir

This module adds a new organizational layer to drupal, making it easy for managing large numbers of files and nodes. It allows the creation of onthefly directory structures and upload of mutliple files at once. Jpeg files are displayed as slideshows, other files can be downloaded from a block.

Category: 
Tags: 

Test regex (regular expression) online

Submitted by Ki Kim on Mon, 08/06/2012 - 8:54pm

svn merge recent change in trunk to production branch

Submitted by Ki Kim on Thu, 08/02/2012 - 9:52am
Ki Kim's picture

This command will bring all changes in trunk since the branch was created (called sync or catch-up).
Caret (^), available since version 1.6, denotes the root of repository so you do not have to specify full URL.

$ cd /to-the-path-of/working-copy-of-the-branch
$ svn merge ^/trunk

# If your project is a subfolder of root (svn represents repository as if they are directory structure).
$ svn merge ^/my-project/trunk

The merged change is still local, so you need to commit the change.

Category: 
Tags: 

Count all lines of code in a directory

Submitted by Ki Kim on Tue, 07/24/2012 - 12:41pm
Ki Kim's picture
$ find . | xargs wc -l

To limit to certain file types such as inc, php, and module

$ find . -type f | egrep "\.(inc|php|module)$" | xargs wc -l
Category: 

jQuery scrollTo plugin

Submitted by Minnur Yunusov on Mon, 07/23/2012 - 1:32pm
Minnur Yunusov's picture

A small, customizable plugin for scrolling elements, or the window itself.
http://demos.flesler.com/jquery/scrollTo/

Category: 

jQuery onScreen plugin

Submitted by Minnur Yunusov on Mon, 07/23/2012 - 1:32pm
Minnur Yunusov's picture

onScreen is a jQuery plugin to detect whether an element is currently visible on-screen. It adds the :onScreen selector which you can use like so: $("span:onScreen").
http://benpickles.github.com/onScreen/

Category: 

Using SVN 1.7 commands on upgraded Drupal modules.

Submitted by Ki Kim on Fri, 07/20/2012 - 9:49am
Ki Kim's picture

These svn commands are not particularly version 1.7, but works well with upgrading Drupal modules.

After replacing module folders and when ready to commit the code change to svn repository:

# Remove dropped files

$ svn st | grep '^!' | awk '{print $2}' | xargs svn rm

# Add new files

$ svn st | grep '^?' | awk '{print $2}' | xargs svn add

# To add, this works too (Make sure you are at directory level that has no ignored files; otherwise --force will add ignored files as well)

$ svn add --force .

# Commit

$ svn ci -m "Upgraded a module."

If you are using svn 1.6, overwrite folders instead of replacing to keep hidden .svn folders. Then the command above should work too. There will be no files getting dropped when overwriting.

Category: 

Edit hidden files on OS X

Submitted by Ki Kim on Wed, 07/11/2012 - 7:10am
Ki Kim's picture

If you want to edit a hidden file (such as .htaccess) without dropping into Terminal, on [Open Files] dialog of a text editor, enter Shift-Command-. (dot).

Wish Finder has the feature too? A little work with Automator can achieve close enough effect.
More info on how to do this.

Category: 

Mac to read/write to windows formatted hard drive

Submitted by Ki Kim on Tue, 07/10/2012 - 9:20am
Ki Kim's picture

Install NTFS-3G and MacFuse.
OS X already reads and writes to FAT32 formatted drive, and can read from NTFS formatted drive. Snow Leopard has built-in write support to NTFS, but it is inactive due to known instability. If you don't need file access permissions and know that every file will be less than 4GB size, you could consider using FAT32 format.
More info

Category: 

Delete all .svn directories

Submitted by Ki Kim on Mon, 06/25/2012 - 1:30pm
Ki Kim's picture
# Find all .svn directories under current directory (see dot).
$ find . -type d -name .svn

# Delete all .svn directories found; note tick(`).
$ rm -rf `find . -type d -name .svn`

Category: 

Override Node Options

Submitted by Minnur Yunusov on Fri, 06/15/2012 - 4:53pm
Minnur Yunusov's picture

The Override Node Options module allows permissions to be set to each field within the Authoring information and Publishing options field sets on the node form. It also allows selected field sets to be set as collapsed and / or collapsible.
http://drupal.org/project/override_node_options

Category: 

Encrypt and decrypt strings in PHP

Submitted by Ki Kim on Wed, 06/13/2012 - 11:06am
Ki Kim's picture

If you don't want to save strings in clear text, there are new php functions (php >= 5.3.0) that can be of help; openssl_encrypt() and openssl_decrypt().

<?php
  $string
= "This is a readable string."
 
$password = "p@ssword";
 
$method = "aes-256-cbc";
 
 
$encrypted = openssl_encrypt($string, $method, $password);
 
  echo
"$string => $encrypted";
 
// Outputs: This is a readable string. => OeOiTWcgIPC1xIZaDJ3XTEaY/D4m1sQmxgPbzjxxlRA=
 
 
$decrypted = openssl_decrypt($encrypted, $method, $password);
  echo
"$encrypted => $decrypted";
 
// Outputs: OeOiTWcgIPC1xIZaDJ3XTEaY/D4m1sQmxgPbzjxxlRA= => This is a readable string.
?>

According to http://stackoverflow.com/questions/1391132/two-way-encryption-in-php, these are possible values for encryption methods.

aes-128-cbc, aes-128-cfb, aes-128-cfb1, aes-128-cfb8, aes-128-ecb, aes-128-ofb, aes-192-cbc, aes-192-cfb, aes-192-cfb1, aes-192-cfb8, aes-192-ecb, aes-192-ofb, aes-256-cbc, aes-256-cfb, aes-256-cfb1, aes-256-cfb8, aes-256-ecb, aes-256-ofb, bf-cbc, bf-cfb, bf-ecb, bf-ofb, camellia-128-cbc, camellia-128-cfb, camellia-128-cfb1, camellia-128-cfb8, camellia-128-ecb, camellia-128-ofb, camellia-192-cbc, camellia-192-cfb, camellia-192-cfb1, camellia-192-cfb8, camellia-192-ecb, camellia-192-ofb, camellia-256-cbc, camellia-256-cfb, camellia-256-cfb1, camellia-256-cfb8, camellia-256-ecb, camellia-256-ofb, cast5-cbc, cast5-cfb, cast5-ecb, cast5-ofb, des-cbc, des-cfb, des-cfb1, des-cfb8, des-ecb, des-ede, des-ede-cbc, des-ede-cfb, des-ede-ofb, des-ede3, des-ede3-cbc, des-ede3-cfb, des-ede3-cfb1, des-ede3-cfb8, des-ede3-ofb, des-ofb, desx-cbc, rc2-40-cbc, rc2-64-cbc, rc2-cbc, rc2-cfb, rc2-ecb, rc2-ofb, rc4, rc4-40, seed-cbc, seed-cfb, seed-ecb, seed-ofb

If you don't know what to choose, try "aes-256-cbc". AES is said to be used by U.S. government.

For alternatives that can be used in older PHP versions, check out, http://us2.php.net/manual/en/refs.crypto.php.

Don't use two-way encryptions on passwords. They should be encrypted with one-way hash functions.

Category: 

Peek at the start/end of a (huge) file

Submitted by Ki Kim on Wed, 06/13/2012 - 9:42am
Ki Kim's picture

See the beginning of a file.

$ head foo.log

See first 25 lines.

$ head -n 25 foo.log

See the end of the file.

$ tail foo.log

"Follow" a log file that is being updated.

$ tail -f foo.log
Category: 

Using tar to zip/unzip files

Submitted by Ki Kim on Wed, 06/13/2012 - 9:28am
Ki Kim's picture

Extract files from a tar file (see x in the arguments).

$ tar -xvf foo.tar

Extract gz compressed tarball.

$ tar -xvzf foo.tar.gz
$ tar -xvzf foo.tgz

tar seems to work without z as well.

$ tar -xvf foo.tar.gz

Extract one file.

$ tar -xvzf foo.tgz onefile.php

Creat a tar file and zip it (see c in the arguments).
(tar allows arguments with no dash for historical reason)

$ tar cvzf foo.tgz *.php *.txt
$ tar cvzf foo.tgz mydirectory
Category: 

Pages