Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Wednesday, August 15, 2012

Converting .docx to pdf (or .doc to pdf, or .doc to odt, etc.) with libreoffice on a webserver on the fly using php

SkyHi @ Wednesday, August 15, 2012

Ok, so I needed to convert .docx files to .pdf files on the fly, but none of the free php libraries that were available let me do it on my server (a webservice was not good enough).
Basically either I needed to pay for a library (and have it maybe suck) or just deal with the free ones that didn't convert the formatting well enough.
Not good enough!
I found that LibreOffice (OpenOffice's successor) allows command line conversion using the LibreOffice conversion engine (which DID preserve the formatting like I wanted and generally worked great).
I loaded the latest version of Ubuntu (http://www.ubuntu.com/download/ubuntu/download) onto my Virtual Box (https://www.virtualbox.org/wiki/Downloads) on my computer and found that I was able to easily convert files using the commandline like this:
libreoffice --headless -convert-to pdf fileToConvert.docx -outdir output/path/for/pdf
I thought: sweet...but I don't have admin rights on my host's web server. I tried to use a "portable" version of LibreOffice that I obtained from http://portablelinuxapps.org/ but I was unable to get it to work on my host's webserver, because my host's webserver didn't have all the dependencies (Dependency Hell! http://en.wikipedia.org/wiki/Dependency_hell)
I was at a loss of how to make it work, until I ran across a cool project made by a Ph.D. student (Philip J. Guo) at Stanford called CDE: http://www.stanford.edu/~pgbovine/cde.html
I will let you look at his explanations of how it works (I followed what he did here:

starting at about 32:00 as well as the directions on his site), but in short, it allows one to avoid dependency hell by copying all the files used when you run certain commands, recreating the linux environment where the command worked. I was able to use this to run LibreOffice without having to resort to someone's portable version of it, and it worked just like it did when I did it on Ubuntu with the command above, with a tweak: I needed to run the wrapper of LibreOffice the CDE generated.
So, below is my PHP code that calls it. In this code snippet, the filename to be copied is passed in as $_POST["filename"]. I copy the file to the same spot where I originally converted the file, convert it, copy it back and then delete all the files (so that it doesn't start growing exponentially).
I did it this way because I wasn't able to make it work otherwise on the webserver. If there is a linux + webserver ninja out there that can figure out how to make it work without doing this, I would be interested to know what you did. Please post a comment or something if you did that.
 
//first copy the file to the magic place where we can convert it to a pdf on the fly
copy($_POST["filename"], "../LibreOffice/cde-package/cde-root/home/robert/Desktop/".$_POST["filename"]);
//change to that directory
chdir('../LibreOffice/cde-package/cde-root/home/robert');
//the magic command that does the conversion
$myCommand = "./libreoffice.cde --headless -convert-to pdf Desktop/".$_POST["filename"]." -outdir Desktop/";
exec ($myCommand);
//copy the file back
copy("Desktop/".str_replace(".docx", ".pdf", $_POST["filename"]), "../../../../../documents/".str_replace(".docx", ".pdf", $_POST["filename"]));
//delete all the files out of the magic place where we can convert it to a pdf on the fly
$files1 = scandir('Desktop');
//my files that I generated all happened to start with a number.
$pattern = '/^[0-9]/';
foreach ($files1 as $value)
{
preg_match($pattern, $value, $matches);
if(count($matches) ?> 0)
{
unlink("Desktop/".$value);
}
}
//changing the header to the location of the file makes it work well on androids
header( 'Location: '.str_replace(".docx", ".pdf", $_POST["filename"]) );
?>

And here is the tar.gz file I generated I generated with CDE. See below for a working example and complete, documented code.
Success! I made a truly portable version of LibreOffice that can convert files on the fly on a webserver using 100% free, open source software!
Note: since when I used CDE I only converted a .docx to a .pdf, my tar.gz file above will probably only work to do that. To get it to do other things, you will have to do them with CDE first.
*****************************************************************************
UPDATE: since several people have had questions on how to get it working or had issues making it work, I am putting a complete working example out there for you to play with and modify.
Click here for working example.

And here is the tar.gz of the working example, tied up in a nice bow for you. To make sure the permissions don't get screwed up, I recommend uploading the tar.gz file to your server and then unpacking it there.

This is my way of giving back to all the great people out there that have helped me out by doing these kinds of things for me. Pay it forward, guys! [licensed under the MIT license.]

Wednesday, June 20, 2012

PHP with ioncube loader CentOS

SkyHi @ Wednesday, June 20, 2012

Guide to upgrade PHP Linux. PHP 5.3.3 Red Hat, CentOS.
1. Remove currently installed PHP.
Without Plesk.
rpm -qa | grep php | xargs rpm -e --nodeps
With Plesk. This is required as the output of rpm -qa | grep php would also pass some Plesk packages to xargs rpm -e --nodeps and Plesk would be broken!
Using the grep -v psa will remove any output containing psa.
rpm -qa | grep php | grep -v psa | xargs rpm -e --nodeps
Personally I advise you check the output of.
rpm -qa | grep php
and,
rpm -qa | grep php | grep -v psa  
before piping to xargs.
2. Install PHP 5.3.3.
yum install php53* php53-*
3. Install the ioncube loader.
Download and extract the ioncube loader and install the ioncube loader module.
x86,
wget http://downloads2.ioncube.com/loader_downloads/ioncube_loaders_lin_x86.tar.gz && cd ioncube && cp ioncube_loader_lin_5.3.so /usr/lib/php/modules/
Now update the PHP config.
echo “zend_extension=/usr/lib/php/modules//ioncube_loader_lin_5.3.so” > /etc/php.d/ioncube-loader.ini
x86_64.
wget http://downloads2.ioncube.com/loader_downloads/ioncube_loaders_lin_x86-64.tar.gz && tar -zxf ioncube_loaders_lin_x86-64.tar.gz && cd ioncube && cp ioncube_loader_lin_5.3.so /usr/lib64/php/modules/
Now update the PHP config.
echo “zend_extension=/usr/lib64/php/modules//ioncube_loader_lin_5.3.so” > /etc/php.d/ioncube-loader.ini
4. Create dummy PHP package (Optional only required for Plesk  as it does not look for PHP53 packages only PHP.
Install rpmbuild.
yum install rpm-build
Create the php.spec.
nano -w php.spec
Include the following in the file.
Summary: Empty PHP required as Plesk doesent recognise the PHP53 packages.
Name: php
Version: 5
Release: 3.3
License: Public
Group: Applications/System
%description
Empty PHP RPM
%files
press ctrl+x to exit and save the file.
Now build the RPM.
rpmbuild -bb php.spec
This will then output a path to the created RPM.
Install the package
rpm -i /path/to/rpm
Exclude PHP from the main repos.
open the yum config.
nano -w /etc/yum.conf
add the following entry.
exclude=php
The exclude php is to prevent issues with php packages conflicting with php53. It wont stop php53 packages from updating.
5. Restart Apache
service httpd restart
Now check the version of PHP
php -v
Job done.



REFERENCES
http://box-admin.com/upgrade-php-linux/

Wednesday, February 22, 2012

Why does my input type=text value get truncated?

SkyHi @ Wednesday, February 22, 2012
When you insert values into textboxes dynamically, you have to remember the same rules that hold true for basic HTML. When you use a string, you must store it within quotes to prevent premature concatenation. :-) 

    <... value=<%=value%>> 

Becomes: 

    <... value="<%=value%>"> 

One thing you want to be careful of is embedded quotes. You might try using ' or " as the delimiter, and eliminating the other for possible entry (using client-side validation of course; the value is destroyed before you'd be able to validate for it on the server side). If you have to allow both ' and ", you could consider using the rarely used "back-apostrophe" (`). You can also try to user Server.HTMLEncode() on the value, before slipping it into the HTML element. 

If you do this: 

<... value='<%="foo's bar"%>'>

This evaluates to: 

<... value='foo's bar'>

And everything after 'foo' is ignored, because the browser interprets that as the end of the string. 


Solution:



; Logging Options


; Defines what classes of security alerts are logged to the syslog daemon.
; Logging of errors of the class S_MEMORY are always logged to syslog, no
; matter what this configuration says, because a corrupted heap could mean that
; the other logging options will malfunction during the logging process.
;suhosin.log.syslog =
; log in /var/log/messages
suhosin.log.syslog = 511    





; Defines the maximum number of variables that may be registered through a POST
; request.
;suhosin.post.max_vars = 200
suhosin.post.max_vars = 1000


; Defines the maximum number of variables that may be registered through the
; COOKIE, the URL or through a POST request. This setting is also an upper
; limit for the variable origin specific configuration directives.
;suhosin.request.max_vars = 200
suhosin.request.max_vars = 1000





REFERENCES
http://classicasp.aspfaq.com/forms/why-does-my-input-type-text-value-get-truncated.html
http://www.frihost.com/forums/vt-104933.html

Thursday, February 2, 2012

PHP Hide your Files location

SkyHi @ Thursday, February 02, 2012
This Code , will Help you Hide your files location, Images, MP3, Video .. and Create a Mask Link to the files .

Example : Your link is :
http://www.somesitehere.com/image.jpg

with this code the link will look like :

http://www.somesitehere.com/getfile.php?file=image.jpg

u can even make catgoreys , and the link will look like

Download

Make sure to edit the code Below to Put down the Name of the Directory will Include ur files that u need to hide thier location , Better way to move your files outside the root of ur site and link to them useing this code .

I have no idea who wrote this Code , Just found it in Google , . I just Modifided it with help of zelfase to fix some bug .

Save the file as getfile.php and edit line 3 if needed.


<?php
// Usage: Download 
// Path to downloadable files (will not be revealed to users so they will never know your file's real address) 
$hiddenPath = "secretfiles/"; 

// VARIABLES 
if (!empty($_GET['file'])){ 
$file = str_replace('%20', ' ', $_GET['file']); 
$category = (!empty($_GET['category'])) ? $_GET['category'] . '/' : ''; 
} 
$file_real = $hiddenPath . $category . $file; 
$ip = $_SERVER['REMOTE_ADDR']; 

// Check to see if the download script was called 
if (basename($_SERVER['PHP_SELF']) == 'download.php'){ 
if ($_SERVER['QUERY_STRING'] != null){ 
// HACK ATTEMPT CHECK 
// Make sure the request isn't escaping to another directory 
//if (substr($file, 0, 1) == '.' ¦¦ strpos($file, '..') > 0 ¦¦ substr($file, 0, 1) == '/' ¦¦ strpos($file, '/') > 0)  { 
if ((substr($file, 0, 1) == '.') || (strpos($file, '..') > 0) || (substr($file, 0, 1) == '/') || (strpos($file, '/') > 0)) 
{ 

// Display hack attempt error 
echo("Hack attempt detected!"); 
die(); 
} 
// If requested file exists 
if (file_exists($file_real)){ 
// Get extension of requested file 
$extension = strtolower(substr(strrchr($file, "."), 1)); 
// Determine correct MIME type 
switch($extension){ 
case "png": $type = "video/x-ms-asf"; break; 
case "avi": $type = "video/x-msvideo"; break; 
case "jpg": $type = "application/octet-stream"; break; 
case "jpeg": $type = "video/quicktime"; break; 
case "mp3": $type = "audio/mpeg"; break; 
case "mpg": $type = "video/mpeg"; break; 
case "gif": $type = "video/mpeg"; break; 
case "rar": $type = "encoding/x-compress"; break; 
case "txt": $type = "text/plain"; break; 
case "wav": $type = "audio/wav"; break; 
case "pdf": $type = "text/plain"; break; 
case "doc": $type = "audio/wav"; break; 
case "jpeg": $type = "text/plain"; break; 
case "bmp": $type = "audio/wav"; break; 
case "wma": $type = "audio/x-ms-wma"; break; 
case "wmv": $type = "video/x-ms-wmv"; break; 
case "zip": $type = "application/x-zip-compressed"; break; 
default: $type = "application/force-download"; break; 
} 
// Fix IE bug [0] 
$header_file = (strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE')) ? preg_replace('/\./', '%2e', $file, substr_count($file, '.') - 1) : $file; 
// Prepare headers 
header("Pragma: public"); 
header("Expires: 0"); 
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
header("Cache-Control: public", false); 
header("Content-Description: File Transfer"); 
header("Content-Type: " . $type); 
header("Accept-Ranges: bytes"); 
header("Content-Disposition: attachment; filename=\"" . $header_file . "\";"); 
header("Content-Transfer-Encoding: binary"); 
header("Content-Length: " . filesize($file_real)); 
// Send file for download 
if ($stream = fopen($file_real, 'rb')){ 
while(!feof($stream) && connection_status() == 0){ 
//reset time limit for big files 
set_time_limit(0); 
print(fread($stream,1024*8)); 
flush(); 
} 
fclose($stream); 
} 
}else{ 
// Requested file does not exist (File not found) 
echo("Requested file does not exist"); 
die(); 
} 
} 
} 
?>



PHP: Hide the Real File URL and Provide Download via a PHP Script

There are times when you need to store a file (such as one that you sell for profit) outside of the document root of your domain and let the buyers download it via a PHP script so as to hide the real path, web address or URL to that file. Use of this approach enables you to:

Check for permissions first before rendering the file download thus protecting it from being downloaded by unprivileged visitors.
Store the file outside of the web document directory of that domain – a good practice in web security in protecting sensitive and important data.
Count the number of downloads and collect other useful download statistics.

Now the actual tip. Given that you have put the file to be downloaded via the PHP script in place at /home/someuser/products/data.tar.gz, write a PHP file with the following content in it and put it in the web document directory where your site visitors can access:

<?php
$path = '/home/someuser/products/data.tar.gz'; // the file made available for download via this PHP file
$mm_type="application/octet-stream"; // modify accordingly to the file type of $path, but in most cases no need to do so

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: " . $mm_type);
header("Content-Length: " .(string)(filesize($path)) );
header('Content-Disposition: attachment; filename="'.basename($path).'"');
header("Content-Transfer-Encoding: binary\n");

readfile($path); // outputs the content of the file

exit();


REFERENCES
http://www.codingforums.com/showthread.php?t=185272
http://www.kavoir.com/2009/05/php-hide-the-real-file-url-and-provide-download-via-a-php-script.html
http://stackoverflow.com/questions/6647165/hide-download-file-location-redirect-download

Wednesday, January 18, 2012

10 Open Source Shopping Carts to Run Your Ecommerce Business

SkyHi @ Wednesday, January 18, 2012
More and more companies have turned to the Web to transact business. And, of course, if you are going to sell on the Web, the right shopping cart can mean the difference between red and black ink. When shopping for your own ecommerce shopping cart software the most important aspect to consider is how well the cart software meets your business objectives. An ecommerce shopping cart has to be customizable to fit your business needs and branding, be flexible enough to scale as your business grows, be secure and support industry standards and provide solid integrate with payment gateways.
Open source shopping cart software is an attractive option. Storeowners might look to open source ecommerce software because it will typically deliver the features and tools to manage a product catalog on a website without the hefty licensing fees that come with proprietary or off-the-shelf packages.
Open source shopping cart software also provides access to communities of users including developers, storeowners and enthusiasts who freely offer community-based support and add-ons to enhance the open source software package.
Storeowners who decide to use open source shopping cart software can always pay for additional support and service through qualified third parties if they find the community-based support is not enough.


Top 10 Free and Open Source Ecommerce Solutions

Here are ten options if you plan to invest in open source shopping cart software for your ecommerce business.
1. Agora Shopping Cart: Lots of Features and Back-End Management Options
AgoraCart is a customizable and secure open source ecommerce shopping cart that you can install on an existing website. With AgoraCart, you can expect the typical features such as customizable templates for setting-up your store, support for different product categories, options for different tax rates in addition to back-end store management tools. On the upscale side of ecommerce, AgoraCart is PA-DSS Complaint (PCI-DSS) and supports more than 10 payment gateways.
The free community edition (5.2.x) is supported only though online community forums. AgoraCart version 6.x Gold is available for $49.95 and offers storeowners additional license, features and support options.

2. Broadleaf Commerce: An Open Source Enterprise Ecommerce Platform
The Broadleaf Commerce solution is an open source alternative for enterprise ecommerce companies. It offers an enterprise-level platform that (built on Java integration technologies) and can be customized to specific business needs.
With Broadleaf Commerce, retailers can manage customer accounts, upsell, create promotions and manage email marketing. The platform supports social integration, catalog browsing, search engine optimization (SEO) and integrates with Google Analytics and any existing business database and fulfillment system. The newest release (Broadleaf Commerce Version 1.5) offers enhanced administration and promotion capability over previous versions.
The Broadleaf Commerce community provides an online forum for discussion and contributions, articles, development guides, and project API documentation. Broadleaf Commerce uses the Apache license.

3. Commerce.CGI: A Free Perl Shopping Cart
Commerce.CGI's claim to fame is being the first free Perl shopping cart on the web. First released in 1998, it is a fully featured shopping cart for Unix-based servers, although it can run on Windows NT with minor code adjustment. Commerce.CGI can be an add-on to an existing web site or installed and configured to manage a new product website.
Commerce.CGI offers the standard shopping cart features you would expect -- it's template-driven and provides tools to configure email management, product search and payment methods. It supports sales tax, multiple shipping options, discount calculations and other options for customer check out.
Commerce.CGI is free and supported through the Commerce.CGI mailing list or BBS. Paid member features ($49.99) include wish lists, product reviews, coupon support and other customizable shopping cart enhancements. The Commerce.CGI site offers user-contributed modifications that are freely distributed. The current version, V.4.6.1, is available in zip or tar formats.

4. Loaded Commerce: A Highly Customizable Cart
Loaded Commerce is the 6.5 release of the software developed by the CRE Loaded team. Loaded Commerce, based on the popular CRE Loaded program, includes security modifications. The Loaded Commerce Community Edition (CE) is a shopping cart designed for the small office, home office (SOHO) storeowner who wants to add transaction capabilities to an existing website.
This ecommerce solution offers a number of features for product, customer, order and content management. It is highly customizable so you can change your site design choosing from hundreds of templates, edit customer information, orders, invoices and more.
The CE is the free edition of the ecommerce shopping cart software and supported by the Loaded Commerce community. A customer account is required to download Loaded Commerce.

5. Magento: Hosted or Deployed Solutions for Small to Enterprise Businesses
Magento offers an enterprise-class ecommerce platform, supported by a global ecosystem of solution partners and third-party developers. Acquired by eBay in 2011, Magento is part of eBay's X.commerce business unit.
Magento ecommerce gives merchants scalability and features for presentation, content and functionality. The platform offers marketing tools, search engine optimization, product catalog management and browsing, one-page checkout and a number of standard tools such as those used to manage shipping, tax and customer service.
The latest stable release of Magento Community Edition (version 1.6.1.0) was released on October 19, 2011. This free version is available under the open source OSL 3.0 license. Merchants looking for a more mission-critical ecommerce platform can upgrade the Enterprise Edition.

6. OpenCart: Manage Multiple Stores with One Admin Interface
The OpenCart shopping cart helps storeowners to quickly and easily install, select a template, add products and start taking online orders. The built-in template system lets you switch between different templates or migrate your site's current design into OpenCart.
Other cart features include a multi-store capability to manage multiple stores from one admin interface, tax zones, shipping methods, back-end store administration, and support for a number of payment gateways and languages.
OpenCart is free open source software published under the GNU GPL License and both free community and commercial support is offered. OpenCart server requirements include Web Server (preferably Apache), PHP (at least 5.2), MySQL, Curl and Fsock.

7. osCommerce Online Merchant: Provides Front and Back-End Tools For Store Owners
The osCommerce Online Merchant ecommerce solution is a free offering that comes with features and tools to help storeowners manage the front-end catalog and back-end administration.
Released under the GNU General Public License, osCommerce Online Merchant v2.3.1 provides a basic template layout structure to customize the catalog front-end. The Administration Tool lets merchants configure the online store, insert products for sale, manage customers and process orders.
There is a large community of more than 256,000 storeowners, developers, service providers and enthusiasts contributing to the help, support and development of osCommerce. Other support options include mailing lists and the osCommerce Newsletter for storeowners. Server requirements include PHP v4+ (PHP v5+ recommended) and MySQL v3+ (MySQL v5+ recommended).

8. PrestaShop Features Multiple Languages and Localization Options
PrestaShop is a customizable, PCI-DSS compliant, ecommerce solution that will handle everything from Web store set-up to managing customers and orders. Storeowners can create and manage the front-end catalog and marketing, customize orders and change shipping options and localization to suit their business. PrestaShop is available in three languages (English, French and Spanish) with an additional 41 translations available.
PrestaShop v.1.4.6.2 (stable) is the current version published under the Open Software License (OSL) v3.0. Server requirements include Linux, UNIX, or Windows, Web Server (Apache 1.3 or later, IIS 6 or later), PHP 5.0 or later and MySQL 5 or later.

9. Zen Cart Requires Only Basic Skills to Install and Configure
Zen Cart is a free and open source shopping cart designed by a group of shop owners, programmers, designers and consultants.
Zen Cart offers a number of options to customize the cart using a template system to select a design and configure product categories, sales discounts, and shipping and payment options. The cart incorporates a WYSIWYG page editor for modifying non-database pages, and nearly every piece of information about your products is customized and managed within the Zen Cart Admin area.
Zen Cart provides community contributed additions for your shop and documentation and the community forum for support is available on the Zen Cart website.

10. Zeuscart Offers Web 2.0 Features
ZeusCart is a web-based PHP/My SQL shopping cart that boasts a rich user interface and a highly usable shopping cart that meets the demands of Web 2.0.
The cart is primarily for small and medium storeowners and offers inventory management, attribute-driven product catalog, category management, a built-in CMS and SEO-friendly URLs. Standard features such as discounts, taxation, shipping options, integration with multiple payment gateways and email templates are also included.
ZeusCart 3.0, licensed under GPL 2, can be installed on any server where a PHP interpreter, MySQL database server and a web server is present.




REFERENCES
http://www.cio.com/article/print/698227

Thursday, December 22, 2011

Drupal 7: HipHop for PHP vs APC – benchmark

SkyHi @ Thursday, December 22, 2011
Drupal is one of two most popular content management systems (CMS) written in PHP . It is used as a back-end system for at least 1.5% of all websites worldwide. It is also one with the the slowest systems of this kind on the Internet
There have been many suggestions on improving Drupal performance, some of them recommend the use of APC module, data caching, or even compilation of the entire system through HipHop for PHP. While the first two solutions have been successfully implemented, no one was able to perform the build process.
After many battles with the compiler and the Drupal code, I present you results of the first successful translation of Drupal 7 to C++ language.

Introduction

All tests were conducted on a modified version of Drupal. These changes were necessary in order to ensure compatibility with HipHop translator.
You can download modified source codes from this link.
The system was installed in the minimal version and then launched in three different ways:
  • as a standard PHP script
  • as a PHP script with APC opcode caching enabled
  • in the form of a compiled program
Due to hardware limitations the MySQL server is located on the same machine as the web server, more advanced tests will be performed in the near future using the multiple servers.

Testing platform

Processor: Intel(R) Core(TM)2 Duo CPU E7600 @ 3.06GHz
Memory: 2.5GB RAM
System: Fedora 12 (64bit)
Kernel: 2.6.32.26-175.fc12.x86_64 #1 SMP
The server was used exclusively for testing purposes – it was running only the services associated with the benchmark.

Test Configuration

Apache version: 2.2.15
MySQL Server Version: 5.1.47
PHP Version: 5.3.3
HipHop for PHP Version: 806ee06
Drupal Version: 7.0
Drupal was compiled with this command:
cd ~drupal
date && ~/hiphop/hiphop-php/src/hphp/hphp  --keep-tempdir=1\
 --log=3\
 --input-list=files.full.list\
 --include-path="." --force=1\
 --cluster-count=240\
 -v "AllDynamic=true"\
 -v "AllVolatile=true"\
 -o /tmp/drupal\
 --parse-on-demand=0\
 --sync-dir=/tmp/sync
And launched with this command:
cd ~drupal
/tmp/drupal/program -m server -p 80\
 -v "Server.SourceRoot=`pwd`"\
 -v "Server.DefaultDocument=index.php"\
 -c $HPHP_HOME/bin/mime.hdf\
 -v "Log.File=/tmp/errors"\
 -v "ErrorHandling.AssertActive=true"\
 -v "ErrorHandling.AssertWarning=true"\
 -v "ErrorHandling.WarningFrequency=10000"\
 -v "ErrorHandling.NoticeFrequency=10000" &

CPU usage

The following test examines the performance of Drupal by simulating the concurrent activity of many visitors on the Drupal home page.
I found that in case of a dual core server four was the optimal number of concurrent users, so I used the ab program as as a benchmark tool and launched it with a following command:
ab -n 300 -c 4 http://achilles.webtutor/
The first result shows the CPU usage of a regular PHP script during the execution of the 300 HTTP requests started by 4 concurrent users:

sy = system CPU usage (gradient color), us = user CPU usage (solid color)
As you can see Drupal is indeed a very demanding system. The test in this case took almost 20 seconds, which gave a dissapointing result of 15 requests per second.
Let’s see what we get after enabling the APC module. Results are as follows:

Drupal performance improved dramatically. The test was completed in 6 seconds, 3 times faster than with traditional PHP! Due to the size of Drupal code, however, this result is not shocking. While the APC module will not speed up the script itself, its opcode cache eliminates the delay caused by having to parse PHP code on every HTTP request.
Interestingly, Drupal is not able to use the full computing power of the test server. The official cause is unknown, however it may caused by some kind of internal locking in the APC module.
Since we know how much the opcode cache improves performance by omitting the PHP parser, its time to test how much we can accelerate the PHP code itself. We check this by translating Drupal source code to C++ and compiling the application:
Compiled Drupal application is five times faster than a regular script, and almost two times faster than a script launched from the opcode cache!
Let’s compare the results. The first is the detailed comparison of CPU usage:
And the overall CPU usage:
Here are the results taken directly from the ab tool:
Environment      Execution 
   Type        time [300 req]
-----------------------------
Regular PHP      19.873 sec
PHP + APC         6.396 sec
HipHop for PHP    3.896 sec

Concurrency benchmark

In this scenario I measured a number of requests performed per second. The summary results are as follows:
Type of environmentRequests per second [#/sec]Time per request [ms]Req/sec ratio [%]
Regular PHP15.1066.242100%
PHP + APC (opcode cache)46.9021.321310%
HipHop for PHP77.0112.985510%

Other concurrency levels

As a curiosity I decided to investigate how the Drupal’s performance can be affected by a variable number of concurrent users. In order to do so I tested the system with seven different workloads simulating 1, 2, 4, 8, 16, 32 and 64 concurrent users.
Here are the results:
Tabular version:
Users    PHP     APC   HipHop 
-----------------------------
  1      8.68   28.11   40.23
  2     13.34   38.25   56.12
  4     15.28   46.82   74.12
  8     14.76   49.96   72.12
  16    14.04   49.09   74.12
  32    12.35   45.00   77.67
  64     5.22   39.17   73.02
Please note: this test is heavily CPU bound and should be executed on a multicore servers instead.
As you can see, in case of APC and HipHop for PHP translator Drupal scales quite well up to the 8 simultaneous users on a dual CPU system. Unfortunately the same cannot be said about regular PHP interpreter, which is much slower in every tested scenario.

Different optimization levels in GCC compiler

Without any optimization option, the compiler’s goal is to reduce the cost of compilation and to make debugging produce the expected results.
Turning on optimization flags makes the compiler attempt to improve the performance and/or code size at the expense of compilation time and possibly the ability to debug the program.
Let’s see the results of different optimization levels switched on during the Drupal compilation:
optimization    req/sec
    level
--------------------------
   default         74.12
     -o2           87.11
     -o3           90.04
The difference of 12 req/sec between a default and -o2 optimization is quite big and almost equals to the 15 req/sec achieved by an interpreted PHP script!
What’s more in case of -o3 optimization Drupal is up to 6 times faster than in a pure PHP environment.

Summary

As I mentioned on the outset Drupal is not the fastest system on the Internet. However, after several changes in the code to add compatibility with HipHop for PHP, it becomes a very effective tool in the hands of every webmaster.

Other articles about HipHop for PHP

Wednesday, December 21, 2011

What to Look for in PHP 5.4.0

SkyHi @ Wednesday, December 21, 2011
PHP 5.4.0 will arrive soon. The PHP team is working to bring to some very nice presents to PHP developers. In the previous release of 5.3.0 they had added a few language changes. This version is no different. Some of the other changes include the ability to use DTrace for watching PHP apps in BSD variants (there is a loadable kernel module for Linux folks). This release features many speed and security improvements along with some phasing out of older language features (Y2K compliance is no longer optional). The mysql extensions for ext/mysql, mysqli, and pdo now use the MySql native driver (mysqlnd). This release also improves support for multibyte strings.

Built-in Development Server

In the past, newcomers to PHP needed to set up a server. There was no built in server like a few other languages/web frameworks already had. If developing on *nix, a server needed to be set up with the right modules and the the files to tested needed to be copied over to the document root. Now, you can just run PHP with some options to get a server:
$ php -S localhost:1337
It runs in the current directory, using index.php or index.html as the default file to serve. A different document root can be specified as either an absolute or relative path:
$ php -S localhost:1337 -t /path/to/docroot
The server will log requests directly to the console. Interestingly, this server will not serve your static files unless your script return false. Existing frameworks will need to be modified to add in functionality that is commonly in rewrite rules. This is really all that is needed:
// If we're using the built-in server, route resources
if (php_sapi_name() == 'cli-server') {
  /*
   * If the request is for one of these image types, return false.
   * It will serve up the requested file.
   */
  if (preg_match('/\.(?:png|jpg|jpeg|gif)$/', $_SERVER["REQUEST_URI"]))
    return false;
}
// Process the rest of your script
One of the inconveniences of the server is lack of support for SSL. Granted, it is meant for development purposes only. However, some projects I’ve worked on required testing with SSL. Perhaps there will be demand for this once it’s out there.

An Overview of Traits

Traits are bits of code that other objects can use. Traits allow composing objects and they promote code reuse. The Self programming language, one of the precursors to the JavaScript language, introduced them. JavaScript, strangely, does not directly implement traits; instead it allows one to extend objects directly with other objects.
In PHP (and other languages), traits cannot be instantiated, only used to compose other objects. Traits do not imply inheritance, they just add methods to classes. They can be used with inheritance and interfaces. Traits could be used as standard implementations of interfaces, then one could easily compose classes that comply with certain interfaces.
Here is an example, demonstrating a simple use of traits:
name().", run!\n";
  }
  // Gets the name of the runner
  // Must be implemented by the class using the trait.
  abstract public function name();
}
/**
 * Define a class that uses the runner trait
 */
class runningPerson {
  // Use the runner trait
  use runner;
  // Used to store the person's name
  protected $name; // Constructor, assigns a name to the person
  public function __construct($name) {
    $this->name = $name;
  }
  // Retrieves the name of the person, required by runner trait
  public function name() {
    return $this->name;
  }
}
$gump = new runningPerson("Forrest");
$gump->run();
When a class implements a method that is also defined in a trait it uses, the method in the trait takes precedence and overrides the class method. If two traits implement the same method, the conflict needs to be resolved using the insteadof keyword, or given a new signature (only visibility and name can be changed) using the as keyword.
do_something();
$testB->do_something();
$testB->something_else();
More examples reside at the current PHP documentation for traits. If that documentation is lacking in substance, Wikipedia’s article on traits links plenty of background info.

Changes to Anonymous Functions

In PHP 5.3.x, working with anonymous functions needed a work around when stored in an array. The function needed to be stored in a temporary variable before it could be called. For instance:
$functions = array();
// assign an anonymous function to an array element
$functions['anonymous'] = function () {
  echo "Hello, the parser needs to make up a name for me...\n";
};
// to call it you had to do this:
$temp = $functions['anonymous'];
$temp();
Now, anonymous functions stored in an array can be called directly without first storing them in a temporary variable:
// assume $functions[] is still around.
$functions['anonymous']();

Closures in Classes

Closures defined inside of a class are automatically early bound to the $this variable. If a class method returns a closure, it retains access to the original class that defined it (along with all the public member properties and methods) no matter where it is passed. If assigned to a member property and called as a method, PHP issues a warning if it was called directly. If called as a local variable or by calling the Closure::__invoke() method (which it inherits), PHP issues no warning.
value = $value;
  }

  public function getValue() {
    return $this->value;
  }

  public function getCallback() {
    return function() {
      return $this->getValue();
    };
  }
}

