Tuesday, April 30, 2013

Blog closed

After 27 months I have published posts and videos at Youtube, I decided to have a new chapter of my life. I will stop to blogging at May 1st 2013. If you want to contact me, please contact me via Twitter. I would like to thanks to my readers and God for opportunity to blogging tutorials and another stuff.

Thursday, April 25, 2013

How to create a virtual interface on CentOS or Debian

I was working on my DNS server and I wanted to create a virtual interface to distinguish internal requests from external requests. The command to create a virtual interface is:

ifconfig eth0:0 199.133.189.122 netmask 255.255.255.192 up

Tuesday, April 16, 2013

Design Patterns - Observer Pattern

Introduction

A design pattern is a specific way of solving a particular problem. They are not simply about reusing code; they are more abstract and generalized than that. They are about using reusing ideas.

The Observer Pattern

Often, you'll have data in your application in your application that changes over time. Say that you have some GUI components that are required to show this data and update it when it changes. How would you handle it? One solution might be to pass the newly updated data to a method of the GUI component so that could redraw the information. A problem with this approach is remembering to do that each time the data is updated. What if it's not clear how often the data will be updated, and whether you want the GUI to update automatically when it does?

The observer pattern solves this problem by using two interfaces, Observer and Observable. As the names suggests, the Observer "watches" the Observable to see whether it changes.

Keeping with the theme of human senses, an Observer is sometimes called a Listener, but for this chapter we stick with the former name.

In its most basic implementation, the Observable can add Observers. Observable then is responsible for notifying them if anything about its state has changed, and the Observer is responsible for reacting to the change. In this example, our data is the Observable and the GUI components are the Observers. If the data changes, those changes will automatically be reflected in any GUI component that is an Observer of the data. Next figure demonstrates the observer pattern

Widgets

Continuing with the previous example, you'll use the observer pattern to handle displaying price information for some of the instruments within graphical elements on a Web page. First, you'll need to define some simple graphical components. These components will be basic HTML table structures whose functionality will be contained within an object. Such components often go under the all-purpose term of widgets.

Designing the Widgets

A graphical widget will have two responsibilities. It will need to draw its own HTML so that it can be seen on a Web page, and it will need to update the data it displays. You may have noticed (from previous figure) that an update method is defined in the Observer interface.

Each widget is an Observer. The item being observed is the data representing the instrument name and price information. The data source is Observable.

All widgets, then, should implement the Observer interface. In addition, each one should descend from an abstract class that defines some shared functionality between widget objects. This is an example of using interfaces and abstract class together. Because the update() method is the same for all widgets, it can be implemented in the abstract class. The class diagram is shown in next figure.

Now all concrete implementations of widgets will descend from the AbstractWidget class. The AbstractWidget class in the turn implements the Observer interface. Notice the update() method in the class diagram for AbstractWidget is not shown in italics. This means that the method is actually implemented at that point. The # symbol indicates that the internalData property is protected. That means subclasses of AbstractWidget have access to it. If it were private, subclasses would not be able to access it.

All text and images above is from the book:"Professional PHP5" ISBN: 0-7645-7282-2 (Lecky-Thompson et al., 2005)

How to configure NTP Server and Client on CentOS

Introduction

NTP is widely used to synchronize a computer to Internet time servers or other sources, such as a radio or satellite receiver or telephone modem service. It can also be used as a server for dependent clients. It provides accuracies typically less than a millisecond on LANs and up to a few milliseconds on WANs. Typical NTP configurations utilize multiple redundant servers and diverse network paths in order to achieve high accuracy and reliability.

Configure NTP Server

  1. yum install ntp (and type 'y' when show up two questions);
  2. vi /etc/ntp.conf
  3. Add the next statement for accept requests from another servers (DNS server, LDAP server, etc.) on your LAN:
    restrict 10.50.0.0 mask 255.255.255.0 nomodify nopeer noquery
  4. Add the next statement for public servers NTP synchronize with server's internal clock on your LAN:
    server 0.europe.pool.ntp.org
    server 1.europe.pool.ntp.org
    server 2.europe.pool.ntp.org
    server 3.europe.pool.ntp.org
  5. This command allow to set current time from indicated NTP server:
    ntpdate pool.ntp.org
  6. The same server can be defined on configuration file:
    vi /etc/ntp/step-tickers
    pool.ntp.org
  7. service ntpd start
  8. Make sure the ntpd is active from beginning Linux boot;
    chkconfig ntpd on
  9. Checking synchronization status of service NTP.
    nptq -pn
    tail -f /var/log/messages

Configure NTP Client

  1. This file allows to set server NTP to use for initial adjust clock during starting of ntpd:
    vi /etc/ntp/step-tickers
    10.50.0.254
  2. On the file /etc/ntp.conf, all you need is add server NTP's IP address to use:
    vi /etc/ntp.conf
    server 10.50.0.254
  3. Checking synchronization status of service NTP.
    nptq -pn
    tail -f /var/log/messages
    It can take long to see message of success. When I was configuring, it took about 1 hour.

Conclusion

This protocol allows to keep operating system's clock synchronized with high accurate, running at information of synchronism got from another NTP servers.

Although the configuration is relatively simple, NTP performances a very important role. The possibility to make sure that clocks from many servers are kept with right time is relevant and it is a important factor to correct of many applications. The correction of local time in operating system allows also ensure that logs recorded in operating system have correct time and that is important when I use logs to diagnostic problems of some services.

