33d9109343aa033ee9bd3e6fbb602135e3ed4fb0
[squirrelmail.git] / src / addrbook_search.php
1 <?php
2
3 /**
4 * addrbook_search.php
5 *
6 * Handle addressbook searching in the popup window.
7 *
8 * NOTE: A lot of this code is similar to the code in
9 * addrbook_search_html.html -- If you change one,
10 * change the other one too!
11 *
12 * @copyright &copy; 1999-2006 The SquirrelMail Project Team
13 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
14 * @version $Id$
15 * @package squirrelmail
16 * @subpackage addressbook
17 */
18
19 /**
20 * Path for SquirrelMail required files.
21 * @ignore
22 */
23 define('SM_PATH','../');
24
25 /** SquirrelMail required files. */
26 include_once(SM_PATH . 'include/validate.php');
27 include_once(SM_PATH . 'functions/strings.php');
28 include_once(SM_PATH . 'functions/global.php');
29 include_once(SM_PATH . 'functions/html.php');
30 include_once(SM_PATH . 'functions/forms.php');
31 include_once(SM_PATH . 'functions/addressbook.php');
32
33 /**
34 * Function to include JavaScript code
35 * @return void
36 */
37 function insert_javascript() {
38 ?>
39 <script language="Javascript"><!--
40
41 function to_and_close($addr) {
42 to_address($addr);
43 parent.close();
44 }
45
46 function to_address($addr) {
47 var prefix = "";
48 var pwintype = typeof parent.opener.document.compose;
49
50 $addr = $addr.replace(/ {1,35}$/, "");
51
52 if (pwintype != "undefined") {
53 if (parent.opener.document.compose.send_to.value) {
54 prefix = ", ";
55 parent.opener.document.compose.send_to.value =
56 parent.opener.document.compose.send_to.value + ", " + $addr;
57 } else {
58 parent.opener.document.compose.send_to.value = $addr;
59 }
60 }
61 }
62
63 function cc_address($addr) {
64 var prefix = "";
65 var pwintype = typeof parent.opener.document.compose;
66
67 $addr = $addr.replace(/ {1,35}$/, "");
68
69 if (pwintype != "undefined") {
70 if (parent.opener.document.compose.send_to_cc.value) {
71 prefix = ", ";
72 parent.opener.document.compose.send_to_cc.value =
73 parent.opener.document.compose.send_to_cc.value + ", " + $addr;
74 } else {
75 parent.opener.document.compose.send_to_cc.value = $addr;
76 }
77 }
78 }
79
80 function bcc_address($addr) {
81 var prefix = "";
82 var pwintype = typeof parent.opener.document.compose;
83
84 $addr = $addr.replace(/ {1,35}$/, "");
85
86 if (pwintype != "undefined") {
87 if (parent.opener.document.compose.send_to_bcc.value) {
88 prefix = ", ";
89 parent.opener.document.compose.send_to_bcc.value =
90 parent.opener.document.compose.send_to_bcc.value + ", " + $addr;
91 } else {
92 parent.opener.document.compose.send_to_bcc.value = $addr;
93 }
94 }
95 }
96
97 // --></script>
98 <?php
99 } /* End of included JavaScript */
100
101
102 /**
103 * List search results
104 * @param array $res Array of search results
105 * @param bool $includesource [Default=true]
106 * @return void
107 */
108 function display_result($res, $includesource = true) {
109 global $color;
110
111 if(sizeof($res) <= 0) return;
112
113 insert_javascript();
114
115 $line = 0;
116 echo html_tag( 'table', '', 'center', '', 'border="0" width="98%"' ) .
117 html_tag( 'tr', '', '', $color[9] ) .
118 html_tag( 'th', '&nbsp;', 'left' ) .
119 html_tag( 'th', '&nbsp;' . _("Name"), 'left' ) .
120 html_tag( 'th', '&nbsp;' . _("E-mail"), 'left' ) .
121 html_tag( 'th', '&nbsp;' . _("Info"), 'left' );
122
123 if ($includesource) {
124 echo html_tag( 'th', '&nbsp;' . _("Source"), 'left', '', 'width="10%"' );
125 }
126 echo "</tr>\n";
127
128 while (list($undef, $row) = each($res)) {
129 $email = htmlspecialchars(addcslashes(AddressBook::full_address($row), "'"), ENT_QUOTES);
130 if ($line % 2) {
131 $tr_bgcolor = $color[12];
132 } else {
133 $tr_bgcolor = $color[4];
134 }
135 echo html_tag( 'tr', '', '', $tr_bgcolor, 'style="white-space: nowrap;"' ) .
136 html_tag( 'td',
137 '<small><a href="javascript:to_address(' .
138 "'" . $email . "');\">"._("To")."</a> | " .
139 '<a href="javascript:cc_address(' .
140 "'" . $email . "');\">"._("Cc")."</a> | " .
141 '<a href="javascript:bcc_address(' .
142 "'" . $email . "');\">"._("Bcc")."</a></small>",
143 'center', '', 'valign="top" width="5%" style="white-space: nowrap;"' ) .
144 html_tag( 'td', '&nbsp;' . htmlspecialchars($row['name']), 'left', '', 'valign="top" style="white-space: nowrap;"' ) .
145 html_tag( 'td', '&nbsp;' .
146 '<a href="javascript:to_and_close(' .
147 "'" . $email . "');\">" . htmlspecialchars($row['email']) . '</a>'
148 , 'left', '', 'valign="top"' ) .
149 html_tag( 'td', htmlspecialchars($row['label']), 'left', '', 'valign="top" style="white-space: nowrap;"' );
150 if ($includesource) {
151 echo html_tag( 'td', '&nbsp;' . $row['source'], 'left', '', 'valign="top" style="white-space: nowrap;"' );
152 }
153
154 echo "</tr>\n";
155 $line++;
156 }
157 echo '</table>';
158 }
159
160 /* ================= End of functions ================= */
161
162 /** lets get the global vars we may need */
163 sqgetGlobalVar('key', $key, SQ_COOKIE);
164 sqgetGlobalVar('username', $username, SQ_SESSION);
165 sqgetGlobalVar('onetimepad',$onetimepad, SQ_SESSION);
166 sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION);
167
168 if (! sqgetGlobalVar('show' , $show)) {
169 $show = '';
170 }
171 if (! sqgetGlobalVar('query', $query, SQ_POST)) {
172 $query = '';
173 }
174 if (! sqgetGlobalVar('listall', $listall, SQ_POST)) {
175 unset($listall);
176 }
177 if (! sqgetGlobalVar('backend', $backend, SQ_POST)) {
178 $backend = '';
179 }
180
181 displayHtmlHeader();
182
183 /** set correct value of $default_charset */
184 global $default_charset;
185 set_my_charset();
186
187 /* Choose correct colors for top and bottom frame */
188 if ($show == 'form' && !isset($listall)) {
189 echo '<body text="' . $color[6] . '" bgcolor="' . $color[3] . '" ' .
190 'link="' . $color[6] . '" vlink="' . $color[6] . '" ' .
191 'alink="' . $color[6] . '" ' .
192 'OnLoad="document.sform.query.focus();">';
193 } else {
194 echo '<body text="' . $color[8] . '" bgcolor="' . $color[4] . '" ' .
195 'link="' . $color[7] . '" vlink="' . $color[7] . '" ' .
196 'alink="' . $color[7] . "\">\n";
197 }
198
199 /* Empty search */
200 if (empty($query) && empty($show) && !isset($listall)) {
201 echo html_tag( 'p', '<br />' .
202 _("No persons matching your search were found"),
203 'center' ) .
204 "\n</body></html>\n";
205 exit;
206 }
207
208 /* Initialize addressbook, show init errors only in bottom frame */
209 $showerr=($show=='form' ? false : true);
210 $abook = addressbook_init($showerr);
211
212 /* Create search form (top frame) */
213 if ($show == 'form' && ! isset($listall)) {
214 echo '<form name="sform" target="abookres" action="addrbook_search.php'.
215 '" method="post">' . "\n" .
216 html_tag( 'table', '', '', '', 'border="0" width="100%" height="100%"' ) .
217 html_tag( 'tr' ) .
218 html_tag( 'td', ' <strong>' . _("Search for") . "</strong>\n", 'left', '', 'style="white-space: nowrap;" valign="middle" width="10%"' ) .
219 html_tag( 'td', '', 'left', '', '' ) .
220 addInput('query', $query, 28);
221
222 /* List all backends to allow the user to choose where to search */
223 if ($abook->numbackends > 1) {
224 echo '<strong>' . _("in") . '</strong>&nbsp;'."\n";
225 $selopts = array();
226 $selopts['-1'] = _("All address books");
227
228 $ret = $abook->get_backend_list();
229 while (list($undef,$v) = each($ret)) {
230 $selopts[$v->bnum] = $v->sname;
231 }
232 echo addSelect('backend', $selopts, '-1', TRUE);
233 } else {
234 echo addHidden('backend', '-1');
235 }
236
237 echo '</td></tr>' .
238 html_tag( 'tr',
239 html_tag( 'td', '', 'left' ) .
240 html_tag( 'td',
241 '<input type="submit" value="' . _("Search") . '" name="show" />' .
242 '&nbsp;|&nbsp;<input type="submit" value="' . _("List all") .
243 '" name="listall" />' . "\n" .
244 '&nbsp;|&nbsp;<input type="button" value="' . _("Close") .
245 '" onclick="parent.close();" />' . "\n" ,
246 'left' )
247 ) .
248 '</table></form>' . "\n";
249 } else {
250 /**
251 * List addresses (bottom frame)
252 * If listall is set, list all entries in selected backend.
253 * If $show is 'blank' (initial call of address book popup) - list
254 * personal address book.
255 */
256 if ($show == 'blank' || isset($listall)) {
257
258 if($backend != -1 || $show == 'blank') {
259 if ($show == 'blank') {
260 $backend = $abook->localbackend;
261 }
262 $res = $abook->list_addr($backend);
263
264 if(is_array($res)) {
265 usort($res,'alistcmp');
266 display_result($res, false);
267 } else {
268 echo html_tag( 'p', '<strong>' .
269 sprintf(_("Unable to list addresses from %s"),
270 $abook->backends[$backend]->sname) . '</strong>' ,
271 'center' ) . "\n";
272 }
273 } else {
274 $res = $abook->list_addr();
275 usort($res,'alistcmp');
276 display_result($res, true);
277 }
278
279 } elseif (!empty($query)) {
280 /* Do the search (listall is not set. query is set.)*/
281
282 if($backend == -1) {
283 $res = $abook->s_search($query);
284 } else {
285 $res = $abook->s_search($query, $backend);
286 }
287
288 if (!is_array($res)) {
289 echo html_tag( 'p', '<b><br />' .
290 _("Your search failed with the following error(s)") .
291 ':<br />' . $abook->error . "</b>\n" ,
292 'center' );
293 } elseif (sizeof($res) == 0) {
294 echo html_tag( 'p', '<br /><b>' .
295 _("No persons matching your search were found") . "</b>\n" ,
296 'center' );
297 } else {
298 display_result($res);
299 }
300 } else {
301 /**
302 * listall is not set, query is not set or empty.
303 * User hit search button without entering search expression.
304 */
305 echo html_tag( 'p', '<br /><b>' . _("Nothing to search") . "</b>\n",'center' );
306 }
307 }
308 $oTemplate->display('footer.tpl');
309 ?>