6 * Copyright (c) 1999-2004 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
20 * Path for SquirrelMail required files.
22 define('SM_PATH','../');
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');
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
);
37 sqgetGlobalVar('show' , $show);
38 sqgetGlobalVar('query', $query, SQ_POST
);
39 sqgetGlobalVar('listall', $listall, SQ_POST
);
40 sqgetGlobalVar('backend', $backend, SQ_POST
);
43 * Function to include JavaScript code
46 function insert_javascript() {
48 <SCRIPT LANGUAGE
="Javascript"><!--
50 function to_and_close($addr) {
55 function to_address($addr) {
57 var pwintype
= typeof parent
.opener
.document
.compose
;
59 $addr = $addr.replace(/ {1,35}$
/, "");
61 if (pwintype
!= "undefined") {
62 if (parent
.opener
.document
.compose
.send_to
.value
) {
64 parent
.opener
.document
.compose
.send_to
.value
=
65 parent
.opener
.document
.compose
.send_to
.value +
", " +
$addr;
67 parent
.opener
.document
.compose
.send_to
.value
= $addr;
72 function cc_address($addr) {
74 var pwintype
= typeof parent
.opener
.document
.compose
;
76 $addr = $addr.replace(/ {1,35}$
/, "");
78 if (pwintype
!= "undefined") {
79 if (parent
.opener
.document
.compose
.send_to_cc
.value
) {
81 parent
.opener
.document
.compose
.send_to_cc
.value
=
82 parent
.opener
.document
.compose
.send_to_cc
.value +
", " +
$addr;
84 parent
.opener
.document
.compose
.send_to_cc
.value
= $addr;
89 function bcc_address($addr) {
91 var pwintype
= typeof parent
.opener
.document
.compose
;
93 $addr = $addr.replace(/ {1,35}$
/, "");
95 if (pwintype
!= "undefined") {
96 if (parent
.opener
.document
.compose
.send_to_bcc
.value
) {
98 parent
.opener
.document
.compose
.send_to_bcc
.value
=
99 parent
.opener
.document
.compose
.send_to_bcc
.value +
", " +
$addr;
101 parent
.opener
.document
.compose
.send_to_bcc
.value
= $addr;
108 } /* End of included JavaScript */
112 * List search results
113 * @param array $res Array of search results
114 * @param bool $includesource [Default=true]
117 function display_result($res, $includesource = true) {
120 if(sizeof($res) <= 0) return;
125 echo html_tag( 'table', '', 'center', '', 'border="0" width="98%"' ) .
126 html_tag( 'tr', '', '', $color[9] ) .
127 html_tag( 'th', ' ', 'left' ) .
128 html_tag( 'th', ' ' . _("Name"), 'left' ) .
129 html_tag( 'th', ' ' . _("E-mail"), 'left' ) .
130 html_tag( 'th', ' ' . _("Info"), 'left' );
132 if ($includesource) {
133 echo html_tag( 'th', ' ' . _("Source"), 'left', 'width="10%"' );
137 while (list($undef, $row) = each($res)) {
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' ) .
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', ' ' . htmlspecialchars($row['name']), 'left', '', 'valign="top" nowrap' ) .
151 html_tag( 'td', ' ' .
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', ' ' . $row['source'], 'left', '', 'valign="top" nowrap' );
166 /* ================= End of functions ================= */
168 require_once('../functions/strings.php');
169 require_once('../functions/addressbook.php');
173 /* Initialize vars */
174 if (!isset($query)) { $query = ''; }
175 if (!isset($show)) { $show = ''; }
176 if (!isset($backend)) { $backend = ''; }
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();">';
185 echo '<BODY TEXT="' . $color[8] . '" BGCOLOR="' . $color[4] . '" ' .
186 'LINK="' . $color[7] . '" VLINK="' . $color[7] . '" ' .
187 'ALINK="' . $color[7] . "\">\n";
191 if (empty($query) && empty($show) && empty($listall)) {
192 echo html_tag( 'p', '<br>' .
193 _("No persons matching your search were found"),
195 "\n</BODY></HTML>\n",
199 /* Initialize addressbook */
200 $abook = addressbook_init();
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%"' ) .
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);
212 /* List all backends to allow the user to choose where to search */
213 if ($abook->numbackends
> 1) {
214 echo '<STRONG>' . _("in") . '</STRONG> '."\n".
215 $selopts['-1'] = _("All address books");
217 $ret = $abook->get_backend_list();
218 while (list($undef,$v) = each($ret)) {
219 $selopts[$v->bnum
] = $v->sname
;
221 echo addSelect('backend', $selopts, '-1', TRUE);
223 echo addHidden('backend', '-1');
228 html_tag( 'td', '', 'left' ) .
230 '<INPUT TYPE=submit VALUE="' . _("Search") . '" NAME=show>' .
231 ' | <INPUT TYPE=submit VALUE="' . _("List all") .
232 '" NAME=listall>' . "\n" .
233 ' | <INPUT TYPE=button VALUE="' . _("Close") .
234 '" onclick="parent.close();">' . "\n" ,
237 '</table></form>' . "\n";
240 /* Show personal addressbook */
241 if ($show == 'blank' && empty($listall)) {
243 if($backend != -1 ||
$show == 'blank') {
244 if ($show == 'blank') {
245 $backend = $abook->localbackend
;
247 $res = $abook->list_addr($backend);
250 usort($res,'alistcmp');
251 display_result($res, false);
253 echo html_tag( 'p', '<strong>' .
254 sprintf(_("Unable to list addresses from %s"),
255 $abook->backends
[$backend]->sname
) . '</strong>' ,
259 $res = $abook->list_addr();
260 usort($res,'alistcmp');
261 display_result($res, true);
265 if( !empty( $listall ) ){
270 if (!empty($query)) {
273 $res = $abook->s_search($query);
275 $res = $abook->s_search($query, $backend);
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" ,
283 "\n</BODY></HTML>\n";
287 if (sizeof($res) == 0) {
288 echo html_tag( 'p', '<br><b>' .
289 _("No persons matching your search was found") . "</b>\n" ,
291 "\n</BODY></HTML>\n";
295 display_result($res);
301 echo "</BODY></HTML>\n";