Getting ready for 1.2.0 release.
[squirrelmail.git] / src / addrbook_search.php
CommitLineData
5100704d 1<?php
895905c0 2
35586184 3/**
4 * addrbook_search.php
5 *
15e6162e 6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
35586184 7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * Handle addressbook searching in the popup window.
10 *
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!
14 *
15 * $Id$
16 */
17
18/*****************************************************************/
19/*** THIS FILE NEEDS TO HAVE ITS FORMATTING FIXED!!! ***/
20/*** PLEASE DO SO AND REMOVE THIS COMMENT SECTION. ***/
21/*** + Base level indent should begin at left margin, as ***/
22/*** the require_once below looks. ***/
23/*** + All identation should consist of four space blocks ***/
24/*** + Tab characters are evil. ***/
25/*** + all comments should use "slash-star ... star-slash" ***/
26/*** style -- no pound characters, no slash-slash style ***/
27/*** + FLOW CONTROL STATEMENTS (if, while, etc) SHOULD ***/
28/*** ALWAYS USE { AND } CHARACTERS!!! ***/
29/*** + Please use ' instead of ", when possible. Note " ***/
30/*** should always be used in _( ) function calls. ***/
31/*** Thank you for your help making the SM code more readable. ***/
32/*****************************************************************/
33
34require_once('../src/validate.php');
f740c049 35
2f73dc15 36 // Function to include JavaScript code
37 function insert_javascript() {
5100704d 38?>
39<SCRIPT LANGUAGE="Javascript"><!--
40
87332e20 41function to_and_close($addr) {
42 to_address($addr);
43 parent.close();
44}
45
5100704d 46function to_address($addr) {
47 var prefix = "";
48 var pwintype = typeof parent.opener.document.compose;
49
dfadb553 50 $addr = $addr.replace(/ {1,35}$/, "");
51
5100704d 52 if(pwintype != "undefined" ) {
53 if ( parent.opener.document.compose.send_to.value ) {
54 prefix = ", ";
9487c2ff 55 parent.opener.document.compose.send_to.value =
dfadb553 56 parent.opener.document.compose.send_to.value + ", " + $addr;
9487c2ff 57
5100704d 58 } else {
59 parent.opener.document.compose.send_to.value = $addr;
60 }
61 }
62}
63
64function cc_address($addr) {
65 var prefix = "";
66 var pwintype = typeof parent.opener.document.compose;
67
dfadb553 68 $addr = $addr.replace(/ {1,35}$/, "");
69
5100704d 70 if(pwintype != "undefined" ) {
71 if ( parent.opener.document.compose.send_to_cc.value ) {
72 prefix = ", ";
9487c2ff 73 parent.opener.document.compose.send_to_cc.value =
74 parent.opener.document.compose.send_to_cc.value + ", " + $addr;
5100704d 75 } else {
76 parent.opener.document.compose.send_to_cc.value = $addr;
77 }
78 }
79}
80
81function bcc_address($addr) {
82 var prefix = "";
83 var pwintype = typeof parent.opener.document.compose;
9487c2ff 84
dfadb553 85 $addr = $addr.replace(/ {1,35}$/, "");
5100704d 86
87 if(pwintype != "undefined" ) {
a4fe507d 88 if ( parent.opener.document.compose.send_to_bcc.value ) {
5100704d 89 prefix = ", ";
9487c2ff 90 parent.opener.document.compose.send_to_bcc.value =
91 parent.opener.document.compose.send_to_bcc.value + ", " + $addr;
5100704d 92 } else {
a4fe507d 93 parent.opener.document.compose.send_to_bcc.value = $addr;
5100704d 94 }
95 }
96}
97
98// --></SCRIPT>
99
9487c2ff 100<?php
2f73dc15 101 } // End of included JavaScript
102
103
9487c2ff 104 // List search results
105 function display_result($res, $includesource = true) {
106 global $color;
107
108 if(sizeof($res) <= 0) return;
109
110 insert_javascript();
111
112 $line = 0;
113 echo '<TABLE BORDER="0" WIDTH="98%" ALIGN=center>';
114 printf("<TR BGCOLOR=\"$color[9]\"><TH ALIGN=left>&nbsp;".
115 "<TH ALIGN=left>&nbsp;%s<TH ALIGN=left>&nbsp;%s".
116 "<TH ALIGN=left>&nbsp;%s",
117 _("Name"), _("E-mail"), _("Info"));
118
119 if($includesource)
120 printf("<TH ALIGN=left WIDTH=\"10%%\">&nbsp;%s", _("Source"));
121
122 echo "</TR>\n";
123
124 while(list($undef, $row) = each($res)) {
125 printf("<tr%s nowrap><td valign=top nowrap align=center width=\"5%%\">".
126 "<small><a href=\"javascript:to_address('%s');\">To</A> | ".
127 "<a href=\"javascript:cc_address('%s');\">Cc</A> | ".
128 "<a href=\"javascript:bcc_address('%s');\">Bcc</A></small>".
129 "<td nowrap valign=top>&nbsp;%s&nbsp;<td nowrap valign=top>".
130 "&nbsp;<a href=\"javascript:to_and_close('%s');\">%s</A>&nbsp;".
131 "<td valign=top>&nbsp;%s&nbsp;",
132 ($line % 2) ? " bgcolor=\"$color[0]\"" : "",
133 $row["email"], $row["email"], $row["email"],
134 $row["name"], $row["email"], $row["email"],
135 $row["label"]);
136
137 if($includesource)
138 printf("<td nowrap valign=top>&nbsp;%s", $row["source"]);
139
140 echo "</TR>\n";
141 $line++;
142 }
143 echo '</TABLE>';
144 }
145
146 /* ================= End of functions ================= */
147
148 require_once('../functions/array.php');
149 require_once('../functions/strings.php');
150 require_once('../functions/addressbook.php');
151
152 displayHtmlHeader();
153
154 // Initialize vars
155 if(!isset($query)) $query = "";
156 if(!isset($show)) $show = "";
157
158 // Choose correct colors for top and bottom frame
159 if($show == 'form') {
160 echo "<BODY BGCOLOR=\"$color[3]\" TEXT=\"$color[6]\" ";
161 echo "LINK=\"$color[6]\" VLINK=\"$color[6]\" ALINK=\"$color[6]\" ";
162 echo 'OnLoad="document.sform.query.focus();">';
163 } else {
164 echo "<BODY TEXT=\"$color[8]\" BGCOLOR=\"$color[4]\" ";
165 echo "LINK=\"$color[7]\" VLINK=\"$color[7]\" ALINK=\"$color[7]\">\n";
166 }
167
168 // Empty search
169 if(empty($query) && empty($show) && empty($listall)) {
170 printf("<P ALIGN=center><BR>%s</P>\n</BODY></HTML>\n",
171 _("No persons matching your search was found"));
2f73dc15 172 exit;
9487c2ff 173 }
174
175 // Initialize addressbook
176 $abook = addressbook_init();
177
178 // Create search form
179 if($show == 'form') {
180 echo "<FORM NAME=sform TARGET=abookres ACTION=\"$PHP_SELF\" METHOD=\"POST\">\n";
181 echo '<TABLE BORDER="0" WIDTH="100%" HEIGHT="100%">';
182 echo "<TR><TD NOWRAP VALIGN=middle>\n";
183 printf(" <STRONG>%s</STRONG>\n", _("Search for"));
184 printf(" <INPUT TYPE=text NAME=query VALUE=\"%s\" SIZE=26>\n",
185 htmlspecialchars($query));
186
187 // List all backends to allow the user to choose where to search
188 if($abook->numbackends > 1) {
189 printf("<STRONG>%s</STRONG>&nbsp;<SELECT NAME=backend>\n",
190 _("in"));
191 printf("<OPTION VALUE=-1 SELECTED>%s\n",
192 _("All address books"));
193 $ret = $abook->get_backend_list();
194 while(list($undef,$v) = each($ret))
195 printf("<OPTION VALUE=%d>%s\n", $v->bnum, $v->sname);
196 print "</SELECT>\n";
197 } else {
198 print "<INPUT TYPE=hidden NAME=backend VALUE=-1>\n";
199 }
200
201 printf("<INPUT TYPE=submit VALUE=\"%s\">",
202 _("Search"));
203 printf("&nbsp;|&nbsp;<INPUT TYPE=submit VALUE=\"%s\" NAME=listall>\n",
2f73dc15 204 _("List all"));
9487c2ff 205 print "</TD><TD ALIGN=right>\n";
206 printf("<INPUT TYPE=button VALUE=\"%s\" onclick=\"parent.close();\">\n",
2f73dc15 207 _("Close window"));
9487c2ff 208 print "</TD></TR></TABLE></FORM>\n";
209 } else
210
211 // Show personal addressbook
212 if($show == 'blank' || !empty($listall)) {
213
214 if($backend != -1 || $show == 'blank') {
215 if($show == 'blank')
216 $backend = $abook->localbackend;
217
218 $res = $abook->list_addr($backend);
219
220 if(is_array($res)) {
221 display_result($res, false);
222 } else {
223 printf("<P ALIGN=center><STRONG>"._("Unable to list addresses from %s").
224 "</STRONG></P>\n", $abook->backends[$backend]->sname);
225 }
226
227 } else {
228 $res = $abook->list_addr();
229 display_result($res, true);
230 }
231
232 } else
233
234 // Do the search
235 if(!empty($query) && empty($listall)) {
236
237 if($backend == -1) {
238 $res = $abook->s_search($query);
239 } else {
240 $res = $abook->s_search($query, $backend);
241 }
242
243 if(!is_array($res)) {
244 printf("<P ALIGN=center><B><BR>%s:<br>%s</B></P>\n</BODY></HTML>\n",
245 _("Your search failed with the following error(s)"),
246 $abook->error);
247 exit;
248 }
249
250 if(sizeof($res) == 0) {
251 printf("<P ALIGN=center><BR><B>%s.</B></P>\n</BODY></HTML>\n",
252 _("No persons matching your search was found"));
253 exit;
254 }
255
256 display_result($res);
257 }
258
259 echo "</BODY></HTML>\n";
260
35586184 261?>