]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Channel.php
change function headers to K&R style
[quix0rs-gnu-social.git] / classes / Channel.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 class Channel {
23     
24     function on($user)
25     {
26         return false;
27     }
28
29     function off($user)
30     {
31         return false;
32     }
33
34     function output($user, $text)
35     {
36         return false;
37     }
38     
39     function error($user, $text)
40     {
41         return false;
42     }
43     
44     function source()
45     {
46         return null;
47     }
48 }
49
50 class XMPPChannel extends Channel {
51
52     var $conn = null;
53     
54     function source()
55     {
56         return 'xmpp';
57     }
58     
59     function __construct($conn)
60     {
61         $this->conn = $conn;
62     }
63     
64     function on($user)
65     {
66         return $this->set_notify($user, 1);
67     }
68     
69     function off($user)
70     {
71         return $this->set_notify($user, 0);
72     }
73
74     function output($user, $text)
75     {
76         $text = '['.common_config('site', 'name') . '] ' . $text;
77         jabber_send_message($user->jabber, $text);
78     }
79     
80     function error($user, $text)
81     {
82         $text = '['.common_config('site', 'name') . '] ' . $text;
83         jabber_send_message($user->jabber, $text);
84     }
85     
86     function set_notify(&$user, $notify)
87     {
88         $orig = clone($user);
89         $user->jabbernotify = $notify;
90         $result = $user->update($orig);
91         if (!$result) {
92             $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
93             common_log(LOG_ERR,
94                        'Could not set notify flag to ' . $notify .
95                        ' for user ' . common_log_objstring($user) .
96                        ': ' . $last_error->message);
97             return false;
98         } else {
99             common_log(LOG_INFO,
100                        'User ' . $user->nickname . ' set notify flag to ' . $notify);
101             return true;
102         }
103     }
104 }
105
106
107 class WebChannel extends Channel {
108
109     function source()
110     {
111         return 'web';
112     }
113     
114     function on($user)
115     {
116         return false;
117     }
118     
119     function off($user)
120     {
121         return false;
122     }
123
124     function output($user, $text)
125     {
126         # XXX: buffer all output and send it at the end
127         # XXX: even better, redirect to appropriate page
128         #      depending on what command was run
129         common_show_header(_('Command results'));
130         common_element('p', null, $text);
131         common_show_footer();
132     }
133     
134     function error($user, $text)
135     {
136         common_user_error($text);
137     }
138 }
139
140
141 class AjaxWebChannel extends WebChannel {
142
143     function output($user, $text)
144     {
145         common_start_html('text/xml;charset=utf-8', true);
146         common_element_start('head');
147         common_element('title', null, _('Command results'));
148         common_element_end('head');
149         common_element_start('body');
150         common_element('p', array('id' => 'command_result'), $text);
151         common_element_end('body');
152         common_element_end('html');
153     }
154
155     function error($user, $text)
156     {
157         common_start_html('text/xml;charset=utf-8', true);
158         common_element_start('head');
159         common_element('title', null, _('Ajax Error'));
160         common_element_end('head');
161         common_element_start('body');
162         common_element('p', array('id' => 'error'), $text);
163         common_element_end('body');
164         common_element_end('html');
165     }
166 }
167
168
169 class MailChannel extends Channel {
170
171     var $addr = null;
172
173     function source()
174     {
175         return 'mail';
176     }
177     
178     function __construct($addr=null)
179     {
180         $this->addr = $addr;
181     }
182     
183     function on($user)
184     {
185         return $this->set_notify($user, 1);
186     }
187     
188     function off($user)
189     {
190         return $this->set_notify($user, 0);
191     }
192
193     function output($user, $text)
194     {
195
196         $headers['From'] = $user->incomingemail;
197         $headers['To'] = $this->addr;
198         
199         $headers['Subject'] = _('Command complete');
200
201         return mail_send(array($this->addr), $headers, $text);
202     }
203     
204     function error($user, $text)
205     {
206         
207         $headers['From'] = $user->incomingemail;
208         $headers['To'] = $this->addr;
209         
210         $headers['Subject'] = _('Command failed');
211
212         return mail_send(array($this->addr), $headers, $text);
213     }
214     
215     function set_notify($user, $value)
216     {
217         $orig = clone($user);
218         $user->smsnotify = $value;
219         $result = $user->update($orig);
220         if (!$result) {
221             common_log_db_error($user, 'UPDATE', __FILE__);
222             return false;
223         }
224         return true;
225     }
226 }