6 * Copyright (c) 1999-2005 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * Handle addressbook searching in the popup window.
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!
16 * @package squirrelmail
17 * @subpackage addressbook
21 * Path for SquirrelMail required files.
24 define('SM_PATH','../');
26 /** SquirrelMail required files. */
27 require_once(SM_PATH
. 'include/validate.php');
28 include_once(SM_PATH
. 'functions/strings.php');
29 include_once(SM_PATH
. 'functions/global.php');
30 include_once(SM_PATH
. 'functions/html.php');
31 include_once(SM_PATH
. 'functions/forms.php');
32 include_once(SM_PATH
. 'functions/addressbook.php');
35 * Function to include JavaScript code
38 function insert_javascript() {
40 <script language
="Javascript"><!--
42 function to_and_close($addr) {
47 function to_address($addr) {
49 var pwintype
= typeof parent
.opener
.document
.compose
;
51 $addr = $addr.replace(/ {1,35}$
/, "");
53 if (pwintype
!= "undefined") {
54 if (parent
.opener
.document
.compose
.send_to
.value
) {
56 parent
.opener
.document
.compose
.send_to
.value
=
57 parent
.opener
.document
.compose
.send_to
.value +
", " +
$addr;
59 parent
.opener
.document
.compose
.send_to
.value
= $addr;
64 function cc_address($addr) {
66 var pwintype
= typeof parent
.opener
.document
.compose
;
68 $addr = $addr.replace(/ {1,35}$
/, "");
70 if (pwintype
!= "undefined") {
71 if (parent
.opener
.document
.compose
.send_to_cc
.value
) {
73 parent
.opener
.document
.compose
.send_to_cc
.value
=
74 parent
.opener
.document
.compose
.send_to_cc
.value +
", " +
$addr;
76 parent
.opener
.document
.compose
.send_to_cc
.value
= $addr;
81 function bcc_address($addr) {
83 var pwintype
= typeof parent
.opener
.document
.compose
;
85 $addr = $addr.replace(/ {1,35}$
/, "");
87 if (pwintype
!= "undefined") {
88 if (parent
.opener
.document
.compose
.send_to_bcc
.value
) {
90 parent
.opener
.document
.compose
.send_to_bcc
.value
=
91 parent
.opener
.document
.compose
.send_to_bcc
.value +
", " +
$addr;
93 parent
.opener
.document
.compose
.send_to_bcc
.value
= $addr;
100 } /* End of included JavaScript */
104 * List search results
105 * @param array $res Array of search results
106 * @param bool $includesource [Default=true]
109 function display_result($res, $includesource = true) {
112 if(sizeof($res) <= 0) return;
117 echo html_tag( 'table', '', 'center', '', 'border="0" width="98%"' ) .
118 html_tag( 'tr', '', '', $color[9] ) .
119 html_tag( 'th', ' ', 'left' ) .
120 html_tag( 'th', ' ' . _("Name"), 'left' ) .
121 html_tag( 'th', ' ' . _("E-mail"), 'left' ) .
122 html_tag( 'th', ' ' . _("Info"), 'left' );
124 if ($includesource) {
125 echo html_tag( 'th', ' ' . _("Source"), 'left', '', 'width="10%"' );
129 while (list($undef, $row) = each($res)) {
130 $email = htmlspecialchars(addcslashes(AddressBook
::full_address($row), "'"), ENT_QUOTES
);
132 $tr_bgcolor = $color[12];
134 $tr_bgcolor = $color[4];
136 echo html_tag( 'tr', '', '', $tr_bgcolor, 'style="white-space: nowrap;"' ) .
138 '<small><a href="javascript:to_address(' .
139 "'" . $email . "');\">"._("To")."</a> | " .
140 '<a href="javascript:cc_address(' .
141 "'" . $email . "');\">"._("Cc")."</a> | " .
142 '<a href="javascript:bcc_address(' .
143 "'" . $email . "');\">"._("Bcc")."</a></small>",
144 'center', '', 'valign="top" width="5%" style="white-space: nowrap;"' ) .
145 html_tag( 'td', ' ' . htmlspecialchars($row['name']), 'left', '', 'valign="top" style="white-space: nowrap;"' ) .
146 html_tag( 'td', ' ' .
147 '<a href="javascript:to_and_close(' .
148 "'" . $email . "');\">" . htmlspecialchars($row['email']) . '</a>'
149 , 'left', '', 'valign="top"' ) .
150 html_tag( 'td', htmlspecialchars($row['label']), 'left', '', 'valign="top" style="white-space: nowrap;"' );
151 if ($includesource) {
152 echo html_tag( 'td', ' ' . $row['source'], 'left', '', 'valign="top" style="white-space: nowrap;"' );
161 /* ================= End of functions ================= */
163 /** lets get the global vars we may need */
164 sqgetGlobalVar('key', $key, SQ_COOKIE
);
165 sqgetGlobalVar('username', $username, SQ_SESSION
);
166 sqgetGlobalVar('onetimepad',$onetimepad, SQ_SESSION
);
167 sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION
);
169 if (! sqgetGlobalVar('show' , $show)) {
172 if (! sqgetGlobalVar('query', $query, SQ_POST
)) {
175 if (! sqgetGlobalVar('listall', $listall, SQ_POST
)) {
178 if (! sqgetGlobalVar('backend', $backend, SQ_POST
)) {
184 /** set correct value of $default_charset */
185 global $default_charset;
188 /* Choose correct colors for top and bottom frame */
189 if ($show == 'form' && !isset($listall)) {
190 echo '<body text="' . $color[6] . '" bgcolor="' . $color[3] . '" ' .
191 'link="' . $color[6] . '" vlink="' . $color[6] . '" ' .
192 'alink="' . $color[6] . '" ' .
193 'OnLoad="document.sform.query.focus();">';
195 echo '<body text="' . $color[8] . '" bgcolor="' . $color[4] . '" ' .
196 'link="' . $color[7] . '" vlink="' . $color[7] . '" ' .
197 'alink="' . $color[7] . "\">\n";
201 if (empty($query) && empty($show) && !isset($listall)) {
202 echo html_tag( 'p', '<br />' .
203 _("No persons matching your search were found"),
205 "\n</body></html>\n";
209 /* Initialize addressbook, show init errors only in bottom frame */
210 $showerr=($show=='form' ?
false : true);
211 $abook = addressbook_init($showerr);
213 /* Create search form (top frame) */
214 if ($show == 'form' && ! isset($listall)) {
215 echo '<form name="sform" target="abookres" action="addrbook_search.php'.
216 '" method="post">' . "\n" .
217 html_tag( 'table', '', '', '', 'border="0" width="100%" height="100%"' ) .
219 html_tag( 'td', ' <strong>' . _("Search for") . "</strong>\n", 'left', '', 'style="white-space: nowrap;" valign="middle" width="10%"' ) .
220 html_tag( 'td', '', 'left', '', '' ) .
221 addInput('query', $query, 28);
223 /* List all backends to allow the user to choose where to search */
224 if ($abook->numbackends
> 1) {
225 echo '<strong>' . _("in") . '</strong> '."\n";
227 $selopts['-1'] = _("All address books");
229 $ret = $abook->get_backend_list();
230 while (list($undef,$v) = each($ret)) {
231 $selopts[$v->bnum
] = $v->sname
;
233 echo addSelect('backend', $selopts, '-1', TRUE);
235 echo addHidden('backend', '-1');
240 html_tag( 'td', '', 'left' ) .
242 '<input type="submit" value="' . _("Search") . '" name="show" />' .
243 ' | <input type="submit" value="' . _("List all") .
244 '" name="listall" />' . "\n" .
245 ' | <input type="button" value="' . _("Close") .
246 '" onclick="parent.close();" />' . "\n" ,
249 '</table></form>' . "\n";
252 * List addresses (bottom frame)
253 * If listall is set, list all entries in selected backend.
254 * If $show is 'blank' (initial call of address book popup) - list
255 * personal address book.
257 if ($show == 'blank' ||
isset($listall)) {
259 if($backend != -1 ||
$show == 'blank') {
260 if ($show == 'blank') {
261 $backend = $abook->localbackend
;
263 $res = $abook->list_addr($backend);
266 usort($res,'alistcmp');
267 display_result($res, false);
269 echo html_tag( 'p', '<strong>' .
270 sprintf(_("Unable to list addresses from %s"),
271 $abook->backends
[$backend]->sname
) . '</strong>' ,
275 $res = $abook->list_addr();
276 usort($res,'alistcmp');
277 display_result($res, true);
280 } elseif (!empty($query)) {
281 /* Do the search (listall is not set. query is set.)*/
284 $res = $abook->s_search($query);
286 $res = $abook->s_search($query, $backend);
289 if (!is_array($res)) {
290 echo html_tag( 'p', '<b><br />' .
291 _("Your search failed with the following error(s)") .
292 ':<br />' . $abook->error
. "</b>\n" ,
294 "\n</body></html>\n";
298 if (sizeof($res) == 0) {
299 echo html_tag( 'p', '<br /><b>' .
300 _("No persons matching your search were found") . "</b>\n" ,
302 "\n</body></html>\n";
306 display_result($res);
309 * listall is not set, query is not set or empty.
310 * User hit search button without entering search expression.
312 echo html_tag( 'p', '<br /><b>' . _("Nothing to search") . "</b>\n",'center' );