1<?php
2	/**************************************************************************\
3	* phpGroupWare - Messenger                                                 *
4	* http://www.phpgroupware.org                                              *
5	* This application written by Joseph Engo <jengo@phpgroupware.org>         *
6	* --------------------------------------------                             *
7	* Funding for this program was provided by http://www.checkwithmom.com     *
8	* --------------------------------------------                             *
9	*  This program is free software; you can redistribute it and/or modify it *
10	*  under the terms of the GNU General Public License as published by the   *
11	*  Free Software Foundation; either version 2 of the License, or (at your  *
12	*  option) any later version.                                              *
13	\**************************************************************************/
14
15	/* $Id: class.bomessenger.inc.php 13603 2003-10-10 01:44:38Z skwashd $ */
16
17	class bomessenger
18	{
19		var $so;
20		var $public_functions = array(
21			'send_message'        => True,
22			'send_global_message' => True,
23			'reply'               => True,
24			'forward'             => True,
25			'list_methods'        => True
26		);
27		var $soap_functions = array();
28		var $xmlrpc_methods = array();
29
30		function bomessenger()
31		{
32			$this->so = createobject('messenger.somessenger');
33
34			$this->xmlrpc_methods[] = array(
35				'name'        => 'read_inbox',
36				'description' => 'This will return an struct within an array of users inbox (Note: If you want raw, unformated values, use messenger.somessenger.read_inbox)',
37				'author'      => 'Joseph Engo <jengo@phpgroupware.org>',
38				'params'      => 'Struct: start => (int), order => (string), sort -> (string)',
39				'type'        => 'array'
40			);
41			$this->xmlrpc_methods[] = array(
42				'name'        => 'send_global_messsage',
43				'description' => 'Send a global message to all users'
44			);
45			$this->xmlrpc_methods[] = array(
46				'name'        => 'send',
47				'description' => 'Create a new message'
48			);
49			$this->xmlrpc_methods[] = array(
50				'name'        => 'update_message_status',
51				'description' => 'Update a message status'
52			);
53			$this->xmlrpc_methods[] = array(
54				'name'        => 'delete_message',
55				'description' => 'Deletes messages'
56			);
57			$this->xmlrpc_methods[] = array(
58				'name'        => 'total_messages',
59				'description' => 'Returns the number of messages in the inbox'
60			);
61		}
62
63		function update_message_status($p)
64		{
65			return $this->so->update_message_status($p['status'],$p['message_id']);
66		}
67
68		function send_global_message($data='')
69		{
70			if(is_array($data))
71			{
72				$message = $data['message'];
73				$send    = $data['send'];
74				$cancel  = $data['cancel'];
75			}
76			else
77			{
78				$message = $_POST['message'];
79				$send    = $_POST['send'];
80				$cancel  = $_POST['cancel'];
81			}
82
83			if (! $GLOBALS['phpgw']->acl->check('run',1,'admin') || $cancel)
84			{
85				Header('Location: ' . $GLOBALS['phpgw']->link('/index.php','menuaction=messenger.uimessenger.inbox'));
86				return False;
87			}
88
89			if (! $message['subject'])
90			{
91				$errors[] = lang('You must enter a subject');
92			}
93
94			if (! $message['content'])
95			{
96				$errors[] = lang("You didn't enter anything for the message");
97			}
98
99			if (is_array($errors))
100			{
101				ExecMethod('messenger.uimessenger.compose',$errors);
102				//$this->ui->compose($errors);
103			}
104			else
105			{
106				$account_info = $GLOBALS['phpgw']->accounts->get_list('accounts');
107
108				$this->so->db->transaction_begin();
109				while (list(,$account) = each($account_info))
110				{
111					$message['to'] = $account['account_lid'];
112					$this->so->send_message($message,True);
113
114				}
115				$this->so->db->transaction_commit();
116				Header('Location: ' . $GLOBALS['phpgw']->link('/index.php','menuaction=messenger.uimessenger.inbox'));
117			}
118		}
119
120		function check_for_missing_fields($message)
121		{
122			$acctid = $GLOBALS['phpgw']->accounts->name2id($message['to']);
123
124			if (!$acctid)
125			{
126				if ($message['to'])
127				{
128					$errors[] = lang("I can't find the username %1 on the system",$message['to']);
129				}
130				else
131				{
132					$errors[] = lang('You must enter the username this message is for');
133				}
134			}
135
136			$acct = createobject('phpgwapi.accounts',$GLOBALS['phpgw']->accounts->name2id($message['to']));
137			$acct->read_repository();
138			if ($acct->is_expired() && $GLOBALS['phpgw']->accounts->name2id($message['to']))
139			{
140				$errors[] = lang("Sorry, %1's account is not currently active",$message['to']);
141			}
142
143			if (! $message['subject'])
144			{
145				$errors[] = lang('You must enter a subject');
146			}
147
148			if (! $message['content'])
149			{
150				$errors[] = lang("You didn't enter anything for the message");
151			}
152			return $errors;
153		}
154
155		function send_message($data='')
156		{
157			if(is_array($data))
158			{
159				$message = $data['message'];
160				$send    = $data['send'];
161				$cancel  = $data['cancel'];
162			}
163			else
164			{
165				$message = $_POST['message'];
166				$send    = $_POST['send'];
167				$cancel  = $_POST['cancel'];
168			}
169
170			if ($cancel)
171			{
172				Header('Location: ' . $GLOBALS['phpgw']->link('/index.php','menuaction=messenger.uimessenger.inbox'));
173				return False;
174			}
175
176			$errors = $this->check_for_missing_fields($message);
177
178			if (is_array($errors))
179			{
180				ExecMethod('messenger.uimessenger.compose',$errors);
181				//$this->ui->compose($errors);
182			}
183			else
184			{
185				$this->so->send_message($message);
186				Header('Location: ' . $GLOBALS['phpgw']->link('/index.php','menuaction=messenger.uimessenger.inbox'));
187			}
188		}
189
190		function read_inbox($values)
191		{
192			$start = $values['start'];
193			$order = $values['order'];
194			$sort  = $values['sort'];
195
196			$messages = $this->so->read_inbox((int)$start,$order,$sort);
197
198			while (is_array($messages) && list(,$message) = each($messages))
199			{
200				if ($message['from'] == -1)
201				{
202					$cached['-1']       = -1;
203					$cached_names['-1'] = lang('Global Message');
204				}
205
206				// Cache our results, so we don't query the same account multiable times
207				if (! $cached[$message['from']])
208				{
209					$acct = createobject('phpgwapi.accounts',$message['from']);
210					$acct->read_repository();
211					$cached[$message['from']]       = $message['from'];
212					$cached_names[$message['from']] = $GLOBALS['phpgw']->common->display_fullname($acct->data['account_lid'],$acct->data['firstname'],$acct->data['lastname']);
213				}
214
215				/*
216				** N - New
217				** R - Replied
218				** O - Old (read)
219				** F - Forwarded
220				*/
221				if ($message['status'] == 'N')
222				{
223					$message['subject'] = '<b>' . $message['subject'] . '</b>';
224					//$message['status'] = 'N';
225					$message['date'] = '<b>' . $GLOBALS['phpgw']->common->show_date($message['date']) . '</b>';
226					$message['from'] = '<b>' . $cached_names[$message['from']] . '</b>';
227				}
228				else
229				{
230					$message['date'] = $GLOBALS['phpgw']->common->show_date($message['date']);
231					$message['from'] = $cached_names[$message['from']];
232				}
233
234				if ($message['status'] == 'O')
235				{
236					//$message['status'] = '&nbsp;';
237				}
238
239				$_messages[] = array(
240					'id'      => $message['id'],
241					'from'    => $message['from'],
242					'status'  => $message['status'],
243					'date'    => $message['date'],
244					'subject' => $message['subject'],
245					'content' => $message['content']
246				);
247			}
248
249			if (gettype($_messages) == 'NULL')
250			{
251				return array();
252			}
253
254			return $_messages;
255		}
256
257		function read_message($message_id)
258		{
259			$message = $this->so->read_message((int)$message_id);
260
261			$message['date'] = $GLOBALS['phpgw']->common->show_date($message['date']);
262
263			if ($message['from'] == -1)
264			{
265				$message['from']           = lang('Global Message');
266				$message['global_message'] = True;
267			}
268			else
269			{
270				$acct = createobject('phpgwapi.accounts',$message['from']);
271				$acct->read_repository();
272				$message['from'] = $GLOBALS['phpgw']->common->display_fullname($acct->data['account_lid'],$acct->data['firstname'],$acct->data['lastname']);
273			}
274
275			return $message;
276		}
277
278		function read_message_for_reply($message_id,$type,$n_message='')
279		{
280			if(!$n_message)
281			{
282				$n_message = $_POST['n_message'];
283			}
284
285			$message = $this->so->read_message($message_id);
286
287			$acct = createobject('phpgwapi.accounts',$message['from']);
288			$acct->read_repository();
289
290			if (! $n_message['content'])
291			{
292				$content_array = explode("\n",$message['content']);
293
294				$new_content_array[] = ' ';
295				$new_content_array[] = '> ' . $GLOBALS['phpgw']->common->display_fullname($acct->data['account_lid'],$acct->data['firstname'],$acct->data['lastname']) . ' wrote:';
296				$new_content_array[] = '>';
297				while (list(,$line) = each($content_array))
298				{
299					$new_content_array[] = '> ' . $line;
300				}
301				$message['content'] = implode("\n",$new_content_array);
302			}
303
304			$message['subject'] = $type . ': ' . $message['subject'];
305			$message['from']    = $acct->data['account_lid'];
306
307			return $message;
308		}
309
310		function delete_message($messages='')
311		{
312			if (! is_array($messages))
313			{
314				return False;
315			}
316
317			$this->so->db->transaction_begin();
318			while (list(,$message_id) = each($messages))
319			{
320				$this->so->delete_message($message_id);
321			}
322			$this->so->db->transaction_commit();
323
324			return True;
325		}
326
327		function reply($message_id='',$n_message='')
328		{
329			if(!$message_id)
330			{
331				$message_id = $_POST['message_id'];
332				$n_message  = $_POST['n_message'];
333			}
334
335			$errors = $this->check_for_missing_fields($n_message);
336			if (is_array($errors))
337			{
338				ExecMethod('messenger.uimessenger.reply',array($errors,$n_message));
339				//$this->ui->reply($errors, $n_message);
340			}
341			else
342			{
343				$this->so->send_message($n_message);
344				$this->so->update_message_status('R',$message_id);
345				Header('Location: ' . $GLOBALS['phpgw']->link('/index.php','menuaction=messenger.uimessenger.inbox'));
346			}
347		}
348
349		function forward($message_id='',$n_message='')
350		{
351			if(!$message_id)
352			{
353				$message_id = $_POST['message_id'];
354				$n_message  = $_POST['n_message'];
355			}
356
357			$errors = $this->check_for_missing_fields($n_message);
358
359			if (is_array($errors))
360			{
361				ExecMethod('messenger.uimessenger.forward',array($errors,$n_message));
362				//$this->ui->forward($errors, $n_message);
363			}
364			else
365			{
366				$this->so->send_message($n_message);
367				$this->so->update_message_status('F',$message_id);
368				Header('Location: ' . $GLOBALS['phpgw']->link('/index.php','menuaction=messenger.uimessenger.inbox'));
369			}
370		}
371
372		function total_messages($extra_where_clause = '')
373		{
374			return $this->so->total_messages($extra_where_clause);
375		}
376
377		function list_methods($_type='xmlrpc')
378		{
379			/*
380			  This handles introspection or discovery by the logged in client,
381			  in which case the input might be an array.  The server always calls
382			  this function to fill the server dispatch map using a string.
383			*/
384			if (is_array($_type))
385			{
386				$_type = $_type['type'] ? $_type['type'] : $_type[0];
387			}
388			switch($_type)
389			{
390				case 'xmlrpc':
391					$xml_functions = array(
392						'delete_message' => array(
393							'function'  => 'delete_message',
394							'signature' => array(array(xmlrpcStruct,xmlrpcStruct)),
395							'docstring' => lang('Delete a message.')
396						),
397						'read_message' => array(
398							'function'  => 'read_message',
399							'signature' => array(array(xmlrpcStruct,xmlrpcInt)),
400							'docstring' => lang('Read a single message.')
401						),
402						'read_inbox' => array(
403							'function'  => 'read_inbox',
404							'signature' => array(array(xmlrpcStruct,xmlrpcStruct)),
405							'docstring' => lang('Read a list of messages.')
406						),
407						'send_message' => array(
408							'function'  => 'send_message',
409							'signature' => array(array(xmlrpcStruct,xmlrpcStruct)),
410							'docstring' => lang('Send a message to a single recipient.')
411						),
412						'update_message_status' => array(
413							'function'  => 'update_message_status',
414							'signature' => array(array(xmlrpcBoolean,xmlrpcStruct)),
415							'docstring' => lang('Update a message status')
416						),
417						'send_global_message' => array(
418							'function'  => 'send_global_message',
419							'signature' => array(array(xmlrpcStruct,xmlrpcStruct)),
420							'docstring' => lang('Send a global message.')
421						),
422						'reply' => array(
423							'function'  => 'reply',
424							'signature' => array(array(xmlrpcInt,xmlrpcInt)),
425							'docstring' => lang('Reply to a received message.')
426						),
427						'forward' => array(
428							'function'  => 'forward',
429							'signature' => array(array(xmlrpcStruct,xmlrpcStruct)),
430							'docstring' => lang('Forward a message to another user.')
431						),
432						'list_methods' => array(
433							'function'  => 'list_methods',
434							'signature' => array(array(xmlrpcStruct,xmlrpcString)),
435							'docstring' => lang('Read this list of methods.')
436						)
437					);
438					return $xml_functions;
439					break;
440				case 'soap':
441					return $this->soap_functions;
442					break;
443				default:
444					return array();
445					break;
446			}
447		}
448	}
449