Monthly Archives: July 2010

Generating Word documents from PHP

PHPDOCX is a PHP library that allows its client code to generate Microsoft Word documents in the .docx format from PHP scripts. PHP is increasingly being used for disparate goals and has to deal with data that comes from strange sources and has to be produced in stranger formats. An off-the-shelf solution for the creation of Word documents from an arbitrary source — being it a database, Excel or a csv file — is indeed a good tool to keep at hand.

Starting with the 1.5 version, which has been released on July 12th, PHPDOCX is now compatible with PHP 5.3. The adoption of PHP 5.3 from operating systems is growing and it will at last replace the previous versions of PHP also in the servers of hosting providers.

Features

PHPDOCX provides some standard features that you would commonly use when generating a document dynamically: managing text, list, tables, images and graphic elements are all basic operations of document editing.

There are more useful features included in the library, which come handy when dealing with long documents. For instance, insertion of headers, footers, page numbering, and table of contents are all supported.

A final note on the feature list is the possibility of outputting PDF and HTML from a given Word document. The library is intended for generation of reports and being able to switch the output format at will is a great point.

Technicalities

PHPDOCX has no requirements for a functional version of MS Word, except for generating legacy versions of the documents (.doc format for Word 2004 or before).

The library does requires the zip and xsl PHP extension to work, but they are probably already installed on your server of choice, or available at will. Apart from that, a generic installation of PHP and Apache will suffice.

Licenses

Like many libraries for web development, PHPDOCX comes with more than one license.

The first possibility is to use the library with an LGPL license, which covers the free version. It has somewhat limited features in comparison to the Pro one, but it includes no watermarks in the produced documents nor it has time limits.

The Pro version has greater capabilities, like the insertion of graphs and MathML constructs for scientifical documents. It also provides technical support, which may be the most compelling point for its adoption.

In conclusion, PHPDOCX is a valid tool to manage production of documents in one of the most diffused formats of the world. It also manages PDF and HTML, which guarantee interoperability with any end user’s machine

EclEmma – Java Code Coverage for Eclipse

EclEmma is a free Java code coverage tool for Eclipse, available under the Eclipse Public License. Internally it is based on the great EMMA Java code coverage tool, trying to adopt EMMA’s philosophy for the Eclipse workbench:

Fast develop/test cycle: Launches from within the workbench like JUnit test runs can directly be analyzed for code coverage.
Rich coverage analysis: Coverage results are immediately summarized and highlighted in the Java source code editors.
Non-invasive: EclEmma does not require modifying your projects or performing any other setup.

The Eclipse integration has its focus on supporting the individual developer in an highly interactive way.

Features

Launching

EclEmma adds a so called launch mode to the Eclipse workbench. It is called Coverage mode and works exactly like the existing Run and Debug modes. The Coverage launch mode can be activated from the Run menu or the workbench’s toolbar:

Simply launch your applications or unit tests in the Coverage mode to collect coverage information. Currently the following launch types are supported:

* Local Java application
* Eclipse/RCP application
* Equinox OSGi framework
* JUnit test
* TestNG test
* JUnit plug-in test
* JUnit RAP test
* SWTBot test

Analysis

After your application or unit test has terminated code coverage information is automatically available in the Eclipse workbench:

* Coverage overview: The Coverage view lists coverage summaries for your Java projects, allowing drill-down to method level.
* Source highlighting: The result of a coverage session is also directly visible in the Java source editors. A customizable color code highlights fully, partly and not covered lines. This works for your own source code as well as for source attached to instrumented external libraries.

Additional features support analysis for your test coverage:

* Different counters: Select whether instructions, lines, basic blocks, methods or loaded types should be summarized.
* Multiple coverage sessions: Switching between coverage data from multiple sessions is possible.
* Merge Sessions: If multiple different test runs should be considered for analysis coverage sessions can easily be merged.

Import/Export

While EclEmma is primarily designed for test runs and analysis within the Eclipse workbench, it provides some import/export features.

* Coverage data import: A wizard allows to import *.ec coverage data files from external launches.
* Coverage report export: Coverage data can be exported as a *.ec file or in XML or HTML format.

Never Use $_GET Again

Please don’t need to use $_GET or $_POST anymore. In fact, probably we shouldn’t use $_GET and $_POST anymore. Since PHP 5.2, there is a new and better way to safely retrieve user-submitted data.

The clever developers have constructed a library that analyzes data and escapes it appropriately. But the problem of validating and sanitizing input is still a substantial issue. Many seasoned PHP developers still spend precious development cycles building custom code to filter input.

