]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/channel.php
Create IM plugin, Pluginize XMPP, Create AIM plugin
[quix0rs-gnu-social.git] / lib / channel.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, StatusNet, 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('STATUSNET') && !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 WebChannel extends Channel
51 {
52     var $out = null;
53
54     function __construct($out=null)
55     {
56         $this->out = $out;
57     }
58
59     function source()
60     {
61         return 'web';
62     }
63
64     function on($user)
65     {
66         return false;
67     }
68
69     function off($user)
70     {
71         return false;
72     }
73
74     function output($user, $text)
75     {
76         # XXX: buffer all output and send it at the end
77         # XXX: even better, redirect to appropriate page
78         #      depending on what command was run
79         $this->out->startHTML();
80         $this->out->elementStart('head');
81         $this->out->element('title', null, _('Command results'));
82         $this->out->elementEnd('head');
83         $this->out->elementStart('body');
84         $this->out->element('p', array('id' => 'command_result'), $text);
85         $this->out->elementEnd('body');
86         $this->out->endHTML();
87     }
88
89     function error($user, $text)
90     {
91         common_user_error($text);
92     }
93 }
94
95 class AjaxWebChannel extends WebChannel
96 {
97     function output($user, $text)
98     {
99         $this->out->startHTML('text/xml;charset=utf-8');
100         $this->out->elementStart('head');
101         $this->out->element('title', null, _('Command results'));
102         $this->out->elementEnd('head');
103         $this->out->elementStart('body');
104         $this->out->element('p', array('id' => 'command_result'), $text);
105         $this->out->elementEnd('body');
106         $this->out->endHTML();
107     }
108
109     function error($user, $text)
110     {
111         $this->out->startHTML('text/xml;charset=utf-8');
112         $this->out->elementStart('head');
113         $this->out->element('title', null, _('Ajax Error'));
114         $this->out->elementEnd('head');
115         $this->out->elementStart('body');
116         $this->out->element('p', array('id' => 'error'), $text);
117         $this->out->elementEnd('body');
118         $this->out->endHTML();
119     }
120 }
121
122 class MailChannel extends Channel
123 {
124
125     var $addr = null;
126
127     function source()
128     {
129         return 'mail';
130     }
131
132     function __construct($addr=null)
133     {
134         $this->addr = $addr;
135     }
136
137     function on($user)
138     {
139         return $this->set_notify($user, 1);
140     }
141
142     function off($user)
143     {
144         return $this->set_notify($user, 0);
145     }
146
147     function output($user, $text)
148     {
149
150         $headers['From'] = $user->incomingemail;
151         $headers['To'] = $this->addr;
152
153         $headers['Subject'] = _('Command complete');
154
155         return mail_send(array($this->addr), $headers, $text);
156     }
157
158     function error($user, $text)
159     {
160
161         $headers['From'] = $user->incomingemail;
162         $headers['To'] = $this->addr;
163
164         $headers['Subject'] = _('Command failed');
165
166         return mail_send(array($this->addr), $headers, $text);
167     }
168
169     function set_notify($user, $value)
170     {
171         $orig = clone($user);
172         $user->smsnotify = $value;
173         $result = $user->update($orig);
174         if (!$result) {
175             common_log_db_error($user, 'UPDATE', __FILE__);
176             return false;
177         }
178         return true;
179     }
180 }