/** * Gets the amount of total log entries * @param \System\Db\Database The database to query * @return int The amount of items in the log */ public static final function getAmount(\System\Db\Database $db) { $query = new \System\Db\Query($db, SQL_SYSERROR_TOTALCOUNT); $results = $db->queryScalar($query); assert($results->hasItems()); return $results->first(); }
/** * Provides functionality to check if a given IP is blocked by a common blacklist * Do note this system requires the use of the PERMABAN_* directives * @param string The IP Address to check. * @return bool True if the IP is allowed, false otherwise */ public static final function isIPAllowed($ipAddress) { //if there is an explicit empty PERMABAN, we accept everything if (PERMABAN_HOST == '') { return true; } $allowed = true; $mc = new \System\Cache\Memcache\Memcache(); $key = self::MEMCACHE_KEY . $ipAddress; //we get the value from the memcache, and only recheck if the blocked user is on it. if (!($allowed = $mc->get($key))) { $db = \System\Db\Database::getConnection(PERMABAN_HOST, PERMABAN_USER, PERMABAN_PASS, PERMABAN_NAME, PERMABAN_PORT); $query = new \System\Db\Query($db, \System\HTTP\Visitor\PermaBan\SQL_PERMABAN_CHECK_PERMABAN); $query->bind($ipAddress, \System\Db\QueryType::TYPE_STRING); $results = $db->query($query); $allowed = $results->count() == 0; $mc->store($key, $allowed); } return $allowed; }
/** * Boots the system and reads the configuration files. Should not be called manually. */ function __bootloader() { //get the current path $currentPath = getcwd(); //we make sure the last character of the current path is a separator if (substr($currentPath, -1) != '/') { $currentPath .= '/'; } //these definitions must be present if (!defined('PATH_SYSTEM')) { throw new \Exception('PATH_SYSTEM is not set in paths.inc'); } if (!defined('PATH_CONFIG')) { throw new \Exception('PATH_CONFIG is not set in paths.inc'); } if (!defined('PATH_TEMP')) { throw new \Exception('PATH_TEMP is not set in paths.inc'); } if (!defined('PATH_LOGS')) { throw new \Exception('PATH_LOGS is not set in paths.inc'); } if (!defined('PATH_MODULES')) { throw new \Exception('PATH_MODULES is not set in paths.inc'); } if (!defined('PATH_PAGECACHE_CACHE')) { throw new \Exception('PATH_PAGECACHE_CACHE is not set in paths.inc'); } //define the security locks so we can include files define('InSite', null); define('System', null); //we define the default character sets to utf8 mb_internal_encoding("UTF-8"); //load the autoloader. After this call, all the classes can be called. $autoloader = PATH_SYSTEM . 'Autoload.class.php'; if (file_exists($autoloader)) { require_once $autoloader; } else { throw new \Exception('Could not load ' . $autoloader . '. Please check the PATH_SYSTEM constant in your configuration!'); } //debug parameters when the platform is our development platform if (\System\Server\OS::getOS() == \System\Server\OS::OS_WINDOWS) { defined('DEBUG') || define('DEBUG', null); } register_shutdown_function('\\System\\Db\\Database::handleShutdown'); //boot the errorhandler and register the exception and error handlers \System\Error\ErrorHandler::getInstance(); //set the timezone values defined('TIMEZONE_IDENTIFIER') || define('TIMEZONE_IDENTIFIER', 'Europe/Amsterdam'); \System\Version::registerRequiredConfigDirective('TIMEZONE_IDENTIFIER'); date_default_timezone_set(TIMEZONE_IDENTIFIER); //register $register = \System\Register\Register::getInstance(); //we set the start timer \System\Calendar\Timer::getSystemExecutionTime(); //config require_once PATH_CONFIG . 'site.inc'; //initialize the language subsystem \System\Internationalization\Language::init(); //initialize the system interaction system \System\System\Interaction\Event\SystemInteractionEvent::registerListeners(); //register extra handlers if needed if (file_exists(PATH_CONFIG . 'handlers.inc')) { require_once PATH_CONFIG . 'handlers.inc'; } //turn the displaying of errors off, when we are in production environment defined('DEBUG') || ini_set('display_errors', 0); //verify the required configuration variables __requirements(); //check if the visitors ip address is allowed. if (!\System\HTTP\Visitor\PermaBan\PermaBan::isIPAllowed(\System\HTTP\Visitor\IP::getClientIP())) { header('HTTP/1.0 403 Forbidden'); header('Status: 403 Forbidden'); header('HTTP/1.1 403 Forbidden'); exit; } //database $register->defaultDb = \System\Db\Database::getConnection(); //we dont want to cache our output, as this allows access without revalidating header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); //requestshizzle \System\Version::registerRequiredConfigDirective('DEFAULT_CONTROLLER'); if (!defined('DEFAULT_CONTROLLER')) { throw new \System\Error\Exception\SystemException('The configuration is invalid: DEFAULT_CONTROLLER not set or invalid.'); } $controller = \System\Web\Controller::callController(); //do buffered output rendering if needed if ($controller->getRenderSetting() == \System\Web\Controller::RENDERER_ENABLED) { //render the surface $renderSurface = $controller->getRenderSurface(); if (!$renderSurface) { throw new \System\Error\Exception\SystemException('Please make sure your controller action sets a RenderSurface!'); } $controller->getRenderSurface()->execute(); } //shutdown the system to prevent further execution of code exit; }
/** * Returns all currently stored values in the LUT cache. * @param \System\Db\Database The database to query * @return \System\Db\DatabaseResult A resultset with all the results. */ public static final function getCache(\System\Db\Database $db) { $query = new \System\Db\Query($db, \System\Cache\LUTCache\SQL_LUTCACHE_RETRIEVE_ALL); $results = $db->query($query); return $results; }
/** * Returns a Vector with all table column names from the requested table * @param \System\Db\Database the database to query * @param string The tablename to retrieve the columns from * @return \System\Collection\Vector a Vector containing all the column names */ public static final function getTableColumnNames(\System\Db\Database $db, $tableName) { $query = new \System\Db\Query($db, 'SHOW COLUMNS FROM %?%'); $query->bind($tableName, \System\Db\QueryType::TYPE_QUERY); $results = $db->query($query); $columnNames = new \System\Collection\Vector(); foreach ($results as $result) { $columnNames[] = $result->Field; } return $columnNames; }
/** * Creates a new database resultset. This function is automatically called by the database and should not * be called directly. * @param \MySQLi The link to the database system * @param \System\Db\Query The query * @param \System\Db\Database The database issueing the request */ public function __construct(\MySQLi $databaseLink, \System\Db\Query $query, \System\Db\Database $database) { $this->databaseLink = $databaseLink; $this->requestIssuer = $database; $this->query = $query; //increase its own querycounter self::$queryCount++; $timer = new \System\Calendar\Timer(); $timer->start(); $actualQuery = $query->getQuery(); if (!($this->results = $databaseLink->query($actualQuery))) { throw new \System\Error\Exception\DatabaseQueryException('Query: ' . $actualQuery . ' - ' . $databaseLink->error); } /** * If there is a query that wants to execute the amount of found rows, we store this amount in the own vector. * Do note that this query gets logged before the actual query, because of the stackframe buildup. The actual execution order is correct */ if (strpos($actualQuery, 'SQL_CALC_FOUND_ROWS') !== false) { $query = new \System\Db\Query($database, 'SELECT FOUND_ROWS() AS amount'); $this->totalAmount = $database->queryScalar($query)->first(); } else { $this->totalAmount = $this->count(); } $timer->stop(); $this->duration = $timer->getDuration(); self::$totalQueryTime += $this->duration; if (round($timer->getDuration()) >= self::SLOW_QUERY_TIME) { $event = new \System\Event\Event\OnSlowMySQLQueryEvent(); $event->setQuery($query); $event->setDuration($this->duration); $event->raise($this); } $this->rewind(); }
/** * Iterates through the given resultset, dispatching every single mail with the given priority. * @param \System\Db\Database The database to use for the emailsystem queue * @param SMTP The smtp to use * @param \System\Db\DatabaseResult The results to iterate * @param int The priority level */ private static final function iterateMailResults(\System\Db\Database $db, \System\Email\SMTP $smtp, \System\Db\DatabaseResult $results, $priority) { foreach ($results as $result) { $attachQuery = new \System\Db\Query($db, \System\Email\SQL_EMAILSYSTEM_GET_ATTACH); $attachQuery->bind($result->id, \System\Db\QueryType::TYPE_INTEGER); $attachQuery->bind($priority, \System\Db\QueryType::TYPE_INTEGER); $attachments = $db->query($attachQuery); self::addToLog('Processing ' . $results->count() . ' ' . self::$priorityNames[$priority][0] . ' priority mails'); self::dispatchSingleMail($smtp, $result->from, self::convertAddressStringToArray($result->to), self::convertAddressStringToArray($result->cc), self::convertAddressStringToArray($result->bcc), $result->subject, $result->message, $attachments); $query = new \System\Db\Query($db, \System\Email\SQL_EMAILSSYTEM_DELETEMAIL); $query->bind(self::$priorityNames[$priority][1], \System\Db\QueryType::TYPE_QUERY); $query->bind($result->id, \System\Db\QueryType::TYPE_INTEGER); $db->query($query); $query = new \System\Db\Query($db, \System\Email\SQL_EMAILSYSTEM_DELETEATTACH); $query->bind($result->id, \System\Db\QueryType::TYPE_INTEGER); $query->bind($priority, \System\Db\QueryType::TYPE_INTEGER); $db->query($query); } }
/** * Delete an onetimecall. This invalidates the object in the db. * @param \System\Db\Database The database to query * @return null so we stop the chaining */ public final function delete(\System\Db\Database $db) { $query = new \System\Db\Query($db, SQL_ONETIMECALL_DELETE); $query->bind($this->getId(), \System\Db\QueryType::TYPE_INTEGER); $db->query($query); return null; }
/** * Creates an instance of the handler and gets the default database */ public function __construct() { $this->database = \System\Db\Database::getConnection(); }
/** * Returns an instance of the Databse from the given DbLookup entry with the given name. * This name is an uniquely identifyable name and represent a database connection. * This function requires the DBPOOL_DB_HOST, DBPOOL_DB_USER, DBPOOL_DB_PASS, DBPOOL_DB_NAME to be set. * This function uses caching and preloads any * @param string The name of the DbLookup entry * @return Database The requested instance of the database */ public static final function getDbLookup($name) { if (defined('DBPOOL_DB_HOST') && defined('DBPOOL_DB_USER') && defined('DBPOOL_DB_PASS') && defined('DBPOOL_DB_NAME')) { $cache = self::getLookupCache(); if (!$cache->hasItems()) { $db = \System\Db\Database::getConnection(DBPOOL_DB_HOST, DBPOOL_DB_USER, DBPOOL_DB_PASS, DBPOOL_DB_NAME); $lookups = self::load($db, 'all', null, true); foreach ($lookups as $lookup) { $cache->set($lookup->getName(), $lookup); } } if ($cache->keyExists($name)) { return $cache->{$name}->getLookupDatabase(); } throw new \System\Error\Exception\SystemException('The given DB Lookup does not exists, or is not properly defined in the lookup pool: ' . $name); } else { throw new \System\Error\Exception\SystemException('The DBPOOL_* connection parameters are not all set. Please verify the settings'); } }