/**
 * Create a class that calls a closure.
 */
class ClosureCaller {
  private $callback;

  public function setCallback($callback) {
    $this->callback = $callback;
  }

  public function doSomething() {
    // Since this is a member variable, call Closure::__invoke().
    echo $this->callback->__invoke() . "\n";
  }
}

// Set up a class to generate closures that reference itself
$test = new ClosureTest();
$test->setValue(42);
$closure = $test->getCallback();
echo $closure() . "\n";

// Test calling a closure from another class
$testCaller = new ClosureCaller();
$testCaller->setCallback($test->getCallback());
$testCaller->doSomething();
Closures allow changing what object scope $this is bound to by calling the bindTo() method and passing in the new object to use as $this.
Currently, no consensus exists around letting closures bound to an object access the private and protected methods of that class. Additionally, PHP still needs to iron out the details around binding closures to static classes. One can find more details about closures, $this, andClosure::bindTo() at https://wiki.php.net/rfc/closures/object-extension

Outlook

There are a lot of established projects that may not immediately start taking advantage of these features, unless the community sees an obvious benefit to drastically changing their projects. When PHP 5.3.0 was released with namespace support and anonymous functions, new frameworks sprung up like Laravel (anonymous functions), FLOW3 (namespaces), Lithium (namespaces), and Symfony2(namespaces). After PHP 5.4.0 is released, I’m sure new frameworks (or new versions, like Symfony2 vs. Symfony) will spring up around using traits to compose functionality and using the new $thisfunctionality in closures defined in classes. The built in server definitely has some potential for making it easier for developers to debug their apps. It’s just a matter of time before frameworks start taking advantage of it.


REFERENCES
http://jburrows.wordpress.com/2011/12/17/what-to-look-for-in-php-5-4-0/
https://svn.php.net/repository/php/php-src/tags/php_5_4_0RC2/NEWS