Author Archives: Saroj Roy

Unable to connect to RDP server from Ubuntu using Remmina remote desktop client

I could connect to a Windows 7 machine using Remmina remote desktop client on Ubuntu 14.04. After few days or weeks suddenly it stopped working. I got this error: Unable to connect to RDP server.

In my case deleting entry from ~/.freerdp/known_hosts worked for me. I guess remote host Identification got changed for some reason.

Borrowed from: askubuntu.com/questions/157723/cannot-rdp-to-windows-7-with-remmina-on-12-04/214498

Problem in embedding images in Swiftmailer within html content having plain-text alternate

Swiftmailer is a very good library for sending email from PHP. It has nice object oritented interfaces. However, in recent days I’ve spent a lot of time finding the reason why the content get messed up in Swiftmailer when

  • The email has both html and alternate text content
  • Embed image in html content

Specification wise, mime structure for HTML with embedded images and plain text fallback is :

multipart/alternative
  text/plain
  multipart/related
    text/html
    image/png
    image/png
    image/jpeg

Swiftmailer has a bug and fails to generate correct mime type entries. The issue is reported here . Thankfully there is a workaround for that.

My previous code was:

$message = Swift_Message::newInstance();

$message->setSubject('Your subject')
        ->setFrom(array('[email protected]' => 'Saroj Roy'))
        ->setTo(array('saroj@saroj-HP-ProBook-4530s'))
        ->setBody('This is a plain text Message','text/plain')       
        ->addPart('Here is the <b>HTML message</b> with image : <img src="' .
                  $message->embed(Swift_Image::fromPath('image.png')).
                  '" alt="Image Missing" />', 'text/html');

$mailer = Swift_Mailer::newInstance(Swift_MailTransport::newInstance());

$result = $mailer->send($message);

I had to change the setBody() to addPart(). This change generates correct mime-type entries in message. However, this is still not 100% full-proof. Problem still occurs in MS Office 2003 and Thunderbird email client when content is viewed as text. These clients convert the html content to text by striping the html-tags out instead of showing the plain-text content. I suppose there are some bugs in these email clients. Please let me know it I were wrong with my assumptions.

By the way, Gmail and console based email client Mutt shows the content correctly.

 

Most accurate way to retrieve a user’s correct IP address in PHP

Sometimes it is required to determine cilent’s IP address in PHP applications. It is straight forward to get it from REMOTE_ADDR headers. However, it becomes tricky if there are proxies in-between.

Some application frameworks like Symfony has built-in method to get it. However, if you need to determine the IP address on your own, here is the the solution:

function get_ip_address(){
	$ipHeaders = array(
        'HTTP_CLIENT_IP', 
        'HTTP_X_FORWARDED_FOR', 
        'HTTP_X_FORWARDED', 
        'HTTP_X_CLUSTER_CLIENT_IP', 
        'HTTP_FORWARDED_FOR', 
        'HTTP_FORWARDED',
        'REMOTE_ADDR'
	);
    foreach ($ipHeaders as $key){
        if (array_key_exists($key, $_SERVER) === true){
            foreach (explode(',', $_SERVER[$key]) as $ip){
                // just to be safe
                $ip = trim($ip); 
                //filter the ip before returning
                if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false){
                    return $ip;
                }
            }
        }
    }
}

WARNING: REMOTE_ADDR still represents the most reliable source of an IP address. The other $_SERVER variables can be spoofed by a remote client very easily.

Ref: Stakeoverflow

Doctrine Error: The partial field selection of class must contain the identifier.

Whenever performing a partial selection, you need to include the primary key of the class you’re selecting from.

In my case, this error occurred when I tried to select a partial class without selecting the id field.

Here is my previous code:

$qb = $this->createQueryBuilder('d')
           ->select(array('partial d.{title, description}'));

Here is my final code:

$qb = $this->createQueryBuilder('d')
           ->select(array('partial d.{id, title, description}'));