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