Archive for the ‘PHP’ Category

Enabling PHP mail function functionality on Debian

By Automater on August 19th, 2010
1
2
3
sudo apt-get install sendmail
sudo sendmailconfig (just say "Y" to all the questions)
Posted in

PHP to Drupal: Get up to speed with drupal from a php developer background

By Automater on July 29th, 2010

While working on various projects with drupal I realized that there is really no one definitive place which lists the concepts that you need to become familiar with when dealing with drupal. So I decided that I would put them in one place for people who maybe are experienced php developers who are trying to learn to work with drupal for the first time.

The main important components when working with drupal on a serious project are:

1. Pages

A page is a unit that most web developers are most familiar with already. And that is the problem, because when you are creating a website with drupal, the code is not organized in these bite sized chunks. The beauty of the drupal model of work is that drupal separates what you need to write into re-usable chunks of code which can be used on numerous pages, and is not confined to one page in particular.

That being said, there are still instances where pages are used, but not in the same way as in a website you have to program from scratch. Often times pages are used instead as a means to configure areas of drupal, as admin pages etc.

2. Blocks

A block is a small chunk of code which can be placed on the same page as page content, or on a node (which I will explain next). The block is a handy thing because it is easy to put blocks across a large number of pages, and it is also possible to let the admin user configure the visibility of a block based on the user’s permission level (called a user role) via the admin interface.

These properties make blocks powerful in that you can put them in almost any configuration on a page, but not care ahead of time what else is on the page. In effect leaving page design to a designer or site admin.

3. Nodes

A node is a very important part of drupal, because it ties in so deeply with the drupal structure. A node is an object in drupal. The node can be displayed and expanded and acted upon by modules. In the drupal module repository there are many modules which will act on nodes allowing for some really cool things.

The node can be a somewhat difficult concept to grasp, but thinking of it as a procedural object which can be acted upon, displayed, filtered, and restricted as a group. So a grouping of these “nodes” (known as a content type) can be exposed to different user roles. This means that if you want a bunch of pages which do the exact same thing you would create a new content type and then the individual nodes would all have the same structure.

4. Theming

Theming is important in drupal as it makes any code which is written and enables the designer to alter the output of that code, by adding tags, markup and css in order to make the website pretty. In this it is important to expose all html to drupal when you are writing it so that a designer can later open up the theme folder and overwrite your theming function adding these ever important tags.

The theming engine in drupal is quite powerful and it is possible to procedurally theme small subsets of a page consistantly on a website by using this theming technique.

5. Modules

A module is where the exciting things happen in drupal. Because as a drupal developer you will want to avoid hacking drupal core as much as possible. This makes upgrading simple and easy, as well as keeping the codebase clean for your modules, making it possible for them all to work together instead of having one module vs. another.

The module system in drupal again is very powerful because of the node structure, and drupal’s ability to edit queries which are written in one area of drupal, or written in one module by another.

6. Menu

The menu structure in drupal is also important. This is the area of drupal which exposes certain menu items based on permissions, and also changes based on where you are in your website’s tree structure. It is important to use the menu structure on your drupal websites because this structure is much more powerful than doing it by hand. And because of the permission based feature.

7. Testing

Going forward into drupal 7 testing is becoming increasingly important in the drupal community. This testing is becoming a part of the core list of modules, but also it is very helpful even in drupal 5 and drupal 6. Unit testing ensures that if you make changes to your code, that it does not effect the code you have already written.

There has recently been a big push for testing to increase the productivity of coding and reduce the number of errors in the code. Drupal’s testing is mostly done with the simpletest module found at http://drupal.org/projects/simpletest.

So this is my broad overview of what concepts a php programmer needs to know when starting to work with drupal. Please let me know if I forgot anything.

Posted in

Installing Xdebug on Ubuntu 10.04

By Automater on May 21st, 2010

Install Xdebug

1
sudo apt-get install php5-xdebug

Update xdebug.ini
Now we need to update the xdebug.ini file. Use the following command to open the file with gedit:

