address parsing with encoded personal names
[squirrelmail.git] / src / addrbook_search_html.php
1 <?php
2
3 /**
4 * addrbook_search_html.php
5 *
6 * Copyright (c) 1999-2003 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * Handle addressbook searching with pure html.
10 *
11 * This file is included from compose.php
12 *
13 * $Id$
14 */
15
16 /* Path for SquirrelMail required files. */
17 if (! defined('SM_PATH') ) {
18 define('SM_PATH','../');
19 }
20
21 /* SquirrelMail required files. */
22 require_once(SM_PATH . 'include/validate.php');
23 require_once(SM_PATH . 'functions/global.php');
24 require_once(SM_PATH . 'functions/date.php');
25 require_once(SM_PATH . 'functions/display_messages.php');
26 require_once(SM_PATH . 'functions/addressbook.php');
27 require_once(SM_PATH . 'functions/plugin.php');
28 require_once(SM_PATH . 'functions/strings.php');
29 require_once(SM_PATH . 'functions/html.php');
30
31 sqgetGlobalVar('session', $session, SQ_POST);
32 sqgetGlobalVar('mailbox', $mailbox, SQ_POST);
33 sqgetGlobalVar('addrquery', $addrquery, SQ_POST);
34 sqgetGlobalVar('listall', $listall, SQ_POST);
35 sqgetGlobalVar('backend', $backend, SQ_POST);
36
37 /* Insert hidden data */
38 function addr_insert_hidden() {
39 global $body, $subject, $send_to, $send_to_cc, $send_to_bcc, $mailbox,
40 $identity, $session;
41
42 echo '<input type=hidden value="';
43 if (substr($body, 0, 1) == "\r") {
44 echo "\n";
45 }
46 echo htmlspecialchars($body) . '" name=body>' . "\n" .
47 '<input type=hidden value="' . $session . '" name=session>' . "\n" .
48 '<input type=hidden value="' . htmlspecialchars($subject) .
49 '" name=subject>' . "\n" .
50 '<input type=hidden value="' . htmlspecialchars($send_to) .
51 '" name=send_to>' . "\n" .
52 '<input type=hidden value="' . htmlspecialchars($send_to_cc) .
53 '" name=send_to_cc>' . "\n" .
54 '<input type=hidden value="' . htmlspecialchars($send_to_bcc) .
55 '" name=send_to_bcc>' . "\n" .
56 '<input type=hidden value="' . htmlspecialchars($identity) .
57 '" name=identity>' . "\n" .
58 '<input type=hidden name=mailbox value="' . htmlspecialchars($mailbox) .
59 "\">\n" . '<input type=hidden value="true" name=from_htmladdr_search>' .
60 "\n";
61 }
62
63
64 /* List search results */
65 function addr_display_result($res, $includesource = true) {
66 global $color, $javascript_on, $PHP_SELF;
67
68 if (sizeof($res) <= 0) return;
69
70 echo '<form method=post action="' . $PHP_SELF . '" name="addrbook">'."\n" .
71 '<input type=hidden name="html_addr_search_done" value="true">' . "\n";
72 addr_insert_hidden();
73 $line = 0;
74
75 if ($javascript_on) {
76 print
77 '<script language="JavaScript" type="text/javascript">' .
78 "\n<!-- \n" .
79 "function CheckAll(ch) {\n" .
80 " for (var i = 0; i < document.addrbook.elements.length; i++) {\n" .
81 " if( document.addrbook.elements[i].type == 'checkbox' &&\n" .
82 " document.addrbook.elements[i].name.substr(0,16) == 'send_to_search['+ch ) {\n" .
83 " document.addrbook.elements[i].checked = !(document.addrbook.elements[i].checked);\n".
84 " }\n" .
85 " }\n" .
86 "}\n" .
87 "//-->\n" .
88 "</script>\n";
89 $chk_all = '<a href="#" onClick="CheckAll(\'T\');">' . _("All") . '</a>&nbsp;<font color="'.$color[9].'">To</font>'.
90 '&nbsp;&nbsp;'.
91 '<a href="#" onClick="CheckAll(\'C\');">' . _("All") . '</a>&nbsp;<font color="'.$color[9].'">Cc</font>'.
92 '&nbsp;&nbsp;'.
93 '<a href="#" onClick="CheckAll(\'B\');">' . _("All") . '</a>';
94 }
95 echo html_tag( 'table', '', 'center', '', 'border="0" width="98%"' ) .
96 html_tag( 'tr', '', '', $color[9] ) .
97 html_tag( 'th', '&nbsp;' . $chk_all, 'left' ) .
98 html_tag( 'th', '&nbsp;' . _("Name"), 'left' ) .
99 html_tag( 'th', '&nbsp;' . _("E-mail"), 'left' ) .
100 html_tag( 'th', '&nbsp;' . _("Info"), 'left' );
101
102 if ($includesource) {
103 echo html_tag( 'th', '&nbsp;' . _("Source"), 'left', '', 'width="10%"' );
104 }
105
106 echo "</tr>\n";
107
108 foreach ($res as $row) {
109 $tr_bgcolor = '';
110 $email = AddressBook::full_address($row);
111 if ($line % 2) { $tr_bgcolor = $color[0]; }
112 echo html_tag( 'tr', '', '', $tr_bgcolor, 'nowrap' ) .
113 html_tag( 'td',
114 '<input type=checkbox name="send_to_search[T' . $line . ']" value = "' .
115 htmlspecialchars($email) . '">&nbsp;' . _("To") . '&nbsp;' .
116 '<input type=checkbox name="send_to_search[C' . $line . ']" value = "' .
117 htmlspecialchars($email) . '">&nbsp;' . _("Cc") . '&nbsp;' .
118 '<input type=checkbox name="send_to_search[B' . $line . ']" value = "' .
119 htmlspecialchars($email) . '">&nbsp;' . _("Bcc") . '&nbsp;' ,
120 'center', '', 'width="5%" nowrap' ) .
121 html_tag( 'td', '&nbsp;' . htmlspecialchars($row['name']) . '&nbsp;', 'left', '', 'nowrap' ) .
122 html_tag( 'td', '&nbsp;' . htmlspecialchars($row['email']) . '&nbsp;', 'left', '', 'nowrap' ) .
123 html_tag( 'td', '&nbsp;' . htmlspecialchars($row['label']) . '&nbsp;', 'left', '', 'nowrap' );
124
125 if ($includesource) {
126 echo html_tag( 'td', '&nbsp;' . $row['source'] . '&nbsp;', 'left', '', 'nowrap' );
127 }
128 echo "</tr>\n";
129 $line ++;
130 }
131 if ($includesource) { $td_colspan = '5'; } else { $td_colspan = '4'; }
132 echo html_tag( 'tr',
133 html_tag( 'td',
134 '<INPUT TYPE=submit NAME="addr_search_done" VALUE="' .
135 _("Use Addresses") . '">' ,
136 'center', '', 'colspan="'. $td_colspan .'"' )
137 ) .
138 '</TABLE>' .
139 '<INPUT TYPE=hidden VALUE=1 NAME="html_addr_search_done">' .
140 '</FORM>';
141 }
142
143 /* --- End functions --- */
144
145 if ($compose_new_win == '1') {
146 compose_Header($color, $mailbox);
147 }
148 else {
149 displayPageHeader($color, $mailbox);
150 }
151 /* Initialize addressbook */
152 $abook = addressbook_init();
153
154
155 echo '<br>' .
156 html_tag( 'table',
157 html_tag( 'tr',
158 html_tag( 'td', '<b>' . _("Address Book Search") . '</b>', 'center', $color[0] )
159 ) ,
160 'center', '', 'width="95%" cellpadding="2" cellspacing="2" border="0"' );
161
162
163 /* Search form */
164 echo '<center>' .
165 html_tag( 'table', '', 'center', '', 'border="0"' ) .
166 html_tag( 'tr' ) .
167 html_tag( 'td', '', 'left', '', 'nowrap valign="middle"' ) . "\n" .
168 '<FORM METHOD=post NAME=f ACTION="' . $PHP_SELF .
169 '?html_addr_search=true">' . "\n<CENTER>\n" .
170 ' <nobr><STRONG>' . _("Search for") . "</STRONG>\n";
171 addr_insert_hidden();
172 if (! isset($addrquery))
173 $addrquery = '';
174 echo ' <INPUT TYPE=text NAME=addrquery VALUE="' .
175 htmlspecialchars($addrquery) . "\" SIZE=26>\n";
176
177 /* List all backends to allow the user to choose where to search */
178 if (!isset($backend)) { $backend = ''; }
179 if ($abook->numbackends > 1) {
180 echo '<STRONG>' . _("in") . '</STRONG>&nbsp;<SELECT NAME=backend>' . "\n" .
181 '<OPTION VALUE=-1';
182 if ($backend == -1) { echo ' SELECTED'; }
183 echo '>' . _("All address books") . "\n";
184 $ret = $abook->get_backend_list();
185 while (list($undef,$v) = each($ret)) {
186 echo '<OPTION VALUE=' . $v->bnum;
187 if ($backend == $v->bnum) { echo ' SELECTED'; }
188 echo '>' . $v->sname . "\n";
189 }
190 echo "</SELECT>\n";
191 } else {
192 echo '<INPUT TYPE=hidden NAME=backend VALUE=-1>' . "\n";
193 }
194 if (isset($session)) {
195 echo "<input type=hidden name=\"session\" value=\"$session\">";
196 }
197
198 echo '<INPUT TYPE=submit VALUE="' . _("Search") . '">' .
199 '&nbsp;|&nbsp;<INPUT TYPE=submit VALUE="' . _("List all") .
200 '" NAME=listall>' . "\n" .
201 '</FORM></center></TD></TR></TABLE>' . "\n";
202 addr_insert_hidden();
203 echo '</center>';
204 do_hook('addrbook_html_search_below');
205 /* End search form */
206
207 /* Show personal addressbook */
208
209 if ( !empty( $listall ) ){
210 $addrquery = '*';
211 }
212
213 if ($addrquery == '' && empty($listall)) {
214
215 if (! isset($backend) || $backend != -1 || $addrquery == '') {
216 if ($addrquery == '') {
217 $backend = $abook->localbackend;
218 }
219
220 /* echo '<H3 ALIGN=center>' . $abook->backends[$backend]->sname) . "</H3>\n"; */
221
222 $res = $abook->list_addr($backend);
223
224 if (is_array($res)) {
225 usort($res,'alistcmp');
226 addr_display_result($res, false);
227 } else {
228 echo html_tag( 'p', '<strong><br>' .
229 sprintf(_("Unable to list addresses from %s"),
230 $abook->backends[$backend]->sname) . "</strong>\n" ,
231 'center' );
232 }
233
234 } else {
235 $res = $abook->list_addr();
236 usort($res,'alistcmp');
237 addr_display_result($res, true);
238 }
239 exit;
240 }
241 else {
242
243 /* Do the search */
244 if (!empty($addrquery)) {
245
246 if ($backend == -1) {
247 $res = $abook->s_search($addrquery);
248 } else {
249 $res = $abook->s_search($addrquery, $backend);
250 }
251
252 if (!is_array($res)) {
253 echo html_tag( 'p', '<b><br>' .
254 _("Your search failed with the following error(s)") .
255 ':<br>' . $abook->error . "</b>\n" ,
256 'center' ) .
257 "\n</BODY></HTML>\n";
258 } else {
259 if (sizeof($res) == 0) {
260 echo html_tag( 'p', '<br><b>' .
261 _("No persons matching your search was found") . "</b>\n" ,
262 'center' ) .
263 "\n</BODY></HTML>\n";
264 } else {
265 addr_display_result($res);
266 }
267 }
268 }
269 }
270
271 if ($addrquery == '' || sizeof($res) == 0) {
272 /* printf('<center><FORM METHOD=post NAME=k ACTION="compose.php">'."\n", $PHP_SELF); */
273 echo '<center><FORM METHOD=post NAME=k ACTION="compose.php">' . "\n";
274 addr_insert_hidden();
275 echo '<INPUT TYPE=submit VALUE="' . _("Return") . '" NAME=return>' . "\n" .
276 '</form></center></nobr>';
277 }
278
279 ?>
280 </body></html>