<commit_before>-------------------------------------------------------------------------------------
-- ASTPP - Open Source VoIP Billing Solution
--
-- Copyright (C) 2016 iNextrix Technologies Pvt. Ltd.
-- <NAME> <<EMAIL>>
-- ASTPP Version 3.0 and above
-- License https://www.gnu.org/licenses/agpl-3.0.html
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU Affero General Public License as
-- published by the Free Software Foundation, either version 3 of the
-- License, or (at your option) any later version.
-- 
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU Affero General Public License for more details.
-- 
-- You should have received a copy of the GNU Affero General Public License
-- along with this program.  If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------------------

-- Load configuration variables from database 
function load_conf()



    local query = "SELECT name,value FROM "..TBL_CONFIG.." WHERE group_title IN ('global','opensips','callingcard')";
    Logger.debug("[LOAD_CONF] Query :" .. query)
    
    local config = {}
    assert (dbh:query(query, function(u)
      config[u.name] = u.value;      
    end))
    return config;
end

-- Get Speed dial number value
function get_speeddial_number(destination_number,accountid)  
	local query = "SELECT A.number FROM "..TBL_SPEED_DIAL.." as A,"..TBL_USERS.." as B WHERE B.status=0 AND B.deleted=0 AND B.id=A.accountid AND A.speed_num =\"" ..destination_number .."\" AND A.accountid = '"..accountid.."' limit 1";

   Logger.debug("[CHECK_SPEEDDIAL] Query :" .. query)
   
    assert (dbh:query(query, function(u)
	speeddial = u;
    end))
	
	if(speeddial and speeddial ~= nil) then
        Logger.info("[Dialplan] SPEED DIAL DESTINATION NUMBER NOT FOUND")
	    return speeddial['number']
	else 
	    return destination_number
	end

end
-- Define call direction
function define_call_direction(destination_number,accountcode,config)  
    local didinfo = check_did(destination_number,config); 
	local sip2sipinfo

	if(didinfo == nil) then
  		sip2sipinfo = check_local_call(destination_number);
	end

	if (didinfo ~= nil) then
		call_direction = "inbound";
	elseif (sip2sipinfo ~= nil) then 
		call_direction = "local";
	else		
		call_direction = "outbound";   
	end

return call_direction;
end


-- Check DID info 
function check_did(destination_number,config)
	local number_translation 
	number_translation = config['did_global_translation'];
	destination_number = do_number_translation(number_translation,destination_number)

	local query = "SELECT A.id as id,B.id as accountid,B.number as account_code,A.number as  did_number,A.connectcost,A.includedseconds,A.cost,A.inc,A.extensions,A.maxchannels,A.call_type,A.city,A.province,A.init_inc FROM "..TBL_DIDS.." AS A,"..TBL_USERS.." AS B WHERE A.status=0 AND B.status=0 AND B.deleted=0 AND B.id=A.accountid AND A.number =\"" ..destination_number .."\" LIMIT 1";

	Logger.debug("[CHECK_DID] Query :" .. query)

	assert (dbh:query(query, function(u)
	didinfo = u;
	end))
	return didinfo;
end

-- check Reseller DID
function check_did_reseller(destination_number,userinfo,config)
	local number_translation 
	number_translation = config['did_global_translation'];
	destination_number = do_number_translation(number_translation,destination_number)   

	local query = "SELECT A.id as id, A.number AS number,B.cost AS cost,B.connectcost AS connectcost,B.includedseconds AS includedseconds,B.inc AS inc,A.city AS city,A.province,A.call_type,A.extensions AS extensions,A.maxchannels AS maxchannels,A.init_inc FROM "..TBL_DIDS.." AS A,"..TBL_RESELLER_PRICING.." as B WHERE A.number = \"" ..destination_number .."\"  AND B.type = '1' AND B.reseller_id = \"" ..userinfo['reseller_id'].."\" AND B.note =\"" ..destination_number .."\"";

	Logger.debug("[CHECK_DID_RESELLER] Query :" .. query)
	
	assert (dbh:query(query, function(u)
	didinfo = u;
	end))
	return didinfo;

end

