New domain

My blog moved here: https://blog.cavar.pw/

Posted in Uncategorized | Leave a comment

Yeah! Framework Initial Configuration

After creating initial project your structure might look like this:


.
├── app_name
│   ├── config
│   ├── controllers
│   │   └── HomeController.php
│   ├── data
│   ├── models
│   └── views
│       ├── index.php
│       └── layouts
│           └── default.php
├── app_name_bootstrap.php
├── lib
└── web
    └── app_name.php

This is default project structure for Yeah! framework and it doesn’t require any additional configuration. Application will try and guess default paths and configuration. For starters, we can ignore whole [app_name] directory and focus on the simple application created. Open [app_name]_bootstrap.php and you can see the following:

$app->routeGet('/', function() {
   echo "You have successfully created Yeah! framework application";
});

That’s called a simple route. It tells the framework to execute closure when a HTTP client sends a request with GET method to your site root. There are several methods available matching HTTP verbs (GET, POST, PUT, DELETE). You can test them:

$app->routeGet('/get', function() {
   echo "GET method used";
});
$app->routePost('/post', function() {
   echo "POST method used";
});
$app->routePut('/put', function() {
   echo "PUT method used";
});
$app->routeDelete('/delete', function() {
   echo "DELETE method used";
});

Route will not execute if it doesn’t match HTTP method and URI. More on routing in another post.
To make necessary objects available to closure consider using “use”:

$app->routeGet(/, function() use(&$app) {
   $request = $app->getRequest();
   $response = $app->getResponse();
});
Posted in PHP, Posts in English, Programming, Web development, Yeah! | Tagged , , , , , , , , , , , | Leave a comment

Install and configure Yeah! DevTools

Hi people, long time passed since my last post. I’ve been busy with various things, but now I’m working to redeem myself. I’ve started writing simple learning PHP framework until it became a monster. Today it’s a full blown framework. I tried to make it as loosely coupled as I could. Classes should be usable on their own individually but some level of strictness is implemented. This one should be the first in a series of posts about how you can write complex PHP applications with ease and integrate external services by using a few design patterns. Framework is called Yeah! and it’s helper tools are called Yeah! DevTools. You can find both at their respective repository links:
https://bitbucket.org/palethorn/yeah/src
https://bitbucket.org/palethorn/yeah-devtools/src
This post is about initial installation of framework development tools, which you can skip actually, but nonetheless, it makes it easier to use the framework.
Clone the tools:

git clone https://bitbucket.org/palethorn/yeah-devtools.git

On windows you can clone it somewhere for easy access for example C:\yeah-devtools. On linux you can clone it wherever and use alias to point to main entry script:

alias yeah="php /path/to/yeah/script"

Create a folder in your webroot:

cd /var/www/
mkdir project_name
cd project_name
yeah create_app app_name

Or on windows:

cd C:\xampp\htdocs\
mkdir project_name
php C:\yeah-devtools\yeah create_app app_name

Start the server:

yeah server:start localhost 5000 app_name

Navigate to localhost:5000 and you’re ready to go.

Posted in PHP, Posts in English, Programming, Web development, Yeah! | Tagged , , , , , , , , , , , , , | Leave a comment

Install and configure bind9 DNS server

Bind9 is the DNS server used to resolve domain names to IP addresses. Since it’s the most popular, I’ll be explaining here how you can set it on your own linux box. I’m running my Debian Squeze GNU/Linux, but the configuration stays similar on most distros. Let’s start with the installation. Bind9 is present in my debian repository so I’m just going to use apt to install it:

# apt-get install bind9 dnsutils

After the installation is complete we can start configuring its environment since we will run it chrooted for security purposes. First we need to tell the server to start in its own environment with the following modifications. First open the file /etc/default/bind9 with vi or nano or whatever you want, as root, and modify the line:

OPTIONS="-u bind"

to look like this:

OPTIONS="-u bind -t /var/lib/named"

After that we can start setting up the environment. Execute the following command sequence to create additional directories and files:

# mkdir -p /var/lib/named/etc
# mkdir /var/lib/named/dev
# mkdir -p /var/lib/named/var/cache/bind
# mkdir -p /var/lib/named/var/run/bind/run
# mv /etc/bind /var/lib/named/etc
# ln -s /var/lib/named/etc/bind /etc/bind
# mknod /var/lib/named/dev/null c 1 3
# mknod /var/lib/named/dev/random c 1 8
# chmod 666 /var/lib/named/dev/*
# chown -R bind:bind /var/lib/named/var/*
# chown -R bind:bind /var/lib/named/etc/bind

We created environment in /var/lib/named and as far bind9 is concerned that is a root directory.
Restart bind9. I use init script which came with the installation:

# /etc/init.d/bind9 restart

Now you can test it by adding server ip in your network configuration and just go to some site. Try stopping your server, then try to open some website. If it cannot open, run your server, then try again. It should be working now. If it’s not opening the page there is some error in your configuration, and you should check it.
Now we can add some domains for it to resolve.
Create directory /etc/bind/zones/master

# mkdir -p /etc/bind/zones/master

Create the zone file:

nano /etc/bind/zones/master/home.zone

You can name your zone file whatever you like, but it’s best to use your domain as the filename. Add the following in the file:

;
; BIND data file for home
;
$TTL    604800
@       IN      SOA     ns.home. info.home. (
                                     1         ; Serial
                                  7200         ; Refresh
                                   120         ; Retry
                               2419200         ; Expire
                                604800)        ; Default TTL
