API-CPanel

 view release on metacpan or  search on metacpan

lib/API/CPanel.pm  view on Meta::CPAN


    return '' unless $query_string;

    my $answer = $fake_answer ? $fake_answer : mk_query_to_server( $auth_hash, $query_string );
    warn $answer if $answer && $DEBUG;

    return $answer ?
        parse_answer(
            answer        => $answer,
            parser_params => $xml_parser_params
        ) : '';
}

# Filter hash
# STATIC(HASHREF: hash, ARRREF: allowed_keys)
# RETURN: hashref only with allowed keys
sub filter_hash {
    my ($hash, $allowed_keys) = @_;

    return unless ref $hash eq 'HASH' &&
        ref $allowed_keys eq 'ARRAY';
    
    my $new_hash = { };

    foreach my $allowed_key (@$allowed_keys) {
        if (exists $hash->{$allowed_key}) {
            $new_hash->{$allowed_key} = $hash->{$allowed_key};
        }
        elsif (exists $hash->{lc $allowed_key}) {
            $new_hash->{$allowed_key} = $hash->{lc $allowed_key};
        };
    }

    return $new_hash;
}

# Get access key, time to live -- 30 minutes
# STATIC(HASHREF: params_hash)
# params_hash:
# - all elements from mk_full_query_string +
# - auth_user*
# - auth_passwd*
sub get_auth_hash {
    my %params_raw = @_;

    warn 'get_auth_hash params: ' . Dumper(\%params_raw)  if $DEBUG;

    my $params = filter_hash(
        \%params_raw,
        [ 'auth_user', 'auth_passwd' ]
    );

    # Check this sub params
    unless ($params->{auth_user} && $params->{auth_passwd}) {
        return '';
    }

    return "Basic " . MIME::Base64::encode( $params->{auth_user} . ":" . $params->{auth_passwd} );
}

# Wrapper for "ref" on undef value, without warnings :)
# Possible very stupid sub :)
# STATIC(REF: our_ref)
sub refs {
    my $ref = shift;

    return '' unless $ref;

    return ref $ref;
}

# INTERNAL!!! Check server answer result
# STATIC(data_block)
sub is_success {
    my $data_block = shift;
    my $want_hash  = shift;

    if ( $data_block &&
         ref $data_block eq 'HASH' &&
         (( $data_block->{status} &&
           $data_block->{status} eq '1' ) ||
         ( $data_block->{result} &&
           $data_block->{result} eq '1' ))
       ) {
        return 1;
    } else {
        return $want_hash ? {} : '';
    }
}

# all params derived from get_auth_hash
sub query_abstract {
    my %params = @_;

    my $params_raw  = $params{params};
    my $func_name   = $params{func};
    my $container   = $params{container};

    my $fields      = $params{allowed_fields} || '';

    my $allowed_fields;
    warn 'query_abstract ' . Dumper( \%params ) if $DEBUG;

    return '' unless $params_raw && $func_name;

    $fields = "host path allow_http auth_user auth_passwd container $fields";
    @$allowed_fields = split(' ', $fields);

    my $xml_parser_params = $params{parser_params};

    my $auth_hash = get_auth_hash( %$params_raw );
    warn "Auth_hash: $auth_hash\n" if $DEBUG;

    if ( $auth_hash ) {
        my $params = filter_hash( $params_raw, $allowed_fields );

        my $query_string = mk_full_query_string( {
            func => $func_name,
            %$params,
        } );



( run in 1.376 second using v1.01-cache-2.11-cpan-d80b1682f3f )