XHTML fixes
[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 $email = htmlspecialchars(addcslashes(AddressBook::full_address($row), "'"), ENT_QUOTES);
140 if ($line % 2) {
141 $tr_bgcolor = $color[12];
142 } else {
143 $tr_bgcolor = $color[4];
144 }
145 echo html_tag( 'tr', '', '', $tr_bgcolor, 'nowrap' ) .
146 html_tag( 'td',
147 '<small><a href="javascript:to_address(' .
148 "'" . $email . "');\">To</a> | " .
149 '<a href="javascript:cc_address(' .
150 "'" . $email . "');\">Cc</a> | " .
151 '<a href="javascript:bcc_address(' .
152 "'" . $email . "');\">Bcc</a></small>",
153 'center', '', 'valign="top" width="5%" nowrap' ) .
154 html_tag( 'td', '&nbsp;' . htmlspecialchars($row['name']), 'left', '', 'valign="top" nowrap' ) .
155 html_tag( 'td', '&nbsp;' .
156 '<a href="javascript:to_and_close(' .
157 "'" . $email . "');\">" . htmlspecialchars($row['email']) . '</a>'
158 , 'left', '', 'valign="top"' ) .
159 html_tag( 'td', htmlspecialchars($row['label']), 'left', '', 'valign="top" nowrap' );
160 if ($includesource) {
161 echo html_tag( 'td', '&nbsp;' . $row['source'], 'left', '', 'valign="top" nowrap' );
162 }
163
164 echo "</tr>\n";
165 $line++;
166 }
167 echo '</table>';
168 }
169
170 /* ================= End of functions ================= */
171
172 require_once('../functions/strings.php');
173 require_once('../functions/addressbook.php');
174
175 displayHtmlHeader();
176
177 /* Initialize vars */
178 if (!isset($query)) { $query = ''; }
179 if (!isset($show)) { $show = ''; }
180 if (!isset($backend)) { $backend = ''; }
181
182 /* Choose correct colors for top and bottom frame */
183 if ($show == 'form' && !isset($listall)) {
184 echo '<body text="' . $color[6] . '" bgcolor="' . $color[3] . '" ' .
185 'link="' . $color[6] . '" vlink="' . $color[6] . '" ' .
186 'alink="' . $color[6] . '" ' .
187 'OnLoad="document.sform.query.focus();">';
188 } else {
189 echo '<body text="' . $color[8] . '" bgcolor="' . $color[4] . '" ' .
190 'link="' . $color[7] . '" vlink="' . $color[7] . '" ' .
191 'alink="' . $color[7] . "\">\n";
192 }
193
194 /* Empty search */
195 if (empty($query) && empty($show) && empty($listall)) {
196 echo html_tag( 'p', '<br />' .
197 _("No persons matching your search were found"),
198 'center' ) .
199 "\n</body></html>\n",
200 exit;
201 }
202
203 /* Initialize addressbook */
204 $abook = addressbook_init();
205
206 /* Create search form */
207 if ($show == 'form' && empty($listall)) {
208 echo '<form name="sform" target="abookres" action="addrbook_search.php'.
209 '" method="post">' . "\n" .
210 html_tag( 'table', '', '', '', 'border="0" width="100%" height="100%"' ) .
211 html_tag( 'tr' ) .
212 html_tag( 'td', ' <strong>' . _("Search for") . "</strong>\n", 'left', '', 'nowrap valign="middle" width="10%"' ) .
213 html_tag( 'td', '', 'left', '', '' ) .
214 addInput('query', $query, 28);
215
216 /* List all backends to allow the user to choose where to search */
217 if ($abook->numbackends > 1) {
218 echo '<strong>' . _("in") . '</strong>&nbsp;'."\n".
219 $selopts['-1'] = _("All address books");
220
221 $ret = $abook->get_backend_list();
222 while (list($undef,$v) = each($ret)) {
223 $selopts[$v->bnum] = $v->sname;
224 }
225 echo addSelect('backend', $selopts, '-1', TRUE);
226 } else {
227 echo addHidden('backend', '-1');
228 }
229
230 echo '</td></tr>' .
231 html_tag( 'tr',
232 html_tag( 'td', '', 'left' ) .
233 html_tag( 'td',
234 '<input type="submit" value="' . _("Search") . '" name="show" />' .
235 '&nbsp;|&nbsp;<input type="submit" value="' . _("List all") .
236 '" name="listall" />' . "\n" .
237 '&nbsp;|&nbsp;<input type="button" value="' . _("Close") .
238 '" onclick="parent.close();" />' . "\n" ,
239 'left' )
240 ) .
241 '</table></form>' . "\n";
242 } else {
243
244 /* Show personal addressbook */
245 if ($show == 'blank' && empty($listall)) {
246
247 if($backend != -1 || $show == 'blank') {
248 if ($show == 'blank') {
249 $backend = $abook->localbackend;
250 }
251 $res = $abook->list_addr($backend);
252
253 if(is_array($res)) {
254 usort($res,'alistcmp');
255 display_result($res, false);
256 } else {
257 echo html_tag( 'p', '<strong>' .
258 sprintf(_("Unable to list addresses from %s"),
259 $abook->backends[$backend]->sname) . '</strong>' ,
260 'center' ) . "\n";
261 }
262 } else {
263 $res = $abook->list_addr();
264 usort($res,'alistcmp');
265 display_result($res, true);
266 }
267
268 } else {
269 if( !empty( $listall ) ){
270 $query = '*';
271 }
272
273 /* Do the search */
274 if (!empty($query)) {
275
276 if($backend == -1) {
277 $res = $abook->s_search($query);
278 } else {
279 $res = $abook->s_search($query, $backend);
280 }
281
282 if (!is_array($res)) {
283 echo html_tag( 'p', '<b><br />' .
284 _("Your search failed with the following error(s)") .
285 ':<br />' . $abook->error . "</b>\n" ,
286 'center' ) .
287 "\n</body></html>\n";
288 exit;
289 }
290
291 if (sizeof($res) == 0) {
292 echo html_tag( 'p', '<br /><b>' .
293 _("No persons matching your search were found") . "</b>\n" ,
294 'center' ) .
295 "\n</body></html>\n";
296 exit;
297 }
298
299 display_result($res);
300 }
301 }
302
303 }
304 ?>
305 </BODY></HTML>