]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Channel.php
move opening brace of class declaration to next line
[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     
25     function on($user)
26     {
27         return false;
28     }
29
30     function off($user)
31     {
32         return false;
33     }
34
35     function output($user, $text)
36     {
37         return false;
38     }
39     
40     function error($user, $text)
41     {
42         return false;
43     }
44     
45     function source()
46     {
47         return null;
48     }
49 }
50
51 class XMPPChannel extends Channel
52 {
53
54     var $conn = null;
55     
56     function source()
57     {
58         return 'xmpp';
59     }
60     
61     function __construct($conn)
62     {
63         $this->conn = $conn;
64     }
65     
66     function on($user)
67     {
68         return $this->set_notify($user, 1);
69     }
70     
71     function off($user)
72     {
73         return $this->set_notify($user, 0);
74     }
75
76     function output($user, $text)
77     {
78         $text = '['.common_config('site', 'name') . '] ' . $text;
79         jabber_send_message($user->jabber, $text);
80     }
81     
82     function error($user, $text)
83     {
84         $text = '['.common_config('site', 'name') . '] ' . $text;
85         jabber_send_message($user->jabber, $text);
86     }
87     
88     function set_notify(&$user, $notify)
89     {
90         $orig = clone($user);
91         $user->jabbernotify = $notify;
92         $result = $user->update($orig);
93         if (!$result) {
94             $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
95             common_log(LOG_ERR,
96                        'Could not set notify flag to ' . $notify .
97                        ' for user ' . common_log_objstring($user) .
98                        ': ' . $last_error->message);
99             return false;
100         } else {
101             common_log(LOG_INFO,
102                        'User ' . $user->nickname . ' set notify flag to ' . $notify);
103             return true;
104         }
105     }
106 }
107
108
109 class WebChannel extends Channel
110 {
111
112     function source()
113     {
114         return 'web';
115     }
116     
117     function on($user)
118     {
119         return false;
120     }
121     
122     function off($user)
123     {
124         return false;
125     }
126
127     function output($user, $text)
128     {
129         # XXX: buffer all output and send it at the end
130         # XXX: even better, redirect to appropriate page
131         #      depending on what command was run
132         common_show_header(_('Command results'));
133         common_element('p', null, $text);
134         common_show_footer();
135     }
136     
137     function error($user, $text)
138     {
139         common_user_error($text);
140     }
141 }
142
143
144 class AjaxWebChannel extends WebChannel
145 {
146
147     function output($user, $text)
148     {
149         common_start_html('text/xml;charset=utf-8', true);
150         common_element_start('head');
151         common_element('title', null, _('Command results'));
152         common_element_end('head');
153         common_element_start('body');
154         common_element('p', array('id' => 'command_result'), $text);
155         common_element_end('body');
156         common_element_end('html');
157     }
158
159     function error($user, $text)
160     {
161         common_start_html('text/xml;charset=utf-8', true);
162         common_element_start('head');
163         common_element('title', null, _('Ajax Error'));
164         common_element_end('head');
165         common_element_start('body');
166         common_element('p', array('id' => 'error'), $text);
167         common_element_end('body');
168         common_element_end('html');
169     }
170 }
171
172
173 class MailChannel extends Channel
174 {
175
176     var $addr = null;
177
178     function source()
179     {
180         return 'mail';
181     }
182     
183     function __construct($addr=null)
184     {
185         $this->addr = $addr;
186     }
187     
188     function on($user)
189     {
190         return $this->set_notify($user, 1);
191     }
192     
193     function off($user)
194     {
195         return $this->set_notify($user, 0);
196     }
197
198     function output($user, $text)
199     {
200
201         $headers['From'] = $user->incomingemail;
202         $headers['To'] = $this->addr;
203         
204         $headers['Subject'] = _('Command complete');
205
206         return mail_send(array($this->addr), $headers, $text);
207     }
208     
209     function error($user, $text)
210     {
211         
212         $headers['From'] = $user->incomingemail;
213         $headers['To'] = $this->addr;
214         
215         $headers['Subject'] = _('Command failed');
216
217         return mail_send(array($this->addr), $headers, $text);
218     }
219     
220     function set_notify($user, $value)
221     {
222         $orig = clone($user);
223         $user->smsnotify = $value;
224         $result = $user->update($orig);
225         if (!$result) {
226             common_log_db_error($user, 'UPDATE', __FILE__);
227             return false;
228         }
229         return true;
230     }
231 }