request = new \MCMS\RequestCLI(); } else $this->request = new \MCMS\Request(); } public function getBasePlugins() { $pilist = array( 'route' => array('cacenter.CCPIRouter'), // order does matter! 'content' => array('cacenter.CCPIMainContent'), 'layout' => array('cacenter.CCPILayout'), // name => plugin 'misc' => array()); return $pilist; } /** * initialize the mammut cms system. * * this initializes the database connection, tries to get the active system instance and user, and * searche/register active plugins */ static public function init() { if(is_object(self::$instance)) throw new \IllegalStateException('system already initialized'); self::$instance = new System(); // bind the instance property to an instance of this class } /** * shut down the mammut cms system an cleans up all */ public static function shutdown() { if(!is_object(self::$instance)) throw new \IllegalStateException('system not initialized'); self::$instance = NULL; // this should call the destructor } // site settings/component instance settings public function getSiteParam($siteInst, $modInst, $key, $default = NULL) { if($this->cacheExists('[siteparam]-' . $siteInst, $modInst, $key)) { $cache = $this->cacheFetch('[siteparam]-' . $siteInst, $modInst, $key, NULL); return $cache; } $db = $this->getDB(); if(!is_object($db)) throw new \IllegalStateException('database not initialized'); $result = $db->table(self::$tbl_siteconfig)->select(array( 'site_id' => $siteInst,'instance' => $modInst,'key' => $key)); if($result->getRowCount() == 1) { $default = $result->fetchObject()->value; $this->cacheStore('[siteparam]-' . $siteInst, $modInst, $key, $default, 30); } $result->close(); return $default; } public function setSiteParam($siteInst, $comInst, $key, $value) { $db = $this->getDB(); if(!is_object($db)) throw new \IllegalStateException('database not initialized'); $cond = array('site_id' => $siteInst,'instance' => $comInst,'key' => $key); $table = $db->table(self::$tbl_siteconfig); $result = $table->select($cond); if($result->getRowCount() == 1) $table->update(array('value' => $value), $cond); else { $data = $cond; $data['value'] = $value; $table->insert($data); } $this->cacheStore('[siteparam]-' . $siteInst, $comInst, $key, $value, 30); } public function getRequest() { return $this->request; } public function initUserSvc($pluginClass, $cfg = array()) { if(!is_null($this->userSvc)) throw new \BadMethodCallException("User plugin is set already"); if(!class_exists($pluginClass)) throw new \ErrorException("User plugin '{$pluginClass}' class not found"); $this->userSvc = new $pluginClass($cfg); $this->plugins['auth'][0] = $this->userSvc; } public function getUserSvc() { return $this->userSvc; } public function isUserSystemAdmin(iUser $user) { return false; // TODO: implement and use } public function getCache() { return $this->cache; } public function cacheExists($siteId, $instance, $key) { $realKey = $siteId . ':' . $instance . '#' . $key; return $this->getCache()->keyExists($realKey); } public function cacheStore($siteId, $instance, $key, $value, $ttl = 60) { $realKey = $siteId . ':' . $instance . '#' . $key; $this->getCache()->set($realKey, $value, $ttl); } public function cacheFetch($siteId, $instance, $key, $default = NULL) { $realKey = $siteId . ':' . $instance . '#' . $key; return $this->getCache()->get($realKey, $default); } public function cacheDelete($siteId, $instance, $key) { $realKey = $siteId . ':' . $instance . '#' . $key; $this->getCache()->delete($realKey); } public function cacheClean($siteId = -1, $instance = NULL) { $this->getCache()->clear(); } public function onException(\Exception $e) { if(defined('_CLI_')) { $this->onExceptionCLI($e); return; } $layout = 'Uncaught exception'; $layout .= '

Uncaught exception

'; $layout .= ''; if ($e instanceof AccessDeniedException) { $layout = 'Access denied'; $layout .= '

Access denied

'; $layout .= ''; } $l2 = $layout; try { $filename = '_system_' . __DS__ . '_error_.php'; if(file_exists(_LAYOUTPATHX_ . __DS__ . $filename)) { $filename = _LAYOUTPATHX_ . __DS__ . $filename; ob_start(); include ($filename); $layout = ob_get_clean(); } elseif(file_exists(_LAYOUTPATH_ . __DS__ . $filename)) { $filename = _LAYOUTPATH_ . __DS__ . $filename; ob_start(); include ($filename); $layout = ob_get_clean(); } } catch(\Exception $e) { $layout = $l2; } $st = ''; try { $this->writeError2DB($e); } catch(\Exception $sqlex) { $st .= '

Note: SQL-Logger reported another error:' . $sqlex->getMessage() . '

'; $round = 0; $subEx = $sqlex; while(!is_null($subEx = $subEx->getPrevious()) && (++$round < 50)) $st .= '

Caused by ' . get_class($subEx) . ': ' . $subEx->getMessage() . '[' . $subEx->getFile() . ':' . $subEx->getLine() . ']

'; $st .= '
Stacktrace of subexception
' . $sqlex->getTraceAsString() . '
'; } if($e instanceof \ErrorException) { switch($e->getSeverity()) { case E_ERROR: $st .= '

Type: Error

'; break; case E_WARNING: case E_USER_WARNING: $st .= '

Type: Warning

'; break; case E_STRICT: $st .= '

Type: Strict coding convention

'; break; default: $st .= '

Type: unknown(' . $e->getSeverity() . ')

'; break; } } if ($e instanceof AccessDeniedException) { if(defined('DEBUG')) { $st .= 'Message: ' . $e->getMessage() . '
'; } else $st .= '

You are not authenticated or have not the needed privileges!

'; } else { $st .= 'Class: ' . get_class($e) . '
'; $st .= 'Code: ' . $e->getCode() . '
'; if(defined('DEBUG')) { $st .= 'Location: ' . $e->getFile() . ':' . $e->getLine() . '
'; $st .= 'Message: ' . $e->getMessage() . '
'; if($e instanceof \Mammut\DB\SQLException) $st .= 'Query: ' . $e->getQuery() . '
'; $st .= '
Stacktrace
' . $e->getTraceAsString() . '
'; $subEx = $e; $round = 0; while(!is_null($subEx = $subEx->getPrevious()) && (++$round < 50)) $st .= '

Caused by ' . get_class($subEx) . ': ' . $subEx->getMessage() . '[' . $subEx->getFile() . ':' . $subEx->getLine() . ']

'; } else $st .= '

If you are a developer and need more information about this incident, define the DEBUG constant

'; } $st = str_replace('', $st, $layout); die($st); } /** * Optimized output on console * * @param Exception $e * an exception */ protected function onExceptionCLI(\Exception $e) { try { $this->writeError2DB($e); } catch(Exception $sqlex) { echo 'Note: SQL-Logger reported another error:' . $sqlex->getMessage() . "\n"; $round = 0; $subEx = $sqlex; while(!is_null($subEx = $subEx->getPrevious()) && (++$round < 50)) echo 'Caused by ' . get_class($subEx) . ': ' . $subEx->getMessage() . '[' . $subEx->getFile() . ':' . $subEx->getLine() . "]\n"; echo "\nStacktrace of subexception:\n" . $sqlex->getTraceAsString() . "\n"; } if($e instanceof \ErrorException) { if (method_exists($e,'getSeverity')) { switch($e->getSeverity()) { case E_ERROR: echo "Type: Error\n"; break; case E_WARNING: case E_USER_WARNING: echo "Type>: Warning\n"; break; case E_STRICT: echo "Type: Strict coding convention\n"; break; default: echo 'Type: unknown(' . $e->getSeverity() . ")\n"; break; } } else echo "Type: unknown\n"; } echo 'Class: ' . get_class($e) . "\n"; echo 'Code: ' . $e->getCode() . "\n"; if(defined('DEBUG')) { echo 'Location: ' . $e->getFile() . ':' . $e->getLine() . "\n"; echo 'Message: ' . $e->getMessage() . "\n"; if($e instanceof \Mammut\DB\SQLException) echo 'Query: ' . $e->getQuery() . "\n"; echo "\nStacktrace\n" . $e->getTraceAsString() . "\n"; $subEx = $e; $round = 0; while(!is_null($subEx = $subEx->getPrevious()) && (++$round < 50)) echo 'Caused by ' . get_class($subEx) . ': ' . $subEx->getMessage() . '[' . $subEx->getFile() . ':' . $subEx->getLine() . "]\n"; } else echo 'If you are a developer and need more information about this incident, define the DEBUG constant.' . "\n"; die(); } /** * Writes a log entry into the database * @param Exception $e an exception */ protected function writeError2DB(\Exception $e) { $db = self::getInstance()->getDB(); $data = new \stdClass(); $data->eventtime = new \DateTime(); $data->level = LOG_ERR; $data->area = 'system'; $data->areaname = NULL; $data->user = 0; $data->code = 0; $data->message = $e->getMessage(); if(strlen($data->message) > 254) $data->message = substr($data->message, 0, 254); $db->table('log')->insert($data); $id = $db->getInsertId(); if($e instanceof \Mammut\DB\SQLException) { $data = new \stdClass(); $data->parent = $id; $data->eventtime = new \DateTime(); $data->level = LOG_ERR; $data->area = 'system'; $data->areaname = 'sql'; $data->user = 0; $data->code = 0; $data->message = '' . $e->getQuery(); if(strlen($data->message) > 254) $data->message = substr($data->message, 0, 254); $db->table('log')->insert($data); } } public function doLog($level, $code, $message) { self::$log[] = array( 'instance' => self::getInstanceId(),'timestamp' => time(),'level' => $level, 'message' => '' . $message); if(($level == LOG_ERR) || ($level == LOG_CRIT)) self::error($message); } /** * helper method which redirects the entry to the current system * instance * * @param $level a * error constant * @param $code an * error code * @param $message a * representing the error */ public static function log($level, $code, $message) { return self::getInstance()->doLog($level, $code, $message); } }