Open Journal Systems  3.0.0
 All Classes Namespaces Functions Variables Groups Pages
Core.inc.php
1 <?php
2 
22 define('PKP_LIB_PATH', 'lib' . DIRECTORY_SEPARATOR . 'pkp');
23 define('USER_AGENTS_FILE', Core::getBaseDir() . DIRECTORY_SEPARATOR . PKP_LIB_PATH . DIRECTORY_SEPARATOR . 'registry' . DIRECTORY_SEPARATOR . 'botAgents.txt');
24 
25 class Core {
26 
28  static $botRegexps = array();
29 
34  static function getBaseDir() {
35  static $baseDir;
36 
37  if (!isset($baseDir)) {
38  // Need to change if the index file moves
39  $baseDir = dirname(INDEX_FILE_LOCATION);
40  }
41 
42  return $baseDir;
43  }
44 
51  static function cleanVar($var) {
52  // only normalize strings that are not UTF-8 already, and when the system is using UTF-8
53  if ( Config::getVar('i18n', 'charset_normalization') == 'On' && strtolower_codesafe(Config::getVar('i18n', 'client_charset')) == 'utf-8' && !String::utf8_is_valid($var) ) {
54 
55  $var = String::utf8_normalize($var);
56 
57  // convert HTML entities into valid UTF-8 characters (do not transcode)
58  $var = html_entity_decode($var, ENT_COMPAT, 'UTF-8');
59 
60  // strip any invalid UTF-8 sequences
61  $var = String::utf8_bad_strip($var);
62 
63  // re-encode special HTML characters
64  if (checkPhpVersion('5.2.3')) {
65  $var = htmlspecialchars($var, ENT_NOQUOTES, 'UTF-8', false);
66  } else {
67  $var = htmlspecialchars($var, ENT_NOQUOTES, 'UTF-8');
68  }
69  }
70 
71  // strip any invalid ASCII control characters
72  $var = String::utf8_strip_ascii_ctrl($var);
73 
74  return trim($var);
75  }
76 
83  static function cleanFileVar($var) {
84  return String::regexp_replace('/[^\w\-]/', '', $var);
85  }
86 
92  static function getCurrentDate($ts = null) {
93  return date('Y-m-d H:i:s', isset($ts) ? $ts : time());
94  }
95 
100  static function microtime() {
101  list($usec, $sec) = explode(' ', microtime());
102  return (float)$sec + (float)$usec;
103  }
104 
109  static function serverPHPOS() {
110  return PHP_OS;
111  }
117  static function serverPHPVersion() {
118  return phpversion();
119  }
125  static function isWindows() {
126  return strtolower_codesafe(substr(Core::serverPHPOS(), 0, 3)) == 'win';
127  }
134  static function checkGeneralPHPModule($moduleName) {
135  if (extension_loaded($moduleName)) {
136  return true;
137  }
138  return false;
139  }
140 
148  static function isUserAgentBot($userAgent, $botRegexpsFile = USER_AGENTS_FILE) {
149  static $botRegexps;
150 
151  if (!isset($botRegexps[$botRegexpsFile])) {
152  $botRegexps[$botRegexpsFile] = array_filter(file($botRegexpsFile),
153  array('Core', '_filterBotRegexps'));
154  }
155 
156  foreach ($botRegexps[$botRegexpsFile] as $regexp) {
157  if (String::regexp_match($regexp, $userAgent)) {
158  return true;
159  }
160  }
161 
162  return false;
163  }
164 
177  static function getContextPaths($urlInfo, $isPathInfo, $contextList = null, $contextDepth = null, $userVars = array()) {
178  $contextPaths = array();
179  $application = Application::getApplication();
181  if (!$contextList) {
182  $contextList = $application->getContextList();
183  }
184  if (!$contextDepth) {
185  $contextDepth = $application->getContextDepth();
186  }
187 
188  // Handle context depth 0
189  if (!$contextDepth) return $contextPaths;
190 
191  if ($isPathInfo) {
192  // Split the path info into its constituents. Save all non-context
193  // path info in $contextPaths[$contextDepth]
194  // by limiting the explode statement.
195  $contextPaths = explode('/', trim($urlInfo, '/'), $contextDepth + 1);
196  // Remove the part of the path info that is not relevant for context (if present)
197  unset($contextPaths[$contextDepth]);
198  } else {
199  // Retrieve context from url query string
200  foreach($contextList as $key => $contextName) {
201  $contextPaths[$key] = Core::_getUserVar($urlInfo, $contextName, $userVars);
202  }
203  }
204 
205  // Canonicalize and clean context paths
206  for($key = 0; $key < $contextDepth; $key++) {
207  $contextPaths[$key] = (
208  isset($contextPaths[$key]) && !empty($contextPaths[$key]) ?
209  $contextPaths[$key] : 'index'
210  );
211  $contextPaths[$key] = Core::cleanFileVar($contextPaths[$key]);
212  }
213 
214  return $contextPaths;
215  }
216 
228  static function getPage($urlInfo, $isPathInfo, $userVars = array()) {
229  $page = Core::_getUrlComponents($urlInfo, $isPathInfo, 0, 'page', $userVars);
230  return Core::cleanFileVar(is_null($page) ? '' : $page);
231  }
232 
244  static function getOp($urlInfo, $isPathInfo, $userVars = array()) {
245  $operation = Core::_getUrlComponents($urlInfo, $isPathInfo, 1, 'op', $userVars);
246  return Core::cleanFileVar(empty($operation) ? 'index' : $operation);
247  }
248 
261  static function getArgs($urlInfo, $isPathInfo, $userVars = array()) {
262  return Core::_getUrlComponents($urlInfo, $isPathInfo, 2, 'path', $userVars);
263  }
270  private static function _filterBotRegexps(&$regexp) {
271  $delimiter = '/';
272  $regexp = trim($regexp);
273  if (!empty($regexp) && $regexp[0] != '#') {
274  if(strpos($regexp, $delimiter) !== 0) {
275  // Make sure delimiters are in place.
276  $regexp = $delimiter . $regexp . $delimiter;
277  return true;
278  }
279  } else {
280  return false;
281  }
282  }
283 
291  private static function _getUserVar($url, $varName, $userVars = array()) {
292  $returner = null;
293  parse_str(parse_url($url, PHP_URL_QUERY), $userVarsFromUrl);
294  if (isset($userVarsFromUrl[$varName])) $returner = $userVarsFromUrl[$varName];
295 
296  if (is_null($returner)) {
297  // Try to retrieve from passed user vars, if any.
298  if (!empty($userVars) && isset($userVars[$varName])) {
299  $returner = $userVars[$varName];
300  }
301  }
302 
303  return $returner;
304  }
305 
317  private static function _getUrlComponents($urlInfo, $isPathInfo, $offset, $varName = '', $userVars = array()) {
318  $component = null;
319 
320  $isArrayComponent = false;
321  if ($varName == 'path') {
322  $isArrayComponent = true;
323  }
324  if ($isPathInfo) {
325  $application = Application::getApplication();
326  $contextDepth = $application->getContextDepth();
327 
328  $vars = explode('/', trim($urlInfo, '/'));
329  if (count($vars) > $contextDepth + $offset) {
330  if ($isArrayComponent) {
331  $component = array_slice($vars, $contextDepth + $offset);
332  for ($i=0, $count=count($component); $i<$count; $i++) {
333  $component[$i] = Core::cleanVar(get_magic_quotes_gpc() ? stripslashes($component[$i]) : $component[$i]);
334  }
335  } else {
336  $component = $vars[$contextDepth + $offset];
337  }
338  }
339  } else {
340  $component = Core::_getUserVar($urlInfo, $varName, $userVars);
341  }
342 
343  if ($isArrayComponent) {
344  if (empty($component)) $component = array();
345  elseif (!is_array($component)) $component = array($component);
346  }
347 
348  return $component;
349  }
350 }
351 
352 ?>
static serverPHPOS()
Definition: Core.inc.php:112
static cleanFileVar($var)
Definition: Core.inc.php:86
static isUserAgentBot($userAgent, $botRegexpsFile=USER_AGENTS_FILE)
Definition: Core.inc.php:151
static getArgs($urlInfo, $isPathInfo, $userVars=array())
Definition: Core.inc.php:264
static utf8_bad_strip($str)
Definition: String.inc.php:510
static regexp_replace($pattern, $replacement, $subject, $limit=-1)
Definition: String.inc.php:377
static getContextPaths($urlInfo, $isPathInfo, $contextList=null, $contextDepth=null, $userVars=array())
Definition: Core.inc.php:180
static utf8_normalize($str)
Definition: String.inc.php:541
Class containing system-wide functions.
Definition: Core.inc.php:25
static microtime()
Definition: Core.inc.php:103
static isWindows()
Definition: Core.inc.php:128
static getOp($urlInfo, $isPathInfo, $userVars=array())
Definition: Core.inc.php:247
static regexp_match($pattern, $subject)
Definition: String.inc.php:339
static utf8_is_valid($str)
Definition: String.inc.php:479
static utf8_strip_ascii_ctrl($str)
Definition: String.inc.php:531
static getVar($section, $key, $default=null)
Definition: Config.inc.php:35
static serverPHPVersion()
Definition: Core.inc.php:120
static $botRegexps
Definition: Core.inc.php:31
static cleanVar($var)
Definition: Core.inc.php:54
static checkGeneralPHPModule($moduleName)
Definition: Core.inc.php:137
static getPage($urlInfo, $isPathInfo, $userVars=array())
Definition: Core.inc.php:231
static getBaseDir()
Definition: Core.inc.php:37
static getCurrentDate($ts=null)
Definition: Core.inc.php:95