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