SSH Login without password

This is actually a repost from the tutorial I often ran onto when working with servers. Lately, the sys-ad I was working with changed users home directory and also changed their passwords, but gave me the root pass. However, I don’t want to bother him for new passwords, and I guess he setup new ones from password generator apps/scripts, which are of course, really really recommended nowadays. Since I have the root pass, and I don’t want to go back to chmod-chown each and every files/folders I created via root, I thought I can automate logging in with each account I need to work ‘as’ their respective users. Or I can use cat /etc/passwd to know their home directories.

First log in on A as user a and generate a pair of authentication keys. Do not enter a passphrase:

a@A:~> ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/a/.ssh/id_rsa):
Created directory '/home/a/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/a/.ssh/id_rsa.
Your public key has been saved in /home/a/.ssh/id_rsa.pub.
The key fingerprint is:
3e:4f:05:79:3a:9f:96:7c:3b:ad:e9:58:37:bc:37:e4 a@A

Now use ssh to create a directory ~/.ssh as user b on B. (The directory may already exist, which is fine):

a@A:~> ssh b@B mkdir -p .ssh
b@B's password:

Finally append a’s new public key to b@B:.ssh/authorized_keys and enter b’s password one last time:

a@A:~> cat .ssh/id_rsa.pub | ssh b@B 'cat >> .ssh/authorized_keys'
b@B's password:

Now I can work as ‘owner’ and ‘user’ within each user home directory.

a@A:~> ssh b@B hostname
B

Some notes, depending on your version of SSH you might also have to do the following changes:

  • Put the public key in .ssh/authorized_keys2
  • Change the permissions of .ssh to 700
  • Change the permissions of .ssh/authorized_keys2 to 640

Source: http://linuxproblem.org/art_9.html

Mastering CSS Coding: Getting Started (Smashing Magazine)

I’ve been practicing strict CSS rules for years now, but it was only this year that I changed the way I fix floated containers inside another container, to stretch the height down to the bottom of the floated inner container. I guess almost everyone started with <br /> with ‘clear’ or the next element with CSS ‘clear’ in it to fix the issue, but we all know it’s ‘dirty’.

I found this great tutorial that would make a ‘css nobody’ become a ‘big guy’ on CSS, and I myself is going to keep this page available when needed.

6_b

Mastering CSS Coding: Getting Started covers:

  1. Padding vs. Margin
    • What Is Padding and Margin?
    • Margin and Padding Values
    • Quick Tip
  2. Floats
  3. Center Alignment
    • Horizontal Alignment
    • Vertical Alignment
  4. Ordered vs. Unordered Lists
    • Customizing Unordered Lists
    • Using Unordered Lists for Navigation
  5. Styling Headings
  6. Overflow
    • What Is Overflow?
  7. Position
    • Adding Flavor With CSS
  8. Background Images
    • Using Large Backgrounds
    • Text Replacement
    • CSS Sprites
  9. Image Enhancement
  10. PSD to HTML

PHP code block – bringing order to chaos

I had to come up with this post because I’ve been working on an old CMS about 3 years old (or even more) and I had been wondering why the calendar links cuts off from 2001 to 2003.

Task: List a year/month links from the present to the oldest published image sets.

Sounds easy, and I believe it is.

But when I checked on the existing script, this is what I got.

 $month_array = array("1"=>'January',"2"=>'February',"3"=>'March',"4"=>'April',"5"=>'May',"6"=>'June',"7"=>'July',"8"=>'August',"9"=>'September',"10"=>'October',"11"=>'November',"12"=>'December');
	$smarty->assign('y2day',$y2day); $y=0;
	for ($q=12; $q >= 1; $q--){
			$month = $month_array[$q];
			if( ($_GET[year] < '2000') && ($m2day ==$q ) ) $w_string = $week_string; else  $w_string ='';
			if( ($_GET[year] == '2007') && ($_GET[month] == $q) ) $w_string = $week_string;
 
		  if($m2day >= $q){
			$tmp_cal_month_now_array = array(
					'month' => $month,
					'string' => $w_string,
					'link' => $q
					); $date_month_now_array[$y++] =  $tmp_cal_month_now_array;	 }
		} //for
 
	 $smarty->assign('calendar_now_month',$date_month_now_array);
 
	$y=0;	$c_yeartoday = date("Y") -1; //echo $c_yeartoday;
	for($q=0; $q < = 5; $q++){
			$tmp =$c_yeartoday - $q; //echo $tmp;
 
		 $tmp_cal_date_array = array(
					  'year' => $tmp
 
				 );  $date_year_array[$y++] =  $tmp_cal_date_array;
	 } //for
 
	$smarty->assign('calendar_year',$date_year_array);
 
	    $y=0;
		for($q=12; $q >= 1; $q--)
		{
			$month = $month_array[$q];
			if( $_GET[month] == $q ) $w_string = $week_string; else $w_string='';
			$tmp_cal_month_array = array(
					'month' => $month,
					'string' => $w_string,
					'link' => $q
					); $date_month_array[$y++] =  $tmp_cal_month_array;
 
		}//for
	 $smarty->assign('calendar_month',$date_month_array);
 
	 $y=0;
		for($q=12; $q >= 10; $q--)
		{
			$month = $month_array[$q];
			if( ($_GET[year] == '2000') && ($_GET[month] == $q) ) $w_string = $week_string; else $w_string='';
			$tmp_cal_month_old_array = array(
					'month' => $month,
					'string' => $w_string,
					'link' => $q
					); $date_month_old_array[$y++] =  $tmp_cal_month_old_array;
 
		}//for
	 $smarty->assign('calendar_month_old',$date_month_old_array);

