<commit_before><?php
/**
 * SURFconext EngineBlock
 *
 * LICENSE
 *
 * Copyright 2011 SURFnet bv, The Netherlands
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License.
 *
 * @category  SURFconext EngineBlock
 * @package
 * @copyright Copyright © 2010-2011 SURFnet SURFnet bv, The Netherlands (http://www.surfnet.nl)
 * @license   http://www.apache.org/licenses/LICENSE-2.0  Apache License 2.0
 */

/**
 *
 * Note that what Grouper calls REST isn't really REST but XML-RPC (tunneling XML over HTTP POST).
 */
class Grouper_Client_Rest implements Grouper_Client_Interface
{
    /**
     * Result in metadata when a member
     *
     * @var String
     */
    const IS_MEMBER = 'IS_MEMBER';

    /**
     * Result in metadata when not a member
     *
     * @var String
     */
    const IS_NOT_MEMBER = 'IS_NOT_MEMBER';

    /**
     * The admin user to retrieve membership information for private teams
     */
    const GROUPER_ADMIN = 'GrouperSystem';

    /**
     * @var string
     */
    protected $_endpointUrl;

    /**
     * @var array
     */
    protected $_options;

    /**
     * @var string
     */
    protected $_subjectId;

    public static function createFromConfig(Zend_Config $config)
    {
        if (!isset($config->host) || $config->host == '') {
            throw new EngineBlock_Exception('No Grouper Host specified! Please set "grouper.host" in your application configuration.');
        }

        $url = $config->protocol .
               '://' .
               $config->user .
               ':' .
               $config->password .
               '@' .
               $config->host .
               (isset($config->port) ? ':' . $config->port : '') .
               $config->path .
               '/' .
               $config->version . '/';

        $grouper = new self($url, $config->toArray());
        return $grouper;
    }

    /**
     * @param string $endpointUrl
     */
    public function __construct($endpointUrl, array $options = array())
    {
        $this->_endpointUrl = $endpointUrl;
        $this->_options = $options;
    }

    /**
     * Retrieve the list of groups that the specified subject is a member of.
     */
    public function getGroups($stem = null)
    {
        $this->_requireSubjectId();

        $subjectIdEncoded = htmlentities($this->_subjectId);
        $request = <<<XML
<WsRestGetGroupsRequest>
    <subjectLookups>
        <WsSubjectLookup>
            <subjectId>$subjectIdEncoded</subjectId>
        </WsSubjectLookup>
    </subjectLookups>
XML;
        if ($stem) {
            $stemEncoded = htmlentities($stem);
            $request .= <<<XML
    <wsStemLookup>
        <stemName>$stemEncoded</stemName>
    </wsStemLookup>
    <stemScope>ALL_IN_SUBTREE</stemScope>
XML;
        }

        $request .= <<<XML
    <actAsSubjectLookup>
        <subjectId>$subjectIdEncoded</subjectId>
    </actAsSubjectLookup>
</WsRestGetGroupsRequest>
XML;
        try {
            $result = $this->_doRest('subjects', $request);
        }
        catch (Exception $e) {
            if (strpos($e->getMessage(), "Problem with actAsSubject, SUBJECT_NOT_FOUND") !== false) {
                throw new Grouper_Client_Exception_SubjectNotFound();
            }
            throw $e;
        }

        $groups = array();
        if (isset($result) and ($result !== FALSE) and (!empty($result->results->WsGetGroupsResult->wsGroups))) {
            foreach ($result->results->WsGetGroupsResult->wsGroups->WsGroup as $group) {
                $groups[] = $this->_mapXmlToGroupModel($group);
            }
        }
        return $groups;
    }

    public function getGroupsWithPrivileges($stem = null)
    {
        $groups = $this->getGroups($stem);
        /** @var $group Grouper_Model_Group */
        foreach ($groups as &$group) {
            $group->privileges = $this->getMemberPrivileges($this->_subjectId, $group->name);
        }
        return $groups;
    }

