<commit_before><?php
//----------------------------------------------------------------------
// Copyright (c) 2012-2014 <NAME>
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and/or hardware specification (the "Work") to
// deal in the Work without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Work, and to permit persons to whom the Work
// is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Work.
//
// THE WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER DEALINGS
// IN THE WORK.
//----------------------------------------------------------------------

// Client-side interface to GENI Clearinghouse Member Authority (MA)

require_once('ma_constants.php');
require_once('chapi.php');
require_once('cert_utils.php');

// A cache of a user's detailed info indexed by member_id
if(!isset($member_cache)) {
  //  error_log("SETTING MEMBER_CACHE");
  $member_cache = array();
  $member_by_attribute_cache = array(); // Only for single attribute lookups
}

// Add member attribute
function add_member_attribute($ma_url, $signer, $member_id, $name, $value, $self_asserted)
{
  $member_urn = get_member_urn($ma_url, $signer, $member_id);
  $client = XMLRPCClient::get_client($ma_url, $signer);
  $results = $client->add_member_attribute($member_urn, _portalkey_to_attkey($name), 
					   $value, $self_asserted, $client->creds(), 
					   array('_dummy' => null));
  return $results;  // probably ignored
}

// Remove member attribute
function remove_member_attribute($ma_url, $signer, $member_id, $name)
{
  $member_urn = get_member_urn($ma_url, $signer, $member_id);
  $client = XMLRPCClient::get_client($ma_url, $signer);
  $results = $client->remove_member_attribute($member_urn, _portalkey_to_attkey($name), 
					   $client->creds(), array('_dummy' => null));
  return $results;  // probably ignored
}

// Get list of all member_ids in repository
function get_member_ids($ma_url, $signer)
{
  $client = XMLRPCClient::get_client($ma_url, $signer);
  $options = array('filter' => array('MEMBER_UID', 'MEMBER_URN')); // match everything, select UID and URN
  $recs = $client->lookup_public_member_info($client->creds(), $options);
  $result = array_map(function($x) { return $x['MEMBER_UID']; }, $recs);
  return $result;
}

// Associate SSH public key with user
function register_ssh_key($ma_url, $signer, $member_id, $filename,
        $description, $ssh_public_key, $ssh_private_key = NULL)
{
  $client = XMLRPCClient::get_client($ma_url, $signer);
  $member_urn = get_member_urn($ma_url, $signer, $member_id);
  $pairs = array('_GENI_KEY_FILENAME' => $filename,
                 'KEY_DESCRIPTION' => $description,
                 'KEY_PUBLIC' => $ssh_public_key,
                 'KEY_MEMBER' => $member_urn);
  if (! is_null($ssh_private_key)) {
    $pairs['KEY_PRIVATE'] = $ssh_private_key;
  }

  $client->create_key($client->creds(), array('fields' => $pairs));
}

// Lookup public SSH keys associated with user
function lookup_public_ssh_keys($ma_url, $signer, $member_id)
{
  if (! isset($member_id) || is_null($member_id) || count($member_id) == 0 || (count($member_id) == 1 && (! isset($member_id[0]) || is_null($member_id[0]) || $member_id[0] == ''))) {
    error_log("Cannot lookup_public_ssh_keys on empty member ID: " . print_r($member_id, true));
    return array();
  }
  $client = XMLRPCClient::get_client($ma_url, $signer);
  $options = array('match'=> array('_GENI_KEY_MEMBER_UID'=>$member_id),
		   'filter'=>array('KEY_PUBLIC', '_GENI_KEY_FILENAME', 'KEY_DESCRIPTION', 'KEY_ID', '_GENI_KEY_MEMBER_UID'));
  $res = $client->lookup_keys($client->creds(), $options);

  $ssh_keys=array();
  foreach ($res as $keydict) {
    foreach ($keydict as $key)  {
      $keyarray = array();
      $keyarray[] = $key;
  $mapped_array = array_map(function($x) { return array('id' => $x['KEY_ID'],
							'public_key' => $x['KEY_PUBLIC'],
							'description' => $x['KEY_DESCRIPTION'],
							'member_id' => $x['_GENI_KEY_MEMBER_UID'],
							'filename' => $x['_GENI_KEY_FILENAME']);}
    ,$keyarray);
  $ssh_keys[] = $mapped_array[0];
    }
  }
  
  return $ssh_keys;
}