Validation Email Address: Server side vs Client side

The week ago I was programming my web application using PHP5.

There is two ways to validate email:

  1. On server side (using filter_var from PHP5)
  2. On client side (using input type email from HTML5)

PHP5 example

$email = "xpto@email.com";
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo "This ".$email." email address is considered valid.";
}

HTML5 example

<form action="file.php" method="post">
  E-mail: <input type="email" name="useremail"><br>
  <input type="submit">
</form>

In my opinion it is better validate email on client side instead server side. So don't validate email on server side because it takes a lot work to warn user that has an invalid email address. With client side to validate email, it saves a lot work to warn user that has an invalid email address.

But if you are programming a web application using OOP, it is good idea to validate email from server side because you are not using form to validate email but you are using object, for example:

class User{
  private $name, email;
 
  public function __construct($name,$email){
   $this->name=$name;
   $this->email=validate($email);
  }
  
 private function validate($email){
  $mail = "";
  if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
   $mail = $email;
  }else{
    echo "This $email is not valid!";
   }
  return $mail;
 }
}
$user = new User("Peter","peter@xpto.com");//valid email
$user = new User("Paul","kjfddks");//prints "This kjfddks is not valid!"

Sharing folders without login - Samba

Samba is an open source software that allows to solve communications problems between Windows and Linux. With Samba, you can share folders, printers for Windows clients and can perform as a Primary Domain Controller (PDC) or as domain member. Samba can also be part of an Active Directory (AD) domain.

As a Systems Administrator, one of first things I wanted to do was share folders on Debian with my Windows to transfer easily files. My Debian is a local Web Server to test my PHP5 applications (yes, I am also PHP5 developer). Why do I want share folders without password? Because it is painful every time I need to login and I am working at home where is no danger around me. No one can crack me at home. At home, it is a great way to share files without login.

    How to install and configure samba
  1. su -
  2. insert your root's password
  3. mkdir /home/yourusername/mysharedfolder
  4. chown nobody /home/yourusername/mysharedfolder
  5. apt-get install samba
  6. nano /etc/samba/smb.conf
  7. security = share
  8. guest account = nobody
  9. [mysharedfolder]
    writable = yes
    path = /path/to/directory
    public = yes
    guest ok = yes
    guest only = yes
    guest account = nobody
    browsable = yes
    
  10. Ctrl+X and then y
  11. service samba restart

Optionally, you can use Samba Web Administration Tool (SWAT) to configure the settings of shared folder.

    How to install SWAT
  1. apt-get install swat
  2. Go to your favorite web browser and type the next URL: http://localhost:901
  3. insert your root login
  4. watch the next video how to configure samba

Samba is very useful tool for who want share files between Windows and Linux and SWAT is also very useful for who want configure settings of sharing easily.

I have created another article about Samba before but this one requires a login. If you want to do login, please read this article.

Box Model - CSS Tutorial

After I want to get better my skills as Web Designer, I decided to study again all concepts of CSS! And there is one thing I consider very important: Box Model.

But what is Box Model and why is it very important?
The Box Model is a box that wraps around HTML elements. It is very important because it allow the design become more organized and clean.

What consists Box Model?
Box Model consits of four things:

  1. Margin - Clears an area around the border. The margin does not have a background color, it is completely transparent;
  2. Border - A border that goes around the padding and content. The border is affected by the background color of the box;
  3. Padding - Clears an area around the content. The padding is affected by the background color of the box
  4. Content - The content of the box, where text and images appear

Design Patterns - Facade Pattern

Introduction

A design pattern is a specific way of solving a particular problem. They are not simply about reusing code; they are more abstract and generalized than that. They are about using reusing ideas.

The Facade Pattern

The best way to understand the facade pattern is to look at a component diagram of a system before and after using a facade.

The next figure shows the front end of a Web site communicating with an application on the Web server. In the right side of the diagram, the front end code is accessing various objects in the application. On the left, showing the facade, the front end communicates only with the facade object, which in turn delegates responsibilities to the internal objects.

All text and images above is from the book:"Professional PHP5" ISBN: 0-7645-7282-2 (Lecky-Thompson et al., 2005)

Monday, April 1, 2013

Design Patterns - Decorator Pattern

Introduction

A design pattern is a specific way of solving a particular problem. They are not simply about reusing code; they are more abstract and generalized than that. They are about using reusing ideas.

The Decorator Pattern

The two concrete widgets created for the observer pattern have a different appearance. If you needed to add a new style of widget, you would subclass Widget and implement the draw() method to write the HTML. Say that needed to add a feature to all existing widgets, such as border. You could go into each draw() method and add some more HTML to each, but then the border would be hard-coded and all widgets would be forced to have a border. You could create a new set of subclasses of existing concrete widgets that implemented the border in the draw() method. If you had only two widgets, as in the Observer example, this might be an option - you would end up with four widgets. However, if you had five widgets to start with, it might not seem so appealing. Suddenly you have a lot of widget classes to worry about.

There is another way to handle situations like this, one that doesn't require you to create a new subclass for every widget you want to add a border to. Using the decorator pattern allows you to add features or functionality to existing objects without using inheritance. Next figure shows the class diagram for the decorator pattern.

The class diagram indicates that Decorators are a type of Widget, too. This is good because we don't want to change the way we access a Widget, whether it's decorated or not.

All text and images above is from the book:"Professional PHP5" ISBN: 0-7645-7282-2 (Lecky-Thompson et al., 2005)