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.
23 define('SM_PATH','../');
25 /** SquirrelMail required files. */
26 require_once(SM_PATH
. 'include/validate.php');
27 require_once(SM_PATH
. 'functions/strings.php');
28 require_once(SM_PATH
. 'functions/global.php');
29 require_once(SM_PATH
. 'functions/html.php');
30 require_once(SM_PATH
. 'functions/forms.php');
32 /** lets get the global vars we may need */
33 sqgetGlobalVar('key', $key, SQ_COOKIE
);
34 sqgetGlobalVar('username', $username, SQ_SESSION
);
35 sqgetGlobalVar('onetimepad',$onetimepad, SQ_SESSION
);
36 sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION
);
38 sqgetGlobalVar('show' , $show);
39 sqgetGlobalVar('query', $query, SQ_POST
);
40 sqgetGlobalVar('listall', $listall, SQ_POST
);
41 sqgetGlobalVar('backend', $backend, SQ_POST
);
44 * Function to include JavaScript code
47 function insert_javascript() {
49 <script language
="Javascript"><!--
51 function to_and_close($addr) {
56 function to_address($addr) {
58 var pwintype
= typeof parent
.opener
.document
.compose
;
60 $addr = $addr.replace(/ {1,35}$
/, "");
62 if (pwintype
!= "undefined") {
63 if (parent
.opener
.document
.compose
.send_to
.value
) {
65 parent
.opener
.document
.compose
.send_to
.value
=
66 parent
.opener
.document
.compose
.send_to
.value +
", " +
$addr;
68 parent
.opener
.document
.compose
.send_to
.value
= $addr;
73 function cc_address($addr) {
75 var pwintype
= typeof parent
.opener
.document
.compose
;
77 $addr = $addr.replace(/ {1,35}$
/, "");
79 if (pwintype
!= "undefined") {
80 if (parent
.opener
.document
.compose
.send_to_cc
.value
) {
82 parent
.opener
.document
.compose
.send_to_cc
.value
=
83 parent
.opener
.document
.compose
.send_to_cc
.value +
", " +
$addr;
85 parent
.opener
.document
.compose
.send_to_cc
.value
= $addr;
90 function bcc_address($addr) {
92 var pwintype
= typeof parent
.opener
.document
.compose
;
94 $addr = $addr.replace(/ {1,35}$
/, "");
96 if (pwintype
!= "undefined") {
97 if (parent
.opener
.document
.compose
.send_to_bcc
.value
) {
99 parent
.opener
.document
.compose
.send_to_bcc
.value
=
100 parent
.opener
.document
.compose
.send_to_bcc
.value +
", " +
$addr;
102 parent
.opener
.document
.compose
.send_to_bcc
.value
= $addr;
109 } /* End of included JavaScript */
113 * List search results
114 * @param array $res Array of search results
115 * @param bool $includesource [Default=true]
118 function display_result($res, $includesource = true) {
121 if(sizeof($res) <= 0) return;
126 echo html_tag( 'table', '', 'center', '', 'border="0" width="98%"' ) .
127 html_tag( 'tr', '', '', $color[9] ) .
128 html_tag( 'th', ' ', 'left' ) .
129 html_tag( 'th', ' ' . _("Name"), 'left' ) .
130 html_tag( 'th', ' ' . _("E-mail"), 'left' ) .
131 html_tag( 'th', ' ' . _("Info"), 'left' );
133 if ($includesource) {
134 echo html_tag( 'th', ' ' . _("Source"), 'left', 'width="10%"' );
138 while (list($undef, $row) = each($res)) {
139 $email = htmlspecialchars(addcslashes(AddressBook
::full_address($row), "'"), ENT_QUOTES
);
141 $tr_bgcolor = $color[12];
143 $tr_bgcolor = $color[4];
145 echo html_tag( 'tr', '', '', $tr_bgcolor, 'nowrap' ) .
147 '<small><a href="javascript:to_address(' .
148 "'" . $email . "');\">"._("To")."</a> | " .
149 '<a href="javascript:cc_address(' .
150 "'" . $email . "');\">"._("Cc")."</a> | " .
151 '<a href="javascript:bcc_address(' .
152 "'" . $email . "');\">"._("Bcc")."</a></small>",
153 'center', '', 'valign="top" width="5%" nowrap' ) .
154 html_tag( 'td', ' ' . htmlspecialchars($row['name']), 'left', '', 'valign="top" nowrap' ) .
155 html_tag( 'td', ' ' .
156 '<a href="javascript:to_and_close(' .
157 "'" . $email . "');\">" . htmlspecialchars($row['email']) . '</a>'
158 , 'left', '', 'valign="top"' ) .
159 html_tag( 'td', htmlspecialchars($row['label']), 'left', '', 'valign="top" nowrap' );
160 if ($includesource) {
161 echo html_tag( 'td', ' ' . $row['source'], 'left', '', 'valign="top" nowrap' );
170 /* ================= End of functions ================= */
172 require_once('../functions/strings.php');
173 require_once('../functions/addressbook.php');
177 /* Initialize vars */
178 if (!isset($query)) { $query = ''; }
179 if (!isset($show)) { $show = ''; }
180 if (!isset($backend)) { $backend = ''; }
182 /* Choose correct colors for top and bottom frame */
183 if ($show == 'form' && !isset($listall)) {
184 echo '<body text="' . $color[6] . '" bgcolor="' . $color[3] . '" ' .
185 'link="' . $color[6] . '" vlink="' . $color[6] . '" ' .
186 'alink="' . $color[6] . '" ' .
187 'OnLoad="document.sform.query.focus();">';
189 echo '<body text="' . $color[8] . '" bgcolor="' . $color[4] . '" ' .
190 'link="' . $color[7] . '" vlink="' . $color[7] . '" ' .
191 'alink="' . $color[7] . "\">\n";
195 if (empty($query) && empty($show) && empty($listall)) {
196 echo html_tag( 'p', '<br />' .
197 _("No persons matching your search were found"),
199 "\n</body></html>\n",
203 /* Initialize addressbook */
204 $abook = addressbook_init();
206 /* Create search form */
207 if ($show == 'form' && empty($listall)) {
208 echo '<form name="sform" target="abookres" action="addrbook_search.php'.
209 '" method="post">' . "\n" .
210 html_tag( 'table', '', '', '', 'border="0" width="100%" height="100%"' ) .
212 html_tag( 'td', ' <strong>' . _("Search for") . "</strong>\n", 'left', '', 'nowrap valign="middle" width="10%"' ) .
213 html_tag( 'td', '', 'left', '', '' ) .
214 addInput('query', $query, 28);
216 /* List all backends to allow the user to choose where to search */
217 if ($abook->numbackends
> 1) {
218 echo '<strong>' . _("in") . '</strong> '."\n".
219 $selopts['-1'] = _("All address books");
221 $ret = $abook->get_backend_list();
222 while (list($undef,$v) = each($ret)) {
223 $selopts[$v->bnum
] = $v->sname
;
225 echo addSelect('backend', $selopts, '-1', TRUE);
227 echo addHidden('backend', '-1');
232 html_tag( 'td', '', 'left' ) .
234 '<input type="submit" value="' . _("Search") . '" name="show" />' .
235 ' | <input type="submit" value="' . _("List all") .
236 '" name="listall" />' . "\n" .
237 ' | <input type="button" value="' . _("Close") .
238 '" onclick="parent.close();" />' . "\n" ,
241 '</table></form>' . "\n";
244 /* Show personal addressbook */
245 if ($show == 'blank' && empty($listall)) {
247 if($backend != -1 ||
$show == 'blank') {
248 if ($show == 'blank') {
249 $backend = $abook->localbackend
;
251 $res = $abook->list_addr($backend);
254 usort($res,'alistcmp');
255 display_result($res, false);
257 echo html_tag( 'p', '<strong>' .
258 sprintf(_("Unable to list addresses from %s"),
259 $abook->backends
[$backend]->sname
) . '</strong>' ,
263 $res = $abook->list_addr();
264 usort($res,'alistcmp');
265 display_result($res, true);
269 if( !empty( $listall ) ){
274 if (!empty($query)) {
277 $res = $abook->s_search($query);
279 $res = $abook->s_search($query, $backend);
282 if (!is_array($res)) {
283 echo html_tag( 'p', '<b><br />' .
284 _("Your search failed with the following error(s)") .
285 ':<br />' . $abook->error
. "</b>\n" ,
287 "\n</body></html>\n";
291 if (sizeof($res) == 0) {
292 echo html_tag( 'p', '<br /><b>' .
293 _("No persons matching your search were found") . "</b>\n" ,
295 "\n</body></html>\n";
299 display_result($res);