_loadConfigFromDir(new File(_CFGPATH_)); $syskey = System::getInstance()->getConfig('syskey'); if(empty($syskey)) throw new \UnexpectedValueException('$cfg[\'syskey\'] needs to be defined for security purpurse'); } public function doInit() { $system = System::getInstance(); $dbcfg = $system->getConfig('db'); // connect to the database if(!isset($dbcfg['option'])) $dbcfg['option'] = array(); defined('PROFILE') && Profiler::start(__METHOD__ . '::initDB'); $system->initDB($dbcfg['dsn'], $dbcfg['user'], $dbcfg['passwd'], $dbcfg['option']); $db = $system->getDB(); $efn_type = $db->escapeColumnName('type'); $efn_key = $db->escapeColumnName('key'); if(($dbv = $db->getObject("SELECT * FROM installinfo WHERE {$efn_type}='system' AND {$efn_key}='cadb'")->version) != \CACenter\System::_VERSION_) die('CRITICAL: DB-VERSION ' . $dbv . '!=' . \CACenter\System::_VERSION_ . ' NEEDED BY CACenter'); defined('PROFILE') && Profiler::stop(__METHOD__ . '::initDB'); // init the cache $cachecfg = $system->getConfig('cache'); if(empty($cachecfg)) $cachecfg['type'] = ''; $cachetype = $cachecfg['type']; unset($cachecfg['type']); defined('PROFILE') && Profiler::start(__METHOD__ . '::initCache'); $cachetype = $system->initCache($cachetype, $cachecfg); defined('PROFILE') && Profiler::stop(__METHOD__ . '::initCache'); unset($cachecfg); // init the plugin system $this->plugins = $system->getBasePlugins(); $this->plugins['cache'] = array($cachetype); // adding user plugins $installedPlugins = $system->cacheFetch('-1', 'sys.info', 'installedPlugins'); if(!is_array($installedPlugins)) { $installedPlugins = $db->getObjectList("SELECT * FROM installinfo WHERE {$efn_type}='plugin'"); $system->cacheStore('-1', 'sys.info', 'installedPlugins', $installedPlugins); } foreach($installedPlugins as $plugin) { list($type, $name) = explode(':', $plugin->key, 2); $this->plugins[$type][] = $name; } // initialize the user service $authcfg = $system->getConfig('auth'); if(empty($authcfg['type'])) $authcfg['type'] = 'mcms.MMPIAuthDefault'; if(empty($authcfg['cfg'])) $authcfg['cfg'] = array(); defined('PROFILE') && Profiler::start(__METHOD__ . '::initUserSvc'); $class = loadPlugin('auth', $authcfg['type']); // add the plugin to the list of used plugins on admin calls if(defined('_ADMIN_')) $this->usedPlugins['auth'][0] = $authcfg['type']; $system->initUserSvc($class, $authcfg['cfg']); // register the auth plugin in the system $this->plugins['auth'][0] = $system->getUserSvc(); defined('PROFILE') && Profiler::stop(__METHOD__ . '::initUserSvc'); $this->request = $system->getRequest(); $this->context = new \MCMS\Context(); $this->response = new \MCMS\Response(); // setting the default system timezone $timezone = $system->getConfig('timezone'); if(empty($timezone)) $timezone = 'UTC'; date_default_timezone_set($timezone); $defaultlocale = $system->getConfig('defaultlocale'); if(empty($defaultlocale)) $defaultlocale = 'en'; $this->context->setDefaultLocale($defaultlocale); unset($timezone, $defaultlocale); foreach($this->plugins as $cat=>$plugins) { foreach($plugins as $id=>$plugin) // ignore already initialized plugins if (!is_object($this->plugins[$cat][$id])) $this->plugins[$cat][$id] = $system->loadPlugin($cat, $plugin); } } protected function loadWebpageSettings($id, $asParent = false) { if(!is_int($id)) throw new \InvalidArgumentException('$id needs to be an integer'); $system = System::getInstance(); $db = $system->getDB(); $table = $db->escapeTableName('websites'); $efn_id = $db->escapeColumnName('id'); $maindata = $db->getObjectP("SELECT * FROM {$table} WHERE {$efn_id}=?", array($id)); $system->setSiteId($maindata->id); if(!empty($maindata->parent_id)) $this->loadWebpageSettings((int) $maindata->parent_id, true); } public function doRoute() { if(defined('_CRON_')) return; $system = System::getInstance(); $db = $system->getDB(); $table = $db->escapeTableName('websites'); $efn_domain = $db->escapeColumnName('domain'); /* if(defined('SINGLESITE')) { // single site tuning $result = $db->query("SELECT * FROM {$table} WHERE {$efn_domain}='*'"); $this->loadWebpageSettings((int) $result->fetchObject()->id); $result->close(); } else { // find page $domain = $_SERVER['HTTP_HOST']; if(empty($domain)) $domain = $_SERVER['SERVER_NAME']; $subdomain = substr($domain, strpos($domain, '.')); // echo "

seaching for ".$domain." or *.".$subdomain."

"; $subdomain = $db->escapeString($subdomain); $domain = $db->escapeString($domain); $result = $db->query("SELECT * FROM {$table} WHERE {$efn_domain}='{$domain}'"); if($result->getRowCount() > 0) { // echo '

found main-domain

'; $this->loadWebpageSettings((int) $result->fetchObject()->id); $result->close(); } else { $result->close(); $result = $db->query("SELECT * FROM {$table} WHERE {$efn_domain} LIKE '%.{$subdomain}'"); if($result->getRowCount() > 0) { // echo '

found sub-domain-catchall

'; $this->loadWebpageSettings((int) $result->fetchObject()->id); $result->close(); } else { $result->close(); $result = $db->query("SELECT * FROM {$table} WHERE {$efn_domain} LIKE '*'"); if($result->getRowCount() > 0) { // echo '

found catchall

'; $this->loadWebpageSettings((int) $result->fetchObject()->id); $result->close(); } else { // echo 'no pagedef has been found'; $result->close(); } } } } */ foreach($this->plugins['route'] as $router) $router->doRoute($this->context); } public function doBeforeRun() { foreach($this->plugins['content'] as $plugin) { defined('PROFILE') && Profiler::start(__METHOD__ . '::runPlugin::' . get_class($plugin)); $plugin->run($this->response, iPlugInContent::STEP_BEFORE_RUN, $this->context); defined('PROFILE') && Profiler::stop(__METHOD__ . '::runPlugin::' . get_class($plugin)); } $cfg = System::getInstance()->getConfig('cron'); if(empty($cfg)) $cfg = array('mode' => 'cli'); if(defined('_CRON_') && $cfg['mode'] != 'emulate') throw new ErrorException("cron can only be called from a shell", 500); } public function doRun() { if(defined('_CRON_')) return; $system = System::getInstance(); try { if($system->getTarget(iSystem::TARGET_INSTANCE) == '*') { $target = $system->getTarget(iSystem::TARGET_VIEW); if(isset($this->plugins['misc'][$target])) $this->plugins['misc'][$target]->handleRequest($system->getRequest(), $this->response, $this->context); else $this->response->setContent('invalid request: ' . $target); } else { foreach($this->plugins['content'] as $plugin) { defined('PROFILE') && Profiler::start(__METHOD__ . '::runPlugin::' . get_class($plugin)); $plugin->run($this->response, iPlugInContent::STEP_RUN, $this->context); defined('PROFILE') && Profiler::stop(__METHOD__ . '::runPlugin::' . get_class($plugin)); } } } catch(\Mammut\Exception\AccessDeniedException $e) { $this->response->addError(403, 'Access denied'); } catch(\Mammut\Exception\IllegalStateException $e) { if (defined('DEBUG')) $this->response->addError($e->getCode(), 'Internal error: '.$e->getMessage(),'run',$e); else $this->response->addError(500, 'Internal error','run'); } } public function doRunCLI() { if(defined('_CRON_')) $this->runCronJobs(); } protected function runCronJobs() { $system = System::getInstance(); $time = time(); $lastrun = (int) $system->getSiteParam(0, 'cron', 'lastrun', -1); $cronrun = false; if($lastrun <= 0 || $time - $lastrun >= 60) { echo "running cron\n"; $cronrun = true; $cronparam = array('time' => $time); $lastrun = $system->setSiteParam(0, 'cron', 'lastrun', $time); } if(!$cronrun) return; foreach($this->plugins as $plugingroup) { foreach($plugingroup as $plugin) { echo $time . ':' . $lastrun . "\n"; if($plugin instanceof \MCMS\Interfaces\iCronClient) { echo 'Running cron on:' . get_class($plugin) . "\n"; $plugin->executeCronRun($cronparam); } } } echo "\nDONE\n"; } public function doAfterRun() { if(defined('_CRON_')) return; foreach($this->plugins['content'] as $plugin) { defined('PROFILE') && Profiler::start(__METHOD__ . '::runPlugin::' . get_class($plugin)); $plugin->run($this->response, iPlugInContent::STEP_AFTER_RUN, $this->context); defined('PROFILE') && Profiler::stop(__METHOD__ . '::runPlugin::' . get_class($plugin)); } } public function doOutput() { if(defined('_CRON_')) return; $system = System::getInstance(); $errors = $this->response->getErrors(); if(count($errors) > 0) { $https = array(); // errors with http codes foreach($errors as $error) { if($error['type'] == 'http') $https[] = $error; } if(count($https) > 0) { $msg = \Mammut\Protocol\HTTP\Status::getMessage($https[0]['code']); header('HTTP/1.0 ' . $https[0]['code'] . ' ' . $msg); echo '' . $https[0]['code'] . ': ' . $msg . ''; echo $https[0]['message']; if(!empty($https[0]['exception']) && defined('DEBUG')) { echo '

Debug info:

' . $https[0]['exception']->getTraceAsString() . '
'; } unset($https[0]); if(count($https) > 0) { echo "
"; echo "Additional errors while processing the page:
"; foreach($https as $error) echo "Code {$error['code']} (" . MFHTTPStatus::getMessage($error['code']) . "): {$error['message']}
\n"; } echo ''; } else { if($this->response->getType()) header('Content-Type: ' . $this->response->getType()); if(!defined('_SERVICE_')) { $layout = $this->plugins['layout'][0]; $layout->setRegionContent('TITLE', $system->getSiteParam($system->getSiteId(), '', 'title', 'Unnamed Page')); $errortext = ''; $layout->setRegionContent('ERROR', $errortext); echo $layout->getDocument('error'); } else { echo $this->response->getContent(); } } } elseif($redirect = $this->response->getRedirect()) { // build a rfc conform redirect if($redirect['url']) { header("Location: {$redirect['url']}"); } else { $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') ? 'https' : 'http'; $host = !empty($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME']; $port = !empty($_SERVER['SERVER_PORT']) ? $_SERVER['SERVER_PORT'] : 80; $uri = $_SERVER['PHP_SELF']; $extra = '?mod=' . $redirect['instance']; $extra .= '&view=' . $redirect['view']; foreach($redirect['param'] as $name=>$value) $extra .= '&' . $name . '=' . $value; header("Location: {$protocol}://{$host}:{$port}{$uri}{$extra}"); } } elseif(($activeInstance = $system->getTarget(iSystem::TARGET_INSTANCE)) == '*') { echo $this->response->getContent(); } else { if($this->response->getType()) header('Content-Type: ' . $this->response->getType()); if(!defined('_SERVICE_')) { defined('PROFILE') && Profiler::start(__METHOD__ . '::loadLayout'); $layout = $this->plugins['layout'][0]; unset($this->plugins['layout'][0]); if(count($this->plugins['layout']) > 0) { foreach($this->plugins['layout'] as $p) { if($p instanceof iPlugInLayoutAddon) { defined('PROFILE') && Profiler::start(__METHOD__ . '::runLayoutAddon::' . get_class($p)); $p->processLayout($layout); defined('PROFILE') && Profiler::stop(__METHOD__ . '::runLayoutAddon::' . get_class($p)); } } } defined('PROFILE') && Profiler::stop(__METHOD__ . '::loadLayout'); // defined('PROFILE') && Profiler::start(__METHOD__ . '::settingHeaders'); // if(isset($this->response->addons[Response::ADDON_CSS])) { // foreach($this->response->addons[Response::ADDON_CSS] as $css) { // $layout->addHeaders[] = ''; // } // } // defined('PROFILE') && Profiler::stop(__METHOD__ . '::settingHeaders'); defined('PROFILE') && Profiler::start(__METHOD__ . '::settingRegions'); foreach($this->response->getAllFragments() as $name=>$value) $layout->setRegionContent($name, $value); $layout->setRegionContent('DEBUGINFO', "Queries:" . $system->getDB()->getQueryCount()); defined('PROFILE') && Profiler::stop(__METHOD__ . '::settingRegions'); defined('PROFILE') && Profiler::start(__METHOD__ . '::generateDoc'); echo $layout->getDocument($this->response->getMedia(), $this->response); defined('PROFILE') && Profiler::stop(__METHOD__ . '::generateDoc'); } else { defined('PROFILE') && Profiler::start(__METHOD__ . '::generateDoc'); echo $this->response->getContent(); defined('PROFILE') && Profiler::stop(__METHOD__ . '::generateDoc'); } } } public function doCleanup() { parent::doCleanup(); } public function __destruct() { System::shutdown(); } }