DECEMBER 2002 - Volume I - Issue 1 The Magazine For PHP Professionals php|architect Zeev Suraski - Brad YoungZeev Suraski - Brad Young P ERFORMANCE M ANAGEMENT O PPORTUNITIES Reviewed for you: Zend Performance Suite Davor's PHP Editor Plus: SNMP Management With PHP PHP nCurses Shell Programming Writing a Webmail System Distill PDFs For Free With PHP Create a DB Abstraction Layer In Colaboration with SUBSCRIBE AND WIN! Comparing PHP With the Industry at Large Technologies Ltd. Visit www.zend.com for evaluation version and ROI calculator Zend Performance Suite Reliable Performance Management for PHP Serve More. With Less. The designers of PHP offer you the full spectrum of PHP solutionsThe designers of PHP offer you the full spectrum of PHP solutions IINNDDEEXX Departments 4 | Editorial rants 5 | New Stuff 6 | PHP-Win A Look at Davor’s PHP Editor 61 | Product Review Zend Performance Suite Version 2.5 65 | Tips & Tricks by John W. Holmes 70 | For Your Reading Pleasure Book Reviews by the php|a team 72 | Exit(0); A War With No Enemies TABLE OF CONTENTS December 2002 · PHP Architect · www.phparch.com 3 php|architect Features 9 | Performance Management Opportunities by Zeev Suraski and Brad Young 14 | Curses! Writing Cool Shell Applications With PHP by Marco Tabini 25 | SNMP Management With PHP by Harrie Hazewinkel 34 | A Concrete Look at Database Abstraction Layers by Maxim Maletsky 41 | Creating A Visual Catalogue System with PHP and IE by Davor Pleskina 49 | Writing a Web-based PDF Converter by Marco Tabini 55 | Directing Mail with qmail’s Maildirs by Jonathan Sharp EEDDIITTOORRIIAALL RRAANNTTSS EDITORIAL December 2002 · PHP Architect · www.phparch.com 4 php|architect Volume I - Issue 1 December, 2002 Publisher Marco Tabini Editors Arbi Arzoumani Marco Tabini Graphics & Layout Arbi Arzoumani Administration Emanuela Corso Authors Arbi Arzoumani, Harrie Hazelwinkel, John W. Holmes, Maxim Maletsky, Davor Pleskina, Jonathan Sharp, Zeev Suraski, Marco Tabini, Brad Young php|architect (ISSN 1705-1142) is published twelve times a year by Marco Tabini & Associates, Inc., P.O. Box. 3342, Markham, ON L3R 6G6, Canada. Although all possible care has been placed in assuring the accuracy of the contents of this mag- azine, including all associated source code, listings and figures, the publisher assumes no responsibil- ities with regards of use of the information con- tained herein or in all associated material. Contact Information: General mailbox: info@phparch.com Editorial: editors@phparch.com Subscriptions: subs@phparch.com Sales & advertising: sales@phparch.com Technical support: support@phparch.com Copyright © 2002 Marco Tabini & Associates, Inc. — All Rights Reserved H ere we are. After months of preparations—and a few sleepless nights—you hold in your hands the very first copy of php|architect! While most editors will spend their first editorial explaining at length the arduous path the brought the fruit of their labor to life, I feel I have a higher goal to aspire to. Getting php|a off the ground has introduced us to some genuinely new experi- ences—nothing beats being awake at two o’clock in the morning (when you couldn’t read the lettering off a truck if it hit you) trying to pick a highly legible font that will work both on the screen and in print. Still, I don’t think you want to hear about that. Let me, instead, give you an idea of how this issue is organ- ized, so that you can take advan- tage of it to its fullest potential. We chose PDF and electronic distribution as a move that, we thought, would drive down our costs and, in turn, make it affordable for anybody in the world to subscribe or buy a sin- gle issue of php|a. Still, we rec- ognize that there are some unique challenges related to using this distribution strategy, and we have taken a few steps that (we hope) will make it easi- er for everyone to enjoy our magazine. The PDF file that you have received has been designed both for on-screen viewing and for printing on a regular inkjet or laser printer. This way, you can look the contents over directly on your computer, and you can print whatever interests you—or the whole thing, if you like the feel of paper in your hands. In fact, although the PDF file itself is encrypted and you should not redistribute it electronically, you can print as many copies of it as you like. If you decide that on-screen viewing is your bag, then here are a couple of pointers. Most of our articles are hot-linked to the Internet—whether it’s an e-mail address to contact an author or the URL of a website from which you can download some great information. Links are an oppor- tunity for us to offer you some- thing that a print magazine will never be able to—a direct con- nection to the world. Naturally, all our advertisements are also linked—so, show us your sup- port and do visit our advertisers’ websites. They all offer great products. No, really, they do! To make the navigation easier, you will find a full table of con- tents, which can be accessed from within Acrobat’s book- marks tab. Notwithstanding all these steps we’ve undertaken, PDF remains a “new” way of publish- ing a magazine, and we can only hope to get better with time. Your feedback in this process is crucial—we want to hear what we’ve done right, but, especial- ly, we want to hear what we’ve done wrong, so that we can fix it. Drop us an e-mail at info@phparch.com and let us know what you like and don’t like, what topics you want to see discussed in future issues and any comments you may have about this month’s articles. Happy reading! NNEEWW SSTTUUFFFF NEW STUFF Open-source Gets a Rap-around As open-source software becomes more and more accepted as a viable alternative to its commercial coun- terpart in IT industry, it’s becoming painfully obvious that it’s necessary to build an infrastructure to facilitate the exchange of information between developers and customers. Thanks to its open nature, OSS lends itself well to customization— something that is nor- mally unthinkable with commercial products. Since there is no licens- ing cost, companies find it easy to use open- source projects as the basis for larger initiatives that have very specific requirements. This results in ad-hoc solu- tions that are based on solid, well-tested code and can be deployed in a short time. Open-source developers are in the unique position of possessing the knowledge needed to successfully mar- ket customization and special features to their software packages. Unfortunately, they often suffer from a lack of exposure that would allow them to defray develop- ment costs of new features by offering such features to multiple customers at the same time. Marcheka Software Ltd. aims at filling the void between OSS vendors and clients through their Rap-X offering, which can be found at http://www.rap-x.com. Labeled as a “collaborative sales & support platform”, Rap-X works by allowing prospective clients to make requests for new features to be added to software pack- ages in a collaborative way, so that multiple requests for similar changes can be merged into a single one and the development costs can be amortized among sever- al players. With this approach, everyone ends up a winner: developers get the exposure they need and can open new markets for their products while still benefiting the public at large by keeping their products free. Customers, on the other hand, benefit from the fact that they can enjoy customized software at a fraction of the costs normally associated with it. Rap-x, which already counts five different open- source projects among its clients, is offered both as an ASP (as in Application Solutions Provider, not Active Server Pages!) product and through a more traditional licensing model. You can find its website here. PHP 4.3 Inches Nearer With the release of RC 2 at the end of November , the new PHP version 4.3 is one step from its final release. The new version of our beloved language includes sev- eral enhancements and performance optimizations compared to its predecessor. Our favorite one is the ability to fetch SSL-protected pages directly through the fopen() URL wrappers—a major step forward, in our opinion, toward making PHP ever more appealing for commercial applications. You can download PHP 4.3 RC2 from the PHP Group’s main page. Sessions, Anyone? Farpointer Technologies has released a new session management tool for Apache called iConductor. Yes, we know, PHP already supports session management. iConductor, however, is interesting because it does so independently of PHP (or any other language) by hook- ing directly into Apache. This makes it possible, for example, to manage sessions directly from within HTML pages, which, in turn, could mean lower over- head when processing pages. iConductor costs $1,295.00 and is available directly from Farpointer at http://www.i-conductor.com. December 2002 · PHP Architect · www.phparch.com 5 You’ve got a great software product and committed users, but you need additional fund- ing to develop the features they require. php|a PPHHPP-WWIINN F or a vim addict (recently converted to using gvim and the open-source CREAM extension) like myself, GUI IDEs are something to look at with an air of suspi- cious resentment. On one hand, they often look easy to use and user-friendly (something that can’t really be said about vim). On the other, however, my shell junkie friends and I fear that, as soon as you need to do some- thing out of the ordinary, they will turn into rigid mon- sters that won’t allow you to move past their pre-set functionality. Luckily, I’ve had a chance to find out that it’s not nec- essarily always the case. After having been scarred for years by Microsoft’s Visual Studio IDE, I had a chance to use Zend Studio, which I found extremely powerful (particularly in its debug functionality, which are very valuable), although made a little slow by the fact that it’s based on Java. Still, even for its price, Zend Studio is extremely good—and works across multiple platforms. A few weeks ago, I came across Davor Pleskina’s PHP Editor (DPHPE from now on), which is available from his website at http://www.pleskina.com. Davor is a great guy (he’s also the author of this month’s article on catalogue management), and he suggested that I give his IDE a try. Firing Up DPHPE is extremely easy to install, although it does- n’t come with a setup program of its own. You do have to manually copy the files from the ZIP file you can find at Davor’s website into a folder on your PC and create the necessary shortcuts. I can’t see this as being a huge problem, since it’s safe to assume that you probably have some level of confidence with the OS if you’re interested in this product. Still, creating a setup pro- gram is not a particularly challenging endeavor these days—there are plenty of tools for that—and a more guided approach to installation would probably benefit most users. To give you an example, during my first attempt at running DPHPE, I simply executed the appli- cation’s main executable from directly within WinRAR (my compression application) without copying any supporting files. The results were not catastrophic—the application was running fine for the most part—but I had lost access to some functionality, such as keyword autocompletion. Once installed, DPHPE takes up very little space on PHP-WIN December 2002 · PHP Architect · www.phparch.com 6 Reviewed For You Davor’s PPHP Editor By Arbi Arzoumani DPHPE is also able to main- tain a list of all the variables that are currently in scope in your script . PHP-WIN Davor’s PHP Editor your hard drive, which luckily also means that it uses very little RAM when it has to run. As much as this does- n’t seem to be a big problem these days, if your machine is running a number of different services at the same time, the last thing you need is another applica- tion that gobbles memory. Basic Features Where do I begin? DPHPE has a wide array of differ- ent features that are undoubtedly helpful for any devel- oper. Most impressive of all, these features are imple- mented in a simple and efficient way, so that they actu- ally help without getting in the way (at least most of the time). The editor that comes with the application features syntax highlighting (for HTML, PHP, Javascript and CSS), and its aspect is fully customizable—both from the point of view of the fonts used and of the colours in which the syntax is rendered. Also featured in the editor is the ability for auto-completion of key- words, which is particularly useful for a language as complex as PHP. It is also possible to have the applica- tion create a list of functions defined in your scripts, although this doesn’t always work well—for example, I had a comment that contained the word “function”, and the editor mistakenly recognized it as a declara- tion. DPHPE is also able to maintain a list of all the variables that are currently in scope in your script (although, unfortunately, it is unable to extend this capability to any include files you may be using) and to automatical- ly pop up a window that allows you to choose a vari- able whenever you type the dollar sign. This feature tends to be a bit annoying after a while, but it can be turned off and the variable list can be used manually as needed. My only real beef with the editor is that I couldn’t find a way to word-wrap my long lines of code. Not everyone uses this feature, but I find it very convenient and not having it makes it a bit difficult to use the edi- tor in a comfortable way. December 2002 · PHP Architect · www.phparch.com 7 Another well-developed fea- ture of DPHPE is the ability to create and maintain projects. PHP-WIN Davor’s PHP Editor Project Management and Web Preview Another well-developed feature of DPHPE is the abil- ity to create and maintain projects. A project is simply a repository of files but, contrary to a folder, it also pos- sesses a number of properties that make it possible, for example, to preview entire web projects at the click of a mouse. What’s most interest- ing, however, is that the concept of projects lays the foundation for a number of future expansions, such as encoding, CVS or SourceSafe compatibility, global search and replace, and so on, that could all be accessi- ble at the click of a mouse. Speaking of web preview, if you have Internet Explorer installed on your machine (which you must have, unless you’re run- ning some ancient version of Windows), DPHPE is capable of interfacing to it and visualizing your PHP scripts by executing them through your web server. If you prefer, you can also launch IE separately rather than within the editor. Unfortunately, DPHPE does not feature an internal debugger—and I personally think that this should be a very important future enhance- ment. Still, this application offers a great deal of func- tionality at a very convenient price—it’s free. What’s more, there is no big corporate presence behind it, so that it really evolves following the needs of its users, and Davor does a great job of listening. December 2002 · PHP Architect · www.phparch.com 8 php|a FFEEAATTUURREESS FEATURES December 2002 · PHP Architect · www.phparch.com 9 T o start, let’s consider the reasons why we should be concerned with Performance Management. In a nutshell, it can be summarized with two simple goals: 1) Save Money 2) Happy Customers Although this may seem obvious, or even trite, it is important to keep in focus when evaluating and deploying performance-related products. To take it one level deeper, let’s understand the issues faced by IT managers and webmasters today: · High expenses for hardware and software pur- chases to support http site traffic · High expenses for database server hardware and software · IT administration time and money spent to reconfigure and maintain server farms · Low customer satisfaction resulting from unrea- sonable latency time and load time With these issues in mind, we can now more clearly attack the question of how to achieve improvements for our projects. Options For Performance Management There are many different ways through which you can squeeze more juice from your limited hardware server resources. These options include hardware, soft- ware, and networking alternatives. We’ll highlight each of these options below. Note that these alternatives are not mutually exclusive. Hardware Options More Hardware - The most common indicator – aside from emails from angry site users – that raises a red flag about performance issues is maxed out CPU utilization. And when this happens, an obvious but costly solution is simply to buy more hardware. If you have two dual-CPU http server machines whose CPU’s Performance Management Opportunities By Zeev Suraski and Brad Young Why Focus on Performance Management? A look at the various alternatives that exist in today’s complex IT world to improve server performance for sites that run PHP. As Yogi Berra famously stated, “It’s hard to make predictions, especially about the future.” For PHP, however, there are some things that are not so difficult to predict. FEATURES Performance Management Opportunities are maxed out, then adding a third machine, with load balancing, will alleviate some of this load stress. It is worth noting that this solution works for steady state performance issues, but does not scale well for spiked performance issues. For example, if your site experi- ences dramatic increases in requests at specific times of the day, you’ll need to add enough hardware to cover the ‘worst-case’ load. (Actually, it is more accurate to call this the ‘best-case’. We want more traffic on our sites, after all.) Smarter Hardware – Similar to adding more hard- ware, adding smarter hardware can provide perform- ance improvements. This includes the more obvious steps of having machines with faster CPUs, or more CPUs per machines. Beyond that, there is also hardware components such as disk drives with built-in memory caching that also help improve hardware performance. Hardware Devices – Another hardware option to improve performance is to utilize hardware devices that have built-in algorithm circuitry. For example, perform- ing SSL encryption using software on your http server adds a significant stress to the CPU load. You can add a hardware card that performs this encryption without requiring any CPU time, thus lowering the effective load. Similarly, this can be done for gzip compression as well. Faster Code It is clear that if your code is faster and more efficient, you’ll be able to handle more requests with the same hardware. Let’s explore how we can make code run faster. C Libraries– The massive adoption of PHP has come about mainly due to its ease of adoption – both from a cost perspective as well as a development perspective. But while PHP speed has improved tremendously, it is a fact that C code runs faster. So if you develop some of your more algorithmically-complex functions in C and call out to them from within your PHP scripts, you can speed up your code. Of course, by doing this, you give up some of the flexibility, transportability and ease of use that brought you to PHP in the first place. Write Better Code – Better coding practices, while tiresome at times to adopt, pay off in the long run. Always stay up to date and aware of the best ways to use PHP by following email lists from php.net and read- ing tutorial materials at sites like PHPbuilder.com and at the Zend Developer Zone (devzone.zend.com)—not to mention the pages of php|a, of course! Code Acceleration – PHP is an interpreted scripting language. This means that each time a PHP script needs to run, the core Zend Engine will parse that file and compile it in memory, then execute it. Since the PHP scripts themselves typically don’t change from request to request, this parsing and compilation phase is repetitive and wasteful. Code Acceleration products available today can eliminate that redundancy by main- taining the compiled version of the file in memory. Output Content Caching Imagine that your friend asked you what the headline of the New York Times was today. To respond, you go outside, walk two blocks to the nearest news-stand, read the headline of the New York Times, then come back and tell him what that headline says. (We leave it to you to understand why your friend would ask this, but just go with the analogy). Now, imagine that another friend calls you and asks the exact same question. Would you repeat the journey down to the news-stand? Unless you are madly in love with the salesperson, probably not. You are smart enough to identify that this new request is identical to the previous request, and hopefully you are also smart enough to remember what the headline was. Well, unfortunately your http server is not as smart as you, and it, figuratively speaking, is probably making way too many trips down to the news-stand (read: database server). This is where content caching comes in. The idea of content caching is straightforward: your server works hard to generate some results to a specif- ic request. Then it keeps those results on hand, and if it gets a similar request, it re-uses the previously generat- ed results. It is interesting to note that caching takes place at the http server level and reduces load on that server, but an even bigger load savings is experienced on the database server, which sees dramatically less SQL requests coming in. The concept is simple enough, but with all things programmatic, the devil is in the details. The challenge mainly boils down to: · Cache Hit Identification – How does the cache determine if this is indeed a similar request, or is it a different request (i.e. Is my second friend asking for the same headline, or is he asking December 2002 · PHP Architect · www.phparch.com 10 PHP is an interpreted script- ing language. This means that each time a PHP script needs to run, the core Zend Engine will parse that file and compile it in memory, then execute it. [...]... database throughput Why Performance Management Matters We opened this article explaining the benefits that Performance Management brings you and your organization: Save Money and Happy Customers But the importance runs even deeper Collectively, the PHP community is proving every day that PHP is ready for front-line, enterprise-level applications Implementing performance management solutions such as those... interface known as Management Information Base (MIB) The agent responds to queries from managers that request information from its MIB A manager can therefore create a complete and coherent picture of the network’s status by requesting specific management information from multiple agents The Management Information Base The Management Information Base (MIB) contains the managed objects under management by...FEATURES Performance Management Opportunities Figure 1- Content Caching Schematic about the New York Post, or is he asking about yesterday’s New York Times, or is the headline different for him because he gets the national edition instead of the local edition)? · Cache Management – How does the cache manage the information results that are... provides a brief explanation of the SNMP protocol and explains the SNMP functionality that is available through PHP Simple Network Management Protocol T he Simple Network Management Protocol (SNMP) provides a framework for network management SNMP enables access to network management information to make, for instance, status monitoring, faultdetection and configuration possible Since its introduction,... have almost covered 70% of MIS, Collaboration, Project Management, Workflow based system just in two months This was only possible due to PHPLens You can develop high quality programs at the speed of thinking with PHPLens Visit phplens.com for more details Free download December 2002 · PHP Architect · www.phparch.com 12 FEATURES Performance Management Opportunities Database Optimization Data Models and... could cache the headlines page or at least 5 minutes Now, if 1,000 page view requests come in during Figure 2 - Partial Page Caching December 2002 · PHP Architect · www.phparch.com 11 FEATURES Performance Management Opportunities that 5 minute period, only one actual PHP execution and one database access will occur, a dramatic decrease compared to the 1,000 that would otherwise occur All the other requests... today and attracting many more developers to join the movement Zeev is a co-founder and CTO of Zend Technologies Ltd, the leading provider of development and performance management tools for PHP-enabled enterprises Brad Young leads the Product Management team at Zend, and has over 15 years experience in on-line database and XML systems deployment You can reach both Zeev and Brad via Zend's website at... becomes much easier Enterprise Performance – Future Directions As Yogi Berra famously stated, “It’s hard to make predictions, especially about the future.” For PHP, however, there are some things that are not so difficult to predict · First, Apache 2.0 adoption may start to grow, Data Compression Gzip Compression – Performance problems” is a big umbrella Sometimes, the performance issue is not necessarily... entities through the appropriation of another SNMP entity’s identity 4 Disclosure of management information that is December 2002 · PHP Architect · www.phparch.com transferred between SNMP entities (users) 5 Eavesdroppers that attempt to capture the management data for other “evil” purposes 6 Unauthorized access to particular management information If you compare these capabilities with earlier version of... to performance from Apache 2.0’s multithreaded capabilities, on platforms such as Solaris PHP 5.0, with its new Zend Engine 2, will include new object handler capabilities that will not only make coding easier, it will also speed up application performance In the database world, newer extensions and support for MySQL 4 and Oracle will likely give another shot in the arm for database throughput Why Performance . http server machines whose CPU’s Performance Management Opportunities By Zeev Suraski and Brad Young Why Focus on Performance Management? A look at the various. | Performance Management Opportunities by Zeev Suraski and Brad Young 14 | Curses! Writing Cool Shell Applications With PHP by Marco Tabini 25 | SNMP Management