-- Check local info 
function check_local_call(destination_number)
    
    local query = "SELECT sip_devices.username as username,number as accountcode,sip_devices.accountid as accountid FROM "..TBL_SIP_DEVICES.." as sip_devices,"..TBL_USERS.." as  accounts WHERE accounts.status=0 AND accounts.deleted=0 AND accounts.id=sip_devices.accountid AND username=\"" ..destination_number .."\" limit 1";

   Logger.debug("[CHECK_LOCAL_CALL] Query :" .. query)
    assert (dbh:query(query, function(u)
	sip2sipinfo = u;
    end))
    return sip2sipinfo;
end

-- Do Authentication 
function doauthentication (destination_number,from_ip)
    return ipauthentication (destination_number,from_ip)
end

-- Do IP base authentication 
function ipauthentication(destination_number,from_ip)

    local query = "SELECT "..TBL_IP_MAP..".*, (SELECT number FROM "..TBL_USERS.." where id=accountid AND status=0 AND deleted=0) AS account_code FROM "..TBL_IP_MAP.." WHERE (INET_ATON(\"" .. from_ip.. "\") & (0xFFFFFFFF & (-1 << 32 - SUBSTRING_INDEX(ip, '/',-1)))) =  ((0xFFFFFFFF & (-1 << 32 - SUBSTRING_INDEX(ip, '/',-1))) & INET_ATON(SUBSTRING_INDEX(ip,'/',1))) OR SUBSTRING( ip, 1, CHAR_LENGTH( ip ) -3 ) = \"" .. from_ip.. "\" AND prefix IN (NULL,'') OR SUBSTRING( ip, 1, CHAR_LENGTH( ip ) -3 ) = \"" .. from_ip.. "\" AND \"" .. destination_number .. "\"  RLIKE prefix ORDER BY LENGTH(prefix) DESC LIMIT 1"

    Logger.debug("[IPAUTHENTICATION] Query :" .. query)
    
    local ipinfo;
    assert (dbh:query(query, function(u)
	ipinfo = u;
	ipinfo ['type'] = 'acl';
    end))
    return ipinfo;
end


-- Do Account authorization
function doauthorization(accountcode,call_direction,destination_number,number_loop)<commit_msg>[FIX] Resolved IP map with PREFIX from same IP is not working issue<commit_after>-------------------------------------------------------------------------------------
-- ASTPP - Open Source VoIP Billing Solution
--
-- Copyright (C) 2016 iNextrix Technologies Pvt. Ltd.
-- <NAME> <<EMAIL>>
-- ASTPP Version 3.0 and above
-- License https://www.gnu.org/licenses/agpl-3.0.html
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU Affero General Public License as
-- published by the Free Software Foundation, either version 3 of the
-- License, or (at your option) any later version.
-- 
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU Affero General Public License for more details.
-- 
-- You should have received a copy of the GNU Affero General Public License
-- along with this program.  If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------------------

-- Load configuration variables from database 
function load_conf()



    local query = "SELECT name,value FROM "..TBL_CONFIG.." WHERE group_title IN ('global','opensips','callingcard')";
    Logger.debug("[LOAD_CONF] Query :" .. query)
    
    local config = {}
    assert (dbh:query(query, function(u)
      config[u.name] = u.value;      
    end))
    return config;
end

-- Get Speed dial number value
function get_speeddial_number(destination_number,accountid)  
	local query = "SELECT A.number FROM "..TBL_SPEED_DIAL.." as A,"..TBL_USERS.." as B WHERE B.status=0 AND B.deleted=0 AND B.id=A.accountid AND A.speed_num =\"" ..destination_number .."\" AND A.accountid = '"..accountid.."' limit 1";

   Logger.debug("[CHECK_SPEEDDIAL] Query :" .. query)
   
    assert (dbh:query(query, function(u)
	speeddial = u;
    end))
	
	if(speeddial and speeddial ~= nil) then
        Logger.info("[Dialplan] SPEED DIAL DESTINATION NUMBER NOT FOUND")
	    return speeddial['number']
	else 
	    return destination_number
	end

end
-- Define call direction
function define_call_direction(destination_number,accountcode,config)  
    local didinfo = check_did(destination_number,config); 
	local sip2sipinfo

	if(didinfo == nil) then
  		sip2sipinfo = check_local_call(destination_number);
	end

	if (didinfo ~= nil) then
		call_direction = "inbound";
	elseif (sip2sipinfo ~= nil) then 
		call_direction = "local";
	else		
		call_direction = "outbound";   
	end

return call_direction;
end


