I see, learn and rediscover… everyday!
Uncategorized
Delete a remote branch in git
Jul 27th
We use Git here at interviewstreet. Using Git, branching, testing and reverting is so easy.
Almost all commands are simple and easy to remember in Git, except for deleting a remote branch.
The command to delete a remote branch “feature1″ is
git push origin :feature1
Just wanted to note this down.
Advanced Bash Scripting – Part 2
Jul 27th
The problem statement is as follows :
The Playfair Cipher encrypts text by substitution of digrams (2-letter groupings). It is traditional to use a 5 x 5 letter scrambled-alphabet key square for the encryption and decryption.
Each letter of the alphabet appears once, except “I” also represents “J”. The arbitrarily chosen key word, “CODES” comes first, then all the rest of the alphabet, in order from left to right, skipping letters already used.
To encrypt, separate the plaintext message into digrams (2-letter groups). If a group has two identical letters, delete the second, and form a new group. If there is a single letter left over at the end, insert a “null” character, typically an “X.”
To read more about the question, check http://tldp.org/LDP/abs/html/writingscripts.html
The solution for this question is pretty long (over 100 lines in shell script). You can check the final solution at http://sp2hari.com/bash/playfair-cipher.html
Explanation of function/code used in the solution.
locateInKeySquare: Searches for the character passed as the parameter in the keySquare and returns the position of the character.
addToKeySquare: Adds a character c passed as the parameter to the keySquare. This checks if the character is already present, if it is J and changes it to uppercase (if needed)
printKeySquare: Prints the keySquare from 1D to 2D format.
The rest of the code adds the keyWord to keySquare. The the remaining words are added to the keySquare. After that, we extract the dialects from the plaintext and based on the 3 rules, we encrypt the text.
Hope this is helpful..
Query String in URL using Codeigniter
Jun 6th
CodeIgniter by default won’t allow you to use query strings in the URL.
This is how you enable query string in url in codeigniter.
1. Set $config['uri_protocol'] = “PATH_INFO” in config.php
2. Set $config['enable_query_strings'] = TRUE in config.php
3. Make you sure you have QSA in your .htaccess. For example,
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|includes)
RewriteRule ^(.*)$ /index.php/$1 [L,QSA]
Note: If you make these changes, the pagination module is affected. So, if you had implemented any pagination before changing these settings, you might want to check them again.
My first 30 day challenge.
Jun 2nd
I’m a big fan of Matt Cutts (who isn’t) and especially like his 30 day challenges.

So, I decided probably I can give it a shot for a couple of months.
So, some of the stuff I had in my list are.
1. Learn Hadoop.
2. Read a book every 2 days.
3. Learn a new programming language
4. Go to beach everyday and spend at least 30 mins there..
So, this is what I’m going to try for this month. Sleep properly for a month. I’ve neglected my bad sleeping habits for a very long time and it’s time I try to do something about it.
So, planning to go to bed by 1 AM and not wake up before 6AM.
Let us see how long I can manage this one…
Importing Contacts from Google Mail using OAuth
May 28th
This is my second post in the fetch contacts from email account series. In this post, we are going to fetch contacts from a Google account. This isn’t a detailed tutorial, but a quick 2 minute how-to.
$this->config->item(‘google_return_url’) is the url of the page to which user is returned after a login attempt.
1. Add the following code where the user has to select Google Mail to import contacts.
<a href="https://www.google.com/accounts/AuthSubRequest?next=<?php echo $this->config->item('google_return_url').'?data=abc'; ?>&scope=http://www.google.com/m8/feeds/contacts/default/thin&secure=0&session=1">Fetch Google Contacts</a>
2. Include these 2 functions in your PHP code.
function make_api_call($url, $token)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$curlheader[0] = sprintf("Authorization: AuthSub token=\"%s\"/n", $token);
curl_setopt($ch, CURLOPT_HTTPHEADER, $curlheader);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
function get_session_token($onetimetoken)
{
$output = make_api_call("https://www.google.com/accounts/AuthSubSessionToken", $onetimetoken);
if (preg_match("/Token=(.*)/", $output, $matches))
{
$sessiontoken = $matches[1];
} else {
echo "Error authenticating with Google.";
exit;
}
return $sessiontoken;
}
3. Add the following code to the return url file.
$sessiontoken = get_session_token($_GET['token']);
$contacts = make_api_call("http://www.google.com/m8/feeds/contacts/default/thin?alt=json&max-results=1000", $sessiontoken);
$contacts = json_decode($contacts, TRUE);
foreach ($contacts['feed']['entry'] as $contact)
{
$emails[] = $contact['gd$email'][0]['address'];
}
//$emails array has the email addresses of all the contacts.
4. That’s it folks. It is that simple.
Further reading :
1. http://code.google.com/apis/contacts/
2. http://code.google.com/apis/contacts/docs/3.0/reference.html
Importing Contacts from Yahoo Mail using OAuth
May 28th
Importing contacts from mail accounts using OAuth is a long solved problem which doesn’t have a good implementation. I googled and googled for a good library which imports contacts from Yahoo/GMail/Hotmail. Finally, I was forced to create one by myself. Here are the instructions on how to get it up and running for Yahoo Mail in your server in 2 mins.
1. You need a Yahoo API Key to fetch contacts from Yahoo. Proceed to Yahoo Developer Dashboard and create a key.
Application URL in the createKey page is the URL to which you will be redirected to after a successful/failed login attempt.
Application Domain is be your domain name.
Choose “This app requires access to private user data.” in the Access Scope and under the options which appear below that, select “Read” access for “Yahoo! Contacts”.
Store the API Key, Shared Secret and Application ID carefully.
2. Download Yahoo Social SDK for PHP from http://developer.yahoo.com/social/sdk/php/
$this->config->item(‘yahoo_consumerkey’) is your Yahoo API Key.
$this->config->item(‘yahoo_consumersecret’) is your Yahoo Shared Secret.
$this->config->item(‘yahoo_applicationurl’) is your Application URL you provided in Step 1.
$this->config->item(‘yahoo_applicationid’) is your Yahoo Application ID.
3. Add the following code where the user has to select Yahoo Mail
<a href="<?php echo YahooSession::createAuthorizationUrl($this->config->item('yahoo_consumerkey'), $this->config->item('yahoo_consumersecret'), $this->config->item('yahoo_applicationurl').'?data=abc'); ?>">Fetch Yahoo Contacts</a>
You may remove the ?data=abc if you don’t want to pass any data to Yahoo. Anything you pass here can retrieved back at Application URL as GET parameters.
4. Include the required files from the opensocial SDK library and then add the following snippet to the Application URL you provided in Step 1.
if (YahooSession::hasSession($this->config->item('yahoo_consumerkey'), $this->config->item('yahoo_consumersecret'), $this->config->item('yahoo_applicationid')))
{
$session = YahooSession::requireSession($this->config->item('yahoo_consumerkey'), $this->config->item('yahoo_consumersecret'), $this->config->item('yahoo_applicationid'));
$user = $session->getSessionedUser();
$contacts = $user->getContacts(0, 1000);
foreach ($contacts->contacts->contact as $contact)
{
foreach ($contact->fields as $field)
{
if ($field->type == "email")
{
$emails[] = $field->value;
}
}
}
//$_GET['data'] will be equal to "abc" at this page.
}
//$emails array has the email addresses of all the contacts.
5. That’s it folks. It is that simple.
Further reading :
1. http://developer.yahoo.com/social/sdk/php/
2. http://developer.yahoo.com/oauth/
Recent Comments