PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/pkp/classes/core/Core.inc.php

https://github.com/lib-uoguelph-ca/ocs
PHP | 128 lines | 48 code | 21 blank | 59 comment | 11 complexity | a83054d7a5d1fd0a0c81a7c4e9ce9754 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * @defgroup core
  4. */
  5. /**
  6. * @file classes/core/Core.inc.php
  7. *
  8. * Copyright (c) 2000-2012 John Willinsky
  9. * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  10. *
  11. * @class Core
  12. * @ingroup core
  13. *
  14. * @brief Class containing system-wide functions.
  15. *
  16. */
  17. // $Id$
  18. class Core {
  19. /**
  20. * Get the path to the base installation directory.
  21. * @return string
  22. */
  23. function getBaseDir() {
  24. static $baseDir;
  25. if (!isset($baseDir)) {
  26. // Need to change if the index file moves
  27. $baseDir = dirname(INDEX_FILE_LOCATION);
  28. }
  29. return $baseDir;
  30. }
  31. /**
  32. * Sanitize a variable.
  33. * Removes leading and trailing whitespace, normalizes all characters to UTF-8.
  34. * @param $var string
  35. * @return string
  36. */
  37. function cleanVar($var) {
  38. // only normalize strings that are not UTF-8 already, and when the system is using UTF-8
  39. if ( Config::getVar('i18n', 'charset_normalization') == 'On' && strtolower(Config::getVar('i18n', 'client_charset')) == 'utf-8' && !String::utf8_is_valid($var) ) {
  40. $var = String::utf8_normalize($var);
  41. // convert HTML entities into valid UTF-8 characters (do not transcode)
  42. if (checkPhpVersion('5.0.0')) {
  43. $var = html_entity_decode($var, ENT_COMPAT, 'UTF-8');
  44. } else {
  45. $var = String::html2utf($var);
  46. }
  47. // strip any invalid UTF-8 sequences
  48. $var = String::utf8_bad_strip($var);
  49. // re-encode special HTML characters
  50. if (checkPhpVersion('5.2.3')) {
  51. $var = htmlspecialchars($var, ENT_NOQUOTES, 'UTF-8', false);
  52. } else {
  53. $var = htmlspecialchars($var, ENT_NOQUOTES, 'UTF-8');
  54. }
  55. }
  56. // strip any invalid ASCII control characters
  57. $var = String::utf8_strip_ascii_ctrl($var);
  58. return trim($var);
  59. }
  60. /**
  61. * Sanitize a value to be used in a file path.
  62. * Removes any characters except alphanumeric characters, underscores, and dashes.
  63. * @param $var string
  64. * @return string
  65. */
  66. function cleanFileVar($var) {
  67. return String::regexp_replace('/[^\w\-]/', '', $var);
  68. }
  69. /**
  70. * Return the current date in ISO (YYYY-MM-DD HH:MM:SS) format.
  71. * @param $ts int optional, use specified timestamp instead of current time
  72. * @return string
  73. */
  74. function getCurrentDate($ts = null) {
  75. return date('Y-m-d H:i:s', isset($ts) ? $ts : time());
  76. }
  77. /**
  78. * Return *nix timestamp with microseconds (in units of seconds).
  79. * @return float
  80. */
  81. function microtime() {
  82. list($usec, $sec) = explode(' ', microtime());
  83. return (float)$sec + (float)$usec;
  84. }
  85. /**
  86. * Get the operating system of the server.
  87. * @return string
  88. */
  89. function serverPHPOS() {
  90. return PHP_OS;
  91. }
  92. /**
  93. * Get the version of PHP running on the server.
  94. * @return string
  95. */
  96. function serverPHPVersion() {
  97. return phpversion();
  98. }
  99. /**
  100. * Check if the server platform is Windows.
  101. * @return boolean
  102. */
  103. function isWindows() {
  104. return strtolower(substr(Core::serverPHPOS(), 0, 3)) == 'win';
  105. }
  106. }
  107. ?>