Adding initial RPC baseline functionality. The delete message request does in fact...
[squirrelmail.git] / src / squirrelmail_rpc.php
1 <?php
2
3 /**
4 * squirrelmail_rpc.php
5 *
6 * This file contains the entry point to the "SquirrelMail API" -- the
7 * remote procedure call request receiver.
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 * @since 1.5.2
14 *
15 */
16 //FIXME: If we decide to route ALL requests, even normal page
17 // requests through this file, need to change page requests
18 // to something like this
19 //http://example.org/squirrelmail/src/squirrelmail_rpc.php?page=read_body&passed_id=47633...
20 // This file would then add ".php" to the "page" variable
21 // and pass the request on to that page by simply require()ing
22 // that page and exiting.
23 // Does this present problems, security or otherwise? What
24 // problems are created by the fact that the page request
25 // is always the same thing (some parts of the code and some
26 // plugins switch functionality based on $PHP_SELF and other
27 // $_SERVER variables that look for specific page names -- those
28 // can be fixed by looking at the "page" GET argument, but what
29 // other issues are created)? What about plugins? How would
30 // they work in this scheme? Would they be a lot more difficult
31 // to develop?
32 //NOTE: It is not entirely clear if doing the above is even desirable.
33 // Initial conversations on the squirrelmail-devel list were
34 // inconclusive. On one hand, doing so would give us one master
35 // file that handles any and all incoming requests, no matter
36 // where they came from or what format/type they are. On the
37 // other, keeping page requests out of this file keeps this file
38 // lean and specific to one technology: our RPC interface.
39
40
41 /**
42 * Include the SquirrelMail initialization file.
43 */
44 //FIXME: init.php assumes it is being called by a browser, so some error
45 // conditions are handled by immediately calling error_box() or
46 // otherwise trying to push something to the browser, which should
47 // be avoided at all costs. This is also pervasive in the whole
48 // core and must be cleaned up entirely before this can be a very
49 // functional RPC interface
50 require('../include/init.php');
51
52
53
54 /**
55 * Get RPC Action (can be in either GET or POST)
56 *
57 */
58 if (!sqGetGlobalVar('rpc_action', $rpc_action, SQ_FORM)) {
59 //FIXME: establish error codes (using 99 in the interim)
60 sm_rpc_return_error(99, _("No RPC action given"));
61 }
62
63
64
65 /**
66 * No matter what our response is, the headers
67 * will not change.
68 *
69 */
70 $oTemplate->header('Content-Type: text/xml');
71 $oTemplate->header('Content-Type: application/xml'); // required by IE
72 //FIXME: which anti-cache headers do we want to use?
73 $oTemplate->header('Cache-Control: no-cache');
74 // $oTemplate->header("Expires: Sat, 1 Jan 2000 00:00:00 GMT");
75 // $oTemplate->header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT");
76 // $oTemplate->header("Cache-Control: no-cache, must-revalidate");
77 // $oTemplate->header("Pragma: no-cache");
78
79
80
81 /**
82 * Allow plugins to add their own RPC action
83 * or modify behavior of SM core RPC actions...
84 *
85 * A plugin that handles a custom RPC action must
86 * return TRUE to the hook so that it knows that
87 * the action was handled and was not an unknown
88 * action. If the action was not handled, the plugin
89 * should return FALSE to the hook.
90 *
91 * Developer note: the $rpc_action parameter is passed
92 * in an array in case we can think of more parameters
93 * to add in the future.
94 *
95 */
96 $handled_by_plugin = boolean_hook_function('squirrelmail_rpc',
97 $temp=array(&$rpc_action),
98 1);
99
100
101
102 /**
103 * Go take care of each RPC action (unless plugin already did)
104 *
105 */
106 if (!$handled_by_plugin) switch (strtolower($rpc_action)) {
107
108 /**
109 * Delete Messages
110 *
111 */
112 case 'delete_messages':
113
114 require_once(SM_PATH . 'functions/mailbox_display.php');
115 require_once(SM_PATH . 'functions/imap.php');
116
117 if (!sqGetGlobalVar('delete_ids', $delete_ids, SQ_FORM)) {
118 sm_rpc_return_error(99, _("No deletion ID given"));
119 }
120 $delete_ids = explode(',', $delete_ids);
121 if (!sqGetGlobalVar('mailbox', $mailbox, SQ_FORM)) {
122 sm_rpc_return_error(99, _("No mailbox given"));
123 }
124 if (sqGetGlobalVar('startMessage', $startMessage, SQ_INORDER, 1)) {
125 $startMessage = (int) $startMessage;
126 }
127 sqGetGlobalVar('what', $what, SQ_FORM, 0);
128 if (sqGetGlobalVar('account', $iAccount, SQ_GET, 0)) {
129 $iAccount = (int) $iAccount;
130 }
131 //FIXME: need to grab the bypass trash variable here too! probably other vars...
132
133 /* FIXME: --- The following code was just experimental/proof-of-concept; the rest
134 of the implementation of this functionality still needs to be done "for real"
135 $oImapMessage = new IMAP_Message(0, $mailbox, $startMessage, $what, $iAccount);
136 foreach ($delete_ids as $id) {
137 $oImapMessage->setUid($id);
138 //FIXME: establish constants for $hide values (the 3 below indicates not to show errors, but to return any error string)
139 $result = $oImapMessage->deleteMessage(3);
140 if ($result !== TRUE) {
141 sm_rpc_return_error(99, $result);
142 }
143 }
144 --- */
145
146 sm_rpc_return_success();
147 //FIXME: Just for testing the line above can be changed to something like this:
148 //sm_rpc_return_success(0, 'Hooray! Message(s) deleted. Refresh your message list and make sure.');
149 break;
150
151
152 /**
153 * Default: error out
154 *
155 */
156 default:
157 sm_rpc_return_error(99, _("RPC action not understood"));
158 break;
159
160 }
161
162
163
164 /**
165 * Returns an error message to the RPC caller and exits
166 *
167 * NOTE that this function exits and will never return
168 *
169 * @param int $error_code The error code for the current error condition
170 * @param string $error_text Any error message associated with the error
171 * condition (OPTIONAL; default empty string)
172 *
173 */
174 function sm_rpc_return_error($error_code, $error_text='') {
175
176 global $oTemplate;
177 $oTemplate->assign('error_code', $error_code);
178 $oTemplate->assign('error_text', $error_text);
179
180 $oTemplate->display('rpc_response_error.tpl');
181
182 exit;
183
184 }
185
186
187
188 /**
189 * Returns a standard success result to the RPC caller and exits
190 *
191 * NOTE that this function exits and will never return
192 *
193 * @param int $result_code The result code (OPTIONAL; default 0)
194 * @param string $result_text Any result message (OPTIONAL; default
195 * empty string)
196 *
197 */
198 function sm_rpc_return_success($result_code=0, $result_text='') {
199
200 global $oTemplate;
201 $oTemplate->assign('result_code', $result_code);
202 $oTemplate->assign('result_text', $result_text);
203
204 $oTemplate->display('rpc_response_success.tpl');
205
206 exit;
207
208 }
209
210
211