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