--[[ - @author Bruno Massa - @file boot.lua - Coordinate all processes to start Ajato ]] --[[ - Find the appropriate configuration directory. - - Try finding a matching configuration directory by stripping the website's - hostname from left to right and pathname from right to left. The first - configuration file found will be used; the remaining will ignored. If no - configuration file is found, return a default value confdir ..'/default'. - - Example for a fictitious site installed at - http://www.exemple.com:8080/mysite/test/ the 'settings.lua' is searched in - the following directories: - - 1. confdir ..'/8080.www.exemple.com.mysite.test' - 2. confdir ..'/www.exemple.com.mysite.test' - 3. confdir ..'/exemple.com.mysite.test' - 4. confdir ..'/com.mysite.test' - - 5. confdir ..'/8080.www.exemple.com.mysite' - 6. confdir ..'/www.exemple.com.mysite' - 7. confdir ..'/exemple.com.mysite' - 8. confdir ..'/com.mysite' - - 9. confdir ..'/8080.www.exemple.com' - 10. confdir ..'/www.exemple.com' - 11. confdir ..'/exemple.com' - 12. confdir ..'/com' - - 13. confdir ..'/default' - - @param require_settings - Boolean, Only configuration directories with an existing settings.lua file - will be recognized. Defaults to TRUE. During initial installation, - this is set to FALSE so that Ajato can detect a matching directory, - then create a new settings.lua file in it. - @param reset - Boolean, Force a full search for matching directories even if one had been - found previously. - @return - String, The path of the matching directory. ]] function ajato_conf_path(require_settings, reset) local conf = '' return function(require_settings, reset) require_settings = require_settings or true if hv(conf) and not reset then return conf end local confdir = './sites' local path if sys['get'] and sys['get']['q'] then path = sys['get']['q'] else path = '' end local uri = ajato_explode(path, '/') local server = ajato_explode(SAPI.Request.servervariable('SERVER_NAME'), '.') -- for i = #uri - 1, 1, -1 do -- for j = #server, 1, -1 do -- dir = implode('.', array_slice(server, -j)) . implode('.', array_slice(uri, 0, i)); -- if (file_exists(confdir ..'/'.. dir ..'/settings.lua') or -- (not require_settings and file_exists(confdir ..'/'.. dir))) then -- conf = confdir ..'/'.. dir -- return conf -- end -- end -- end conf = confdir ..'/default' return conf end end; ajato_conf_path = ajato_conf_path() --[[ - Load the site settings and initialize some basic useful - variables. - - One of the first things that happen on Ajato is checking if - it has been installed. If the default.lua has the information - about the current site, the process continues. If not, the install - process takes up - - It also splits the cgi variable that comes from Kepler - into two tables: sys['get'] and sys['post'] ]] function ajato_init() -- Fill the tables besed on which request method sys['get'] = cgilua.QUERY or {} sys['post'] = cgilua.POST or {} -- The post data should respect the parent order. Even -- its not as #tree if hv(sys['post']) then local post = {} for index, value in pairs(sys['post']) do local patterns = {} for parent in index:gmatch('[%a_]+') do patterns[#patterns + 1] = parent end local post_temp = post for i, v in pairs(patterns) do for n = i, #patterns - 1, 1 do post_temp[v] = post_temp[v] or {} post_temp = post_temp[v] end post_temp[v] = value end post_temp = value end sys['post'] = post end -- Load the site settings ajato_include_once(ajato_conf_path() ..'/settings.lua') -- It is now useless cgi = nil end --[[ - Load all variables stored on database ]] function ajato_variable_del(variable) if variable then db_query('DELETE FROM {variable} WHERE name = %q', variable) -- Delete the current value if sys['variables'] and sys['variables'][variable] then sys['variables'][variable] = nil end end end --[[ - Get the internal variable value or the default value - - Some modules need individual values to be recorded into the SQL. - All these variables are loaded on boot and modules can access them - thru this function - - @param variable - String, the variable name - @param default - Any type, in case the variable doesnt exist - @return - Any type, the variable value ]] function ajato_variable_get(variable, default) -- In case variables were not loaded yet if not sys['variables'] and sys['db_conn'] then ajato_variable_init() end if not sys['variables'] then sys['variables'] = {} end return sys['variables'][variable] or default end --[[ - Load all internal variables stored on database ]] function ajato_variable_init() -- Initialize the variables holer sys['variables'] = {} local variables = db_query('SELECT * FROM {variable}') if hv(variables) then for variable in db_rows(variables) do sys['variables'][variable['name']] = ajato_unserialize(variable['value']) end end end --[[ - Save a new value to an internal variable - - @param variable - String, the variable name - @param value - Any type, the variable data ]] function ajato_variable_set(variable, value) -- Check if the variables existed if not variable or not value then return end -- Set the new value on sys['variables'] if not hv(sys['variables']) then ajato_variable_init() end if sys['variables'][variable] then sys['variables'][variable] = value if type(value) == 'table' then value = ajato_serialize(value) end db_query('UPDATE {variable} SET value = %q WHERE name = %q', value, variable) else sys['variables'][variable] = value value = ajato_serialize(value) db_query('INSERT INTO {variable} (value, name) VALUES (%q, %q)', value, variable) end end --[[ - Log a system message. - - @param mtype - String, The category to which this message belongs. - @param message - String, The message to store in the log. See t() for documentation - on how $message and $variables interact. Keep $message - translatable by not concatenating dynamic values into it! - @param variables - Table, Values to replace in the message on display or - NULL if message is already translated or not possible to - translate. - @param severity - String, The severity of the message, as per RFC 3164 - @param link - String, A link to associate with the message. - - @see watchdog_severity_levels ]] function watchdog(mtype, message, variables, severity, link) -- global base_root severity = severity or C.WATCHDOG_NOTICE variables = variables or {} base_root = base_root or '' -- Prepare the fields to be logged log_message = { ['type'] = mtype, ['message'] = message, ['variables'] = variables, ['severity'] = severity, ['link'] = link, ['user'] = user, ['request_uri'] = base_root .. ajato_path_format(sys['get']['q']), ['referer'] = '', --referer_uri(), ['ip'] = '', --ip_address(), ['timestamp'] = os.time(), } -- Call the logging hooks to log/process the message ajato_module_hook('watchdog', log_message) end