PHP (from 5.2 onward) has a built-in filtering system that makes the tasks of validating and sanitizing data trivially easy. Rather than accessing the $_GET and $_POST superglobals directly, you can make use of PHP functions like filter_input() and filter_input_array(). Let’s take a quick look at an example:

$my_string = filter_input(INPUT_GET, ‘my_string’, FILTER_SANITIZE_STRING);

The code above is roughly the equivalent of retrieving $_GET[‘my_string’] and then running it through some sort of filter that strips HTML and other undesirable characters. This represents data sanitization, one of the two things that the filtering system can do. These are the two tasks of the filtering system:

* Validation: Making sure the supplied data complies with specific expectations. In this mode, the filtering system will indicate (as a boolean) whether or not the data matches some criterion.
* Sanitizing: Removing unwanted data from the input and performing any necessary type coercion. In this mode the filtering system returns the sanitized data.

By default, the filter system provides a menagerie of filters ranging from validation and sanitization of basic types (booleans, integers, floats, etc.) to more advanced filters which allow regular expressions or even custom callbacks.

The utility of this library should be obvious. Gone are the days of rolling our own input checking tools. We can use a standard (and better performing) built-in system.

If we take things one step further than merely presenting this as an option. We can say that we should no longer directly access superglobals containing user input. There is simply no reason why we should. And the plethora of security issues related to failure to filter input provides more than sufficient justification for my claim. Always use the filtering system. Make it mandatory.

“But,” one might object, “what if I don’t want my data filtered?” The filtering system provides a null filter (FILTER_UNSAFE_RAW). In cases where the data needn’t be filtered (and these cases are rare), one ought to use something like this:

$unfiltered_data = filter_input(FILTER_GET, ‘unfiltered_data’, FILTER_UNSAFE_RAW);

Following this pattern provides a boon: We can very quickly discover all of the unfiltered variables in the code by running a simple find operation looking for the FILTER_UNSAFE_RAW constant. This is much easier than hunting through calls to $_GET to find those that are not correctly validated or sanitized. Risky treatment of input can be managed more efficiently by following this pattern.

Filters won’t solve every security-related problem, but they are a tremendous step in the right direction when it comes to writing safe (and performant) code. It’s also simpler. Sure, the function call is longer, but it relieves developers of the need to write their own filtering systems. These are darn good reasons to never use $_GET (or $_POST and the others) again.

10 Ways to Increase Hard Disk Life and Performance

Performance of hard disk has always been an underrated aspects of the overall system performance. The hard disk were considered only as a place to store and people paid heed to how they affected the operation of the PC as a whole. Each time you read and write on the hard disk some performance is lost, because the disk subsystem is the slowest component in your computer system. There is not much to do about the fact, but you might be able to take a number of actions to make certain that the computer’s hard disk is always running in tiptop shape. We provide 10 ways to increase hard disk life and performance.

1. Remove duplicate files from hard disk

The first step towards enhancing the performance of duplicate files from hard disk. There are several free duplicate file finders that easily find all sort of duplicate files on the disk. Users can then remove all the duplicate copies and just keep a single one. You might use duplicate file finders such as Duplicate Cleaner. It might even find files that have some common content, even if the file names are different.

It is an important step in any hard disk cleaning exercise. Removing all the duplicate files from the hard disk can considerably reduce the space occupied on the hard drive.

2. Defragment Hard Disk

It’s one most widely known that speeds up the hard disk and improves performance.

Step 1: Now open My Computer. Right-click the disk that you want to defragment and click Properties. On the Tools tab, click the Defragment Now button.
Step 2: The Disk Defragmenter window appears. Click the Analyze button.
Step 3: An analysis of the drive is performed, and a message appears telling you whether or not you should defragment the drive.
Step 4: If the drive needs to be defragmented, click the Defragment button. The defragmentation process begins and may take some time, depending on how badly the drive is fragmented.

3. Checking up for disk errors

It’s easy to check the disk errors as another useful tool that is provided by Windows XP. The tool is available in the Tools tab of the hard disk properties sheet. It offers simple check box options to check for file system errors and recover bad sectors. The error checking tool needs complete access to the disk for its work. Sometimes the application is closed and the user needs to reboot before it starts to gain complete access to the disk. In case you use the computer a lot it’s a great idea to run this tool once for a month to ensure your disk is working perfectly.

To use the error checking utility

Step 1: Go to Start > My Computer
Step 2: Right click on the hard disk or partition that you want to check for errors
Step 3: Click on properties and then “Tools”
Step 4: Under “Error checking” click on “Check Now”
Step 5: Select the Scan for and attempt recovery of bad sectors
Step 6: Click on Start

This will scan the disk for errors and mark bad sectors

4. Compression/Encryption

