rename squirrelmail_rpc.php to rpc.php, it doesn't make much sense to
[squirrelmail.git] / src / addrbook_search_html.php
1 <?php
2 /**
3 * addrbook_search_html.php
4 *
5 * Handle addressbook searching with pure html.
6 *
7 * This file is included from compose.php
8 *
9 * @copyright &copy; 1999-2007 The SquirrelMail Project Team
10 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
11 * @version $Id$
12 * @package squirrelmail
13 * @subpackage addressbook
14 */
15
16 /**
17 * Include the SquirrelMail initialization file.
18 * Because this file can also be included within compose we check for the $bInit
19 * var which is set inside ini.php. It's needed because compose already includes
20 * init.php.
21 */
22 if (!isset($bInit)) {
23 /** This is the addrbook_search_html page */
24 define('PAGE_NAME', 'addrbook_search_html');
25
26 include('../include/init.php');
27 }
28
29 /** SquirrelMail required files. */
30 include_once(SM_PATH . 'functions/date.php');
31 include_once(SM_PATH . 'functions/addressbook.php');
32 include_once(SM_PATH . 'templates/util_addressbook.php');
33
34 sqgetGlobalVar('session', $session, SQ_POST);
35 sqgetGlobalVar('mailbox', $mailbox, SQ_POST);
36 if (! sqgetGlobalVar('query', $addrquery, SQ_POST))
37 $addrquery='';
38 sqgetGlobalVar('listall', $listall, SQ_POST);
39 sqgetGlobalVar('backend', $backend, SQ_POST);
40
41 /**
42 * Insert hidden data
43 */
44 function addr_insert_hidden() {
45 global $body, $subject, $send_to, $send_to_cc, $send_to_bcc, $mailbox,
46 $request_mdn, $request_dr, $identity, $session;
47
48 if (substr($body, 0, 1) == "\r") {
49 echo addHidden('body', "\n".$body);
50 } else {
51 echo addHidden('body', $body);
52 }
53
54 echo addHidden('session', $session).
55 addHidden('subject', $subject).
56 addHidden('send_to', $send_to).
57 addHidden('send_to_bcc', $send_to_bcc).
58 addHidden('send_to_cc', $send_to_cc).
59 addHidden('mailprio', $mailprio).
60 addHidden('request_mdn', $request_mdn).
61 addHidden('request_dr', $request_dr).
62 addHidden('identity', $identity).
63 addHidden('mailbox', $mailbox).
64 addHidden('from_htmladdr_search', 'true');
65 }
66
67
68 /**
69 * List search results
70 * @param array $res Array containing results of search
71 * @param bool $includesource If true, adds backend column to address listing
72 */
73 function addr_display_result($res, $includesource = true) {
74 global $PHP_SELF, $oTemplate, $oErrorHandler;
75
76
77 echo addForm($PHP_SELF, 'post', 'addressbook').
78 addHidden('html_addr_search_done', 'true');
79 addr_insert_hidden();
80
81 $oTemplate->assign('use_js', false);
82 $oTemplate->assign('include_abook_name', $includesource);
83 $oTemplate->assign('addresses', formatAddressList($res));
84
85 $oTemplate->display('addrbook_search_list.tpl');
86
87 echo '</form>';
88 }
89
90 /* --- End functions --- */
91
92 if ($compose_new_win == '1') {
93 compose_Header($color, $mailbox);
94 }
95 else {
96 displayPageHeader($color, $mailbox);
97 }
98
99 /** set correct value of $default_charset */
100 set_my_charset();
101
102 /* Initialize addressbook */
103 $abook = addressbook_init();
104
105
106 /* Search form */
107 echo addForm($PHP_SELF.'?html_addr_search=true', 'post', 'f');
108 addr_insert_hidden();
109 if (isset($session)) {
110 echo addHidden('session', $session);
111 }
112
113 $oTemplate->assign('use_js', false);
114 $oTemplate->assign('backends', getBackends());
115
116 $oTemplate->display('addressbook_search_form.tpl');
117
118 echo "</form>\n";
119 do_hook('addrbook_html_search_below', $null);
120 /* End search form */
121
122 /* List addresses. Show personal addressbook */
123 if ($addrquery == '' || ! empty($listall)) {
124 // TODO: recheck all conditions and simplity if statements
125 if (! isset($backend) || $backend != -1 || $addrquery == '') {
126 if ($addrquery == '' && empty($listall)) {
127 $backend = $abook->localbackend;
128 }
129
130 $res = $abook->list_addr($backend);
131
132 if (is_array($res)) {
133 usort($res,'alistcmp');
134 addr_display_result($res, false);
135 } else {
136 plain_error_message(_("Unable to list addresses from %s"), $abook->backends[$backend]->sname);
137 }
138
139 } else {
140 $res = $abook->list_addr();
141 usort($res,'alistcmp');
142 addr_display_result($res, true);
143 }
144 $oTemplate->display('footer.tpl');
145 exit;
146 } elseif (!empty($addrquery)) {
147 /* Do the search */
148 if ($backend == -1) {
149 $res = $abook->s_search($addrquery);
150 } else {
151 $res = $abook->s_search($addrquery, $backend);
152 }
153
154 if (!is_array($res)) {
155 plain_error_message(_("Your search failed with the following error(s)") .':<br />'. nl2br(htmlspecialchars($abook->error)));
156 } elseif (sizeof($res) == 0) {
157 $oTemplate->assign('note', _("No persons matching your search were found"));
158 $oTemplate->display('note.tpl');
159 } else {
160 addr_display_result($res);
161 }
162 } else {
163 // not first time display, not listall and search is empty
164 // TODO: I think, this part of control structure is never reached.
165 plain_error_message(_("Nothing to search"));
166 }
167
168 if ($addrquery == '' || sizeof($res) == 0) {
169 echo '<div style="text-align: center;">'.
170 addForm('compose.php','post','k');
171 addr_insert_hidden();
172 echo '<input type="submit" value="' . _("Return") . '" name="return" />' . "\n" .
173 '</form></div></nobr>';
174 }
175
176 echo '<hr />';
177
178 $oTemplate->display('footer.tpl');