// Lookup private SSH keys associated with user
function lookup_private_ssh_keys($ma_url, $signer, $member_id)
{
  if (! isset($member_id) || is_null($member_id) || count($member_id) == 0 || (count($member_id) == 1 && (! isset($member_id[0]) || is_null($member_id[0]) || $member_id[0] == ''))) {
    error_log("Cannot lookup_private_ssh_keys on empty member ID: " . print_r($member_id, true));
    return array();
  }
  $client = XMLRPCClient::get_client($ma_url, $signer);
  $options = array('match'=> array('_GENI_KEY_MEMBER_UID'=>$member_id),
		   'filter'=>array('KEY_PRIVATE', 'KEY_PUBLIC', '_GENI_KEY_FILENAME', 'KEY_DESCRIPTION', 'KEY_ID', '_GENI_KEY_MEMBER_UID'));
  $res = $client->lookup_keys($client->creds(), $options);

  function privmapkeys($x) 
  { 
    return array('id' => $x['KEY_ID'],
		 'private_key' => $x['KEY_PRIVATE'],
		 'public_key' => $x['KEY_PUBLIC'],
		 'description' => $x['KEY_DESCRIPTION'],
		 'member_id' => $x['_GENI_KEY_MEMBER_UID'],
		 'filename' => $x['_GENI_KEY_FILENAME']); 
  }
  $ssh_keys=array();
  foreach ($res as $keydict) {
    foreach ($keydict as $key)  {
      $keyarray = array();
      $keyarray[] = $key;
      $mapped_array = array_map(function($x) {return array('id' => $x['KEY_ID'],
							   'private_key' => $x['KEY_PRIVATE'],
							   'public_key' => $x['KEY_PUBLIC'],
							   'description' => $x['KEY_DESCRIPTION'],
							   'member_id' => $x['_GENI_KEY_MEMBER_UID'],
							   'filename' => $x['_GENI_KEY_FILENAME']);}
	,$keyarray);
      $ssh_keys[] = $mapped_array[0];
    }
  }
  
  return $ssh_keys;
}

/*  // removed since there's an obvious typo, so cannot have worked
// Lookup a single SSH key by id
function lookup_public_ssh_key($ma_url, $signer, $member_id, $ssh_key_id)
{
  $keys = lookup_publc_ssh_keys($ma_url, $signer, $member_id);
  foreach ($keys as $key) {
    if ($key[MA_SSH_KEY_TABLE_FIELDNAME::ID] === $ssh_key_id) {
      return $key;
    }
  }
  // No key found, return NULL
  return NULL;
}
*/

function update_ssh_key($ma_url, $signer, $member_id, $ssh_key_id,
                        $filename, $description)
{
  $client = XMLRPCClient::get_client($ma_url, $signer);
  $member_urn = get_member_urn($ma_url, $signer, $member_id);
  $pairs = array();
  if ($filename || $filename == '') {
    $pairs['_GENI_KEY_FILENAME'] = $filename;
  }
  if ($description || $description == '') {
    $pairs['KEY_DESCRIPTION'] = $description;
  }
  if (sizeof($pairs) > 0) {
    $client->update_key($member_urn, $ssh_key_id, $client->creds(),
                      array('fields' => $pairs));
  }

  //return $ssh_key;
  // CHAPI: no return for now.  If needed, we'll need to retrieve it
}

function delete_ssh_key($ma_url, $signer, $member_id, $ssh_key_id)
{
  $client = XMLRPCClient::get_client($ma_url, $signer);
  $member_urn = get_member_urn($ma_url, $signer, $member_id);
  $client->delete_key($member_urn, $ssh_key_id, $client->creds(),
                      array('_dummy' => null));
}