;
@       IN      NS      ns.example.com.
home.    IN      MX      10      mail.home.
home.    IN      A       192.168.1.1
ns                     IN      A       192.168.1.1
www                    IN      CNAME   home.
mail                   IN      A       192.168.1.1
ftp                    IN      CNAME   home.
home.           IN     TXT     "v=spf1 ip4:192.168.1.1 a mx ~all"
mail                   IN      TXT     "v=spf1 a -all"

Instead of home you can use whatever you like, for example, my.home or home.lan, but try not to create ambiguous name. For example, if you choose wordpress.com for your domain it won’t go to wordpress as it will resolve to your selected server. Now you can access your http server by directing your browser to http://www.home.
After adding your zone file you need to direct the server to use it. Open the file /etc/bind/named.conf.local and add this:

zone "home" {
       type master;
       file "/etc/bind/zones/master/home.zone";
};

Restart your server and start using it.
To test it start pinging it:

$ ping ns.home

Do the nslookup which should return something like this:

$ nslookup www.wordpress.com

nslookup www.wordpress.com
Server:		192.168.1.1
Address:	192.168.1.1#53

Non-authoritative answer:
www.wordpress.com	canonical name = lb.wordpress.com.
Name:	lb.wordpress.com
Address: 74.200.244.59
Name:	lb.wordpress.com
Address: 76.74.254.120
Name:	lb.wordpress.com
Address: 76.74.254.123
Name:	lb.wordpress.com
Address: 72.233.2.58
Name:	lb.wordpress.com
Address: 72.233.69.6
Name:	lb.wordpress.com
Address: 74.200.243.251

If everything goes smoothly you can start using your local DNS server.

EDIT: If someone runs into some problems you could just enable config to work with IPV4 by editing /etc/default/bind9 and add “-4” to options like this:

OPTIONS="-4 -u bind -t /var/lib/named"

Thank you for reading.

Posted in General, GNU/Linux, Posts in English, Web development | Tagged , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , | 3 Comments

Decoupling

Someone asked me what can he do with interfaces and abstract classes. Plenty of fun there as we shall see now. I’ll use C++ and C# in this example, and I’ll present this on simple math operations. Interfaces and abstract classes are different in more than one way. For example, interfaces can’t contain any properties, or implemented methods, and they can’t contain access modifiers, while abstract classes can. One thing in common between interface and abstract class is that they can’t be instantiated, only derived and implemented. Interface defined in C# because it’s the easiest:

    interface IintOperation
    {
        int doIt(int a, int b);
    }

So what can you do with and interface? Consider this example:

    class Plus : IintOperation
    {
        public int doIt(int a, int b)
        {
            return a + b;
        }
    }
    class Minus : IintOperation
    {
        public int doIt(int a, int b)
        {
            return a - b;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            IintOperation p = new Plus();
            IintOperation m = new Minus();
            Console.WriteLine((p.doIt(3, 2).ToString()));
            Console.WriteLine((m.doIt(3, 2).ToString()));
        }
    }

I’ve successfully and completely used polymorphism. Never need to worry what kind of properties class contains and how to handle them in different sections of your project. Call on the behavior of the object and let it work itself on it’s own. That’s the whole point of OOP. Now, if you want your class to have some properties you can do it like this:

    class Plus : IintOperation
    {
        public int a, b, result;
        public Plus(int a, int b)
        {
            this.a = a;
            this.b = b;
            this.result = 0;
        }
        public int doIt()
        {
            result = a + b;
            return result;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            IintOperation p = new Plus(3, 5);
            // returns 5
            p.doIt();
        }
    }

Regardless of the public access modifier on properties a and b of the corresponding class, after initialization those properties are not visible. You can only see and call public method doIt(). This is called decoupling and it’s pretty powerful. It’s used when we want to minimize the dependency between multiple building block of an application. If the changes occur on one part it should not affect the other. Here’s an example of it’s usage in event processing. This will be a complicated example for newbies, but when you’re working with multiple classes it makes sense:

    interface IintOperation
    {
        int doIt();
    }
    class Plus : IintOperation
    {
        public delegate void op(IintOperation o);
        public op handle;
        int a, b, result;
        public Plus(int a, int b)
        {
            this.a = a;
            this.b = b;
            this.handle += handler;
        }
        public void handler(IintOperation o)
        {
            result = o.doIt();
        }
        public int add()
        {
            handle(this);
            return result;
        }
        public int doIt()
        {
            result = a + b;
            return result;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            plus p = new Plus(3, 2);
            Console.WriteLine((p.add().ToString()));
        }
    }
}

I’ve used delegates here instead of standard events because you can define the parameters as you like. In this case handler accepts one object type operation and we automatically have access to doIt() method. You can notice that a and b are not accessible and we can use this method to handle multiple objects with various implementations if they are of the same subtype.
In C++ things differ a little. It does not support abstract or interface keywords, but you can still define abstract class. In C++, a class is considered abstract if it contains one or more pure virtual functions. Like the following example:

class  IntOperation
{
	public:
		virtual int doIt() = 0;
};

And after that you can inherit that class and implement doIt() method like this:

class Plus: public IntOperation
{
	private:
		int a, b;
	public:
		Plus(int a, int b)
		{
			this->a = a;
			this->b = b;
		}
		int doIt()
		{
			return a + b;
		}
};

Now we can use that class to create object of a subtype IntOperation:

#include "operation.h"
#include <iostream>
int main(void)
{
	IntOperation *p = new *lus(3, 2);
	std::cout << p->doIt() << std::endl;
	return 0;
}

Unlike C++, C# supports abstract keyword and you can define abstract class like this:

class IntOperation
{
    abstract int doIt();
}

And the usage is similar.
Please note the differences between abstract classes and interfaces as I mentioned them on the top of this post and use them wisely.
Thank you for reading.

Posted in CSharp, OOP, Posts in English, Programming | Tagged , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , | Leave a comment