_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 'Debug info:
' . $https[0]['exception']->getTraceAsString() . ''; } unset($https[0]); if(count($https) > 0) { echo "