Category Archives: WEB Programming

Postgres Query with GPS distance calculations without PostGIS

Let’s consider the following scenario:
You have a Postgres Database and an addresses table which stores items with GPS latitude and longitude values. You want to select all the items located inside a circular area around a GPS point you have.

You can write your SQL query without using PostGIS extension.
Let’s define the following data:

1. table: streets
id | name | latitude | longitude

2. GPS_CENTER_LATITUDE, GPS_CENTER_LONGITUDE = coordinates of the center area point around which you wanna search streets

3. AREARADIUS = the area’s radius expressed in meters

select 
	count(*) 
from 
	(
		select 
			(acos(sin(radians(s.latitude)) * sin(radians(GPS_CENTER_LATITUDE)) + cos(radians(s.latitude)) * cos(radians(GPS_CENTER_LATITUDE)) * cos(radians(s.longitude-GPS_CENTER_LONGITUDE))) * 6371 * 1000) computedDistance, 
			s.name
		from 
			streets s 
	) 
as tempQuery 
where 
	computedDistance < AREARADIUS;

WordPress search and replace serialized strings in database

Every now and then I take little projects which involve developing a website in WordPress.
Naturally I install it on a test development machine, do all the stuff required and then move the thing to live server.

The moving part is sometimes painful especially if the site was populated with real data (articles) during testing phase by the client and he doesn’t want to loose the data already entered.

Since WP serializes it’s settings and then saves them into database, a quick search and replace for URLs and other variables which usually change when the site is moved onto other server and domain it’s not productive.

There are some WordPress plugins which help do the search and replace part, but I usually don’t like relying on plugins. And if the installation is dead, I cannot access the plugin anyway, so it’s pointless.

I found a little piece of software which does provide a tremendous help when moving a wordpress site to another hosting or domain and which does not rely on plugins.

The best thing it can do is it can search and replace a string in the entire database, unserializing strings when it has to and serializing them back.

It’s a PHP script you just drop into a secretly named folder on the same level with /wp-admin and the rest and then you navigate with the browser to that folder’s path and the script will try to automatically connect to WordPress’ database and initialize.

You can find the script here: https://interconnectit.com/products/search-and-replace-for-wordpress-databases/, many thanks to it’s creators! 🙂

Find if a point is inside a polygon with native PostgreSQL

There is the big and exhaustive PostGIS which can be installed into your PostgreSQL database and will provide you with lots of geometry functions and types you can use for your complicated geometry problems.

But what if you need, like I needed, to find out wether a point is inside a described polygon or not and you don’t want to go into all the trouble of installing PostGIS?

Well, good news. As I was happy to learn, PostgreSQL has native support for some geometry types and operators.

For instance, it supports the type polygon, which has the following syntax:


( (x1,y1) , (x2,y2), ... , (xn,yn))

It also supports the following operator which means “contains”

@>

So, let’s put this two together and make a functional example:

-- postgresql demo for point-in-polygon with native support

