sensimevanidus

7 April, 2011

boolean toggle

While exploring Tumblr, I saw the following post:

ec1975:

…when switching don’t mind using:

if ($val==0) $val = $1; else $val = 0;

instead use:

$val = abs ($val-1);

It’s ok (except the $1 typo); but the following solution is far more efficient:

$val ^= 1;

I saw the post and wanted to leave a comment; but I couldn’t. That’s why I’m posting this. :)

sensimevanidus

22 July, 2010

Change Default Controller in Zend Framework

By default, Zend Framework dispatches a request to the IndexController if no other controller is defined (modules, controllers and actions are selected via URL). Like me, you may want to change the default controller for some reason. In Zend Framework, you can do that by adding or changing the resources.frontController.defaultControllerName property in the application.ini file. It looks like this:

resources.frontController.defaultControllerName = "auth"

Now Zend Framework sees the AuthController as the default controller.

That’s all. Hope this helps…

sensimevanidus

20 July, 2010

Fetching Configuration Data from Action Controllers in Zend Framework

If you’re familiar with Zend Framework, you probably know what an application-specific configuration file is. I tend to use application.ini file (located under the /application/configs directory) to store my application’s configuration data and generally use these variables in the bootstrap file.

Today, I needed to reach smtp configuration values from within a controller and it took some time to figure out how to do that although it was simple as this:

$mailOptions = $this->getInvokeArg('bootstrap')->getOption('mail');

This code snippet fetches these mail configuration data; 

mail.transport.smtp.host = "<some_host_value>"
mail.transport.smtp.port = "<some_port_value>"
mail.transport.smtp.user = "<some_user_value>"
mail.transport.smtp.pass = "<some_pass_value>"

from the application.ini file as a PHP array:

array(1) {
["transport"]=>
array(1) {
["smtp"]=>
array(4) {
["host"]=>
string(19) "<some_host_value>"
["port"]=>
string(3) "<some_port_value>"
["user"]=>
string(19) "<some_user_value>"
["pass"]=>
string(7) "<some_pass_value>"
}
}
}

You can also fetch all configuration data via:

$options = $this->getInvokeArg('bootstrap')->getOptions();

That’s all! Hope that helps…

sensimevanidus

30 June, 2009

Using Gmail SMTP within CodeIgniter

I tried to send emails via Gmail SMTP (smtp.gmail.com) using CodeIgniter; but got the following error:

ERROR - 2009-06-30 16:54:21 —> 220 mx.google.com ESMTP g9sm76291gvc.10 
<br /><pre>hello: 250-mx.google.com at your service, [85.107.180.116]
250-SIZE 35651584 
250-8BITMIME
250-STARTTLS
250-ENHANCEDSTATUSCODES
250 PIPELINING
</pre>Failed to send AUTH LOGIN command. Error: 530 5.7.0 Must issue a STARTTLS command first. g9sm76291gvc.10 
<br /><pre>from: 530 5.7.0 Must issue a STARTTLS command first. g9sm76291gvc.10 
</pre>The following SMTP error was encountered: 530 5.7.0 Must issue a STARTTLS command first. g9sm76291gvc.10 
<br /><pre>to: 530 5.7.0 Must issue a STARTTLS command first. g9sm76291gvc.10 
</pre>The following SMTP error was encountered: 530 5.7.0 Must issue a STARTTLS command first. g9sm76291gvc.10 
<br /><pre>data: 530 5.7.0 Must issue a STARTTLS command first. g9sm76291gvc.10 
</pre>The following SMTP error was encountered: 530 5.7.0 Must issue a STARTTLS command first. g9sm76291gvc.10 
<br />502 5.5.1 Unrecognized command. g9sm76291gvc.10 
<br />The following SMTP error was encountered: 502 5.5.1 Unrecognized command. g9sm76291gvc.10

As you can see, it kept saying “Must issue a STARTTLS command first” which means Gmail allowed only SSL connections. So I had to change the host value to:

ssl://smtp.gmail.com

and port value to:

465

And it worked! Note that you need to have OpenSSL support in your PHP installation.