    public function getMembers($groupName)
    {
        $this->_requireSubjectId();

        $subjectIdEncoded = htmlentities($this->_subjectId);
        $groupNameEncoded = htmlentities($groupName);
        $request = <<<XML
<WsRestGetMembersRequest>
  <includeSubjectDetail>T</includeSubjectDetail>
  <wsGroupLookups>
    <WsGroupLookup>
      <groupName>$groupNameEncoded</groupName>
    </WsGroupLookup>
  </wsGroupLookups>
  <actAsSubjectLookup>
    <subjectId>$subjectIdEncoded</subjectId>
  </actAsSubjectLookup>
</WsRestGetMembersRequest>
XML;

        try {
            $result = $this->_doRest('groups', $request);
        }
        catch (Exception $e) {
            if (strpos($e->getMessage(), "Problem with actAsSubject, SUBJECT_NOT_FOUND") !== false) {
                throw new Grouper_Client_Exception_SubjectNotFound();
            }
            throw $e;
        }

        $members = array();
        if (isset($result) and ($result !== FALSE) and (isset($result->results->WsGetMembersResult->wsSubjects->WsSubject))) {
            foreach ($result->results->WsGetMembersResult->wsSubjects->WsSubject as $member) {
                $memberObject = $this->_mapXmlToSubjectModel($member);
                $members[$memberObject->id] = $memberObject;
            }
        }
        else {
            throw new EngineBlock_Exception(__METHOD__ . ' Bad result: <pre>' . var_export($result, true));
        }
        return $members;<commit_msg>Add a message to an exception when it is thrown<commit_after><?php
/**
 * SURFconext EngineBlock
 *
 * LICENSE
 *
 * Copyright 2011 SURFnet bv, The Netherlands
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License.
 *
 * @category  SURFconext EngineBlock
 * @package
 * @copyright Copyright © 2010-2011 SURFnet SURFnet bv, The Netherlands (http://www.surfnet.nl)
 * @license   http://www.apache.org/licenses/LICENSE-2.0  Apache License 2.0
 */

/**
 *
 * Note that what Grouper calls REST isn't really REST but XML-RPC (tunneling XML over HTTP POST).
 */
class Grouper_Client_Rest implements Grouper_Client_Interface
{
    /**
     * Result in metadata when a member
     *
     * @var String
     */
    const IS_MEMBER = 'IS_MEMBER';

    /**
     * Result in metadata when not a member
     *
     * @var String
     */
    const IS_NOT_MEMBER = 'IS_NOT_MEMBER';

    /**
     * The admin user to retrieve membership information for private teams
     */
    const GROUPER_ADMIN = 'GrouperSystem';

    /**
     * @var string
     */
    protected $_endpointUrl;

    /**
     * @var array
     */
    protected $_options;

    /**
     * @var string
     */
    protected $_subjectId;

    public static function createFromConfig(Zend_Config $config)
    {
        if (!isset($config->host) || $config->host == '') {
            throw new EngineBlock_Exception('No Grouper Host specified! Please set "grouper.host" in your application configuration.');
        }

        $url = $config->protocol .
               '://' .
               $config->user .
               ':' .
               $config->password .
               '@' .
               $config->host .
               (isset($config->port) ? ':' . $config->port : '') .
               $config->path .
               '/' .
               $config->version . '/';

        $grouper = new self($url, $config->toArray());
        return $grouper;
    }

    /**
     * @param string $endpointUrl
     */
    public function __construct($endpointUrl, array $options = array())
    {
        $this->_endpointUrl = $endpointUrl;
        $this->_options = $options;
    }

    /**
     * Retrieve the list of groups that the specified subject is a member of.
     */
    public function getGroups($stem = null)
    {
        $this->_requireSubjectId();

        $subjectIdEncoded = htmlentities($this->_subjectId);
        $request = <<<XML
<WsRestGetGroupsRequest>
    <subjectLookups>
        <WsSubjectLookup>
            <subjectId>$subjectIdEncoded</subjectId>
        </WsSubjectLookup>
    </subjectLookups>
XML;
        if ($stem) {
            $stemEncoded = htmlentities($stem);
            $request .= <<<XML
    <wsStemLookup>
        <stemName>$stemEncoded</stemName>
    </wsStemLookup>
    <stemScope>ALL_IN_SUBTREE</stemScope>
XML;
        }

        $request .= <<<XML
    <actAsSubjectLookup>
        <subjectId>$subjectIdEncoded</subjectId>
    </actAsSubjectLookup>
</WsRestGetGroupsRequest>
XML;
        try {
            $result = $this->_doRest('subjects', $request);
        }
        catch (Exception $e) {
            if (strpos($e->getMessage(), "Problem with actAsSubject, SUBJECT_NOT_FOUND") !== false) {
                throw new Grouper_Client_Exception_SubjectNotFound($e->getMessage());
            }
            throw $e;
        }

        $groups = array();
        if (isset($result) and ($result !== FALSE) and (!empty($result->results->WsGetGroupsResult->wsGroups))) {
            foreach ($result->results->WsGetGroupsResult->wsGroups->WsGroup as $group) {
                $groups[] = $this->_mapXmlToGroupModel($group);
            }
        }
        return $groups;
    }

    public function getGroupsWithPrivileges($stem = null)
    {
        $groups = $this->getGroups($stem);
        /** @var $group Grouper_Model_Group */
        foreach ($groups as &$group) {
            $group->privileges = $this->getMemberPrivileges($this->_subjectId, $group->name);
        }
        return $groups;
    }

    public function getMembers($groupName)
    {
        $this->_requireSubjectId();

        $subjectIdEncoded = htmlentities($this->_subjectId);
        $groupNameEncoded = htmlentities($groupName);
        $request = <<<XML
<WsRestGetMembersRequest>
  <includeSubjectDetail>T</includeSubjectDetail>
  <wsGroupLookups>
    <WsGroupLookup>
      <groupName>$groupNameEncoded</groupName>
    </WsGroupLookup>
  </wsGroupLookups>
  <actAsSubjectLookup>
    <subjectId>$subjectIdEncoded</subjectId>
  </actAsSubjectLookup>
</WsRestGetMembersRequest>
XML;

        try {
            $result = $this->_doRest('groups', $request);
        }
        catch (Exception $e) {
            if (strpos($e->getMessage(), "Problem with actAsSubject, SUBJECT_NOT_FOUND") !== false) {
                throw new Grouper_Client_Exception_SubjectNotFound($e->getMessage());
            }
            throw $e;
        }

        $members = array();
        if (isset($result) and ($result !== FALSE) and (isset($result->results->WsGetMembersResult->wsSubjects->WsSubject))) {
            foreach ($result->results->WsGetMembersResult->wsSubjects->WsSubject as $member) {
                $memberObject = $this->_mapXmlToSubjectModel($member);
                $members[$memberObject->id] = $memberObject;
            }
        }
        else {
            throw new EngineBlock_Exception(__METHOD__ . ' Bad result: <pre>' . var_export($result, true));
        }
        return $members;
{'fraction_non_alphanumeric': 0.12630691399662733, 'fraction_numerical': 0.003372681281618887, 'mean_word_length': 2.287416851441242, 'pattern_counts': {'":': 0, '<': 95, '<?xml version=': 0, '>': 159, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4902051', 'n_tokens_mistral': 3468, 'n_tokens_neox': 3328, 'n_words': 796}