/**
  * Get a list of groups the specified user belongs to
  *
  * @param int $uid User id
  * @param array|null $cache
  * @return array
  */
 protected static function getGroups($uid, $cache = null)
 {
     if ($cache === null) {
         $user = User::newFromId($uid);
         $effectiveGroups = $user->getEffectiveGroups();
     } else {
         $effectiveGroups = isset($cache[$uid]) ? $cache[$uid] : array();
     }
     $groups = array_diff($effectiveGroups, User::getImplicitGroups());
     return $groups;
 }
 /**
  * Get a list of groups the specified user belongs to
  *
  * @param $uid Integer: user id
  * @return array
  */
 protected static function getGroups($uid)
 {
     $user = User::newFromId($uid);
     $groups = array_diff($user->getEffectiveGroups(), User::getImplicitGroups());
     return $groups;
 }
 function revertAction($action, $result)
 {
     switch ($action) {
         case 'block':
             $block = Block::newFromTarget(User::whoIs($result['userid']));
             if (!$block || $block->getBy() != AbuseFilter::getFilterUser()->getId()) {
                 return false;
                 // Not blocked by abuse filter.
             }
             $block->delete();
             $log = new LogPage('block');
             $log->addEntry('unblock', Title::makeTitle(NS_USER, $result['user']), wfMsgForContent('abusefilter-revert-reason', $this->mPage->mFilter, $this->mReason));
             break;
         case 'blockautopromote':
             global $wgMemc;
             $wgMemc->delete(AbuseFilter::autopromoteBlockKey(User::newFromId($result['userid'])));
             break;
         case 'degroup':
             // Pull the user's groups from the vars.
             $oldGroups = $result['vars']['USER_GROUPS'];
             $oldGroups = explode(',', $oldGroups);
             $oldGroups = array_diff($oldGroups, array_intersect($oldGroups, User::getImplicitGroups()));
             $rows = array();
             foreach ($oldGroups as $group) {
                 $rows[] = array('ug_user' => $result['userid'], 'ug_group' => $group);
             }
             // Cheat a little bit. User::addGroup repeatedly is too slow.
             $user = User::newFromId($result['userid']);
             $currentGroups = $user->getGroups();
             $newGroups = array_merge($oldGroups, $currentGroups);
             // Don't do anything if there are no groups to add.
             if (!count(array_diff($newGroups, $currentGroups))) {
                 return;
             }
             $dbw = wfGetDB(DB_MASTER);
             $dbw->insert('user_groups', $rows, __METHOD__, array('IGNORE'));
             $user->invalidateCache();
             $log = new LogPage('rights');
             $log->addEntry('rights', $user->getUserPage(), wfMsgForContent('abusefilter-revert-reason', $this->mPage->mFilter, $this->mReason), array(implode(',', $currentGroups), implode(',', $newGroups)));
     }
 }