create table test_polygon
("id" serial,
"data" polygon NOT NULL
PRIMARY KEY ("id));

insert into test_polygon (data) values ('((1,1),(12,1),(12,12),(1,12))');

-- example of query which will validate a point as being inside the polygon
select * from test_polygon where data @> '(3,4)';

-- example of query which will invalidate a point as being inside the polygon
select * from test_polygon where data @> '(24,14)';

The code is simple and powerful. If you combine it with an online tool for generating a list of coordinates for a polygon drew on a map, you can easily create systems which will compute if a GPS point is inside that map polygon.

As for PostgreSQL’s native support for geometry stuff, (which I believe is great), you can read more on the manual here: http://www.postgresql.org/docs/9.3/static/datatype-geometric.html and here: http://www.postgresql.org/docs/8.2/static/functions-geometry.html.

 

Putty automatic reconnect after internet interruption

I love working on my servers via SSH by using Putty. I think it’s a great SSH client.

However there’s one thing that annoys me the most: when my internet connection gets interrupted or when after I wake up my computer from sleep, all my Putty instances get disconnected. I have to log in again to all of them, make the initial commands in each of them, etc, etc.

I’ve recently discovered another Putty-fork client, called Putty Tray which you can find here: https://puttytray.goeswhere.com/.

The best new thing added by Putty Tray to the old and best classic Putty are two new options in the Conenction tab, targeting automatic reconnecting after interruptions: “attempt to reconnect on network failure” and “attempt to reconnect on system wake-up”. You can see them on the screenshot below:

putty-tray

 

Useful Postgres SQL Example Codes

1. create basic demo table in postgres sql with an ENUM column and an auto-increment one


-- we create an enum type
CREATE TYPE demo_enum AS ENUM ('item1', 'item2', 'item3');

-- we create a sequence
CREATE SEQUENCE demotable_id_seq;

-- we create the actual table and we'll use the sequence for autoincrement column
-- and the type for an enum-type column
CREATE TABLE demo_table
("id" integer NOT NULL DEFAULT nextval('demotable_id_seq'),
start_date timestamp,
end_date timestamp,
demo_choice demo_enum,
demo_extern integer,
status smallint DEFAULT 1,
PRIMARY KEY ("id"));

-- we alter the sequence
ALTER SEQUENCE demotable_id_seq OWNED BY demo_table.id;

-- we add a foreign key constraint
ALTER TABLE demo_table ADD FOREIGN KEY (demo_extern) REFERENCES demo_other_table(demo_extern_column);

How to make NetBeans 8 for PHP look more like Notepad++

Writing on the same subject here as the last 2 articles (line-spacing and python support) on NetBeans.

Little back-story, for the past 7 years I worked and I’m still working all my web-related projects in Notepad++. I’ve tried all the IDEs out there, didn’t liked any of them for reasons more or less logical (Yes, sometimes I listen my feelings/emotions when making choices, for example “I like this editor or not” :p )

Recently I decided to give another try at NetBeans (following my colleagues persistence 😀 ) and first thing I wanted to do was to make it look more like my notepad++. Especially when it comes to color highlight and syntax.

I found the same question being asked here. One of the answers gave a full solution to the case: a download offering all the color-configs for making the NetBeans IDE show PHP codes colored like Notepad++ does.

You can download it from the following link: Notepad++ Theme for NetBeans 6.7.x

1. Unzip file
2. From menu go Tools/Options then click on Import button in the shown dialogue browse the unzipped folder
3. From the shown tree check “Fonts & Colors” and click on OK button.
4. After restarting IDE again from menu goto Tools/Options select Fonts & Colors tab and from Profile combo box select “Notepad_plusplus”
have a nice time.

Since I have no idea if the download link will work forever (probably not :p) or if the original poster will take it down, etc, etc, I am uploading the source on my blog as well and provide it for download from the link below. Again, all the credits go to the original author and to the user who posted the solution on the askubuntu.com forum: Saeed Zarinfam (I don’t know if the author of the config files is the same person with the author of the post, whomever they are, many thanks!)

Download directly: Notepad_plusplus_Theme_for_NetBeans_6.7.x_(php5-html-css)

How to increase line-height (line spacing) in NetBeans 8

Being a Notepad++ veteran user, the differences in code coloring, highlight and editor display in NetBeans IDE kinda put me off.

So first thing I wanted was to increase the line-height of the text (code, to be precise :p ) which is shown in NetBeans’ editor.

Luckily for me, I was not alone, other people asked the same question on google and some even answered it :))

The best answer by far is the one given by user pengemizt from stackoverflow.com on the question you can find here. That question was asking about NetBeans 7 but I just tried it on NetBeans 8 and it works ok.

I’m gonna copy/paste the answer here (all the credit going, like I’ve said above, to user pengemizt)

1. Find org-netbeans-modules-editor-settings-CustomPreferences.xml in ~/.netbeans/x.x/config/Editors/Preferences where x.x is your netbeans version.
In my case, this was C:\Users\Andrei\AppData\Roaming\NetBeans\8.0\config\Editors\Preferences\org-netbeans-modules-editor-settings-CustomPreferences.xml

NOTE.: By trial and error I found that this file is generated only after you first run NetBeans first time. I’ve just installed it on my home computer and decided to do all the changes first, and this file was nowhere to be found on my pc, so I had to run first netbeans and then edit the xml config file.

2. Add this xml markup just above </editor-preferences> or anywhere if you know what you doing.

    <entry javaType="java.lang.Float" name="line-height-correction" xml:space="preserve">

        <value><![CDATA[1.4]]></value>

    </entry>

 

And I may add, restart NetBeans. The solution above gave as example setting the line-height to 1.4 but in my tests I found that a value of 1.2 works better (for me).

How to set up Netbeans with Python support

Recently I had to work with some python-powered codes and one of my colleagues pointed me to a nice tutorial on how to enable support for Python on Netbeans IDE.

Since I’m a 7-years-old Notepad++ veteran user :p I was glad to get some help on my new “still in tests” IDE of use 😀

So here’s the tutorial’s link. Enjoy!

BTW, although the tutorial is for Netbeans 7.1, I can confirm it works on NetBeans 8 as well.

https://blogs.oracle.com/geertjan/entry/python_in_netbeans_ide_71

Basic PHP PDO Extension Usage & Syntax

PDO Extension defines a consistent interface for accessing databases in PHP.

What does that mean? Well, it means programmers can write PHP codes which can be easily ported from one database type to another; let’s say you write your codes for MS SQL but then suddenly the project takes a turn and you need to adapt your codes for MySQL … would be great to be able to make the transition easy, wouldn’t be?

