Do not pass the $key to sqimap_login, that takes care of that itself.
[squirrelmail.git] / src / addrbook_search.php
1 <?php
2
3 /**
4 * addrbook_search.php
5 *
6 * Handle addressbook searching in the popup window.
7 *
8 * NOTE: A lot of this code is similar to the code in
9 * addrbook_search_html.html -- If you change one,
10 * change the other one too!
11 *
12 * @copyright &copy; 1999-2006 The SquirrelMail Project Team
13 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
14 * @version $Id$
15 * @package squirrelmail
16 * @subpackage addressbook
17 */
18
19 /**
20 * Include the SquirrelMail initialization file.
21 */
22 require('../include/init.php');
23
24 include_once(SM_PATH . 'functions/forms.php');
25 include_once(SM_PATH . 'functions/addressbook.php');
26
27 /**
28 * Function to include JavaScript code
29 * @return void
30 */
31 function insert_javascript() {
32 ?>
33 <script type="text/javascript"><!--
34
35 function to_and_close($addr) {
36 to_address($addr);
37 parent.close();
38 }
39
40 function to_address($addr) {
41 var prefix = "";
42 var pwintype = typeof parent.opener.document.compose;
43
44 $addr = $addr.replace(/ {1,35}$/, "");
45
46 if (pwintype != "undefined") {
47 if (parent.opener.document.compose.send_to.value) {
48 prefix = ", ";
49 parent.opener.document.compose.send_to.value =
50 parent.opener.document.compose.send_to.value + ", " + $addr;
51 } else {
52 parent.opener.document.compose.send_to.value = $addr;
53 }
54 }
55 }
56
57 function cc_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_cc.value) {
65 prefix = ", ";
66 parent.opener.document.compose.send_to_cc.value =
67 parent.opener.document.compose.send_to_cc.value + ", " + $addr;
68 } else {
69 parent.opener.document.compose.send_to_cc.value = $addr;
70 }
71 }
72 }
73
74 function bcc_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_bcc.value) {
82 prefix = ", ";
83 parent.opener.document.compose.send_to_bcc.value =
84 parent.opener.document.compose.send_to_bcc.value + ", " + $addr;
85 } else {
86 parent.opener.document.compose.send_to_bcc.value = $addr;
87 }
88 }
89 }
90
91 // --></script>
92 <?php
93 } /* End of included JavaScript */
94
95
96 /**
97 * List search results
98 * @param array $res Array of search results
99 * @param bool $includesource [Default=true]
100 * @return void
101 */
102 function display_result($res, $includesource = true) {
103 global $color;
104
105 if(sizeof($res) <= 0) return;
106
107 insert_javascript();
108
109 $line = 0;
110 echo html_tag( 'table', '', 'center', '', 'border="0" width="98%"' ) .
111 html_tag( 'tr', '', '', $color[9] ) .
112 html_tag( 'th', '&nbsp;', 'left' ) .
113 html_tag( 'th', '&nbsp;' . _("Name"), 'left' ) .
114 html_tag( 'th', '&nbsp;' . _("E-mail"), 'left' ) .
115 html_tag( 'th', '&nbsp;' . _("Info"), 'left' );
116
117 if ($includesource) {
118 echo html_tag( 'th', '&nbsp;' . _("Source"), 'left', '', 'width="10%"' );
119 }
120 echo "</tr>\n";
121
122 while (list($undef, $row) = each($res)) {
123 $email = htmlspecialchars(addcslashes(AddressBook::full_address($row), "'"), ENT_QUOTES);
124 if ($line % 2) {
125 $tr_bgcolor = $color[12];
126 } else {
127 $tr_bgcolor = $color[4];
128 }
129 echo html_tag( 'tr', '', '', $tr_bgcolor, 'style="white-space: 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%" style="white-space: nowrap;"' ) .
138 html_tag( 'td', '&nbsp;' . htmlspecialchars($row['name']), 'left', '', 'valign="top" style="white-space: 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" style="white-space: nowrap;"' );
144 if ($includesource) {
145 echo html_tag( 'td', '&nbsp;' . $row['source'], 'left', '', 'valign="top" style="white-space: nowrap;"' );
146 }
147
148 echo "</tr>\n";
149 $line++;
150 }
151 echo '</table>';
152 }
153
154 /* ================= End of functions ================= */
155
156 /** lets get the global vars we may need */
157
158 if (! sqgetGlobalVar('show' , $show)) {
159 $show = '';
160 }
161 if (! sqgetGlobalVar('query', $query, SQ_POST)) {
162 $query = '';
163 }
164 if (! sqgetGlobalVar('listall', $listall, SQ_POST)) {
165 unset($listall);
166 }
167 if (! sqgetGlobalVar('backend', $backend, SQ_POST)) {
168 $backend = '';
169 }
170
171 displayHtmlHeader();
172
173 /** set correct value of $default_charset */
174 global $default_charset;
175 set_my_charset();
176
177 /* Choose correct colors for top and bottom frame */
178 if ($show == 'form' && !isset($listall)) {
179 echo '<body text="' . $color[6] . '" bgcolor="' . $color[3] . '" ' .
180 'link="' . $color[6] . '" vlink="' . $color[6] . '" ' .
181 'alink="' . $color[6] . '" ' .
182 'OnLoad="document.sform.query.focus();">';
183 } else {
184 echo '<body text="' . $color[8] . '" bgcolor="' . $color[4] . '" ' .
185 'link="' . $color[7] . '" vlink="' . $color[7] . '" ' .
186 'alink="' . $color[7] . "\">\n";
187 }
188
189 /* Empty search */
190 if (empty($query) && empty($show) && !isset($listall)) {
191 echo html_tag( 'p', '<br />' .
192 _("No persons matching your search were found"),
193 'center' ) .
194 "\n</body></html>\n";
195 exit;
196 }
197
198 /* Initialize addressbook, show init errors only in bottom frame */
199 $showerr=($show=='form' ? false : true);
200 $abook = addressbook_init($showerr);
201
202 /* Create search form (top frame) */
203 if ($show == 'form' && ! isset($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><label for="query">' . _("Search for") .
209 "</label></strong>\n", 'left', '',
210 'style="white-space: nowrap;" valign="middle" width="10%"' ) .
211 html_tag( 'td', '', 'left', '', '' ) .
212 addInput('query', $query, 28);
213
214 /* List all backends to allow the user to choose where to search */
215 if ($abook->numbackends > 1) {
216 echo '<strong><label for="backend">' . _("in") . '</label></strong>&nbsp;'."\n";
217 $selopts = array();
218 $selopts['-1'] = _("All address books");
219
220 $ret = $abook->get_backend_list();
221 while (list($undef,$v) = each($ret)) {
222 $selopts[$v->bnum] = $v->sname;
223 }
224 echo addSelect('backend', $selopts, '-1', TRUE);
225 } else {
226 echo addHidden('backend', '-1');
227 }
228
229 echo '</td></tr>' .
230 html_tag( 'tr',
231 html_tag( 'td', '', 'left' ) .
232 html_tag( 'td',
233 '<input type="submit" value="' . _("Search") . '" name="show" />' .
234 '&nbsp;|&nbsp;<input type="submit" value="' . _("List all") .
235 '" name="listall" />' . "\n" .
236 '&nbsp;|&nbsp;<input type="button" value="' . _("Close") .
237 '" onclick="parent.close();" />' . "\n" ,
238 'left' )
239 ) .
240 '</table></form>' . "\n";
241 } else {
242 /**
243 * List addresses (bottom frame)
244 * If listall is set, list all entries in selected backend.
245 * If $show is 'blank' (initial call of address book popup) - list
246 * personal address book.
247 */
248 if ($show == 'blank' || isset($listall)) {
249
250 if($backend != -1 || $show == 'blank') {
251 if ($show == 'blank') {
252 $backend = $abook->localbackend;
253 }
254 $res = $abook->list_addr($backend);
255
256 if(is_array($res)) {
257 usort($res,'alistcmp');
258 display_result($res, false);
259 } else {
260 echo html_tag( 'p', '<strong>' .
261 sprintf(_("Unable to list addresses from %s"),
262 $abook->backends[$backend]->sname) . '</strong>' ,
263 'center' ) . "\n";
264 }
265 } else {
266 $res = $abook->list_addr();
267 usort($res,'alistcmp');
268 display_result($res, true);
269 }
270
271 } elseif (!empty($query)) {
272 /* Do the search (listall is not set. query is set.)*/
273
274 if($backend == -1) {
275 $res = $abook->s_search($query);
276 } else {
277 $res = $abook->s_search($query, $backend);
278 }
279
280 if (!is_array($res)) {
281 echo html_tag( 'p', '<b><br />' .
282 _("Your search failed with the following error(s)") .
283 ':<br />' . nl2br(htmlspecialchars($abook->error)) . "</b>\n" ,
284 'center' );
285 } elseif (sizeof($res) == 0) {
286 echo html_tag( 'p', '<br /><b>' .
287 _("No persons matching your search were found") . "</b>\n" ,
288 'center' );
289 } else {
290 display_result($res);
291 }
292 } else {
293 /**
294 * listall is not set, query is not set or empty.
295 * User hit search button without entering search expression.
296 */
297 echo html_tag( 'p', '<br /><b>' . _("Nothing to search") . "</b>\n",'center' );
298 }
299 }
300 $oTemplate->display('footer.tpl');
301 ?>