// Lookup inside keys/certs associated with a user UUID
function lookup_keys_and_certs($ma_url, $signer, $member_uuid)
{
  if (! isset($member_uuid) || is_null($member_uuid) || count($member_uuid) == 0 || (count($member_uuid) == 1 && (! isset($member_uuid[0]) || is_null($member_uuid[0]) || $member_uuid[0] == ''))) {
    error_log("Cannot lookup_keys_and_certs on empty member ID: " . print_r($member_uuid, true));
    return array();
  }
  $client = XMLRPCClient::get_client($ma_url, $signer);
  $options = array('match'=> array('MEMBER_UID'=>$member_uuid),
		   'filter'=>array('_GENI_MEMBER_INSIDE_PRIVATE_KEY'));
  $prires = $client->lookup_private_member_info($client->creds(), $options);
  //  error_log("PRIRES_OPTS = " . print_r($options, true));
  //  error_log("PRIRES = " . print_r($prires, true));
  if (sizeof($prires)>0) {
    $all_urns = array_keys($prires);
    $urn = $all_urns[0];<commit_msg>Remove member_urn argument form update_key and delete_key calls in v2<commit_after><?php
//----------------------------------------------------------------------
// Copyright (c) 2012-2014 <NAME>
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and/or hardware specification (the "Work") to
// deal in the Work without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Work, and to permit persons to whom the Work
// is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Work.
//
// THE WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER DEALINGS
// IN THE WORK.
//----------------------------------------------------------------------

// Client-side interface to GENI Clearinghouse Member Authority (MA)

require_once('ma_constants.php');
require_once('chapi.php');
require_once('cert_utils.php');

// A cache of a user's detailed info indexed by member_id
if(!isset($member_cache)) {
  //  error_log("SETTING MEMBER_CACHE");
  $member_cache = array();
  $member_by_attribute_cache = array(); // Only for single attribute lookups
}

// Add member attribute
function add_member_attribute($ma_url, $signer, $member_id, $name, $value, $self_asserted)
{
  $member_urn = get_member_urn($ma_url, $signer, $member_id);
  $client = XMLRPCClient::get_client($ma_url, $signer);
  $results = $client->add_member_attribute($member_urn, _portalkey_to_attkey($name), 
					   $value, $self_asserted, $client->creds(), 
					   array('_dummy' => null));
  return $results;  // probably ignored
}

// Remove member attribute
function remove_member_attribute($ma_url, $signer, $member_id, $name)
{
  $member_urn = get_member_urn($ma_url, $signer, $member_id);
  $client = XMLRPCClient::get_client($ma_url, $signer);
  $results = $client->remove_member_attribute($member_urn, _portalkey_to_attkey($name), 
					   $client->creds(), array('_dummy' => null));
  return $results;  // probably ignored
}

// Get list of all member_ids in repository
function get_member_ids($ma_url, $signer)
{
  $client = XMLRPCClient::get_client($ma_url, $signer);
  $options = array('filter' => array('MEMBER_UID', 'MEMBER_URN')); // match everything, select UID and URN
  $recs = $client->lookup_public_member_info($client->creds(), $options);
  $result = array_map(function($x) { return $x['MEMBER_UID']; }, $recs);
  return $result;
}

// Associate SSH public key with user
function register_ssh_key($ma_url, $signer, $member_id, $filename,
        $description, $ssh_public_key, $ssh_private_key = NULL)
{
  $client = XMLRPCClient::get_client($ma_url, $signer);
  $member_urn = get_member_urn($ma_url, $signer, $member_id);
  $pairs = array('_GENI_KEY_FILENAME' => $filename,
                 'KEY_DESCRIPTION' => $description,
                 'KEY_PUBLIC' => $ssh_public_key,
                 'KEY_MEMBER' => $member_urn);
  if (! is_null($ssh_private_key)) {
    $pairs['KEY_PRIVATE'] = $ssh_private_key;
  }

  $client->create_key($client->creds(), array('fields' => $pairs));
}

// Lookup public SSH keys associated with user
function lookup_public_ssh_keys($ma_url, $signer, $member_id)
{
  if (! isset($member_id) || is_null($member_id) || count($member_id) == 0 || (count($member_id) == 1 && (! isset($member_id[0]) || is_null($member_id[0]) || $member_id[0] == ''))) {
    error_log("Cannot lookup_public_ssh_keys on empty member ID: " . print_r($member_id, true));
    return array();
  }
  $client = XMLRPCClient::get_client($ma_url, $signer);
  $options = array('match'=> array('_GENI_KEY_MEMBER_UID'=>$member_id),
		   'filter'=>array('KEY_PUBLIC', '_GENI_KEY_FILENAME', 'KEY_DESCRIPTION', 'KEY_ID', '_GENI_KEY_MEMBER_UID'));
  $res = $client->lookup_keys($client->creds(), $options);

  $ssh_keys=array();
  foreach ($res as $keydict) {
    foreach ($keydict as $key)  {
      $keyarray = array();
      $keyarray[] = $key;
  $mapped_array = array_map(function($x) { return array('id' => $x['KEY_ID'],
							'public_key' => $x['KEY_PUBLIC'],
							'description' => $x['KEY_DESCRIPTION'],
							'member_id' => $x['_GENI_KEY_MEMBER_UID'],
							'filename' => $x['_GENI_KEY_FILENAME']);}
    ,$keyarray);
  $ssh_keys[] = $mapped_array[0];
    }
  }
  
  return $ssh_keys;
}

// Lookup private SSH keys associated with user
function lookup_private_ssh_keys($ma_url, $signer, $member_id)
{
  if (! isset($member_id) || is_null($member_id) || count($member_id) == 0 || (count($member_id) == 1 && (! isset($member_id[0]) || is_null($member_id[0]) || $member_id[0] == ''))) {
    error_log("Cannot lookup_private_ssh_keys on empty member ID: " . print_r($member_id, true));
    return array();
  }
  $client = XMLRPCClient::get_client($ma_url, $signer);
  $options = array('match'=> array('_GENI_KEY_MEMBER_UID'=>$member_id),
		   'filter'=>array('KEY_PRIVATE', 'KEY_PUBLIC', '_GENI_KEY_FILENAME', 'KEY_DESCRIPTION', 'KEY_ID', '_GENI_KEY_MEMBER_UID'));
  $res = $client->lookup_keys($client->creds(), $options);

  function privmapkeys($x) 
  { 
    return array('id' => $x['KEY_ID'],
		 'private_key' => $x['KEY_PRIVATE'],
		 'public_key' => $x['KEY_PUBLIC'],
		 'description' => $x['KEY_DESCRIPTION'],
		 'member_id' => $x['_GENI_KEY_MEMBER_UID'],
		 'filename' => $x['_GENI_KEY_FILENAME']); 
  }
  $ssh_keys=array();
  foreach ($res as $keydict) {
    foreach ($keydict as $key)  {
      $keyarray = array();
      $keyarray[] = $key;
      $mapped_array = array_map(function($x) {return array('id' => $x['KEY_ID'],
							   'private_key' => $x['KEY_PRIVATE'],
							   'public_key' => $x['KEY_PUBLIC'],
							   'description' => $x['KEY_DESCRIPTION'],
							   'member_id' => $x['_GENI_KEY_MEMBER_UID'],
							   'filename' => $x['_GENI_KEY_FILENAME']);}
	,$keyarray);
      $ssh_keys[] = $mapped_array[0];
    }
  }
  
  return $ssh_keys;
}

/*  // removed since there's an obvious typo, so cannot have worked
// Lookup a single SSH key by id
function lookup_public_ssh_key($ma_url, $signer, $member_id, $ssh_key_id)
{
  $keys = lookup_publc_ssh_keys($ma_url, $signer, $member_id);
  foreach ($keys as $key) {
    if ($key[MA_SSH_KEY_TABLE_FIELDNAME::ID] === $ssh_key_id) {
      return $key;
    }
  }
  // No key found, return NULL
  return NULL;
}
*/

function update_ssh_key($ma_url, $signer, $member_id, $ssh_key_id,
                        $filename, $description)
{
  $client = XMLRPCClient::get_client($ma_url, $signer);
  //  $member_urn = get_member_urn($ma_url, $signer, $member_id);
  $pairs = array();
  if ($filename || $filename == '') {
    $pairs['_GENI_KEY_FILENAME'] = $filename;
  }
  if ($description || $description == '') {
    $pairs['KEY_DESCRIPTION'] = $description;
  }
  if (sizeof($pairs) > 0) {
    $client->update_key($ssh_key_id, $client->creds(),
                      array('fields' => $pairs));
  }

  //return $ssh_key;
  // CHAPI: no return for now.  If needed, we'll need to retrieve it
}

function delete_ssh_key($ma_url, $signer, $member_id, $ssh_key_id)
{
  $client = XMLRPCClient::get_client($ma_url, $signer);
  //  $member_urn = get_member_urn($ma_url, $signer, $member_id);
  $client->delete_key($ssh_key_id, $client->creds(),
                      array('_dummy' => null));
}

// Lookup inside keys/certs associated with a user UUID
function lookup_keys_and_certs($ma_url, $signer, $member_uuid)
{
  if (! isset($member_uuid) || is_null($member_uuid) || count($member_uuid) == 0 || (count($member_uuid) == 1 && (! isset($member_uuid[0]) || is_null($member_uuid[0]) || $member_uuid[0] == ''))) {
    error_log("Cannot lookup_keys_and_certs on empty member ID: " . print_r($member_uuid, true));
    return array();
  }
  $client = XMLRPCClient::get_client($ma_url, $signer);
  $options = array('match'=> array('MEMBER_UID'=>$member_uuid),
		   'filter'=>array('_GENI_MEMBER_INSIDE_PRIVATE_KEY'));
  $prires = $client->lookup_private_member_info($client->creds(), $options);
  //  error_log("PRIRES_OPTS = " . print_r($options, true));
  //  error_log("PRIRES = " . print_r($prires, true));
  if (sizeof($prires)>0) {
    $all_urns = array_keys($prires);
    $urn = $all_urns[0];
{'fraction_non_alphanumeric': 0.1803808424697057, 'fraction_numerical': 0.0032890940565493366, 'mean_word_length': 4.055717619603267, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 117, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1510618', 'n_tokens_mistral': 6241, 'n_tokens_neox': 5894, 'n_words': 1318}