Download favicon using Javascript

This is from a stackoverflow post

(function() {
    var i=0;
    var arrLink = [];
    var found = 0;
    arrLink = document.getElementsByTagName('link');
    if (arrLink.length > 0) {
        for (i=0; i < arrLink.length; i++) {
if (typeof arrLink[i].rel !== 'undefined'
&& arrLink[i].rel.indexOf('icon') > -1)
{
                found = 1;
                window.open(arrLink[i].href);
            }
        }
        if (found === 0) {
            alert('favico Not Found');
        }
    } else {
        alert('favico Not Found');
    }
})();

 

Zip Current Directory and its contents recursively

This is a slightly modified version of one stackoverflow post. If the folder to be zipped is large the execution time may be larger than your current setting. So you may need to modify max_execution_time parameter.

<?php 	
$rootPath = getcwd(); 	
$zip = new ZipArchive(); 	
$filename = 'Archive.zip'; 	
$zip->open( $filename, ZipArchive::CREATE | ZipArchive::OVERWRITE);

	$files = new RecursiveIteratorIterator(
		new RecursiveDirectoryIterator($rootPath),
		RecursiveIteratorIterator::LEAVES_ONLY
	);

	foreach ($files as $name => $file)
	{
		if (!$file->isDir())
		{
			// Get real and relative path for current file
			$filePath = $file->getRealPath();
			$relativePath = substr($filePath, strlen($rootPath) + 1);
			// Add current file to archive
			$zip->addFile($filePath, $relativePath);
		}
	}
	$zip->close();
	header('Content-Type: application/zip');
    header("Content-Disposition: attachment; filename = $");
    header('Content-Length: ' . filesize($filename));
    header("Location: $filename");
?>

Sending Mail through PHP Mailer using TLS

<?php
	define('SMTP_SERVER', 'mail.servername.com');
	define('SMTP_PORT', 587);
	define('SMTP_USERNAME', 'user@servername.com');
	define('SMTP_PASSWORD', 'password');
	define('MAIL_ORDERS_ADDRESS', 'user@servername.com');
	define('MAIL_ORDERS_NAME', 'User');

	require_once "PHPMailerAutoload.php";

	try{
		$mail = new PHPMailer(true);
		$mail->IsSMTP();
		$mail->SMTPAuth = true;
		$mail->Host = SMTP_SERVER;
		$mail->Port = SMTP_PORT;
		$mail->Username = SMTP_USERNAME;
		$mail->Password = SMTP_PASSWORD;
		$mail->SMTPSecure = 'tls';
		$mail->SetFrom(MAIL_ORDERS_ADDRESS, MAIL_ORDERS_NAME);
		$mail->Subject = 'Mail test ';
		$mail->MsgHTML('<b>Ahoy</b>');
		$mail->AddAddress('receiver@servername.com', 'Receiver');

		if(!$mail->Send()) throw new Exception($mail->ErrorInfo);
	}
	catch(Exception $e){
		echo $e->getMessage();
	}

?>

Archlinux User Theme Fix for Gnome Shell 3.4

Install User theme from Gnome extensions website. Which is accomplished by clicking the OFF button (top left). It will ask for your permission to install. Proceed and then it will be installed under you home directory. Once succesfully installed OFF will turn to ON

But if you check gnome tweak tool, most probably, shell theme will be seen disabled. The fix is mentioned in the comments section of the page.


sudo cp $HOME/.local/share/gnome-shell/extensions/user-theme\@gnome-shell-extensions.gcampax.github.com/schemas/org.gnome.shell.extensions.user-theme.gschema.xml /usr/share/glib-2.0/schemas/

sudo glib-compile-schemas /usr/share/glib-2.0/schemas

Now check gnome tweak tool, you will be able to change the shell theme.

A program to chart your bandwidth* usage in linux

Version 0.2 of inetstat has been released.

Get it from
https://github.com/rjanak/inetstat

git clone git://github.com/rjanak/inetstat.git
cd inetstat
./installer.sh

It now includes a charter program, in order to run it, you need to have GD::Graph perl module. Either install it from system repositories or from cpan.

In Linux Mint

sudo apt-get install libgd-graph-perl

With cpan

cpan GD::Graph

Now run charter.pl in you terminal

charter.pl

You also have the freedom to specify an output file ( uses barchart.png by default ).

charter.pl -o mychart.png

That’s it. Have a great day!.

*by bandwidth, I’m referring to monthly data transfer.

Switching Vim Colors Revisted

This script does things a bit differently. Also, I have used rand to generate random number. So you need to have rand installed.

sudo apt-get install rand

The Script

" colorswitch.vim - Switch between installed Vim color schemes
" Author:       Rahul Janak
" Version:      0.1 (updated 21/01/2012)
" Usage:
" 		F8 Switches to next Color Scheme
"		Shift-F8 Switches to previous one
"		Alt-F8 Switches to a random one
"

if v:version < 700 || exists('loaded_schemes')
	finish
endif

if !exists('g:colors_name')
		colorscheme default
endif

let loaded_schemes = 1
let s:paths = split(globpath(&runtimepath, 'colors/*.vim'), "\n")
let g:mycolors = map(s:paths, 'fnamemodify(v:val, ":t:r")')

function! SwitchColorRandom()
		execute "let random_number = system('rand -M " . len(g:mycolors) . "')"
		execute 'colorscheme ' g:mycolors[random_number]
		redraw
		execute 'colorscheme'
endfunction

function! SwitchColorForward()
	    let s:idx = index(g:mycolors, g:colors_name)
		if s:idx < len(g:mycolors)
				execute 'colorscheme ' g:mycolors[s:idx + 1]
				redraw
				execute 'colorscheme'
		endif
endfunction

function! SwitchColorBackward()
	    let s:idx = index(g:mycolors, g:colors_name)
		if s:idx >= 1
				execute 'colorscheme ' g:mycolors[s:idx -1]
				redraw
				execute 'colorscheme'
		endif
endfunction

nnoremap <F8> :call SwitchColorForward()<CR>
nnoremap <S-F8> :call SwitchColorBackward()<CR>
nnoremap <A-F8> :call SwitchColorRandom()<CR>

If you don’t have many color schemes installed  then you can go to this page and download ColorSamplerPack.zip. Then extract it to ~/.vim/colors/

Next if you want to try the above script, save it in ~/.vim/plugin. Next time you open vim you will be able to access it via below mentioned shortcuts.

F8 – Test Next Scheme

Shift – F8 – Test Previous Scheme

Alt – F8 – Test a Random One

Happy Switching 🙂

Switching Colorschemes in Vim

If you have lots of vim color schemes and you want to check each one out, then, I don’t think anything could be better than this. Check it out.

http://vim.wikia.com/wiki/Switch_color_schemes

If you are unsure of how to use it, first save the file ( Found below the Script Section or download it from here) into your plugin directory  ~/.vim/plugin. If it’s not there, make one.

mkdir -p ~/.vim/plugin

cp setcolors.vim ~/.vim/plugin

Restart vim or you can source the file from Vim for immediate effect, like done below.

:so ~/.vim/plugin/setcolors.vim 

Now press F8 to try next scheme
Shift F8 to try previous scheme ( if any )
Alt F8 to try a random one.

Awesome!