/lib/pkp/classes/core/Core.inc.php
PHP | 128 lines | 48 code | 21 blank | 59 comment | 11 complexity | a83054d7a5d1fd0a0c81a7c4e9ce9754 MD5 | raw file
Possible License(s): GPL-2.0
- <?php
- /**
- * @defgroup core
- */
- /**
- * @file classes/core/Core.inc.php
- *
- * Copyright (c) 2000-2012 John Willinsky
- * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
- *
- * @class Core
- * @ingroup core
- *
- * @brief Class containing system-wide functions.
- *
- */
- // $Id$
- class Core {
- /**
- * Get the path to the base installation directory.
- * @return string
- */
- function getBaseDir() {
- static $baseDir;
- if (!isset($baseDir)) {
- // Need to change if the index file moves
- $baseDir = dirname(INDEX_FILE_LOCATION);
- }
- return $baseDir;
- }
- /**
- * Sanitize a variable.
- * Removes leading and trailing whitespace, normalizes all characters to UTF-8.
- * @param $var string
- * @return string
- */
- function cleanVar($var) {
- // only normalize strings that are not UTF-8 already, and when the system is using UTF-8
- if ( Config::getVar('i18n', 'charset_normalization') == 'On' && strtolower(Config::getVar('i18n', 'client_charset')) == 'utf-8' && !String::utf8_is_valid($var) ) {
- $var = String::utf8_normalize($var);
- // convert HTML entities into valid UTF-8 characters (do not transcode)
- if (checkPhpVersion('5.0.0')) {
- $var = html_entity_decode($var, ENT_COMPAT, 'UTF-8');
- } else {
- $var = String::html2utf($var);
- }
- // strip any invalid UTF-8 sequences
- $var = String::utf8_bad_strip($var);
- // re-encode special HTML characters
- if (checkPhpVersion('5.2.3')) {
- $var = htmlspecialchars($var, ENT_NOQUOTES, 'UTF-8', false);
- } else {
- $var = htmlspecialchars($var, ENT_NOQUOTES, 'UTF-8');
- }
- }
- // strip any invalid ASCII control characters
- $var = String::utf8_strip_ascii_ctrl($var);
- return trim($var);
- }
- /**
- * Sanitize a value to be used in a file path.
- * Removes any characters except alphanumeric characters, underscores, and dashes.
- * @param $var string
- * @return string
- */
- function cleanFileVar($var) {
- return String::regexp_replace('/[^\w\-]/', '', $var);
- }
- /**
- * Return the current date in ISO (YYYY-MM-DD HH:MM:SS) format.
- * @param $ts int optional, use specified timestamp instead of current time
- * @return string
- */
- function getCurrentDate($ts = null) {
- return date('Y-m-d H:i:s', isset($ts) ? $ts : time());
- }
- /**
- * Return *nix timestamp with microseconds (in units of seconds).
- * @return float
- */
- function microtime() {
- list($usec, $sec) = explode(' ', microtime());
- return (float)$sec + (float)$usec;
- }
- /**
- * Get the operating system of the server.
- * @return string
- */
- function serverPHPOS() {
- return PHP_OS;
- }
- /**
- * Get the version of PHP running on the server.
- * @return string
- */
- function serverPHPVersion() {
- return phpversion();
- }
- /**
- * Check if the server platform is Windows.
- * @return boolean
- */
- function isWindows() {
- return strtolower(substr(Core::serverPHPOS(), 0, 3)) == 'win';
- }
- }
- ?>