Open Journal Systems  2.4.4
 All Classes Namespaces Functions Variables Groups Pages
Core.inc.php
1 <?php
2 
21 define('USER_AGENTS_FILE', Core::getBaseDir() . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'pkp' . DIRECTORY_SEPARATOR . 'registry' . DIRECTORY_SEPARATOR . 'botAgents.txt');
22 
23 class Core {
24 
26  static $botRegexps = array();
27 
32  function getBaseDir() {
33  static $baseDir;
34 
35  if (!isset($baseDir)) {
36  // Need to change if the index file moves
37  $baseDir = dirname(INDEX_FILE_LOCATION);
38  }
39 
40  return $baseDir;
41  }
42 
49  function cleanVar($var) {
50  // only normalize strings that are not UTF-8 already, and when the system is using UTF-8
51  if ( Config::getVar('i18n', 'charset_normalization') == 'On' && strtolower_codesafe(Config::getVar('i18n', 'client_charset')) == 'utf-8' && !String::utf8_is_valid($var) ) {
52 
53  $var = String::utf8_normalize($var);
54 
55  // convert HTML entities into valid UTF-8 characters (do not transcode)
56  if (checkPhpVersion('5.0.0')) {
57  $var = html_entity_decode($var, ENT_COMPAT, 'UTF-8');
58  } else {
59  $var = String::html2utf($var);
60  }
61 
62  // strip any invalid UTF-8 sequences
63  $var = String::utf8_bad_strip($var);
64 
65  // re-encode special HTML characters
66  if (checkPhpVersion('5.2.3')) {
67  $var = htmlspecialchars($var, ENT_NOQUOTES, 'UTF-8', false);
68  } else {
69  $var = htmlspecialchars($var, ENT_NOQUOTES, 'UTF-8');
70  }
71  }
72 
73  // strip any invalid ASCII control characters
74  $var = String::utf8_strip_ascii_ctrl($var);
75 
76  return trim($var);
77  }
78 
85  function cleanFileVar($var) {
86  return String::regexp_replace('/[^\w\-]/', '', $var);
87  }
88 
94  function getCurrentDate($ts = null) {
95  return date('Y-m-d H:i:s', isset($ts) ? $ts : time());
96  }
97 
102  function microtime() {
103  list($usec, $sec) = explode(' ', microtime());
104  return (float)$sec + (float)$usec;
105  }
106 
111  function serverPHPOS() {
112  return PHP_OS;
113  }
114 
119  function serverPHPVersion() {
120  return phpversion();
121  }
122 
127  function isWindows() {
128  return strtolower_codesafe(substr(Core::serverPHPOS(), 0, 3)) == 'win';
129  }
130 
138  function isUserAgentBot($userAgent, $botRegexpsFile = USER_AGENTS_FILE) {
139  static $botRegexps;
140 
141  if (!isset($botRegexps[$botRegexpsFile])) {
142  $cacheManager =& CacheManager::getManager();
143  $cache =& $cacheManager->getCache('core', $botRegexpsFile, array('Core', '_botFileListCacheMiss'), CACHE_TYPE_FILE);
144  $botRegexps[$botRegexpsFile] = $cache->getContents($botRegexpsFile);
145  }
146 
147  foreach ($botRegexps[$botRegexpsFile] as $regexp) {
148  if (String::regexp_match($regexp, $userAgent)) {
149  return true;
150  }
151  }
152 
153  return false;
154  }
155 
168  function getContextPaths($urlInfo, $isPathInfo, $contextList = null, $contextDepth = null, $userVars = array()) {
169  $contextPaths = array();
170  $application =& Application::getApplication();
171 
172  if (!$contextList) {
173  $contextList = $application->getContextList();
174  }
175  if (!$contextDepth) {
176  $contextDepth = $application->getContextDepth();
177  }
178 
179  // Handle context depth 0
180  if (!$contextDepth) return $contextPaths;
181 
182  if ($isPathInfo) {
183  // Split the path info into its constituents. Save all non-context
184  // path info in $contextPaths[$contextDepth]
185  // by limiting the explode statement.
186  $contextPaths = explode('/', trim($urlInfo, '/'), $contextDepth + 1);
187  // Remove the part of the path info that is not relevant for context (if present)
188  unset($contextPaths[$contextDepth]);
189  } else {
190  // Retrieve context from url query string
191  foreach($contextList as $key => $contextName) {
192  $contextPaths[$key] = Core::_getUserVar($urlInfo, $contextName, $userVars);
193  }
194  }
195 
196  // Canonicalize and clean context paths
197  for($key = 0; $key < $contextDepth; $key++) {
198  $contextPaths[$key] = (
199  isset($contextPaths[$key]) && !empty($contextPaths[$key]) ?
200  $contextPaths[$key] : 'index'
201  );
202  $contextPaths[$key] = Core::cleanFileVar($contextPaths[$key]);
203  }
204 
205  return $contextPaths;
206  }
207 
219  function getPage($urlInfo, $isPathInfo, $userVars = array()) {
220  $page = Core::_getUrlComponents($urlInfo, $isPathInfo, 0, 'page', $userVars);
221  return Core::cleanFileVar(is_null($page) ? '' : $page);
222  }
223 
235  function getOp($urlInfo, $isPathInfo, $userVars = array()) {
236  $operation = Core::_getUrlComponents($urlInfo, $isPathInfo, 1, 'op', $userVars);
237  return Core::cleanFileVar(empty($operation) ? 'index' : $operation);
238  }
239 
252  function getArgs($urlInfo, $isPathInfo, $userVars = array()) {
253  return Core::_getUrlComponents($urlInfo, $isPathInfo, 2, 'path', $userVars);
254  }
255 
264  function removeBaseUrl($url) {
265  list($baseUrl, $contextPath) = Core::_getBaseUrlAndPath($url);
266 
267  if (!$baseUrl) return false;
268 
269  // Remove base url from url, if any.
270  $url = str_replace($baseUrl, '', $url);
271 
272  // If url doesn't have the entire protocol and host part,
273  // remove any possible base url path from url.
274  $baseUrlPath = parse_url($baseUrl, PHP_URL_PATH);
275  if ($baseUrlPath == $url) {
276  // Access to the base url, no context, the entire
277  // url is part of the base url and we can return empty.
278  $url = '';
279  } else {
280  // Handle case where index.php was removed by rewrite rules,
281  // and we have base url followed by the args.
282  if (strpos($url, $baseUrlPath . '?') === 0) {
283  $replacement = '?'; // Url path replacement.
284  $baseSystemEscapedPath = preg_quote($baseUrlPath . '?', '/');
285  } else {
286  $replacement = '/'; // Url path replacement.
287  $baseSystemEscapedPath = preg_quote($baseUrlPath . '/', '/');
288  }
289  $url = preg_replace('/^' . $baseSystemEscapedPath . '/', $replacement, $url);
290 
291  // Remove possible index.php page from url.
292  $url = str_replace('/index.php', '', $url);
293  }
294 
295  if ($contextPath) {
296  // We found the contextPath using the base_url
297  // config file settings. Check if the url starts
298  // with the context path, if not, apend it.
299  if (strpos($url, '/' . $contextPath) !== 0) {
300  $url = '/' . $contextPath . $url;
301  }
302  }
303 
304  // Remove any possible trailing slashes.
305  $url = rtrim($url, '/');
306 
307  return $url;
308  }
309 
318  function _getBaseUrlAndPath($url) {
319  $baseUrl = false;
320  $contextPath = false;
321 
322  // Check for override base url settings.
323  $contextBaseUrls =& Config::getContextBaseUrls();
324 
325  if (empty($contextBaseUrls)) {
326  $baseUrl = Config::getVar('general', 'base_url');
327  } else {
328  // Arrange them in length order, so we make sure
329  // we get the correct one, in case there's an overlaping
330  // of contexts, eg.:
331  // base_url[context1] = http://somesite.com/
332  // base_url[context2] = http://somesite.com/context2
333  $sortedBaseUrls = array_combine($contextBaseUrls, array_map('strlen', $contextBaseUrls));
334  arsort($sortedBaseUrls);
335 
336  foreach ($sortedBaseUrls as $workingBaseUrl => $baseUrlLength) {
337  $urlHost = parse_url($url, PHP_URL_HOST);
338  if (is_null($urlHost)) {
339  // Check the base url without the host part.
340  $baseUrlHost = parse_url($workingBaseUrl, PHP_URL_HOST);
341  if (is_null($baseUrlHost)) break;
342  $baseUrlToSearch = substr($workingBaseUrl, strpos($workingBaseUrl, $baseUrlHost) + strlen($baseUrlHost));
343  // Base url with only host part, add trailing slash
344  // so it can be checked below.
345  if (!$baseUrlToSearch) $baseUrlToSearch = '/';
346  } else {
347  $baseUrlToSearch = $workingBaseUrl;
348  }
349 
350  $baseUrlCheck = Core::_checkBaseUrl($baseUrlToSearch, $url);
351  if (is_null($baseUrlCheck)) {
352  // Can't decide. Stop searching.
353  break;
354  } else if ($baseUrlCheck === true) {
355  $contextPath = array_search($workingBaseUrl, $contextBaseUrls);
356  $baseUrl = $workingBaseUrl;
357  break;
358  }
359  }
360  }
361 
362  return array($baseUrl, $contextPath);
363  }
364 
377  function _checkBaseUrl($baseUrl, $url) {
378  // Check if both base url and url have host
379  // component or not.
380  $baseUrlHasHost = (boolean) parse_url($baseUrl, PHP_URL_HOST);
381  $urlHasHost = (boolean) parse_url($url, PHP_URL_HOST);
382  if ($baseUrlHasHost !== $urlHasHost) return false;
383 
384  $contextBaseUrls =& Config::getContextBaseUrls();
385 
386  // If the base url is found inside the passed url,
387  // then we might found the right context path.
388  if (strpos($url, $baseUrl) === 0) {
389  if (strpos($url, '/index.php') == strlen($baseUrl) - 1) {
390  // index.php appears right after the base url,
391  // no more possible paths.
392  return true;
393  } else {
394  // Still have to check if there is no other context
395  // base url that combined with it's context path is
396  // equal to this base url. If it exists, we can't
397  // tell which base url is contained in url.
398  foreach ($contextBaseUrls as $contextPath => $workingBaseUrl) {
399  $urlToCheck = $workingBaseUrl . '/' . $contextPath;
400  if (!$baseUrlHasHost) $urlToCheck = parse_url($urlToCheck, PHP_URL_PATH);
401  if ($baseUrl == $urlToCheck) {
402  return null;
403  }
404  }
405 
406  return true;
407  }
408  }
409 
410  return false;
411  }
412 
418  function _botFileListCacheMiss(&$cache) {
419  $id = $cache->getCacheId();
420  $botRegexps = array_filter(file($id),
421  array('Core', '_filterBotRegexps'));
422 
423  $cache->setEntireCache($botRegexps);
424  return $botRegexps;
425  }
426 
432  function _filterBotRegexps(&$regexp) {
433  $delimiter = '/';
434  $regexp = trim($regexp);
435  if (!empty($regexp) && $regexp[0] != '#') {
436  if(strpos($regexp, $delimiter) !== 0) {
437  // Make sure delimiters are in place.
438  $regexp = $delimiter . $regexp . $delimiter;
439  }
440  return true;
441  } else {
442  return false;
443  }
444  }
445 
453  function _getUserVar($url, $varName, $userVars = array()) {
454  $returner = null;
455  parse_str(parse_url($url, PHP_URL_QUERY), $userVarsFromUrl);
456  if (isset($userVarsFromUrl[$varName])) $returner = $userVarsFromUrl[$varName];
457 
458  if (is_null($returner)) {
459  // Try to retrieve from passed user vars, if any.
460  if (!empty($userVars) && isset($userVars[$varName])) {
461  $returner = $userVars[$varName];
462  }
463  }
464 
465  return $returner;
466  }
467 
479  function _getUrlComponents($urlInfo, $isPathInfo, $offset, $varName = '', $userVars = array()) {
480  $component = null;
481 
482  $isArrayComponent = false;
483  if ($varName == 'path') {
484  $isArrayComponent = true;
485  }
486  if ($isPathInfo) {
487  $application = Application::getApplication();
488  $contextDepth = $application->getContextDepth();
489 
490  $vars = explode('/', trim($urlInfo, '/'));
491  if (count($vars) > $contextDepth + $offset) {
492  if ($isArrayComponent) {
493  $component = array_slice($vars, $contextDepth + $offset);
494  for ($i=0, $count=count($component); $i<$count; $i++) {
495  $component[$i] = Core::cleanVar(get_magic_quotes_gpc() ? stripslashes($component[$i]) : $component[$i]);
496  }
497  } else {
498  $component = $vars[$contextDepth + $offset];
499  }
500  }
501  } else {
502  $component = Core::_getUserVar($urlInfo, $varName, $userVars);
503  }
504 
505  if ($isArrayComponent) {
506  if (empty($component)) $component = array();
507  elseif (!is_array($component)) $component = array($component);
508  }
509 
510  return $component;
511  }
512 }
513 
514 ?>
serverPHPOS()
Definition: Core.inc.php:111
regexp_match($pattern, $subject)
Definition: String.inc.php:325
html2utf($str)
Definition: String.inc.php:691
serverPHPVersion()
Definition: Core.inc.php:119
getOp($urlInfo, $isPathInfo, $userVars=array())
Definition: Core.inc.php:235
getContextPaths($urlInfo, $isPathInfo, $contextList=null, $contextDepth=null, $userVars=array())
Definition: Core.inc.php:168
utf8_normalize($str)
Definition: String.inc.php:582
_botFileListCacheMiss(&$cache)
Definition: Core.inc.php:418
Class containing system-wide functions.
Definition: Core.inc.php:23
& getContextBaseUrls()
Definition: Config.inc.php:93
getCurrentDate($ts=null)
Definition: Core.inc.php:94
_getUrlComponents($urlInfo, $isPathInfo, $offset, $varName= '', $userVars=array())
Definition: Core.inc.php:479
microtime()
Definition: Core.inc.php:102
isUserAgentBot($userAgent, $botRegexpsFile=USER_AGENTS_FILE)
Definition: Core.inc.php:138
regexp_replace($pattern, $replacement, $subject, $limit=-1)
Definition: String.inc.php:350
_filterBotRegexps(&$regexp)
Definition: Core.inc.php:432
utf8_is_valid($str)
Definition: String.inc.php:520
getVar($section, $key, $default=null)
Definition: Config.inc.php:34
utf8_strip_ascii_ctrl($str)
Definition: String.inc.php:572
cleanFileVar($var)
Definition: Core.inc.php:85
_checkBaseUrl($baseUrl, $url)
Definition: Core.inc.php:377
getBaseDir()
Definition: Core.inc.php:32
utf8_bad_strip($str)
Definition: String.inc.php:551
cleanVar($var)
Definition: Core.inc.php:49
getArgs($urlInfo, $isPathInfo, $userVars=array())
Definition: Core.inc.php:252
removeBaseUrl($url)
Definition: Core.inc.php:264
_getBaseUrlAndPath($url)
Definition: Core.inc.php:318
isWindows()
Definition: Core.inc.php:127
_getUserVar($url, $varName, $userVars=array())
Definition: Core.inc.php:453
getPage($urlInfo, $isPathInfo, $userVars=array())
Definition: Core.inc.php:219