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