Removed dependency on IMAP. Uses auth.php instead.
[squirrelmail.git] / src / addrbook_search.php
CommitLineData
5100704d 1<?php
2 /**
3 ** addrbook_search.php
4 **
5 ** Handle addressbook searching in the popup window.
6 **
2c5d4cd4 7 ** NOTE: A lot of this code is similar to the code in
8 ** addrbook_search_html.html -- If you change one,
9 ** change the other one too!
5100704d 10 **/
11
2f73dc15 12 // Function to include JavaScript code
13 function insert_javascript() {
5100704d 14?>
15<SCRIPT LANGUAGE="Javascript"><!--
16
87332e20 17function to_and_close($addr) {
18 to_address($addr);
19 parent.close();
20}
21
5100704d 22function to_address($addr) {
23 var prefix = "";
24 var pwintype = typeof parent.opener.document.compose;
25
dfadb553 26 $addr = $addr.replace(/ {1,35}$/, "");
27
5100704d 28 if(pwintype != "undefined" ) {
29 if ( parent.opener.document.compose.send_to.value ) {
30 prefix = ", ";
31 parent.opener.document.compose.send_to.value =
dfadb553 32 parent.opener.document.compose.send_to.value + ", " + $addr;
33
5100704d 34 } else {
35 parent.opener.document.compose.send_to.value = $addr;
36 }
37 }
38}
39
40function cc_address($addr) {
41 var prefix = "";
42 var pwintype = typeof parent.opener.document.compose;
43
dfadb553 44 $addr = $addr.replace(/ {1,35}$/, "");
45
5100704d 46 if(pwintype != "undefined" ) {
47 if ( parent.opener.document.compose.send_to_cc.value ) {
48 prefix = ", ";
49 parent.opener.document.compose.send_to_cc.value =
50 parent.opener.document.compose.send_to_cc.value + ", " + $addr;
51 } else {
52 parent.opener.document.compose.send_to_cc.value = $addr;
53 }
54 }
55}
56
57function bcc_address($addr) {
58 var prefix = "";
59 var pwintype = typeof parent.opener.document.compose;
dfadb553 60
61 $addr = $addr.replace(/ {1,35}$/, "");
5100704d 62
63 if(pwintype != "undefined" ) {
a4fe507d 64 if ( parent.opener.document.compose.send_to_bcc.value ) {
5100704d 65 prefix = ", ";
a4fe507d 66 parent.opener.document.compose.send_to_bcc.value =
67 parent.opener.document.compose.send_to_bcc.value + ", " + $addr;
5100704d 68 } else {
a4fe507d 69 parent.opener.document.compose.send_to_bcc.value = $addr;
5100704d 70 }
71 }
72}
73
74// --></SCRIPT>
75
76<?php
2f73dc15 77 } // End of included JavaScript
78
79
80 // List search results
81 function display_result($res, $includesource = true) {
82 global $color;
83
84 if(sizeof($res) <= 0) return;
85
86 insert_javascript();
87
88 $line = 0;
89 print "<TABLE BORDER=0 WIDTH=\"98%\" ALIGN=center>";
90 printf("<TR BGCOLOR=\"$color[9]\"><TH ALIGN=left>&nbsp;".
91 "<TH ALIGN=left>&nbsp;%s<TH ALIGN=left>&nbsp;%s".
92 "<TH ALIGN=left>&nbsp;%s",
93 _("Name"), _("E-mail"), _("Info"));
94
95 if($includesource)
96 printf("<TH ALIGN=left WIDTH=\"10%%\">&nbsp;%s", _("Source"));
97
98 print "</TR>\n";
99
100 while(list($key, $row) = each($res)) {
2c5d4cd4 101 printf("<tr%s nowrap><td valign=top nowrap align=center width=\"5%%\">".
2f73dc15 102 "<small><a href=\"javascript:to_address('%s');\">To</A> | ".
103 "<a href=\"javascript:cc_address('%s');\">Cc</A> | ".
104 "<a href=\"javascript:bcc_address('%s');\">Bcc</A></small>".
2c5d4cd4 105 "<td nowrap valign=top>&nbsp;%s&nbsp;<td nowrap valign=top>".
106 "&nbsp;<a href=\"javascript:to_and_close('%s');\">%s</A>&nbsp;".
107 "<td valign=top>&nbsp;%s&nbsp;",
2f73dc15 108 ($line % 2) ? " bgcolor=\"$color[0]\"" : "",
109 $row["email"], $row["email"], $row["email"],
110 $row["name"], $row["email"], $row["email"],
111 $row["label"]);
112
113 if($includesource)
2c5d4cd4 114 printf("<td nowrap valign=top>&nbsp;%s", $row["source"]);
2f73dc15 115
116 print "</TR>\n";
117 $line++;
118 }
119 print "</TABLE>";
120 }
121
122 /* ================= End of functions ================= */
123
124 session_start();
125
126 if(!isset($logged_in)) {
127 echo _("You must login first.");
128 exit;
129 }
130 if(!isset($username) || !isset($key)) {
131 echo _("You need a valid user and password to access this page!");
132 exit;
133 }
134
135 if (!isset($config_php))
136 include("../config/config.php");
137 if (!isset($array_php))
138 include("../functions/array.php");
139 if (!isset($strings_php))
140 include("../functions/strings.php");
141 if (!isset($imap_php))
142 include("../functions/imap.php");
143 if (!isset($page_header_php))
144 include("../functions/page_header.php");
145 if (!isset($addressbook_php))
146 include("../functions/addressbook.php");
147
148 // Authenticate user and load prefs
149 $imapConnection = sqimap_login($username, $key,
150 $imapServerAddress, $imapPort, 10);
151 include("../src/load_prefs.php");
152 sqimap_logout ($imapConnection);
153
154 displayHtmlHeader();
155
156 // Choose correct colors for top and bottom frame
157 if($show == "form") {
158 echo "<BODY BGCOLOR=\"$color[3]\" TEXT=\"$color[6]\" ";
159 echo "LINK=\"$color[6]\" VLINK=\"$color[6]\" ALINK=\"$color[6]\" ";
160 echo "OnLoad=\"document.sform.query.focus();\">";
161 } else {
162 echo "<BODY TEXT=\"$color[8]\" BGCOLOR=\"$color[4]\" ";
163 echo "LINK=\"$color[7]\" VLINK=\"$color[7]\" ALINK=\"$color[7]\">\n";
164 }
165
166 // Empty search
167 if(empty($query) && empty($show) && empty($listall)) {
168 printf("<P ALIGN=center><BR>%s</P>\n</BODY></HTML>\n",
169 _("No persons matching your search was found"));
170 exit;
171 }
172
173 // Initialize addressbook
174 $abook = addressbook_init();
175
176 // Create search form
177 if($show == "form") {
178 printf("<FORM NAME=sform TARGET=abookres ACTION=\"%s\" METHOD=\"POST\">\n",
179 $PHP_SELF);
180 printf("<TABLE BORDER=0 WIDTH=\"100%%\" HEIGHT=\"100%%\">");
2c5d4cd4 181 printf("<TR><TD NOWRAP VALIGN=middle>\n");
2f73dc15 182 printf(" <STRONG>%s</STRONG>\n", _("Search for"));
183 printf(" <INPUT TYPE=text NAME=query VALUE=\"%s\" SIZE=26>\n",
184 htmlspecialchars($query));
185
186 // List all backends to allow the user to choose where to search
187 if($abook->numbackends > 1) {
188 printf("<STRONG>%s</STRONG>&nbsp;<SELECT NAME=backend>\n",
189 _("in"));
190 printf("<OPTION VALUE=-1 SELECTED>%s\n",
191 _("All address books"));
192 $ret = $abook->get_backend_list();
193 while(list($k,$v) = each($ret))
2c5d4cd4 194 printf("<OPTION VALUE=%d>%s\n", $v->bnum, $v->sname);
2f73dc15 195 printf("</SELECT>\n");
196 } else {
197 printf("<INPUT TYPE=hidden NAME=backend VALUE=-1>\n");
198 }
199
200 printf("<INPUT TYPE=submit VALUE=\"%s\">",
201 _("Search"));
202 printf("&nbsp;|&nbsp;<INPUT TYPE=submit VALUE=\"%s\" NAME=listall>\n",
203 _("List all"));
204 printf("</TD><TD ALIGN=right>\n");
205 printf("<INPUT TYPE=button VALUE=\"%s\" onclick=\"parent.close();\">\n",
206 _("Close window"));
207 printf("</TD></TR></TABLE></FORM>\n");
208 } else
209
210 // Show personal addressbook
211 if($show == "blank" || !empty($listall)) {
212
213 if($backend != -1 || $show == "blank") {
214 if($show == "blank")
215 $backend = $abook->localbackend;
216
217 //printf("<H3 ALIGN=center>%s</H3>\n", $abook->backends[$backend]->sname);
218
219 $res = $abook->list_addr($backend);
220
221 if(is_array($res)) {
222 display_result($res, false);
223 } else {
224 printf("<P ALIGN=center><STRONG>"._("Unable to list addresses from %s").
225 "</STRONG></P>\n", $abook->backends[$backend]->sname);
226 }
227
228 } else {
229 $res = $abook->list_addr();
230 display_result($res, true);
231 }
232
233 } else
5100704d 234
235 // Do the search
2f73dc15 236 if(!empty($query) && empty($listall)) {
237
238 if($backend == -1) {
239 $res = $abook->s_search($query);
240 } else {
241 $res = $abook->s_search($query, $backend);
242 }
5100704d 243
244 if(!is_array($res)) {
2c5d4cd4 245 printf("<P ALIGN=center><B><BR>%s:<br>%s</B></P>\n</BODY></HTML>\n",
a51f9f4b 246 _("Your search failed with the following error(s)"),
247 $abook->error);
248 exit;
249 }
250
251 if(sizeof($res) == 0) {
2c5d4cd4 252 printf("<P ALIGN=center><BR><B>%s.</B></P>\n</BODY></HTML>\n",
5100704d 253 _("No persons matching your search was found"));
254 exit;
255 }
256
2f73dc15 257 display_result($res);
5100704d 258 }
259?>
260
261</BODY></HTML>