PhpStorm templates

PhpStorm templates

Posted by Sweetchuck on August 14, 2012 at 7:21am

THIS IS NOT A MODULE, but Drupal-specific “Live Templates” and “File Templates” for PhpStorm.


Manager script has been moved into another repository.

Steps to start using it the first time:

  1. Exit from PhpStorm
  2. $ cd ~/where/you/want/to/store/the/config/files
  3. $ git clone
  4. $ cd WebIdeConfigManager/ConfigHome
  5. $ git clone --recursive --branch 7.x-1.x drupal
  6. $ cd ..
  7. $ ./WebIdeConfigManager.php push
  8. Start PhpStorm
  9. type “hook_mail” or whatever hook and press Tab for template.

To update:

  1. Exit from PhpStorm
  2. $ cd /PathTo/WebIdeConfigManager/ConfigHome/drupal
  3. $ git pull
  4. $ cd ../..
  5. One of:
    • $ ./WebIdeConfigManager.php push
    • OR
    • $ ./WebIdeConfigManager.php push -h drupal
  6. Start PhpStorm
  7. Enjoy.

Super fast getimagesize in php

function ranger($url){
$headers = array(
“Range: bytes=0-32768”

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
return $data;

$start = microtime(true);

$url = “”;

$raw = ranger($url);
$im = imagecreatefromstring($raw);

$width = imagesx($im);
$height = imagesy($im);

$stop = round(microtime(true) – $start, 5);

echo $width.” x “.$height.” ({$stop}s)”;


Installing and configuring mod_jk

Installation of Mod_jk is not that hard but to make it work or integrate with apache and tomcat a bit tricky. I am explaining here how to install and configure apache to serve the java pages or webapps with the help of mod_jk module.

Let me brief my scenario here, yours may be different. You can take the refference from here. I am having app1 and app2 and i want URL to serve the pages from app1 tomcat webapp and serve the pages from app2 tomcat webapp. Also you need to take care for the tomcat port also, if you want to use two tomcat instace you you have to use the two diff ports like i am using. app1 is on 8080 port and app2 is on 8081 port.

You can install apache and tomcat via yum if you are using Redhat/CentOS distro and if you are using any Debian based system you can use apt-get/aptitude utility for the same.
I am explaining here on CentOS-5.4 disto

#yum install httpd
#/etc/init.d/httpd restart
#chkconfig httpd on

Now its time to install mod_jk, i am using here the rpm package you can even compile it from source as well.
You can download it from centOS testing repo.


#rpm -ivh mod_jk-ap20-1.2.26-1jpp.i386.rpm or
#rpm -ivh  mod_jk-ap20-1.2.28-2.el5.centos.i386.rpm

Now its time to install tomcat. You can install it via yum or compile it from source. I am using the source here.

Get the tar.gz for Tomcat 5.5 — you can download it from the Apache Tomcat download site( I am using tomcat-5.5 version you can use the latest release also.

Unpack apache-tomcat-5.5.23.tar.gz under /usr/local. Rename apache-tomcat-5.5.23 to tomcat8080. Unpack the tar.gz one more time, rename it to tomcat8081.

cd /usr/local/tomcat8081/conf
- edit server.xml and change following ports:
8005 (shutdown port) -> 8006
8080 (non-SSL HTTP/1.1 connector) -> 8081
8009 (AJP 1.3 connector) -> 8010

There are other ports in server.xml, but I found that just changing the 3 ports above does the trick.

I won’t go into the details of getting the 2 Tomcat instances to run. You need to create a tomcat user, make sure you have a Java JDK or JRE installed, etc., etc.
One more thing i want to mention here, you have to set JAVA_HOME variable set to make the java application to find the exact JRE/JAVA location. If you want to set them system wide then mention that variable in /etc/profile file instead if ‘export’ on shell.

The startup/shutdown scripts for Tomcat are /usr/local/tomcat808X/bin/|

I will assume that at this point you are able to start up the 2 Tomcat instances. The first one will listen on port 8080 and will have an AJP 1.3 connector (used by mod_jk) listening on port 8009. The second one will listen on port 8081 and will have the AJP 1.3 connector listening on port 8010.

I am assuming that you are well aware, how to deploy the tomcat apps.So i am skipping that section. Please write me at if you want to have a chapter on this as well.

Create Apache virtual hosts for and and tie them to the 2 Tomcat instances via mod_jk.

Here is the general mod_jk section in httpd.conf — note that it needs to be OUTSIDE of the virtual host sections:

# Mod_jk settings
# Load mod_jk module
LoadModule    jk_module  modules/
# Where to find
JkWorkersFile conf/
# Where to put jk logs
JkLogFile     logs/mod_jk.log
# Set the jk log level [debug/error/info]
JkLogLevel    emerg
# Select the log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
# JkOptions indicate to send SSL KEY SIZE,
JkOptions     +ForwardKeySize +ForwardURICompat -ForwardDirectories
# JkRequestLogFormat set the request format
JkRequestLogFormat     "%w %V %T"

Note that the section above has an entry called JkWorkersFile, referring to a file called, which I put in /etc/httpd/conf. This file contains information about so-called workers, which correspond to the Tomcat instances we’re running on that server. Here are the contents of my file:

# This file provides minimal jk configuration properties needed to
# connect to Tomcat.
# The workers that jk should create and work with

worker.list=app1, app2



The file declares 2 workers that I named app1 and app2. The first worker corresponds to the AJP 1.3 connector running on port 8009 (which is part of the Tomcat instance running on port 8080), and the second worker corresponds to the AJP 1.3 connector running on port 8010 (which is part of the Tomcat instance running on port 8081).

The way Apache ties into Tomcat is that each of the VirtualHost sections configured for and declares a specific worker. Here is the VirtualHost section I have in httpd.conf for

DocumentRoot "/usr/local/tomcat8080/webapps/ROOT"

  # Options Indexes FollowSymLinks MultiViews
  Options None
  AllowOverride None
  Order allow,deny
  allow from all

ErrorLog logs/app1-error.log
CustomLog logs/app1-access.log combined
# Send ROOT app. to worker named app1
JkMount  /* app1
RewriteEngine On
RewriteRule ^/(images/.+);jsessionid=\w+$ /$1

The 2 important lines as far as the Apache/mod_jk/Tomcat configuration is concerned are:

JkMount /* app1

The line “JkMount /* app1″ tells Apache to send everything to the worker app1, which then ties into the Tomcat instance on port 8080.

The line “JkUnMount /images/* app1″ tells Apache to handle everything under /images itself — which was one of our goals.

At this point, you need to restart Apache, for example via ‘sudo service httpd restart’. If everything went well, you should be able to go to and and see your 2 Web applications running merrily.

You may have noticed a RewriteRule in each of the 2 VirtualHost sections in httpd.conf. What happens with many Java-based Web application is that when a user first visits a page, the application does not know yet if the user has cookies enabled or not, so the application will use a session ID mechanism fondly known as jsessionid. If the user does have cookies enabled, the application will not use jsessionid the second time a page is loaded. If cookies are not enabled, the application (Tomcat in our example) will continue generating URLs such as;jsessionid=0E45D13A0815A172BD1DC1D985793D02

In our example, we told Apache to process all URLs that start with ‘images’. But those URLs have already been polluted by Tomcat with jsessionid the very first time they were hit. As a result, Apache was trying to process them, and was failing miserably, so images didn’t get displayed the first time a user hit a page. If the user refreshed the page, images would get displayed properly (if the user had cookies enabled).

The solution I found for this issue was to use a RewriteRule that would get rid of the jsessionid in every URL that starts with ‘images’. This seemed to do the trick.

That’s about it. I hope this helps somebody

How to install LAMP on CentOS 6 server

You can check yum by running


yum update

Now if everything went well with yum, lets setup the hostname of the server – in case you didn’t already.
First check the current short hostname with


command, then you can try the same command with


switch to show the FQDN (fully qualified domain name) version of the hostname.
In case you have to setup the hostname you can perform the following steps:
Edit /etc/sysconfig/network and add this


Or run the following command:

echo "HOSTNAME=yourhostname.tld"

Now run

 hostname "yourhostname.tld"

and you are done setting your hostname.
Re check with



hostname -f

Now lets install the Apache web server. This is the most popular choice today among web browsers and usually it is the best solution combining easy configuration, wide compatibility, stability, maturity of the platform and performance.
Do not forget to run

yum update

unless you listened and already did that at the first stages fot this tutorial.
After the update run:

yum install httpd

Now make backup for the original Apache configuration file, usually this backup is not needed but in case you will need to restore the default configuration you will be glad you have it.
Backup the configuration by running:

mkdir /yournewconfbackupdir
cp /etc/httpd/conf/httpd.conf /yournewconfbackupdir/httpd.conf.original

It is a very recommended practice to backup configuration files before you edit and save the changes. Takes only one simple command to do it just as it will take another simple command to restore the configuration backup in case of multiple mistakes in configuration file on a production server leading to long downtime until you will find the mistakes in the configuration. It is much simpler to restore the file from backup and shortening the downtime to mere seconds instead of minutes or tens of minutes.

Now we will use this directory /etc/httpd/conf.d/ – Apache threats any files with .conf extensions in this path as its configuration files and it will load any such file on start or reload.
I’m going to to show you how to create Name based Virtual Hosts in /etc/httpd/conf.d . There are different approaches to this but personally I’ve always preferred to create Virtual Hosts as different configuration files in the conf.d directory, you can of course make one configuration file and include all your virtual hosts in this file. In this example I will show how to divide the configuration to separate files.
So for example you would like to create a virtual host for domain, what you are going to do is to create a new file in /etc/httpd/conf.d/:

 touch /etc/httpd/conf.d/

Now you are going to edit this file and add the configuration itself:

     DocumentRoot /http/www/
     ErrorLog /http/www/
     CustomLog /http/www/ combined

Now you have to create the directories we specified in the configuration:

mkdir -p /http/www/
mkdir /httpd/www/

Before you can start apache and test the configuration you have to edit


the same file you backed up at the beginning of this tutorial. Find the part with name-based Vritual Host and uncomment NameVirtualHost directive:

# Use name-based virtual hosting.
NameVirtualHost *:80
# NOTE: NameVirtualHost cannot be used without a port specifier
# (e.g. :80) if mod_ssl is being used, due to the nature of the
# SSL protocol.

Now you can try to start Apache for the first time:

service httpd start


/etc/init.d/httpd start

If the service is already running just use restart instead of start.
After you tested everything and you are sure about the configuration you can add the Apache (httpd) service to the system start up so that next time you will reboot the server you won’t have to worry about Apache service.
Run this command to make sure that the Apache service will auto start next time on server reboot:

chkconfig --levels 235 httpd on


/sbin/chkconfig --levels 235 httpd on

Do not forget to reload Apache service on each configuration edit, both httpd.conf or vhost configuration files:

service httpd reload


/etc/init.d/httpd reload

Now that you are finished with Apache, its time to continue with the process and install MySQL. MySQL is a database server and today most web applications, cms or blog system use mysql databases. So probably you will need MySQL anyway.

Install mysql-server packages from the repository:

yum install mysql-server

Now add the MySQL server to the startup so you won’t have to worry about it either when you reboot the server:

chkconfig --levels 235 mysqld on


/sbin/chkconfig --levels 235 mysqld on

and its time to test the installation and start the mysql server:

service mysqld start


/etc/init.d/mysqld start

After insuring mysql is running and everything is ok you should run the following command to make sure you secure your mysql installation, remove default databases and users and disable root logins except from localhost:


You should answer yes to all of the questions that will follow and do not forget to set the mysql root password.
Now login to mysql server:

mysql -u root -p

Enter your mysql password and you will get mysql prompt where you can run sql statements.
For example you can create a database and give permissions to user by running the following commands:

create database mydatabase;
grant all on mydatabase.* to 'myuser' identified by 'mypassword';

You can quit the mysql prompt by running the following command:


In case you have to change mysql configuration, your configuration file is my.cnf and you can find it in /etc.
You are almost done and what’s left is to install PHP on the server, you probably already know what PHP is and why should you need PHP on your server, in case you don’t – PHP is a general-purpose scripting language for web development, its main purpose is to create dynamic pages, websites and web applications.
Install PHP by running the following command:

yum install php php-pear php-mysql

Now you can change defaults in /etc/php.ini if you already know what you are doing and there are specific settings you need to change. If you are not sure about this one, do not worry – you can always do it later if you encounter problems with web applications that will demand different PHP settings.
You can reload everything again or just reboot the server and make sure everything works and you are done. Congratulations! You have a working LAMP server setup.

Find URLs in Text, Make Links


// The Regular Expression filter
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";

// The Text you want to filter for urls
$text = "The text you want to filter goes here.";

// Check if there is a url in the text
if(preg_match($reg_exUrl, $text, $url)) {

       // make the urls hyper links
       echo preg_replace($reg_exUrl, "<a href="{$url[0]}">{$url[0]}</a> ", $text);

} else {

       // if no urls in the text just return the text
       echo $text;