request = new \MCMS\RequestCLI(); } else $this->request = new \MCMS\Request(); } /** * 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) { $cache = self::getContext(self::CTX_CACHE); $value = $cache->get(array('siteparam',$siteInst,$modInst,$key)); if(is_null($value)) { $db = $this->getDB(); if(!is_object($db)) throw new IllegalStateException('database not initialized'); // TODO: use prepared statement with parameters $where = array('site_id' => new Parameter(),'instance' => new Parameter(),'key' => new Parameter()); $q = $db->select()->from(self::$tbl_siteconfig)->where($where); $result = $db->prepareStatement($q); $result->execute(array($siteInst, $modInst, $key)); if($result->getRowCount() == 1) { $value = $result->fetchObject()->value; $cache->set(array('siteparam',$siteInst,$modInst,$key), $value, 30); } $result->close(); } return is_null($value) ? $default : $value; } 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 self::getContext(iSystem::CTX_CACHE); } public function onException(\Exception $e) { if(defined('_CLI_')) { $this->onExceptionCLI($e); return; } $layout = '
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 .= '' . $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() . 'You are not authenticated or have not the needed privileges!
'; } else { $st .= 'Class: ' . get_class($e) . '' . $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 getSiteLayout($siteId, $type = 'default') { $t = new CTemplate(); $t->loadString(''); return $t; } 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); } }