DomainStatusNetworkPlugin.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2011, StatusNet, Inc.
  5. *
  6. * One status_network per email domain
  7. *
  8. * PHP version 5
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. * @category DomainStatusNetwork
  24. * @package StatusNet
  25. * @author Evan Prodromou <evan@status.net>
  26. * @copyright 2011 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('STATUSNET')) {
  31. // This check helps protect against security problems;
  32. // your code file can't be executed directly from the web.
  33. exit(1);
  34. }
  35. $_dir = dirname(__FILE__);
  36. require_once $_dir . '/extlib/effectiveTLDs.inc.php';
  37. require_once $_dir . '/extlib/regDomain.inc.php';
  38. /**
  39. * Tools to map one status_network to one email domain in a multi-site
  40. * installation.
  41. *
  42. * @category DomainStatusNetwork
  43. * @package StatusNet
  44. * @author Evan Prodromou <evan@status.net>
  45. * @copyright 2011 StatusNet, Inc.
  46. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  47. * @link http://status.net/
  48. */
  49. class DomainStatusNetworkPlugin extends Plugin
  50. {
  51. static $_thetree = null;
  52. function initialize()
  53. {
  54. // For various reasons this gets squished
  55. global $tldTree;
  56. if (empty($tldTree)) {
  57. if (!empty(self::$_thetree)) {
  58. $tldTree = self::$_thetree;
  59. }
  60. }
  61. $nickname = StatusNet::currentSite();
  62. if (empty($nickname)) {
  63. $this->log(LOG_WARNING, "No current site");
  64. return;
  65. }
  66. try {
  67. $sn = Status_network::getKV('nickname', $nickname);
  68. } catch (Exception $e) {
  69. $this->log(LOG_ERR, $e->getMessage());
  70. return;
  71. }
  72. $tags = $sn->getTags();
  73. foreach ($tags as $tag) {
  74. if (strncmp($tag, 'domain=', 7) == 0) {
  75. $domain = substr($tag, 7);
  76. $this->log(LOG_INFO, "Setting email domain to {$domain}");
  77. common_config_append('email', 'whitelist', $domain);
  78. }
  79. }
  80. }
  81. static function toDomain($raw)
  82. {
  83. $parts = explode('@', $raw);
  84. if (count($parts) == 1) {
  85. $domain = $parts[0];
  86. } else {
  87. $domain = $parts[1];
  88. }
  89. $domain = strtolower(trim($domain));
  90. return $domain;
  91. }
  92. static function registeredDomain($domain)
  93. {
  94. return getRegisteredDomain($domain);
  95. }
  96. static function nicknameAvailable($nickname)
  97. {
  98. $sn = Status_network::getKV('nickname', $nickname);
  99. if (!empty($sn)) {
  100. return false;
  101. }
  102. $usn = Unavailable_status_network::getKV('nickname', $nickname);
  103. if (!empty($usn)) {
  104. return false;
  105. }
  106. return true;
  107. }
  108. function onRouterInitialized($m)
  109. {
  110. if (common_config('globalapi', 'enabled')) {
  111. foreach (array('register', 'login', 'recover') as $method) {
  112. $m->connect('api/statusnet/global/'.$method,
  113. array('action' => 'global'.$method));
  114. }
  115. }
  116. return true;
  117. }
  118. function onLoginAction($action, &$login) {
  119. $this->debug($action);
  120. if (in_array($action, array('globalregister', 'globallogin', 'globalrecover'))) {
  121. $login = true;
  122. return false;
  123. }
  124. return true;
  125. }
  126. static function nicknameForDomain($domain)
  127. {
  128. $registered = self::registeredDomain($domain);
  129. $parts = explode('.', $registered);
  130. $base = $parts[0];
  131. if (self::nicknameAvailable($base)) {
  132. return $base;
  133. }
  134. $domainish = str_replace('.', '-', $registered);
  135. if (self::nicknameAvailable($domainish)) {
  136. return $domainish;
  137. }
  138. $i = 1;
  139. // We don't need to keep doing this forever
  140. while ($i < 1024) {
  141. $candidate = $domainish.'-'.$i;
  142. if (self::nicknameAvailable($candidate)) {
  143. return $candidate;
  144. }
  145. $i++;
  146. }
  147. return null;
  148. }
  149. static function siteForDomain($domain)
  150. {
  151. $snt = Status_network_tag::withTag('domain='.$domain);
  152. while ($snt->fetch()) {
  153. $sn = Status_network::getKV('site_id', $snt->site_id);
  154. if (!empty($sn)) {
  155. return $sn;
  156. }
  157. }
  158. return null;
  159. }
  160. function onPluginVersion(&$versions)
  161. {
  162. $versions[] = array('name' => 'DomainStatusNetwork',
  163. 'version' => GNUSOCIAL_VERSION,
  164. 'author' => 'Evan Prodromou',
  165. 'homepage' => 'http://status.net/wiki/Plugin:DomainStatusNetwork',
  166. 'rawdescription' =>
  167. // TRANS: Plugin description.
  168. _m('A plugin that maps a single status_network to an email domain.'));
  169. return true;
  170. }
  171. static function userExists($email)
  172. {
  173. $domain = self::toDomain($email);
  174. $sn = self::siteForDomain($domain);
  175. if (empty($sn)) {
  176. return false;
  177. }
  178. StatusNet::switchSite($sn->nickname);
  179. $user = User::getKV('email', $email);
  180. return !empty($user);
  181. }
  182. static function registerEmail($email)
  183. {
  184. $domain = self::toDomain($email);
  185. if (FreeEmail::isFree($domain)) {
  186. throw new ClientException(_("Use your work email."));
  187. }
  188. $sn = self::siteForDomain($domain);
  189. if (empty($sn)) {
  190. $installer = new DomainStatusNetworkInstaller($domain);
  191. // Do the thing
  192. $installer->main();
  193. $sn = $installer->getStatusNetwork();
  194. $config = $installer->getConfig();
  195. Status_network::$wildcard = $config['WILDCARD'];
  196. }
  197. StatusNet::switchSite($sn->nickname);
  198. $confirm = EmailRegistrationPlugin::registerEmail($email);
  199. return $confirm;
  200. }
  201. static function login($email, $password)
  202. {
  203. $domain = self::toDomain($email);
  204. $sn = self::siteForDomain($domain);
  205. if (empty($sn)) {
  206. throw new ClientException(_("No such site."));
  207. }
  208. StatusNet::switchSite($sn->nickname);
  209. $user = common_check_user($email, $password);
  210. if (empty($user)) {
  211. // TRANS: Form validation error displayed when trying to log in with incorrect credentials.
  212. throw new ClientException(_('Incorrect username or password.'));
  213. }
  214. $loginToken = Login_token::makeNew($user);
  215. if (empty($loginToken)) {
  216. throw new ServerException(sprintf(_('Could not create new login token for user %s'), $user->nickname));
  217. }
  218. $url = common_local_url('otp', array('user_id' => $loginToken->user_id,
  219. 'token' => $loginToken->token));
  220. if (empty($url)) {
  221. throw new ServerException(sprintf(_('Could not create new OTP URL for user %s'), $user->nickname));
  222. }
  223. return $url;
  224. }
  225. static function recoverPassword($email)
  226. {
  227. $domain = self::toDomain($email);
  228. $sn = self::siteForDomain($domain);
  229. if (empty($sn)) {
  230. throw new NoSuchUserException(array('email' => $email));
  231. }
  232. StatusNet::switchSite($sn->nickname);
  233. $user = User::getKV('email', $email);
  234. if (empty($user)) {
  235. throw new ClientException(_('No such user.'));
  236. }
  237. }
  238. }
  239. // The way addPlugin() works, this global variable gets disappeared.
  240. // So, we re-appear it.
  241. DomainStatusNetworkPlugin::$_thetree = $tldTree;