Pages

Showing posts with label server. Show all posts
Showing posts with label server. Show all posts

Sunday, January 26, 2014

Debian/Linux/Ubuntu-Configure Postfix to use Gmail

Configure Postfix to use Gmail

Install necessary packages first

 sudo apt-get install postfix mailutils libsasl2-2 ca-certificates libsasl2-modules  

 Configure Postfix using


 sudo dpkg-reconfigure postfix  


Navigate to Postfix Directory

 cd /etc/postfix  

Open Postfix config file using following command

 sudo gedit main.cf  

Add following lines to it


 relayhost = [smtp.gmail.com]:587  
 smtp_sasl_auth_enable = yes  
 smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd  
 smtp_sasl_security_options = noanonymous  
 smtp_tls_CAfile = /etc/postfix/cacert.pem  
 smtp_use_tls = yes  

Now we need to create a file called sasl_passwd


 sudo vi /etc/postfix/sasl_passwd  

Add the following line to the same


 [smtp.gmail.com]:587  USERNAME@gmail.com:PASSWORD  

Configure permissions for sasl_passwd


 sudo chmod 400 /etc/postfix/sasl_passwd  
 sudo postmap /etc/postfix/sasl_passwd  

Validate Certificates


 cat /etc/ssl/certs/Thawte_Premium_Server_CA.pem | sudo tee -a /etc/postfix/cacert.pem  

Reload Postfix


 sudo /etc/init.d/postfix reload  

Test Mail from Terminal


 echo "Test mail from postfix" | mail -s "Test Postfix" you@example.com  

Copy of my php.ini mail section


 SMTP = ssl://smtp.gmail.com  
 smtp_port = 587  
 auth_username = usrname  
 auth_password = pwd  
 sendmail_from = from_mail  

Sunday, September 15, 2013

OOPS-Static keyword,Singleton Design Pattern and its implementation

Static keyword is useful in creating Singleton design pattern because in such a scenario changes made by 1 object to a variable is reflected in all the objects of that class in that request.

Here is my example code with Singleton Design Pattern.

What I was trying to do is I was trying a singleton object over different request's and was trying to see whether it maintains its state or not. And it doesn't I tried running this page from different browser's and I failed to retrieve data of previous request's or session's.

Singleton Pattern is just needed so that only one object access at a time a resource to avoid conflict's

Singleton's are used to write log's and database access,maintain configuration variable's.

And that is when I realized the importance of an Application Server over a Web Server where we can create application object's.

 <?php  
 session_start();  
 //Singleton class example  
 error_reporting(E_ALL);  
 final class UserInfo  
 {  
   static $user=null;  
   static $instance=NULL;  
   public static function Instance()  
   {  
     if (self::$instance === null) {  
       self::$instance = new UserInfo();  
     }  
     return self::$instance;  
   }  
   /*  
    * Shows the array  
    */  
   public static function showUser()  
   {  
     if(self::$user!=null)  
     {  
       foreach(self::$user as $key=>$value)  
       {  
         echo "<br/>Sessionid ".$key." is assigned to ".$value."<br/>";  
       }  
     }  
   }  
   /*  
    * Adds session Id and user to the array  
    */  
   public static function AddUser()  
   {  
     echo "<br/>";  
     echo "Object count is ==>";  
     echo $ucount=count(self::$user);  
     echo "<br/>";  
     if(self::$user===null)  
     {  
       self::$user=array();  
       self::$user[session_id()]="User ".($ucount+1);  
       //print_r(self::$user);  
     }  
     else   
     {  
       if(!(array_key_exists(session_id(),self::$user)))  
       {  
         self::$user[session_id()]="User ".$ucount+1;  
       }  
     }  
   }  
   private function __construct()  
   {  
   }  
 }  
 echo "My Session Id is :". session_id();  
 UserInfo::Instance()->AddUser();  
 $fact1= UserInfo::Instance();  
 $fact= UserInfo::Instance();  
 $fact1->AddUser();  
 $fact->showUser();  
 UserInfo::Instance()->showUser();  
 if($fact1===$fact)  
   echo "Matches";  
 /*  
  * ERROR LINE  
  */  
 //$fact3= new UserInfo();  
 ?>  

Output is:
 My Session Id is :p15snn5ebgn52ftsj0eb43e4n0  
 Object count is ==>0  
 Object count is ==>1  
 Sessionid p15snn5ebgn52ftsj0eb43e4n0 is assigned to User 1  
 Sessionid p15snn5ebgn52ftsj0eb43e4n0 is assigned to User 1  
 Matches  

As you can see above is mine singleton class with a private constructor.I have 2 static methods one to store data and other to show data

In first case I have added user name as value and corresponding session id as key to the array.
I have called my method in 2 way's directly via class and by an object.

In the second call count goes 1

Finally I call show user in 2 way's and the 2 object's matches as per the condition

Thursday, February 7, 2013

Request data from a server in a different domain

To request data from a different domain we use json with padding

or jsonp(json with padding)

we use Response to come in a callback function

Refer to following urls

http://itfeast.blogspot.in/2012/07/cross-domain-request-using-jquery.html
http://en.wikipedia.org/wiki/JSONP
http://api.jquery.com/jQuery.ajax/


script = document.createElement(”script”);
script.type = text/javascript”;
script.src = http://www.someWebApiServer.com/some-data”;
 
We get data like this
 
<script>
{['some string 1', 'some data', 'whatever data']}
</script>
 
Due to inconvenience in fetching data from script tag jsonp was introduced as follows
 
script = document.createElement(”script”);
script.type = text/javascript”;
script.src = http://www.someWebApiServer.com/some-data?callback=my_callback”;
 
Now since we have passed a parameter to return jsonp data we will always get sth like
 
my_callback({['some string 1', 'some data', 'whatever data']});
 
in the script tags     
 
Let's take an simple example of using the twitter feed.
 
RAW javascript demonstration (simple twitter feed using jsonp) 

<html>
    <head>
    </head>
    <body>
        <div id = 'twitterFeed'></div>
        <script>
        function myCallback(dataWeGotViaJsonp){
            var text = '';
            var len = dataWeGotViaJsonp.length;
            for(var i=0;i<len;i++){
                twitterEntry = dataWeGotViaJsonp[i];
                text += '<p><img src = "' + twitterEntry.user.profile_image_url_https +'"/>' + twitterEntry['text'] + '</p>'
            }
            document.getElementById('twitterFeed').innerHTML = text;
        }
        </script>
        <script type="text/javascript" src="http://twitter.com/status/user_timeline/padraicb.json?count=10&callback=myCallback"></script>
    </body>
</html>
 
And Basic jQuery example (simple twitter feed using jsonp)
 
<html>
    <head>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
        <script>
            $(document).ready(function(){
                $.ajax({
                    url: 'http://twitter.com/status/user_timeline/padraicb.json?count=10',
                    dataType: 'jsonp',
                    success: function(dataWeGotViaJsonp){
                        var text = '';
                        var len = dataWeGotViaJsonp.length;
                        for(var i=0;i<len;i++){
                            twitterEntry = dataWeGotViaJsonp[i];
                            text += '<p><img src = "' + twitterEntry.user.profile_image_url_https +'"/>' + twitterEntry['text'] + '</p>'
                        }
                        $('#twitterFeed').html(text);
                    }
                });
            })
        </script>
    </head>
    <body>
        <div id = 'twitterFeed'></div>
    </body>
</html>