Continue reading

Variable, variables and lots of variables

List of variable names, oddly named and other stuff I’m glad I bumped into :D . Found them in project about 5 or 6 yrs old that needs modification and I believe optimization too, yes really. So why glad? Because I realized the way I named my variables isn’t that bad at all.

/**
 * This one is on top of a movie player page. Maybe 'he' wasn't so sure 
 * that the parameters passed on to this page is for a 'video'. Just making
 * sure for the nth time I guess.
 */
$videoistrue=0;
/**
 * I think 'he' hate lines. And this one is good, page title is saved 
 * in a session variable, so if another programmer writes a new page
 * and forgot to assign $_SESSION['pagetitle'] a new value for an FAQ
 * page, say faq.php, it'd show up <title>Show Series</title>
 */
session_start();$_SESSION['pagetitle'] = "Show Series";
/**
 * ... and it went on and on
 * $_SESSION['pagetitle'] = "Calendar";
 * session_start();$_SESSION['pagetitle'] = "Browse Model";
 *
 * Hmmmm, $ii as iteration variable, struggling with names? I often 
 * use $i, $n or $x, sometime $c, hehehe! I've seen $i for many. And 
 * have you noticed the class name 'smalleditnoline', can it be any 
 * longer? Yes! and if he had enough time, it may have look like 
 * 'smalleditnolinewitharialbold' or 'smalleditnolineFFFbgcolor333fgcolor'
 * crazy! This is CSS anyway, will surely post one for that, hahaha!
 */
for ($ii=1;$ii< =$totalpages;$ii++)
{
  if ($ii==$page) {
    echo "<font class='smallediton'>[$ii]";
  }
  else {
    echo "<a href='?sort=" . $_GET['sort'] . "&page=$ii' class='smalleditnoline'>[$ii]</a>";
  }
}
/**
 * more variables with default values, note that this is set just
 * before a loop starts
 */
$thisCat = "";
$firstrun = true;
$firstrow = 0;
$rowcount = 0;
/**
 * "NO QUOTES" for key names in square brackets :( 
 * if only my IDE could complain
 */
$GLOBALS[catadded] = false;
$GLOBALS[keywordadded] = false;
$GLOBALS[modeladded] = false;
/**
 * Consistent! Maybe 'He' doesn't want anybody to easily understand 
 * the script that's why no new lines after semi-colon at least.
 */
if ($seriesRow[seriesNo] < 10) $seriesNoTxt = "0".$seriesRow[seriesNo]; else $seriesNoTxt = $seriesRow[seriesNo];

Ok, enough of grumbling here. I have released my frustration for today, going back to work. But if I am to start a project or even a simple script, I follow formatting conventions, consistent quotes and escapes, logical variable names and useful comments with links and samples. It doesn't only make your code readable, it helps a lot for the next programmer to just add-in the stuff he needs to and keeping his sanity.

PHPAnywhere – edit PHP from browser

Found this post from Smash!ng Apps

PHPanywhere is a new online service that’s changing the way people develop on the web. They enable users to develop and maintain their php/html projects online using any standard web browser.

This is a web based free Integrated Development Environment or IDE for the PHP language, in other words it is an application that gives developers all the code editing capabilities they need to develop PHP applications online. It includes a real-time syntax code editor with support for all web formats and a powerful FTP editor.

Because it works in a browser you can start coding right away, no need for installing anything and best of all you can work from anywhere, all you need is an internet connection and a web browser and you’re ready to go.