Example #4
0
 public function execute()
 {
     $params = $this->extractRequestParams();
     if (!is_null($params['prop'])) {
         $this->prop = array_flip($params['prop']);
     } else {
         $this->prop = array();
     }
     $users = (array) $params['users'];
     $goodNames = $done = array();
     $result = $this->getResult();
     // Canonicalize user names
     foreach ($users as $u) {
         $n = User::getCanonicalName($u);
         if ($n === false || $n === '') {
             $vals = array('name' => $u, 'invalid' => '');
             $fit = $result->addValue(array('query', $this->getModuleName()), null, $vals);
             if (!$fit) {
                 $this->setContinueEnumParameter('users', implode('|', array_diff($users, $done)));
                 $goodNames = array();
                 break;
             }
             $done[] = $u;
         } else {
             $goodNames[] = $n;
         }
     }
     if (count($goodNames)) {
         $this->addTables('user', 'u1');
         $this->addFields('u1.*');
         $this->addWhereFld('u1.user_name', $goodNames);
         if (isset($this->prop['groups'])) {
             $this->addTables('user_groups');
             $this->addJoinConds(array('user_groups' => array('LEFT JOIN', 'ug_user=u1.user_id')));
             $this->addFields('ug_group');
         }
         $this->showHiddenUsersAddBlockInfo(isset($this->prop['blockinfo']));
         $data = array();
         $res = $this->select(__METHOD__);
         foreach ($res as $row) {
             $user = User::newFromRow($row);
             $name = $user->getName();
             $data[$name]['name'] = $name;
             if (isset($this->prop['editcount'])) {
                 $data[$name]['editcount'] = intval($user->getEditCount());
             }
             if (isset($this->prop['registration'])) {
                 $data[$name]['registration'] = wfTimestampOrNull(TS_ISO_8601, $user->getRegistration());
             }
             if (isset($this->prop['groups']) && !is_null($row->ug_group)) {
                 // This row contains only one group, others will be added from other rows
                 $data[$name]['groups'][] = $row->ug_group;
             }
             if (isset($this->prop['rights']) && !is_null($row->ug_group)) {
                 if (!isset($data[$name]['rights'])) {
                     $data[$name]['rights'] = User::getGroupPermissions(User::getImplicitGroups());
                 }
                 $data[$name]['rights'] = array_unique(array_merge($data[$name]['rights'], User::getGroupPermissions(array($row->ug_group))));
                 $result->setIndexedTagName($data[$name]['rights'], 'r');
             }
             if ($row->ipb_deleted) {
                 $data[$name]['hidden'] = '';
             }
             if (isset($this->prop['blockinfo']) && !is_null($row->ipb_by_text)) {
                 $data[$name]['blockedby'] = $row->ipb_by_text;
                 $data[$name]['blockreason'] = $row->ipb_reason;
                 $data[$name]['blockexpiry'] = $row->ipb_expiry;
             }
             if (isset($this->prop['emailable']) && $user->canReceiveEmail()) {
                 $data[$name]['emailable'] = '';
             }
             if (isset($this->prop['gender'])) {
                 $gender = $user->getOption('gender');
                 if (strval($gender) === '') {
                     $gender = 'unknown';
                 }
                 $data[$name]['gender'] = $gender;
             }
             if (!is_null($params['token'])) {
                 $tokenFunctions = $this->getTokenFunctions();
                 foreach ($params['token'] as $t) {
                     $val = call_user_func($tokenFunctions[$t], $user);
                     if ($val === false) {
                         $this->setWarning("Action '{$t}' is not allowed for the current user");
                     } else {
                         $data[$name][$t . 'token'] = $val;
                     }
                 }
             }
         }
     }
     // Second pass: add result data to $retval
     foreach ($goodNames as $u) {
         if (!isset($data[$u])) {
             $data[$u] = array('name' => $u);
             $urPage = new UserrightsPage();
             $iwUser = $urPage->fetchUser($u);
             if ($iwUser instanceof UserRightsProxy) {
                 $data[$u]['interwiki'] = '';
                 if (!is_null($params['token'])) {
                     $tokenFunctions = $this->getTokenFunctions();
                     foreach ($params['token'] as $t) {
                         $val = call_user_func($tokenFunctions[$t], $iwUser);
                         if ($val === false) {
                             $this->setWarning("Action '{$t}' is not allowed for the current user");
                         } else {
                             $data[$u][$t . 'token'] = $val;
                         }
                     }
                 }
             } else {
                 $data[$u]['missing'] = '';
             }
         } else {
             if (isset($this->prop['groups']) && isset($data[$u]['groups'])) {
                 $autolist = ApiQueryUsers::getAutoGroups(User::newFromName($u));
                 $data[$u]['groups'] = array_merge($autolist, $data[$u]['groups']);
                 $this->getResult()->setIndexedTagName($data[$u]['groups'], 'g');
             }
         }
         $fit = $result->addValue(array('query', $this->getModuleName()), null, $data[$u]);
         if (!$fit) {
             $this->setContinueEnumParameter('users', implode('|', array_diff($users, $done)));
             break;
         }
         $done[] = $u;
     }
     return $this->getResult()->setIndexedTagName_internal(array('query', $this->getModuleName()), 'user');
 }
 /**
  * Build an input for an array setting
  *
  * @param str $conf setting name
  * @param mixed $default current value (but should be array :)
  * @param bool $allowed
  */
 protected function buildArrayInput($conf, $default, $allowed)
 {
     if (!isset(self::$arrayDefs[$conf]) || self::$arrayDefs[$conf] == 'array') {
         return $allowed ? '<span class="array">(array)</span>' : '<span class="array-disabled">(array)</span>';
     }
     $type = self::$arrayDefs[$conf];
     if ($type == 'simple') {
         if (!$allowed) {
             return "<pre>\n" . htmlspecialchars(is_array($default) ? implode("\n", $default) : $default) . "\n</pre>";
         }
         $text = "<textarea id='wp{$conf}' name='wp{$conf}' cols='30' rows='8'>";
         if (is_array($default)) {
             $text .= implode("\n", $default);
         }
         $text .= "</textarea>\n";
         return $text;
     }
     if ($type == 'assoc') {
         $keydesc = wfMsgHtml('configure-desc-key');
         $valdesc = wfMsgHtml('configure-desc-val');
         $class = !$allowed ? array('class' => 'disabled') : array();
         $encConf = htmlspecialchars($conf);
         $text = "<table class='assoc' id='{$encConf}'>\n<tr><th>{$keydesc}</th><th>{$valdesc}</th></tr>\n";
         if (is_array($default) && count($default) > 0) {
             $i = 0;
             foreach ($default as $key => $val) {
                 $text .= '<tr>' . Xml::openElement('td', $class);
                 if ($allowed) {
                     $text .= Xml::element('input', array('name' => 'wp' . $conf . "-key-{$i}", 'type' => 'text', 'value' => $key)) . "<br/>\n";
                 } else {
                     $text .= htmlspecialchars($key);
                 }
                 $text .= '</td>' . Xml::openElement('td', $class);
                 if ($allowed) {
                     $text .= Xml::element('input', array('name' => 'wp' . $conf . "-val-{$i}", 'type' => 'text', 'value' => $val)) . "<br/>\n";
                 } else {
                     $text .= htmlspecialchars($val);
                 }
                 $text .= '</td></tr>';
                 $i++;
             }
         } else {
             if ($allowed) {
                 $text .= '<tr><td>';
                 $text .= Xml::element('input', array('name' => 'wp' . $conf . "-key-0", 'type' => 'text', 'value' => '')) . "<br/>\n";
                 $text .= '</td><td>';
                 $text .= Xml::element('input', array('name' => 'wp' . $conf . "-val-0", 'type' => 'text', 'value' => '')) . "<br/>\n";
                 $text .= '</td></tr>';
             } else {
                 $text .= "<tr><td class='disabled' style='width:10em; height:1.5em;'><hr /></td><td class='disabled' style='width:10em; height:1.5em;'><hr /></td></tr>\n";
             }
         }
         $text .= '</table>';
         return $text;
     }
     if ($type == 'simple-dual') {
         $var = array();
         foreach ($default as $arr) {
             $var[] = implode(',', $arr);
         }
         if (!$allowed) {
             return "<pre>\n" . htmlspecialchars(implode("\n", $var)) . "\n</pre>";
         }
         $text = "<textarea id='wp{$conf}' name='wp{$conf}' cols='30' rows='8'>";
         if (is_array($var)) {
             $text .= implode("\n", $var);
         }
         $text .= "</textarea>\n";
         return $text;
     }
     if ($type == 'ns-bool') {
         global $wgContLang;
         $text = '';
         $attr = !$allowed ? array('disabled' => 'disabled') : array();
         foreach ($wgContLang->getNamespaces() as $ns => $name) {
             $name = str_replace('_', ' ', $name);
             if ('' == $name) {
                 $name = wfMsgExt('blanknamespace', array('parseinline'));
             }
             $text .= Xml::checkLabel($name, "wp{$conf}-ns{$ns}", "wp{$conf}-ns{$ns}", isset($default[$ns]) && $default[$ns], $attr) . "\n";
         }
         return $text;
     }
     if ($type == 'ns-text') {
         global $wgContLang;
         $nsdesc = wfMsgHtml('configure-desc-ns');
         $valdesc = wfMsgHtml('configure-desc-val');
         $text = "<table class='ns-text'>\n<tr><th>{$nsdesc}</th><th>{$valdesc}</th></tr>\n";
         foreach ($wgContLang->getNamespaces() as $ns => $name) {
             $name = str_replace('_', ' ', $name);
             if ('' == $name) {
                 $name = wfMsgExt('blanknamespace', array('parseinline'));
             }
             $text .= '<tr><td>' . $name . '</td><td>';
             if ($allowed) {
                 $text .= Xml::element('input', array('name' => "wp{$conf}-ns{$ns}", 'type' => 'text', 'value' => isset($default[$ns]) ? $default[$ns] : '')) . "\n";
             } else {
                 $text .= htmlspecialchars(isset($default[$ns]) ? $default[$ns] : '');
             }
             $text .= '</td></tr>';
         }
         $text .= '</table>';
         return $text;
     }
     if ($type == 'ns-array') {
         global $wgContLang;
         $nsdesc = wfMsgHtml('configure-desc-ns');
         $valdesc = wfMsgHtml('configure-desc-val');
         $text = "<table class='ns-array'>\n<tr><th>{$nsdesc}</th><th>{$valdesc}</th></tr>\n";
         foreach ($wgContLang->getNamespaces() as $ns => $name) {
             if ($ns < 0) {
                 continue;
             }
             $name = str_replace('_', ' ', $name);
             if ('' == $name) {
                 $name = wfMsgExt('blanknamespace', array('parseinline'));
             }
             $text .= '<tr><td>' . $name . '</td><td>';
             if ($allowed) {
                 $text .= Xml::openElement('textarea', array('name' => "wp{$conf}-ns{$ns}", 'id' => "wp{$conf}-ns{$ns}", 'cols' => 30, 'rows' => 5)) . (isset($default[$ns]) ? implode("\n", $default[$ns]) : '') . Xml::closeElement('textarea') . "<br/>\n";
             } else {
                 $text .= "<pre>\n" . (isset($default[$ns]) ? htmlspecialchars(implode("\n", $default[$ns])) : '') . "\n</pre>";
             }
             $text .= '</td></tr>';
         }
         $text .= '</table>';
         return $text;
     }
     if ($type == 'group-bool' || $type == 'group-array') {
         $all = array();
         $attr = !$allowed ? array('disabled' => 'disabled') : array();
         if ($type == 'group-bool') {
             if (is_callable(array('User', 'getAllRights'))) {
                 // 1.13 +
                 $all = User::getAllRights();
             } else {
                 foreach ($default as $rights) {
                     $all = array_merge($all, array_keys($rights));
                 }
                 $all = array_unique($all);
             }
             $iter = $default;
         } else {
             $all = array_keys($this->getSettingValue('wgGroupPermissions'));
             $iter = array();
             foreach ($all as $group) {
                 $iter[$group] = isset($default[$group]) && is_array($default[$group]) ? $default[$group] : array();
             }
             if ($this->isSettingAvailable('wgImplicitGroups')) {
                 // 1.12 +
                 $all = array_diff($all, $this->getSettingValue('wgImplicitGroups'));
             } else {
                 $all = array_diff($all, User::getImplicitGroups());
             }
         }
         $groupdesc = wfMsgHtml('configure-desc-group');
         $valdesc = wfMsgHtml('configure-desc-val');
         $encConf = htmlspecialchars($conf);
         $text = "<table id= '{$encConf}' class='{$type}'>\n<tr><th>{$groupdesc}</th><th>{$valdesc}</th></tr>\n";
         foreach ($iter as $group => $levs) {
             $row = '<div style="-moz-column-count:2"><ul>';
             foreach ($all as $right) {
                 if ($type == 'group-bool') {
                     $checked = isset($levs[$right]) && $levs[$right];
                 } else {
                     $checked = in_array($right, $levs);
                 }
                 $id = Sanitizer::escapeId('wp' . $conf . '-' . $group . '-' . $right);
                 $desc = $type == 'group-bool' && is_callable(array('User', 'getRightDescription')) ? User::getRightDescription($right) : $right;
                 $row .= '<li>' . Xml::checkLabel($desc, $id, $id, $checked, $attr) . "</li>\n";
             }
             $row .= '</ul></div>';
             $groupName = User::getGroupName($group);
             $encId = Sanitizer::escapeId('wp' . $conf . '-' . $group);
             $text .= "<tr id=\"{$encId}\">\n<td>{$groupName}</td>\n<td>{$row}</td>\n</tr>";
         }
         $text .= '</table>';
         return $text;
     }
 }