#!/usr/bin/perl
##LICENSE##
=head
Basic app process tasks: enables profiling and output buffering.
@package RVL::Task::Init
=cut
package RVL::Task::Init;
use base qw(RVL::DecorateProcess);
use Class::Std::Utils;
{
sub process {
my ($this, $input, $output) = @_;
RVL::logMessage('', __CONSTANT__::RVL_LOG_DEBUG);
$this->SUPER::process($input, $output);
}
}
=head
=cut
package RVL::Task::SetupORM;
use base qw(RVL::DecorateProcess);
use Class::Std::Utils;
{
sub process {
my ($this, $input, $output) = @_;
RVL::logMessage('', __CONSTANT__::RVL_LOG_DEBUG);
$this->SUPER::process($input, $output);
}
}
=head
=cut
package RVL::Task::ResolveManager;
use base qw(RVL::DecorateProcess);
use Class::Std::Utils;
{
sub process($$) {
my ($this, $input, $output) = @_;
RVL::logMessage('', __CONSTANT__::RVL_LOG_DEBUG);
$req = $$input->getRequest();
my ($moduleName) = ${$req}->get('moduleName');
my ($managerName) = ${$req}->get('managerName');
#set default theme
my ($defaultTheme) = RVL::Config::get('site.defaultTheme');
${$input}->{theme} = $defaultTheme;
my ($getDefaultMgr) = 0;
if ($moduleName && !$managerName) {
${$req}->set('managerName', $moduleName);
$managerName = $moduleName;
}
if (!$moduleName || !$managerName) {
RVL::logMessage('Module and manager names could not be determined from request', __CONSTANT__::RVL_LOG_DEBUG);
$getDefaultMgr = 1;
} else {
RVL::logMessage("Load Module '$moduleName' and Manager '$managerName'", __CONSTANT__::RVL_LOG_DEBUG);
if (!RVL::moduleIsEnabled($moduleName)) {
RVL::raiseError('module "'.$moduleName.'" does not appear to be registered',
__CONSTANT__::RVL_ERROR_RESOURCENOTFOUND);
$getDefaultMgr = 1;
} else {
my ($c) = RVL::Config::singleton();
my ($conf) = $c->ensureModuleConfigLoaded($moduleName);
my ($mgrPath) = __CONSTANT__::RVL_MOD_DIR . '/' . $moduleName . '/classes/';
my ($retMgrName) = $this->getManagerName($managerName, $mgrPath, $conf);
if ($retMgrName eq 0) {
RVL::raiseError("Specified manager '$managerName' could not be found, ".
"defaults loaded, pls ensure full manager name is present in module's conf.yaml",
__CONSTANT__::RVL_ERROR_RESOURCENOTFOUND);
}
my ($fullMgrName) = ($retMgrName) ? $retMgrName : $this->getManagerName($moduleName, __CONSTANT__::RVL_MOD_DIR . '/default/classes/', $conf);
if ($fullMgrName ne 0) {
my ($classPath) = $mgrPath . $fullMgrName . '.pm';
if (-f $classPath) {
my ($mgr);
eval{ require $classPath; $mgr = new $fullMgrName(); };
if ($@) {
RVL::raiseError("$@", __CONSTANT__::RVL_ERROR_RESOURCENOTFOUND);
$getDefaultMgr = 1;
} else {
${$input}->{moduleName} = $moduleName;
${$input}->set('manager', \$mgr);
my ($req) = ${$input}->getRequest();
${$req}->set('moduleName', $moduleName);
${$req}->set('managerName', $managerName);
${$input}->setRequest($req);
}
}
} else {
RVL::logMessage("Could not find module '$moduleName' manager '$managerName' ", __CONSTANT__::RVL_LOG_WARNING);
$getDefaultMgr = 1;
}
}
}
if ($getDefaultMgr) {
my ($ok) = $this->getConfiguredDefaultManager($input);
if ($ok ne 1) {
$ok = $this->getDefaultManager($input);
}
}
$this->SUPER::process($input, $output);
}
sub getManagerName{
my ($this, $managerName, $path, $conf) = @_;
RVL::logMessage('', __CONSTANT__::RVL_LOG_DEBUG);
my (@confValues) = keys %{$conf};
my (@confValuesLowerCase) = RVL::String::array_map('lc', @confValues);
$managerName = RVL::Inflector::getManagerNameFromSimplifiedName($managerName);
my ($isFound) = RVL::String::array_search($managerName, @confValues);
my (@matches);
if ($isFound ne '') {
push(@matches, $confValues[$isFound]);
}
$isFound = RVL::String::array_search(lc($managerName), @confValuesLowerCase);
if ($isFound ne '') {
push(@matches, $confValues[$isFound]);
}
foreach my $match (@matches) {
return $match if (-f $path . $match . '.pm');
}
return 0;
}
sub getConfiguredDefaultManager {
my ($this, $input) = @_;
RVL::logMessage('', __CONSTANT__::RVL_LOG_DEBUG);
my ($defaultModule) = RVL::Config::get('site.defaultModule');
my ($defaultMgr) = RVL::Config::get('site.defaultManager');
my ($c) = RVL::Config::singleton();
my ($conf) = $c->ensureModuleConfigLoaded($defaultModule);
use RVL::Inflector;
my ($mgrName) = RVL::Inflector::caseFix(RVL::Inflector::getManagerNameFromSimplifiedName($defaultMgr));
my ($path) = __CONSTANT__::RVL_MOD_DIR . '/' .$defaultModule . '/classes/' . $mgrName.'.pm';
RVL::logMessage($path, __CONSTANT__::RVL_LOG_DEBUG);
if (!-f($path)) {
RVL::raiseError('could not locate default manager, ' . $path,
__CONSTANT__::RVL_ERROR_NOFILE);
return 0;
}
my ($mgr);
eval{
require $path;
$mgr = new $mgrName();
};
if ($@) {
RVL::fatalErrors($@);
}
# Input and Req is REF ใช้ $$ อ้างถึงตำแหน่ง address
${$input}->{moduleName} = $defaultModule;
${$input}->{managerName} = $defaultMgr;
${$input}->set('manager', \$mgr);
my ($req) = ${$input}->getRequest();
${$req}->set('moduleName', $defaultModule);
${$req}->set('managerName', $defaultMgr);
${$input}->setRequest($req);
return 1;
}
sub getDefaultManager{
my ($this, $input) = @_;
RVL::logMessage('', __CONSTANT__::RVL_LOG_DEBUG);
my ($defaultModule) = 'default';
my ($defaultMgr) = 'default';
my ($mgrName) = RVL::Inflector::caseFix(
RVL::Inflector::getManagerNameFromSimplifiedName($defaultMgr));
my ($path) = __CONSTANT__::RVL_MOD_DIR .'/'.$defaultModule.'/classes/'.$mgrName.'.pm';
my ($mgr);
eval{
require $path;
$mgr = new $mgrName();
};
if ($@) {
RVL::fatalErrors($@);
}
${$input}->{moduleName} = $defaultModule;
${$input}->set('manager', $mgr);
my ($req) = ${$input}->getRequest();
${$req}->set('moduleName', $defaultModule);
${$req}->set('managerName', $defaultMgr);
${$input}->setRequest($req);
return 1;
}
}
=head
=cut
package RVL::Task::LicenseCycle;
use base qw(RVL::DecorateProcess);
use Class::Std::Utils;
{
sub process {
my ($this, $input, $output) = @_;
RVL::logMessage('', __CONSTANT__::RVL_LOG_DEBUG);
my $cycleLicense = RVL::LicenseCycle->new();
$cycleLicense->checkFileConfigLicenseCycle();
$this->SUPER::process($input, $output);
}
}
=head
=cut
package RVL::Task::LoadCpHandle;
use base qw(RVL::DecorateProcess);
use Class::Std::Utils;
{
sub process {
my ($this, $input, $output) = @_;
RVL::logMessage('', __CONSTANT__::RVL_LOG_DEBUG);
my ($req) = ${$input}->getRequest();
my ($moduleName) = $$req->get("moduleName");
my ($fileCPHandle) = __CONSTANT__::RVL_MOD_DIR . "/$moduleName/classes/CPHandle.pm";
if (-f $fileCPHandle) {
require $fileCPHandle;
}
$this->SUPER::process($input, $output);
}
}
=head
=cut
package RVL::Task::LoadLibsModule;
use base qw(RVL::DecorateProcess);
use Class::Std::Utils;
{
sub process {
my ($this, $input, $output) = @_;
RVL::logMessage('', __CONSTANT__::RVL_LOG_DEBUG);
my ($req) = ${$input}->getRequest();
my ($moduleName) = $$req->get("moduleName");
my ($modulePackage) = ucfirst(lc($moduleName)) . 'Libs';
my ($fileLibsModule) = __CONSTANT__::RVL_MOD_DIR . "/$moduleName/classes/Libs.pm";
if (-f $fileLibsModule) {
require $fileLibsModule;
eval {
my ($callInit) = 'Libs::' . ucfirst(lc($moduleName)) . '::Init';
my ($oInit) = new $callInit();
$oInit->run();
};
}
$this->SUPER::process($input, $output);
}
}
=head
=cut
package RVL::Task::CreateSession;
use base qw(RVL::DecorateProcess);
use RVL::Session;
use Class::Std::Utils;
{
sub process {
my ($this, $input, $output) = @_;
RVL::logMessage('', __CONSTANT__::RVL_LOG_DEBUG);
my ($session) = RVL::Session::singleton();
${$input}->set('session', $session);
$this->SUPER::process($input, $output);
}
}
=head
=cut
package RVL::Task::SetupLangSupport;
use base qw(RVL::DecorateProcess);
use Class::Std::Utils;
{
sub process {
my ($this, $input, $output) = @_;
RVL::logMessage('', __CONSTANT__::RVL_LOG_DEBUG);
my $lang = RVL::Translation::transformLangID(RVL::Config::get('translation.fallbackLang'));
if ($lang eq '') {
$lang = $this->_resolveLanguage();
}
if(!defined RVL::Session::singleton()->param('aPrefs.language') || RVL::Session::singleton()->param('aPrefs.language') eq ''){
RVL::Session::singleton()->param(-name=> 'aPrefs.language', -value=> $lang);
}else{
$lang = RVL::Session::singleton()->param('aPrefs.language');
}
my ($req) = ${$input}->getRequest();
my ($moduleDefault);
if (RVL::Config::get('translation.defaultLangBC')) {
$moduleDefault = 'default';
} else {
$moduleDefault = RVL::Config::get('site.defaultModule');
}
$moduleCurrent = ${$req}->get('moduleName')
? ${$req}->get('moduleName')
: $moduleDefault;
my ($translation) = RVL::Translation::getTranslations($moduleCurrent, $moduleDefault, $lang);
RVL::Registry::singleton()->set('translation', \$translation);
$this->SUPER::process($input, $output);
}
sub resolveLanguageFromBrowser {
my ($this) = @_;
$ret = '';
if (defined $ENV{'HTTP_ACCEPT_LANGUAGE'}) {
my ($env) = $ENV{'HTTP_ACCEPT_LANGUAGE'};
my (@langs) = split(/[\s,]+/, substr($env, 0, index($env . ';', ';')));
foreach my $langCode (@langs) {
$langCode =~s/^en\-.*/en/gi;
$lang = $langCode . '-' . RVL::Translation::getFallbackCharset();
if (RVL::Translation::isAllowedLanguage($lang)) {
$ret = $lang;
last;
}
}
}
return $ret;
}
sub resolveLanguageFromDomain {
my ($this) = @_;
$ret = '';
if (defined $ENV{'HTTP_HOST'}) {
my (@aLangCode) = split(/\./, $ENV{'HTTP_HOST'});
my ($langCode) = pop(@aLangCode);
}
return $ret;
}
sub _resolveLanguage {
my ($this) = @_;
my ($req) = RVL::Request::singleton();
my ($lang) = $req->get('lang');
my ($anonRequest) = RVL::Session::isFirstAnonRequest();
if ($lang eq '' || !RVL::Translation::isAllowedLanguage($lang)) {
$lang = RVL::Session::singleton()->param('aPrefs.language');
if (!defined $lang || !RVL::Translation::isAllowedLanguage($lang) || $anonRequest) {
$lang = $this->resolveLanguageFromBrowser();
if ($lang eq '') {
$lang = $this->resolveLanguageFromDomain();
}
}
}
return $lang;
}
}
=head
=cut
package RVL::Task::SetupLocale;
use base qw(RVL::DecorateProcess);
use Class::Std::Utils;
{
sub process {
my ($this, $input, $output) = @_;
RVL::logMessage('', __CONSTANT__::RVL_LOG_DEBUG);
$this->SUPER::process($input, $output);
}
}
=head
=cut
package RVL::Task::AuthenticateRequest;
use base qw(RVL::DecorateProcess);
use Class::Std::Utils;
{
sub process {
my ($this, $input, $output) = @_;
RVL::logMessage('', __CONSTANT__::RVL_LOG_DEBUG);
my ($authFilterChain) = RVL::Config::get('site.authFilterChain');
my ($direct);
if (RVL::Config::get('site.authFilterChain') && $authFilterChain ne '') {
require "RVL/AuthenticateRequest/$authFilterChain.pm";
$direct = $authFilterChain->run($input, $output);
} else {
### วิธีการ Login
}
if ($direct ne '') {
RVL::logMessage('redirect URL: ' . $url, __CONSTANT__::RVL_LOG_DEBUG);
print RVL::Session::singleton()->header(-location => $direct);
exit;
}
$this->SUPER::process($input, $output);
}
}
=head
=cut
package RVL::Task::SetupPerms;
use base qw(RVL::DecorateProcess);
use Class::Std::Utils;
{
sub process {
my ($this, $input, $output) = @_;
RVL::logMessage('', __CONSTANT__::RVL_LOG_DEBUG);
$this->SUPER::process($input, $output);
}
}
=head
=cut
package RVL::Task::BuildHeaders;
use base qw(RVL::DecorateProcess);
use CGI ':standard';
use Class::Std::Utils;
{
sub process {
my ($this, $input, $output) = @_;
$this->SUPER::process($input, $output);
RVL::logMessage('', __CONSTANT__::RVL_LOG_DEBUG);
if (RVL::runningFromCLI()) {
}
#my ($session) = RVL::Session::singleton();
my (@header) = ();
if(defined ${$output}->{contentType}){
@header = (
-type => ${$output}->{contentType},
-X_Powered_By => 'RV Framework http://www.rvglobalsoft.com',
);
}else{
@header = (
-type => 'text/html; charset=' . ${$output}->{charset},
-X_Powered_By => 'RV Framework http://www.rvglobalsoft.com',
);
my ($obj) = RVL::Output::singleton();
foreach my $listOutput ($obj->getHeader()) {
push(@header, $listOutput);
}
}
if (!RVL::runningFromCLI()) {
print RVL::Session::singleton()->header(@header);
}
}
}
=head
=cut
package RVL::Task::BuildView;
use base qw(RVL::DecorateProcess);
use RVL::HtmlSimpleView;
use Class::Std::Utils;
{
sub process {
my ($this, $input, $output) = @_;
$this->SUPER::process($input, $output);
RVL::logMessage('', __CONSTANT__::RVL_LOG_DEBUG);
${$output}->{data} = RVL::HtmlSimpleView::singleton()->render(${$output});
}
}
=head
=cut
package RVL::Task::SetupGui;
use base qw(RVL::DecorateProcess);
use Class::Std::Utils;
{
sub process {
my ($this, $input, $output) = @_;
$this->SUPER::process($input, $output);
RVL::logMessage('', __CONSTANT__::RVL_LOG_DEBUG);
}
}
=head
=cut
package RVL::Task::BuildOutputData;
use base qw(RVL::DecorateProcess);
use Class::Std::Utils;
{
sub process {
my ($this, $input, $output) = @_;
$this->SUPER::process($input, $output);
RVL::logMessage('', __CONSTANT__::RVL_LOG_DEBUG);
$this->addOutputData($output);
}
sub addOutputData {
my ($this, $output) = @_;
RVL::logMessage('', __CONSTANT__::RVL_LOG_DEBUG);
if (!RVL::runningFromCLI()) {
${$output}->{remoteIp} = $ENV{'REMOTE_ADDR'};
# TODO: หา currUrl
# $$output->{currUrl} = (RVL::Config::get('site.inputUrlHandlers') eq 'Horde_Routes')
# ? SGL_Task_BuildOutputData::getCurrentUrlFromRoutes()
# : $ENV['PHP_SELF'];
}
${$output}->{currLang} = RVL::getCurrentLang();
${$output}->{charset} = RVL::getCurrentCharset();
# TODO: หา currFullLang
# $$output->currFullLang = $_SESSION['aPrefs']['language'];
${$output}->{langDir} = (${$output}->{currLang} eq 'ar'
|| ${$output}->{currLang} eq 'he')
? 'rtl' : 'ltr';
# setup theme
if (!defined(${$output}->{theme})) {
${$output}->{theme} = 'default';
}
# check if theme is affected by the current manager
if (defined(${$output}->{manager})) {
${$output}->{managerName} = RVL::Inflector::caseFix(ref ${${$output}->{manager}});
if (RVL::Config::get(${$output}->{managerName} . '.theme')) {
${$output}->{theme} = RVL::Config::get(${$output}->{managerName} . '.theme');
}
}
my ($c) = RVL::Config::singleton();
${$output}->{conf} = $c->getAll();
# TODO: หา webRoot define constant RVL_BASE_URL
${$output}->{webRoot} = __CONSTANT__::RVL_BASE_URL;
${$output}->{imagesDir} = __CONSTANT__::RVL_BASE_URL . '/themes/' . ${$output}->{theme} . '/images';
${$output}->{sessID} = RVL::Session::singleton()->id();
${$output}->{scriptOpen} = "\n<script type='text/javascript'>\n//<![CDATA[\n";
${$output}->{scriptClose} = "\n//]]>\n</script>\n";
}
}
1;
Copyright 2K16 - 2K18 Indonesian Hacker Rulez