| |||||||||||||||||||||||||||||||
|
![]() HotMail the hands or That can PHP In this clause{article} I would like To show, that creation of efficient and useful dynamic web-sites Not so such challenge as can seem at reading the texts describing The interface cgi. Actually similar job (at least, at use linux) Most of all reminds game the incredible machines where it is necessary kremnem to cut a spark, That light has fallen to the solar battery from which works supermodern Computer. Here too all, as a rule, is going to from components which appeared Near at hand. As an example we shall consider creation of post service on the web-server - webmail- On the basis of language php3 with maximal use of ready components. For the staff Creation of new users, their registration in a database, etc. will stay. Ours Mail besides will not process the letter with investments and in codings, excellent{different} From koi8-r to support notepads, etc. - all this only would increase the sizes Scripts, not having given anything essentially new. Components In the beginning about " ready components ". I have already mentioned that all described It is below designed for use linux. However existing distribution kits of this OS So differ on a complete set, that hardly it will be possible to construct an example, which Would suit any version. Therefore I shall be limited to the version from which it is the best way It is familiar, - ksi-linux 2.0 (http://www.ksi-linux.com). If at you other variant linux, For example redhat 5.2 or debian 2.0 you, most likely, will need to receive Some components from internet or perekompilirovat` already available. It will be necessary for us russian apache 1.3.x rus/pl 27.4 web-server, php 3.0.6 with support The report imap4r1 and a mail server imap4r1. Thanking russian apache we shall be, on To extreme measure partly, are relieved of problems with codings: irrespective of the coding, Used by the client, the data on the server will be always sent in koi8-r (it is adjustment russian apache by default). Imap4r1 server will allow us to manipulate post Box, not describing in the obvious form of access right (contacting this server, you Specify the name and the password, and he solves all problems with the rights). And at last, php 3.0.6 With support of the report imap4r1 will enable to work with imap4r1 server, not Realizing independently corresponding report, which rather and rather netrivialen. Here many will ask a question: and why not perl? The matter is that, though language perl- Standard " the working horsy " for creation of web-sites, at him is certain{determined} Lacks. The interpreter perl is essentially more complex than the interpreter php and demands for Job there are more than resources of a computer. Besides it not the interpreter in exact sense Words: he compiles the program in the specialized pseudo-code, which then It is immediately executed. If in the program there are cycles, such approach can give Essential prize, but if no (as it often happens in simple scripts for The web-server - for example, in all our realization webmail there will be only one cycle), Overhead charge do not pay off. Application mod_perl - the additional module apache- Allows to avoid repeated recompilation of scripts, but thus to memory of the server Made great demands even more. Shortly about php Now some words that such php. It is interpretive Language for creation of active web-pages. The program on php, similarly to the text on javascript, vbscript or asp, it is inserted into a html-file. The beginning and the end of the program are marked Special brackets <? php and?>. The text outside of these brackets php does not interpret: he It is passed a web-browser " as is ". In listing 1 realization on php "eternal" is resulted Example - the counter. As you can see, it is completely usual html-file, however in that place, Where there should be a quantity{an amount} of visitings, there is a script on php3, which in quality Result of the job deduces number of visitings stranicy1. Syntax php is based on syntax of languages Si, java and perl and is rather in detail described In a management{manual} which is included into the complete set of delivery (it also it is possible to take on the site http://www.php.net/). Ways to force the server correctly to react to html-files with Inserts on php, generally speaking, are various for different servers, but it happens more often It is enough to give a name of a file expansion .php3. So, we would like to have an opportunity to read and send a mail with the help web-browser. Probably, it will be reasonable to make the interface similar, say, on netscape messenger: the window is divided{shared} across into two parts, in top there is a list of letters In our mail box on the server, in bottom - the current letter. But before showing To the user a html-file with the description of frames, we shall demand from him to enter the name and The password (at incorrectly entered password he will receive a file containing the message about To mistake). This function will be carried out with a file index.php3, shown in listing 2. Let's see at him closer. First of all it is necessary to pay attention what a bracket <? php, opening The php-script, costs{stands} right at the beginning of a file. It is not casual. The matter is that it is not necessary To inform a browser that the document will consist of several frames while the user Will not enter a correct name and the password. To request the password, we change " the answer of the server " with Usual " 200 ok " on " 401 auth required " 2. It is very simple: specially for similar Cases in php function header () is stipulated. But this function works only at Condition, that before its{her} call the conclusion of the document has not been started yet, and therefore, a bracket <? php should stand right at the beginning of a file... The further is obvious: we look at the name entered by the user and the password, we try To contact imap4r1 server and if all has passed successfully, simply we give out the information about Set of frames. However now in all scripts in that subdirectory where we is placed The starting file index.php3, will be passed the information on a name and the password! 3 Let's notice also, that before a name of function imap_open there is a symbol. He means, that Messages on possible{probable} mistakes at job of function should not be deduced{removed} in the text on html (the behaviour php by default) and to be saved in a special variable. It is necessary To use function header (): she will not work, if in a body The document any text, including the message on a mistake is already deduced{removed}. Other files Now we need to create files top.php3 and main.php3 on which are available Links in index.php3. We shall start with top.php3 (listing 3). In him we build the table, in which Each line corresponds{meets} to one letter and contains his serial number, a subject, a name (Address) of the sender and date of sending. The information is taken from corresponding Fields of heading of the letter. At registration of links (href) we should not forget to pass number{room} of the letter (as at To job with the usual cgi-script). The file main.php3 (4) checks listing, whether is established Variable $mail. If no, the inscription new message with corresponding is deduced only The link and if yes, two more links - reply and delete are added. Besides links, Letters meeting in the text, are replaced with links html. Files mail.php3, del.php3 and send.php3 (listings 5-7) are arranged extremely simply, however, as files del.php3 and send.php3 Contain calls of function header (), they begin with a bracket <? php. On this time These calls are necessary that the message on a successful parcel{sending} or removal{distance} of the letter It was replaced with the basic window automatically, without any actions from the party The user. That we have not made That's all. The service webmail is ready (see figure). As it is simple To notice, we have only connected ready parts - all our six files do not total And two hundreds lines - also have received almost present{true} mailer. Certainly, with her Some problems are connected also. For example, such. 1. That will be, if with our help webmail to try to read a mail simultaneously with Two computers? At removal{distance} of the letter on one computer numbering "s``edet", and on The second rather unpleasant things will start to occur. 2. As it has already been noticed, on the server With not completely controllable maintenance use of our program can result To hit of passwords in dirty hands. 3. There is no support of the codings which are distinct from koi8-r, For mail (that for a web-browser thanking russian apache all are supported The widespread codings). 4. There is no support of investments. 5. There is no notepad. 6. No Supports of folders. However, we also did not set as to ourselves the purpose to create the program, capable to be compared with eudora Or pegasus (but, by the way, the program imap webmail program - see http://web.horde.org/imp/ - It is very close to this). Let's make some more remarks concerning possible{probable} decisions of the first and second problem. These problems are connected to a safety so, demand special attention. For the decision of the first problem it is possible to pass in addition to number{room} of the letter also The identifier of the message (messageid); it, however, will entail essential complication Programs as it will be necessary to organize detailed "trial" in a case Discrepancies of identifiers, and also correct updating of the list of letters. The second problem is solved by addition of one more form on an input{entrance} and transfers of a name The user and the password from the script to the script through url - how it is now passed Number{Room} of the letter. But then the password will appear in an address bar of a browser. With it it is possible To struggle in two ways - or zashifrovyvaja the password before a parcel{sending} and deciphering at Reception, or having created two more frames: the first is not used in any way (or in him Advertising is placed, that, in general, the same), in the second occurs all job. It is possible And to combine the named ways. Dare! LISTING 1 File counter.php3 (the counter of number of visitings of web-page) <html> <body bgcolor = " * ffffff " text = " * 000000 " link = " * 0000ff " vlink = " * 800080 " alink = " * ff0000 "> <p> Number posehheniij: <? php $filename = "counter.dat"; $fp = @fopen ($filename, "r"); if ($fp) {$counter=fgets ($fp, 10); fclose ($fp);} else { $counter=0;} $counter ++; print $counter; $fp = fopen ($filename, "w"); if ($fp) { $counter=fputs ($fp, $counter); fclose ($fp);}?> </body> </html> LISTING 2 File index.php3 (identification of the user, an establishment of contact with Mail server and creation of a set of frames for a conclusion of the list of letters and the text current Letters) <? php $realm = " web mail "; $popserver = ' 127.0.0.1 '; $logerrors = 1; if (! isset ($php_auth_user)): header (" www-authenticate: basic realm = " $ realm " "); header (" http/1.0 401 unauthorized "); echo " <h1> authorization required </h1> n "; exit; else: if (! ($imap_stream = imap_open (" {127.0.0.1:143} inbox "," $php_ auth_user "," $php_auth_pw ", op_readonly))): header (" www-authenticate: basic realm = " $ realm " "); header (" http/1.0 401 auth required "); echo " <h1> authorization required </h1> n "; exit; else: imap_close ($imap_stream);?> <html> <frameset border = "0" framespacing = "-2 " rows = " 20 %, 80 % "> <frame src = "top.php3" name = "top" marginwidth = "1" marginheight = "1" framespacing = "0"> <frame src = "main.php3" name = "main" marginwidth = "1" marginheight = "1" framespacing = "0"> </frameset> <noframes> <body> sorry, but your browser does not support frames... <br> </body> </noframes> </html> <? php endif; endif; ?> LISTING 3 File top.php3 (formation of the list of letters) <html> <body bgcolor = " * c04040 " text = " * ffffff " link = " * 0000ff " vlink = " * 800080 " alink = " * ff0000 "> <base target = "main"> <? php $imap_stream=imap_open (" {127.0.0.1:143} inbox ", "$php_auth_user", "$php_auth_pw", op_readonly); $inbox=imap_mailboxmsginfo ($imap_stream);?> <center> <table border=3 width = " 100 % "> <tr> <th> nn </th> <th> subject </th> <th> from </th> <th> date </th> </tr> <tr> <td> <? php for ($i=1; $i <= $inbox-> nmsgs; $i ++): $header=imap_header ($imap_stream, $i, 300,300,0); echo " <tr> <td> <b> <a href=main.php3? mail = ". $ i. ">". $i. " </a> </b> </td> <td> <a href=main.php3? mail = ". $ i. "> ". $header-> subject. " </a> </td> <td> <a href=main.php3? mail = ". $ i. "> ". $header-> fromaddress. " </a> </td> <td> <a href=main.php3? mail = ". $ i. ">". $header-> date. " </a> </td> </tr> "; endfor; imap_close ($imap_stream);?> </table> </center> </body> </html> LISTING 4 File main.php3 (stock-taking of letters and a conclusion of buttons) <html> <body bgcolor = " * 408080 " text = " * ffffff " link = " * 0000ff " vlink = " * 800080 " alink = " * ff0000 "> <base target = "main"> <a href=mail.php3> new message </a> <? php if (isset ($mail) ** ($mail> =1)):?> | <a href=mail.php3? mail = <? php echo $mail;?>> reply </a> | <a href=del.php3? mail = <? php echo $mail;?> target = _top> delete </a> <? php endif;?> <br> <br> <? php if (isset ($mail) ** ($mail> =1)): $imap_stream=imap_open (" {127.0.0.1:143} inbox ", "$php_auth_user", "$php_auth_pw", op_readonly); $inbox=imap_mailboxmsginfo ($imap_stream); $body=imap_fetchbody ($imap_stream, $mail, 1,0); print (nl2br (eregi_replace ( " (http|https|ftp): // ([-= % _ a-za-z0-9. / ~?:] +) "," <a href = "\1: //\2 " target = " _ blank "> \1: //\2 </a> ", htmlspecialchars ($body)))); imap_close ($imap_stream); endif;?> </body> </html> LISTING 5 File mail.php3 (creation of the letter) <html> <body bgcolor = " * 408080 " text = " * ffffff " link = " * 0000ff " vlink = " * 800080 " alink = " * ff0000 "> <base target = "main"> <? php if (isset ($mail) ** ($mail> =1)): $imap_stream=imap_open (" {127.0.0.1:143} inbox ", "$php_auth_user", "$php_auth_pw", op_readonly); $header=imap_header ($imap_stream, $mail, 300,300,0); endif; ?> <form action ='send.php3 ' method ='post '> <table border=0 cellspacing=0 cellpadding=0 width=100 %> <tr> <td class=light width = "150"> <b> <b> your name: </b> </b> </td> <td colspan=2 class=light> <input type = "text" name = "sendername" value = " <? php echo $sendername;?> " size=60 maxlength=70> </td> </tr> <tr> <td class=light width = "150"> <b> <b> from: </b> </b> </td> <td colspan=2 class=light> <input type = "text" name = "sender" value = " <? php echo $php_auth_user."@host.some_domain.com ";?> " size=60 maxlength=70> </td> </tr> <tr> <td class=light width = "150"> <b> <b> subject: </b> </b> </td> <td colspan=2 class=light> <input type = "text" name = "subject" value = " <? php if (isset ($mail) ** ($mail> =1)):?> re: <? php echo eregi_replace ("", " and *34; ", $header-> subject); endif;?> " size=60 maxlength=70> </td> </tr> <tr> <td class=light width = "150"> <b> <b> to: </b> </b> </td> <td colspan=2 class=light> <input type = "text" name = "mailto" value = " <? php if (isset ($mail) ** ($mail> =1)): echo eregi_replace ("" ", and *34; ", $header-> fromaddress); endif;?> " size=60 maxlength=70> </td> </tr> <tr> <td class=light width = "150"> <b> message body: </td> <td colspan=2 class=light> <textarea name = "text" rows=15 cols=59> <? php if (isset ($mail) ** ($mail> =1)): echo " *gt; " .eregi_replace ("n", " n*gt; ", htmlspecialchars (imap_fetchbody ($imap_stream, $mail, 1,0))); endif;?> </textarea> <center> <input type=submit value = "send"> </center> <br> </td> </tr> </table> </form> </body> </html> <? php if (isset ($mail) ** ($mail> =1)):imap_close ($imap_stream); endif; ?> LISTING 6 File del.php (removal{distance} of the letter) <html> <body bgcolor = " * 408080 " text = " * ffffff " link = " * 0000ff " vlink = " * 800080 " alink = " * ff0000 "> <base target = "main"> <? php if (isset ($mail) ** ($mail> =1)): $imap_stream=imap_open (" {127.0.0.1:143} inbox ", "$php_auth_user", "$php_auth_pw", op_readonly); $header=imap_header ($imap_stream, $mail, 300,300,0); endif; ?> <form action ='send.php3 ' method ='post '> <table border=0 cellspacing=0 cellpadding=0 width=100 %> <tr> <td class=light width = "150"> <b> <b> your name: </b> </b> </td> <td colspan=2 class=light> <input type = "text" name = "sendername" value = " <? php echo $sendername;?> " size=60 maxlength=70> </td> </tr> <tr> <td class=light width = "150"> <b> <b> from: </b> </b> </td> <td colspan=2 class=light> <input type = "text" name = "sender" value = " <? php echo $php_auth_user."@host.some_domain.com ";?> " size=60 maxlength=70> </td> </tr> <tr> <td class=light width = "150"> <b> <b> subject: </b> </b> </td> <td colspan=2 class=light> <input type = "text" name = "subject" value = " <? php if (isset ($mail) ** ($mail> =1)):?> re: <? php echo eregi_replace ("", " and *34; ", $header-> subject); endif;?> " size=60 maxlength=70> </td> </tr> <tr> <td class=light width = "150"> <b> <b> to: </b> </b> </td> <td colspan=2 class=light> <input type = "text" name = "mailto" value = " <? php if (isset ($mail) ** ($mail> =1)): echo eregi_replace ("" ", and *34; ", $header-> fromaddress); endif;?> " size=60 maxlength=70> </td> </tr> <tr> <td class=light width = "150"> <b> message body: </td> <td colspan=2 class=light> <textarea name = "text" rows=15 cols=59> <? php if (isset ($mail) ** ($mail> =1)): echo " *gt; " .eregi_replace ("n", " n*gt; ", htmlspecialchars (imap_fetchbody ($imap_stream, $mail, 1,0))); endif;?> </textarea> <center> <input type=submit value = "send"> </center> <br> </td> </tr> </table> </form> </body> </html> <? php if (isset ($mail) ** ($mail> =1)):imap_close ($imap_stream); endif; ?> LISTING 7 File send.php3 (sending of the letter) <? php header (" refresh: 5; url=main.php3 ");?> <html> <body bgcolor = " * 408080 " text = " * ffffff " link = " * 0000ff " vlink = " * 800080 " alink = " * ff0000 "> <base target = "main"> <? php mail ($mailto, $subject, $text, " from: $sendername <$sender> ncontent-type: text/plain; charset=koi8-r ");?> message was succesfully sent. </body> </html> -------------------------------------------------------------------------------- 1 Script is intentionally extremely simplified: he does not care of blocking files, not Processes mistakes of recording on a disk, etc. We Shall notice, that our counter, as against The majority of others, it is written without use tega <img>. It allows to reduce The traffic also solves a problem of not graphic browsers (for example, browsers for blind), but Can create problems if the uniform counter is necessary for several "mirrors" of the basic for us The site. "Traditional" realization of the counter in php also does not represent a problem, As he has set of functions for generation of gif-files (switching even reception The beautiful inscriptions executed by fonts truetype). 2 Number 401 is taken not from a ceiling, and from the description of the report http. 3 Unfortunately if in any other subdirectory to specify realm = " web mail " Scripts taking place in him also will receive the corresponding information. Therefore given The method is unacceptable, if you cannot supervise contents of all site. In the end We shall discuss clauses{articles} how to bypass this restriction. |
|
|||||||||||||||||||||||||||||