194 Chapter 5 Basic Web Application Strategies PHP and COM The Component Object Model (COM ) is a software architecture developed by Microsoft that allows abstracting software chunks into binary components. COM defines a standard for component interoperability and specific features a component must have. It doesn t depend on any programming language; indeed, Microsoft shows efforts to establish COM as an open IETF (Internet Engineering Task Force) standard. COM objects can be accessed by any compliant application and programming language; for example,from Visual Basic,Delphi,or PHP. The question COM addresses is this: How can a system be designed in such a way that binary software components from different vendors can interoperate? Microsoft s first proposed answer to this question, the concept of DLLs, failed miserably. Because of the missing versioning in DLLs, no two DLLs exposing the same interface could be used on a single system. Let s make up an example. Say you ve got a server-monitoring application that continually checks whether a server is down and logs the collected data.You have a front end with pretty statistics and some logic to react in an appropriate way if the server is unreachable.The back end of the software could be a DLL performing the actual monitoring: Let s say that ServerSpy.dll exposes a function is_reachable(), taking a server name and returning true if the server is reachable or false if it s not. We haven t defined what kind of server to monitor yet indeed, it could be a Web server, mail server, name server, or any other system, and the function declaration itself as well as the back end wouldn t need to be changed.The only part needing a change would be the implementation of the function is_reachable(). With DLLs, you could have exactly one monitoring service on the system either Web server spy or mail server spy, etc. If you install another DLL, it would overwrite the previous one. Your application couldn t let the user choose between available monitoring services. Nor would it be possible to have a different version of one service available on the system. If another software vendor created a better implementation of ServerSpy.dll and sold that to your customer, it would again overwrite your version of the DLL. If the function worked exactly the same, all would be great otherwise, your application would stop working. A proper component model tries to solve these problems. COM provides a way to identify components through a globally unique ID (GUID), allowing you to use many services concurrently the operating system knows all installed components and your application could query it for components in the category Server Spy monitoring services. COM provides versioning, allowing you to have different versions of a component on the system without affecting each other. And finally, it provides component introspection, allowing you to see which methods and properties a component exposes. You probably have heard of ActiveX controls. ActiveX controls, also known as OLE controls or OCX because of Microsoft s creative marketing department, are simply an application of COM components.They provide a reduced set of COM interfaces so
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP Web Hosting services
192 Chapter 5 Basic Web Application Strategies Traditional Client/Server In the past, a traditional, non-Web application was responsible for handling everything from user input to application logic to data storage.These three entities were interwoven with each other, making it difficult or impossible to change one of them without affecting the others. If you wanted to provide such an application to multiple users,you d have a problem:What if the data format changes? What happens if you re required to change the application logic? You d have to provide all users with a new local copy of the application, an unfeasible approach for larger systems. With the advent of object-oriented analysis, design, and programming principles, business applications became component-based. User interfaces were commonly deployed on workstations, and data and application logic was hosted on a mainframe server.The term client/server has been used to describe this separation of layers or tiers. A two-tier client/server architecture provides a basic separation of tasks.The client,or first tier, is responsible for the presentation of data to the user (user interface), and the server, or second tier, is responsible for supplying data services to the client and handling the application logic. So far, so good, but you see that the two-tier model still combines two distinct concepts in the second tier (the server): data services and application logic.The application logic is the heart of the program,responsible for data validation, processing rules, etc. The data services manage the application s raw data and consist often of a relational database management system (RDBMS) or a mainframe legacy system, in which a company has already invested considerable time and money.The interweaving of these two concepts in a two-tier application introduces problems of scalability, reuse, and maintenance.Take the recent introduction of the Euro as the official currency throughout the European Community: If an application hadn t cleanly separated application logic from data, both tiers would need to be changed. Hard-coded currency values in the data validation were to be altered as well as the data-storage format of currency amounts. Using a multi-tier approach, all three concepts can be cleanly separated. PHP and Multi-Tier Applications The concept of multi-tier systems became popular in the early 1990s and is now establishing itself as the enterprise application software architecture for the late 90s and early 21st century.A multi-tier (often referred to as three-tier) architecture provides greater application scalability, lower maintenance, and increased reuse of components. Three-tier architecture offers a technology-neutral method of building client/server applications with vendors who employ standard interfaces, providing services for each logical layer. Look at the three-tier model in Figure 5.5 doesn t it look familiar to you as a Web application developer? In the first tier is a thin client translated to the world of Web applications, this would be the browser.The middle tier (application server) is obviously PHP (and the Web server as host application), while the third tier consists of an RDBMS.
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP Web Hosting services
Three-Tier Applications 193 I. Client (UI) II. Application Server Business Objects Java Beans COM Controls III. Data Server Figure 5.5 Multi-tier application layers. Working with PHP leads to a basic three-tier model in such a coherent way that you don t even notice it. But there is (or was) a catch to using PHP as an application server. Until version 4.0, PHP was not fully prepared to access third-party software objects and was therefore not the best tool to use in business scenarios with similar requirements.This has changed, and we feel this is one of the most dramatic points in the evolution of PHP.The current version allows you not only to access COM controls on Win32 systems, but also to execute Java methods on any system with a Java Virtual Machine (JVM). There are three standard architectures for such enterprise-wide object reuse: (D)COM from Microsoft, JavaBeans/RMI from Sun Microsystems, and CORBA from the Object Management Group.The most widely used at this time is COM,because of the broad use of Windows systems (see the next section for details on using PHP and COM). However, enterprises with heterogeneous environments will prefer Java or CORBA for their wider platform support. For example, JavaBeans (and alternatively COM objects) are available to access SAP R/3 systems through their Business API (BAPI) interface, enabling you to integrate the SAP business framework into your application server layer.Think about the possibilities this would introduce to an e-commerce system:You could effectively combine a front-end online shop with the enterprise warehousing application in the back end.
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP Web Hosting services
190 Chapter 5 Basic Web Application Strategies You can manually specify the -b option if you cvs add a file, but that soon becomes a kludge to remember and use consistently. It would be easier if CVS could enforce the binary type on some file extensions. Luckily, this is easy using the administrative file cvswrappers.This file allows you to set up actions that will transform files on each commit or checkout. It s similar to the loginfo file in that it will be invoked on a commit, but, unlike directives in the loginfo file, it can execute actions that will alter the file in question.To treat images and archive files as binary,the cvswrappers file could look like this: *.gif -kb *.jp[e]g -kb *.png -kb *.gz -kb *.ta -kb *.zip -kb This will automatically append the -kb to all CVS commands involving files matched by the specified filter.The -k option prevents CVS from trying to use keyword expansion; the -b switch indicates a binary file and disables the ability to produce text diffs for this file. Sometimes you need to interface external command-line tools with the Web. Many UNIX programs expect input on standard input and output the results of the computation to standard output for example, sort.The easiest way to pass input and read output of the program is by using popen() and PHP streams. popen() opens a unidirectional pipe to a program and allows you to pass data to its standard input.And as you already know,you can use fopen() to read the standard input,to which the external program will write.A simple example could look like this: // Open a writing pipe to the sort command $fpout = popen( sort , w ); // Open stdin with PHP-Streams $fpin = fopen( php://stdin , r ); // Output some characters to sort fputs($fpout, an ); fputs($fpout, cn ); fputs($fpout, bn ); // Close the pipe to sort - sort will now process the input // and write it to stdout pclose($fpout); // Sort s stdout is stdin for us - so read it until feof(). while($c = fread($fpin, 1)) { print($c); }
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP MySQL Web Hosting services
Three-Tier Applications 191 CVS Quick Reference While CVS features about 25 main commands, a multitude of options, and even more possible combinations,in real life you usually need only a small set of commands.The following table provides a quick overview that can serve as a reminder for the basic commands. Command Example Description cvs login cvs login Logs into the CVS server; this will ask for your password. cvs checkout cvs checkout phpBook Gets a module from the CVS repository. Usually used to check out a module (entire directory under the top level). cvs update cvs update TOC.xml Updates your local copy of a file or directory. cvs commit cvs commit TOC.xml Sends your local version of files or directories to the CVS server. cvs add cvs add TOC.xml Adds a new file or directory to the CVS server.The addition will be completed the next time you commit. cvs remove cvs remove TOC.xml Deletes a file or directory from the CVS server.The deletion will be completed the next time you commit. Note that this command doesn t actually delete a file; you can still access older versions. cvs status cvs status TOC.xml Shows the modification status of your local version. cvs diff cvs diff TOC.xml Shows the differences between two versions of a file.The default is to show the differences between the local copy and the remote version. cvs logcvs logTOC.xml Shows the CVS log messages for all revisions of a file. Three-Tier Applications With larger development teams, effective means for organizing a project become vital. By now, you ve seen methods of handling the separation between layout and code, organizing directory structures, and using a version-control system for managing the source code.As soon as you talk about distributed development,phase separation,and business processes, marketing people will jump on you and yell about multi-tier applications.What s all the fuss about?
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP MySQL Web Hosting services
CVS: Concurrent Versions System 189 Listing 5.3 Prompt for user input using PHP input/output streams. function readln() { // Initialize return value $ret = ; // Read from stdin until the user presses enter $fp = fopen( php://stdin , r ) or die( Fatal Error: could not read from .stdin ); do { $char = fread($fp, 1); $ret .= $char; } while($char != n ); return($ret); } print( Please enter your firstname:n ); $firstname = readln(); print( You entered: $firstnamen ); The other available streams are php://stdout and php://sterr.This provides an effective and platform-independent way of accessing standard input, standard output, and the standard error-handling device. Indeed, this works even on Windows 98/NT. The concept of PHP streams was introduced with PHP 4.0 and is not available in the 3.x tree. On UNIX systems, you can work around this limitation by directly accessing the UNIX devices /dev/stdin, /dev/stdout, or /dev/stderr. Of course, this works only on systems with valid devices. Most current UNIX systems have it Windows is left out again. If you use CVS to manage all your files on a Web site project not only source code but also images, archives, Flash, etc. you ll soon notice that there are some limits with binary files.Text diffs don t make sense on binary data it s usually not of much interest to you which bits in an image file have changed. CVS can indeed create problems when treating all files as ASCII. CVS treats certain character sequences as keywords; for example $Id: Basic_Web_Application_Strategies.xml,v 1.5 1999/12/15 15:49:55 tobias Exp $ and substitutes them on commit with the expanded counterpart, which may create a mess with binary data.
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP MySQL Web Hosting services
188 Chapter 5 Basic Web Application Strategies Listing 5.2 Continued { $body .= fgets($fp, 64); } fclose($fp); // Mail the message to all specified recipients for($i=2; $i chmod +x script.php tobias@dev:/home/tobias/ > ./script.php On Windows, you can associate the file extension .php with the interpreter to achieve the same result. The script shows two important concepts often needed in command-line scripts: accessing arguments passed to the script and reading from standard input. PHP automatically sets up an array named $argv that contains the script s filename as first entry and as subsequent entries all arguments to the script.This is actually the same behavior as in C. Just as in C, there s another variable, $argc, containing the number of arguments. Of course, you can also use count($argv). For example, if you call a PHP script with php script.php3 foo, $argv[1] would contain the first argument, foo. The other noteworthy part of the code is reading from standard input. PHP 4.0 allows you to use so-called PHP streams with fopen().To accept user input on the command line or read in data passed from another program, php://stdin is used.The script uses it to read the log message provided by CVS; you could also prompt for user input with a similar function. Listing 5.3 shows it in action.
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Inexpensive Web Hosting services
186 Chapter 5 Basic Web Application Strategies Automated Notifications At this point,Jane will have to inform her colleague again of the merge.As we ve said, CVS cannot replace effective communication within the team. But it can help. The CVS server can be set up to perform a certain action on each commit. Sending email to a mailing list server could be such an action. By setting up a mailing list or alias group for each project, it s easy to broadcast all commit messages, including logs, to all project members. To accomplish this, one of the CVS administrative files, namely the loginfo file, needs to be edited.This file controls how commit information is handled you can send it by mail, but also log it to a file, store it in a database, etc. You can alter the administrative files directly in the system s CVSROOT directory.The recommended way to change them is via CVS, though this way you ll have all the regular features and the safety net of CVS.To start a session,check out the CVSROOT directory: jane@dev:/home/jane/ > cvs checkout CVSROOT Then edit the loginfo file.The first part of a line is a regular expression that s tested against the directory or file being committed. If a match is found, the remainder of the line is the program to be invoked.The program should expect the commit information on standard input.You may also have one line in the file starting with DEFAULT instead of a regular expression:This directive will be considered if no directory is matched.Another special directive is ALL,which will be invoked in all cases.Two examples for such lines: ^phpBook$ /cvs/loginfo_process_phpBook.sh ALL /cvs/loginfo_process_all.sh The first shell script, loginfo_process_phpBook.sh, is only invoked for the phpBook repository.The second script, loginfo_process_all.sh, will be invoked on every commit, regardless of the repository, because it s marked with the ALL keyword. As part of the program to be invoked, a set of special variables can be used to give extended information about the commit.These variables are identified by a preceding percent sign (%), similar to the format declaration in printf(). If more than one variable is used, they must be grouped inside curly braces.The available variables are shown in the following table. Variable Meaning s Filename V Version number before the commit v Version number after the commit As soon as you use one of these variables, another string containing the name of the CVS module is automatically added before the variable.
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Inexpensive Web Hosting services
CVS: Concurrent Versions System 187 To send mail to John and Jane, the following line could be used: ^f-api /bin/mail -s CVS update: %s -c john jane But what if the commit message should also be logged to a file? CVS matches only the first entry for a directory, so specifying another line for ^f-api wouldn t help. Without doubt, a shell script could handle it, but what if the routine should be extended to log the message to a database later? Listing 5.2 shows how to solve this problem with PHP. In the loginfo file, it s called like this: phpBook /usr/local/cvsroot/CVSROOT/Commit_Info.php3 . /usr/local/cvsroot/CVSROOT/logs/default.log CVS update %s till tobias This line tells CVS to invoke the script Commit_Info.php3 when a commit happens for the phpBook repository.The script gets invoked with at least three arguments (four in our example): n A filename where a log summary for this commit should be stored. n The subject line of the email message. n The recipient(s). One recipient email address is required; simply provide a list of addresses as shown in the preceding example if you want to send the message to more recipients. Listing 5.2 Mail and log a commit info message. // Check for correct number of arguments if(count($argv) < 4) { print( Usage: Commit_Info.php3 logfile subject to-address [to-address ...]n ); print( n ); print( A script to log and mail CVS commit messages.n ); print( From Web Application Development with PHPn ); print( Tobias Ratschiller and Till Gerken. .Copyright (c) 2000, New Riders Publishingn ); exit; } // First argument is the logfilename $logfile = $argv[1]; // Second argument is the mail s subject $subject = $argv[2]; // Initialize the body variable $body = ; // Get the commit message passed via stdin $fp = fopen( php://stdin , r ) or die( Fatal Error: could not read from stdin. ); while(!feof($fp)) continues
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Inexpensive Web Hosting services
CVS: Concurrent Versions System 183 diff -r1.1 -r1.5 3c3 < define( FT_ZIP_ARCHIVE , ZIP_ARC ); // GZip Archive > define( FT_ZIP , ZIP_ARC ); // GZip Archive (not PkZip compatible!) While CVS is usually very good at merging different revisions of a file, under certain circumstances it s easier to avoid merges. For example, if the diff shows extensive changes on the remote server, and Jane has made only a few changes in her version, she may want to abandon her version in favor of the revision in the CVS repository. To do so, she d simply delete the local file and do a new checkout from the server. CVS Timesavers: GUIs and CVSweb If you know the basic CVS commands described in the preceding section, you hold the full power of a command-line tool in your hands:You can use CVS in remote shells, automate processes with shell scripts, and show your colleagues that you really know the ins and outs of your job. For everyday work, however, we prefer a GUI. For Windows workstations,WinCVS,available from www.wincvs.org, is a nice utility to help in getting the job done (see Figure 5.3).This software doesn t hide the complexity of CVS by any means, but it gives you convenient access to the most- often-needed features through a graphical front end. Figure 5.3 WinCVS.
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP MySQL Web Hosting services