Getting Command Output via SSH2 Functions in PHP

April 18th, 2013 No comments

Running command and getting the output via ssh2 is not as straightforward as running command directly using function exec(), it requires a few function calls using “stream”.

The following code illustrates how to do it:

// get connection
$conn = ssh2_connect($host, $port);

// login via pub-private keys
ssh2_auth_pubkey_file($conn, $username, $publicKey, $privateKey);

// run command via ssh2
$stream = ssh2_exec($conn, 'php -v');
stream_set_blocking($stream, true);
$stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);

// get the output
echo stream_get_contents($stream_out);

This is simply for my personal note for future reference, but hopefully it can also help who might be looking for solutions for this.

Categories: PHP, Programming Tags:

Remove “Block Configuration” Option in Account Page in Drupal 6

April 14th, 2013 No comments

In Drupal 6, it shows the “Block Configuration” for Navigation by default for all user accounts. It is useless and confusing for most of the users in our case.

Without knowing what it is, it would be hard to turn it off in the backend. It is as simple as two steps required in the backend.

profile-page

Firstly go to “Administor” >> “Site Building” >> “Blocks”, and then click on “configure” button next to the “Navigation” block:

blocks-list

Then click on “Users cannot control whether or not they see this block”, and save the settings.

navigation-block-config

Now the annoying block configuration is gone:

drupal-account-page-removed-block-configuration

Hope it helps..

Categories: CMS Tags: ,

Remove ‘The content of this field is kept private and will not be shown publicly.’ Form Help Text in Drupal 6

April 9th, 2013 No comments

Using profile module, I have set up a number of fields for user information. Because they are for members’ profile only, I set them as private field in the backend, but when user is editing the form in the frontend, each field shows help text “The content of this field is kept private and will not be shown publicly.” underneath.

drupal-6-form-field-help-text

This looks especially ugly when you have lots of fields to show and my client requested to remove it completely.

There are a couple of ways to remove it, but for my purpose, it is as simply as remove the code generated this text.

Open file /modules/profile/profile.module and simply comment out line 340:


function _profile_form_explanation($field) {
  $output = $field->explanation;

  if ($field->type == 'list') {
    $output .= ' '. t('Put each item on a separate line or separate them by commas. No HTML allowed.');
  }

  if ($field->visibility == PROFILE_PRIVATE) {
    //$output .= ' '. t('The content of this field is kept private and will not be shown publicly.');
  }

  return $output;
}

You might also want to use String Overrides module for Durpal 6 if you don’t feel to modify the source code.

Categories: General Tags: ,

Weekly Goal – Cycling 200KM By Friday

March 3rd, 2013 No comments

I have lots of friends who do close, or more than 200KMs of cycling each week. I have been riding to working for more than 1 year now, but only limited to one way each day of 22KMs, so averaging to around 100KMs/week.

I would like to push myself a bit harder this week. So I have setup a goal in RunKeeper to ride at least 200KM by this Friday, which means I will simply need to cycle both ways to and from work each day.

Screen Shot 2013-03-04 at 9.56.35 AM

I have also check the weather and it turned out it will be sunny until next week, so it is a good week to choose.

Categories: Diary Tags:

Disable Drupal Comment Preview Button

February 25th, 2013 No comments

Drupal 6 only allows you to set the comment preview button be either “Required” or “Optional”, but does not allow you to turn it off totally. But you can turn it off easily by updating your theme’s CSS file.

To hide the preview button, add the following to your theme’s CSS:

#comment-form #edit-preview {
    display: none;
}

Hope this helps..

Categories: CMS Tags:

Copy Hadoop Data From One HDFS to Another

February 21st, 2013 No comments

If you have two HDFS cluster operating on two different places (production vs alpha for example), sometimes you might want to copy some data from one cluster to another.

To do it is easy using Hadoop’s internal “distcp” command:

hadoop distcp hdfs://hadoop-namenode/data/2013/01 hdfs:///data/2013/

We have the following directory structure in the source:

/data/2013/01/01
/data/2013/01/02
/data/2013/01/03
/data/2013/01/04
...
...
...

The end result from the above command will simply copy the directory “01″ and its sub-directories to the destination HDFS, so that we will end up with the same directory structure.

This is a very handy and useful tool to copy data in HDFS.

Categories: General Tags:

PHP – All Combinations Between Array Elements

February 13th, 2013 No comments

OK, this is what I need to solve.

Given an array of the following elements:

$array1 = array(1, 2);
$array2 = array(3);
$array3 = array(4, 5);

I want to get the following combinations:

array(1, 3, 4)
array(1, 3, 5)
array(2, 3, 4)
array(2, 3, 5)

It is quite obvious that I will need a recursive function to do this, but it is not straightforward to start writing the function straightaway. With the hope that someone else might have already got such a function to do the task, I started googling and found a perfect post from Amoeba

However, his function only prints out combinations with strings, I have modified it slightly so that it returns arrays for my own purpose, and without using of global variables:

public static function getArrayCombinations($arr, &$final, &$codes = array(), &$pos = 0)
{
    if(count($arr)) {
        for($i=0; $i<count($arr[0]); $i++) {
            $tmp = $arr;
            $codes[$pos] = $arr[0][$i];
            $tarr = array_shift($tmp);
            $pos++;
            self::getArrayCombinations($tmp, $final, $codes, $pos);
        }
    } else {
        $final[] = $codes;
    }

    $pos--;
}

It works perfectly fine and is exactly what I am looking for. Thanks for the post and it saved me lots of time to write this recursive function.

Categories: PHP, Programming Tags:

Find All Possible Combination Of An Array In PHP

February 1st, 2013 No comments

Today, one of the tasks involved at work required me to solve the problem outlined in the title of this post.

For example, given an array of (‘red’, ‘blue’, ‘green’), we want to return the following:

red 
blue 
red blue 
green 
red green 
blue green 
red blue green

Initially my first impression was to use recursive function call, but it turned out to be quite complicated. After googling, Tom Butler has a very simple solution. I have modified it so that it will return an array of combinations:

public static function getQuestionCombinations($array)
{
    $return = array();

    $num = count($array);

    // The total number of possible combinations
    $total = pow(2, $num);

    // Loop through each possible combination
    for ($i = 0; $i < $total; $i++) {
        //For each combination check if each bit is set
        $data = array();
        for ($j = 0; $j < $total; $j++) {
           // Is bit $j set in $i?
            if (pow(2, $j) & $i) {
                $data[] = $array[$j];
            }
        }

        if($data) {
            $return[] = $data;
        }
    }

    return $return;
}

Tom has already explained in this post about the idea behind the function. This saved me heaps of time.

Categories: PHP, Programming Tags:

WordPress vs Joomla 2013 [INFOGRAPHIC]

January 23rd, 2013 No comments

I have tried both WordPress & Joomla before when I was trying to learn the new CMSes several years ago. My very first impression was that WordPress was so easy to pick up straightaway without getting too much detail, however, Joomla was totally the opposite. It was really hard to understand and it took lots of steps to get one page setup.

A few years has passed and I am stick with WordPress. It works great and meets all my needs, so I have not re-tried with Joomla since then. Red Giant Design has kindly produced an infographic compare those two CMSes in detail. And again, it proved my original theory / experience.

WordPress v Joomla - Infographic by Red Giant Design

I have also tried Drupal recently and found that it is quite easy to use and learn. And I am currently building a website for my friend using Drupal. From my personal point of view, WordPress is great for personal blog, while Drupal & Joomla are designed mainly for building business / commercial website using CMS tools.

I hope they (Red Giant Design) can produce another infographic detail the differences between the three would be awesome.

Keep up the good work!

 

Categories: CMS, General Tags: , , ,

Git Push & Delete Remote Branches

January 18th, 2013 No comments

We all know that after we have finished with a certain feature branch in git, we can remove it easily by running the following command in shell:


git branch -d feature/branch-name

However, this only deletes this specific feature branch locally on your machine, and it does not remove the one sits on the server.

To delete the feature branch on the remote server, the syntax is a bit odd, but you will get used to it:


git push origin :feature/branch-name

That is, you still use “push” command, even though you are deleting it. And this command works with the “branch -d” command independently, so you need to run those two commands to remove both local and remote branches.

Categories: General Tags: