6 * Copyright (c) 1999-2003 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!
18 /* Path for SquirrelMail required files. */
19 define('SM_PATH','../');
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/html.php');
26 /* lets get the global vars we may need */
27 $key = $_COOKIE['key'];
28 $username = $_SESSION['username'];
29 $onetimepad = $_SESSION['onetimepad'];
30 $base_uri = $_SESSION['base_uri'];
32 sqgetGlobalVar('show' , $show);
33 if ( isset($_POST['query']) ) {
34 $query = $_POST['query'];
36 if ( isset($_POST['listall']) ) {
37 $listall = $_POST['listall'];
39 if ( isset($_POST['backend'] ) ) {
40 $backend = $_POST['backend'];
43 /* Function to include JavaScript code */
44 function insert_javascript() {
46 <SCRIPT LANGUAGE
="Javascript"><!--
48 function to_and_close($addr) {
53 function to_address($addr) {
55 var pwintype
= typeof parent
.opener
.document
.compose
;
57 $addr = $addr.replace(/ {1,35}$
/, "");
59 if (pwintype
!= "undefined") {
60 if (parent
.opener
.document
.compose
.send_to
.value
) {
62 parent
.opener
.document
.compose
.send_to
.value
=
63 parent
.opener
.document
.compose
.send_to
.value +
", " +
$addr;
65 parent
.opener
.document
.compose
.send_to
.value
= $addr;
70 function cc_address($addr) {
72 var pwintype
= typeof parent
.opener
.document
.compose
;
74 $addr = $addr.replace(/ {1,35}$
/, "");
76 if (pwintype
!= "undefined") {
77 if (parent
.opener
.document
.compose
.send_to_cc
.value
) {
79 parent
.opener
.document
.compose
.send_to_cc
.value
=
80 parent
.opener
.document
.compose
.send_to_cc
.value +
", " +
$addr;
82 parent
.opener
.document
.compose
.send_to_cc
.value
= $addr;
87 function bcc_address($addr) {
89 var pwintype
= typeof parent
.opener
.document
.compose
;
91 $addr = $addr.replace(/ {1,35}$
/, "");
93 if (pwintype
!= "undefined") {
94 if (parent
.opener
.document
.compose
.send_to_bcc
.value
) {
96 parent
.opener
.document
.compose
.send_to_bcc
.value
=
97 parent
.opener
.document
.compose
.send_to_bcc
.value +
", " +
$addr;
99 parent
.opener
.document
.compose
.send_to_bcc
.value
= $addr;
106 } /* End of included JavaScript */
109 /* List search results */
110 function display_result($res, $includesource = true) {
113 if(sizeof($res) <= 0) return;
118 echo html_tag( 'table', '', 'center', '', 'border="0" width="98%"' ) .
119 html_tag( 'tr', '', '', $color[9] ) .
120 html_tag( 'th', ' ', 'left' ) .
121 html_tag( 'th', ' ' . _("Name"), 'left' ) .
122 html_tag( 'th', ' ' . _("E-mail"), 'left' ) .
123 html_tag( 'th', ' ' . _("Info"), 'left' );
125 if ($includesource) {
126 echo html_tag( 'th', ' ' . _("Source"), 'left', 'width="10%"' );
130 while (list($undef, $row) = each($res)) {
132 $email = htmlspecialchars(addcslashes(AddressBook
::full_address($row), "'"), ENT_QUOTES
);
133 if ($line %
2) { $tr_bgcolor = $color[0]; }
134 echo html_tag( 'tr', '', '', $tr_bgcolor, 'nowrap' ) .
136 '<small><a href="javascript:to_address(' .
137 "'" . $email . "');\">To</A> | " .
138 '<a href="javascript:cc_address(' .
139 "'" . $email . "');\">Cc</A> | " .
140 '<a href="javascript:bcc_address(' .
141 "'" . $email . "');\">Bcc</A></small>",
142 'center', '', 'valign="top" width="5%" nowrap' ) .
143 html_tag( 'td', ' ' . htmlspecialchars($row['name']), 'left', '', 'valign="top" nowrap' ) .
144 html_tag( 'td', ' ' .
145 '<a href="javascript:to_and_close(' .
146 "'" . $email . "');\">" . htmlspecialchars($row['email']) . '</A>'
147 , 'left', '', 'valign="top"' ) .
148 html_tag( 'td', htmlspecialchars($row['label']), 'left', '', 'valign="top" nowrap' );
149 if ($includesource) {
150 echo html_tag( 'td', ' ' . $row['source'], 'left', '', 'valign="top" nowrap' );
159 /* ================= End of functions ================= */
161 require_once('../functions/strings.php');
162 require_once('../functions/addressbook.php');
166 /* Initialize vars */
167 if (!isset($query)) { $query = ''; }
168 if (!isset($show)) { $show = ''; }
169 if (!isset($backend)) { $backend = ''; }
171 /* Choose correct colors for top and bottom frame */
172 if ($show == 'form' && !isset($listall)) {
173 echo '<BODY TEXT="' . $color[6] . '" BGCOLOR="' . $color[3] . '" ' .
174 'LINK="' . $color[6] . '" VLINK="' . $color[6] . '" ' .
175 'ALINK="' . $color[6] . '" ' .
176 'OnLoad="document.sform.query.focus();">';
178 echo '<BODY TEXT="' . $color[8] . '" BGCOLOR="' . $color[4] . '" ' .
179 'LINK="' . $color[7] . '" VLINK="' . $color[7] . '" ' .
180 'ALINK="' . $color[7] . "\">\n";
184 if (empty($query) && empty($show) && empty($listall)) {
185 echo html_tag( 'p', '<br>' .
186 _("No persons matching your search was found"),
188 "\n</BODY></HTML>\n",
192 /* Initialize addressbook */
193 $abook = addressbook_init();
195 /* Create search form */
196 if ($show == 'form' && empty($listall)) {
197 echo '<FORM NAME=sform TARGET=abookres ACTION="addrbook_search.php'.
198 '" METHOD="POST">' . "\n" .
199 html_tag( 'table', '', '', '', 'border="0" width="100%" height="100%"' ) .
201 html_tag( 'td', ' <strong>' . _("Search for") . "</strong>\n", 'left', '', 'nowrap valign="middle" width="10%"' ) .
202 html_tag( 'td', '', 'left', '', '' ) .
203 '<INPUT TYPE=text NAME=query VALUE="' . htmlspecialchars($query) .
206 /* List all backends to allow the user to choose where to search */
207 if ($abook->numbackends
> 1) {
208 echo '<STRONG>' . _("in") . '</STRONG> <SELECT NAME=backend>'."\n".
209 '<OPTION VALUE=-1 SELECTED>' . _("All address books") . "\n";
210 $ret = $abook->get_backend_list();
211 while (list($undef,$v) = each($ret)) {
212 echo '<OPTION VALUE=' . $v->bnum
. '>' . $v->sname
. "\n";
216 echo '<INPUT TYPE=hidden NAME=backend VALUE=-1>' . "\n";
221 html_tag( 'td', '', 'left' ) .
223 '<INPUT TYPE=submit VALUE="' . _("Search") . '" NAME=show>' .
224 ' | <INPUT TYPE=submit VALUE="' . _("List all") .
225 '" NAME=listall>' . "\n" .
226 ' | <INPUT TYPE=button VALUE="' . _("Close") .
227 '" onclick="parent.close();">' . "\n" ,
230 '</table></form>' . "\n";
233 /* Show personal addressbook */
234 if ($show == 'blank' && empty($listall)) {
236 if($backend != -1 ||
$show == 'blank') {
237 if ($show == 'blank') {
238 $backend = $abook->localbackend
;
240 $res = $abook->list_addr($backend);
243 usort($res,'alistcmp');
244 display_result($res, false);
246 echo html_tag( 'p', '<strong>' .
247 sprintf(_("Unable to list addresses from %s"),
248 $abook->backends
[$backend]->sname
) . '</strong>' ,
252 $res = $abook->list_addr();
253 usort($res,'alistcmp');
254 display_result($res, true);
258 if( !empty( $listall ) ){
263 if (!empty($query)) {
266 $res = $abook->s_search($query);
268 $res = $abook->s_search($query, $backend);
271 if (!is_array($res)) {
272 echo html_tag( 'p', '<b><br>' .
273 _("Your search failed with the following error(s)") .
274 ':<br>' . $abook->error
. "</b>\n" ,
276 "\n</BODY></HTML>\n";
280 if (sizeof($res) == 0) {
281 echo html_tag( 'p', '<br><b>' .
282 _("No persons matching your search was found") . "</b>\n" ,
284 "\n</BODY></HTML>\n";
288 display_result($res);
294 echo "</BODY></HTML>\n";