Hmmm, very active Changelog today :-)
[squirrelmail.git] / src / addrbook_search.php
1 <?php
2
3 /**
4 * addrbook_search.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 in the popup window.
10 *
11 * NOTE: A lot of this code is similar to the code in
12 * addrbook_search_html.html -- If you change one,
13 * change the other one too!
14 *
15 * $Id$
16 */
17
18 /* Path for SquirrelMail required files. */
19 define('SM_PATH','../');
20
21 /* SquirrelMail required files. */
22 require_once(SM_PATH . 'include/validate.php');
23 require_once(SM_PATH . 'functions/strings.php');
24 require_once(SM_PATH . 'functions/html.php');
25
26 /* lets get the global vars we may need */
27 $key = $_COOKIE['key'];
28 $username = $_SESSION['username'];
29 $onetimepad = $_SESSION['onetimepad'];
30 $base_uri = $_SESSION['base_uri'];
31
32 sqgetGlobalVar('show' , $show);
33 if ( isset($_POST['query']) ) {
34 $query = $_POST['query'];
35 }
36 if ( isset($_POST['listall']) ) {
37 $listall = $_POST['listall'];
38 }
39 if ( isset($_POST['backend'] ) ) {
40 $backend = $_POST['backend'];
41 }
42
43 /* Function to include JavaScript code */
44 function insert_javascript() {
45 ?>
46 <SCRIPT LANGUAGE="Javascript"><!--
47
48 function to_and_close($addr) {
49 to_address($addr);
50 parent.close();
51 }
52
53 function to_address($addr) {
54 var prefix = "";
55 var pwintype = typeof parent.opener.document.compose;
56
57 $addr = $addr.replace(/ {1,35}$/, "");
58
59 if (pwintype != "undefined") {
60 if (parent.opener.document.compose.send_to.value) {
61 prefix = ", ";
62 parent.opener.document.compose.send_to.value =
63 parent.opener.document.compose.send_to.value + ", " + $addr;
64 } else {
65 parent.opener.document.compose.send_to.value = $addr;
66 }
67 }
68 }
69
70 function cc_address($addr) {
71 var prefix = "";
72 var pwintype = typeof parent.opener.document.compose;
73
74 $addr = $addr.replace(/ {1,35}$/, "");
75
76 if (pwintype != "undefined") {
77 if (parent.opener.document.compose.send_to_cc.value) {
78 prefix = ", ";
79 parent.opener.document.compose.send_to_cc.value =
80 parent.opener.document.compose.send_to_cc.value + ", " + $addr;
81 } else {
82 parent.opener.document.compose.send_to_cc.value = $addr;
83 }
84 }
85 }
86
87 function bcc_address($addr) {
88 var prefix = "";
89 var pwintype = typeof parent.opener.document.compose;
90
91 $addr = $addr.replace(/ {1,35}$/, "");
92
93 if (pwintype != "undefined") {
94 if (parent.opener.document.compose.send_to_bcc.value) {
95 prefix = ", ";
96 parent.opener.document.compose.send_to_bcc.value =
97 parent.opener.document.compose.send_to_bcc.value + ", " + $addr;
98 } else {
99 parent.opener.document.compose.send_to_bcc.value = $addr;
100 }
101 }
102 }
103
104 // --></SCRIPT>
105 <?php
106 } /* End of included JavaScript */
107
108
109 /* List search results */
110 function display_result($res, $includesource = true) {
111 global $color;
112
113 if(sizeof($res) <= 0) return;
114
115 insert_javascript();
116
117 $line = 0;
118 echo html_tag( 'table', '', 'center', '', 'border="0" width="98%"' ) .
119 html_tag( 'tr', '', '', $color[9] ) .
120 html_tag( 'th', '&nbsp;', 'left' ) .
121 html_tag( 'th', '&nbsp;' . _("Name"), 'left' ) .
122 html_tag( 'th', '&nbsp;' . _("E-mail"), 'left' ) .
123 html_tag( 'th', '&nbsp;' . _("Info"), 'left' );
124
125 if ($includesource) {
126 echo html_tag( 'th', '&nbsp;' . _("Source"), 'left', 'width="10%"' );
127 }
128 echo "</tr>\n";
129
130 while (list($undef, $row) = each($res)) {
131 $tr_bgcolor = '';
132 $email = htmlspecialchars(addcslashes(AddressBook::full_address($row), "'"), ENT_QUOTES);
133 if ($line % 2) { $tr_bgcolor = $color[0]; }
134 echo html_tag( 'tr', '', '', $tr_bgcolor, 'nowrap' ) .
135 html_tag( 'td',
136 '<small><a href="javascript:to_address(' .
137 "'" . $email . "');\">To</A> | " .
138 '<a href="javascript:cc_address(' .
139 "'" . $email . "');\">Cc</A> | " .
140 '<a href="javascript:bcc_address(' .
141 "'" . $email . "');\">Bcc</A></small>",
142 'center', '', 'valign="top" width="5%" nowrap' ) .
143 html_tag( 'td', '&nbsp;' . htmlspecialchars($row['name']), 'left', '', 'valign="top" nowrap' ) .
144 html_tag( 'td', '&nbsp;' .
145 '<a href="javascript:to_and_close(' .
146 "'" . $email . "');\">" . htmlspecialchars($row['email']) . '</A>'
147 , 'left', '', 'valign="top"' ) .
148 html_tag( 'td', htmlspecialchars($row['label']), 'left', '', 'valign="top" nowrap' );
149 if ($includesource) {
150 echo html_tag( 'td', '&nbsp;' . $row['source'], 'left', '', 'valign="top" nowrap' );
151 }
152
153 echo "</tr>\n";
154 $line++;
155 }
156 echo '</table>';
157 }
158
159 /* ================= End of functions ================= */
160
161 require_once('../functions/strings.php');
162 require_once('../functions/addressbook.php');
163
164 displayHtmlHeader();
165
166 /* Initialize vars */
167 if (!isset($query)) { $query = ''; }
168 if (!isset($show)) { $show = ''; }
169 if (!isset($backend)) { $backend = ''; }
170
171 /* Choose correct colors for top and bottom frame */
172 if ($show == 'form' && !isset($listall)) {
173 echo '<BODY TEXT="' . $color[6] . '" BGCOLOR="' . $color[3] . '" ' .
174 'LINK="' . $color[6] . '" VLINK="' . $color[6] . '" ' .
175 'ALINK="' . $color[6] . '" ' .
176 'OnLoad="document.sform.query.focus();">';
177 } else {
178 echo '<BODY TEXT="' . $color[8] . '" BGCOLOR="' . $color[4] . '" ' .
179 'LINK="' . $color[7] . '" VLINK="' . $color[7] . '" ' .
180 'ALINK="' . $color[7] . "\">\n";
181 }
182
183 /* Empty search */
184 if (empty($query) && empty($show) && empty($listall)) {
185 echo html_tag( 'p', '<br>' .
186 _("No persons matching your search was found"),
187 'center' ) .
188 "\n</BODY></HTML>\n",
189 exit;
190 }
191
192 /* Initialize addressbook */
193 $abook = addressbook_init();
194
195 /* Create search form */
196 if ($show == 'form' && empty($listall)) {
197 echo '<FORM NAME=sform TARGET=abookres ACTION="addrbook_search.php'.
198 '" METHOD="POST">' . "\n" .
199 html_tag( 'table', '', '', '', 'border="0" width="100%" height="100%"' ) .
200 html_tag( 'tr' ) .
201 html_tag( 'td', ' <strong>' . _("Search for") . "</strong>\n", 'left', '', 'nowrap valign="middle" width="10%"' ) .
202 html_tag( 'td', '', 'left', '', '' ) .
203 '<INPUT TYPE=text NAME=query VALUE="' . htmlspecialchars($query) .
204 "\" SIZE=28>\n";
205
206 /* List all backends to allow the user to choose where to search */
207 if ($abook->numbackends > 1) {
208 echo '<STRONG>' . _("in") . '</STRONG>&nbsp;<SELECT NAME=backend>'."\n".
209 '<OPTION VALUE=-1 SELECTED>' . _("All address books") . "\n";
210 $ret = $abook->get_backend_list();
211 while (list($undef,$v) = each($ret)) {
212 echo '<OPTION VALUE=' . $v->bnum . '>' . $v->sname . "\n";
213 }
214 echo "</SELECT>\n";
215 } else {
216 echo '<INPUT TYPE=hidden NAME=backend VALUE=-1>' . "\n";
217 }
218
219 echo '</td></tr>' .
220 html_tag( 'tr',
221 html_tag( 'td', '', 'left' ) .
222 html_tag( 'td',
223 '<INPUT TYPE=submit VALUE="' . _("Search") . '" NAME=show>' .
224 '&nbsp;|&nbsp;<INPUT TYPE=submit VALUE="' . _("List all") .
225 '" NAME=listall>' . "\n" .
226 '&nbsp;|&nbsp;<INPUT TYPE=button VALUE="' . _("Close") .
227 '" onclick="parent.close();">' . "\n" ,
228 'left' )
229 ) .
230 '</table></form>' . "\n";
231 } else {
232
233 /* Show personal addressbook */
234 if ($show == 'blank' && empty($listall)) {
235
236 if($backend != -1 || $show == 'blank') {
237 if ($show == 'blank') {
238 $backend = $abook->localbackend;
239 }
240 $res = $abook->list_addr($backend);
241
242 if(is_array($res)) {
243 usort($res,'alistcmp');
244 display_result($res, false);
245 } else {
246 echo html_tag( 'p', '<strong>' .
247 sprintf(_("Unable to list addresses from %s"),
248 $abook->backends[$backend]->sname) . '</strong>' ,
249 'center' ) . "\n";
250 }
251 } else {
252 $res = $abook->list_addr();
253 usort($res,'alistcmp');
254 display_result($res, true);
255 }
256
257 } else {
258 if( !empty( $listall ) ){
259 $query = '*';
260 }
261
262 /* Do the search */
263 if (!empty($query)) {
264
265 if($backend == -1) {
266 $res = $abook->s_search($query);
267 } else {
268 $res = $abook->s_search($query, $backend);
269 }
270
271 if (!is_array($res)) {
272 echo html_tag( 'p', '<b><br>' .
273 _("Your search failed with the following error(s)") .
274 ':<br>' . $abook->error . "</b>\n" ,
275 'center' ) .
276 "\n</BODY></HTML>\n";
277 exit;
278 }
279
280 if (sizeof($res) == 0) {
281 echo html_tag( 'p', '<br><b>' .
282 _("No persons matching your search was found") . "</b>\n" ,
283 'center' ) .
284 "\n</BODY></HTML>\n";
285 exit;
286 }
287
288 display_result($res);
289 }
290 }
291
292 }
293
294 echo "</BODY></HTML>\n";
295
296 ?>