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