* Forgot to make $identity global -- bug fixed
[squirrelmail.git] / src / addrbook_search_html.php
1 <?php
2 /**
3 ** addrbook_search.php
4 **
5 ** Copyright (c) 1999-2000 The SquirrelMail development team
6 ** Licensed under the GNU GPL. For full terms see the file COPYING.
7 **
8 ** Handle addressbook searching with pure html.
9 **
10 ** This file is included from compose.php
11 **
12 ** NOTE: A lot of this code is similar to the code in
13 ** addrbook_search.html -- If you change one, change
14 ** the other one too!
15 **
16 ** $Id$
17 **/
18
19 include('../src/validate.php');
20 include('../functions/page_header.php');
21 include('../functions/date.php');
22 include('../functions/smtp.php');
23 include('../functions/display_messages.php');
24 include('../functions/addressbook.php');
25 include('../functions/plugin.php');
26 include('../src/load_prefs.php');
27
28 // Insert hidden data
29 function addr_insert_hidden() {
30 global $body, $subject, $send_to, $send_to_cc, $send_to_bcc, $mailbox,
31 $identity;
32
33 echo '<input type=hidden value="';
34 if (substr($body, 0, 1) == "\r")
35 echo "\n";
36 echo htmlspecialchars($body) . '" name=body>' . "\n";
37 echo '<input type=hidden value="' . htmlspecialchars($subject)
38 . '" name=subject>' . "\n";
39 echo '<input type=hidden value="' . htmlspecialchars($send_to)
40 . '" name=send_to>' . "\n";
41 echo "<input type=hidden value=\"" . htmlspecialchars($send_to_cc)
42 . '" name=send_to_cc>' . "\n";
43 echo "<input type=hidden value=\"" . htmlspecialchars($send_to_bcc)
44 . '" name=send_to_bcc>' . "\n";
45 echo "<input type=hidden value=\"" . htmlspecialchars($identity)
46 . '" name=identity>' . "\n";
47 echo "<input type=hidden name=mailbox value=\"" .
48 htmlspecialchars($mailbox) . "\">\n";
49 echo "<input type=hidden value=\"true\" name=from_htmladdr_search>\n";
50 }
51
52
53 // List search results
54 function addr_display_result($res, $includesource = true) {
55 global $color, $PHP_SELF;
56
57 if(sizeof($res) <= 0) return;
58
59 echo '<form method=post action="' . $PHP_SELF . "\">\n";
60 echo '<input type=hidden name="html_addr_search_done" value="true">';
61 echo "\n";
62 addr_insert_hidden();
63 $line = 0;
64
65 print "<TABLE BORDER=0 WIDTH=\"98%\" ALIGN=center>";
66 printf("<TR BGCOLOR=\"$color[9]\"><TH ALIGN=left>&nbsp;".
67 "<TH ALIGN=left>&nbsp;%s<TH ALIGN=left>&nbsp;%s".
68 "<TH ALIGN=left>&nbsp;%s",
69 _("Name"), _("E-mail"), _("Info"));
70
71 if($includesource)
72 printf("<TH ALIGN=left WIDTH=\"10%%\">&nbsp;%s", _("Source"));
73
74 print "</TR>\n";
75
76 foreach ($res as $row) {
77 echo '<tr';
78 if ($line % 2) echo ' bgcolor="' . $color[0] . '"';
79 echo ' nowrap><td nowrap align=center width="5%">';
80 echo '<input type=checkbox name="send_to_search[T' . $line . ']" value = "' .
81 htmlspecialchars($row['email']) . '">&nbsp;To&nbsp;';
82 echo '<input type=checkbox name="send_to_search[C' . $line . ']" value = "' .
83 htmlspecialchars($row['email']) . '">&nbsp;Cc&nbsp;';
84 echo '<input type=checkbox name="send_to_search[B' . $line . ']" value = "' .
85 htmlspecialchars($row['email']) . '">&nbsp;Bcc&nbsp;';
86 echo '</td><td nowrap>&nbsp;' . $row['name'] . '&nbsp;</td>';
87 echo '<td nowrap>&nbsp;' . $row['email'] . '&nbsp;</td>';
88 echo '<td nowrap>&nbsp;' . $row['label'] . '&nbsp;</td>';
89 if($includesource)
90 echo '<td nowrap>&nbsp;' . $row['source'] . '&nbsp;</td>';
91 echo "</tr>\n";
92 $line ++;
93 }
94 printf('<TR><TD ALIGN=center COLSPAN=%d><INPUT TYPE=submit '.
95 'NAME="addr_search_done" VALUE="%s"></TD></TR>',
96 4 + ($includesource ? 1 : 0),
97 _("Use Addresses"));
98 print '</TABLE>';
99 print '<INPUT TYPE=hidden VALUE=1 NAME="html_addr_search_done">';
100 print '</FORM>';
101 }
102
103 // --- End functions ---
104
105 global $mailbox;
106 displayPageHeader($color, $mailbox);
107
108 // Initialize addressbook
109 $abook = addressbook_init();
110
111 ?>
112
113 <br>
114 <table width=95% align=center cellpadding=2 cellspacing=2 border=0>
115 <tr><td bgcolor="<?php echo $color[0] ?>">
116 <center><b><?php echo _("Address Book Search") ?></b></center>
117 </td></tr></table>
118
119 <?php
120 // Search form
121 print "<CENTER>\n";
122 print "<TABLE BORDER=0>\n";
123 print "<TR><TD NOWRAP VALIGN=middle>\n";
124 printf('<FORM METHOD=post NAME=f ACTION="%s?html_addr_search=true">'."\n", $PHP_SELF);
125 print "<CENTER>\n";
126 printf(" <nobr><STRONG>%s</STRONG>\n", _("Search for"));
127 addr_insert_hidden();
128 if (! isset($addrquery))
129 $addrquery = '';
130 printf(" <INPUT TYPE=text NAME=addrquery VALUE=\"%s\" SIZE=26>\n",
131 htmlspecialchars($addrquery));
132
133 // List all backends to allow the user to choose where to search
134 if(!isset($backend)) $backend = "";
135 if($abook->numbackends > 1) {
136 printf("<STRONG>%s</STRONG>&nbsp;<SELECT NAME=backend>\n",
137 _("in"));
138 printf("<OPTION VALUE=-1 %s>%s\n",
139 ($backend == -1) ? "SELECTED" : "",
140 _("All address books"));
141 $ret = $abook->get_backend_list();
142 while(list($undef,$v) = each($ret))
143 printf("<OPTION VALUE=%d %s>%s\n",
144 $v->bnum,
145 ($backend == $v->bnum) ? "SELECTED" : "",
146 $v->sname);
147 print "</SELECT>\n";
148 } else {
149 print "<INPUT TYPE=hidden NAME=backend VALUE=-1>\n";
150 }
151 printf("<INPUT TYPE=submit VALUE=\"%s\">",
152 _("Search"));
153 printf("&nbsp;|&nbsp;<INPUT TYPE=submit VALUE=\"%s\" NAME=listall>\n",
154 _("List all"));
155 print '</FORM></center>';
156
157 print "</TD></TR></TABLE>\n";
158 addr_insert_hidden();
159 print "</CENTER>";
160 do_hook('addrbook_html_search_below');
161 // End search form
162
163 // Show personal addressbook
164 if($addrquery == '' || !empty($listall)) {
165
166 if(! isset($backend) || $backend != -1 || $addrquery == '') {
167 if($addrquery == '')
168 $backend = $abook->localbackend;
169
170 //printf("<H3 ALIGN=center>%s</H3>\n", $abook->backends[$backend]->sname);
171
172 $res = $abook->list_addr($backend);
173
174 if(is_array($res)) {
175 addr_display_result($res, false);
176 } else {
177 printf("<P ALIGN=center><STRONG>"._("Unable to list addresses from %s").
178 "</STRONG></P>\n", $abook->backends[$backend]->sname);
179 }
180
181 } else {
182 $res = $abook->list_addr();
183 addr_display_result($res, true);
184 }
185 exit;
186
187 } else
188
189 // Do the search
190 if(!empty($addrquery) && empty($listall)) {
191
192 if($backend == -1) {
193 $res = $abook->s_search($addrquery);
194 } else {
195 $res = $abook->s_search($addrquery, $backend);
196 }
197
198 if(!is_array($res)) {
199 printf("<P ALIGN=center><B><BR>%s:<br>%s</B></P>\n</BODY></HTML>\n",
200 _("Your search failed with the following error(s)"),
201 $abook->error);
202 } else if(sizeof($res) == 0) {
203 printf("<P ALIGN=center><BR><B>%s.</B></P>\n</BODY></HTML>\n",
204 _("No persons matching your search was found"));
205 } else {
206 addr_display_result($res);
207 }
208 }
209
210 if ($addrquery == '' || sizeof($res) == 0) {
211 printf('<center><FORM METHOD=post NAME=k ACTION="compose.php">'."\n", $PHP_SELF);
212 addr_insert_hidden();
213 printf("<INPUT TYPE=submit VALUE=\"%s\" NAME=return>\n", _("Return"));
214 print '</form>';
215 print '</center></nobr>';
216 }
217
218 ?>
219 </body></html>