cyan yelloW maGenTa Black panTone 123 c Books for professionals By professionals® The eXperT’s Voice® in WeB DeVelopmenT Companion eBook Available Practical Web 2.0 Applications with PHP Practical Dear Reader, Many programming books on the market today focus specifically on a particu- W lar methodology or software package, and although you will gain a solid under- Practical standing of the subject matter from these books, you won’t always know how to apply what you’ve learned in a real-world situation. This book is designed e to show you how to bring together many different ideas and features by start- ing with a clean slate and gradually building the code base so it evolves into a b complete web application. Web 2.0 The premise of the application we build in this book is that it is a “Web 2.0” application. What this means is that (among other things) our application gen- 2 erates accessible and standards-compliant code while making heavy of use of Ajax. We achieve this by using the Smarty™ Template Engine and Cascading . Style Sheets, as well as the Prototype JavaScript library. Additionally, we create PHP 0 a fun and intuitive interface by applying simple visual effects on various pages using the Script.aculo.us JavaScript library. To help with the development of the extensive PHP code in this book, we A Applications with use the Zend Framework. This is an open source PHP 5 library that contains many different components that you can easily use in any of your day-to-day p development. We use many of the Zend Framework components in this book, such p as database abstraction (with a focus on MySQL® and PostgreSQL), logging, l authentication, and search. i c The “Web 2.0” application that we build in this book is a collaborative a blogging tool. It will allow users to register and create a personal blog. When t creating blog posts, users will be able upload images, apply tags, and assign i locations (using Google Maps). We will also look at how to use microformats o when displaying user blog posts. n Quentin Zervaas s Develop a complete PHP web application from start to finish w Companion eBook RelAted titleS i t h P See last page for details on $10 eBook version H P Quentin Zervaas SOURCE CODE ONLINE ISBN-13: 978-1-59059-906-8 www.apress.com ISBN-10: 1-59059-906-3 54499 Zervaas US $44.99 Shelve in PHP User level: 9 781590 599068 Intermediate–Advanced www.it-ebooks.info this print for content only—size & color not accurate spine = 1.1163" 592 page count www.it-ebooks.info 9063CH00CMP3 11/19/07 8:39 PM Page i Practical Web 2.0 Applications with PHP Quentin Zervaas www.it-ebooks.info 9063CH00CMP3 11/19/07 8:39 PM Page ii Practical Web 2.0 Applications with PHP Copyright © 2008 by Quentin Zervaas All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-59059-906-8 ISBN-10 (pbk): 1-59059-906-3 ISBN-13 (electronic): 978-1-4302-0474-9 ISBN-10 (electronic): 1-4302-0474-5 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Lead Editor: Ben Renow-Clarke Technical Reviewer: Jeff Sambells Editorial Board: Steve Anglin, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan Gennick, Jason Gilmore, Kevin Goff, Jonathan Hassell, Matthew Moodie, Joseph Ottinger, Jeffrey Pepper, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Project Manager: Richard Dal Porto Copy Editors: Andy Carroll, Kim Wimpsett Assistant Production Director: Kari Brooks-Copony Production Editor: Liz Berry Compositor: Diana Van Winkle Proofreader: Lisa Hamilton Indexer: Broccoli Information Management Artist: Diana Van Winkle Cover Designer: Kurt Krames Manufacturing Director: Tom Debolski Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail [email protected], or visit http://www.springeronline.com. For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600, Berkeley, CA 94705. Phone 510-549-5930, fax 510-549-5939, e-mail [email protected], or visit http://www.apress.com. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at http://www.apress.com. www.it-ebooks.info 9063CH00CMP3 11/19/07 8:39 PM Page iii Contents at a Glance About the Author. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvi Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii ■ CHAPTER 1 Application Planning and Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 ■ CHAPTER 2 Setting Up the Application Framework . . . . . . . . . . . . . . . . . . . . . . . . . . 9 ■ CHAPTER 3 User Authentication,Authorization,and Management . . . . . . . . . . . . . 45 ■ CHAPTER 4 User Registration,Login,and Logout . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 ■ CHAPTER 5 Introduction to Prototype and Scriptaculous . . . . . . . . . . . . . . . . . . 123 ■ CHAPTER 6 Styling the Web Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 ■ CHAPTER 7 Building the Blogging System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 ■ CHAPTER 8 Extending the Blog Manager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 ■ CHAPTER 9 Personalized User Areas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297 ■ CHAPTER 10 Implementing Web 2.0 Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335 ■ CHAPTER 11 A Dynamic Image Gallery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371 ■ CHAPTER 12 Implementing Site Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427 ■ CHAPTER 13 Integrating Google Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469 ■ CHAPTER 14 Deployment and Maintenance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519 ■ INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547 iii www.it-ebooks.info 9063CH00CMP3 11/19/07 8:39 PM Page iv www.it-ebooks.info 9063CH00CMP3 11/19/07 8:39 PM Page v Contents About the Author. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvi Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii ■ CHAPTER 1 Application Planning and Design . . . . . . . . . . . . . . . . . . . . . . . . . . 1 What Is Web 2.0? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Database Connectivity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Web Site Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Web Site Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Main Home Page and User Home Page . . . . . . . . . . . . . . . . . . . . . . . . 3 User Registration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Account Login and Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 User Blogs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Web Site Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Application Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Other Aspects of Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Search-Engine Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 PHPDoc-Style Commenting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Application Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Maintainability and Extensibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Version Control and Unit Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 ■ CHAPTER 2 Setting Up the Application Framework . . . . . . . . . . . . . . . . . . . . 9 Web Server Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Operating System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Installing the Apache HTTP Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Installing MySQL 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Installing PHP 5.2.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 v www.it-ebooks.info 9063CH00CMP3 11/19/07 8:39 PM Page vi vi ■ CONTENTS Application Filesystem Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Web Root Directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Data Storage Directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 PHP Classes Directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Templates Directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Full Directory Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Installing the Zend Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Configuring the Web Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Creating a Virtual Host in Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Creating a Virtual Host in Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Restarting Your Web Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Setting Up the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Using the Model-View-Controller Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Separating Application Logic from Presentation Logic . . . . . . . . . . . 19 Directing All Requests to index.php . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Introduction to the Zend_Controller Class . . . . . . . . . . . . . . . . . . . . . 22 How Requests Work with Zend_Controller . . . . . . . . . . . . . . . . . . . . . 23 Creating the IndexController . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Defining Application Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Connecting to the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Testing the Database Connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 The Smarty Template Engine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Why Not Use a Different Template Engine? . . . . . . . . . . . . . . . . . . . . 33 Downloading and Installing Smarty . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Automatic View Rendering with Zend_Controller . . . . . . . . . . . . . . . 36 Integrating Smarty with the Web Site Controllers . . . . . . . . . . . . . . . 39 Adding Logging Capabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Writing to the Log File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 ■ CHAPTER 3 User Authentication,Authorization,and Management. . . . . . 45 Creating the User Database Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Timestamps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 User Profiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Introduction to Zend_Auth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Instantiating Zend_Auth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Authenticating with Zend_Auth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Introduction to Zend_Acl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 A Zend_Acl Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 www.it-ebooks.info 9063CH00CMP3 11/19/07 8:39 PM Page vii ■ CONTENTS vii Combining Zend_Auth,Zend_Acl,and Zend_Controller_Front . . . . . . . . 57 Managing User Records with DatabaseObject . . . . . . . . . . . . . . . . . . . . . . 61 The DatabaseObject_User Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Using DatabaseObject_User . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Managing User Profiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 Using Profile_User . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Integrating Profile_User with DatabaseObject_User . . . . . . . . . . . . 69 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 ■ CHAPTER 4 User Registration,Login,and Logout . . . . . . . . . . . . . . . . . . . . . 73 Adding User Registration to the Application . . . . . . . . . . . . . . . . . . . . . . . . 73 Creating the Form Processor for User Registration . . . . . . . . . . . . . 74 Displaying the Registration Form and Processing Registrations. . . 81 Adding CAPTCHA to the User Registration Form . . . . . . . . . . . . . . . . 88 Adding E-mail Functionality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Implementing Account Login and Logout . . . . . . . . . . . . . . . . . . . . . . . . . . 100 Creating the Login Template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 Adding the Account Controller Login Action . . . . . . . . . . . . . . . . . . . 102 Logging Successful and Failed Login Attempts . . . . . . . . . . . . . . . . 105 Logging Users Out of Their Accounts . . . . . . . . . . . . . . . . . . . . . . . . 107 Dealing with Forgotten Passwords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 Resetting a User’s Password . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 Functions for Resetting Passwords . . . . . . . . . . . . . . . . . . . . . . . . . . 112 Implementing Account Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 Creating the Account Home Page . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 Updating the Web Site Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 Allowing Users to Update Their Details . . . . . . . . . . . . . . . . . . . . . . . 120 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 ■ CHAPTER 5 Introduction to Prototype and Scriptaculous . . . . . . . . . . . . 123 Downloading and Installing Prototype . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 Prototype Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 Selecting Objects in the Document Object Model . . . . . . . . . . . . . . . . . . . 124 The $() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 The getElementsByClassName() Function . . . . . . . . . . . . . . . . . . . . 125 The $$() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 The getElementsBySelector() Function . . . . . . . . . . . . . . . . . . . . . . . 129 Prototype’s Hash Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 vii www.it-ebooks.info 9063CH00CMP3 11/19/07 8:39 PM Page viii viii ■ CONTENTS Other Element Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 Showing and Hiding Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 Retrieving Dimensions of Elements . . . . . . . . . . . . . . . . . . . . . . 131 Managing Classes of Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 Manipulating Strings with Prototype . . . . . . . . . . . . . . . . . . . . . . . . . 133 Ajax Operations in Prototype . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 Ajax Request Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 Ajax Callback Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 JavaScript Object Notation (JSON) . . . . . . . . . . . . . . . . . . . . . . . . . . 138 An Ajax.Request Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 Event Handling in Prototype . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 Observing an Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 Finding Out Which Element an Event Occurred On . . . . . . . . . . . . . 146 Canceling an Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 Creating JavaScript Classes in Prototype . . . . . . . . . . . . . . . . . . . . . . . . . 147 Creating a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 Binding Function Calls to Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 From Prototype to Scriptaculous . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 Prebuilt Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 Drag and Drop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 Visual Effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 DOM Element Builder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 JavaScript Unit Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 Downloading and Installing Scriptaculous . . . . . . . . . . . . . . . . . . . . . . . . . 154 Combining Prototype,Scriptaculous,Ajax,and PHP in a Useful Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 Creating the Main HTML Page:index.php . . . . . . . . . . . . . . . . . . . . 156 Styling the Application:styles.css . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 Creating and Populating the Database:schema.sql . . . . . . . . . . . . 158 Managing the List Items on the Server Side:items.php . . . . . . . . 159 Processing Ajax Requests on the Server Side:processor.php . . . 161 Creating the Client-Side Application Logic:scripts.js . . . . . . . . . . . 163 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 ■ CHAPTER 6 Styling the Web Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 Adding Page Titles and Breadcrumbs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 The Breadcrumbs Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 Generating URLs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 Setting the Title and Trail for Each Controller Action . . . . . . . . . . . . 178 Creating a Smarty Plug-In to Output Breadcrumbs . . . . . . . . . . . . 180 Displaying the Page Title . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 809b8b6f91d5ff50033254241f3132ed www.it-ebooks.info
Description: