DomainStatusNetworkPlugin.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. const PLUGIN_VERSION = '2.0.0';
  52. static $_thetree = null;
  53. function initialize()
  54. {
  55. // For various reasons this gets squished
  56. global $tldTree;
  57. if (empty($tldTree)) {
  58. if (!empty(self::$_thetree)) {
  59. $tldTree = self::$_thetree;
  60. }
  61. }
  62. $nickname = GNUsocial::currentSite();
  63. if (empty($nickname)) {
  64. $this->log(LOG_WARNING, "No current site");
  65. return;
  66. }
  67. try {
  68. $sn = Status_network::getKV('nickname', $nickname);
  69. } catch (Exception $e) {
  70. $this->log(LOG_ERR, $e->getMessage());
  71. return;
  72. }
  73. $tags = $sn->getTags();
  74. foreach ($tags as $tag) {
  75. if (strncmp($tag, 'domain=', 7) == 0) {
  76. $domain = substr($tag, 7);
  77. $this->log(LOG_INFO, "Setting email domain to {$domain}");
  78. common_config_append('email', 'whitelist', $domain);
  79. }
  80. }
  81. }
  82. static function toDomain($raw)
  83. {
  84. $parts = explode('@', $raw);
  85. if (count($parts) == 1) {
  86. $domain = $parts[0];
  87. } else {
  88. $domain = $parts[1];
  89. }
  90. $domain = strtolower(trim($domain));
  91. return $domain;
  92. }
  93. static function registeredDomain($domain)
  94. {
  95. return getRegisteredDomain($domain);
  96. }
  97. static function nicknameAvailable($nickname)
  98. {
  99. $sn = Status_network::getKV('nickname', $nickname);
  100. if (!empty($sn)) {
  101. return false;
  102. }
  103. $usn = Unavailable_status_network::getKV('nickname', $nickname);
  104. if (!empty($usn)) {
  105. return false;
  106. }
  107. return true;
  108. }
  109. function onRouterInitialized($m)
  110. {
  111. if (common_config('globalapi', 'enabled')) {
  112. foreach (array('register', 'login', 'recover') as $method) {
  113. $m->connect('api/statusnet/global/'.$method,
  114. ['action' => 'global'.$method]);
  115. }
  116. }
  117. return true;
  118. }
  119. function onLoginAction($action, &$login) {
  120. $this->debug($action);
  121. if (in_array($action, array('globalregister', 'globallogin', 'globalrecover'))) {
  122. $login = true;
  123. return false;
  124. }
  125. return true;
  126. }
  127. static function nicknameForDomain($domain)
  128. {
  129. $registered = self::registeredDomain($domain);
  130. $parts = explode('.', $registered);
  131. $base = $parts[0];
  132. if (self::nicknameAvailable($base)) {
  133. return $base;
  134. }
  135. $domainish = str_replace('.', '-', $registered);
  136. if (self::nicknameAvailable($domainish)) {
  137. return $domainish;
  138. }
  139. $i = 1;
  140. // We don't need to keep doing this forever
  141. while ($i < 1024) {
  142. $candidate = $domainish.'-'.$i;
  143. if (self::nicknameAvailable($candidate)) {
  144. return $candidate;
  145. }
  146. $i++;
  147. }
  148. return null;
  149. }
  150. static function siteForDomain($domain)
  151. {
  152. $snt = Status_network_tag::withTag('domain='.$domain);
  153. while ($snt->fetch()) {
  154. $sn = Status_network::getKV('site_id', $snt->site_id);
  155. if (!empty($sn)) {
  156. return $sn;
  157. }
  158. }
  159. return null;
  160. }
  161. public function onPluginVersion(array &$versions): bool
  162. {
  163. $versions[] = array('name' => 'DomainStatusNetwork',
  164. 'version' => self::PLUGIN_VERSION,
  165. 'author' => 'Evan Prodromou',
  166. 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/DomainStatusNetwork',
  167. 'rawdescription' =>
  168. // TRANS: Plugin description.
  169. _m('A plugin that maps a single status_network to an email domain.'));
  170. return true;
  171. }
  172. static function userExists($email)
  173. {
  174. $domain = self::toDomain($email);
  175. $sn = self::siteForDomain($domain);
  176. if (empty($sn)) {
  177. return false;
  178. }
  179. GNUsocial::switchSite($sn->nickname);
  180. $user = User::getKV('email', $email);
  181. return !empty($user);
  182. }
  183. static function registerEmail($email)
  184. {
  185. $domain = self::toDomain($email);
  186. if (FreeEmail::isFree($domain)) {
  187. throw new ClientException(_("Use your work email."));
  188. }
  189. $sn = self::siteForDomain($domain);
  190. if (empty($sn)) {
  191. $installer = new DomainStatusNetworkInstaller($domain);
  192. // Do the thing
  193. $installer->main();
  194. $sn = $installer->getStatusNetwork();
  195. $config = $installer->getConfig();
  196. Status_network::$wildcard = $config['WILDCARD'];
  197. }
  198. GNUsocial::switchSite($sn->nickname);
  199. $confirm = EmailRegistrationPlugin::registerEmail($email);
  200. return $confirm;
  201. }
  202. static function login($email, $password)
  203. {
  204. $domain = self::toDomain($email);
  205. $sn = self::siteForDomain($domain);
  206. if (empty($sn)) {
  207. throw new ClientException(_("No such site."));
  208. }
  209. GNUsocial::switchSite($sn->nickname);
  210. $user = common_check_user($email, $password);
  211. if (empty($user)) {
  212. // TRANS: Form validation error displayed when trying to log in with incorrect credentials.
  213. throw new ClientException(_('Incorrect username or password.'));
  214. }
  215. $loginToken = Login_token::makeNew($user);
  216. if (empty($loginToken)) {
  217. throw new ServerException(sprintf(_('Could not create new login token for user %s'), $user->nickname));
  218. }
  219. $url = common_local_url('otp', array('user_id' => $loginToken->user_id,
  220. 'token' => $loginToken->token));
  221. if (empty($url)) {
  222. throw new ServerException(sprintf(_('Could not create new OTP URL for user %s'), $user->nickname));
  223. }
  224. return $url;
  225. }
  226. static function recoverPassword($email)
  227. {
  228. $domain = self::toDomain($email);
  229. $sn = self::siteForDomain($domain);
  230. if (empty($sn)) {
  231. throw new NoSuchUserException(array('email' => $email));
  232. }
  233. GNUsocial::switchSite($sn->nickname);
  234. $user = User::getKV('email', $email);
  235. if (empty($user)) {
  236. throw new ClientException(_('No such user.'));
  237. }
  238. }
  239. }
  240. // The way addPlugin() works, this global variable gets disappeared.
  241. // So, we re-appear it.
  242. DomainStatusNetworkPlugin::$_thetree = $tldTree;