2021-12-30 12:05:56 +01:00
|
|
|
package APIjsonclient;
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
# Copyright (c) Rainer Gümpelein, TeilRad GmbH
|
|
|
|
#
|
|
|
|
#Client for shareejson api
|
|
|
|
#
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
use POSIX;
|
|
|
|
use CGI; # only for debugging
|
|
|
|
use JSON;
|
|
|
|
use LWP::UserAgent;
|
|
|
|
use URI::Encode;
|
|
|
|
use Config::General;
|
|
|
|
use Mod::Basework;
|
2022-01-31 14:55:05 +01:00
|
|
|
use Mod::DBtank;
|
2021-12-30 12:05:56 +01:00
|
|
|
use Data::Dumper;
|
|
|
|
|
|
|
|
|
|
|
|
sub new {
|
|
|
|
my $class = shift;
|
|
|
|
my $self = {};
|
|
|
|
bless($self,$class);
|
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
my $q = new CGI;
|
|
|
|
my $netloc = $q->url(-base=>1);
|
|
|
|
my $ua = LWP::UserAgent->new;
|
|
|
|
my $uri_encode = URI::Encode->new( { encode_reserved => 1 } );
|
|
|
|
$ua->agent("APIclient $netloc");
|
|
|
|
my $bw = new Basework;
|
2022-01-31 14:55:05 +01:00
|
|
|
my $dbt = new DBtank;
|
2021-12-30 12:05:56 +01:00
|
|
|
|
|
|
|
my $json = JSON->new->allow_nonref;
|
|
|
|
|
|
|
|
sub loop_sharees {
|
|
|
|
my $self = shift;
|
|
|
|
my $q = shift || "";
|
2022-01-31 14:55:05 +01:00
|
|
|
my $authraw = shift;
|
2022-01-18 07:04:04 +01:00
|
|
|
my $return_merchant = shift || "";
|
2021-12-30 12:05:56 +01:00
|
|
|
my @keywords = $q->param;
|
2022-01-31 14:55:05 +01:00
|
|
|
#my $project = $return_merchant->{project_id} || "all";
|
2021-12-30 12:05:56 +01:00
|
|
|
|
|
|
|
#only request keys which initated by sharee primary requests to operator
|
|
|
|
my $rest = "";
|
|
|
|
foreach (@keywords){
|
|
|
|
if($_ =~ /request|authcookie|system|bike|station/g){
|
|
|
|
my $val = $q->param($_);
|
|
|
|
my $encoded_val = $uri_encode->encode($val);
|
|
|
|
$rest .= "$_=$encoded_val&";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$rest =~ s/\&$//;
|
|
|
|
|
|
|
|
my $response_in = {};
|
|
|
|
|
|
|
|
my $globalconf_file = "/var/www/copri4/shareeconf/global.cfg";
|
|
|
|
my $conf = Config::General->new($globalconf_file);
|
|
|
|
my %globalconf = $conf->getall;
|
|
|
|
|
|
|
|
my @uri_operator_array = ();
|
|
|
|
my @user_group = ();
|
|
|
|
my @user_tour = ();
|
|
|
|
my $fetch_hash = {};
|
|
|
|
while (my ($key, $value) = each %{ $globalconf{operator} }) {
|
|
|
|
my $ret_json = "";
|
2022-01-18 07:04:04 +01:00
|
|
|
|
2022-01-31 14:55:05 +01:00
|
|
|
#shareetool, to loop only operators who is the shareetool by usersconf enabled
|
|
|
|
my $users_serviceapp = { u_id => 0 };
|
|
|
|
if($return_merchant->{aowner} && $return_merchant->{aowner} == 187 && $value->{database}->{dbname} && $authraw->{txt17} =~ /$value->{database}->{dbname}/){
|
|
|
|
my $dbh_operator = $dbt->dbconnect_extern($value->{database}->{dbname});
|
|
|
|
$users_serviceapp = $dbt->select_users($dbh_operator,$authraw->{c_id},"and int09=1");
|
|
|
|
$bw->log("shareetool select_users $return_merchant->{aowner} on $value->{database}->{dbname} $authraw->{c_id} $authraw->{txt17}",$users_serviceapp->{u_id},"");
|
|
|
|
}
|
2022-02-01 20:53:23 +01:00
|
|
|
#every sharee client must have a merchant_id which associate a project
|
|
|
|
#if($value->{operatorApp} && ($users_serviceapp->{u_id} || ($return_merchant->{project_id} eq $value->{project} || $return_merchant->{project_id} eq "all"))){
|
|
|
|
if($value->{operatorApp} && ($users_serviceapp->{u_id} || ($return_merchant->{project_id} eq $value->{project}))){
|
2022-01-31 14:55:05 +01:00
|
|
|
|
|
|
|
$bw->log("--> LOOP-start ($return_merchant->{merchant_id}) jsonclient loop_sharees $key by operatorApp: $value->{operatorApp}, netloc: $netloc if($return_merchant->{project_id} eq $value->{project} || $users_serviceapp->{u_id} > 0)\n","","");
|
2021-12-30 12:05:56 +01:00
|
|
|
$ret_json = $self->fetch_operator_json($value->{operatorApp},$rest);
|
|
|
|
if($ret_json){
|
|
|
|
push(@uri_operator_array, $value->{operatorApp});
|
|
|
|
eval {
|
|
|
|
my $response_in = decode_json($ret_json);
|
|
|
|
|
|
|
|
#collect OP user_group
|
|
|
|
if($response_in->{shareejson}->{user_group}){
|
|
|
|
push (@user_group, @{$response_in->{shareejson}->{user_group}});
|
|
|
|
}
|
|
|
|
#collect OP user_tour
|
|
|
|
if($response_in->{shareejson}->{user_tour}){
|
|
|
|
push (@user_tour, @{$response_in->{shareejson}->{user_tour}});
|
|
|
|
}
|
|
|
|
|
|
|
|
if($q->param('request') && $q->param('request') =~ /stations_all|stations_available/){
|
|
|
|
foreach my $result (keys (%{ $response_in->{shareejson}->{stations} })) {
|
|
|
|
$fetch_hash->{$result} = $response_in->{shareejson}->{stations}->{$result};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if($q->param('request') && $q->param('request') =~ /bikes_all|bikes_available/){
|
|
|
|
foreach my $result (keys (%{ $response_in->{shareejson}->{bikes} })) {
|
|
|
|
$fetch_hash->{$result} = $response_in->{shareejson}->{bikes}->{$result};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if($q->param('request') && $q->param('request') =~ /user_bikes_occupied/){
|
|
|
|
foreach my $result (keys (%{ $response_in->{shareejson}->{bikes_occupied} })) {
|
|
|
|
$fetch_hash->{$result} = $response_in->{shareejson}->{bikes_occupied}->{$result};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if ($@){
|
|
|
|
$bw->log("Failure, eval json from jsonclient","","");
|
|
|
|
warn $@;
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
$bw->log("NO json ","","");
|
|
|
|
}
|
2022-01-18 07:04:04 +01:00
|
|
|
$bw->log("--> LOOP-end jsonclient loop_sharees user_group:\n",\@user_group,"");
|
2021-12-30 12:05:56 +01:00
|
|
|
}
|
2022-01-18 07:04:04 +01:00
|
|
|
|
2021-12-30 12:05:56 +01:00
|
|
|
}
|
|
|
|
#print "ALL:" . Dumper($fetch_hash);
|
|
|
|
#
|
|
|
|
return ($fetch_hash,\@uri_operator_array,\@user_group,\@user_tour);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub fetch_operator_json {
|
|
|
|
my $self = shift;
|
|
|
|
my $operator_server = shift || "";
|
|
|
|
my $rest = shift || "";
|
|
|
|
my $operator_request = "$operator_server/APIjsonserver?$rest";
|
|
|
|
|
|
|
|
$bw->log("fetch_operator_json >> ","$operator_request","");
|
|
|
|
|
|
|
|
my $req = HTTP::Request->new(GET => "$operator_request");
|
|
|
|
$req->content_type('application/x-www-form-urlencoded');
|
|
|
|
$req->content($rest);
|
|
|
|
|
|
|
|
my $res = $ua->request($req);
|
|
|
|
if ($res->is_success) {
|
|
|
|
#print $res->content;
|
|
|
|
return $res->content;
|
|
|
|
}else {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|