Secure PHP Building 50 Practical Applications Development phần 2 doc

92 102 0
Secure PHP Building 50 Practical Applications Development phần 2 doc

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Developing Intranet Solutions CHAPTER 4 Architecture of an Intranet Application CHAPTER 5 Central Authentication System CHAPTER 6 Central User Management System CHAPTER 7 Intranet System CHAPTER 8 Intranet Simple Document Publisher CHAPTER 9 Intranet Contact Manager CHAPTER 10 Intranet Calendar Manager CHAPTER 11 Internet Resource Manager CHAPTER 12 Online Help System Part II 06 549669 PP02.qxd 4/4/03 9:24 AM Page 63 06 549669 PP02.qxd 4/4/03 9:24 AM Page 64 Chapter 4 Architecture of an Intranet Application INTRANET APPLICATIONS ARE PRIMARILY focused on automating an organization’s daily business processes. A modern company has many intranet applications that are available to its employees to help them be more productive and efficient. For example, a group calendar system or task-tracking system can save a great deal of time and resources for most companies with more than five employees. This chap- ter focuses on the underlying architecture of intranet applications and discusses an open-source framework that enables you to develop intranet PHP applications in a rapid manner. Understanding Intranet Requirements To develop intranet applications, you need to understand how a typical intranet is deployed. A company with two employees can have an intranet, but the average intranet application is deployed in an organization with tens to hundreds of users. Figure 4-1 shows how an intranet “connects” employees in multiple departments of a company that uses an intranet application server to manage its daily internal business functions. A company generally uses its intranet server to automate interdepartment com- munication activities such as a shared calendar, shared contact database, document management, project/task tracking, and so forth. Before you develop the framework that will enable you to create intranet appli- cations in PHP, you need to understand the intranet user requirements. Figure 4-2 shows how a single department within an organization appears from an intranet- requirements point of view. Users in organizations work in teams. A team usually has a team leader and a project assignment. The projects are managed by the department head. This type of hierarchical user base is very common in modern organizations. 65 07 549669 ch04.qxd 4/4/03 9:24 AM Page 65 Figure 4-1: A typical intranet-enabled company. Figure 4-2: User requirements for a typical intranet-enabled company. Each intranet application you develop must be able to authenticate and autho- rize different types of users. For example, an employee vacation management application has to incorporate the hierarchical chain of command that enables employee vacation requests to be reviewed and approved first by team leaders and then by the department head. So far, our intranet application framework has the following requirements: ◆ Central authentication: Users need to be authenticated to access intranet applications. There are likely to be many intranet applications within an organization and therefore user authentication should be done such that a user logs in only once to access any application. A session should be Any Department Department Head Team Employee Project 1 Team Leader Team Employee Project (n) Team Leader Marketing PC PC PC Engineering PC PCPC PC PC Sales MIS PC PCPC PC PC Intranet Server Firewall Database Server Administration PC PCPC PC PC 66 Part II: Developing Intranet Solutions 07 549669 ch04.qxd 4/4/03 9:24 AM Page 66 created that allows all applications to identify an authenticated user. When a user attempts to access an intranet application without logging in first, the application should automatically redirect the user to the login application. When the user is successfully authenticated via the login application, she should be automatically forwarded back to the applica- tion she had been attempting to access. The login process should be seam- less. Similarly, a central, seamless logout facility should be provided to allow the users to log out from the intranet. ◆ Application-specific authorization: Different types of users exist in an intranet and, therefore, intranet applications must discriminate when authorizing users. Employee access to an intranet application will vary. Because each application will have different requirements for authorizing the user, the task of authorization should be left to the application itself. ◆ A shared database: Most intranet activity involves collaboration or group efforts. For example, users working in a team within a project might need to report the status of the project tasks individually, but the team leader or department head needs to access the information from the entire team to make technical or business decisions. A shared database is therefore the solution to store data. Based on these requirements, let’s go ahead and build an intranet application framework. Building an Intranet Application Framework An intranet consists of many applications. It is a good idea to create an application framework that provides a set of commonly needed objects and services to imple- ment applications. Typical intranet applications have user authentication require- ments, database access requirements, user interfaces requirements, and business logic requirements. Each application’s business logic, which is the work done by the application, is unique and must be implemented in the application code itself. However, each application can benefit from using a standard application frame- work consisting of objects that standardize authentication, database access, user interface, etc. The framework I will build here will do just that. Figure 4-3 shows the high-level design diagram for an intranet application that will use our application framework. Now let’s discuss the components of this architecture. Chapter 4: Architecture of an Intranet Application 67 07 549669 ch04.qxd 4/4/03 9:24 AM Page 67 Figure 4-3: High-level architecture diagram of an intranet application using our framework. Using an HTML template-based presentation layer All input and output to and from the application is handled via a template-driven HTML presentation layer. When the application needs input from the user, it pre- sents an HTML page generated from an appropriate HTML template. Similarly, when the application needs to display output, it generates an HTML page by replac- ing special application-specific tags within the template. This ensures that cosmetic changes to the input or output interfaces can be done without requiring help from the application developer. For example, an application that uses the template-based presentation layer can have its interface modified by an HTML writer or graphics artist. Using PHP Application Framework components The components in the PHP Application Framework (PHPAF) layer implement the base application by providing the following services: ◆ Database abstraction support: See the “Relational database” section later in this chapter for details. ◆ Centralized authentication support: All applications defer the login and logout to the central authentication facility, as discussed earlier in this chapter. Relational Database Business Logic Your PHP Application PHP Application Framework Components HTML Template-based Presentation Layer INPUT OUTPUT 68 Part II: Developing Intranet Solutions 07 549669 ch04.qxd 4/4/03 9:24 AM Page 68 ◆ Override authorization support: Each application using the intranet application defines its own authorization method. ◆ Debugging support: An application needs to be debugged many times during the development process. Because debugging is a core part of the development process, the framework includes a built-in debugger. The current implementation is very simple yet useful. ◆ Internationalized error and status message handling support: Each application using the framework must use a central error message and status message repository. Both error and status messages can be internationalized. Business logic Each application has its own business-logic requirements. The business-logic objects will be given database connectivity from the application framework. This ensures that database abstraction is maintained. Relational database The relational database access is abstracted from the application using an abstrac- tion layer, which is part of the application framework. This ensures that application database requirements can change without drastically affecting the application. For example, an application can be developed with this framework such that it works with the widely used, high-performance MySQL database and then deployed in an environment where the database is Oracle. Of course, the developers have to be careful not to use any vendor-specific features. Figure 4-4 shows a block diagram of an application that uses the previously mentioned application framework. Figure 4-4: A block diagram of an application using the PHP Application Framework. Application Specific Error and Status Messages (Supports Internationalization) Database Independent Abstraction Authentication (Valid User Credentials) Authorization (Application Specific Authorization Requirements) Application Run() (Application Specific Driver Code) Business Logic Objects (Application Specific Code) Chapter 4: Architecture of an Intranet Application 69 07 549669 ch04.qxd 4/4/03 9:24 AM Page 69 The application checks for valid user credentials in the authentication phase, which is already supplied by the framework’s login application for valid users. The authorization step involves application-specific privilege management. Not all valid (authenticated) users are likely to have the same privilege based on the type of application. For example, an Employee Information System (EIS) applica- tion in an engineering firm can assign different privileges to executive manage- ment, department heads, team leaders, and engineers. This is why the authorization code is specific to the instance of the application and should be written by the application developer and should not be provided by the framework. When an application has gone through the authentication and authorization phases, it will run the application. This code will involve invoking application spe- cific business objects and database interaction. The application will have database access via the database-independent abstrac- tion and also will produce status messages and errors using the facilities provided by the framework. Figure 4-5 shows a real-world application framework that we will create in this chapter. Figure 4-5: A real-world PHP Application Framework. The core of this framework is the class.PHPApplication.php. This class provides an abstract PHP application that you can extend to incorporate facilities provided by the error handler (class.ErrorHandler.php), the debugger (class.Debugger.php), and the database abstraction (class.DBI.php). DB.php (from PEAR) class.PHPApplication.php class.Debugger.phpclass.ErrorHandler.php class.DBI.php Your PHP Application Business Logic 70 Part II: Developing Intranet Solutions 07 549669 ch04.qxd 4/4/03 9:24 AM Page 70 This framework is provided with the CD-ROM. You don’t need to create it from scratch.Also note that the database abstraction uses DB.php from the PEAR. Now let’s create the classes needed to implement this application framework. Creating a Database Abstraction Class Accessing a database using its own API is common in the PHP world. For example, most PHP developers use PHP with MySQL and, therefore, they write code that is specific to the MySQL API found in PHP. There is nothing wrong with this approach if you know that your PHP applica- tions will be used only for the MySQL database server. However, if there is a chance that your applications will be used with other databases such as Oracle, Postgres, and so forth, you need to avoid MySQL-specific API. A developer who has abstracted the database API in a level above the vendor-specific API can enjoy the speed of porting the application to different relational databases. Here, we will cre- ate a class called class.DBI.php that will implement a database abstraction layer for our application framework. Listing 4-1 shows class.DBI.php, which implements the database abstraction using PEAR DB. See http://pear.php.net/manual/en/core.db.php for details on PEAR DB, a unified API for accessing SQL-databases. Listing 4-1: class.DBI.php <?php /* Database abstraction class Purpose: this class provides database abstraction using the PEAR DB package. Continued Chapter 4: Architecture of an Intranet Application 71 07 549669 ch04.qxd 4/4/03 9:24 AM Page 71 Listing 4-1 (Continued) */ define(‘DBI_LOADED’, TRUE); class DBI { var $VERSION = “1.0.0”; function DBI($DB_URL) { $this->db_url = $DB_URL; $this->connect(); if ($this->connected == TRUE) { // set default mode for all resultset $this->dbh->setFetchMode(DB_FETCHMODE_OBJECT); } } function connect() { // connect to the database $status = $this->dbh = DB::connect($this->db_url); if (DB::isError($status)) { $this->connected = FALSE; $this->error = $status->getMessage(); } else { $this->connected = TRUE; } return $this->connected; } function isConnected() 72 Part II: Developing Intranet Solutions 07 549669 ch04.qxd 4/4/03 9:24 AM Page 72 [...]... uses class.DBI .php, class.ErrorHandler .php, and class.Debugger .php to create an abstract PHP application class Listing 4-4: class.PHPApplication .php < ?php /* * * PHPApplication class * * @author * @access public * * Version 1.0.1 */ if (defined(“DEBUGGER_LOADED”) && ! empty($DEBUGGER_CLASS)) { include_once $DEBUGGER_CLASS; } //require_once ‘lib.session_handler .php ; class PHPApplication... call: < ?php // Turn on all error reporting error_reporting(E_ALL); // If you have installed PEAR packages in a different // directory than %DocumentRoot%/pear change the // setting below $PEAR_DIR = $_SERVER[‘DOCUMENT_ROOT’] ‘/pear’ ; // If you have installed PHPLIB in a different // directory than %DocumentRoot%/phplib, change // the setting below $PHPLIB_DIR = $_SERVER[‘DOCUMENT_ROOT’] ‘/phplib’;... different // directory than %DocumentRoot%/pear change the // setting below $PEAR_DIR = $_SERVER[‘DOCUMENT_ROOT’] ‘/pear’ ; // If you have installed PHPLIB in a different // directory than %DocumentRoot%/phplib, change // the setting below $PHPLIB_DIR = $_SERVER[‘DOCUMENT_ROOT’] ‘/phplib’; // If you have installed framework directory in // a different directory than // %DocumentRoot%/framework, change... called test_debugger2 .php can use this debugging facility: < ?php // Turn on all error reporting error_reporting(E_ALL); // If you have installed framewirk directory in // a different directory than // %DocumentRoot%/framework, change the setting below $APP_FRAMEWORK_DIR=$_SERVER[‘DOCUMENT_ROOT’] ‘/framework’; // Insert the path in the PHP include_path so that PHP // looks for our PEAR, PHPLIB and application... a different // directory than %DocumentRoot%/phplib, change // the setting below $PHPLIB_DIR = $_SERVER[‘DOCUMENT_ROOT’] ‘/phplib’; // If you have installed framework directory in // a different directory than // %DocumentRoot%/framework, change the setting below $APP_FRAMEWORK_DIR=$_SERVER[‘DOCUMENT_ROOT’] ‘/framework’; // Create a path consisting of the PEAR, // PHPLIB and our application framework... $PHPLIB_DIR ‘:’ $APP_FRAMEWORK_DIR; // Insert the path in the PHP include_path so that PHP // looks for our PEAR, PHPLIB and application framework // classes in these directories ini_set( ‘include_path’, ‘:’ $PATH ‘:’ ini_get(‘include_path’)); // Now load the DB .php class from PEAR require_once ‘DB .php ; // Now load our DBI class from application framework // directory require_once(‘class.DBI .php );... // %DocumentRoot%/framework, change the setting below $APP_FRAMEWORK_DIR=$_SERVER[‘DOCUMENT_ROOT’] ‘/framework’; // Create a path consisting of the PEAR, // PHPLIB and our application framework // path ($APP_FRAMEWORK_DIR) $PATH = $PEAR_DIR ‘:’ $PHPLIB_DIR ‘:’ $APP_FRAMEWORK_DIR; 79 07 549669 ch04.qxd 80 4/4/03 9 :24 AM Page 80 Part II: Developing Intranet Solutions // Insert the path in the PHP. .. $ERRORS 07 549669 ch04.qxd 4/4/03 9 :24 AM Page 85 Chapter 4: Architecture of an Intranet Application Creating a Built-In Debugger Class When developing applications, each developer uses at least some form of debugging Although PHP- supported Integrated Development Environments (IDEs) are becoming available, they’re still not the primary development tools for most PHP developers, who are still using... %DocumentRoot%/framework, change the setting below $APP_FRAMEWORK_DIR=$_SERVER[‘DOCUMENT_ROOT’] ‘/framework’; // Create a path consisting of the PEAR, // PHPLIB and our application framework // path ($APP_FRAMEWORK_DIR) $PATH = $PEAR_DIR ‘:’ $PHPLIB_DIR ‘:’ $APP_FRAMEWORK_DIR; // Insert the path in the PHP include_path so that PHP // looks for our PEAR, PHPLIB and application framework // classes in these directories... called test_dbi .php that creates a DBI object < ?php // Turn on all error reporting error_reporting(E_ALL); // If you have installed PEAR packages in a different // directory than %DocumentRoot%/pear change the 07 549669 ch04.qxd 4/4/03 9 :24 AM Page 75 Chapter 4: Architecture of an Intranet Application // setting below $PEAR_DIR = $_SERVER[‘DOCUMENT_ROOT’] ‘/pear’ ; // If you have installed PHPLIB in a . (class.ErrorHandler .php) , the debugger (class.Debugger .php) , and the database abstraction (class.DBI .php) . DB .php (from PEAR) class.PHPApplication .php class.Debugger.phpclass.ErrorHandler .php class.DBI .php Your. $_SERVER[‘DOCUMENT_ROOT’] . ‘/pear’ ; // If you have installed PHPLIB in a different // directory than %DocumentRoot%/phplib, change // the setting below. $PHPLIB_DIR = $_SERVER[‘DOCUMENT_ROOT’] . ‘/phplib’; //. $_SERVER[‘DOCUMENT_ROOT’] . ‘/pear’ ; // If you have installed PHPLIB in a different // directory than %DocumentRoot%/phplib, change // the setting below. $PHPLIB_DIR = $_SERVER[‘DOCUMENT_ROOT’] . ‘/phplib’; //

Ngày đăng: 13/08/2014, 12:21

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan