Chapter 2: Understanding and Avoiding Security Risks
Identifying the Sources of Risk
Minimizing User-Input Risks
Not Revealing Sensitive Information
Summary
Chapter 3: PHP Best Practices
Best Practices for Naming Variables and Functions
Best Practices for Function/Method
Best Practices for Database
Best Practices for User Interface
Best Practices for Documentation
Best Practices for Web Security
Best Practices for Source Configuration Management
Summary
Part II
Chapter 4: Architecture of an Intranet Application
Understanding Intranet Requirements
Building an Intranet Application Framework
Creating a Database Abstraction Class
Creating an Error Handler Class
Creating a Built-In Debugger Class
Creating an Abstract Application Class
Creating a Sample Application
Summary
Chapter 5: Central Authentication System
How the System Works
Creating an Authentication Class
Creating the Central Login Application
Creating the Central Logout Application
Creating the Central Authentication Database
Testing Central Login and Logout
Making Persistent Logins in Web Server Farms
Summary
Chapter 6: Central User Management System
Identifying the Functionality Requirements
Creating a User Class
User Interface Templates
Creating a User Administration Application
Creating a User Password Application
Creating a Forgotten-Password Recovery Application
Summary
Chapter 7: Intranet System
Identifying Functionality Requirements
Designing the Database
Designing and Implementing the Intranet Classes
Setting Up Application Configuration Files
Setting Up the Application Templates
Intranet Home Application
Installing Intranet Applications from the CD- ROM
Testing the Intranet Home Application
Summary
Chapter 8: Intranet Simple Document Publisher
Identifying the Functionality Requirements
The Prerequisites
Designing the Database
The Intranet Document Application Classes
Setting up Application Configuration Files
Setting Up the Application Templates
The Document Publisher Application
Installing Intranet Document Application
Testing Intranet Document Application
Summary
Chapter 9: Intranet Contact Manager
Functionality Requirements
Understanding Prerequisites
The Database
The Intranet Contact Manager Application Classes
The Application Configuration Files
The Application Templates
The Contact Category Manager Application
The Contact Manager Application
Installing Intranet Contract Manager
Testing Contract Manager
Summary
Chapter 10: Intranet Calendar Manager
Identifying Functionality Requirements
Understanding Prerequisites
Designing the Database
The Intranet Calendar Application Event Class
The Application Configuration Files
The Application Templates
The Calendar Manager Application
The Calendar Event Manager Application
Installing the Event Calendar on Your Intranet
Testing the Event Calendar
Summary
Chapter 11: Internet Resource Manager
Functionality Requirements
Understanding the Prerequisites
Designing the Database
Designing and Implementing the Internet Resource Manager Application Classes
Creating Application Configuration Files
Creating Application Templates
Creating a Category Manager Application
Creating a Resource Manager Application
Creating a Resource Tracking Application
Creating a Search Manager Application
Installing an IRM on Your Intranet
Testing IRM
Security Concerns
Summary
Chapter 12: Online Help System
Functionality Requirements
Understanding the Prerequisites
Designing and Implementing the Help Application Classes
Creating Application Configuration Files
Creating Application Templates
Creating the Help Indexing Application
Creating the Help Application
Installing Help Applications
Testing the Help System
Security Considerations
Summary
Part III
Chapter 13: Tell-a-Friend System
Functionality Requirements
Understanding Prerequisites
Designing the Database
Designing and Implementing the Tell- a- Friend Application Classes
Creating Application Configuration Files
Creating Application Templates
Creating the Tell-a-Friend Main Menu Manager Application
Creating a Tell-a-Friend Form Manager Application
Creating a Tell-a-Friend Message Manager Application
Creating a Tell-a-Friend Form Processor Application
Creating a Tell-a-Friend Subscriber Application
Creating a Tell-a-Friend Reporter Application
Installing a Tell-a-Friend System
Testing the Tell-a-Friend System
Security Considerations
Summary
Chapter 14: E-mail Survey System
Functionality Requirements
Architecture of the Survey System
Designing the Database
Designing and Implementing the Survey Classes
Designing and Implementing the Survey Applications
Developing Survey Execution Manager
Setting Up the Central Survey Configuration File
Setting Up the Interface Template Files
Testing the Survey System
Security Considerations
Summary
Chapter 15: E-campaign System
Features of an E-campaign System
Architecting an E-campaign System
Designing an E-campaign Database
Understanding Customer Database Requirements
Designing E-campaign Classes
Creating Common Configuration and Resource Files
Creating Interface Template Files
Creating an E-campaign User Interface Application
Creating a List Manager Application
Creating a URL Manager Application
Creating a Message Manager Application
Creating a Campaign Manager Application
Creating a Campaign Execution Application
Creating a URL Tracking and Redirection Application
Creating an Unsubscription Tracking Application
Creating a Campaign Reporting Application
Testing the E-Campaign System
Security Considerations
Summary
Part IV
Chapter 16: Command-Line PHP Utilities
Working with the Command-Line Interpreter
Building a Simple Reminder Tool
Building a Geo Location Finder Tool for IP
Building a Hard Disk Usage Monitoring Utility
Building a CPU Load Monitoring Utility
Summary
Chapter 17: Apache Virtual Host Maker
Understanding an Apache Virtual Host
Defining Configuration Tasks
Creating a Configuration Script
Developing makesite
Installing makesite on Your System
Testing makesite
Summary
Chapter 18: BIND Domain Manager
Features of makezone
Creating the Configuration File
Understanding makezone
Installing makezone
Testing makezone
Summary
Part V
Chapter 19: Web Forms Manager
Functionality Requirements
Understanding Prerequisites
Designing the Database
Designing and Implementing the Web Forms Manager Application Classes
Creating the Application Configuration Files
Creating Application Templates
Creating the Web Forms Submission Manager Application
Creating the Web Forms Reporter Application
Creating the CSV Data Exporter Application
Installing the Web Forms Manager
Testing the Web Forms Manager
Security Considerations
Summary
Chapter 20: Web Site Tools
Functionality Requirements
Understanding Prerequisites
Designing the Database
Designing and Implementing the Voting Tool Application Class
Creating the Application Configuration Files
Creating the Application Templates
Creating the Vote Application
Installing the Voting Tool
Testing the Voting Tool
Summary
Part VI
Chapter 21: Speeding Up PHP Applications
Benchmarking Your PHP Application
Buffering Your PHP Application Output
Compressing Your PHP Application Output
Caching Your PHP Applications
Summary
Chapter 22: Securing PHP Applications
Controlling Access to Your PHP Applications
Securely Uploading Files
Using Safe Database Access
Recommended php.ini Settings for a Production Environment
Limiting File System Access for PHP Scripts
Running PHP Applications in Safe Mode
Summary
Part VII
Appendix A: What's on the CD-ROM
System Requirements
What's on the CD
Troubleshooting
Appendix B: PHP Primer
Object-Oriented PHP
Appendix C: MySQL Primer
Using MySQL from the Command- Line
Using phpMyAdmin to Manage MySQL Database
Appendix D: Linux Primer
Installing and Configuring Apache 2.0
Installing and Configuring MySQL Server
Installing and Configuring PHP for Apache 2.0
Common File/Directory Commands
Index
Wiley Publishing, Inc. End-User License Agreement
Nội dung
3. Create all the necessary Web and user account configurations and copy appropriate contents based on account type “gold.” In other words, all the “gold” account features will be implemented. This may include enabling PHP, CGI, SSI, and other Web features, or the installation of custom soft- ware such as phpMyAdmin, and so on. 4. Automatically restart Apache server. 5. Automatically test the new Web site by making requests to see if the site is operational. 6. Automatically notify the account owner via e-mail that the site is opera- tional, and provide necessary instructions to access and manage the Web site. Clearly, this script reduces quite a bit of work for a Web administrator, and therefore it is well worth developing. However, before we can make such an inter- esting script, we need to define a set of standard account types for the system. Creating a Configuration Script The makesite script uses the -type=account_type command-line argument to determine numerous tasks that need to be performed in the virtual host setup process. For example, we can define the following accounts: $ACCOUNT_TYPE[‘standard’] = array( vhost_template => ‘std_vhost.conf’, master_contents =>’std_contents.conf’, mail_template => ‘std_vhost.mail’, shell => ‘/bin/true’ ); $ACCOUNT_TYPE[‘gold’] = array( vhost_template => ‘gold_vhost.conf’, master_contents =>’gold_contents.conf’, mail_template => ‘gold_vhost.mail’, shell => ‘/bin/tcsh’ ); $ACCOUNT_TYPE[‘platinum’] = array( vhost_template => ‘platinum_vhost.conf’, master_contents =>’platinum_contents.conf’, mail_template => ‘platinum_vhost.mail’, shell => ‘/bin/bash’, ); Chapter 17: Apache Virtual Host Maker 611 22 549669 ch17.qxd 4/4/03 9:27 AM Page 611 This script defines three types of Web site accounts: standard, gold, and plat- inum. These names are arbitrary, of course. You can choose whatever names you like. For example, a university administrator might choose: coursesite, deptsite, stu- dentsite, and so on, as account types. Now let’s examine one of the account config- urations. The standard account type defines that the virtual host configuration (vhost_template) template is std_vhost.conf. This is a PHP-based virtual host con- figuration generator script. The std_contents.conf script is another PHP-based con- tents configuration script. The mail template is simply a text file with special tags that are replaced before it is sent to the account owner. When creating a standard account, the makesite script does the following: 1. It uses the vhost_template (std_vhost.conf) to create the Apache configuration. 2. It uses the master_contents (std_contents.conf) to do content-specific configuration. 3. Optionally (if told to do so), it uses the mail_template (std_vhost.mail) to send an e-mail to the account owner. Note that there is no restriction governing how many types of accounts you can define as long as you also create the necessary configuration templates. Developing makesite As you have seen, the makesite script has several pieces that must be created and fit together for the script to operate. The following sections will cover the development of the various pieces. Creating the makesite.conf file The makesite script uses the makesite.conf file, as shown in Listing 17-1. The make- site configuration file is the central configuration file for the application. Listing 17-1: makesite.conf <?php // Set this to the PEAR directory $PEAR_DIR = ‘/example/intranet/htdocs/pear’ ; ini_set( ‘include_path’, ‘:’ . $PEAR_DIR . ‘:’ . ini_get(‘include_path’)); require_once “Console/Getopt.php”; define(DEBUG, FALSE); 612 Part IV: Using PHP for Sysadmin Tasks 22 549669 ch17.qxd 4/4/03 9:27 AM Page 612 $APACHE_INFO = array( ‘user’ => ‘httpd’, ‘group’ => ‘httpd’, ‘path’ => ‘/usr/local/apache’, ‘bin_dir’ => ‘bin’, ‘conf_dir’ => ‘conf’, ‘conf_flie’ => ‘httpd.conf’, ‘server_bin’ => ‘httpd’, ‘config_chk_opt’ => ‘-t -f’, ‘restart_opt’ => ‘-k restart’, ‘vhost_conf_dir’ => ‘conf/vhosts’ ); $SYSTEM_INFO = array( ‘passwd_file’ => ‘/etc/passwd’, ‘home_dir’ => ‘/home’, ‘min_passwd_length’ => 5, ‘group_file’ => ‘/etc/group’, ‘server_ip’ => ‘192.168.0.11’, ‘useradd_bin’ => ‘/usr/sbin/useradd’, ‘www_partition’ => ‘/www’, ‘permission’ => ‘0755’, ‘symlink_bin’ => ‘/bin/ln’, ‘symlink_opt’ => ‘-s’, ‘cp_bin’ => ‘/bin/cp’, ‘cp_opt’ => ‘-r’, ‘mkdir_bin’ => ‘/bin/mkdir’, ‘chmod_bin’ => ‘/bin/chmod’, ‘chown_bin’ => ‘/bin/chown’ ); define(DEFAULT_ACCOUNT_TYPE , ‘standard’); define(DEFAULT_SYMLINK_USER_TO_WEBSITE , TRUE); //$TEMPLATE_DIR = ‘/www/vhosts’; $TEMPLATE_DIR = ‘vhosts’; $ACCOUNT_TYPE[‘standard’] = array( vhost_template => ‘std_vhost.conf’, master_contents =>’std_contents.conf’, mail_template => ‘std_vhost.mail’, shell => ‘/bin/true’ ); Continued Chapter 17: Apache Virtual Host Maker 613 22 549669 ch17.qxd 4/4/03 9:27 AM Page 613 Listing 17-1 (Continued) $ACCOUNT_TYPE[‘gold’] = array( vhost_template => ‘gold_vhost.conf’, master_contents =>’gold_contents.conf’, mail_template => ‘gold_vhost.mail’, shell => ‘/bin/tcsh’ ); $ACCOUNT_TYPE[‘platinum’] = array( vhost_template => ‘platinum_vhost.conf’, master_contents =>’platinum_contents.conf’, mail_template => ‘platinum_vhost.mail’, shell => ‘/bin/bash’, ); ?> This configuration file defines a set of the $ACCOUNT_TYPE associative array as discussed previously and also defines the following: ◆ $APACHE_INFO is an associative array that holds information about vari- ous Apache-specific configurations. For example, the sample configura- tion indicates that the Apache server is run as the ‘httpd’ user, as $APACHE_INFO[user] is set to ‘httpd’. Similarly, it indicates that Apache is installed in $APACHE_INFO[path], which is set to /usr/local/apache. Numerous other configuration parameters specify options to check, options for restarting the server, and locations of configuration files. ◆ $SYSTEM_INFO is an associative array that holds information specific to the system on which the Apache server is running. For example, $SYSTEM_INFO[passwd_file] points to the password file used by the system, which is /etc/passwd. Similarly, it defines $SYSTEM_INFO [server_ip] , the IP address Apache server listens on. Numerous other configuration parameters specify paths to system commands, the main path to the www directory, and the value of various options to be set by the script. ◆ The DEFAULT_ACCOUNT_TYPE constant defines the default account type. ◆ The DEFAULT_SYMLINK_USER_TO_WEBSITE constant is set to TRUE if a symbolic soft link is to be created from the user’s home directory to his or her Web server’s directory. ◆ The $TEMPLATE_DIR constant points to the subdirectory in the makesite script directory, which holds the account-specific templates. For example, the standard account templates (std_vhost.conf, std_contents.conf, and std_vhost.mail) are stored in the vhosts directory. 614 Part IV: Using PHP for Sysadmin Tasks 22 549669 ch17.qxd 4/4/03 9:27 AM Page 614 The makesite script uses the Console/Getopt.php class from PEAR;therefore, the makesite.conf script loads this script in the beginning of the configura- tion file. Make sure that the $PEAR_DIR variable in makesite.conf is set properly to point to your PEAR installation. Creating the virtual host configuration Listing 17-2 shows the standard account’s virtual host configuration, std_vhosts.conf. This is loaded by the makesite script and processed by calling the makeVirtualHost() function within the configuration file. Listing 17-2: vhosts/std_vhost.conf <?php function makeVirtualHost() { $www = $GLOBALS[SYSTEM_INFO][www_partition]; $ipAddr = $GLOBALS[SYSTEM_INFO][server_ip]; $server = $GLOBALS[SERVER_NAME]; $serverRoot = sprintf(“%s/%s”, $www, $server); $docRoot = sprintf(“%s/%s/htdocs”, $www, $server); $logDir = sprintf(“%s/%s/logs”, $www, $server); $vhostConfig = <<<STD_VHOST_CONF # # Automated virtual host configuration for $GLOBALS[SERVER_NAME] # # Account Type: standard # <VirtualHost $ipAddr> ServerName $server DocumentRoot “$docRoot” ErrorLog “$logDir/errors.log” CustomLog “$logDir/access.log” common Continued Chapter 17: Apache Virtual Host Maker 615 22 549669 ch17.qxd 4/4/03 9:27 AM Page 615 . features will be implemented. This may include enabling PHP, CGI, SSI, and other Web features, or the installation of custom soft- ware such as phpMyAdmin, and so on. 4. Automatically restart Apache. configuration (vhost_template) template is std_vhost.conf. This is a PHP- based virtual host con- figuration generator script. The std_contents.conf script is another PHP- based con- tents configuration script. The. makesite.conf < ?php // Set this to the PEAR directory $PEAR_DIR = ‘/example/intranet/htdocs/pear’ ; ini_set( ‘include_path’, ‘:’ . $PEAR_DIR . ‘:’ . ini_get(‘include_path’)); require_once “Console/Getopt .php ; define(DEBUG,