In the NTFS you can compress folders and encrypt folders and files to stop unauthorized access of those files and folders. Given that the compression feature is impressive, compressed files takes longer to open and resave. If you want the maximum possible speed from the system you need to avoid compressing the drives.

Encryption will also reduce the performance in terms of the opening files. The encryption process needs to make sure that you have an authorized view of the file. In general the rule is to follow the encrypt files or folders that are necessary. Don’t get in the habit of encrypting everything.

5. To NTFS overhead disable the 8.3 filenames

NTFS is a feature packed file system that Windows XP users can work with. For compatibility with MS-DOS and old Windows 3.x systems, NTFS supports 8.3 filenames. It implies that the files are named with eight characters, followed by a dot and three-charecter extensions. It’s nothing wrong with it. Overload is unnecessary when you are not supporting older programs and systems. Some other programs depend on 8.3 filenames, so you need to turn off the 8.3 filename feature some programs might not work properly. It is unlikely at this point in time.

To reduce NTFS overhead we have the following steps

Step 1: Click Start>Run. Type regedit and click OK

Step 2: In the Registry Editor, navigate to
HKEY_LOCAL_MACHINE\SYSTEM \CurrentControlSet\Control\Filesystem.

Step 3: Locate the NtfsDisable8dot3NameCreation entry and change the value to 1. This will disable the creation of 8.3 filenames
Step 4: Close Registry Editor

6. Master File Table

The NTFS Master File Table (MFT) keeps track of files on disks. This file logs all the files that are stored on a given disk. It includes the entry for the MFT itself. It works like an index of everything on the hard disk in much the same way that the address book. It makes the index of all these files so are easy to locate for defragmentation and for use by application.

You can add a registry entry to ensure the table is large enough and has the space it requires. It will take up more space on the hard disk that reduces overall NTFS overhead to help general performance.

Steps 1: Click Start>Run. Type regedit and click OK

Step 2: In the Registry Editor, navigate to

HKEY_LOCAL_MACHINE\SYSTEM \CurrentControlSet\Control\FileSystem

Step 3: Create a REG_DWORD entry and name it NtfsMftZone Reservation

Step 4: Set the value of the entry to 2.
Step 5: Close the Registry Editor.
Step 6: Set the value of the entry to 2

7. Stop Hibernation

In Windows systems like XP the Hibernation feature is quite handy. It can be shared with others to optimize Windows XP disk performance to switch off hard disk Hibernation mode in Windows XP. To use this mode follow the steps below

Step 1: Click Start
Step 2: Control Panel
Step 3: Power Options Properties
Step 4: Click on the Hibernate tab and clear the Enable Hibernation check box

8. Clean up unnecessary files and optimize the Recycle Bin

The number of temporary files, temporary Internet files, recent documents list in Windows start menu, download files and log files which Windows XP generates. In case you want the hard disk to perform in the best possible way, just delete all the junk files from it. It is an overstuffed hard drive that makes Windows XP work harder. Make sure that you have the system in place to keep the old files and junk cleaned up and removed.

Make sure you optimize the Recycling Bin. The size of recycle bin is the percentage of hard drive.

Step 1: Right click on the Recycle Bin and choose Properties
Step 2: In the Recycle Bin properties, move the recycle Bin size slider from 10 percent to 3 or even 1 percent. It is still a decent amount of storage since you now have a larger disk to work with.
Step 3: Click OK

9. Convert to NTFS

NTFS is better than FAT/FAT32 and allows users to use some management features of Windows XP that FAT/FAT32 does not support. Try to convert any FAT/FAT32 drives to NTFS. It’s only exception is the rule used in a dual-boot system that also boots earlier version of Windows that doesn’t support NTFS, such as Windows 98 or Windows Me.

To convert FAT or FAT32 drive to NTFS follow the steps below

Step 1: Click Start>Run. Type command and click OK.

Step 2: At the command prompt, you will use the Convert command to convert the FAT drive to NTFS. Keep in mind that the conversion process is completely safe and all of your data will remain as it is. The command and syntax is as follows:

convert D: /FS:NTFS
Press Enter.

Step 3: Conversion may take several minutes, depending on the size of the drive. When the process is complete, simply exit the command interface. If you converted the boot partition, you will be prompted to reboot the computer.

10. Remove the temporary files

Windows creates a lot of temporary files during the normal operation. They try to clean up those files while some of them stay and keep accumulating. For instance file fragments, browser cache, memory dumps, log files, cookies, Recycle Bin.

Windows provides a built-in tool to clear such files that is not as efficient as some of the free disk cleaners available in the market. You can use FCleaner, CCleaner, and Comodo System Cleaner.