DomainStatusNetworkPlugin.php 8.5 KB

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