Sindbad~EG File Manager

Current Path : /opt/nginxhttpd_/etc/openresty_config/lua/lib/
Upload File :
Current File : //opt/nginxhttpd_/etc/openresty_config/lua/lib/o2switch_utils.lua

--[[
-- This file contains a modules with the some utilities functions. For instance to extract the real domain name.
--]]

local _M = {}

local cjson = require "cjson"
local o2config = require "lib/o2switch_config"
local o2debug = require "lib/o2switch_debug"
local string_gsub = string.gsub
local string_lower = string.lower
local type = type

---  Remove the 'www.' from the hostname provided as argument
-- @param host The raw hostname
-- @return string Hostname without www.
function _M.extractDomainWww(host)
    return string_lower((host:gsub('^www%.', '', 1)))
end

--- Extract the main domain name by removing all known proxy subdomain
-- @param host The raw hostname
-- @return string Hostname without all known proxy subdomain
function _M.extractDomain(host)
    if host == nil then
        return nil
    end
    -- local name = host
    if o2config.serverType == 'mutu' then
        return string_lower((host:gsub('^(%w+).', {
            ['cpanel'] = '',
            ['whm'] = '',
            ['webmail'] = '', 
            ['autoconfig'] = '',
            ['autodiscover'] = '',
            ['cpcalendars'] = '',
            ['cpcontacts'] = '',
            ['webdisk'] = '',
            ['mail'] = ''
        }, 1)))
    end
    return host
end

--- Remove one subdomain from the name : usefull in case of wildcard ...
-- @param host The raw hostname
-- @return string Remove the first subdomain
function _M.transformSubdomainToWildcard(name)
    local name = name:gsub('^[a-zA-Z0-9-]+%.', '*.');
    -- --o2debug.debug("transformSubdomainToWildcard called, new name is : " .. name)
    return name
end

--- Configure the proxy pass with default value
function _M.defaultProxyPassMutu()
    ngx.var.proxyPassProtocol = ngx.var.https == 'on' and 'https' or 'http'
    ngx.var.proxyPassIp = ngx.var.server_addr
    ngx.var.proxyPassPort = ngx.var.https == 'on' and '4430' or '8081'
    ngx.var.proxyPassFullLine =
    (ngx.var.https == 'on' and 'https' or 'http')
            ..  '://'
            .. ngx.var.server_addr
            .. ':'
            .. (ngx.var.https == 'on' and '4430' or '8081')
end

--- Return a Nginx final error, this error can't be catch by Nginx
-- @param status HTTP status code
-- @param errorMessage The error message
-- @return nothing, send the error message directly and end.
function _M.ngxFinalError(status, errorMessage)
    ngx.status = status or 503
    if errorMessage then
        ngx.header.content_type = "text/html; charset=utf-8"
        ngx.header.cache_control = "private, max-age=0, no-store, no-cache, must-revalidate, post-check=0, pre-check=0"
        ngx.header.expires = "Thu, 01 Jan 1970 00:00:01 GMT"
        ngx.header_referer_policy = "same-origin"
        ngx.say(errorMessage)
    end
    ngx.exit(200) -- 200 to force the error to be final (cf. openresty doc)
end

-- Send a json response from a lua table
function _M.sendJsonResponse(data, status) 
    ngx.status = status or 200
    ngx.header.cache_control = "private, max-age=0, no-store, no-cache, must-revalidate, post-check=0, pre-check=0"
    ngx.header.expires = "Thu, 01 Jan 1970 00:00:01 GMT"
    ngx.header_referer_policy = "same-origin"
    ngx.header.content_type = "application/json; charset=utf-8"
    if type(data) ~= 'table' then
        o2debug.debugErr("Date is not a table")
        ngx.say('Err: wrong data type')
    else
        ngx.say(cjson.encode(data))
    end
    ngx.exit(200)
end

-- Send a 302/301 redirect response to the URL given or if no url is provided, the current URL in the context of nginx (self refresh page)
function _M.sendRedirectResponse(url, status)
    local redirectTo = type(url) == 'string' and url or (ngx.var.scheme .. "://" .. ngx.var.http_host .. ngx.var.request_uri)
    ngx.status = (status == 301 or status == 302) and status or 302
    ngx.header.cache_control = "private, max-age=0, no-store, no-cache, must-revalidate, post-check=0, pre-check=0"
    ngx.header.expires = "Thu, 01 Jan 1970 00:00:01 GMT"
    ngx.header.content_type = "text/html; charset=UTF-8"
    ngx.header_referer_policy = "same-origin"
    ngx.header.location = redirectTo
    ngx.say(((string_gsub([=[
        <HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
        <TITLE>301 Moved</TITLE></HEAD><BODY>
        <H1>301 Moved</H1>
        The document has moved
        <A HREF="#url">here</A>.
        </BODY></HTML>
    ]=], '#url', redirectTo))))
    ngx.exit(ngx.HTTP_OK)
end


return _M

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists