Raju Gautam

Zend Certified Engineer – PHP 5; Joomla, Web Service, Wordpress Developer


E-mail: raju@devraju.com
raju.rachana@gmail.com
Tel: +977-985-111-3638

About Me

Hello, this is Raju Gautam, a PHP/JavaScript Programmer, PHP/AJAX Developer, Perl Programmer, RoR Programmer. I have been working as a Web Programmer since 2005 as soon as I completed my graduation. Though I worked with different other script/programming languages i.e. Perl, RoR, ASP (traditional), Visual Basic 6 but I have to say that my specialization has been only in PHP, MySQL, JavaScript/Ajax Programmer in recent couple of years.

Installing Percona 5.6 in Ubuntu 13.10 Saucy !

Posted by rajug On January - 17 - 2014 0 Comment

First of all, I would recommend login as root so that you don’t have to sudo every time you hit the command and give the root password.

$ su -

Now you should be logged in as root. Mine now is

root@rajug:~#

Step 1:
First of all lets remove everything that is related to MySQL from the machine.

apt-get remove mysql*
apt-get purge mysql*
apt-get autoremeve

Step 2: Adding the key
Now lets add the key that is provided by Percona, we have to add this to be allowed to install from Percona server.

apt-key adv --keyserver keys.gnupg.net --recv-keys 1C4CBDCDCD2EFD2A

Step 3: Adding the repository
Lets add the Percona repository in the sources list in the machine/Ubuntu.

vim /etc/apt/sources.list

Add the following two lines in the source list:

deb http://repo.percona.com/apt saucy main
deb-src http://repo.percona.com/apt saucy main

Step 4: Apt-Pinning the packages
In some cases you might need to “pin” the selected packages to avoid the upgrades from the distribution repositories. You’ll need to make a new file /etc/apt/preferences.d/00percona.pref and add the following lines in it:

vim /etc/apt/preferences.d/00percona.pref

Add the following line:

Package: *
Pin: release o=Percona Development Team
Pin-Priority: 1001

Step 5: Update

$apt-get update

Step 6: Install Percona Server and Client

$ apt-get install percona-server-server-5.6 percona-server-client-5.6

Step 7: Install dependencies
If there are any dependencies left, then lets install them

$ apt-get install -f

Step 8: Testing
To make sure if Percona Server is installed in your machine with MySQL simply try:

$ mysql -u root -p

And it showed me :

root@rajug:~# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.6.15-63.0 Percona Server (GPL), Release 63.0

Copyright (c) 2009-2013 Percona LLC and/or its affiliates
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

That means I have now Percona Server !

I had updgraded my Ubuntu from 13.04 to 13.10 last week and everything went quite good. The only problems that I faced as developer are:

  1. Apache 2.4 was not working with default previous installation and configuration and I had to remove it (completely uninstall/purge and autoremove) and install it again.
  2. All the virtual host setting did not work. I did not go for opting to change the Include setting in apache2.conf instead I rewrote all the settings myself.
  3. Another annoying and very strange problem was that PHP and its libraries. First I had problems with installing apache module that connects to PHP. Secondly I had problems of missing mcrypt library which is required for couple of projects on my system. Particularly a project developed in Zend 1.11 and phpmyadmin did not work. I had the following error where I had written some methods for AES encryption with PHP’s mcrypt library.
Fatal Error: Undefined function mcrypt_encrypt()

I thought the mcrypt library was missing in my PHP installation and I simply tried to install it via command line as follows:

$ sudo apt-get install php5-mcrypt

And I got the following:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
php5-mcrypt is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 13 not upgraded.

Weird :(. If it is already there why isn’t it working with my projects? Then I Googled for sometime and could not find exact solution anywhere. I tried to look the ini in /etc/php5/conf.d/ and there was mcrypt.ini file with quite proper content in it:

; configuration for php MCrypt module
extension=mcrypt.so

Again I pulled my hair out and went out for the tea. Then I came and tried to look at the other folders inside /etc/php5 folder and saw there mods-available which had again some ini files in it but missing mcrypt.ini. Then I just tried moving that mcrypt.ini file from /etc/php5/conf.d/ to /etc/php5/mods-available/ then restarting the apache server.

$mv /etc/php5/conf.d/mcrypt.ini /etc/php5/mods-available/
$ sudo service apache2 restart

Then I tried phpmyadmin from the browser, amazingly it worked now !

Hope this will help others who are facing the same issue.

I don’t know if there are other ways to have a .PEM file from a PFX file in Linux (Ubuntu), but after couple of research/study in the internet, I came to find some work around. First of all what those formats are actually, let me explain what I came to understand.

PFX Format
Originally defined by RSA in the Public-Key Cryptography Standards, the “12″ variant was enhanced by Microsoft. This is a passworded container format that contains both public and private certificate pairs. Unlike .pem files, this container is fully encrypted. Every time I get one I have to google to remember the openssl-fu required to break it into .key and .pem files.

PEM Format
Governed by RFCs, it’s used preferentially by open-source software. It can have a variety of extensions (.pem, .key, .cer, .cert, more). And in most cases while we need to use the private and public keys together in scripting languages like PHP then we have this format.

Converting PFX file to certificate and key format first
Openssh is the open source application to convert these. The command openssl in Linux comes with lots of other options but we will be using few here:

openssl pkcs12 -in yourpfxfile.pfx -clcerts -nokeys -out desired_certificate_filename.cer

This command extracts public key to desired_certificate_filename.cer.

openssl pkcs12 -in yourpfxfile.pfx -nocerts -nodes  -out desired_publickey_filename.key

This command extracts private key to desired_certificate_filename.key.

PEM File
Now lets combine both into one .pem file.

cat desired_certificate_filename.cer desired_certificate_filename.key > desired_pem_filename.pem

Hope this will help !
:)

I was struggling to check session expiry for Zend_Auth and redirect the users to login page with certain message to let them know the session has expired. Found following code to limit the session expiry time:

$authns = new Zend_Session_Namespace(Zend_Auth::getInstance()->getStorage()->getNamespace());
$authns->setExpirationSeconds(60 * 15); // for 15 minutes

Then the major problem that I encountered while setting the messages using Flash Messenger plugin. Though I am not quite experienced Zend programmer but since last couple months, I had used the Flash Messenger plugin directly from within action controllers. It seems the plugin is automatically loaded in the action controllers. But I was about to check the user’s login session expiry in one of my own custom plugin which extends Zend_Controller_Plugin_Abstract as follows:

class FHF_Acl_AccessCheck extends Zend_Controller_Plugin_Abstract
{
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        /**
         * @author Raju Gautam
         * Check login session and throw some message for expiry
         */
        $hasIdentity = Zend_Auth::getInstance()->hasIdentity();
        if(!$hasIdentity && $module != 'user' && $controller != 'user' && $action != 'login'){
            // set some message and login form
        }
    }
}

Where I have checked if the the Zend_Auth has null identity and show/load the login form with some message. But while setting the message first I tried:

$this->_helper->FlashMessenger->addMessage(array('error' => "Session expired, please login again !"));

But this did not worked, threw error message instead.

So I researched then found in one of the pages in the internet and found the following solution:

class FHF_Acl_AccessCheck extends Zend_Controller_Plugin_Abstract
{
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        /**
         * @author Raju Gautam
         * Check login session and throw some message for expiry
         */
        $hasIdentity = Zend_Auth::getInstance()->hasIdentity();
        if(!$hasIdentity && $module != 'user' && $controller != 'user' && $action != 'login'){
            $flashmessenger = Zend_Controller_Action_HelperBroker::getStaticHelper ( 'FlashMessenger' );
            $flashmessenger->addMessage (array('error' => 'Session Expired ! Please login again !'));
            $request->setModuleName('user')
                    ->setControllerName('user')
                    ->setActionName('login');
        }
    }
}

So if anyone wants to set the flash messages from outside of the action controllers, then you should use something like this using Zend_Controller_Action_HelperBroker class and its static helper getStaticHelper.

$flashmessenger = Zend_Controller_Action_HelperBroker::getStaticHelper ( 'FlashMessenger' );
$flashmessenger->addMessage (array('error' => 'Session Expired ! Please login again !'));

With this, you can get any plugin outside of the action controllers.

Hope this will help others struggling for the same.

Good luck!

Thanks.

Avoid login screen in phpmyadmin and auto login

Posted by rajug On December - 5 - 2012 1 Comment

phpMyAdmin is the mostly used web based PHP application for MySQL client. When we use it in the local systems, we always need to enter username and password to login. To avoid prompting the login screen you need to overwrite the existing settings with our own. phpMyAdmin lets us to overwrite the settings/configurations by using a file in the root of its directory.

A file is always delivered named config.sample.inc.php in its root. You just need to rename it to config.inc.php and change the following line:

$cfg['Servers'][$i]['auth_type'] 	= 'cookie';

to

$cfg['Servers'][$i]['auth_type'] 	= 'config';

And add the following two lines:

$cfg['Servers'][$i]['user']         = 'root';
$cfg['Servers'][$i]['password']     = 'asdf#asdf'; // use here your MySQL password

Hope this helps someone who is willing to avoid login screen in phpMyAdmin.

Cheers !

If anyone of you trying to import some data from CSV directly into MySQL database, always use LOAD DATA LOCAL INFILE command. It is quite faster and easier to import lots of data within few seconds even more than 25MB of CSV data.

The syntax for the command can be found in details at http://dev.mysql.com/doc/refman/5.1/en/load-data.html but the simple one would be something like below:
> Login to the MySQL server first i.e. mysql -h localhost -u root -p (And give the root password).
> Use the specific database i.e. use .
> The run the following command:
LOAD DATA LOCAL INFILE "/path/to/csvfile.csv" INTO TABLE
FIELDS TERMINATED BY "," LINES TERMINATED BY "\n" (field1, field2, field3, field4, fieldN);

Note: Remember the fields should be exactly same as number of columns in the CSV.

The used command is not allowed with this MySQL version

This problem makes crazy and I had spent around 4 hours (half of the working day) to fix it. I was on Ubuntu 12.04 LTS and I was not sure where exactly to look into for this to fix. I tried to look at my.cnf but could not find anything out there. Actually this problem comes when the flag/setting “local-infile” is disabled.

First solution would be to add the following on the my.cnf itself:
[odbc]
local-infile=1

Or Secondly you can simply enable it while logging into the MySQL server.
> mysql --local-infile=1 -u root -p

Then run the above LOAD DATA LOCAL INFILE… command and it will work like a charm and you will get all the data from CSV to your MySQL database table.

Hope this helps you !

Today I was integrating MageBridge to work with Joomla and Magento together. The MageBridge stand alone was never a problem for me as I have been doing this in couple of other projects as well.

This time I had a little different where Magento has one very popular and useful module used called OneStepCheckout which gives a single page while checkout with all the available & required options in it. Normal process is working quite fine except one. There is feature in OneStepCheckout that already existing customer can login by clicking in a link called “Already registered? Click here to login.”. This was not working anymore when I browse the Magento from Joomla through MageBridge.

MageBridge has a good and almost precise tutorial on this as Using OneStepCheckout with MageBridge. As stated there under “Getting the JavaScript right” as “Also, make sure to configure a MageBridge Custom Block module that displays the block before_body_end. It is best to add a new Joomla! module position to your template just before the tag to add this module to.”

We should add custom block module in Joomla positioned at the end of the template page or i.e. just before the closing body tag.

So I added a module position mbbeforebodyend in the template at the end of the template page as:




And then added the MageBridge’s custom block module as illustrated below:

1. Add new module and select the module type.

 

 

 

 

 

 

2. Select the custom block Body Before End in the Block drop-down:

 

 

 

 

 

Hope this will help others facing same kind of issue.

Today I encountered a sudden problem with administrator login of Joomla 1.7 in Ubuntu 12.04 LTS. The error messages was something like this:

Login: 500 - An error has occurred!

The files were downloaded from LIVE site and tried to run in my laptop. In any case there was no lock.

After Googling sometime I found that it is because of something problems with permission settings for the Joomla folders. Actually in my case one of the required folders/directories “cache” was missed to be downloaded. So what I did to fix the error is:

1. Created the cache folder myself and gave 777 permissions.
2. Gave 777 permissions to following folders:
– tmp
– logs
– cache
– administrator/cache
3. Set all the files 644 permission and 755 for all rest of the folders (except images).

Though sometimes the error message could be due to something else too but in my case it worked like a charm. So I thought that I should share at least someone might be facing the same problem can get some information to fix.

Thanks :-)

Today I was struggling with an issue in Magento. There was a frequent Fatal error message as follows while creating/printing Invoice from Magento back end.

Fatal error: Declaration of Zend_Pdf_FileParserDataSource_File::__construct() must be compatible with Zend_Pdf_FileParserDataSource::__construct() in /web/procuriosqa/x/mikasei/lib/Zend/Pdf/FileParserDataSource/File.php on line 41

I was almost sure this is all about the problem with Zend Framework. I tried to look in the Google if anyone has posted a solution though the solution must be provided by the Zend Framework itself. Then I came to know that there is problem with the implementation of stricter inheritence rule in the framework as described in:

http://framework.zend.com/issues/browse/ZF-12093

http://zend-framework-community.634137.n4.nabble.com/PHP-5-4-0RC1-ZFv1-td4094438.html

And it is all about the PHP bug as reported as here:

https://bugs.php.net/bug.php?id=55375

So the solution would be either one of these:
- Comment out the constructor and destructor methods in the specified class
- Or provide the path of the file explicitly.

For me it was quite difficult to find the exact path of fonts in this case, so I opted for the first solution as suggested here as well:

https://github.com/zendframework/zf2/commit/ad43d3128d61ec67071872d4e4484ea2be1d4d8e

Until Zend Framework team fixed the issue and Magento puts this in the core Magento we don’t have other options as well I think.

When I commented two lines of codes used to define constructor and destructor methods in the file lib/Zend/Pdf/FileParserDataSource.php file which looks like:

abstract class Zend_Pdf_FileParserDataSource
{
    //abstract public function __construct();

    //abstract public function __destruct();
................................................

Now it worked fine.

Hope this will help others too!

Thanks and regards !

Raju

Slow email sending in Ubuntu 12.04 LTS solution

Posted by rajug On July - 10 - 2012 5 Comments

I assume that apache, PHP is running quite fine. But the sending emails from local machine is not working. You need to installl the sendmail and set the path in php.ini. To install follow the below simple steps:

1. Open the terminal and run the following command to install send mail and php mail.

sudo apt-get install sendmail php-mail postfix

2. Uncomment and change the sendmail_path in your file /etc/php5/apache2/php.ini. The line should now look like this:

sendmail_path = /usr/sbin/sendmail -i -t

Somethings without restarting the Apache it might not work so simply restart the apache server once:

$ /etc/init.d/apache2 restart

Now try to run the following PHP mail function:

if(mail("test@test.com", "This some subject", "This is some message body"))
	echo "Sent";
else
	echo "Not sent";

If the mail is sent successfully quickly then fine. But sometimes it does not send the email quickly. It takes sometime i.e. 2 minutes in my case. I googled a lot but could not find a quite proper solution. When I saw the mail log and came to know somehow that it is because not resolving the hostname. I checked my hostname “rajug” in /etc/hosts file which was missing.

1. To check the hostname of your system, run the following command if you don’t know the hostname.

$ cat /etc/hostname # rajug in my case.

2. Make sure something like following line is at your top of the /etc/hosts file.
127.0.0.1 localhost.localdomain localhost
To check it open the file in editor (I use gedit):

$ gedit /etc/hosts

And added the line at top. Actually localhost was there but my hostname “rajug” was not there. So I just added it.

3. Thirdly, edit the sendmail configuration file (/etc/mail/sendmail.cf in Ubuntu) and Uncomment the line:

O HostsFile=/etc/hosts

4. Now restart the sendmail and apache once.

$ /etc/init.d/sendmail restart
$ /etc/init.d/apache2 restart

Now try the above PHP script to send the email, it will send quite faster (2/3 seconds in my case).

Hope this will help others having the same problem !

:-)

Switch to our mobile site