Now, you can read all about it on the Official PHP Manual and also you can read tutorials about it, but I’m about to give you below a simple example of the basic PDO Syntax.

<?php
$db = new PDO(
	'mysql:host = localhost; dbname=someDbName; charset=utf8', 
	'username', 
	'password',
	array(
		PDO::ATTR_EMULATE_PREPARES => false,
		PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
	)
);

try{
	$res = $db->query('err');
}
catch(PDOException $ex){
	echo "error";
	callSomeLogFunction($ex->getMessage());
}

//other code examples:
$stmt = $db->query('select * from table');
$results = $stmt-?fetchAll(PDO::FETCH_ASSOC);
$count = $stmt->rowCount();

//here's how to get the ID of the last inserted row
//when you're doing an insert before
$lastId = $db->lastInsertId();

//preparing queries
$stmt = $db->prepare("select * from table where id=:id and name=:name");
$stmt->bindValue(':id',$id,PDO::PARAM_INT);
$stmt->bindValue(':name',$name,PDO::PARAM_STR);
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

Setup new Centos VPS with LAMP, Git and gitolite

Recently I had to install and maintain several Centos servers and I had to repeat the same setup instructions multiple times.

I thought it’d be nice to have the following list of to-do steps which needed to be repeated on every server install, especially so other employees could take on this job after me.

The scenario I had to work on required the setup of a new Centos 6 server with LAMP (php 5.5 at least) and configuring Git and gitolite for git user-managing access.

So the first thing was to install a Centos 6.5 minimal distribution and then all the work was done through Putty and heavy-usage of yum.

To avoid conflicts later-on because of repository packages, one of my colleagues recommended me to use yum-plugin-priorities. (thanks, Madalin 😉 )

Now, although this plugin is a bit controversial, we found that during our exploitation it was really helpful in preventing our servers to become a packages-mess in no-time. So I’m gonna go along and recommend it further. You should go however to the link and read the documentation for yum-plugin-priorities to see if it suits your needs and to learn how to proper configure it.

