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