+ /**
+ * This is a filter function passed in to array_filter()
+ * in order to strip out the user's domain, which will
+ * be re-inserted as the first element (see sortWhitelist()
+ * below).
+ *
+ * @param string $domain domain to check
+ * @return boolean whether to include the domain
+ */
+ function userDomainFilter($domain)
+ {
+ $user = common_current_user();
+ $userDomain = $this->domainFromEmail($user->email);
+ if ($userDomain == $domain) {
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * This function sorts the whitelist alphabetically, and sets the
+ * current user's domain as the first element in the array of
+ * allowed domains. Mostly, this is for the JavaScript on the invite
+ * page--in the case of multiple allowed domains, it's nicer if the
+ * user's own domain is the first option, and this seemed like a good
+ * way to do it.
+ *
+ * @param array $whitelist whitelist of allowed email domains
+ * @return array an ordered or sorted version of the whitelist
+ */
+ function sortWhitelist($whitelist)
+ {
+ $whitelist = array_unique($whitelist);
+ natcasesort($whitelist);
+
+ $user = common_current_user();
+
+ if (!empty($user) && !empty($user->email)) {
+ $userDomain = $this->domainFromEmail($user->email);
+
+ $orderedWhitelist = array_values(
+ array_filter(
+ $whitelist,
+ array($this, "userDomainFilter")
+ )
+ );
+
+ if (in_array($userDomain, $whitelist)) {
+ array_unshift($orderedWhitelist, $userDomain);
+ }
+ return $orderedWhitelist;
+ }
+
+ return $whitelist;
+ }
+
+ /**
+ * Show a fancier invite form when domains are restricted to the
+ * whitelist.
+ *
+ * @param action $action the invite action
+ * @return boolean hook value
+ */
+ function onStartShowInviteForm($action)
+ {
+ $this->showConfirmDialog($action);
+ $form = new WhitelistInviteForm($action, $this->getWhitelist());
+ $form->show();
+ return false;
+ }
+
+ function showConfirmDialog($action)
+ {
+ // For JQuery UI modal dialog
+ $action->elementStart(
+ 'div',
+ // TRANS: Title for invitiation deletion dialog.
+ array('id' => 'confirm-dialog', 'title' => _m('Confirmation Required'))
+ );
+ // TRANS: Confirmation text for invitation deletion dialog.
+ $action->text(_m('Really delete this invitation?'));
+ $action->elementEnd('div');
+ }
+
+ /**
+ * This is a bit of a hack. We take the values from the custom
+ * whitelist invite form and reformat them so they look like
+ * their coming from the the normal invite form.
+ *
+ * @param action &$action the invite action
+ * @return boolean hook value
+ */
+ function onStartSendInvitations(&$action)
+ {
+ $emails = array();
+ $usernames = $action->arg('username');
+ $domains = $action->arg('domain');
+
+ for($i = 0; $i < count($usernames); $i++) {
+ if (!empty($usernames[$i])) {
+ $emails[] = $usernames[$i] . '@' . $domains[$i] . "\n";
+ }
+ }
+
+ $action->args['addresses'] = implode($emails);
+
+ return true;
+ }
+
+ function onPluginVersion(array &$versions)