Next In the next project, we will build a Web-based interface that will allow you to check and send email from the Web using IMAP... 27 Building a Web-Based Email Service MORE AND MORE O
Trang 1582 Chapter 26 Building a Content Management System
if ($story[published]) { print '[<a href="unpublish_story.php?story='.$story['id'].'">unpublish</a>] '; }
else { print '[<a href="publish_story.php?story='.$story['id'].'">publish</a>] '; print '[<a href="delete_story.php?story='.$story['id'].'">delete</a>] '; }
print '[<a href="story.php?story='.$story['id'].'">edit</a>] ';
print '</td></tr>';
} print '</table>';
?>
This script should be made available only to the people who are authorized to publish stories to the live site In our sample application, this would be the site editor, but there
is no access control on this script for simplicity It should, however, be protected in a live situation
This is very similar to stories.phpexcept that the editor is given a screen showing the stories for every writer, not just her own.The ifstatement ensures that appropriate options are presented for each story Published stories can be unpublished, and unpub-lished stories can be pubunpub-lished or deleted
These three links submit to unpublish_story.php,publish_story.php, and delete_story.php, respectively
The script publish_story.phpuses the following SQL query:
update stories set published = $now
where id = $story
This will mark a story as published and authorize it for public viewing
Similarly,unpublish_story.phpuses the following query to mark a story as unpub-lished and stop it from being displayed to the public:
update stories set published = null
where id = $story
The edit link appears regardless of whether a story is published, so the editor can always make changes.This is different to the writers’ level of access, where they can only modify
a story before it has been published
Extending the Project
There are several ways this project could be extended to make a more comprehensive content management system:
Listing 26.10 Continued
Trang 2583 Next
n You could allow groups of users to work on stories together (collaboration)
n You could implement a more flexible page layout so that editors can position text and images on the page
n An image library could be built so that frequently used pictures are not duplicated, and search keywords are assigned to images as well as story text
n You could also add spell-checking functionality to the content editor A check could be implemented using, for example, the aspell library
Next
In the next project, we will build a Web-based interface that will allow you to check and send email from the Web using IMAP
Trang 427 Building a Web-Based Email
Service
MORE AND MORE OFTEN THESE DAYS, sites want to offer Web-based email to their users.This chapter explains how to implement a Web interface to an existing mail server using the PHP IMAP library.You can use it to check your own existing mailbox through
a Web page, or perhaps extend it to support many users for mass Web-based email like Hotmail
In this project, we will build an email client,Warm Mail, that will enable users to
n Connect to their accounts on POP3 or IMAP mail servers
n Read mail
n Send mail
n Reply to mail messages
n Forward mail messages
n Delete mail from their accounts
The Problem
In order for a user to be able to read his mail, we will need to find a way to connect to his mail server.This generally won’t be the same machine as the Web server
We will need a way to interact with his mailbox, to see what messages have been received and to deal with each message individually
Two main protocols are supported by mail servers for reading user mailboxes: POP3 and IMAP If possible, we should support both of these POP3 stands for Post Office Protocol version 3, and IMAP stands for Internet Message Access Protocol
The main difference between these two is that POP3 is intended for, and usually used
by, people who connect to a network for a short time to download and delete their mail
Trang 5586 Chapter 27 Building a Web-Based Email Service
from a server IMAP is intended for online use, to interact with mail permanently kept
on the remote server IMAP has some more advanced features that we won’t use here
If you are interested in the differences between these protocols, you can consult the RFCs for them (RFC 1939 for POP version 3 and RFC 2060 for IMAP version 4 rev1) An excellent article comparing the two can be found at
http://www.imap.org/papers/imap.vs.pop.brief.html
Neither of these protocols is designed for sending mail—for that we must use the SMTP
(Simple Mail Transfer Protocol), which we have used before from PHP via the mail() func-tion.This protocol is described in RFC 821
Solution Components
PHP has excellent IMAP and POP3 support, but it is provided via the IMAP function library In order to use the code presented in this chapter, you will need to have installed the IMAP library.You can tell if you already have this installed by looking at the output
of the phpinfo()function
If not, you will need to download the extension.You can get the latest version via FTP from
ftp://ftp.cac.washington.edu/imap/c-client.tar.Z
Under UNIX, download the source and compile it for your operating system.When you have done this, copy rfc822.h,mail.h, and linkage.hto /usr/local/includeor another directory in your include path, run PHP’s configure script, adding the
with-imapdirective to any other parameters you use, and recompile PHP
Documentation exists on compiling the Windows version yourself, but it is much more complex than compiling for UNIX.To use the IMAP extension with Windows, down-load the Zip file version of PHP rather than the Install Wizard.The Zip file contains many extensions including IMAP Open your php.inifile and uncomment out the line:
extension=php_imap.dll
One interesting thing to note is that although these are called IMAP functions they also work equally well with POP3 and NNTP (Network News Transfer Protocol).We will use them for IMAP and POP3, but the Warm Mail application could be easily extended
to use NNTP and be a newsreader as well as mail client
There are a very large number of functions in this library, but in order to implement the functionality in this application, we will use only a few.We’ll explain these functions
as we use them, but be aware that there are many more See the documentation if your needs are different from ours, or if you want to add extra features to the application You can build a fairly useful mail application with only a fraction of the built-in functions.This means that you need only plow through a fraction of the documentation The IMAP functions we use in this chapter are