DomainWhitelistPlugin.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2011, StatusNet, Inc.
  5. *
  6. * Restrict the email addresses in a domain to a select whitelist
  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 Cache
  24. * @package StatusNet
  25. * @author Evan Prodromou <evan@status.net>
  26. * @author Zach Copley <zach@status.net>
  27. * @copyright 2011 StatusNet, Inc.
  28. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  29. * @link http://status.net/
  30. */
  31. if (!defined('STATUSNET')) {
  32. // This check helps protect against security problems;
  33. // your code file can't be executed directly from the web.
  34. exit(1);
  35. }
  36. /**
  37. * Restrict the email addresses to a domain whitelist
  38. *
  39. * @category General
  40. * @package StatusNet
  41. * @author Evan Prodromou <evan@status.net>
  42. * @author Zach Copley <zach@status.net>
  43. * @copyright 2011 StatusNet, Inc.
  44. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  45. * @link http://status.net/
  46. */
  47. class DomainWhitelistPlugin extends Plugin
  48. {
  49. /**
  50. * Get the path to the plugin's installation directory. Used
  51. * to link in js files and whatnot.
  52. *
  53. * @return String the absolute path
  54. */
  55. protected function getPath() {
  56. return preg_replace('/^' . preg_quote(INSTALLDIR, '/') . '\//', '', dirname(__FILE__));
  57. }
  58. /**
  59. * Link in a JavaScript script for the whitelist invite form
  60. *
  61. * @param Action $action Action being shown
  62. *
  63. * @return boolean hook flag
  64. */
  65. function onEndShowStatusNetScripts($action) {
  66. $name = $action->arg('action');
  67. if ($name == 'invite') {
  68. $action->script($this->getPath() . '/js/whitelistinvite.js');
  69. }
  70. return true;
  71. }
  72. function onRequireValidatedEmailPlugin_Override($user, &$knownGood)
  73. {
  74. $knownGood = (!empty($user->email) && $this->matchesWhitelist($user->email));
  75. return true;
  76. }
  77. function onEndValidateUserEmail($user, $email, &$valid)
  78. {
  79. if ($valid) { // it's otherwise valid
  80. if (!$this->matchesWhitelist($email)) {
  81. $whitelist = $this->getWhitelist();
  82. if (count($whitelist) == 1) {
  83. // TRANS: Client exception thrown when a given e-mailaddress is not in the domain whitelist.
  84. // TRANS: %s is a whitelisted e-mail domain.
  85. $message = sprintf(_m('Email address must be in this domain: %s.'),
  86. $whitelist[0]);
  87. } else {
  88. // TRANS: Client exception thrown when a given e-mailaddress is not in the domain whitelist.
  89. // TRANS: %s are whitelisted e-mail domains separated by comma's (localisable).
  90. $message = sprintf(_m('Email address must be in one of these domains: %s.'),
  91. // TRANS: Separator for whitelisted domains.
  92. implode(_m('SEPARATOR',', '), $whitelist));
  93. }
  94. throw new ClientException($message);
  95. }
  96. }
  97. return true;
  98. }
  99. function onStartAddEmailAddress($user, $email)
  100. {
  101. if (!$this->matchesWhitelist($email)) {
  102. // TRANS: Exception thrown when an e-mail address does not match the site's domain whitelist.
  103. throw new Exception(_m('That email address is not allowed on this site.'));
  104. }
  105. return true;
  106. }
  107. function onEndValidateEmailInvite($user, $email, &$valid)
  108. {
  109. if ($valid) {
  110. $valid = $this->matchesWhitelist($email);
  111. }
  112. return true;
  113. }
  114. function matchesWhitelist($email)
  115. {
  116. $whitelist = $this->getWhitelist();
  117. if (empty($whitelist) || empty($whitelist[0])) {
  118. return true;
  119. }
  120. $userDomain = $this->domainFromEmail($email);
  121. return in_array($userDomain, $whitelist);
  122. }
  123. /**
  124. * Helper function to pull out a domain from
  125. * an email address
  126. *
  127. * @param string $email and email address
  128. * @return string the domain
  129. */
  130. function domainFromEmail($email)
  131. {
  132. $parts = explode('@', $email);
  133. return strtolower(trim($parts[1]));
  134. }
  135. function getWhitelist()
  136. {
  137. $whitelist = common_config('email', 'whitelist');
  138. if (is_array($whitelist)) {
  139. return $this->sortWhiteList($whitelist);
  140. } else {
  141. return explode('|', $whitelist);
  142. }
  143. }
  144. /**
  145. * This is a filter function passed in to array_filter()
  146. * in order to strip out the user's domain, which will
  147. * be re-inserted as the first element (see sortWhitelist()
  148. * below).
  149. *
  150. * @param string $domain domain to check
  151. * @return boolean whether to include the domain
  152. */
  153. function userDomainFilter($domain)
  154. {
  155. $user = common_current_user();
  156. $userDomain = $this->domainFromEmail($user->email);
  157. if ($userDomain == $domain) {
  158. return false;
  159. }
  160. return true;
  161. }
  162. /**
  163. * This function sorts the whitelist alphabetically, and sets the
  164. * current user's domain as the first element in the array of
  165. * allowed domains. Mostly, this is for the JavaScript on the invite
  166. * page--in the case of multiple allowed domains, it's nicer if the
  167. * user's own domain is the first option, and this seemed like a good
  168. * way to do it.
  169. *
  170. * @param array $whitelist whitelist of allowed email domains
  171. * @return array an ordered or sorted version of the whitelist
  172. */
  173. function sortWhitelist($whitelist)
  174. {
  175. $whitelist = array_unique($whitelist);
  176. natcasesort($whitelist);
  177. $user = common_current_user();
  178. if (!empty($user) && !empty($user->email)) {
  179. $userDomain = $this->domainFromEmail($user->email);
  180. $orderedWhitelist = array_values(
  181. array_filter(
  182. $whitelist,
  183. array($this, "userDomainFilter")
  184. )
  185. );
  186. if (in_array($userDomain, $whitelist)) {
  187. array_unshift($orderedWhitelist, $userDomain);
  188. }
  189. return $orderedWhitelist;
  190. }
  191. return $whitelist;
  192. }
  193. /**
  194. * Show a fancier invite form when domains are restricted to the
  195. * whitelist.
  196. *
  197. * @param action $action the invite action
  198. * @return boolean hook value
  199. */
  200. function onStartShowInviteForm($action)
  201. {
  202. $this->showConfirmDialog($action);
  203. $form = new WhitelistInviteForm($action, $this->getWhitelist());
  204. $form->show();
  205. return false;
  206. }
  207. function showConfirmDialog($action)
  208. {
  209. // For JQuery UI modal dialog
  210. $action->elementStart(
  211. 'div',
  212. // TRANS: Title for invitiation deletion dialog.
  213. array('id' => 'confirm-dialog', 'title' => _m('Confirmation Required'))
  214. );
  215. // TRANS: Confirmation text for invitation deletion dialog.
  216. $action->text(_m('Really delete this invitation?'));
  217. $action->elementEnd('div');
  218. }
  219. /**
  220. * This is a bit of a hack. We take the values from the custom
  221. * whitelist invite form and reformat them so they look like
  222. * their coming from the the normal invite form.
  223. *
  224. * @param action &$action the invite action
  225. * @return boolean hook value
  226. */
  227. function onStartSendInvitations(&$action)
  228. {
  229. $emails = array();
  230. $usernames = $action->arg('username');
  231. $domains = $action->arg('domain');
  232. for($i = 0; $i < count($usernames); $i++) {
  233. if (!empty($usernames[$i])) {
  234. $emails[] = $usernames[$i] . '@' . $domains[$i] . "\n";
  235. }
  236. }
  237. $action->args['addresses'] = implode($emails);
  238. return true;
  239. }
  240. function onPluginVersion(array &$versions)
  241. {
  242. $versions[] = array('name' => 'DomainWhitelist',
  243. 'version' => GNUSOCIAL_VERSION,
  244. 'author' => 'Evan Prodromou, Zach Copley',
  245. 'homepage' => 'http://status.net/wiki/Plugin:DomainWhitelist',
  246. 'rawdescription' =>
  247. // TRANS: Plugin description.
  248. _m('Restrict domains for email users.'));
  249. return true;
  250. }
  251. }