diff --git a/resources/install/scripts/app/xml_handler/resources/scripts/dialplan/dialplan.lua b/resources/install/scripts/app/xml_handler/resources/scripts/dialplan/dialplan.lua index 2589d28a77..50bf6291aa 100644 --- a/resources/install/scripts/app/xml_handler/resources/scripts/dialplan/dialplan.lua +++ b/resources/install/scripts/app/xml_handler/resources/scripts/dialplan/dialplan.lua @@ -1,6 +1,6 @@ -- xml_handler.lua -- Part of FusionPBX --- Copyright (C) 2013-2016 Mark J Crane +-- Copyright (C) 2013-2017 Mark J Crane -- All rights reserved. -- -- Redistribution and use in source and binary forms, with or without @@ -24,6 +24,7 @@ -- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE -- POSSIBILITY OF SUCH DAMAGE. +--includes local cache = require"resources.functions.cache" local log = require"resources.functions.log"["xml_handler"] @@ -32,19 +33,37 @@ call_context = "public"; end ---get the cache - XML_STRING, err = cache.get("dialplan:" .. call_context) +--set the defaults + if (context_type == nil) then + context_type = "multiple"; + end + domain_name = 'global'; - if debug['cache'] then +--get the context + local context_name = call_context; + if (call_context == "public" or string.sub(call_context, 0, 7) == "public@" or string.sub(call_context, -7) == ".public") then + context_name = 'public'; + end + --freeswitch.consoleLog("notice", "[xml_handler] ".. context_type .. " key:" .. key .. "\n"); + +--set the key + local key = "dialplan:" .. call_context; + if (context_name == 'public' and context_type == "single") then + key = "dialplan:" .. call_context .. ":" .. destination_number; + end + +--get the cache + XML_STRING, err = cache.get(key); + if (debug['cache']) then if XML_STRING then - log.notice("dialplan:"..call_context.." source: memcache"); + log.notice(key.." source: cache"); elseif err ~= 'NOT FOUND' then log.notice("error get element from cache: " .. err); end end --set the cache - if not XML_STRING then + if (not XML_STRING) then --connect to the database local Database = require "resources.functions.database"; @@ -70,25 +89,54 @@ table.insert(xml, [[ ]]); --get the dialplan xml - sql = "select dialplan_xml from v_dialplans as p "; - if (call_context == "public" or string.sub(call_context, 0, 7) == "public@" or string.sub(call_context, -7) == ".public") then - sql = sql .. "where p.dialplan_context = :call_context "; + if (context_name == 'public' and context_type == 'single') then + sql = "select d.domain_name, dialplan_xml from v_dialplans as p, v_domains as d "; + sql = sql .. "where ( "; + sql = sql .. " p.dialplan_uuid in (select dialplan_uuid from v_destinations where destination_number = :destination_number) "; + sql = sql .. " or (p.dialplan_context like '%public%' and p.domain_uuid is null) "; + sql = sql .. ") "; + sql = sql .. "and p.domain_uuid = d.domain_uuid "; + sql = sql .. "and (p.hostname = :hostname or p.hostname is null) "; + sql = sql .. "and p.dialplan_enabled = 'true' "; + sql = sql .. "order by p.dialplan_order asc "; + local params = {destination_number = destination_number, hostname = hostname}; + if (debug["sql"]) then + freeswitch.consoleLog("notice", "[dialplan] SQL: " .. sql .. "; params:" .. json.encode(params) .. "\n"); + end + local found = false; + dbh:query(sql, params, function(row) + if (row.domain_uuid ~= nil) then + domain_name = row.domain_name; + end + table.insert(xml, row.dialplan_xml); + found = true; + end); + if (not found) then + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + table.insert(xml, [[ ]]); + end else - sql = sql .. "where (p.dialplan_context = :call_context or p.dialplan_context = '${domain_name}') "; + sql = "select dialplan_xml from v_dialplans as p "; + if (context_name == "public") then + sql = sql .. "where p.dialplan_context = :call_context "; + else + sql = sql .. "where (p.dialplan_context = :call_context or p.dialplan_context = '${domain_name}') "; + end + sql = sql .. "and (p.hostname = :hostname or p.hostname is null) "; + sql = sql .. "and p.dialplan_enabled = 'true' "; + sql = sql .. "order by p.dialplan_order asc "; + local params = {call_context = call_context, hostname = hostname}; + if (debug["sql"]) then + freeswitch.consoleLog("notice", "[dialplan] SQL: " .. sql .. "; params:" .. json.encode(params) .. "\n"); + end + dbh:query(sql, params, function(row) + table.insert(xml, row.dialplan_xml); + end); end - sql = sql .. "and (hostname = :hostname or hostname is null) "; - sql = sql .. "and p.dialplan_enabled = 'true' "; - sql = sql .. "order by "; - sql = sql .. "p.dialplan_order asc "; - local params = {call_context = call_context, hostname = hostname}; - if (debug["sql"]) then - freeswitch.consoleLog("notice", "[dialplan] SQL: " .. sql .. "; params:" .. json.encode(params) .. "\n"); - end - local x = 0; - local pass - dbh:query(sql, params, function(row) - table.insert(xml, row.dialplan_xml); - end); --set the xml array and then concatenate the array to a string table.insert(xml, [[ ]]); @@ -98,19 +146,19 @@ --set the cache if cache.support() then - cache.set("dialplan:" .. call_context, XML_STRING, expire["dialplan"]) + cache.set(key, XML_STRING, expire["dialplan"]) end --send the xml to the console if (debug["xml_string"]) then - local file = assert(io.open(temp_dir .. "/dialplan-" .. call_context .. ".xml", "w")); + local file = assert(io.open(temp_dir .. "/" .. key .. ".xml", "w")); file:write(XML_STRING); file:close(); end --send to the console if (debug["cache"]) then - log.notice("dialplan:"..call_context.." source: database"); + log.notice(key .. " source: database"); end --close the database connection