You can develop and maintain multiple projects with ease using PHPanywhere, because as soon as you log in all your projects (FTP servers) are immediately accessible. PHPanywhere even remembers what files you were working on and reopens them as well.

Real-time syntax code editor

The real-time syntax code editor is really what PHPanywhere is all about, so here is a list of just some of the things it does:

  • Syntax highlighting
  • Smart indentation for each language
  • Code folding
  • Full internationalization
  • Unlimited undo/redo (now handled on client (browser) side, no more waiting for server response)
  • Unlimited tabs
  • Find and replace feature (New Feature)
  • Displays line numbers (New Feature)
  • Code auto completion, snippets (New Feature)
  • Displays line numbers (New Feature)
  • Code indention (TAB key) (New Feature)

Continue reading

Smarty Gravatar plugin

I realize the need for this when working on an MVC based CMS especially with Smarty engine in it.

Oh and in case you’re wondering why its sounds like the upcoming movie Avatar by James Cameron, I don’t know. What it has to do with internet term ‘avatar‘? I really don’t know. But in ‘www’, Gravatar is a short term for “Globally Recognized Avatar” assigned by a user as his/her default avatar in every places of the internet, same people behind WordPress. There are other similar services too.

So, this plugin is based from its documentation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
 * Gravatar
 *
 * @link http://www.marvinmarcelo.com
 *
 * @param array $params
 * @param Smarty $smarty
 */
function smarty_function_gravatar($params, &$smarty)
{
  /**
   * constant $gravatar_host
   */
  $gravatar_host = "http://www.gravatar.com/avatar/";
 
  if ( !isset($params['email']) )
  {
    $smarty->trigger_error("gravatar: email parameter not set");
    return;
  }
 
  if ( !eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $params['email']) )
  {
    $smarty->trigger_error("gravatar: {$params['email']} is not a valid email.");
    return;
  }
  /**
   * @link http://en.gravatar.com/site/implement/url
   */
  $hash = strtolower(md5(trim($params["email"])));
  $src = $gravatar_host . $hash . ".jpg?";
 
  if ( !isset($params["size"]) || !($params["size"] >= 1 && $params["size"] < =512 ))
  {
    $params["size"] = 32;
  }
  $src .= "s={$params["size"]}";
 
  if ( !isset($params["default"]) )
  {
    $params["default"] = "identicon";
  }
  $src .= "d={$params["default"]}";
 
  if ( !isset($params["rating"]) )
  {
    $params["rating"] = "G";
  }
  $src .= "r={$params["rating"]}";
 
  $extras = "";
 
  if ( isset($params["class"]) )
  {
    $extras .= " class=\"{$params["class"]}\"";
  }
 
  if ( isset($params["id"]) )
  {
    $extras .= " id=\"{$params["id"]}\"";
  }
  /**
   * @example http://www.gravatar.com/avatar/7023218434f12aee57f4b03454dadcaa?s=32&d=identicon&r=G
   */
  print "<img src=\"$src\" alt=\"{$params["alt"]}\"$extras />";
}

Simply extract function.gravatar.zip into your Smarty plugin directory. And use it in your template file like this

{gravatar email='someemail@domain.com'}

You’re done!

Javascript Encryption

jCryption, is simply Javascript Encryption. “j” stands for jQuery as core framework. And as always, everybody likes jQuery.

Normally if you submit a form and you don’t use SSL, your data will be sent in plain text.
But SSL is neither supported by every webhost nor it’s easy to install/apply sometimes.
So I created this plug-in in order that you are able to encrypt your data fast and simple.
jCryption uses the public-key algorithm of RSA for the encryption.

Author’s site had a good documentation on usage.

Fix unresponsive browser when serving files via PHP

After an hour long reading his frustrations over IM, I began google’ing on files served thru PHP but locks clicks in a client’s browser until download is finished. It is unresponsive until the download is complete.

// add this function before a readfile() or chunked with fopen()
session_write_close();
// header goes here, content-type, filename, etc
readfile($filepath);

And, also, I found this class to support downloading using a download manager.

firefox 3.5 rc2 on Hardy

If you follow install procedures of Firefox 3.5 on Ubuntu, it will setup a new Mozilla browser instance named Shriketo “Shiretoko”, and in your main menu will appear as “Minefield”.

I had it installed, but removed it. I’ll wait for final release in default apt sourcelist. Also removed entries there I added using the tutorials. Why? Not much of a reason, I just prefer Firefox in the title bar. I’d like it even better if they change letter “e” in place of the globe there :D