1
<code>sudo gedit /etc/php5/apache2/conf.d/xdebug.ini</code>

Change the file so it looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
<code>; configuration for php xdebug module
zend_extension="/usr/lib/php5/20090626/xdebug.so"
xdebug.remote_enable=1
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.remote_host=127.0.0.1
xdebug.remote_port=9000</code>

Restart Apache
All left to do now is restart Apache with the following command:

1
<code>sudo /etc/init.d/apache2 restart</code>

Now you can check if all is OK with phpinfo(). You should see the following text:

This program makes use of the Zend Scripting Language Engine:
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
with Xdebug v2.0.5, Copyright (c) 2002-2008, by Derick Rethans
with Suhosin v0.9.29, Copyright (c) 2007, by SektionEins GmbH

Setup Virtual Hosts on Ubuntu/Debian using Apache2 + MySQL5.X + PHP5

By Automater on May 21st, 2010

This entire tutorial is done after entering the ’sudo -i’ command which allows us to act as the SUPER-USER for the entirety of our terminal session.

1
sudo -i

1) Install the required modules from the command line

1
apt-get install apache2 mysql-server mysql-client php5 php5-cli php5-mysql

2) Change the directory to /etc/apache2/sites-available

1
cd /etc/apache2/sites-available

3) If you run the ‘ls’ command while in the sites-available directory you should see the following

1
ls

Output

1
default  default-ssl

4) Copy the ‘default’ config to a site specific config. For this tutorial I am using dev.scottfaisal.com.

1
cp default dev.scottfaisal.com.conf

5) Make the application directory

1
mkdir /var/dev.scottfaisal.com

6) Open the file with an editor of your choosing. I prefer NANO.

1
nano dev.scottfaisal.com.conf

The output below is the entire file but I will discuss certain parts that we will need to edit.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<VirtualHost *:80>
ServerAdmin webmaster@localhost

DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>

ErrorLog /var/log/apache2/error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog /var/log/apache2/access.log combined

Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>

</VirtualHost>

7) Add the name of the server. This will be the name that you type in the URL field of your web browser (IE: Firefox)

1
2
3
4
5
6
7
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName dev.scottfaisal.com
...

8 ) Point the Virtual host to the correct directory

1
2
3
4
5
6
7
8
9
10
11
12
13
...
DocumentRoot /var/dev.scottfaisal.com
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
...

9) Also make the change here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
...
<Directory /var/dev.scottfaisal.com/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
...

10) So this is what your dev.scottfaisal.com.conf file should look like when you are done. Write/Quite the file and we will move on.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName dev.scottfaisal.com

DocumentRoot /var/dev.scottfaisal.com
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/dev.scottfaisal.com/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>

ErrorLog /var/log/apache2/error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog /var/log/apache2/access.log combined

Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>

</VirtualHost>

10a) IMPORTANT! Make a symbolic link in the sites-enabled directory

1
ln -s /etc/apache2/sites-available/dev.scottfaisal.com.conf /etc/apache2/sites-enabled/000-dev.scottfaisal.com.conf

11) We need to edit our /etc/hosts file

1
nano /etc/hosts

Output

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
127.0.0.1       localhost
127.0.1.1       servername

# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts

12) Right beneath the ‘localhost’ definition, add the following

1
127.0.0.1      dev.scottfaisal.com

13) The complete file looks like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
127.0.0.1       localhost
127.0.0.1       dev.scottfaisal.com
127.0.1.1       servername

# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts

14) Create an index.php file in /var/dev.scottfaisal.com for testing purposes.

1
nano /var/dev.scottfaisal.com/index.php

Contents of .php file.

1
2
3
4
5
6
7
8
9
<?php

echo 'This is a test!!!!';
phpinfo();

?>

15) Reboot the apache2 process.

1
/etc/init.d/apache2 restart

16) Now open up your web browser and enter dev.scottfaisal.com into the URL bar and it should work!

UPDATE: I forgot to mention that in this environment, I like to configure log files for each virtual host. This is our current dev.scottfaisal.com file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName dev.scottfaisal.com

DocumentRoot /var/dev.scottfaisal.com
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/dev.scottfaisal.com/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>

ErrorLog /var/log/apache2/error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog /var/log/apache2/access.log combined

Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>

</VirtualHost>

Just change the ErrorLog and CustomLog names to match the virtual host.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
...
ErrorLog /var/log/apache2/dev.scottfaisal.com_error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog /var/log/apache2/dev.scottfaisal.com_access.log combined
...
</VirtualHost>

PHPUnit Assertions For Unit Testing

By Automater on May 20th, 2010
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
assertArrayHasKey()
assertClassHasAttribute()
assertClassHasStaticAttribute()
assertContains()
assertContainsOnly()
assertEqualXMLStructure()
assertEquals()
assertFalse()
assertFileEquals()
assertFileExists()
assertGreaterThan()
assertGreaterThanOrEqual()
assertLessThan()
assertLessThanOrEqual()
assertNotNull()
assertObjectHasAttribute()
assertRegExp()
assertSame()
assertSelectCount()
assertSelectEquals()
assertSelectRegExp()
assertStringEqualsFile()
assertTag()
assertThat()
assertTrue()
assertType()
assertXmlFileEqualsXmlFile()
assertXmlStringEqualsXmlFile()
assertXmlStringEqualsXmlString()
Posted in

Xdebug installation in ubuntu/debian

By Automater on May 20th, 2010

The Xdebug is the extension for PHP that helps debugging PHP scripts by providing a lot of valuable debug information. The debug information includes stack traces and function traces in error messages, memory allocation and protection for infinite recursions.

1
apt-get install php5-xdebug
Posted in

Best MYSQL Database Field Type for IP address storage

By Automater on May 19th, 2010

IP addresses should be stored in a database an INT UNSIGNED:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
mysql> SELECT INET_ATON(‘192.168.0.10′) AS ipn;

+————+

| ipn |

+————+

| 3232235530 |

+————+

mysql> SELECT INET_NTOA(3232235530) AS ipa;

+————–+

| ipa |

+————–+

| 192.168.0.10 |

So you can store an IP address in an INT UNSIGNED (4 bytes) which is of course much more efficient and faster than a CHAR(15). Naturally, you can call the function while you’re inserting, so something like this is fine also:

1
INSERT INTO tbl VALUES (..., INET_ATON('192.168.0.10'), ...)
Posted in
Tags

Preventing SQL injection attacks by making form post values safe

By Automater on May 19th, 2010

try the following before inputting any values in database.

1
$_POST = array_map('mysql_real_escape_string', $_POST);

Using ImageMagick with PHP

By Automater on May 19th, 2010

There is a PHP module for Imagemagick, called imagick, sort of like the one for GD.

Following is some simple code that has worked for me:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
if(!extension_loaded('imagick')) { dl('imagick.so'); }
$handle = imagick_create();
imagick_set_attribute($handle,array("quality"=&gt;1,"magick"=&gt;"png"));
imagick_set_attribute($handle,"size","98x20");
imagick_read($handle,"xc:#cccccc");
imagick_annotate($handle,array(
"primitive"     =>  "text 10,14 hello",
"pointsize"     =>  16,
"antialias"     =>  1,
"stroke"        =>  "#000000",
"font"          =>  "arial.ttf"
));
$handle = imagick_copy_rotate ($handle, 270);
header("Content-type: image/png");
imagick_dump($handle,"png");
Posted in

Installing Imagemagick and Imagick support for PHP in Debian

By Automater on May 6th, 2010

Real Simple

Step 1: Install Imagemagick

$ apt-get install imagemagick

Step 2: Imagick support for PHP

$ apt-get install php5-imagick

Step 3: Restart Apache

$ /etc/init.d/apache2 restart

if you need imagemagick for your Ruby, you can install the librmagick-ruby package.