*/ class ParTCP_Admins { public $fileSystem; public $serverName; public function __construct( $fileSystem, $serverName ){ $this->fileSystem = $fileSystem; $this->serverName = $serverName; } public function get_list( $baseDir = '' ){ if ( $baseDir === 0 ){ $dir = 'root-admins'; } elseif ( ! $baseDir || $baseDir == '.' ){ $dir = 'admins'; } else { $dir = $baseDir . '/admins'; } if ( ! $this->fileSystem->exists( $dir ) ){ return []; } $data = $this->fileSystem->get_recent_contents( $dir, "[0-9]*-{admin,root}-*" ); if ( ! $data ){ return []; } $message = yaml_parse( $data ); $list = $message['Admins'] ?? []; if ( $this->serverName ){ $list = array_map( [ $this, 'sanitize_ptcp_id' ], $list ); } return $list; } public function get_responsibles( $baseDir = '' ){ $direct = TRUE; do { $list = $this->get_list( $baseDir ); if ( $list || $baseDir == '' ){ break; } $baseDir = dirname( $baseDir ); $baseDir = $baseDir == '.' ? '' : $baseDir; $direct = FALSE; } while ( TRUE ); return compact( 'list', 'direct' ); } public function is_appointed( $ptcpId, $baseDir ){ $adminList = $this->get_list( $baseDir ); $ptcpId = $this->sanitize_ptcp_id( $ptcpId ); return $adminList && ptcp_is_in_list( $ptcpId, $adminList ); } public function appoint( $ptcpId, $baseDir ){ $adminList = $this->get_list( $baseDir ); $ptcpId = $this->sanitize_ptcp_id( $ptcpId ); if ( ptcp_is_in_list( $ptcpId, $adminList ) ){ return FALSE; // participant is already appointed as admin } $adminList[] = $ptcpId; sort( $adminList ); return $adminList; } public function dismiss( $ptcpId, $baseDir ){ $adminList = $this->get_list( $baseDir ); $ptcpId = $this->sanitize_ptcp_id( $ptcpId ); if ( ! ptcp_is_in_list( $ptcpId, $adminList ) ){ return FALSE; // participant has not been appointed as admin } $adminList = array_diff( $adminList, [ $ptcpId ] ); return array_values( $adminList ); } private function sanitize_ptcp_id( $ptcpId ){ if ( ! $this->serverName ){ return $ptcpId; } return str_replace( "@{$this->serverName}", '', $ptcpId ); } } // end of file lib/admins.class.php