_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(); defined('PROFILE') && Profiler::start(__METHOD__ . '::initDB'); $this->doInitDb($system); $db = $system->getDB(); defined('PROFILE') && Profiler::stop(__METHOD__ . '::initDB'); defined('PROFILE') && Profiler::start(__METHOD__ . '::checkDBver'); $this->doInitCheckDBVersion($db); defined('PROFILE') && Profiler::stop(__METHOD__ . '::checkDBver'); // init the cache defined('PROFILE') && Profiler::start(__METHOD__ . '::initCache'); $this->doInitCache($system); defined('PROFILE') && Profiler::stop(__METHOD__ . '::initCache'); // $dbcfg = $system->getConfig('dbx'); // if(!empty($dbcfg) && is_array($dbcfg)) { // defined('PROFILE') && Profiler::start(__METHOD__ . '::initDBx'); // foreach($dbcfg as $name=>$dbc) { // if(!isset($dbc['option'])) // $dbc['option'] = array(); // $system->addDB($name, $dbc['dsn'], $dbc['user'], $dbc['passwd'], $dbc['option']); // } // unset($dbc); // defined('PROFILE') && Profiler::stop(__METHOD__ . '::initDBx'); // } // unset($dbcfg); // init the plugin system $this->plugins = $system->getBasePlugins(); $this->plugins['cache'] = array($cache =System::getContext(iSystem::CTX_CACHE)); // adding user plugins $installedPlugins = $cache->get(array('sys','-1','sys.info','installedPlugins')); if(!is_array($installedPlugins)) { $q = $db->select()->from('installinfo')->where(array('type' => 'plugin')); $installedPlugins = $db->getObjectList($q); $cache->set(array('sys','-1','sys.info','installedPlugins'), $installedPlugins, 60); } foreach($installedPlugins as $plugin) { list($type, $name) = explode(':', $plugin->key, 2); $this->plugins[$type][] = $name; } if(defined('_ADMIN_')) { $this->usedPlugins = $this->plugins; $this->plugins = $system->getBaseAdminPlugins(); foreach($this->plugins as $cat=>$list) { $this->usedPlugins[$cat] = array_merge($this->usedPlugins[$cat], $this->plugins[$cat]); } } // 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 Context(); $this->response = new 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 doInitDb(iSystem &$system) { $dbcfg = $system->getConfig('db'); if(!isset($dbcfg['option'])) $dbcfg['option'] = array(); $system->initDB($dbcfg['dsn'], $dbcfg['user'], $dbcfg['passwd'], $dbcfg['option']); } protected function doInitCache(iSystem &$system) { $cachecfg = $system->getConfig('cache'); if(empty($cachecfg)) $cachecfg['type'] = ''; $cachetype = $cachecfg['type']; unset($cachecfg['type']); $system->initCache($cachetype, $cachecfg); } protected function doInitCheckDBVersion(DB &$db) { $q = $db->select()->columns(array('version'))->from('installinfo')->where(array('type' => 'system','key' => 'mcmsdb'))->limit(1); if(($dbv = $db->getObject($q)->version) != MCMS_DB) throw new \ErrorException('CRITICAL: DB-VERSION ' . $dbv . '!=' . MCMS_DB . ' NEEDED BY SYSTEM'); } 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(); $sql = $db->select()->from('websites')->where(array('id' => new Parameter())); $maindata = $db->getObjectP($sql, 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() { if(defined('_ADMIN_')) $this->plugins['content'][0]->setUsedPlugins($this->usedPlugins); 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(); if(defined('_ADMIN_')) { $usrSvc = $system->getUserSvc(); $usr = $usrSvc->getUser(); if (is_null($usr) || !$usrSvc->userHasPriv($usrSvc->getUser())) return; } 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(); } } } else { // $header = $this->response->get if($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(); } }