channel.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
  20. /**
  21. * @todo Needs documentation.
  22. */
  23. class Channel
  24. {
  25. public $widgetOpts;
  26. public $scoped;
  27. function on($user)
  28. {
  29. return false;
  30. }
  31. function off($user)
  32. {
  33. return false;
  34. }
  35. function output($user, $text)
  36. {
  37. return false;
  38. }
  39. function error($user, $text)
  40. {
  41. return false;
  42. }
  43. function source()
  44. {
  45. return null;
  46. }
  47. }
  48. class CLIChannel extends Channel
  49. {
  50. function source()
  51. {
  52. return 'cli';
  53. }
  54. function output($user, $text)
  55. {
  56. $site = common_config('site', 'name');
  57. print "[{$user->nickname}@{$site}] $text\n";
  58. }
  59. function error($user, $text)
  60. {
  61. $this->output($user, $text);
  62. }
  63. }
  64. class WebChannel extends Channel
  65. {
  66. var $out = null;
  67. function __construct($out=null)
  68. {
  69. $this->out = $out;
  70. }
  71. function source()
  72. {
  73. return 'web';
  74. }
  75. function on($user)
  76. {
  77. return false;
  78. }
  79. function off($user)
  80. {
  81. return false;
  82. }
  83. function output($user, $text)
  84. {
  85. // XXX: buffer all output and send it at the end
  86. // XXX: even better, redirect to appropriate page
  87. // depending on what command was run
  88. $this->out->startHTML();
  89. $this->out->elementStart('head');
  90. // TRANS: Title for command results.
  91. $this->out->element('title', null, _('Command results'));
  92. $this->out->elementEnd('head');
  93. $this->out->elementStart('body');
  94. $this->out->element('p', array('id' => 'command_result'), $text);
  95. $this->out->elementEnd('body');
  96. $this->out->endHTML();
  97. }
  98. function error($user, $text)
  99. {
  100. common_user_error($text);
  101. }
  102. }
  103. class AjaxWebChannel extends WebChannel
  104. {
  105. function output($user, $text)
  106. {
  107. $this->out->startHTML('text/xml;charset=utf-8');
  108. $this->out->elementStart('head');
  109. // TRANS: Title for command results.
  110. $this->out->element('title', null, _('Command results'));
  111. $this->out->elementEnd('head');
  112. $this->out->elementStart('body');
  113. $this->out->element('p', array('id' => 'command_result'), $text);
  114. $this->out->elementEnd('body');
  115. $this->out->endHTML();
  116. }
  117. function error($user, $text)
  118. {
  119. $this->out->startHTML('text/xml;charset=utf-8');
  120. $this->out->elementStart('head');
  121. // TRANS: Title for command results.
  122. $this->out->element('title', null, _('AJAX error'));
  123. $this->out->elementEnd('head');
  124. $this->out->elementStart('body');
  125. $this->out->element('p', array('id' => 'error'), $text);
  126. $this->out->elementEnd('body');
  127. $this->out->endHTML();
  128. }
  129. }
  130. class MailChannel extends Channel
  131. {
  132. var $addr = null;
  133. function source()
  134. {
  135. return 'mail';
  136. }
  137. function __construct($addr=null)
  138. {
  139. $this->addr = $addr;
  140. }
  141. function on($user)
  142. {
  143. return $this->setNotify($user, 1);
  144. }
  145. function off($user)
  146. {
  147. return $this->setNotify($user, 0);
  148. }
  149. function output($user, $text)
  150. {
  151. $headers['From'] = $user->incomingemail;
  152. $headers['To'] = $this->addr;
  153. // TRANS: E-mail subject when a command has completed.
  154. $headers['Subject'] = _('Command complete');
  155. return mail_send(array($this->addr), $headers, $text);
  156. }
  157. function error($user, $text)
  158. {
  159. $headers['From'] = $user->incomingemail;
  160. $headers['To'] = $this->addr;
  161. // TRANS: E-mail subject when a command has failed.
  162. $headers['Subject'] = _('Command failed');
  163. return mail_send(array($this->addr), $headers, $text);
  164. }
  165. function setNotify($user, $value)
  166. {
  167. $orig = clone($user);
  168. $user->smsnotify = $value;
  169. $result = $user->update($orig);
  170. if (!$result) {
  171. common_log_db_error($user, 'UPDATE', __FILE__);
  172. return false;
  173. }
  174. return true;
  175. }
  176. }