-- Check DID info 
function check_did(destination_number,config)
	local number_translation 
	number_translation = config['did_global_translation'];
	destination_number = do_number_translation(number_translation,destination_number)

	local query = "SELECT A.id as id,B.id as accountid,B.number as account_code,A.number as  did_number,A.connectcost,A.includedseconds,A.cost,A.inc,A.extensions,A.maxchannels,A.call_type,A.city,A.province,A.init_inc FROM "..TBL_DIDS.." AS A,"..TBL_USERS.." AS B WHERE A.status=0 AND B.status=0 AND B.deleted=0 AND B.id=A.accountid AND A.number =\"" ..destination_number .."\" LIMIT 1";

	Logger.debug("[CHECK_DID] Query :" .. query)

	assert (dbh:query(query, function(u)
	didinfo = u;
	end))
	return didinfo;
end

-- check Reseller DID
function check_did_reseller(destination_number,userinfo,config)
	local number_translation 
	number_translation = config['did_global_translation'];
	destination_number = do_number_translation(number_translation,destination_number)   

	local query = "SELECT A.id as id, A.number AS number,B.cost AS cost,B.connectcost AS connectcost,B.includedseconds AS includedseconds,B.inc AS inc,A.city AS city,A.province,A.call_type,A.extensions AS extensions,A.maxchannels AS maxchannels,A.init_inc FROM "..TBL_DIDS.." AS A,"..TBL_RESELLER_PRICING.." as B WHERE A.number = \"" ..destination_number .."\"  AND B.type = '1' AND B.reseller_id = \"" ..userinfo['reseller_id'].."\" AND B.note =\"" ..destination_number .."\"";

	Logger.debug("[CHECK_DID_RESELLER] Query :" .. query)
	
	assert (dbh:query(query, function(u)
	didinfo = u;
	end))
	return didinfo;

end

-- Check local info 
function check_local_call(destination_number)
    
    local query = "SELECT sip_devices.username as username,number as accountcode,sip_devices.accountid as accountid FROM "..TBL_SIP_DEVICES.." as sip_devices,"..TBL_USERS.." as  accounts WHERE accounts.status=0 AND accounts.deleted=0 AND accounts.id=sip_devices.accountid AND username=\"" ..destination_number .."\" limit 1";

   Logger.debug("[CHECK_LOCAL_CALL] Query :" .. query)
    assert (dbh:query(query, function(u)
	sip2sipinfo = u;
    end))
    return sip2sipinfo;
end

-- Do Authentication 
function doauthentication (destination_number,from_ip)
    return ipauthentication (destination_number,from_ip)
end

-- Do IP base authentication 
function ipauthentication(destination_number,from_ip)

    local query = "SELECT "..TBL_IP_MAP..".*, (SELECT number FROM "..TBL_USERS.." where id=accountid AND status=0 AND deleted=0) AS account_code FROM "..TBL_IP_MAP.." WHERE ((INET_ATON(\"" .. from_ip.. "\") & (0xFFFFFFFF & (-1 << 32 - SUBSTRING_INDEX(ip, '/',-1)))) =  ((0xFFFFFFFF & (-1 << 32 - SUBSTRING_INDEX(ip, '/',-1))) & INET_ATON(SUBSTRING_INDEX(ip,'/',1)))) AND ((SUBSTRING( ip, 1, CHAR_LENGTH( ip ) -3 ) = \"" .. from_ip.. "\" AND prefix IN (NULL,'')) OR (SUBSTRING( ip, 1, CHAR_LENGTH( ip ) -3 ) = \"" .. from_ip.. "\" AND \"" .. destination_number .. "\"  RLIKE prefix)) ORDER BY LENGTH(prefix) DESC LIMIT 1"

    Logger.debug("[IPAUTHENTICATION] Query :" .. query)
    
    local ipinfo;
    assert (dbh:query(query, function(u)
	ipinfo = u;
	ipinfo ['type'] = 'acl';
    end))
    return ipinfo;
end


-- Do Account authorization
function doauthorization(accountcode,call_direction,destination_number,number_loop)
{'fraction_non_alphanumeric': 0.15607278882776132, 'fraction_numerical': 0.007278882776132036, 'mean_word_length': 3.4979063570612867, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, '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': '2360615', 'n_tokens_mistral': 3841, 'n_tokens_neox': 3707, 'n_words': 1033}