So here’s the list. It’s not something new or original content, it’s more of a compilation list to have all the info in one place. I have included links to the sites from where the info was used.

  1. Install yum-plugin-priorities (say yes if you’re asked about a key, it’s because you’re using the repo’s for the first time
    yum install yum-plugin-priorities
    
  1. install wget:
    yum install wget
    
  2. cd to user’s home:
    cd ~
    
  3. install remi repo:
    wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpmwget
    http://rpms.famillecollet.com/enterprise/remi-release-6.rpmsudo
    rpm -Uvh remi-release-6*.rpm epel-release-6*.rpm
    

    You can read more about the remi repo here.

  4. install webtatic repo:
    rpm -Uvh http://mirror.webtatic.com/yum/el6/latest.rpm
    

    You can read more about the webtatic repo here.

  5. Important! edit repo files located in /etc/yum.repos.d/ and enable the ones you want and add priority=N, according to yum-priorities plugin required settings (http://wiki.centos.org/PackageManagement/Yum/Priorities)
    .
  6. update your system
    yum update
    
  7. install sort-of lamp, but don’t install php, since we need php 5.5 at least which we’ll get from the webtatic repo one step later:
    Install Apache and start it:

    yum install httpd
    service httpd start
    

    Install MySQL and start it:

    yum install mysql, mysql-server
    service mysql start
    

    Secure your fresh MySQL installation by running this script:

    /usr/bin/mysql_secure_installation
    

    You can read more about the default configuration for securing your mysql on this tutorial from digitalocean.com

  8. Install PHP 5.5 and the modules you want (we installed them all)
    yum install php55w  php55w-bcmath  php55w-cli  php55w-common  php55w-dba  php55w-devel  php55w-embedded  php55w-enchant  php55w-fpm  php55w-gd  php55w-imap  php55w-interbase  php55w-intl  php55w-ldap  php55w-mbstring  php55w-mcrypt  php55w-mssql  php55w-mysqlnd  php55w-odbc  php55w-opcache  php55w-pdo  php55w-pear.noarch  php55w-pecl-apcu  php55w-pecl-apcu-devel  php55w-pecl-memcache  php55w-pecl-xdebug  php55w-pgsql  php55w-process  php55w-pspell  php55w-recode  php55w-snmp  php55w-soap  php55w-tidy  php55w-xml  php55w-xmlrpc
    
  9. don’t forget to add chkconfig setting so the services would start automatically
    chkconfig --levels 235 httpd on
    chkconfig --levels 235 mysqld on
    
  10. Optionally, install nodejs, if you need it (we did)
    yum install nodejs
    
  11. Install the dev-tools
    yum groupinstall "Development tools"
    
  12. Install Git and create user/group
    yum install git
    useradd git
    usermod -u 600 git
    groupmod -g 600 git
    passwd git
    
  13. Install gitolite on the server by loging in as git user and cloning the gitolite repo
    git clone https://github.com/sitaramc/gitolite
    
  14. Since gitolite administration is based on keys and we used windows machines to connect to the linux servers, we used putty to generate keys for every user. Copy-paste the text from puttygen as USERNAME.pub and save the private key as USERNAME.ppkThe .pub key will have to be uploaded by gitolite admin to the gitolite repos which the USERNAME will have access.

    We used this tutorial for our first install, what’s really important to keep in mind is that there are two machines in use: one is the server (linux server) and the other is the client (in the tutorial I’ve linked the client is also a linux box; in our scenario the clients were windows machines).

    It’s important to note that for the first use of gitolite, you have to create the .pub/.ppk pair of keys for the client user which will be the gitolite admin (so you’ll create this keys on the client machine, may that be linux, windows, w/e) and copy the .pub key into the git folder from the Server machine and give it proper permissions. Let’s say it will be git-admin.pub and git-admin.ppk like in the tutorial link above.
    Then, after you’ll install gitolite by logging in as user git onto the server machine, you’ll use the git-admin.pub key as argument to the initial setup of gitolite, like this:

    gitolite/install -ln
    gitolite setup -pk Git-Admin.pub
    
  15. Since we used windows machines to develop on, we installed git client on them and gitextensions also.For the git installer setup  we used the following options:
    – advanced context menu, associate .git
    – use git bash only
    – use plink
    – checkout windows style, commit linux style

    And for the gitextensions setup we used the following options:
    – install kdiff only
    – use putty
    – (dumb observation: sometimes the kdiff installer windows pops under the main gitextensions installer window and at first sight it seems the main installer hanged; it didn’t, it just awaits for your action on the windows behind)

  16. We also had an interesting setup where our git server would automatically deploy upon receive, on the httpd test server installed on the same machine. We used a script placed in hooks/post-receive folder with the following content:
    #!/bin/sh
    GIT_WORK_TREE=/home/path/to/your/www
    export GIT_WORK_TREE
    umask 002
    git checkout -f
    

    The script has to have proper permissions.
    Also, the www folder should be owned by apache user and have the group set to apache group and the git user should be added to the apache group.

    chmod +x hooks/post-receive
    chown apache:apache /home/path/to/your/www
    usermod --groups apache git
    

    Also, you have to init a new empty repo into the www folder

    cd /home/path/to/your/www
    mkdir newfolder
    cd newfolder
    git init
    
  17. After this, you can create new repos on the git server machine, from the client machine, by using the gitolite-admin repo.You must clone first the admin repo (git clone gitolite-admin.git) on your client machine and then use this repo as a setup tool for the repos you wanna create and manage.You can create new git repos, add users to those repos by putting their pub keys into the keys folder and setting up the gitolite.conf file.

    Every time you push the changes for the gitolite-admin repo, the new keys will be uploaded to server and gitolite.conf changes taken into consideration and this will basically be your tool to manage the git repos on the (test) server machine.

    A very good tutorial which we initially used when setting gitolite can be found here: http://sachinsharm.wordpress.com/2013/10/04/installsetup-and-configure-git-server-with-gitolite-and-gitweb-on-centosrhel-6-4/ 

 


 

External links for reference, used in this article:

remi repo: http://www.rackspace.com/knowledge_center/article/installing-rhel-epel-repo-on-centos-5x-or-6x
webtatic repo: http://webtatic.com/projects/yum-repository/
yum-priorities: http://wiki.centos.org/PackageManagement/Yum/Priorities
simple tutorial on lamp install: https://www.digitalocean.com/community/articles/how-to-install-linux-apache-mysql-php-lamp-stack-on-centos-6
about gitolite: http://gitolite.com/gitolite/index.html
large tutorial on git and gitolite: http://sachinsharm.wordpress.com/2013/10/04/installsetup-and-configure-git-server-with-gitolite-and-gitweb-on-centosrhel-6-4/
git clients: http://git-scm.com/downloads
gitextensions for windows: https://code.google.com/p/gitextensions/
good info on how to auto-deploy from git server: http://stackoverflow.com/questions/9132144/how-can-i-automatically-deploy-my-app-after-a-git-push-github-and-node-js
if you want to install phpmyadmin via yum: http://tecadmin.net/how-to-install-phpmyadmin-on-centos-using-yum/
in case you get httpd dead but subsys locked error: http://sandzoctanium.com/upgrade-from-php-5-3-to-php-5-5-and-get-error-httpd-dead-but-subsys-locked/

 

Page 1 of 212