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